From a54b57a380c8bdbd58ec5f478ddf758e0ac19954 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Fri, 24 Sep 2021 04:14:53 +0800 Subject: [PATCH] 2020092403 Signed-off-by: guduhanyan --- .../innerkits/include/time_service_client.h | 2 +- .../js/napi/system_time/src/js_systemtime.cpp | 8 +- .../js/napi/system_timer/src/system_timer.cpp | 4 +- services/time_manager/src/time_service.cpp | 77 ++++++++++--------- .../time_manager/src/time_service_stub.cpp | 3 +- services/time_manager/src/time_zone_info.cpp | 6 +- .../test/unittest/include/time_service_test.h | 4 +- .../test/unittest/include/timer_info_test.h | 4 +- services/timer/src/batch.cpp | 6 +- services/timer/src/timer_handler.cpp | 6 +- services/timer/src/timer_manager.cpp | 9 ++- utils/native/src/time_permission.cpp | 4 +- 12 files changed, 72 insertions(+), 61 deletions(-) diff --git a/interfaces/innerkits/include/time_service_client.h b/interfaces/innerkits/include/time_service_client.h index 535837c..2e223d8 100644 --- a/interfaces/innerkits/include/time_service_client.h +++ b/interfaces/innerkits/include/time_service_client.h @@ -27,7 +27,7 @@ namespace OHOS { namespace MiscServices { constexpr int64_t ERROR_OPREATION_FAILED = -1; -class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient{ +class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit TimeSaDeathRecipient(); ~TimeSaDeathRecipient() = default; diff --git a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp index 62b8bbf..78bd3c9 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -126,7 +126,11 @@ static napi_value JSSystemTimeSetTimeZone(napi_env env,napi_callback_info info) if (i == 0 && valueType == napi_string) { char timeZoneChars[MAX_TIME_ZONE_ID]; size_t timeZoneCharsSize; - if (napi_ok != napi_get_value_string_utf8(env,argv[i],timeZoneChars,MAX_TIME_ZONE_ID-1,&timeZoneCharsSize)){ + if (napi_ok != napi_get_value_string_utf8(env, + argv[i], + timeZoneChars, + MAX_TIME_ZONE_ID-1, + &timeZoneCharsSize)) { delete asyncContext; NAPI_ASSERT(env, false, "input para invalid"); } @@ -189,7 +193,7 @@ static napi_value JSSystemTimeSetTimeZone(napi_env env,napi_callback_info info) }, (void*)asyncContext, &asyncContext->work); - napi_queue_async_work(env,asyncContext->work); + napi_queue_async_work(env, asyncContext->work); return result; } diff --git a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp index 0c2f142..bb4e7e9 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -345,10 +345,10 @@ napi_value CreateTimer(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); std::shared_ptr iTimerInfoInstance = std::make_shared(); napi_ref callback = nullptr; - if (ParseParametersByCreateTimer(env, argv, argc, iTimerInfoInstance, callback) == nullptr){ + if (ParseParametersByCreateTimer(env, argv, argc, iTimerInfoInstance, callback) == nullptr) { return JSParaError(env, callback); } - AsyncCallbackInfoCreate *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCreate{ + AsyncCallbackInfoCreate *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCreate { .env = env, .asyncWork = nullptr, .iTimerInfoInstance = iTimerInfoInstance diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index a63e2e8..6b3811f 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -19,16 +19,17 @@ #include #include #include -#include -#include #include #include #include -#include -#include #include #include +#include +#include +#include +#include + #include "pthread.h" #include "time_service.h" #include "time_zone_info.h" @@ -71,7 +72,7 @@ TimeService::TimeService(int32_t systemAbilityId, bool runOnCreate) } TimeService::TimeService() - :state_(ServiceRunningState::STATE_NOT_START),rtc_id(get_wall_clock_rtc_id()) + :state_(ServiceRunningState::STATE_NOT_START), rtc_id(get_wall_clock_rtc_id()) { } @@ -79,9 +80,9 @@ TimeService::~TimeService() {}; sptr TimeService::GetInstance() { - if (instance_ == nullptr){ + if (instance_ == nullptr) { std::lock_guard autoLock(instanceLock_); - if (instance_ == nullptr){ + if (instance_ == nullptr) { instance_ = new TimeService; } } @@ -173,13 +174,13 @@ void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, T bool isRealtime = false; bool isWakeup = false; paras.flag = 0; - if ((type & TIMER_TYPE_REALTIME_MASK) > 0 ){ + if ((type & TIMER_TYPE_REALTIME_MASK) > 0 ) { isRealtime = true; } - if ((type & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0 ){ + if ((type & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0) { isWakeup = true; } - if ((type & TIMER_TYPE_EXACT_MASK) > 0){ + if ((type & TIMER_TYPE_EXACT_MASK) > 0) { paras.windowLength = 0; }else{ paras.windowLength = -1; @@ -230,7 +231,11 @@ uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval, } } return timerManagerHandler_->CreateTimer(paras.timerType, - paras.windowLength,paras.interval,paras.flag,callbackFunc,0); + paras.windowLength, + paras.interval, + paras.flag, + callbackFunc, + 0); } bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes) @@ -295,28 +300,28 @@ int32_t TimeService::SetTime(const int64_t time) return E_TIME_NO_PERMISSION; } TIME_HILOGI(TIME_MODULE_SERVICE, "Setting time of day to milliseconds: %{public}" PRId64 "", time); - if (time < 0 || time / 1000LL >= LONG_MAX) { + if (time < 0 || time/1000LL >= LONG_MAX) { TIME_HILOGE(TIME_MODULE_SERVICE, "input param error"); return E_TIME_PARAMETERS_INVALID; } struct timeval tv{}; - tv.tv_sec = (time_t) (time / MILLI_TO_BASE); - tv.tv_usec = (suseconds_t) ((time % MILLI_TO_BASE) * MILLI_TO_MICR); + tv.tv_sec = (time_t) (time/MILLI_TO_BASE); + tv.tv_usec = (suseconds_t)((time % MILLI_TO_BASE) * MILLI_TO_MICR); int result = settimeofday(&tv, NULL); - if (result < 0) { + if (result < 0){ TIME_HILOGE(TIME_MODULE_SERVICE, "settimeofday fail: %{public}d.",result); return E_TIME_DEAL_FAILED; } auto ret = set_rtc_time(tv.tv_sec); - if (ret < 0) { + if (ret < 0){ TIME_HILOGE(TIME_MODULE_SERVICE, "set rtc fail: %{public}d.", ret); return E_TIME_SET_RTC_FAILED; } int64_t currentTime = 0; GetWallTimeMs(currentTime); - if (timeServiceNotify_ != nullptr) { + if (timeServiceNotify_ != nullptr){ timeServiceNotify_->PublishTimeChanageEvents(currentTime); } @@ -324,12 +329,12 @@ int32_t TimeService::SetTime(const int64_t time) } int TimeService::set_rtc_time(time_t sec) { - struct rtc_time rtc {}; + struct rtc_time rtc{}; struct tm tm {}; struct tm *gmtime_res = nullptr; int fd = 0; int res; - if (rtc_id < 0) { + if (rtc_id < 0){ TIME_HILOGE(TIME_MODULE_SERVICE, "invalid rtc id: %{public}s:", strerror(ENODEV)); return -1; } @@ -354,8 +359,8 @@ int TimeService::set_rtc_time(time_t sec) { rtc.tm_wday = tm.tm_wday; rtc.tm_yday = tm.tm_yday; rtc.tm_isdst = tm.tm_isdst; - res = ioctl(fd, RTC_SET_TIME, &rtc); - if (res < 0) { + res = ioctl(fd, RTC_SET_TIME,&rtc); + if (res < 0){ TIME_HILOGE(TIME_MODULE_SERVICE, "ioctl RTC_SET_TIME failed: %{public}s", strerror(errno)); } }else{ @@ -374,9 +379,9 @@ bool TimeService::check_rtc(std::string rtc_path, uint64_t rtc_id_t) uint32_t hctosys; std::fstream file(hctosys_path.data(), std::ios_base::in); - if (file.is_open()) { + if (file.is_open()){ file >> hctosys; - } else { + } else{ TIME_HILOGE(TIME_MODULE_SERVICE, "failed to open %{public}s", hctosys_path.data()); return false; } @@ -395,16 +400,16 @@ int TimeService::get_wall_clock_rtc_id() struct dirent *dirent; std::string s = "rtc"; - while (errno = 0, - dirent = readdir(dir.get())) { + while (errno = 0, + dirent = readdir(dir.get())){ std::string name(dirent->d_name); - unsigned long rtc_id_t = 0 ; - auto index = name.find(s); - if (index == std::string::npos) { + unsigned long rtc_id_t = 0; + auto index = name.find(s); + if (index == std::string::npos){ continue; } else { - auto rtc_id_str = name.substr(index+s.length()); + auto rtc_id_str = name.substr(index + s.length()); rtc_id_t = std::stoul(rtc_id_str); } @@ -414,7 +419,7 @@ int TimeService::get_wall_clock_rtc_id() } } - if (errno == 0) { + if (errno == 0){ TIME_HILOGE(TIME_MODULE_SERVICE, "no wall clock rtc found"); }else{ TIME_HILOGE(TIME_MODULE_SERVICE, "failed to check rtc: %{public}s", strerror(errno)); @@ -456,7 +461,7 @@ int32_t TimeService::GetWallTimeMs(int64_t ×) { struct timespec tv{}; - if (GetTimeByClockid(CLOCK_REALTIME, &tv)) { + if (GetTimeByClockid(CLOCK_REALTIME, &tv)){ times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI; return ERR_OK; } @@ -468,7 +473,7 @@ int32_t TimeService::GetWallTimeNs(int64_t ×) { struct timespec tv{}; - if (GetTimeByClockid(CLOCK_REALTIME, &tv)) { + if (GetTimeByClockid(CLOCK_REALTIME, &tv)){ times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; return ERR_OK; } @@ -480,7 +485,7 @@ int32_t TimeService::GetBootTimeMs(int64_t ×) { struct timespec tv{}; - if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) { + if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)){ times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI; return ERR_OK; } @@ -492,7 +497,7 @@ int32_t TimeService::GetBootTimeNs(int64_t ×) { struct timespec tv{}; - if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) { + if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)){ times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; return ERR_OK; } @@ -504,7 +509,7 @@ int32_t TimeService::GetMonotonicTimeMs(int64_t ×) { struct timespec tv{}; - if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) { + if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)){ times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI; return ERR_OK; } @@ -516,7 +521,7 @@ int32_t TimeService::GetMonotonicTimeNs(int64_t ×) { struct timespec tv{}; - if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) { + if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)){ times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; return ERR_OK; } diff --git a/services/time_manager/src/time_service_stub.cpp b/services/time_manager/src/time_service_stub.cpp index fd2d7c7..12bdcff 100644 --- a/services/time_manager/src/time_service_stub.cpp +++ b/services/time_manager/src/time_service_stub.cpp @@ -59,7 +59,8 @@ int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Mes } pid_t p = IPCSkeleton::GetCallingPid(); pid_t p1 = IPCSkeleton::GetCallingUid(); - TIME_HILOGI(TIME_MODULE_SERVICE,"CallingPid = %{public}d,CallingUid = %{public}d,code = %{public}u",p,p1,code); + TIME_HILOGI(TIME_MODULE_SERVICE,"CallingPid = % {public}d,CallingUid = % {public}d,code = % {public}u", + p, p1, code); auto itFunc = memberFuncMap_.find(code); if (itFunc != memberFuncMap_.end()) { auto memberFunc = itFunc->second; diff --git a/services/time_manager/src/time_zone_info.cpp b/services/time_manager/src/time_zone_info.cpp index 71c9130..6c65bdb 100644 --- a/services/time_manager/src/time_zone_info.cpp +++ b/services/time_manager/src/time_zone_info.cpp @@ -119,8 +119,8 @@ bool TimeZoneInfo::SetOffsetToKernel(float offsetHour) struct timezone tz{}; tz.tz_minuteswest = static_cast(offsetHour * HOURS_TO_MINUTES); tz.tz_dsttime = 0; - TIME_HILOGD(TIME_MODULE_SERVICE,"settimeofday,Offset hours:%{public}f,Offset minutes:%{public}d", - offsetHour,tz.tz_minuteswest); + TIME_HILOGD(TIME_MODULE_SERVICE, "settimeofday, Offset hours % {public}f, Offset minutes % {public}d", + offsetHour, tz.tz_minuteswest); int result = settimeofday(NULL, &tz); if (result < 0) { TIME_HILOGE(TIME_MODULE_SERVICE,"settimeofday fail:%{public}d.",result); @@ -165,7 +165,7 @@ bool TimeZoneInfo::SaveTimezoneToFile(std::string timezoneId) bool TimeZoneInfo::GetOffsetById(const std::string timezoneId, float &offset) { auto itEntry = timezoneInfoMap_.find(timezoneId); - if (itEntry != timezoneInfoMap_.end()){ + if (itEntry != timezoneInfoMap_.end()) { auto zoneInfo = itEntry->second; offset = zoneInfo.utcOffsetHours; curTimezoneId_ = timezoneId; diff --git a/services/time_manager/test/unittest/include/time_service_test.h b/services/time_manager/test/unittest/include/time_service_test.h index 19838f3..454dc66 100644 --- a/services/time_manager/test/unittest/include/time_service_test.h +++ b/services/time_manager/test/unittest/include/time_service_test.h @@ -19,13 +19,13 @@ std::atomic g_data1(0); -void TimeOutCallback1() +void TimeOutCallback1(void) { g_data1 += 1; } std::atomic g_data2(0); -void TimeOutCallback2() +void TimeOutCallback2(void) { g_data2 += 1; } diff --git a/services/time_manager/test/unittest/include/timer_info_test.h b/services/time_manager/test/unittest/include/timer_info_test.h index 9852b5d..5a2183c 100644 --- a/services/time_manager/test/unittest/include/timer_info_test.h +++ b/services/time_manager/test/unittest/include/timer_info_test.h @@ -42,8 +42,8 @@ public: virtual void SetWantAgent(std::shared_ptr wantAgent) override; void SetCallbackInfo(const std::function &callBack); - private: - std::function callBack_ = nullptr; +private: + std::function callBack_ = nullptr; }; TimerInfoTest::TimerInfoTest() diff --git a/services/timer/src/batch.cpp b/services/timer/src/batch.cpp index be89e8c..968c3d8 100644 --- a/services/timer/src/batch.cpp +++ b/services/timer/src/batch.cpp @@ -57,7 +57,7 @@ bool Batch::Add (const std::shared_ptr &alarm) auto it = std::upper_bound(alarms_.begin(), alarms_.end(), alarm, - [](const std::shared_ptr &first, const std::shared_ptr &second){ + [](const std::shared_ptr &first, const std::shared_ptr &second) { return first->whenElapsed < second->whenElapsed; }); alarms_.insert (it, alarm); // 根据Alarm.when_elapsed从小到大排列 @@ -117,8 +117,8 @@ bool Batch::Remove (std::function predicate) bool Batch::HasPackage (const std::string &package_name) { return std::find_if(alarms_.begin(), - alarms_.end(), - [package_name](const std::shared_ptr &alarm){ + alarms_.end(), + [package_name](const std::shared_ptr &alarm) { return alarm->Matches(package_name); }) != alarms_.end(); } diff --git a/services/timer/src/timer_handler.cpp b/services/timer/src/timer_handler.cpp index ef6ab1b..9327946 100644 --- a/services/timer/src/timer_handler.cpp +++ b/services/timer/src/timer_handler.cpp @@ -95,7 +95,7 @@ TimerHandler::TimerHandler(const TimerFds &fds, int epollfd) TimerHandler::~TimerHandler() { - for (auto fd : fds_){ + for (auto fd : fds_) { epoll_ctl(epollFd_, EPOLL_CTL_DEL, fd, nullptr); close(fd); } @@ -126,12 +126,12 @@ uint32_t TimerHandler::WaitForAlarm() } uint32_t result = 0; - for(int i = 0;i < nevents;i++){ + for(int i = 0;i < nevents;i++) { uint32_t alarm_idx = events[i].data.u32; uint64_t unused; ssize_t err = read(fds_[alarm_idx], &unused, sizeof(unused)); if (err < 0) { - if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED){ + if (alarm_idx == ALARM_TYPE_COUNT && errno == ECANCELED) { result |= ALARM_TIME_CHANGE_MASK; } else { return err; diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 434caf5..e2e3032 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -343,7 +343,7 @@ void TimerManager::TimerLooper() if (lastTimeChangeClockTime == system_clock::time_point::min() || nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND)) - || nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))){ + || nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))) { TIME_HILOGI(TIME_MODULE_SERVICE, "Time changed notification from kernel; rebatching"); ReBatchAllTimers(); lastTimeChangeClockTime_ = nowRtc; @@ -398,7 +398,7 @@ bool TimerManager::TriggerTimersLocked(std::vector> & auto alarm = batch->Get(i); alarm->count = 1; triggerList.push_back(alarm); - if (alarm->repeatInterval > milliseconds::zero()){ + if (alarm->repeatInterval > milliseconds::zero()) { alarm->count += duration_cast(nowElapsed- alarm->expectedWhenElapsed) / alarm->repeatInterval; auto delta = alarm->count * alarm->repeatInterval; @@ -414,7 +414,7 @@ bool TimerManager::TriggerTimersLocked(std::vector> & } std::sort(triggerList.begin(), triggerList.end(), - [](const std::shared_ptr &l, const std::shared_ptr &r){ + [](const std::shared_ptr &l, const std::shared_ptr &r) { return l->whenElapsed < r->whenElapsed; }); @@ -516,7 +516,8 @@ bool AddBatchLocked(std::vector> &list, const std::shared return it == list.begin(); } -steady_clock::time_point MaxTriggerTime(steady_clock::time_point now,steady_clock::time_point triggerAtTime, +steady_clock::time_point MaxTriggerTime(steady_clock::time_point now, + steady_clock::time_point triggerAtTime, milliseconds interval) { milliseconds futurity = (interval == milliseconds::zero()) ? diff --git a/utils/native/src/time_permission.cpp b/utils/native/src/time_permission.cpp index 946e045..6808662 100644 --- a/utils/native/src/time_permission.cpp +++ b/utils/native/src/time_permission.cpp @@ -50,8 +50,8 @@ bool TimePermission::CheckCallingPermission(int32_t uid, std::string permName) return true; } auto userId = uid / UID_TO_USERID; - TIME_HILOGI(TIME_MODULE_COMMON,"VerifyPermission bundleName:%{public}s,permission:%{public}s", - bundleName.c_str(),permName.c_str()); + TIME_HILOGI(TIME_MODULE_COMMON,"VerifyPermission bundleName: % {public}s,permission: % {public}s", + bundleName.c_str(), permName.c_str()); return MockPermission::VerifyPermission(bundleName, permName, userId); }