mirror of
https://github.com/openharmony/miscservices_time.git
synced 2026-07-19 20:03:38 -04:00
@@ -73,7 +73,6 @@ public:
|
||||
virtual void SetWantAgent(std::shared_ptr<OHOS::Notification::WantAgent::WantAgent> wantAgent) = 0;
|
||||
virtual void OnTrigger() = 0;
|
||||
};
|
||||
|
||||
} // MiscServices
|
||||
} // OHOS
|
||||
|
||||
|
||||
@@ -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,20 +37,13 @@ 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;
|
||||
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 +51,7 @@ typedef struct AsyncContext {
|
||||
|
||||
struct TimeCallbackPromiseInfo {
|
||||
napi_ref callback = nullptr;
|
||||
napi_deferred deferred;
|
||||
napi_deferred deferred = nullptr;
|
||||
bool isCallback = false;
|
||||
int errorCode = NO_ERROR;
|
||||
};
|
||||
|
||||
@@ -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,58 +71,98 @@ 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) {
|
||||
AsyncContext* asyncContext = (AsyncContext*)data;
|
||||
napi_create_async_work(env,
|
||||
nullptr,
|
||||
resource,
|
||||
[](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;
|
||||
}
|
||||
@@ -124,73 +171,77 @@ 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;
|
||||
asyncContext = nullptr;
|
||||
}
|
||||
},
|
||||
(void*)asyncContext, &asyncContext->work);
|
||||
napi_queue_async_work(env, asyncContext->work);
|
||||
return result;
|
||||
(void*)asyncContext,
|
||||
&asyncContext->work);
|
||||
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, nullptr, resource,
|
||||
[](napi_env env, void* data) {
|
||||
AsyncContext* asyncContext = (AsyncContext*)data;
|
||||
napi_create_async_work(env,
|
||||
nullptr,
|
||||
resource,
|
||||
[](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;
|
||||
}
|
||||
@@ -199,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;
|
||||
@@ -213,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
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServicesNapi {
|
||||
|
||||
} // OHOS
|
||||
} // MiscServicesNapi
|
||||
#endif // TIMER_INIT_H
|
||||
@@ -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> iTimerInfoInstance;
|
||||
napi_deferred deferred = nullptr;
|
||||
std::shared_ptr<ITimerInfoInstance> 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;
|
||||
@@ -283,7 +283,7 @@ napi_value GetTimerOptions(const napi_env &env, const napi_value &value,
|
||||
if (wantAgent == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
std::shared_ptr<OHOS::Notification::WantAgent::WantAgent> sWantAgent =
|
||||
std::shared_ptr<OHOS::Notification::WantAgent::WantAgent> sWantAgent =
|
||||
std::make_shared<OHOS::Notification::WantAgent::WantAgent>(*wantAgent);
|
||||
iTimerInfoInstance->SetWantAgent(sWantAgent);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -342,7 +341,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> iTimerInfoInstance = std::make_shared<ITimerInfoInstance>();
|
||||
@@ -427,8 +426,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;
|
||||
@@ -444,7 +445,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));
|
||||
|
||||
@@ -455,8 +456,11 @@ 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;
|
||||
@@ -552,7 +558,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));
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -657,7 +664,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));
|
||||
|
||||
|
||||
@@ -43,7 +43,6 @@ static napi_module _module = {
|
||||
.nm_modname = "systemtimer",
|
||||
.nm_priv = ((void *)0),
|
||||
.reserved = {0}
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
|
||||
@@ -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,20 +169,22 @@ void TimeService::InitTimerHandler()
|
||||
}
|
||||
timerManagerHandler_ = TimerManager::Create();
|
||||
}
|
||||
|
||||
void TimeService::PaserTimerPara(int32_t type, bool repeat, uint64_t interval, TimerPara ¶s)
|
||||
{
|
||||
bool isRealtime = false;
|
||||
bool isWakeup = false;
|
||||
auto uIntType = static_cast<uint32_t>(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{
|
||||
} else {
|
||||
paras.windowLength = -1;
|
||||
}
|
||||
|
||||
@@ -240,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();
|
||||
@@ -304,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);
|
||||
@@ -328,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;
|
||||
@@ -341,7 +343,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;
|
||||
@@ -362,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;
|
||||
}
|
||||
@@ -380,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;
|
||||
}
|
||||
@@ -420,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;
|
||||
@@ -458,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;
|
||||
@@ -469,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;
|
||||
@@ -480,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;
|
||||
@@ -491,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;
|
||||
@@ -502,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;
|
||||
@@ -513,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;
|
||||
@@ -524,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);
|
||||
@@ -541,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;
|
||||
|
||||
@@ -140,14 +140,12 @@ uint64_t TimeServiceClient::CreateTimer(std::shared_ptr<ITimerInfo> 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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -17,6 +17,9 @@
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
namespace {
|
||||
const int WANTAGENT_CODE_ELEVEN = 11;
|
||||
}
|
||||
std::mutex TimerCallback::instanceLock_;
|
||||
sptr<TimerCallback> TimerCallback::instance_;
|
||||
|
||||
@@ -92,12 +95,12 @@ void TimerCallback::NotifyTimer(const uint64_t timerId)
|
||||
std::shared_ptr<AAFwk::Want> 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);
|
||||
}
|
||||
}
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
|
||||
}
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
@@ -55,6 +55,5 @@ void TimerCallbackProxy::NotifyTimer(const uint64_t timerId)
|
||||
}
|
||||
TIME_HILOGI(TIME_MODULE_CLIENT, "end.");
|
||||
}
|
||||
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -47,6 +47,5 @@ int TimerCallbackStub::OnTriggerStub(MessageParcel& data)
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -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<TimerInfo> 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<TimerInfo> &alarm);
|
||||
bool Remove (const TimerInfo &alarm);
|
||||
bool Remove (std::function<bool (const TimerInfo &)> 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<TimerInfo> 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<TimerInfo> &alarm);
|
||||
bool Remove(const TimerInfo &alarm);
|
||||
bool Remove(std::function<bool(const TimerInfo &)> predicate);
|
||||
bool HasPackage(const std::string &package_name);
|
||||
bool HasWakeups() const;
|
||||
|
||||
private:
|
||||
std::chrono::steady_clock::time_point start_;
|
||||
|
||||
@@ -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<void (const uint64_t)> 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<void (const uint64_t)> 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
|
||||
|
||||
@@ -32,54 +32,56 @@ namespace MiscServices {
|
||||
class TimerManager : public ITimerManager {
|
||||
public:
|
||||
static std::shared_ptr<TimerManager> Create();
|
||||
|
||||
uint64_t CreateTimer(int type, uint64_t windowLength, uint64_t interval,int flag,
|
||||
std::function<void (const uint64_t)> callback,uint64_t uid) override;
|
||||
uint64_t CreateTimer(int type,
|
||||
uint64_t windowLength,
|
||||
uint64_t interval,
|
||||
int flag,
|
||||
std::function<void (const uint64_t)> 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<TimerHandler> impl);
|
||||
void TimerLooper();
|
||||
|
||||
void SetHandler(uint64_t id, int type, uint64_t triggerAtTime, uint64_t windowLength, uint64_t interval, int flag,
|
||||
std::function<void (const uint64_t)> 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<void (const uint64_t)> 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<void (const uint64_t)> 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<void (const uint64_t)> 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<TimerInfo> timer,
|
||||
std::chrono::steady_clock::time_point nowElapsed, bool doValidate);
|
||||
|
||||
std::chrono::steady_clock::time_point nowElapsed,
|
||||
bool doValidate);
|
||||
void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool doValidate);
|
||||
void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> 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<std::shared_ptr<TimerInfo>> &triggerList,
|
||||
std::chrono::steady_clock::time_point nowElapsed);
|
||||
|
||||
std::chrono::steady_clock::time_point nowElapsed);
|
||||
void RescheduleKernelTimerLocked();
|
||||
|
||||
void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList,
|
||||
std::chrono::steady_clock::time_point nowElapsed);
|
||||
|
||||
std::chrono::steady_clock::time_point nowElapsed);
|
||||
std::shared_ptr<Batch> 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<std::shared_ptr<Batch>> 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
|
||||
|
||||
|
||||
@@ -48,14 +48,14 @@ public:
|
||||
ELAPSED_REALTIME = 3
|
||||
};
|
||||
|
||||
virtual uint64_t CreateTimer (int type, uint64_t windowLength, uint64_t interval, int flag,
|
||||
std::function<void (const uint64_t)> callback,
|
||||
virtual uint64_t CreateTimer(int type, uint64_t windowLength, uint64_t interval, int flag,
|
||||
std::function<void(const uint64_t)> 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
|
||||
|
||||
@@ -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<TimerInfo> (seed)}
|
||||
Batch::Batch(const TimerInfo &seed)
|
||||
: start_ {seed.whenElapsed},
|
||||
end_ {seed.maxWhenElapsed},
|
||||
flags_ {seed.flags},
|
||||
alarms_ {std::make_shared<TimerInfo>(seed)}
|
||||
{
|
||||
}
|
||||
|
||||
size_t Batch::Size () const
|
||||
size_t Batch::Size() const
|
||||
{
|
||||
return alarms_.size ();
|
||||
return alarms_.size();
|
||||
}
|
||||
|
||||
std::shared_ptr<TimerInfo> Batch::Get (size_t index) const
|
||||
std::shared_ptr<TimerInfo> Batch::Get(size_t index) const
|
||||
{
|
||||
return (index >= alarms_.size()) ? nullptr : alarms_.at(index);
|
||||
}
|
||||
|
||||
bool Batch::CanHold (std::chrono::steady_clock::time_point whenElapsed,
|
||||
std::chrono::steady_clock::time_point maxWhen) const
|
||||
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<TimerInfo> &alarm)
|
||||
bool Batch::Add(const std::shared_ptr<TimerInfo> &alarm)
|
||||
{
|
||||
bool new_start = false;
|
||||
auto it = std::upper_bound(alarms_.begin(),
|
||||
alarms_.end(),
|
||||
alarm,
|
||||
[](const std::shared_ptr<TimerInfo> &first, const std::shared_ptr<TimerInfo> &second) {
|
||||
return first->whenElapsed < second->whenElapsed;
|
||||
});
|
||||
alarms_.insert (it, alarm); // 根据Alarm.when_elapsed从小到大排列
|
||||
alarms_.end(),
|
||||
alarm,
|
||||
[](const std::shared_ptr<TimerInfo> &first, const std::shared_ptr<TimerInfo> &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<TimerInfo> &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<bool (const TimerInfo &)> predicate)
|
||||
bool Batch::Remove(std::function<bool (const TimerInfo &)> 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<bool (const TimerInfo &)> 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<TimerInfo> &alarm) {
|
||||
return alarm->Matches(package_name);
|
||||
}) != alarms_.end();
|
||||
alarms_.end(),
|
||||
[package_name] (const std::shared_ptr<TimerInfo> &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<TimerInfo> &item) {
|
||||
return (static_cast<uint32_t>(item->type) & TYPE_NONWAKEUP_MASK) == 0;
|
||||
});
|
||||
return std::any_of(alarms_.begin(), alarms_.begin(),
|
||||
[] (const std::shared_ptr<TimerInfo> &item) {
|
||||
return (static_cast<uint32_t>(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_;
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ int TimerHandler::Set(uint32_t type, std::chrono::nanoseconds when)
|
||||
}
|
||||
|
||||
auto second = std::chrono::duration_cast<std::chrono::seconds>(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);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
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<void (const uint64_t)> _callback,
|
||||
uint32_t _flags,
|
||||
uint64_t _uid)
|
||||
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<void(const uint64_t)> _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},
|
||||
|
||||
@@ -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<std::shared_ptr<Batch>> &list, const std::shared_ptr<Batch> &batch);
|
||||
extern steady_clock::time_point MaxTriggerTime(steady_clock::time_point now,
|
||||
@@ -68,7 +71,7 @@ uint64_t TimerManager::CreateTimer(int type,
|
||||
uint64_t uid)
|
||||
{
|
||||
TIME_HILOGI(TIME_MODULE_SERVICE,
|
||||
"Create timer:%{public}d windowLength:%{public}" PRId64"interval:%{public}" PRId64"flag:%{public}d",
|
||||
"Create timer: %{public}d windowLength:%{public}" PRId64 "interval:%{public}" PRId64 "flag:%{public}d",
|
||||
type,
|
||||
windowLength,
|
||||
interval,
|
||||
@@ -84,7 +87,8 @@ uint64_t TimerManager::CreateTimer(int type,
|
||||
interval,
|
||||
flag,
|
||||
std::move(callback),
|
||||
uid});
|
||||
uid
|
||||
});
|
||||
std::lock_guard<std::mutex> lock(entryMapMutex_);
|
||||
timerEntryMap_.insert(std::make_pair(timerNumber, timerInfo));
|
||||
return timerNumber;
|
||||
@@ -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) {
|
||||
@@ -238,7 +242,7 @@ void TimerManager::RemoveLocked(uint64_t id)
|
||||
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,8 +343,6 @@ void TimerManager::TimerLooper()
|
||||
lastTimeChangeClockTime = lastTimeChangeClockTime_;
|
||||
expectedClockTime = lastTimeChangeClockTime + (duration_cast<milliseconds>(nowElapsed.time_since_epoch()) -
|
||||
duration_cast<milliseconds>(lastTimeChangeRealtime_.time_since_epoch()));
|
||||
|
||||
|
||||
if (lastTimeChangeClockTime == system_clock::time_point::min()
|
||||
|| nowRtc < (expectedClockTime - milliseconds(ONE_THOUSAND))
|
||||
|| nowRtc > (expectedClockTime + milliseconds(ONE_THOUSAND))) {
|
||||
@@ -348,7 +350,6 @@ void TimerManager::TimerLooper()
|
||||
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<std::mutex> lock(mutex_);
|
||||
@@ -399,7 +399,8 @@ bool TimerManager::TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &
|
||||
alarm->count = 1;
|
||||
triggerList.push_back(alarm);
|
||||
if (alarm->repeatInterval > milliseconds::zero()) {
|
||||
alarm->count += duration_cast<milliseconds>(nowElapsed - alarm->expectedWhenElapsed) / alarm->repeatInterval;
|
||||
alarm->count += duration_cast<milliseconds>(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,
|
||||
@@ -412,10 +413,10 @@ bool TimerManager::TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &
|
||||
}
|
||||
}
|
||||
std::sort(triggerList.begin(),
|
||||
triggerList.end(),
|
||||
[](const std::shared_ptr<TimerInfo> &l, const std::shared_ptr<TimerInfo> &r) {
|
||||
return l->whenElapsed < r->whenElapsed;
|
||||
});
|
||||
triggerList.end(),
|
||||
[] (const std::shared_ptr<TimerInfo> &l, const std::shared_ptr<TimerInfo> &r) {
|
||||
return l->whenElapsed < r->whenElapsed;
|
||||
});
|
||||
|
||||
return hasWakeup;
|
||||
}
|
||||
@@ -444,10 +445,11 @@ void TimerManager::RescheduleKernelTimerLocked()
|
||||
|
||||
std::shared_ptr<Batch> TimerManager::FindFirstWakeupBatchLocked()
|
||||
{
|
||||
auto it = std::find_if (alarmBatches_.begin(),alarmBatches_.end(),
|
||||
[](const std::shared_ptr<Batch> &batch) {
|
||||
return batch->HasWakeups();
|
||||
});
|
||||
auto it = std::find_if(alarmBatches_.begin(),
|
||||
alarmBatches_.end(),
|
||||
[](const std::shared_ptr<Batch> &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<std::shared_ptr<TimerInfo>> &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,7 +508,9 @@ void TimerManager::DeliverTimersLocked(const std::vector<std::shared_ptr<TimerIn
|
||||
bool AddBatchLocked(std::vector<std::shared_ptr<Batch>> &list, const std::shared_ptr<Batch> &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<Batch> &first, const std::shared_ptr<Batch> &second) {
|
||||
return first->GetStart() < second->GetStart();
|
||||
});
|
||||
@@ -515,14 +519,16 @@ bool AddBatchLocked(std::vector<std::shared_ptr<Batch>> &list, const std::shared
|
||||
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<milliseconds>(triggerAtTime - now)) : interval;
|
||||
if (futurity < MIN_FUZZABLE_INTERVAL) {
|
||||
futurity = milliseconds::zero();
|
||||
}
|
||||
return triggerAtTime + milliseconds(static_cast<long>(0.75 * futurity.count()));
|
||||
return triggerAtTime + milliseconds(static_cast<long>(BATCH_WINDOW_COE * futurity.count()));
|
||||
}
|
||||
} // MiscServices
|
||||
} // OHOS
|
||||
Reference in New Issue
Block a user