From e0e889b4b23698a0f4ee5f609e36dba8a7d242f8 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Thu, 29 Feb 2024 06:30:26 +0000 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=8C=AF=E5=8A=A8=E7=A7=BB=E5=85=A5js?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E7=BA=BF=E7=A8=8B=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: I7b61728f5b22a927225a9545e38642316ae41485 --- .../vibrator/include/vibrator_napi_utils.h | 19 ++ .../js/napi/vibrator/src/vibrator_js.cpp | 116 ++++++------ .../napi/vibrator/src/vibrator_napi_utils.cpp | 174 +++++++++++------- .../inner_api/vibrator/vibrator_agent.h | 4 +- .../inner_api/vibrator/vibrator_agent_type.h | 16 +- utils/common/include/vibrator_infos.h | 11 ++ 6 files changed, 213 insertions(+), 127 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h b/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h index 8ccf665..6965381 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h @@ -25,6 +25,7 @@ #include "refbase.h" #include "sensors_errors.h" +#include "vibrator_infos.h" namespace OHOS { namespace Sensors { @@ -38,6 +39,16 @@ enum CallbackType { IS_SUPPORT_EFFECT_CALLBACK, }; +enum class VibrateType { + CANCEL, + IS_SUPPORT_EFFECT_CALLBACK, + START_VIBRATE, + START_VIBRATE_TIME, + STOP_VIBRATOR, + VIBRATE_EFFECT_ID, + SYSTEM_VIBRATE_CALLBACK, +}; + class AsyncCallbackInfo : public RefBase { public: struct AsyncCallbackError { @@ -53,7 +64,12 @@ public: napi_ref callback[CALLBACK_NUM] = {0}; AsyncCallbackError error; CallbackType callbackType = COMMON_CALLBACK; + VibrateType vibrateType; bool isSupportEffect {false}; + VibrateMsg msg; + std::string effectId; + std::string mode; + int32_t duration {0}; AsyncCallbackInfo(napi_env env) : env(env) {} ~AsyncCallbackInfo(); }; @@ -76,6 +92,9 @@ bool ConstructCommonResult(const napi_env &env, sptr asyncCal int32_t length); bool ConstructIsSupportEffectResult(const napi_env &env, sptr asyncCallbackInfo, napi_value result[], int32_t length); +void ExecuteAsyncWork(napi_env env, void *data); +void CompleteCallback(napi_env env, napi_status status, void *data); +void CompletePromise(napi_env env, napi_status status, void *data); void EmitAsyncCallbackWork(sptr async_callback_info); void EmitPromiseWork(sptr asyncCallbackInfo); } // namespace Sensors diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index bb5aed3..e2c7e2b 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -50,17 +50,6 @@ static std::map g_usageType = { {"simulateReality", USAGE_SIMULATE_REALITY}, }; -struct VibrateInfo { - std::string type; - std::string usage; - int32_t duration = 0; - std::string effectId; - int32_t count = 0; - int32_t fd = -1; - int64_t offset = 0; - int64_t length = -1; -}; - static napi_value EmitAsyncWork(napi_value param, sptr info) { CHKPP(info); @@ -93,7 +82,8 @@ static napi_value VibrateTime(napi_env env, napi_value args[], size_t argc) NAPI_ASSERT(env, GetInt32Value(env, args[0], duration), "Get int number fail"); sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); - asyncCallbackInfo->error.code = StartVibratorOnce(duration); + asyncCallbackInfo->vibrateType = VibrateType::START_VIBRATE_TIME; + asyncCallbackInfo->duration = duration; if (argc >= 2 && IsMatchType(env, args[1], napi_function)) { return EmitAsyncWork(args[1], asyncCallbackInfo); } @@ -107,7 +97,8 @@ static napi_value VibrateEffectId(napi_env env, napi_value args[], size_t argc) NAPI_ASSERT(env, GetStringValue(env, args[0], effectId), "Wrong argument type. String or function expected"); sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); - asyncCallbackInfo->error.code = StartVibrator(effectId.c_str()); + asyncCallbackInfo->effectId = effectId; + asyncCallbackInfo->vibrateType = VibrateType::VIBRATE_EFFECT_ID; if (argc >= 2 && IsMatchType(env, args[1], napi_function)) { return EmitAsyncWork(args[1], asyncCallbackInfo); } @@ -157,41 +148,39 @@ static napi_value VibrateMode(napi_env env, napi_value args[], size_t argc) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); asyncCallbackInfo->callbackType = SYSTEM_VIBRATE_CALLBACK; + asyncCallbackInfo->vibrateType = VibrateType::SYSTEM_VIBRATE_CALLBACK; string mode = "long"; if (!GetCallbackInfo(env, args, asyncCallbackInfo, mode)) { MISC_HILOGE("Get callback info fail"); return nullptr; } int32_t duration = ((mode == "long") ? VIBRATE_LONG_DURATION : VIBRATE_SHORT_DURATION); - asyncCallbackInfo->error.code = StartVibratorOnce(duration); - if (asyncCallbackInfo->error.code != SUCCESS) { - asyncCallbackInfo->error.message = "Vibrator vibrate fail"; - } + asyncCallbackInfo->duration = duration; EmitAsyncCallbackWork(asyncCallbackInfo); return nullptr; } -bool ParseParameter(napi_env env, napi_value args[], size_t argc, VibrateInfo &info) +bool ParseParameter(napi_env env, napi_value args[], size_t argc, VibrateMsg &msg) { CHKCF((argc >= 2), "Wrong argument number"); - CHKCF(GetPropertyString(env, args[0], "type", info.type), "Get vibrate type fail"); - if (info.type == "time") { - CHKCF(GetPropertyInt32(env, args[0], "duration", info.duration), "Get vibrate duration fail"); - } else if (info.type == "preset") { - CHKCF(GetPropertyInt32(env, args[0], "count", info.count), "Get vibrate count fail"); - CHKCF(GetPropertyString(env, args[0], "effectId", info.effectId), "Get vibrate effectId fail"); - } else if (info.type == "file") { + CHKCF(GetPropertyString(env, args[0], "type", msg.type), "Get vibrate type fail"); + if (msg.type == "time") { + CHKCF(GetPropertyInt32(env, args[0], "duration", msg.duration), "Get vibrate duration fail"); + } else if (msg.type == "preset") { + CHKCF(GetPropertyInt32(env, args[0], "count", msg.count), "Get vibrate count fail"); + CHKCF(GetPropertyString(env, args[0], "effectId", msg.effectId), "Get vibrate effectId fail"); + } else if (msg.type == "file") { napi_value hapticFd = nullptr; CHKCF(GetPropertyItem(env, args[0], "hapticFd", hapticFd), "Get vibrate hapticFd fail"); CHKCF(IsMatchType(env, hapticFd, napi_object), "Wrong argument type. Napi object expected"); - CHKCF(GetPropertyInt32(env, hapticFd, "fd", info.fd), "Get vibrate fd fail"); - GetPropertyInt64(env, hapticFd, "offset", info.offset); - int64_t fdSize = GetFileSize(info.fd); - CHKCF((info.offset >= 0) && (info.offset <= fdSize), "The parameter of offset is invalid"); - info.length = fdSize - info.offset; - GetPropertyInt64(env, hapticFd, "length", info.length); + CHKCF(GetPropertyInt32(env, hapticFd, "fd", msg.fd), "Get vibrate fd fail"); + GetPropertyInt64(env, hapticFd, "offset", msg.offset); + int64_t fdSize = GetFileSize(msg.fd); + CHKCF((msg.offset >= 0) && (msg.offset <= fdSize), "The parameter of offset is invalid"); + msg.length = fdSize - msg.offset; + GetPropertyInt64(env, hapticFd, "length", msg.length); } - CHKCF(GetPropertyString(env, args[1], "usage", info.usage), "Get vibrate usage fail"); + CHKCF(GetPropertyString(env, args[1], "usage", msg.usage), "Get vibrate usage fail"); return true; } @@ -204,40 +193,54 @@ bool SetUsage(const std::string &usage) return SetUsage(g_usageType[usage]); } -int32_t StartVibrate(const VibrateInfo &info) +int32_t CheckVibrateMsg(const VibrateMsg &msg) { - if (!SetUsage(info.usage)) { + if (!SetUsage(msg.usage)) { MISC_HILOGE("SetUsage fail"); return PARAMETER_ERROR; } - if ((info.type != "time") && (info.type != "preset") && (info.type != "file")) { - MISC_HILOGE("Invalid vibrate type, type:%{public}s", info.type.c_str()); + if ((msg.type != "time") && (msg.type != "preset") && (msg.type != "file")) { + MISC_HILOGE("Invalid vibrate type, type:%{public}s", msg.type.c_str()); return PARAMETER_ERROR; } - if (info.type == "preset") { - if (!SetLoopCount(info.count)) { + if (msg.type == "preset") { + if (!SetLoopCount(msg.count)) { MISC_HILOGE("SetLoopCount fail"); return PARAMETER_ERROR; } - return StartVibrator(info.effectId.c_str()); - } else if (info.type == "file") { - return PlayVibratorCustom(info.fd, info.offset, info.length); + return SUCCESS; + } else if (msg.type == "file") { + #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM + MISC_HILOGD("Time delay measurement:start time"); + if (msg.fd < 0 || msg.offset < 0 || msg.length <= 0) { + MISC_HILOGE("Input parameter invalid, fd:%{public}d, offset:%{public}lld, length:%{public}lld", + fd, static_cast(offset), static_cast(length)); + return PARAMETER_ERROR; + } + return SUCCESS; + #else + MISC_HILOGE("The device does not support this operation"); + return IS_NOT_SUPPORTED; + #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM } - return StartVibratorOnce(info.duration); + if (msg.duration <= 0) { + MISC_HILOGE("duration is invalid"); + return PARAMETER_ERROR; + } + return SUCCESS; } static napi_value VibrateEffect(napi_env env, napi_value args[], size_t argc) { - VibrateInfo info; - if (!ParseParameter(env, args, argc, info)) { + sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); + CHKPP(asyncCallbackInfo); + asyncCallbackInfo->vibrateType = VibrateType::START_VIBRATE; + if (!ParseParameter(env, args, argc, asyncCallbackInfo->msg)) { ThrowErr(env, PARAMETER_ERROR, "parameter fail"); return nullptr; } - sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); - CHKPP(asyncCallbackInfo); - asyncCallbackInfo->error.code = StartVibrate(info); - if ((asyncCallbackInfo->error.code != SUCCESS) && (asyncCallbackInfo->error.code == PARAMETER_ERROR)) { - ThrowErr(env, PARAMETER_ERROR, "parameters invalid"); + if (CheckVibrateMsg(asyncCallbackInfo->msg) == PARAMETER_ERROR) { + ThrowErr(env, PARAMETER_ERROR, "parameter fail"); return nullptr; } if (argc >= 3 && IsMatchType(env, args[2], napi_function)) { @@ -298,7 +301,7 @@ static napi_value Cancel(napi_env env, napi_callback_info info) } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); - asyncCallbackInfo->error.code = Cancel(); + asyncCallbackInfo->vibrateType = VibrateType::CANCEL; if ((argc > 0) && (IsMatchType(env, args[0], napi_function))) { return EmitAsyncWork(args[0], asyncCallbackInfo); } @@ -321,13 +324,15 @@ static napi_value Stop(napi_env env, napi_callback_info info) ThrowErr(env, PARAMETER_ERROR, "Parameters invalid"); return nullptr; } - sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); - CHKPP(asyncCallbackInfo); - asyncCallbackInfo->error.code = StopVibrator(mode.c_str()); - if ((asyncCallbackInfo->error.code != SUCCESS) && (asyncCallbackInfo->error.code == PARAMETER_ERROR)) { + if (mode != "time" && mode != "preset") { + MISC_HILOGE("Input parameter invalid, mode is %{public}s", mode.c_str()); ThrowErr(env, PARAMETER_ERROR, "Parameters invalid"); return nullptr; } + sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); + CHKPP(asyncCallbackInfo); + asyncCallbackInfo->mode = mode; + asyncCallbackInfo->vibrateType = VibrateType::STOP_VIBRATOR; if (argc >= 2 && IsMatchType(env, args[1], napi_function)) { return EmitAsyncWork(args[1], asyncCallbackInfo); } @@ -355,7 +360,8 @@ static napi_value IsSupportEffect(napi_env env, napi_callback_info info) sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); asyncCallbackInfo->callbackType = IS_SUPPORT_EFFECT_CALLBACK; - asyncCallbackInfo->error.code = IsSupportEffect(effectId.c_str(), &asyncCallbackInfo->isSupportEffect); + asyncCallbackInfo->vibrateType = VibrateType::IS_SUPPORT_EFFECT_CALLBACK; + asyncCallbackInfo->effectId = effectId; if ((argc > 1) && (IsMatchType(env, args[1], napi_function))) { return EmitAsyncWork(args[1], asyncCallbackInfo); } diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index 27a16dd..f8d63c4 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -20,6 +20,7 @@ #include "securec.h" #include "miscdevice_log.h" +#include "vibrator_agent.h" #include "vibrator_napi_error.h" #undef LOG_TAG @@ -30,6 +31,7 @@ namespace Sensors { namespace { constexpr int32_t RESULT_LENGTH = 2; } // namespace + AsyncCallbackInfo::~AsyncCallbackInfo() { CALL_LOG_ENTER; @@ -251,6 +253,80 @@ void EmitSystemCallback(const napi_env &env, sptr asyncCallba NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, 1, result, &callResult)); } +void ExecuteAsyncWork(napi_env env, void *data) +{ + CALL_LOG_ENTER; + sptr asyncCallbackInfo(static_cast(data)); + if (asyncCallbackInfo->vibrateType == VibrateType::START_VIBRATE) { + if (asyncCallbackInfo->msg.type == "preset") { + asyncCallbackInfo->error.code = StartVibrator(asyncCallbackInfo->msg.effectId.c_str()); + return; + } else if (asyncCallbackInfo->msg.type == "file") { + asyncCallbackInfo->error.code = PlayVibratorCustom(asyncCallbackInfo->msg.fd, asyncCallbackInfo->msg.offset, + asyncCallbackInfo->msg.length); + return; + } + asyncCallbackInfo->error.code = StartVibratorOnce(asyncCallbackInfo->msg.duration); + } else if (asyncCallbackInfo->vibrateType == VibrateType::START_VIBRATE_TIME) { + asyncCallbackInfo->error.code = StartVibratorOnce(asyncCallbackInfo->duration); + } else if (asyncCallbackInfo->vibrateType == VibrateType::VIBRATE_EFFECT_ID) { + asyncCallbackInfo->error.code = StartVibrator(asyncCallbackInfo->effectId.c_str()); + } else if (asyncCallbackInfo->vibrateType == VibrateType::SYSTEM_VIBRATE_CALLBACK) { + asyncCallbackInfo->error.code = StartVibratorOnce(asyncCallbackInfo->duration); + if (asyncCallbackInfo->error.code != SUCCESS) { + asyncCallbackInfo->error.message = "Vibrator vibrate fail"; + } + } else if (asyncCallbackInfo->vibrateType == VibrateType::IS_SUPPORT_EFFECT_CALLBACK) { + asyncCallbackInfo->error.code = IsSupportEffect(asyncCallbackInfo->effectId.c_str(), + &asyncCallbackInfo->isSupportEffect); + } else if (asyncCallbackInfo->vibrateType == VibrateType::STOP_VIBRATOR) { + asyncCallbackInfo->error.code = StopVibrator(asyncCallbackInfo->mode.c_str()); + } else if (asyncCallbackInfo->vibrateType == VibrateType::CANCEL) { + asyncCallbackInfo->error.code = Cancel(); + } +} + +void CompleteCallback(napi_env env, napi_status status, void *data) +{ + CALL_LOG_ENTER; + sptr asyncCallbackInfo(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); + if (asyncCallbackInfo->vibrateType == VibrateType::SYSTEM_VIBRATE_CALLBACK) { + EmitSystemCallback(env, asyncCallbackInfo); + return; + } + if (asyncCallbackInfo->error.code != SUCCESS) { + if (asyncCallbackInfo->vibrateType == VibrateType::IS_SUPPORT_EFFECT_CALLBACK) { + MISC_HILOGE("IsSupportEffect failed"); + } else if (asyncCallbackInfo->vibrateType == VibrateType::STOP_VIBRATOR) { + MISC_HILOGE("Stop vibtate failed"); + } else if (asyncCallbackInfo->vibrateType == VibrateType::CANCEL) { + MISC_HILOGE("Cancel vibtate failed"); + } + MISC_HILOGE("Start vibrate failed"); + } + CHKPV(asyncCallbackInfo->callback[0]); + napi_value callback = nullptr; + napi_status ret = napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); + CHKCV((ret == napi_ok), "napi_get_reference_value fail"); + napi_value result[RESULT_LENGTH] = { 0 }; + CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), + "Callback type invalid in async work"); + bool state = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, + sizeof(result) / sizeof(napi_value)); + CHKCV(state, "Create napi data fail in async work"); + napi_value callResult = nullptr; + CHKCV((napi_call_function(env, nullptr, callback, 2, result, &callResult) == napi_ok), + "napi_call_function fail"); +} + void EmitAsyncCallbackWork(sptr asyncCallbackInfo) { CALL_LOG_ENTER; @@ -262,37 +338,8 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) CHKCV((ret == napi_ok), "napi_create_string_latin1 fail"); asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work( - env, nullptr, resourceName, [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - CALL_LOG_ENTER; - sptr asyncCallbackInfo(static_cast(data)); - /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the - * asynchronous task is created, and subtract 1 from the reference count after the naked - * pointer is converted to a pointer when the asynchronous task is executed, the reference - * count of the smart pointer is guaranteed to be 1. - */ - asyncCallbackInfo->DecStrongRef(nullptr); - if (asyncCallbackInfo->callbackType == SYSTEM_VIBRATE_CALLBACK) { - EmitSystemCallback(env, asyncCallbackInfo); - return; - } - CHKPV(asyncCallbackInfo->callback[0]); - napi_value callback = nullptr; - napi_status ret = napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - CHKCV((ret == napi_ok), "napi_get_reference_value fail"); - napi_value result[RESULT_LENGTH] = { 0 }; - CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), - "Callback type invalid in async work"); - bool state = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, - sizeof(result) / sizeof(napi_value)); - CHKCV(state, "Create napi data fail in async work"); - napi_value callResult = nullptr; - CHKCV((napi_call_function(env, nullptr, callback, 2, result, &callResult) == napi_ok), - "napi_call_function fail"); - }, - asyncCallbackInfo.GetRefPtr(), &asyncCallbackInfo->asyncWork); + env, nullptr, resourceName, ExecuteAsyncWork, CompleteCallback, asyncCallbackInfo.GetRefPtr(), + &asyncCallbackInfo->asyncWork); if (status != napi_ok || napi_queue_async_work_with_qos( asyncCallbackInfo->env, asyncCallbackInfo->asyncWork, napi_qos_default) != napi_ok) { @@ -301,6 +348,38 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) } } +void CompletePromise(napi_env env, napi_status status, void *data) +{ + CALL_LOG_ENTER; + sptr asyncCallbackInfo(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); + CHKPV(asyncCallbackInfo->deferred); + if (asyncCallbackInfo->vibrateType == VibrateType::SYSTEM_VIBRATE_CALLBACK) { + EmitSystemCallback(env, asyncCallbackInfo); + return; + } + napi_value result[RESULT_LENGTH] = { 0 }; + CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), + "Callback type invalid in promise"); + bool ret = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, + sizeof(result) / sizeof(napi_value)); + CHKCV(ret, "Callback type invalid in promise"); + if (asyncCallbackInfo->error.code != SUCCESS) { + CHKCV((napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]) == napi_ok), + "napi_reject_deferred fail"); + } else { + CHKCV((napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[1]) == napi_ok), + "napi_resolve_deferred fail"); + } +} + void EmitPromiseWork(sptr asyncCallbackInfo) { CALL_LOG_ENTER; @@ -313,37 +392,8 @@ void EmitPromiseWork(sptr asyncCallbackInfo) // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work( - env, nullptr, resourceName, [](napi_env env, void *data) {}, - [](napi_env env, napi_status status, void *data) { - CALL_LOG_ENTER; - sptr asyncCallbackInfo(static_cast(data)); - /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced - * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the - * asynchronous task is created, and subtract 1 from the reference count after the naked - * pointer is converted to a pointer when the asynchronous task is executed, the reference - * count of the smart pointer is guaranteed to be 1. - */ - asyncCallbackInfo->DecStrongRef(nullptr); - CHKPV(asyncCallbackInfo->deferred); - if (asyncCallbackInfo->callbackType == SYSTEM_VIBRATE_CALLBACK) { - EmitSystemCallback(env, asyncCallbackInfo); - return; - } - napi_value result[RESULT_LENGTH] = { 0 }; - CHKCV((g_convertFuncList.find(asyncCallbackInfo->callbackType) != g_convertFuncList.end()), - "Callback type invalid in promise"); - bool ret = g_convertFuncList[asyncCallbackInfo->callbackType](env, asyncCallbackInfo, result, - sizeof(result) / sizeof(napi_value)); - CHKCV(ret, "Callback type invalid in promise"); - if (asyncCallbackInfo->error.code != SUCCESS) { - CHKCV((napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]) == napi_ok), - "napi_reject_deferred fail"); - } else { - CHKCV((napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[1]) == napi_ok), - "napi_resolve_deferred fail"); - } - }, asyncCallbackInfo.GetRefPtr(), &asyncCallbackInfo->asyncWork); + env, nullptr, resourceName, ExecuteAsyncWork, CompletePromise, + asyncCallbackInfo.GetRefPtr(), &asyncCallbackInfo->asyncWork); if (status != napi_ok || napi_queue_async_work_with_qos( asyncCallbackInfo->env, asyncCallbackInfo->asyncWork, napi_qos_default) != napi_ok) { diff --git a/interfaces/inner_api/vibrator/vibrator_agent.h b/interfaces/inner_api/vibrator/vibrator_agent.h index ad4e80c..d2c2be7 100644 --- a/interfaces/inner_api/vibrator/vibrator_agent.h +++ b/interfaces/inner_api/vibrator/vibrator_agent.h @@ -43,14 +43,14 @@ namespace Sensors { * * @since 6 */ -const char *VIBRATOR_STOP_MODE_TIME = "time"; +static const char *VIBRATOR_STOP_MODE_TIME = "time"; /** * @brief Indicates the mode of stopping a preset vibration effect. * * @since 6 */ -const char *VIBRATOR_STOP_MODE_PRESET = "preset"; +static const char *VIBRATOR_STOP_MODE_PRESET = "preset"; /** * @brief Controls this vibrator to perform a vibration with a preset vibration effect. diff --git a/interfaces/inner_api/vibrator/vibrator_agent_type.h b/interfaces/inner_api/vibrator/vibrator_agent_type.h index 73b23f5..ae8553e 100644 --- a/interfaces/inner_api/vibrator/vibrator_agent_type.h +++ b/interfaces/inner_api/vibrator/vibrator_agent_type.h @@ -25,56 +25,56 @@ extern "C" { * * @since 1 */ -const char *VIBRATOR_TYPE_CLOCK_TIMER = "haptic.clock.timer"; +static const char *VIBRATOR_TYPE_CLOCK_TIMER = "haptic.clock.timer"; /** * @brief Describes the vibration effect of the vibrator when authentication fails. * * @since 11 */ -const char *VIBRATOR_TYPE_FAIL = "haptic.fail"; +static const char *VIBRATOR_TYPE_FAIL = "haptic.fail"; /** * @brief Describes the vibration effect of the vibrator when charging. * * @since 11 */ -const char *VIBRATOR_TYPE_CHARGING = "haptic.charging"; +static const char *VIBRATOR_TYPE_CHARGING = "haptic.charging"; /** * @brief Describes the vibration effect of the vibrator when long pressed. * * @since 11 */ -const char *VIBRATOR_TYPE_LONG_PRESS_LIGHT = "haptic.long_press.light"; +static const char *VIBRATOR_TYPE_LONG_PRESS_LIGHT = "haptic.long_press.light"; /** * @brief Describes the vibration effect of the vibrator when long pressed. * * @since 11 */ -const char *VIBRATOR_TYPE_LONG_PRESS_MEDIUM = "haptic.long_press.medium"; +static const char *VIBRATOR_TYPE_LONG_PRESS_MEDIUM = "haptic.long_press.medium"; /** * @brief Describes the vibration effect of the vibrator when long pressed. * * @since 11 */ -const char *VIBRATOR_TYPE_LONG_PRESS_HEAVY = "haptic.long_press.heavy"; +static const char *VIBRATOR_TYPE_LONG_PRESS_HEAVY = "haptic.long_press.heavy"; /** * @brief Describes the vibration effect of the vibrator when slide. * * @since 11 */ -const char *VIBRATOR_TYPE_SLIDE_LIGHT = "haptic.slide.light"; +static const char *VIBRATOR_TYPE_SLIDE_LIGHT = "haptic.slide.light"; /** * @brief Describes the vibration effect of the vibrator when the threshold is reached. * * @since 11 */ -const char *VIBRATOR_TYPE_THRESHOID = "haptic.threshold"; +static const char *VIBRATOR_TYPE_THRESHOID = "haptic.threshold"; /** * @brief Enumerates vibration usages. diff --git a/utils/common/include/vibrator_infos.h b/utils/common/include/vibrator_infos.h index 1647018..ef099cd 100644 --- a/utils/common/include/vibrator_infos.h +++ b/utils/common/include/vibrator_infos.h @@ -124,6 +124,17 @@ struct VibrateInfo { VibratePackage package; }; +struct VibrateMsg { + std::string type; + std::string usage; + int32_t duration = 0; + std::string effectId; + int32_t count = 0; + int32_t fd = -1; + int64_t offset = 0; + int64_t length = -1; +}; + struct VibrateParameter { int32_t intensity = 100; // from 0 to 100 int32_t frequency = 0; // from -100 to 100 -- Gitee