diff --git a/OAT.xml b/OAT.xml
index 174f95278b0f829bedd758b02710fd66e93fcefe..673594280d908adee694cf171ff76cd9d98a8b14 100755
--- a/OAT.xml
+++ b/OAT.xml
@@ -35,45 +35,12 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/README.md b/README.md
index 7ef85618861d9420c2b04bb53d812f7c9b3d1374..86646ee05049a20a0aada2252594c3806e154d2b 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 9053661e4c37a99737666fafc37fd5686d801d07..4023602bdcfc2339178269afe4aeac64b8637229 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 0f8a1b7aa9d952f64aff14dbdd3539c28a3f431b..0574caa06d886c260f97b4193eb8082da1688b2e 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 69%
rename from interfaces/plugin/vibrate/include/vibrator_napi_utils.h
rename to interfaces/plugin/vibrator/include/vibrator_napi_utils.h
index f0720499f27f6ba127a5fd09e884421caa4ea6eb..a73f01b5024fe7b65ba25ae2cd71f7d48d77bb63 100755
--- a/interfaces/plugin/vibrate/include/vibrator_napi_utils.h
+++ b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h
@@ -15,14 +15,24 @@
#include "napi/native_api.h"
#include "napi/native_node_api.h"
+#include
#include
+using std::string;
+const int32_t CALLBACK_RESULT_LENGTH = 2;
+struct AsyncCallbackError {
+ int32_t code;
+ string message;
+ string name;
+ 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);
@@ -30,6 +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);
\ 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/vibrate/src/vibrate_js.cpp b/interfaces/plugin/vibrator/src/vibrator_js.cpp
similarity index 52%
rename from interfaces/plugin/vibrate/src/vibrate_js.cpp
rename to interfaces/plugin/vibrator/src/vibrator_js.cpp
index 1c1395241ebcae3c0816a9a2a7b1d96f1134ae45..8eae8dd5f933165861b15679d0a8c78ff0378f89 100755
--- a/interfaces/plugin/vibrate/src/vibrate_js.cpp
+++ b/interfaces/plugin/vibrator/src/vibrator_js.cpp
@@ -25,70 +25,56 @@
#include "vibrator_napi_utils.h"
using namespace OHOS::HiviewDFX;
-static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002757, "VibrateJsAPI" };
-constexpr int32_t ARGS_LENGTH = 2;
+static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002757, "VibratorJsAPI" };
+
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->status = 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);
- }
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->status = 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);
- } 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->status = 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->status = 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/vibrate/src/vibrator_napi_utils.cpp b/interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp
similarity index 69%
rename from interfaces/plugin/vibrate/src/vibrator_napi_utils.cpp
rename to interfaces/plugin/vibrator/src/vibrator_napi_utils.cpp
index ce5e980509d40dc5e996ab04b22de9f624e78a8c..f227990e09b47fffaf99f5fc58a6bc767998c739 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)
{
@@ -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,6 +72,25 @@ int64_t GetCppInt64(napi_value value, napi_env env)
return number;
}
+napi_value GreateBusinessError(napi_env env, int32_t errCode, string errMessage, string errName, 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,16 +108,13 @@ 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);
}
- 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;
@@ -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 15fb68f443736729e8bd58a817c8d324de339937..a3d869f0950b5fe642e39eb989717ea6c72c3033 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"
]
}
}