From 2c620e60eb600c77b7589f8d228a571db63456c3 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 30 Nov 2021 02:33:23 +0000 Subject: [PATCH 1/2] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- OAT.xml | 33 -------------- README.md | 22 +++++----- README_zh.md | 20 ++++----- interfaces/plugin/BUILD.gn | 8 ++-- .../include/vibrator_napi_utils.h | 13 +++++- .../src/vibrator_js.cpp} | 14 +++--- .../src/vibrator_napi_utils.cpp | 43 +++++++++++++------ .../test/unittest/BUILD.gn | 0 .../test/unittest/ExampleJsunit.test.js | 0 .../test/unittest/config.json | 0 ohos.build | 2 +- 11 files changed, 74 insertions(+), 81 deletions(-) rename interfaces/plugin/{vibrate => vibrator}/include/vibrator_napi_utils.h (76%) rename interfaces/plugin/{vibrate/src/vibrate_js.cpp => vibrator/src/vibrator_js.cpp} (93%) rename interfaces/plugin/{vibrate => vibrator}/src/vibrator_napi_utils.cpp (71%) rename interfaces/plugin/{vibrate => vibrator}/test/unittest/BUILD.gn (100%) rename interfaces/plugin/{vibrate => vibrator}/test/unittest/ExampleJsunit.test.js (100%) rename interfaces/plugin/{vibrate => vibrator}/test/unittest/config.json (100%) diff --git a/OAT.xml b/OAT.xml index 174f952..6735942 100755 --- a/OAT.xml +++ b/OAT.xml @@ -35,45 +35,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/README.md b/README.md index 7ef8561..86646ee 100755 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ The APIs provided for the vibrator are used to trigger and stop vibration. The f ### How to Use -1. Import the **vibrate** package. +1. Import the **vibrator** package. 2. Trigger vibration with a specific duration. 3. Stop vibration that is triggered with a specific duration. 4. Trigger vibration with a specific effect. @@ -125,39 +125,39 @@ The APIs provided for the vibrator are used to trigger and stop vibration. The f The following sample code provides a complete process of using the vibrator APIs: ``` -//Step 1 Import the vibrate package. -import vibrate from '@ohos.vibrate'; +//Step 1 Import the vibrator package. +import vibrator from '@ohos.vibrator'; export default { onCreate() { console.info('MiscdeviceJsAPI AceApplication onCreate'); // Step 2 Trigger vibration with a specific duration. - vibrate.vibrate(100, function(error) { + vibrator.vibrate(100, function(error) { if (error) { - console.error("Failed to trigger vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); // Step 3 Stop vibration that is triggered with a specific duration. - vibrate.stop("time", function(error) { + vibrator.stop("time", function(error) { if (error) { - console.error("Failed to stop vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); }); // Step 4 Trigger vibration based on with a specific effect. - vibrate.vibrate("haptic.clock.timer", function(error) { + vibrator.vibrate("haptic.clock.timer", function(error) { if (error) { - console.error("Failed to trigger vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); // Step 54 Stop vibration that is triggered with a specific effect. - vibrate.stop("preset", function(error) { + vibrator.stop("preset", function(error) { if (error) { - console.error("Failed to stop vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); diff --git a/README_zh.md b/README_zh.md index 9053661..4023602 100755 --- a/README_zh.md +++ b/README_zh.md @@ -116,7 +116,7 @@ ### 使用说明 -1. 导入vibrate包。 +1. 导入vibrator包。 2. 触发马达按照指定持续时间振动。 3. 停止马达按照指定持续时间振动。 4. 触发按照指定振动效果字符串振动。 @@ -126,38 +126,38 @@ ``` //步骤1 导包 -import vibrate from '@ohos.vibrate'; +import vibrator from '@ohos.vibrator'; export default { onCreate() { console.info('MiscdeviceJsAPI AceApplication onCreate'); //步骤2 触发马达按照指定持续的时间振动 - vibrate.vibrate(100, function(error) { + vibrator.vibrate(100, function(error) { if (error) { - console.error("Failed to trigger vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); //步骤3 停止马达按照指定持续的时间振动 - vibrate.stop("time", function(error) { + vibrator.stop("time", function(error) { if (error) { - console.error("Failed to stop vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); }); //步骤4 触发马达按照指定的字符串效果振动 - vibrate.vibrate("haptic.clock.timer", function(error) { + vibrator.vibrate("haptic.clock.timer", function(error) { if (error) { - console.error("Failed to trigger vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); //步骤5 停止马达按照指定的字符串效果振动 - vibrate.stop("preset", function(error) { + vibrator.stop("preset", function(error) { if (error) { - console.error("Failed to stop vibration. Error code: " + error.code + "; message: " + error.message); + console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); diff --git a/interfaces/plugin/BUILD.gn b/interfaces/plugin/BUILD.gn index 0f8a1b7..0574caa 100755 --- a/interfaces/plugin/BUILD.gn +++ b/interfaces/plugin/BUILD.gn @@ -19,17 +19,17 @@ ohos_shared_library("vibrator") { "//native_engine", "../native/vibrator/include", "./include", - "./vibrate/include", + "./vibrator/include", "//utils/native/base/include", ] defines = [ - "APP_LOG_TAG = \"vibrateJs\"", + "APP_LOG_TAG = \"vibratorJs\"", "LOG_DOMAIN = 0xD002700", ] sources = [ - "./vibrate/src/vibrate_js.cpp", - "./vibrate/src/vibrator_napi_utils.cpp", + "./vibrator/src/vibrator_js.cpp", + "./vibrator/src/vibrator_napi_utils.cpp", ] deps = [ diff --git a/interfaces/plugin/vibrate/include/vibrator_napi_utils.h b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h similarity index 76% rename from interfaces/plugin/vibrate/include/vibrator_napi_utils.h rename to interfaces/plugin/vibrator/include/vibrator_napi_utils.h index f072049..a69c4ee 100755 --- a/interfaces/plugin/vibrate/include/vibrator_napi_utils.h +++ b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h @@ -15,14 +15,22 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" +#include #include +struct AsyncCallbackError { + int32_t code; + std::string message; + std::string name; + std::string stack; +}; + struct AsyncCallbackInfo { napi_env env; napi_async_work asyncWork; napi_deferred deferred; napi_ref callback[1] = { 0 }; - int32_t status; + AsyncCallbackError error; }; bool IsMatchType(napi_value value, napi_valuetype type, napi_env env); @@ -32,4 +40,5 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *async_callback_info); int64_t GetCppInt64(napi_value value, napi_env env); napi_value NapiGetNamedProperty(napi_value jsonObject, std::string name, napi_env env); void EmitPromiseWork(AsyncCallbackInfo *asyncCallbackInfo); -std::string GetCppString(napi_value value, napi_env env); \ No newline at end of file +std::string GetCppString(napi_value value, napi_env env); +napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage, std::string errName, std::string errStack); \ No newline at end of file diff --git a/interfaces/plugin/vibrate/src/vibrate_js.cpp b/interfaces/plugin/vibrator/src/vibrator_js.cpp similarity index 93% rename from interfaces/plugin/vibrate/src/vibrate_js.cpp rename to interfaces/plugin/vibrator/src/vibrator_js.cpp index 1c13952..9f9c17f 100755 --- a/interfaces/plugin/vibrate/src/vibrate_js.cpp +++ b/interfaces/plugin/vibrator/src/vibrator_js.cpp @@ -25,7 +25,7 @@ #include "vibrator_napi_utils.h" using namespace OHOS::HiviewDFX; -static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002757, "VibrateJsAPI" }; +static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002757, "VibratorJsAPI" }; constexpr int32_t ARGS_LENGTH = 2; static napi_value Vibrate(napi_env env, napi_callback_info info) @@ -50,13 +50,13 @@ static napi_value Vibrate(napi_env env, napi_callback_info info) napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); if (IsMatchType(args[0], napi_number, env)) { int32_t duration = GetCppInt32(args[0], env); - asyncCallbackInfo->status = StartVibratorOnce(duration); + asyncCallbackInfo->error.code = StartVibratorOnce(duration); } else if (IsMatchType(args[0], napi_string, env)) { size_t bufLength = 0; napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &bufLength); char *buff = (char *)malloc((bufLength + 1) * sizeof(char)); status = napi_get_value_string_utf8(env, args[0], buff, bufLength + 1, &bufLength); - asyncCallbackInfo->status = StartVibrator(buff); + asyncCallbackInfo->error.code = StartVibrator(buff); } EmitAsyncCallbackWork(asyncCallbackInfo); } else if (argc == 1) { @@ -71,13 +71,13 @@ static napi_value Vibrate(napi_env env, napi_callback_info info) asyncCallbackInfo->deferred = deferred; if (IsMatchType(args[0], napi_number, env)) { int32_t duration = GetCppInt32(args[0], env); - asyncCallbackInfo->status = StartVibratorOnce(duration); + asyncCallbackInfo->error.code = StartVibratorOnce(duration); } else if (IsMatchType(args[0], napi_string, env)) { size_t bufLength = 0; napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &bufLength); char *buff = (char *)malloc((bufLength + 1) * sizeof(char)); status = napi_get_value_string_utf8(env, args[0], buff, bufLength + 1, &bufLength); - asyncCallbackInfo->status = StartVibrator(buff); + asyncCallbackInfo->error.code = StartVibrator(buff); } else { HiLog::Error(LABEL, "%{public}s input parameter type does not match", __func__); return nullptr; @@ -111,7 +111,7 @@ static napi_value Stop(napi_env env, napi_callback_info info) }; napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); const char *mode = GetCppString(args[0], env).c_str(); - asyncCallbackInfo->status = StopVibrator(mode); + asyncCallbackInfo->error.code = StopVibrator(mode); EmitAsyncCallbackWork(asyncCallbackInfo); } else if (argc == 1) { if (!IsMatchType(args[0], napi_string, env)) { @@ -128,7 +128,7 @@ static napi_value Stop(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); asyncCallbackInfo->deferred = deferred; const char *mode = GetCppString(args[0], env).c_str(); - asyncCallbackInfo->status = StopVibrator(mode); + asyncCallbackInfo->error.code = StopVibrator(mode); EmitPromiseWork(asyncCallbackInfo); return promise; } else { diff --git a/interfaces/plugin/vibrate/src/vibrator_napi_utils.cpp b/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp similarity index 71% rename from interfaces/plugin/vibrate/src/vibrator_napi_utils.cpp rename to interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp index ce5e980..89562e1 100755 --- a/interfaces/plugin/vibrate/src/vibrator_napi_utils.cpp +++ b/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp @@ -19,7 +19,7 @@ using namespace OHOS::HiviewDFX; -static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002708, "VibrateJsAPI"}; +static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002708, "VibratorJsAPI"}; bool IsMatchType(napi_value value, napi_valuetype type, napi_env env) { @@ -72,6 +72,25 @@ int64_t GetCppInt64(napi_value value, napi_env env) return number; } +napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage, std::string errName, std::string errStack) +{ + napi_value result = nullptr; + napi_value code = nullptr; + napi_value message = nullptr; + napi_value name = nullptr; + napi_value stack = nullptr; + NAPI_CALL(env, napi_create_int32(env, errCode, &code)); + NAPI_CALL(env, napi_create_string_utf8(env, errMessage.data(), NAPI_AUTO_LENGTH, &message)); + NAPI_CALL(env, napi_create_string_utf8(env, errName.data(), NAPI_AUTO_LENGTH, &name)); + NAPI_CALL(env, napi_create_string_utf8(env, errStack.data(), NAPI_AUTO_LENGTH, &stack)); + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_set_named_property(env, result, "code", code)); + NAPI_CALL(env, napi_set_named_property(env, result, "message", message)); + NAPI_CALL(env, napi_set_named_property(env, result, "name", name)); + NAPI_CALL(env, napi_set_named_property(env, result, "stack", stack)); + return result; +} + void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) { HiLog::Debug(LABEL, "%s begin", __func__); @@ -89,12 +108,9 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); napi_value result = nullptr; napi_value callResult = nullptr; - if (asyncCallbackInfo->status < 0) { - napi_value code = nullptr; - napi_value message = nullptr; - napi_create_string_utf8(env, "-1", NAPI_AUTO_LENGTH, &code); - napi_create_string_utf8(env, "failed", NAPI_AUTO_LENGTH, &message); - napi_create_error(env, code, message, &result); + if (asyncCallbackInfo->error.code < 0) { + result = GreateBusinessError(env, asyncCallbackInfo->error.code, asyncCallbackInfo->error.message, + asyncCallbackInfo->error.name, asyncCallbackInfo->error.stack); } else { napi_get_undefined(env, &result); } @@ -121,13 +137,14 @@ void EmitPromiseWork(AsyncCallbackInfo *asyncCallbackInfo) asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value message = nullptr; - if (asyncCallbackInfo->status == 0) { - napi_get_undefined(env, &message); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, message); + napi_value result = nullptr; + if (asyncCallbackInfo->error.code == 0) { + napi_get_undefined(env, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); } else { - napi_create_int32(env, asyncCallbackInfo->status, &message); - napi_reject_deferred(env, asyncCallbackInfo->deferred, message); + result = GreateBusinessError(env, asyncCallbackInfo->error.code, asyncCallbackInfo->error.message, + asyncCallbackInfo->error.name, asyncCallbackInfo->error.stack); + napi_reject_deferred(env, asyncCallbackInfo->deferred, result); } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; diff --git a/interfaces/plugin/vibrate/test/unittest/BUILD.gn b/interfaces/plugin/vibrator/test/unittest/BUILD.gn similarity index 100% rename from interfaces/plugin/vibrate/test/unittest/BUILD.gn rename to interfaces/plugin/vibrator/test/unittest/BUILD.gn diff --git a/interfaces/plugin/vibrate/test/unittest/ExampleJsunit.test.js b/interfaces/plugin/vibrator/test/unittest/ExampleJsunit.test.js similarity index 100% rename from interfaces/plugin/vibrate/test/unittest/ExampleJsunit.test.js rename to interfaces/plugin/vibrator/test/unittest/ExampleJsunit.test.js diff --git a/interfaces/plugin/vibrate/test/unittest/config.json b/interfaces/plugin/vibrator/test/unittest/config.json similarity index 100% rename from interfaces/plugin/vibrate/test/unittest/config.json rename to interfaces/plugin/vibrator/test/unittest/config.json diff --git a/ohos.build b/ohos.build index 15fb68f..a3d869f 100755 --- a/ohos.build +++ b/ohos.build @@ -23,7 +23,7 @@ } ], "test_list": [ - "//base/sensors/miscdevice/interfaces/plugin/vibrate/test/unittest:unittest" + "//base/sensors/miscdevice/interfaces/plugin/vibrator/test/unittest:unittest" ] } } -- Gitee From ed9e8e6cce5774eeadc69cfeb23b383b423e92c2 Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 30 Nov 2021 07:10:43 +0000 Subject: [PATCH 2/2] Signed-off-by:hellohyh001 Signed-off-by: h00514358 --- .../vibrator/include/vibrator_napi_utils.h | 15 ++- .../plugin/vibrator/src/vibrator_js.cpp | 125 ++++++++---------- .../vibrator/src/vibrator_napi_utils.cpp | 8 +- 3 files changed, 66 insertions(+), 82 deletions(-) diff --git a/interfaces/plugin/vibrator/include/vibrator_napi_utils.h b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h index a69c4ee..a73f01b 100755 --- a/interfaces/plugin/vibrator/include/vibrator_napi_utils.h +++ b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h @@ -18,11 +18,13 @@ #include #include +using std::string; +const int32_t CALLBACK_RESULT_LENGTH = 2; struct AsyncCallbackError { int32_t code; - std::string message; - std::string name; - std::string stack; + string message; + string name; + string stack; }; struct AsyncCallbackInfo { @@ -38,7 +40,8 @@ napi_value GetNapiInt32(int32_t number, napi_env env); int32_t GetCppInt32(napi_value value, napi_env env); void EmitAsyncCallbackWork(AsyncCallbackInfo *async_callback_info); int64_t GetCppInt64(napi_value value, napi_env env); -napi_value NapiGetNamedProperty(napi_value jsonObject, std::string name, napi_env env); +napi_value NapiGetNamedProperty(napi_value jsonObject, string name, napi_env env); void EmitPromiseWork(AsyncCallbackInfo *asyncCallbackInfo); -std::string GetCppString(napi_value value, napi_env env); -napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage, std::string errName, std::string errStack); \ No newline at end of file +string GetCppString(napi_value value, napi_env env); +napi_value GreateBusinessError(napi_env env, int32_t errCode, string errMessage, + string errName, string errStack); \ No newline at end of file diff --git a/interfaces/plugin/vibrator/src/vibrator_js.cpp b/interfaces/plugin/vibrator/src/vibrator_js.cpp index 9f9c17f..8eae8dd 100755 --- a/interfaces/plugin/vibrator/src/vibrator_js.cpp +++ b/interfaces/plugin/vibrator/src/vibrator_js.cpp @@ -26,69 +26,55 @@ using namespace OHOS::HiviewDFX; static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002757, "VibratorJsAPI" }; -constexpr int32_t ARGS_LENGTH = 2; + static napi_value Vibrate(napi_env env, napi_callback_info info) { size_t argc = 2; - napi_value args[2]; + napi_value args[CALLBACK_RESULT_LENGTH]; napi_value thisArg; - napi_value result; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr)); - if (argc == ARGS_LENGTH) { - if (!IsMatchType(args[1], napi_function, env) || - (!IsMatchType(args[0], napi_number, env) && !IsMatchType(args[0], napi_string, env))) { - HiLog::Error(LABEL, "%{public}s input parameter type does not match", __func__); - napi_get_undefined(env, &result); - return result; + if (argc < 1 || argc > CALLBACK_RESULT_LENGTH) { + HiLog::Error(LABEL, "%{public}s the number of input parameters does not match", __func__); + return nullptr; + } + if (!IsMatchType(args[0], napi_number, env) && !IsMatchType(args[0], napi_string, env)) { + HiLog::Error(LABEL, "%{public}s input parameter type does not match number or string", __func__); + return nullptr; + } + AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; + if (IsMatchType(args[0], napi_number, env)) { + int32_t duration = GetCppInt32(args[0], env); + asyncCallbackInfo->error.code = StartVibratorOnce(duration); + } else if (IsMatchType(args[0], napi_string, env)) { + size_t bufLength = 0; + napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &bufLength); + char *vibratorEffect = (char *)malloc((bufLength + 1) * sizeof(char)); + status = napi_get_value_string_utf8(env, args[0], vibratorEffect, bufLength + 1, &bufLength); + asyncCallbackInfo->error.code = StartVibrator(vibratorEffect); + } + if (argc == CALLBACK_RESULT_LENGTH) { + if (!IsMatchType(args[1], napi_function, env)) { + HiLog::Error(LABEL, "%{public}s input parameter type does not match function", __func__); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + return nullptr; } - AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - }; napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); - if (IsMatchType(args[0], napi_number, env)) { - int32_t duration = GetCppInt32(args[0], env); - asyncCallbackInfo->error.code = StartVibratorOnce(duration); - } else if (IsMatchType(args[0], napi_string, env)) { - size_t bufLength = 0; - napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &bufLength); - char *buff = (char *)malloc((bufLength + 1) * sizeof(char)); - status = napi_get_value_string_utf8(env, args[0], buff, bufLength + 1, &bufLength); - asyncCallbackInfo->error.code = StartVibrator(buff); - } EmitAsyncCallbackWork(asyncCallbackInfo); } else if (argc == 1) { - AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - }; napi_deferred deferred; napi_value promise; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); asyncCallbackInfo->deferred = deferred; - if (IsMatchType(args[0], napi_number, env)) { - int32_t duration = GetCppInt32(args[0], env); - asyncCallbackInfo->error.code = StartVibratorOnce(duration); - } else if (IsMatchType(args[0], napi_string, env)) { - size_t bufLength = 0; - napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &bufLength); - char *buff = (char *)malloc((bufLength + 1) * sizeof(char)); - status = napi_get_value_string_utf8(env, args[0], buff, bufLength + 1, &bufLength); - asyncCallbackInfo->error.code = StartVibrator(buff); - } else { - HiLog::Error(LABEL, "%{public}s input parameter type does not match", __func__); - return nullptr; - } EmitPromiseWork(asyncCallbackInfo); return promise; - } else { - HiLog::Error(LABEL, "%{public}s the number of input parameters does not match", __func__); } - napi_get_undefined(env, &result); - return result; + return nullptr; } static napi_value Stop(napi_env env, napi_callback_info info) @@ -96,46 +82,41 @@ static napi_value Stop(napi_env env, napi_callback_info info) size_t argc = 2; napi_value args[2]; napi_value thisArg; - napi_value result; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr)); - if (argc == ARGS_LENGTH) { + if (argc < 1 || argc > CALLBACK_RESULT_LENGTH) { + HiLog::Error(LABEL, "%{public}s the number of input parameters does not match", __func__); + return nullptr; + } + if (!IsMatchType(args[0], napi_string, env)) { + HiLog::Error(LABEL, "%{public}s input parameter type does not match string", __func__); + return nullptr; + } + AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + }; + const char *mode = GetCppString(args[0], env).c_str(); + asyncCallbackInfo->error.code = StopVibrator(mode); + + if (argc == CALLBACK_RESULT_LENGTH) { if (!IsMatchType(args[1], napi_function, env) || !IsMatchType(args[0], napi_string, env)) { HiLog::Error(LABEL, "%{public}s input parameter type does not match", __func__); - napi_get_undefined(env, &result); - return result; + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + return nullptr; } - AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - }; napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]); - const char *mode = GetCppString(args[0], env).c_str(); - asyncCallbackInfo->error.code = StopVibrator(mode); EmitAsyncCallbackWork(asyncCallbackInfo); } else if (argc == 1) { - if (!IsMatchType(args[0], napi_string, env)) { - HiLog::Error(LABEL, "%{public}s input parameter type does not match", __func__); - return nullptr; - } - AsyncCallbackInfo *asyncCallbackInfo = new AsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - }; napi_deferred deferred; napi_value promise; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); asyncCallbackInfo->deferred = deferred; - const char *mode = GetCppString(args[0], env).c_str(); - asyncCallbackInfo->error.code = StopVibrator(mode); EmitPromiseWork(asyncCallbackInfo); return promise; - } else { - HiLog::Error(LABEL, "%{public}s the number of input parameters does not match", __func__); } - napi_get_undefined(env, &result); - return result; + return nullptr; } EXTERN_C_START diff --git a/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp b/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp index 89562e1..f227990 100755 --- a/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp +++ b/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp @@ -39,7 +39,7 @@ napi_value GetNapiInt32(int32_t number, napi_env env) return value; } -napi_value NapiGetNamedProperty(napi_value jsonObject, std::string name, napi_env env) +napi_value NapiGetNamedProperty(napi_value jsonObject, string name, napi_env env) { napi_value value; napi_get_named_property(env, jsonObject, name.c_str(), &value); @@ -53,7 +53,7 @@ int32_t GetCppInt32(napi_value value, napi_env env) return number; } -std::string GetCppString(napi_value value, napi_env env) +string GetCppString(napi_value value, napi_env env) { size_t bufLength = 0; napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength); @@ -72,7 +72,7 @@ int64_t GetCppInt64(napi_value value, napi_env env) return number; } -napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage, std::string errName, std::string errStack) +napi_value GreateBusinessError(napi_env env, int32_t errCode, string errMessage, string errName, string errStack) { napi_value result = nullptr; napi_value code = nullptr; @@ -114,7 +114,7 @@ void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo) } else { napi_get_undefined(env, &result); } - napi_call_function(env, nullptr, callback, 2, &result, &callResult); + napi_call_function(env, nullptr, callback, 1, &result, &callResult); napi_delete_reference(env, asyncCallbackInfo->callback[0]); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; -- Gitee