From 6e93876d293b336dc8d2492ea2bd86f99efb6d74 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Tue, 28 Sep 2021 21:46:34 +0800 Subject: [PATCH 1/4] 20210928LTS04 Signed-off-by: guduhanyan --- .../kits/js/napi/system_timer/src/system_timer.cpp | 8 ++++---- services/time_manager/src/time_service.cpp | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) 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 d4ccf86..d0bd3be 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -342,7 +342,7 @@ void PaddingAsyncCallbackInfoIsByCreateTimer( napi_value CreateTimer(napi_env env, napi_callback_info info) { size_t argc = CREATE_MAX_PARA; - napi_value argv[CREATE_MAX_PARA]; + napi_value argv[CREATE_MAX_PARA] = {0}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); std::shared_ptr iTimerInfoInstance = std::make_shared(); @@ -444,7 +444,7 @@ void PaddingAsyncCallbackInfoIsByStartTimer( napi_value StartTimer(napi_env env, napi_callback_info info) { size_t argc = START_MAX_PARA; - napi_value argv[START_MAX_PARA]; + napi_value argv[START_MAX_PARA] = {0}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); @@ -552,7 +552,7 @@ void PaddingAsyncCallbackInfoIsByStopTimer( napi_value StopTimer(napi_env env, napi_callback_info info) { size_t argc = STOP_MAX_PARA; - napi_value argv[STOP_MAX_PARA]; + napi_value argv[STOP_MAX_PARA] = {0}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); @@ -657,7 +657,7 @@ void PaddingAsyncCallbackInfoIsByDestroyTimer( napi_value DestroyTimer(napi_env env, napi_callback_info info) { size_t argc = DESTROY_MAX_PARA; - napi_value argv[DESTROY_MAX_PARA]; + napi_value argv[DESTROY_MAX_PARA] = {0}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index 84fd622..edc4f7d 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -173,14 +173,15 @@ void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, T { bool isRealtime = false; bool isWakeup = false; + auto uIntType = static_cast(type); paras.flag = 0; - if ((type & TIMER_TYPE_REALTIME_MASK) > 0 ) { + if ((uIntType & TIMER_TYPE_REALTIME_MASK) > 0) { isRealtime = true; } - if ((type & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0) { + if ((uIntType & TIMER_TYPE_REALTIME_WAKEUP_MASK) > 0) { isWakeup = true; } - if ((type & TIMER_TYPE_EXACT_MASK) > 0) { + if ((uIntType & TIMER_TYPE_EXACT_MASK) > 0) { paras.windowLength = 0; }else{ paras.windowLength = -1; @@ -341,7 +342,8 @@ int TimeService::set_rtc_time(time_t sec) { strs << "/dev/rtc" << rtc_id; auto rtc_dev = strs.str(); TIME_HILOGI(TIME_MODULE_SERVICE, "rtc_dev : %{public}s:", rtc_dev.data()); - fd = open(rtc_dev.data(), O_RDWR); + auto rtc_data = rtc_dev.data(); + fd = open(rtc_data, O_RDWR); if (fd < 0) { TIME_HILOGE(TIME_MODULE_SERVICE, "open failed %{public}s: %{public}s", rtc_dev.data(), strerror(errno)); return -1; From 7ccc412b730784216095de6660d2db226359313f Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 29 Sep 2021 12:31:38 +0800 Subject: [PATCH 2/4] 20210929LTS00 Signed-off-by: guduhanyan --- interfaces/innerkits/include/itimer_info.h | 1 - .../napi/system_time/include/js_systemtime.h | 10 +-- .../js/napi/system_time/src/js_systemtime.cpp | 25 +++--- .../js/napi/system_timer/include/timer_init.h | 1 - .../js/napi/system_timer/src/system_timer.cpp | 61 +++++++------ .../js/napi/system_timer/src/timer_init.cpp | 1 - services/time_manager/src/time_service.cpp | 39 ++++---- .../time_manager/src/time_service_client.cpp | 2 - .../time_manager/src/time_service_notify.cpp | 6 +- .../time_manager/src/time_service_proxy.cpp | 30 +++---- .../time_manager/src/time_service_stub.cpp | 4 +- services/time_manager/src/time_zone_info.cpp | 6 +- services/time_manager/src/timer_call_back.cpp | 5 +- .../src/timer_call_back_proxy.cpp | 1 - .../time_manager/src/timer_call_back_stub.cpp | 1 - services/timer/include/batch.h | 28 +++--- services/timer/include/timer_info.h | 22 ++--- services/timer/include/timer_manager.h | 60 ++++++------- .../timer/include/timer_manager_interface.h | 12 +-- services/timer/src/batch.cpp | 84 ++++++++--------- services/timer/src/timer_handler.cpp | 2 +- services/timer/src/timer_info.cpp | 10 +-- services/timer/src/timer_manager.cpp | 90 ++++++++++--------- 23 files changed, 257 insertions(+), 244 deletions(-) diff --git a/interfaces/innerkits/include/itimer_info.h b/interfaces/innerkits/include/itimer_info.h index 2c9372b..eed70e3 100644 --- a/interfaces/innerkits/include/itimer_info.h +++ b/interfaces/innerkits/include/itimer_info.h @@ -73,7 +73,6 @@ public: virtual void SetWantAgent(std::shared_ptr wantAgent) = 0; virtual void OnTrigger() = 0; }; - } // MiscServices } // OHOS diff --git a/interfaces/kits/js/napi/system_time/include/js_systemtime.h b/interfaces/kits/js/napi/system_time/include/js_systemtime.h index fe0110e..b92e8bb 100644 --- a/interfaces/kits/js/napi/system_time/include/js_systemtime.h +++ b/interfaces/kits/js/napi/system_time/include/js_systemtime.h @@ -43,12 +43,12 @@ const int ARGS_TWO = 2; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data) typedef struct AsyncContext { - napi_env env; - napi_async_work work; + napi_env env = nullptr; + napi_async_work work = nullptr; int64_t time; std::string timeZone; - napi_deferred deferred; - napi_ref callbackRef; + napi_deferred deferred = nullptr; + napi_ref callbackRef = nullptr; bool isCallback = false; bool isOK = false; int errorCode = NO_ERROR; @@ -56,7 +56,7 @@ typedef struct AsyncContext { struct TimeCallbackPromiseInfo { napi_ref callback = nullptr; - napi_deferred deferred; + napi_deferred deferred = nullptr; bool isCallback = false; int errorCode = NO_ERROR; }; 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 e7c9e14..dd49cb1 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -110,7 +110,9 @@ napi_value JSSystemTimeSetTime(napi_env env, napi_callback_info info) napi_value resource = nullptr; napi_create_string_utf8(env, "JSSystemTimeSetTime", NAPI_AUTO_LENGTH, &resource); - napi_create_async_work(env, nullptr, resource,[](napi_env env, void* data) { + napi_create_async_work(env, + nullptr, + resource,[](napi_env env, void* data) { AsyncContext* asyncContext = (AsyncContext*)data; asyncContext->isOK = TimeServiceClient::GetInstance()->SetTime(asyncContext->time); }, @@ -136,12 +138,13 @@ napi_value JSSystemTimeSetTime(napi_env env, napi_callback_info info) asyncContext = nullptr; } }, - (void*)asyncContext, &asyncContext->work); + (void*)asyncContext, + &asyncContext->work); napi_queue_async_work(env, asyncContext->work); return result; } -napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) +napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) { TIME_HILOGI(TIME_MODULE_JS_NAPI, "JSSystemTimeSetTimeZone start"); GET_PARAMS(env, info, TWO_PARAMETERS); @@ -157,12 +160,12 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) 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)) { - delete asyncContext; - NAPI_ASSERT(env, false, "input para invalid"); + argv[i], + timeZoneChars, + MAX_TIME_ZONE_ID - 1, + &timeZoneCharsSize)) { + delete asyncContext; + NAPI_ASSERT(env, false, "input para invalid"); } std::string timeZoneStr(timeZoneChars, timeZoneCharsSize); asyncContext->timeZone = timeZoneStr; @@ -184,7 +187,9 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) } napi_value resource = nullptr; napi_create_string_utf8(env, "JSSystemTimeSetTimeZone", NAPI_AUTO_LENGTH, &resource); - napi_create_async_work(env, nullptr, resource, + napi_create_async_work(env, + nullptr, + resource, [](napi_env env, void* data) { AsyncContext* asyncContext = (AsyncContext*)data; asyncContext->isOK = TimeServiceClient::GetInstance()->SetTimeZone(asyncContext->timeZone); diff --git a/interfaces/kits/js/napi/system_timer/include/timer_init.h b/interfaces/kits/js/napi/system_timer/include/timer_init.h index 27673ef..bf49d53 100644 --- a/interfaces/kits/js/napi/system_timer/include/timer_init.h +++ b/interfaces/kits/js/napi/system_timer/include/timer_init.h @@ -21,7 +21,6 @@ namespace OHOS { namespace MiscServicesNapi { - } // OHOS } // MiscServicesNapi #endif // TIMER_INIT_H \ No newline at end of file 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 d0bd3be..b6594ae 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -38,32 +38,32 @@ const int PARAM1 = 1; struct CallbackPromiseInfo { napi_ref callback = nullptr; - napi_deferred deferred; + napi_deferred deferred = nullptr; bool isCallback = false; int errorCode = 0; }; struct ReceiveDataWorker { - napi_env env; + napi_env env = nullptr; napi_ref ref = 0; }; struct AsyncCallbackInfoCreate { - napi_env env; - napi_async_work asyncWork; + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; napi_ref callback = nullptr; - napi_deferred deferred; - std::shared_ptr iTimerInfoInstance; + napi_deferred deferred = nullptr; + std::shared_ptr iTimerInfoInstance = nullptr; uint64_t timerId = 0; bool isCallback = false; int errorCode = NO_ERROR; }; struct AsyncCallbackInfoStart { - napi_env env; - napi_async_work asyncWork; + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; napi_ref callback = nullptr; - napi_deferred deferred; + napi_deferred deferred = nullptr; uint64_t timerId = 0; uint64_t triggerTime = 0; bool isOK = false; @@ -72,10 +72,10 @@ struct AsyncCallbackInfoStart { }; struct AsyncCallbackInfoStop { - napi_env env; - napi_async_work asyncWork; + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; napi_ref callback = nullptr; - napi_deferred deferred; + napi_deferred deferred = nullptr; uint64_t timerId = 0; bool isOK = false; bool isCallback = false; @@ -83,10 +83,10 @@ struct AsyncCallbackInfoStop { }; struct AsyncCallbackInfoDestroy { - napi_env env; - napi_async_work asyncWork; + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; napi_ref callback = nullptr; - napi_deferred deferred; + napi_deferred deferred = nullptr; uint64_t timerId = 0; bool isOK = false; bool isCallback = false; @@ -198,9 +198,9 @@ void ITimerInfoInstance::OnTrigger() return; } - SetCallback(dataWorkerData->env, - dataWorkerData->ref, - NO_ERROR, + SetCallback(dataWorkerData->env, + dataWorkerData->ref, + NO_ERROR, NapiGetNull(dataWorkerData->env)); delete dataWorkerData; dataWorkerData = nullptr; @@ -283,7 +283,7 @@ napi_value GetTimerOptions(const napi_env &env, const napi_value &value, if (wantAgent == nullptr) { return nullptr; } - std::shared_ptr sWantAgent = + std::shared_ptr sWantAgent = std::make_shared(*wantAgent); iTimerInfoInstance->SetWantAgent(sWantAgent); } @@ -427,8 +427,10 @@ napi_value ParseParametersByStartTimer(const napi_env &env, const napi_value (&a return NapiGetNull(env); } -void PaddingAsyncCallbackInfoIsByStartTimer( - const napi_env &env, AsyncCallbackInfoStart *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) +void PaddingAsyncCallbackInfoIsByStartTimer(const napi_env &env, + AsyncCallbackInfoStart *&asynccallbackinfo, + const napi_ref &callback, + napi_value &promise) { if (callback) { asynccallbackinfo->callback = callback; @@ -455,8 +457,10 @@ napi_value StartTimer(napi_env env, napi_callback_info info) return JSParaError(env, callback); } - AsyncCallbackInfoStart *asynccallbackinfo = new (std::nothrow) - AsyncCallbackInfoStart{.env = env, .asyncWork = nullptr, .timerId = timerId, .triggerTime = triggerTime}; + AsyncCallbackInfoStart *asynccallbackinfo = new (std::nothrow)AsyncCallbackInfoStart{.env = env, + .asyncWork = nullptr, + .timerId = timerId, + .triggerTime = triggerTime}; if (!asynccallbackinfo) { return JSParaError(env, callback); } @@ -535,8 +539,10 @@ napi_value ParseParametersByStopTimer(const napi_env &env, const napi_value (&ar return NapiGetNull(env); } -void PaddingAsyncCallbackInfoIsByStopTimer( - const napi_env &env, AsyncCallbackInfoStop *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) +void PaddingAsyncCallbackInfoIsByStopTimer(const napi_env &env, + AsyncCallbackInfoStop *&asynccallbackinfo, + const napi_ref &callback, + napi_value &promise) { if (callback) { asynccallbackinfo->callback = callback; @@ -562,8 +568,9 @@ napi_value StopTimer(napi_env env, napi_callback_info info) return JSParaError(env, callback); } - AsyncCallbackInfoStop *asynccallbackinfo = - new (std::nothrow) AsyncCallbackInfoStop{.env = env, .asyncWork = nullptr, .timerId = timerId}; + AsyncCallbackInfoStop *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoStop{.env = env, + .asyncWork = nullptr, + .timerId = timerId}; if (!asynccallbackinfo) { return JSParaError(env, callback); } diff --git a/interfaces/kits/js/napi/system_timer/src/timer_init.cpp b/interfaces/kits/js/napi/system_timer/src/timer_init.cpp index 75e075f..e7c23f2 100644 --- a/interfaces/kits/js/napi/system_timer/src/timer_init.cpp +++ b/interfaces/kits/js/napi/system_timer/src/timer_init.cpp @@ -43,7 +43,6 @@ static napi_module _module = { .nm_modname = "systemtimer", .nm_priv = ((void *)0), .reserved = {0} - }; /* diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index edc4f7d..0602a96 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -50,7 +50,7 @@ static const std::int32_t INIT_INTERVAL = 10000L; static const uint32_t TIMER_TYPE_REALTIME_MASK = 1 << 0; static const uint32_t TIMER_TYPE_REALTIME_WAKEUP_MASK = 1 << 1; static const uint32_t TIMER_TYPE_EXACT_MASK = 1 << 2; - +constexpr int MIN_TRIGGER_TIMES = 5000; constexpr int MILLI_TO_MICR = MICR_TO_BASE / MILLI_TO_BASE; constexpr int NANO_TO_MILLI = NANO_TO_BASE / MILLI_TO_BASE; } @@ -169,6 +169,7 @@ void TimeService::InitTimerHandler() } timerManagerHandler_ = TimerManager::Create(); } + void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, TimerPara ¶s) { bool isRealtime = false; @@ -183,7 +184,7 @@ void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, T } if ((uIntType & TIMER_TYPE_EXACT_MASK) > 0) { paras.windowLength = 0; - }else{ + } else { paras.windowLength = -1; } @@ -241,8 +242,8 @@ uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval, bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes) { - uint64_t triggerTimesIn = 5000; - triggerTimesIn = (triggerTimes < 5000) ? 5000 : triggerTimes; + uint64_t triggerTimesIn; + triggerTimesIn = (triggerTimes < MIN_TRIGGER_TIMES) ? MIN_TRIGGER_TIMES : triggerTimes; if (timerManagerHandler_ == nullptr) { TIME_HILOGE(TIME_MODULE_SERVICE, "Timer manager nullptr."); timerManagerHandler_ = TimerManager::Create(); @@ -305,13 +306,13 @@ int32_t TimeService::SetTime(const int64_t time) TIME_HILOGE(TIME_MODULE_SERVICE, "input param error"); return E_TIME_PARAMETERS_INVALID; } - struct timeval tv{}; + struct timeval tv {}; 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) { - TIME_HILOGE(TIME_MODULE_SERVICE, "settimeofday fail: %{public}d.",result); + TIME_HILOGE(TIME_MODULE_SERVICE, "settimeofday fail: %{public}d.", result); return E_TIME_DEAL_FAILED; } auto ret = set_rtc_time(tv.tv_sec); @@ -329,7 +330,7 @@ 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; @@ -364,7 +365,7 @@ int TimeService::set_rtc_time(time_t sec) { if (res < 0) { TIME_HILOGE(TIME_MODULE_SERVICE, "ioctl RTC_SET_TIME failed: %{public}s", strerror(errno)); } - }else{ + } else { TIME_HILOGE(TIME_MODULE_SERVICE, "convert rtc time failed: %{public}s", strerror(errno)); res = -1; } @@ -382,7 +383,7 @@ bool TimeService::check_rtc(std::string rtc_path, uint64_t rtc_id_t) std::fstream file(hctosys_path.data(), std::ios_base::in); if (file.is_open()) { file >> hctosys; - } else{ + } else { TIME_HILOGE(TIME_MODULE_SERVICE, "failed to open %{public}s", hctosys_path.data()); return false; } @@ -422,7 +423,7 @@ int TimeService::get_wall_clock_rtc_id() if (errno == 0) { TIME_HILOGE(TIME_MODULE_SERVICE, "no wall clock rtc found"); - }else{ + } else { TIME_HILOGE(TIME_MODULE_SERVICE, "failed to check rtc: %{public}s", strerror(errno)); } return -1; @@ -460,7 +461,7 @@ int32_t TimeService::GetTimeZone(std::string &timeZoneId) int32_t TimeService::GetWallTimeMs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; if (GetTimeByClockid(CLOCK_REALTIME, &tv)) { times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI; @@ -471,7 +472,7 @@ int32_t TimeService::GetWallTimeMs(int64_t ×) int32_t TimeService::GetWallTimeNs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; if (GetTimeByClockid(CLOCK_REALTIME, &tv)) { times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; @@ -482,7 +483,7 @@ int32_t TimeService::GetWallTimeNs(int64_t ×) int32_t TimeService::GetBootTimeMs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) { times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI; @@ -493,7 +494,7 @@ int32_t TimeService::GetBootTimeMs(int64_t ×) int32_t TimeService::GetBootTimeNs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; if (GetTimeByClockid(CLOCK_BOOTTIME, &tv)) { times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; @@ -504,7 +505,7 @@ int32_t TimeService::GetBootTimeNs(int64_t ×) int32_t TimeService::GetMonotonicTimeMs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) { times = tv.tv_sec * MILLI_TO_BASE + tv.tv_nsec / NANO_TO_MILLI; @@ -515,7 +516,7 @@ int32_t TimeService::GetMonotonicTimeMs(int64_t ×) int32_t TimeService::GetMonotonicTimeNs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; if (GetTimeByClockid(CLOCK_MONOTONIC, &tv)) { times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; @@ -526,7 +527,7 @@ int32_t TimeService::GetMonotonicTimeNs(int64_t ×) int32_t TimeService::GetThreadTimeMs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; int ret; clockid_t cid; ret = pthread_getcpuclockid(pthread_self(), &cid); @@ -543,14 +544,14 @@ int32_t TimeService::GetThreadTimeMs(int64_t ×) int32_t TimeService::GetThreadTimeNs(int64_t ×) { - struct timespec tv{}; + struct timespec tv {}; int ret; clockid_t cid; ret = pthread_getcpuclockid(pthread_self(), &cid); if (ret != 0) { return E_TIME_PARAMETERS_INVALID; } - + if (GetTimeByClockid(cid, &tv)) { times = tv.tv_sec * NANO_TO_BASE + tv.tv_nsec; return ERR_OK; diff --git a/services/time_manager/src/time_service_client.cpp b/services/time_manager/src/time_service_client.cpp index 98a1e71..9eb833b 100644 --- a/services/time_manager/src/time_service_client.cpp +++ b/services/time_manager/src/time_service_client.cpp @@ -140,14 +140,12 @@ uint64_t TimeServiceClient::CreateTimer(std::shared_ptr TimerOptions TimerOptions->repeat, TimerOptions->interval, timerCallbackInfoObject); - if (timerId == 0) { TIME_HILOGE(TIME_MODULE_CLIENT, "Create timer failed"); return 0; } TIME_HILOGI(TIME_MODULE_SERVICE, "CreateTimer id: %{public}" PRId64 "", timerId); auto ret = TimerCallback::GetInstance()->InsertTimerCallbackInfo(timerId, TimerOptions); - if (!ret) { return 0; } diff --git a/services/time_manager/src/time_service_notify.cpp b/services/time_manager/src/time_service_notify.cpp index 3985977..a228ba5 100644 --- a/services/time_manager/src/time_service_notify.cpp +++ b/services/time_manager/src/time_service_notify.cpp @@ -23,8 +23,8 @@ using namespace OHOS::AAFwk; using namespace OHOS::EventFwk; -namespace OHOS{ -namespace MiscServices{ +namespace OHOS { +namespace MiscServices { void TimeServiceNotify::RegisterPublishEvents() { if (publishInfo_ != nullptr) { @@ -61,7 +61,5 @@ void TimeServiceNotify::PublishTimeZoneChangeEvents(int64_t eventTime) { PublishEvents(eventTime, timeZoneChangeWant_); } - - } // MiscService } // OHOS diff --git a/services/time_manager/src/time_service_proxy.cpp b/services/time_manager/src/time_service_proxy.cpp index a185d68..1d69de8 100644 --- a/services/time_manager/src/time_service_proxy.cpp +++ b/services/time_manager/src/time_service_proxy.cpp @@ -42,7 +42,7 @@ int32_t TimeServiceProxy::SetTime(const int64_t time) } int32_t result = Remote()->SendRequest(SET_TIME, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "SetTime failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "SetTime failed, error code is: %{public}d", result); return result; } return result; @@ -79,7 +79,7 @@ uint64_t TimeServiceProxy::CreateTimer(int32_t type, bool repeat, uint64_t inter } int32_t result = Remote()->SendRequest(CREATE_TIMER, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "CreateTimer failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "CreateTimer failed, error code is: %{public}d", result); return 0; } auto TimerId = reply.ReadUint64(); @@ -108,7 +108,7 @@ bool TimeServiceProxy::StartTimer(uint64_t timerId, uint64_t triggerTimes) } int32_t result = Remote()->SendRequest(START_TIMER, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "Start failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "Start failed, error code is: %{public}d", result); return false; } return true; @@ -130,7 +130,7 @@ bool TimeServiceProxy::StopTimer(uint64_t timerId) } int32_t result = Remote()->SendRequest(STOP_TIMER, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "Stop failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "Stop failed, error code is: %{public}d", result); return false; } @@ -152,7 +152,7 @@ bool TimeServiceProxy::DestroyTimer(uint64_t timerId) } int32_t result = Remote()->SendRequest(DESTORY_TIMER, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "failed, error code is: %{public}d", result); return false; } @@ -176,7 +176,7 @@ int32_t TimeServiceProxy::SetTimeZone(const std::string timezoneId) int32_t result = Remote()->SendRequest(SET_TIME_ZONE, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "SetTimeZone failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "SetTimeZone failed, error code is: %{public}d", result); return result; } return result; @@ -194,7 +194,7 @@ int32_t TimeServiceProxy::GetTimeZone(std::string &timezoneId) int32_t result = Remote()->SendRequest(GET_TIME_ZONE, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetTimeZone failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetTimeZone failed, error code is: %{public}d", result); return result; } timezoneId = reply.ReadString(); @@ -213,7 +213,7 @@ int32_t TimeServiceProxy::GetWallTimeMs(int64_t ×) int32_t result = Remote()->SendRequest(GET_WALL_TIME_MILLI, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeMs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeMs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -232,7 +232,7 @@ int32_t TimeServiceProxy::GetWallTimeNs(int64_t ×) int32_t result = Remote()->SendRequest(GET_WALL_TIME_NANO, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeNs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetWallTimeNs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -251,7 +251,7 @@ int32_t TimeServiceProxy::GetBootTimeMs(int64_t ×) int32_t result = Remote()->SendRequest(GET_BOOT_TIME_MILLI, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeMs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeMs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -270,7 +270,7 @@ int32_t TimeServiceProxy::GetBootTimeNs(int64_t ×) int32_t result = Remote()->SendRequest(GET_BOOT_TIME_MILLI, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeNs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetBootTimeNs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -289,7 +289,7 @@ int32_t TimeServiceProxy::GetMonotonicTimeMs(int64_t ×) int32_t result = Remote()->SendRequest(GET_MONO_TIME_MILLI, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeMs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeMs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -308,7 +308,7 @@ int32_t TimeServiceProxy::GetMonotonicTimeNs(int64_t ×) int32_t result = Remote()->SendRequest(GET_MONO_TIME_NANO, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeNs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetMonotonicTimeNs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -327,7 +327,7 @@ int32_t TimeServiceProxy::GetThreadTimeMs(int64_t ×) int32_t result = Remote()->SendRequest(GET_THREAD_TIME_MILLI, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeMs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeMs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); @@ -346,7 +346,7 @@ int32_t TimeServiceProxy::GetThreadTimeNs(int64_t ×) int32_t result = Remote()->SendRequest(GET_THREAD_TIME_NANO, data, reply, option); if (result != ERR_NONE) { - TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeNs failed, error code is: %{public}d",result); + TIME_HILOGE(TIME_MODULE_CLIENT, "GetThreadTimeNs failed, error code is: %{public}d", result); return result; } times = reply.ReadInt64(); diff --git a/services/time_manager/src/time_service_stub.cpp b/services/time_manager/src/time_service_stub.cpp index 4ef5fbb..852b1aa 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; @@ -263,7 +264,6 @@ int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply) } TIME_HILOGI(TIME_MODULE_SERVICE, "end."); return ERR_OK; - } int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply) diff --git a/services/time_manager/src/time_zone_info.cpp b/services/time_manager/src/time_zone_info.cpp index b213852..8f48594 100644 --- a/services/time_manager/src/time_zone_info.cpp +++ b/services/time_manager/src/time_zone_info.cpp @@ -16,8 +16,8 @@ #include "time_zone_info.h" #include "time_file_utils.h" -namespace OHOS{ -namespace MiscServices{ +namespace OHOS { +namespace MiscServices { namespace { const std::string TIMEZONE_FILE_PATH = "/data/misc/zoneinfo/timezone.json"; static const int HOURS_TO_MINUTES = 60; @@ -149,7 +149,7 @@ bool TimeZoneInfo::SetOffsetToKernel(float offsetHour) 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); + TIME_HILOGE(TIME_MODULE_SERVICE, "settimeofday fail: %{public}d.", result); return false; } return true; diff --git a/services/time_manager/src/timer_call_back.cpp b/services/time_manager/src/timer_call_back.cpp index 784c5d1..b84d89a 100644 --- a/services/time_manager/src/timer_call_back.cpp +++ b/services/time_manager/src/timer_call_back.cpp @@ -17,6 +17,9 @@ namespace OHOS { namespace MiscServices { +namespace { + const int WANTAGENT_CODE_ELEVEN = 11; +} std::mutex TimerCallback::instanceLock_; sptr TimerCallback::instance_; @@ -92,7 +95,7 @@ void TimerCallback::NotifyTimer(const uint64_t timerId) std::shared_ptr want = Notification::WantAgent::WantAgentHelper::GetWant(it->second->wantAgent); - OHOS::Notification::WantAgent::TriggerInfo paramsInfo("", nullptr, want, 11); + OHOS::Notification::WantAgent::TriggerInfo paramsInfo("", nullptr, want, WANTAGENT_CODE_ELEVEN); Notification::WantAgent::WantAgentHelper::TriggerWantAgent(context, it->second->wantAgent, nullptr, paramsInfo); } diff --git a/services/time_manager/src/timer_call_back_proxy.cpp b/services/time_manager/src/timer_call_back_proxy.cpp index 33d9f33..fb55f69 100644 --- a/services/time_manager/src/timer_call_back_proxy.cpp +++ b/services/time_manager/src/timer_call_back_proxy.cpp @@ -55,6 +55,5 @@ void TimerCallbackProxy::NotifyTimer(const uint64_t timerId) } TIME_HILOGI(TIME_MODULE_CLIENT, "end."); } - } // namespace MiscServices } // namespace OHOS diff --git a/services/time_manager/src/timer_call_back_stub.cpp b/services/time_manager/src/timer_call_back_stub.cpp index 1a27607..70eef1d 100644 --- a/services/time_manager/src/timer_call_back_stub.cpp +++ b/services/time_manager/src/timer_call_back_stub.cpp @@ -47,6 +47,5 @@ int TimerCallbackStub::OnTriggerStub(MessageParcel& data) TIME_HILOGD(TIME_MODULE_SERVICE, "end."); return ERR_OK; } - } // namespace MiscServices } // namespace OHOS diff --git a/services/timer/include/batch.h b/services/timer/include/batch.h index a9a0f46..424a2ee 100644 --- a/services/timer/include/batch.h +++ b/services/timer/include/batch.h @@ -26,21 +26,21 @@ namespace OHOS { namespace MiscServices { class Batch { public: - Batch (); - explicit Batch (const TimerInfo &seed); + Batch(); + explicit Batch(const TimerInfo &seed); virtual ~Batch() = default; - std::chrono::steady_clock::time_point GetStart () const; - std::chrono::steady_clock::time_point GetEnd () const; - uint32_t GetFlags () const; - size_t Size () const; - std::shared_ptr Get (size_t index) const; - bool CanHold (std::chrono::steady_clock::time_point whenElapsed, - std::chrono::steady_clock::time_point maxWhen) const; - bool Add (const std::shared_ptr &alarm); - bool Remove (const TimerInfo &alarm); - bool Remove (std::function predicate); - bool HasPackage (const std::string &package_name); - bool HasWakeups () const; + std::chrono::steady_clock::time_point GetStart() const; + std::chrono::steady_clock::time_point GetEnd() const; + uint32_t GetFlags() const; + size_t Size() const; + std::shared_ptr Get(size_t index) const; + bool CanHold(std::chrono::steady_clock::time_point whenElapsed, + std::chrono::steady_clock::time_point maxWhen) const; + bool Add(const std::shared_ptr &alarm); + bool Remove(const TimerInfo &alarm); + bool Remove(std::function predicate); + bool HasPackage(const std::string &package_name); + bool HasWakeups() const; private: std::chrono::steady_clock::time_point start_; diff --git a/services/timer/include/timer_info.h b/services/timer/include/timer_info.h index 0c20cc3..2376a3a 100644 --- a/services/timer/include/timer_info.h +++ b/services/timer/include/timer_info.h @@ -44,18 +44,18 @@ public: std::chrono::steady_clock::time_point expectedMaxWhenElapsed; std::chrono::milliseconds repeatInterval; - TimerInfo (uint64_t id, int type, - std::chrono::milliseconds when, - std::chrono::steady_clock::time_point whenElapsed, - std::chrono::milliseconds windowLength, - std::chrono::steady_clock::time_point maxWhen, - std::chrono::milliseconds interval, - std::function callback, - uint32_t flags, - uint64_t uid); + TimerInfo(uint64_t id, int type, + std::chrono::milliseconds when, + std::chrono::steady_clock::time_point whenElapsed, + std::chrono::milliseconds windowLength, + std::chrono::steady_clock::time_point maxWhen, + std::chrono::milliseconds interval, + std::function callback, + uint32_t flags, + uint64_t uid); virtual ~TimerInfo() = default; - bool operator== (const TimerInfo &other) const; - bool Matches (const std::string &packageName) const; + bool operator==(const TimerInfo &other) const; + bool Matches(const std::string &packageName) const; }; } // MiscService } // OHOS diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 177246c..0388aa4 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -32,54 +32,56 @@ namespace MiscServices { class TimerManager : public ITimerManager { public: static std::shared_ptr Create(); - - uint64_t CreateTimer(int type, uint64_t windowLength, uint64_t interval,int flag, - std::function callback,uint64_t uid) override; + uint64_t CreateTimer(int type, + uint64_t windowLength, + uint64_t interval, + int flag, + std::function callback, + uint64_t uid) override; bool StartTimer(uint64_t timerNumber, uint64_t triggerTime) override; bool StopTimer(uint64_t timerNumber) override; bool DestroyTimer(uint64_t timerNumber) override; - ~TimerManager() override; private: explicit TimerManager(std::shared_ptr impl); void TimerLooper(); - void SetHandler(uint64_t id, int type, uint64_t triggerAtTime, uint64_t windowLength, uint64_t interval, int flag, - std::function callback, - uint64_t uid); - - void SetHandlerLocked(uint64_t id, int type, - std::chrono::milliseconds when, - std::chrono::steady_clock::time_point whenElapsed, - std::chrono::milliseconds windowLength, - std::chrono::steady_clock::time_point maxWhen, - std::chrono::milliseconds interval, - std::function callback, - uint32_t flags, - bool doValidate, - uint64_t callingUid); - + void SetHandler(uint64_t id, + int type, + uint64_t triggerAtTime, + uint64_t windowLength, + uint64_t interval, + int flag, + std::function callback, + uint64_t uid); + void SetHandlerLocked(uint64_t id, + int type, + std::chrono::milliseconds when, + std::chrono::steady_clock::time_point whenElapsed, + std::chrono::milliseconds windowLength, + std::chrono::steady_clock::time_point maxWhen, + std::chrono::milliseconds interval, + std::function callback, + uint32_t flags, + bool doValidate, + uint64_t callingUid); void RemoveHandler(uint64_t id); void RemoveLocked(uint64_t id); void ReBatchAllTimers(); void ReBatchAllTimersLocked(bool doValidate); void ReAddTimerLocked(std::shared_ptr timer, - std::chrono::steady_clock::time_point nowElapsed, bool doValidate); - + std::chrono::steady_clock::time_point nowElapsed, + bool doValidate); void SetHandlerLocked(std::shared_ptr alarm, bool rebatching, bool doValidate); void InsertAndBatchTimerLocked(std::shared_ptr alarm); int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed, - std::chrono::steady_clock::time_point maxWhen); - + std::chrono::steady_clock::time_point maxWhen); bool TriggerTimersLocked(std::vector> &triggerList, - std::chrono::steady_clock::time_point nowElapsed); - + std::chrono::steady_clock::time_point nowElapsed); void RescheduleKernelTimerLocked(); - void DeliverTimersLocked(const std::vector> &triggerList, - std::chrono::steady_clock::time_point nowElapsed); - + std::chrono::steady_clock::time_point nowElapsed); std::shared_ptr FindFirstWakeupBatchLocked(); void SetLocked(int type, std::chrono::nanoseconds when); std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type); @@ -92,11 +94,9 @@ private: std::vector> alarmBatches_; std::mutex mutex_; std::mutex entryMapMutex_; - std::chrono::system_clock::time_point lastTimeChangeClockTime_; std::chrono::steady_clock::time_point lastTimeChangeRealtime_; }; // timer_manager - } // MiscServices } // OHOS diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index a3929bc..a0e71c7 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -48,14 +48,14 @@ public: ELAPSED_REALTIME = 3 }; - virtual uint64_t CreateTimer (int type, uint64_t windowLength, uint64_t interval, int flag, - std::function callback, + virtual uint64_t CreateTimer(int type, uint64_t windowLength, uint64_t interval, int flag, + std::function callback, uint64_t uid) = 0; - virtual bool StartTimer (uint64_t timerNumber, uint64_t triggerTime) = 0; - virtual bool StopTimer (uint64_t timerNumber) = 0; - virtual bool DestroyTimer (uint64_t timerNumber) = 0; - virtual ~ITimerManager () = default; + virtual bool StartTimer(uint64_t timerNumber, uint64_t triggerTime) = 0; + virtual bool StopTimer(uint64_t timerNumber) = 0; + virtual bool DestroyTimer(uint64_t timerNumber) = 0; + virtual ~ITimerManager() = default; }; // ITimerManager } // MiscService } // OHOS diff --git a/services/timer/src/batch.cpp b/services/timer/src/batch.cpp index 968c3d8..ea7238a 100644 --- a/services/timer/src/batch.cpp +++ b/services/timer/src/batch.cpp @@ -20,47 +20,47 @@ namespace OHOS { namespace MiscServices { const auto TYPE_NONWAKEUP_MASK = 0x1; -Batch::Batch () - : start_{std::chrono::steady_clock::time_point::min ()}, - end_{std::chrono::steady_clock::time_point::max ()}, - flags_{0} +Batch::Batch() + : start_ {std::chrono::steady_clock::time_point::min()}, + end_ {std::chrono::steady_clock::time_point::max()}, + flags_ {0} { } -Batch::Batch (const TimerInfo &seed) - : start_{seed.whenElapsed}, - end_{seed.maxWhenElapsed}, - flags_{seed.flags}, - alarms_{std::make_shared (seed)} +Batch::Batch(const TimerInfo &seed) + : start_ {seed.whenElapsed}, + end_ {seed.maxWhenElapsed}, + flags_ {seed.flags}, + alarms_ {std::make_shared(seed)} { } -size_t Batch::Size () const +size_t Batch::Size() const { - return alarms_.size (); + return alarms_.size(); } -std::shared_ptr Batch::Get (size_t index) const +std::shared_ptr Batch::Get(size_t index) const { return (index >= alarms_.size()) ? nullptr : alarms_.at(index); } -bool Batch::CanHold (std::chrono::steady_clock::time_point whenElapsed, +bool Batch::CanHold(std::chrono::steady_clock::time_point whenElapsed, std::chrono::steady_clock::time_point maxWhen) const { return (end_ > whenElapsed) && (start_ <= maxWhen); } -bool Batch::Add (const std::shared_ptr &alarm) +bool Batch::Add(const std::shared_ptr &alarm) { bool new_start = false; - auto it = std::upper_bound(alarms_.begin(), - alarms_.end(), - alarm, - [](const std::shared_ptr &first, const std::shared_ptr &second) { - return first->whenElapsed < second->whenElapsed; - }); - alarms_.insert (it, alarm); // 根据Alarm.when_elapsed从小到大排列 + auto it = std::upper_bound(alarms_.begin(), + alarms_.end(), + alarm, + [](const std::shared_ptr &first, const std::shared_ptr &second) { + return first->whenElapsed < second->whenElapsed; + }); + alarms_.insert(it, alarm); // 根据Alarm.when_elapsed从小到大排列 if (alarm->whenElapsed > start_) { start_ = alarm->whenElapsed; @@ -75,22 +75,22 @@ bool Batch::Add (const std::shared_ptr &alarm) return new_start; } -bool Batch::Remove (const TimerInfo &alarm) +bool Batch::Remove(const TimerInfo &alarm) { - return Remove ([alarm] (const TimerInfo &a) { return a == alarm; }); + return Remove([alarm] (const TimerInfo &a) { return a == alarm; }); } -bool Batch::Remove (std::function predicate) +bool Batch::Remove(std::function predicate) { TIME_HILOGD(TIME_MODULE_SERVICE, "start"); bool didRemove = false; - auto newStart = std::chrono::steady_clock::time_point::min (); - auto newEnd = std::chrono::steady_clock::time_point::max (); + auto newStart = std::chrono::steady_clock::time_point::min(); + auto newEnd = std::chrono::steady_clock::time_point::max(); uint32_t newFlags = 0; - for (auto it = alarms_.begin (); it != alarms_.end ();) { + for (auto it = alarms_.begin(); it != alarms_.end();) { auto alarm = *it; TIME_HILOGD(TIME_MODULE_SERVICE, "looper"); - if (predicate (*alarm)) { + if (predicate(*alarm)) { TIME_HILOGD(TIME_MODULE_SERVICE, "erase"); it = alarms_.erase(it); didRemove = true; @@ -114,34 +114,34 @@ bool Batch::Remove (std::function predicate) return didRemove; } -bool Batch::HasPackage (const std::string &package_name) +bool Batch::HasPackage(const std::string &package_name) { - return std::find_if(alarms_.begin(), - alarms_.end(), - [package_name](const std::shared_ptr &alarm) { - return alarm->Matches(package_name); - }) != alarms_.end(); + return std::find_if(alarms_.begin(), + alarms_.end(), + [package_name] (const std::shared_ptr &alarm) { + return alarm->Matches(package_name); + }) != alarms_.end(); } -bool Batch::HasWakeups () const +bool Batch::HasWakeups() const { - return std::any_of (alarms_.begin (), alarms_.begin (), - [] (const std::shared_ptr &item) { - return (static_cast(item->type) & TYPE_NONWAKEUP_MASK) == 0; - }); + return std::any_of(alarms_.begin(), alarms_.begin(), + [] (const std::shared_ptr &item) { + return (static_cast(item->type) & TYPE_NONWAKEUP_MASK) == 0; + }); } -std::chrono::steady_clock::time_point Batch::GetStart () const +std::chrono::steady_clock::time_point Batch::GetStart() const { return start_; } -std::chrono::steady_clock::time_point Batch::GetEnd () const +std::chrono::steady_clock::time_point Batch::GetEnd() const { return end_; } -uint32_t Batch::GetFlags () const +uint32_t Batch::GetFlags() const { return flags_; } diff --git a/services/timer/src/timer_handler.cpp b/services/timer/src/timer_handler.cpp index aa94ace..f5ccfca 100644 --- a/services/timer/src/timer_handler.cpp +++ b/services/timer/src/timer_handler.cpp @@ -111,7 +111,7 @@ int TimerHandler::Set(uint32_t type, std::chrono::nanoseconds when) } auto second = std::chrono::duration_cast(when); - timespec ts { second.count(),(when - second).count()}; + timespec ts {second.count(), (when - second).count()}; itimerspec spec {timespec {}, ts}; return timerfd_settime(fds_[type], TFD_TIMER_ABSTIME, &spec, nullptr); } diff --git a/services/timer/src/timer_info.cpp b/services/timer/src/timer_info.cpp index 1a589fb..0f55edf 100644 --- a/services/timer/src/timer_info.cpp +++ b/services/timer/src/timer_info.cpp @@ -17,30 +17,30 @@ namespace OHOS { namespace MiscServices { -bool TimerInfo::operator ==(const TimerInfo &other) const +bool TimerInfo::operator==(const TimerInfo &other) const { return this->id == other.id; } -bool TimerInfo::Matches (const std::string &packageName) const +bool TimerInfo::Matches(const std::string &packageName) const { return false; } -TimerInfo::TimerInfo (uint64_t _id, int _type, +TimerInfo::TimerInfo(uint64_t _id, int _type, std::chrono::milliseconds _when, std::chrono::steady_clock::time_point _whenElapsed, std::chrono::milliseconds _windowLength, std::chrono::steady_clock::time_point _maxWhen, std::chrono::milliseconds _interval, - std::function _callback, + std::function _callback, uint32_t _flags, uint64_t _uid) : id {_id}, type {_type}, origWhen {_when}, wakeup {_type == ITimerManager::ELAPSED_REALTIME_WAKEUP || _type == ITimerManager::RTC_WAKEUP}, - callback {std::move (_callback)}, + callback {std::move(_callback)}, flags {_flags}, uid {_uid}, when {_when}, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 3c255f1..909dcbb 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -23,16 +23,19 @@ namespace OHOS { namespace MiscServices { using namespace std::chrono; - +namespace { static int TIME_CHANGED_BITS = 16; static uint32_t TIME_CHANGED_MASK = 1 << TIME_CHANGED_BITS; -static int ONE_THOUSAND = 1000; +const int ONE_THOUSAND = 1000; +const float_t BATCH_WINDOW_COE = 0.75; const auto MIN_FUTURITY = seconds(5); const auto MIN_INTERVAL = seconds(5); const auto MAX_INTERVAL = hours(24 * 365); const auto INTERVAL_HOUR = hours(1); const auto INTERVAL_HALF_DAY = hours(12); const auto MIN_FUZZABLE_INTERVAL = milliseconds(10000); +} + extern bool AddBatchLocked(std::vector> &list, const std::shared_ptr &batch); extern steady_clock::time_point MaxTriggerTime(steady_clock::time_point now, @@ -60,15 +63,15 @@ TimerManager::TimerManager(std::shared_ptr impl) alarmThread_.reset(new std::thread(&TimerManager::TimerLooper, this)); } -uint64_t TimerManager::CreateTimer(int type, - uint64_t windowLength, - uint64_t interval, +uint64_t TimerManager::CreateTimer(int type, + uint64_t windowLength, + uint64_t interval, int flag, - std::function callback, + std::function callback, uint64_t uid) { - TIME_HILOGI(TIME_MODULE_SERVICE, - "Create timer:%{public}d windowLength:%{public}" PRId64"interval:%{public}" PRId64"flag:%{public}d", + TIME_HILOGI(TIME_MODULE_SERVICE, + "Create timer: %{public}d windowLength:%{public}" PRId64 "interval:%{public}" PRId64 "flag:%{public}d", type, windowLength, interval, @@ -78,13 +81,14 @@ uint64_t TimerManager::CreateTimer(int type, timerNumber = random_(); } auto timerInfo = std::make_shared(TimerEntry { - timerNumber, + timerNumber, type, - windowLength, - interval, - flag, - std::move(callback), - uid}); + windowLength, + interval, + flag, + std::move(callback), + uid + }); std::lock_guard lock(entryMapMutex_); timerEntryMap_.insert(std::make_pair(timerNumber, timerInfo)); return timerNumber; @@ -147,7 +151,7 @@ void TimerManager::SetHandler(uint64_t id, uint64_t windowLength, uint64_t interval, int flag, - std::function callback, + std::function callback, uint64_t uid) { TIME_HILOGI(TIME_MODULE_SERVICE, "start id: %{public}" PRId64 "", id); @@ -162,7 +166,7 @@ void TimerManager::SetHandler(uint64_t id, auto intervalDuration = milliseconds(interval); if (intervalDuration > milliseconds::zero() && intervalDuration < MIN_INTERVAL) { intervalDuration = MIN_INTERVAL; - }else if (intervalDuration > MAX_INTERVAL) { + } else if (intervalDuration > MAX_INTERVAL) { intervalDuration = MAX_INTERVAL; } if (triggerAtTime < 0) { @@ -232,13 +236,13 @@ void TimerManager::RemoveLocked(uint64_t id) }; bool didRemove = false; - for (auto it = alarmBatches_.begin(); it != alarmBatches_.end();) { + for (auto it = alarmBatches_.begin(); it != alarmBatches_.end(); ) { auto batch = *it; didRemove = batch->Remove(whichAlarms); if (batch->Size() == 0) { TIME_HILOGD(TIME_MODULE_SERVICE, "erase"); it = alarmBatches_.erase(it); - }else{ + } else { ++it; } } @@ -267,7 +271,7 @@ void TimerManager::ReBatchAllTimers() TIME_HILOGI(TIME_MODULE_SERVICE, "end"); } -void TimerManager::ReBatchAllTimersLocked(bool doValidate) +void TimerManager::ReBatchAllTimersLocked(bool doValidate) { TIME_HILOGI(TIME_MODULE_SERVICE, "start"); auto oldSet = alarmBatches_; @@ -339,16 +343,13 @@ void TimerManager::TimerLooper() lastTimeChangeClockTime = lastTimeChangeClockTime_; expectedClockTime = lastTimeChangeClockTime + (duration_cast(nowElapsed.time_since_epoch()) - duration_cast(lastTimeChangeRealtime_.time_since_epoch())); - - - if (lastTimeChangeClockTime == system_clock::time_point::min() - || nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND)) + if (lastTimeChangeClockTime == system_clock::time_point::min() + || nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND)) || nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))) { TIME_HILOGI(TIME_MODULE_SERVICE, "Time changed notification from kernel; rebatching"); ReBatchAllTimers(); lastTimeChangeClockTime_ = nowRtc; lastTimeChangeRealtime_ = nowElapsed; - } } @@ -357,7 +358,6 @@ void TimerManager::TimerLooper() auto hasWakeup = TriggerTimersLocked(triggerList, nowElapsed); TIME_HILOGI(TIME_MODULE_SERVICE, "hasWakeup= %{public}d", hasWakeup); DeliverTimersLocked(triggerList, nowElapsed); - RescheduleKernelTimerLocked(); } else { std::lock_guard lock(mutex_); @@ -399,7 +399,8 @@ bool TimerManager::TriggerTimersLocked(std::vector> & alarm->count = 1; triggerList.push_back(alarm); if (alarm->repeatInterval > milliseconds::zero()) { - alarm->count += duration_cast(nowElapsed - alarm->expectedWhenElapsed) / alarm->repeatInterval; + alarm->count += duration_cast(nowElapsed - + alarm->expectedWhenElapsed) / alarm->repeatInterval; auto delta = alarm->count * alarm->repeatInterval; auto nextElapsed = alarm->whenElapsed + delta; SetHandlerLocked(alarm->id, alarm->type, alarm->when + delta, nextElapsed, alarm->windowLength, @@ -411,11 +412,11 @@ bool TimerManager::TriggerTimersLocked(std::vector> & } } } - std::sort(triggerList.begin(), - triggerList.end(), - [](const std::shared_ptr &l, const std::shared_ptr &r) { - return l->whenElapsed < r->whenElapsed; - }); + std::sort(triggerList.begin(), + triggerList.end(), + [] (const std::shared_ptr &l, const std::shared_ptr &r) { + return l->whenElapsed < r->whenElapsed; + }); return hasWakeup; } @@ -444,10 +445,11 @@ void TimerManager::RescheduleKernelTimerLocked() std::shared_ptr TimerManager::FindFirstWakeupBatchLocked() { - auto it = std::find_if (alarmBatches_.begin(),alarmBatches_.end(), - [](const std::shared_ptr &batch) { - return batch->HasWakeups(); - }); + auto it = std::find_if(alarmBatches_.begin(), + alarmBatches_.end(), + [](const std::shared_ptr &batch) { + return batch->HasWakeups(); + }); return (it != alarmBatches_.end()) ? *it : nullptr; } @@ -491,7 +493,7 @@ int64_t TimerManager::AttemptCoalesceLocked(std::chrono::steady_clock::time_poin } void TimerManager::DeliverTimersLocked(const std::vector> &triggerList, - std::chrono::steady_clock::time_point nowElapsed) + std::chrono::steady_clock::time_point nowElapsed) { TIME_HILOGI(TIME_MODULE_SERVICE, "start"); for (const auto &alarm : triggerList) { @@ -506,23 +508,27 @@ void TimerManager::DeliverTimersLocked(const std::vector> &list, const std::shared_ptr &newBatch) { TIME_HILOGI(TIME_MODULE_SERVICE, "start"); - auto it = std::upper_bound(list.begin(), list.end(), newBatch, + auto it = std::upper_bound(list.begin(), + list.end(), + newBatch, [](const std::shared_ptr &first, const std::shared_ptr &second) { - return first->GetStart() < second->GetStart(); - }); + return first->GetStart() < second->GetStart(); + }); list.insert(it, newBatch); TIME_HILOGI(TIME_MODULE_SERVICE, "end"); return it == list.begin(); } -steady_clock::time_point MaxTriggerTime(steady_clock::time_point now, steady_clock::time_point triggerAtTime, milliseconds interval) +steady_clock::time_point MaxTriggerTime(steady_clock::time_point now, + steady_clock::time_point triggerAtTime, + milliseconds interval) { - milliseconds futurity = (interval == milliseconds::zero()) ? + milliseconds futurity = (interval == milliseconds::zero()) ? (duration_cast(triggerAtTime - now)) : interval; if (futurity < MIN_FUZZABLE_INTERVAL) { futurity = milliseconds::zero(); } - return triggerAtTime + milliseconds(static_cast(0.75 * futurity.count())); + return triggerAtTime + milliseconds(static_cast(BATCH_WINDOW_COE * futurity.count())); } } // MiscServices } // OHOS \ No newline at end of file From baca39d6ef1f83f4528e03e19d02331318b2d193 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 29 Sep 2021 15:51:46 +0800 Subject: [PATCH 3/4] 20210929LTS01 Signed-off-by: guduhanyan --- .../napi/system_time/include/js_systemtime.h | 9 +- .../js/napi/system_time/src/js_systemtime.cpp | 223 +++++++++++------- .../js/napi/system_timer/src/system_timer.cpp | 4 +- 3 files changed, 139 insertions(+), 97 deletions(-) diff --git a/interfaces/kits/js/napi/system_time/include/js_systemtime.h b/interfaces/kits/js/napi/system_time/include/js_systemtime.h index b92e8bb..697ce40 100644 --- a/interfaces/kits/js/napi/system_time/include/js_systemtime.h +++ b/interfaces/kits/js/napi/system_time/include/js_systemtime.h @@ -27,6 +27,8 @@ const int NONE_PARAMETER = 0; const int ONE_PARAMETER = 1; const int TWO_PARAMETERS = 2; const int THREE_PARAMETERS = 3; +const int SET_TIME_MAX_PARA = 2; +const int SET_TIMEZONE_MAX_PARA = 2; const int MAX_TIME_ZONE_ID = 1024; const int NO_ERROR = 0; const int ERROR = -1; @@ -35,13 +37,6 @@ const int PARAM1 = 1; const int ARGS_TWO = 2; } -#define GET_PARAMS(env, info, num) \ - size_t argc = num; \ - napi_value argv[num] = {0}; \ - napi_value thisVar = nullptr; \ - void *data; \ - napi_get_cb_info(env, info, &argc, argv, &thisVar, &data) - typedef struct AsyncContext { napi_env env = nullptr; napi_async_work work = nullptr; 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 dd49cb1..e45c2c8 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -36,6 +36,13 @@ napi_value TimeGetCallbackErrorValue(napi_env env, int errCode) return result; } +napi_value TimeNapiGetNull(napi_env env) +{ + napi_value result = nullptr; + napi_get_null(env, &result); + return result; +} + void TimeSetPromise(const napi_env &env, const napi_deferred &deferred, const napi_value &result) { napi_resolve_deferred(env, deferred, result); @@ -64,55 +71,93 @@ void TimeReturnCallbackPromise(const napi_env &env, const TimeCallbackPromiseInf } } +napi_value TimeJSParaError(const napi_env &env, const napi_ref &callback) +{ + if (callback) { + return TimeNapiGetNull(env); + } else { + napi_value promise = nullptr; + napi_deferred deferred = nullptr; + napi_create_promise(env, &deferred, &promise); + TimeSetPromise(env, deferred, TimeNapiGetNull(env)); + return promise; + } +} + +void TimePaddingAsyncCallbackInfo(const napi_env &env, + AsyncContext *&asynccallbackinfo, + const napi_ref &callback, + napi_value &promise) +{ + if (callback) { + asynccallbackinfo->callbackRef = callback; + asynccallbackinfo->isCallback = true; + } else { + napi_deferred deferred = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); + asynccallbackinfo->deferred = deferred; + asynccallbackinfo->isCallback = false; + } +} + +napi_value ParseParametersBySetTime(const napi_env &env, const napi_value (&argv)[SET_TIME_MAX_PARA], + const size_t &argc, int64_t ×, napi_ref &callback) +{ + NAPI_ASSERT(env, argc >= SET_TIME_MAX_PARA - 1, "Wrong number of arguments"); + napi_valuetype valueType = napi_undefined; + + // argv[0]: times or date object + NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); + NAPI_ASSERT(env, valueType == napi_string || valueType == napi_object, "Wrong argument type. string expected."); + if (valueType == napi_number) { + napi_get_value_int64(env, argv[0], ×); + NAPI_ASSERT(env, times >= 0, "Wrong argument timer. Positive number expected."); + } else { + bool hasProperty = false; + napi_valuetype resValueType = napi_undefined; + NAPI_CALL(env, napi_has_named_property(env, argv[0], "getTime", &hasProperty)); + NAPI_ASSERT(env, hasProperty, "type expected."); + napi_value getTimeFunc = nullptr; + napi_get_named_property(env, argv[0], "getTime", &getTimeFunc); + napi_value getTimeResult = nullptr; + napi_call_function(env, argv[0], getTimeFunc, 0, nullptr, &getTimeResult); + NAPI_CALL(env, napi_typeof(env, getTimeResult, &resValueType)); + NAPI_ASSERT(env, resValueType == napi_number, "type mismatch"); + napi_get_value_int64(env, getTimeResult, ×); + } + + // argv[1]:callback + if (argc >= SET_TIME_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + NAPI_ASSERT(env, valueType == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[1], 1, &callback); + } + return TimeNapiGetNull(env); +} + napi_value JSSystemTimeSetTime(napi_env env, napi_callback_info info) { - TIME_HILOGI(TIME_MODULE_JS_NAPI, "JSSystemTimeSetTime start"); - GET_PARAMS(env, info, TWO_PARAMETERS); - NAPI_ASSERT(env, argc == ONE_PARAMETER || argc == TWO_PARAMETERS, "type mismatch"); - - AsyncContext* asyncContext = new (std::nothrow)AsyncContext(); - asyncContext->env = env; - - for (size_t i = 0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); - if (i == 0 && valueType == napi_number) { - napi_get_value_int64(env, argv[i], &asyncContext->time); - } else if (i == 0 && valueType == napi_object) { - bool hasProperty = false; - napi_valuetype resValueType = napi_undefined; - NAPI_CALL(env, napi_has_named_property(env, argv[i], "getTime", &hasProperty)); - NAPI_ASSERT(env, hasProperty, "type expected."); - napi_value getTimeFunc = nullptr; - napi_get_named_property(env, argv[i], "getTime", &getTimeFunc); - napi_value getTimeResult = nullptr; - napi_call_function(env, argv[i], getTimeFunc, 0, nullptr, &getTimeResult); - NAPI_CALL(env, napi_typeof(env, getTimeResult, &resValueType)); - NAPI_ASSERT(env, resValueType == napi_number, "type mismatch"); - int64_t dateValue = 0; - napi_get_value_int64(env, getTimeResult, &dateValue); - asyncContext->time = dateValue; - } else if (i == 1 && valueType == napi_function) { - asyncContext->isCallback = true; - napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); - } else { - delete asyncContext; - NAPI_ASSERT(env, false, "type mismatch"); - } + size_t argc = SET_TIME_MAX_PARA; + napi_value argv[SET_TIME_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + int64_t times; + napi_ref callback = nullptr; + if (ParseParametersBySetTime(env, argv, argc, times, callback) == nullptr) { + return TimeJSParaError(env, callback); } - - napi_value result = nullptr; - if (!asyncContext->isCallback) { - napi_create_promise(env, &asyncContext->deferred, &result); - } else { - napi_get_undefined(env, &result); + AsyncContext* asyncContext = new (std::nothrow)AsyncContext{.env = env, .time = times}; + if (!asyncContext) { + return TimeJSParaError(env, callback); } - + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); napi_value resource = nullptr; napi_create_string_utf8(env, "JSSystemTimeSetTime", NAPI_AUTO_LENGTH, &resource); napi_create_async_work(env, nullptr, - resource,[](napi_env env, void* data) { + resource, + [](napi_env env, void* data) { AsyncContext* asyncContext = (AsyncContext*)data; asyncContext->isOK = TimeServiceClient::GetInstance()->SetTime(asyncContext->time); }, @@ -126,12 +171,9 @@ napi_value JSSystemTimeSetTime(napi_env env, napi_callback_info info) info.callback = asyncContext->callbackRef; info.deferred = asyncContext->deferred; info.errorCode = asyncContext->errorCode; - - // result: void napi_value result = 0; napi_get_null(env, &result); TimeReturnCallbackPromise(env, info, result); - napi_delete_async_work(env, asyncContext->work); if (asyncContext) { delete asyncContext; @@ -140,51 +182,55 @@ napi_value JSSystemTimeSetTime(napi_env env, napi_callback_info info) }, (void*)asyncContext, &asyncContext->work); - napi_queue_async_work(env, asyncContext->work); - return result; + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value ParseParametersBySetTimezone(const napi_env &env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], + const size_t &argc, std::string &timezoneId, napi_ref &callback) +{ + NAPI_ASSERT(env, argc >= SET_TIMEZONE_MAX_PARA - 1, "Wrong number of arguments"); + napi_valuetype valueType = napi_undefined; + + // argv[0]: timezoneid + NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); + NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. string expected."); + char timeZoneChars[MAX_TIME_ZONE_ID]; + size_t copied; + napi_get_value_string_utf8(env, argv[0], timeZoneChars, MAX_TIME_ZONE_ID - 1, &copied); + NAPI_ASSERT(env, copied == 0, "Wrong argument timezone. timezoneid length >0 expected."); + timezoneId.assign(timeZoneChars, copied); + + // argv[1]:callback + if (argc >= SET_TIMEZONE_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + NAPI_ASSERT(env, valueType == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[1], 1, &callback); + } + return TimeNapiGetNull(env); } napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) { - TIME_HILOGI(TIME_MODULE_JS_NAPI, "JSSystemTimeSetTimeZone start"); - GET_PARAMS(env, info, TWO_PARAMETERS); - NAPI_ASSERT(env, argc == ONE_PARAMETER || argc == TWO_PARAMETERS, "type mismatch"); - - AsyncContext* asyncContext = new AsyncContext(); - asyncContext->env = env; - - for (size_t i = 0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); - 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)) { - delete asyncContext; - NAPI_ASSERT(env, false, "input para invalid"); - } - std::string timeZoneStr(timeZoneChars, timeZoneCharsSize); - asyncContext->timeZone = timeZoneStr; - } else if (i == 1 && valueType == napi_function) { - asyncContext->isCallback = true; - napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); - } else { - delete asyncContext; - NAPI_ASSERT(env, false, "type mismatch"); - } + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + std::string timezoneId; + napi_ref callback = nullptr; + if (ParseParametersBySetTimezone(env, argv, argc, timezoneId, callback) == nullptr) { + return TimeJSParaError(env, callback); } - - napi_value result = nullptr; - - if (!asyncContext->isCallback) { - napi_create_promise(env, &asyncContext->deferred, &result); - } else { - napi_get_undefined(env, &result); + AsyncContext* asyncContext = new (std::nothrow)AsyncContext{.env = env, .timeZone = timezoneId}; + if (!asyncContext) { + return TimeJSParaError(env, callback); } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); napi_value resource = nullptr; napi_create_string_utf8(env, "JSSystemTimeSetTimeZone", NAPI_AUTO_LENGTH, &resource); napi_create_async_work(env, @@ -204,12 +250,9 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) info.callback = asyncContext->callbackRef; info.deferred = asyncContext->deferred; info.errorCode = asyncContext->errorCode; - - // result: void napi_value result = 0; napi_get_null(env, &result); TimeReturnCallbackPromise(env, info, result); - napi_delete_async_work(env, asyncContext->work); if (asyncContext) { delete asyncContext; @@ -218,8 +261,12 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) }, (void*)asyncContext, &asyncContext->work); - napi_queue_async_work(env, asyncContext->work); - return result; + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } } EXTERN_C_START 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 b6594ae..4babbf1 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -298,7 +298,6 @@ napi_value GetTimerOptions(const napi_env &env, const napi_value &value, napi_create_reference(env, result, 1, &onTriggerCallback); iTimerInfoInstance->SetCallbackInfo(env, onTriggerCallback); } - return NapiGetNull(env); } @@ -460,7 +459,8 @@ napi_value StartTimer(napi_env env, napi_callback_info info) AsyncCallbackInfoStart *asynccallbackinfo = new (std::nothrow)AsyncCallbackInfoStart{.env = env, .asyncWork = nullptr, .timerId = timerId, - .triggerTime = triggerTime}; + .triggerTime = triggerTime + }; if (!asynccallbackinfo) { return JSParaError(env, callback); } From eb2fc261f00a10ad9de5be255b1c871a63ad7ba6 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 29 Sep 2021 16:50:00 +0800 Subject: [PATCH 4/4] 20210929LTS16.49 Signed-off-by: guduhanyan --- .../js/napi/system_time/src/js_systemtime.cpp | 24 +++++----- .../js/napi/system_timer/src/system_timer.cpp | 6 +-- .../time_manager/src/time_service_stub.cpp | 2 +- services/time_manager/src/timer_call_back.cpp | 6 +-- services/timer/src/batch.cpp | 12 ++--- services/timer/src/timer_info.cpp | 16 +++---- services/timer/src/timer_manager.cpp | 44 +++++++++---------- 7 files changed, 55 insertions(+), 55 deletions(-) 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 e45c2c8..5827d07 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -154,15 +154,15 @@ napi_value JSSystemTimeSetTime(napi_env env, napi_callback_info info) TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); napi_value resource = nullptr; napi_create_string_utf8(env, "JSSystemTimeSetTime", NAPI_AUTO_LENGTH, &resource); - napi_create_async_work(env, - nullptr, + napi_create_async_work(env, + nullptr, resource, - [](napi_env env, void* data) { - AsyncContext* asyncContext = (AsyncContext*)data; + [](napi_env env, void *data) { + AsyncContext *asyncContext = (AsyncContext*)data; asyncContext->isOK = TimeServiceClient::GetInstance()->SetTime(asyncContext->time); }, - [](napi_env env, napi_status status, void* data) { - AsyncContext* asyncContext = (AsyncContext*)data; + [](napi_env env, napi_status status, void *data) { + AsyncContext *asyncContext = (AsyncContext*)data; if (!asyncContext->isOK) { asyncContext->errorCode = ERROR; } @@ -214,7 +214,7 @@ napi_value ParseParametersBySetTimezone(const napi_env &env, const napi_value (& return TimeNapiGetNull(env); } -napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) +napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) { size_t argc = SET_TIMEZONE_MAX_PARA; napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; @@ -225,7 +225,7 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) if (ParseParametersBySetTimezone(env, argv, argc, timezoneId, callback) == nullptr) { return TimeJSParaError(env, callback); } - AsyncContext* asyncContext = new (std::nothrow)AsyncContext{.env = env, .timeZone = timezoneId}; + AsyncContext *asyncContext = new (std::nothrow)AsyncContext{.env = env, .timeZone = timezoneId}; if (!asyncContext) { return TimeJSParaError(env, callback); } @@ -236,12 +236,12 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) napi_create_async_work(env, nullptr, resource, - [](napi_env env, void* data) { - AsyncContext* asyncContext = (AsyncContext*)data; + [](napi_env env, void *data) { + AsyncContext *asyncContext = (AsyncContext*)data; asyncContext->isOK = TimeServiceClient::GetInstance()->SetTimeZone(asyncContext->timeZone); }, - [](napi_env env, napi_status status, void* data) { - AsyncContext* asyncContext = (AsyncContext*)data; + [](napi_env env, napi_status status, void *data) { + AsyncContext *asyncContext = (AsyncContext*)data; if (!asyncContext->isOK) { asyncContext->errorCode = ERROR; } 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 4babbf1..e79f001 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -198,9 +198,9 @@ void ITimerInfoInstance::OnTrigger() return; } - SetCallback(dataWorkerData->env, - dataWorkerData->ref, - NO_ERROR, + SetCallback(dataWorkerData->env, + dataWorkerData->ref, + NO_ERROR, NapiGetNull(dataWorkerData->env)); delete dataWorkerData; dataWorkerData = nullptr; diff --git a/services/time_manager/src/time_service_stub.cpp b/services/time_manager/src/time_service_stub.cpp index 852b1aa..3d744a3 100644 --- a/services/time_manager/src/time_service_stub.cpp +++ b/services/time_manager/src/time_service_stub.cpp @@ -59,7 +59,7 @@ 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, + 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()) { diff --git a/services/time_manager/src/timer_call_back.cpp b/services/time_manager/src/timer_call_back.cpp index b84d89a..f3ea1db 100644 --- a/services/time_manager/src/timer_call_back.cpp +++ b/services/time_manager/src/timer_call_back.cpp @@ -18,7 +18,7 @@ namespace OHOS { namespace MiscServices { namespace { - const int WANTAGENT_CODE_ELEVEN = 11; + const int WANTAGENT_CODE_ELEVEN = 11; } std::mutex TimerCallback::instanceLock_; sptr TimerCallback::instance_; @@ -102,5 +102,5 @@ void TimerCallback::NotifyTimer(const uint64_t timerId) } TIME_HILOGD(TIME_MODULE_SERVICE, "end."); } -} // namespace MiscServices -} // namespace OHOS \ No newline at end of file +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/services/timer/src/batch.cpp b/services/timer/src/batch.cpp index ea7238a..049b4c9 100644 --- a/services/timer/src/batch.cpp +++ b/services/timer/src/batch.cpp @@ -46,7 +46,7 @@ std::shared_ptr Batch::Get(size_t index) const } bool Batch::CanHold(std::chrono::steady_clock::time_point whenElapsed, - std::chrono::steady_clock::time_point maxWhen) const + std::chrono::steady_clock::time_point maxWhen) const { return (end_ > whenElapsed) && (start_ <= maxWhen); } @@ -54,8 +54,8 @@ bool Batch::CanHold(std::chrono::steady_clock::time_point whenElapsed, bool Batch::Add(const std::shared_ptr &alarm) { bool new_start = false; - auto it = std::upper_bound(alarms_.begin(), - alarms_.end(), + auto it = std::upper_bound(alarms_.begin(), + alarms_.end(), alarm, [](const std::shared_ptr &first, const std::shared_ptr &second) { return first->whenElapsed < second->whenElapsed; @@ -116,10 +116,10 @@ bool Batch::Remove(std::function predicate) bool Batch::HasPackage(const std::string &package_name) { - return std::find_if(alarms_.begin(), + return std::find_if(alarms_.begin(), alarms_.end(), - [package_name] (const std::shared_ptr &alarm) { - return alarm->Matches(package_name); + [package_name] (const std::shared_ptr &alarm) { + return alarm->Matches(package_name); }) != alarms_.end(); } diff --git a/services/timer/src/timer_info.cpp b/services/timer/src/timer_info.cpp index 0f55edf..ab4f42b 100644 --- a/services/timer/src/timer_info.cpp +++ b/services/timer/src/timer_info.cpp @@ -28,14 +28,14 @@ bool TimerInfo::Matches(const std::string &packageName) const } TimerInfo::TimerInfo(uint64_t _id, int _type, - std::chrono::milliseconds _when, - std::chrono::steady_clock::time_point _whenElapsed, - std::chrono::milliseconds _windowLength, - std::chrono::steady_clock::time_point _maxWhen, - std::chrono::milliseconds _interval, - std::function _callback, - uint32_t _flags, - uint64_t _uid) + std::chrono::milliseconds _when, + std::chrono::steady_clock::time_point _whenElapsed, + std::chrono::milliseconds _windowLength, + std::chrono::steady_clock::time_point _maxWhen, + std::chrono::milliseconds _interval, + std::function _callback, + uint32_t _flags, + uint64_t _uid) : id {_id}, type {_type}, origWhen {_when}, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 909dcbb..1964681 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -63,14 +63,14 @@ TimerManager::TimerManager(std::shared_ptr impl) alarmThread_.reset(new std::thread(&TimerManager::TimerLooper, this)); } -uint64_t TimerManager::CreateTimer(int type, - uint64_t windowLength, - uint64_t interval, +uint64_t TimerManager::CreateTimer(int type, + uint64_t windowLength, + uint64_t interval, int flag, - std::function callback, + std::function callback, uint64_t uid) { - TIME_HILOGI(TIME_MODULE_SERVICE, + TIME_HILOGI(TIME_MODULE_SERVICE, "Create timer: %{public}d windowLength:%{public}" PRId64 "interval:%{public}" PRId64 "flag:%{public}d", type, windowLength, @@ -81,12 +81,12 @@ uint64_t TimerManager::CreateTimer(int type, timerNumber = random_(); } auto timerInfo = std::make_shared(TimerEntry { - timerNumber, + timerNumber, type, - windowLength, - interval, - flag, - std::move(callback), + windowLength, + interval, + flag, + std::move(callback), uid }); std::lock_guard lock(entryMapMutex_); @@ -151,7 +151,7 @@ void TimerManager::SetHandler(uint64_t id, uint64_t windowLength, uint64_t interval, int flag, - std::function callback, + std::function callback, uint64_t uid) { TIME_HILOGI(TIME_MODULE_SERVICE, "start id: %{public}" PRId64 "", id); @@ -236,7 +236,7 @@ void TimerManager::RemoveLocked(uint64_t id) }; bool didRemove = false; - for (auto it = alarmBatches_.begin(); it != alarmBatches_.end(); ) { + for (auto it = alarmBatches_.begin(); it != alarmBatches_.end();) { auto batch = *it; didRemove = batch->Remove(whichAlarms); if (batch->Size() == 0) { @@ -343,8 +343,8 @@ void TimerManager::TimerLooper() lastTimeChangeClockTime = lastTimeChangeClockTime_; expectedClockTime = lastTimeChangeClockTime + (duration_cast(nowElapsed.time_since_epoch()) - duration_cast(lastTimeChangeRealtime_.time_since_epoch())); - if (lastTimeChangeClockTime == system_clock::time_point::min() - || nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND)) + if (lastTimeChangeClockTime == system_clock::time_point::min() + || nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND)) || nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))) { TIME_HILOGI(TIME_MODULE_SERVICE, "Time changed notification from kernel; rebatching"); ReBatchAllTimers(); @@ -399,7 +399,7 @@ bool TimerManager::TriggerTimersLocked(std::vector> & alarm->count = 1; triggerList.push_back(alarm); if (alarm->repeatInterval > milliseconds::zero()) { - alarm->count += duration_cast(nowElapsed - + alarm->count += duration_cast(nowElapsed - alarm->expectedWhenElapsed) / alarm->repeatInterval; auto delta = alarm->count * alarm->repeatInterval; auto nextElapsed = alarm->whenElapsed + delta; @@ -412,7 +412,7 @@ bool TimerManager::TriggerTimersLocked(std::vector> & } } } - std::sort(triggerList.begin(), + std::sort(triggerList.begin(), triggerList.end(), [] (const std::shared_ptr &l, const std::shared_ptr &r) { return l->whenElapsed < r->whenElapsed; @@ -508,19 +508,19 @@ void TimerManager::DeliverTimersLocked(const std::vector> &list, const std::shared_ptr &newBatch) { TIME_HILOGI(TIME_MODULE_SERVICE, "start"); - auto it = std::upper_bound(list.begin(), - list.end(), + auto it = std::upper_bound(list.begin(), + list.end(), newBatch, [](const std::shared_ptr &first, const std::shared_ptr &second) { - return first->GetStart() < second->GetStart(); - }); + return first->GetStart() < second->GetStart(); + }); list.insert(it, newBatch); TIME_HILOGI(TIME_MODULE_SERVICE, "end"); 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()) ?