diff --git a/vibrator/BUILD.gn b/vibrator/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..f05e307594d58cc38805cec337bdd7c69c13a48c --- /dev/null +++ b/vibrator/BUILD.gn @@ -0,0 +1,25 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +################################################################################ + +group("sensors_miscdevice_packages") { + if (is_standard_system) { + deps = [ + "interfaces/kits/js/napi:vibrator", + "interfaces/kits/js/declaration:vib", + ] + } +} diff --git a/vibrator/README.md b/vibrator/README.md new file mode 100755 index 0000000000000000000000000000000000000000..0c6e558091cdf04e92420d53307913583d070e9b --- /dev/null +++ b/vibrator/README.md @@ -0,0 +1,29 @@ +# Vibrator + +- [Introduction](#section11660541593) +- [Directory Structure](#section161941989596) +- [Repositories Involved](#section1371113476307) + +## Introduction + +The vibrator module provides APIs for managing vibrator. + +**Figure 1** Subsystem architecture +![](figures/subsystem_architecture.png "subsystem-architecture") + +## Directory Structure + +``` +base/sensors/miscdevice/vibrator +├── figures # Architecture diagram +├── interfaces # APIs for external systems and applications +│ └── kits # APIs +``` + + +## Repositories Involved + +**Misc subsystem** + +sensors\_vibrator + diff --git a/vibrator/README_zh.md b/vibrator/README_zh.md new file mode 100755 index 0000000000000000000000000000000000000000..0ce6b8c9a76a1949ea136e9fcee0adb581047066 --- /dev/null +++ b/vibrator/README_zh.md @@ -0,0 +1,28 @@ +# 振动组件 + +- [简介](#section11660541593) +- [目录](#section161941989596) +- [相关仓](#section1371113476307) + +## 简介 + +振动组件提供管理振动的能力。 + +**图 1** 子系统架构图 +![](figures/subsystem_architecture_zh.png "子系统架构图") + +## 目录 + +``` +base/sensors/miscdevice/vibrator +├── figures # 构架图 +├── interfaces # 组件对外提供的接口代码 + └── kits # 对应用提供的接口 + +``` + +## 相关仓 + +**Misc软件服务子系统** + +sensors\_vibrator diff --git a/vibrator/figures/subsystem_architecture.png b/vibrator/figures/subsystem_architecture.png new file mode 100644 index 0000000000000000000000000000000000000000..da67c3e46e53d0efe4bfa1dd35f15abf61389d0f Binary files /dev/null and b/vibrator/figures/subsystem_architecture.png differ diff --git a/vibrator/figures/subsystem_architecture_zh.png b/vibrator/figures/subsystem_architecture_zh.png new file mode 100644 index 0000000000000000000000000000000000000000..972db5b8facd6b1cd38540d1d5842ead7f95d9ec Binary files /dev/null and b/vibrator/figures/subsystem_architecture_zh.png differ diff --git a/vibrator/interfaces/kits/js/declaration/BUILD.gn b/vibrator/interfaces/kits/js/declaration/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..f27e01061d38827e6d912f7cbd03ec1233137ff9 --- /dev/null +++ b/vibrator/interfaces/kits/js/declaration/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +js_declaration("vib") { + part_name = "sensors_miscdevice" + sources = [ + "./api", + ] +} + +ohos_copy("vib_declaration") { + sources = [ + "./api" + ] + outputs = [ target_out_dir + "/$target_name/" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} \ No newline at end of file diff --git a/vibrator/interfaces/kits/js/declaration/api/@ohos.vibrator.d.ts b/vibrator/interfaces/kits/js/declaration/api/@ohos.vibrator.d.ts new file mode 100755 index 0000000000000000000000000000000000000000..bdb698d202c7030694e60474b693e8b4c877598c --- /dev/null +++ b/vibrator/interfaces/kits/js/declaration/api/@ohos.vibrator.d.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { AsyncCallback, ErrorCallback } from './basic'; + +/** + * vibrator. + */ +declare namespace vibrator { + function vibrate(duration: number, callback?: AsyncCallback): void; + function vibrate(duration: number): Promise; +} + +export default Vibrator; \ No newline at end of file diff --git a/vibrator/interfaces/kits/js/napi/AsyncWorkData.h b/vibrator/interfaces/kits/js/napi/AsyncWorkData.h new file mode 100644 index 0000000000000000000000000000000000000000..a1666afaa518014e84ed8774b5232cd25457e179 --- /dev/null +++ b/vibrator/interfaces/kits/js/napi/AsyncWorkData.h @@ -0,0 +1,167 @@ +/* + * Copyright 2021 Institute of Software Chinese Academy of Sciences, ISRC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NAPI_ASYNCWORKDATA_H +#define NAPI_ASYNCWORKDATA_H + +#include +#include + +class AsyncWorkData +{ +public: + AsyncWorkData(napi_env env) + { + this->env = env; + } + ~AsyncWorkData() + { + for (unsigned int i = 0; i < sizeof(callbacks) / sizeof(napi_ref); ++i) { + if (callbacks[i]) { + napi_delete_reference(env, callbacks[i]); + } + } + if (async_work) { + napi_delete_async_work(env, async_work); + } + } + bool parse_callback_args(size_t argc, napi_value argv[], napi_value *retval) + { + napi_value cb[3] = { nullptr, nullptr, nullptr }; + napi_valuetype type; + + NAPI_ASSERT_BASE(env, argc <= 3, "Wrong number of arguments", false); + for (unsigned int i = 0; i < argc; ++i) { + NAPI_CALL_BASE(env, napi_typeof(env, argv[i], &type), false); + if (type == napi_undefined || type == napi_null) { + continue; + } + NAPI_ASSERT_BASE(env, type == napi_function, "Wrong argument type. Function expected.", false); + cb[i] = argv[i]; + } + for (unsigned int i = 0; i < argc; ++i) { + if (cb[i]) { + NAPI_CALL_BASE(env, napi_create_reference(env, cb[i], 1, &callbacks[i]), false); + } + } + for (unsigned int i = 0; i < sizeof(callbacks) / sizeof(napi_ref); ++i) { + if (callbacks[i]) { + *retval = nullptr; + return true; + } + } + NAPI_CALL_BASE(env, napi_create_promise(env, &deferred, retval), false); + return true; + } + + void notify_multiple_argument(size_t argc, napi_value argv[], const char *names[], bool resolved = true) + { + napi_value param; + // promise mode + if (!deferred) { + goto callbackMode; + } + if (napi_create_object(env, ¶m) != napi_ok) { + goto callbackMode; + } + for (size_t i = 0; i < argc; ++i) { + if (napi_set_named_property(env, param, names[i], argv[i]) != napi_ok) { + goto callbackMode; + } + } + if (resolved) { + napi_resolve_deferred(env, deferred, param); + } else { + napi_reject_deferred(env, deferred, param); + } + callbackMode: + // callback mode + napi_value undefined; + if (napi_get_undefined(env, &undefined) != napi_ok) { + return; + } + auto make_callback = [this, undefined](napi_ref ref_function, size_t argc, napi_value argv[]) + { + napi_value cb, call_result; + if (!ref_function) { + return; + } + if (napi_get_reference_value(env, ref_function, &cb) != napi_ok) { + return; + } + if (argc) { + napi_call_function(env, undefined, cb, argc, argv, &call_result); + } else { + napi_call_function(env, undefined, cb, 0, nullptr, &call_result); + } + }; + + if (resolved) { + make_callback(callbacks[0], argc, argv); + } else { + make_callback(callbacks[1], argc, argv); + } + make_callback(callbacks[2], 0, nullptr); + } + + void notify(napi_value param, bool resolved = true) + { + // notify promise + if (deferred) { + if (resolved) { + napi_resolve_deferred(env, deferred, param); + } else { + napi_reject_deferred(env, deferred, param); + } + } + // callback mode + napi_value undefined; + + if (napi_get_undefined(env, &undefined) != napi_ok) { + return; + } + + auto make_callback = [this, undefined](napi_ref ref_function, napi_value param) { + napi_value cb, call_result; + if (!ref_function) { + return; + } + if (napi_get_reference_value(env, ref_function, &cb) != napi_ok) { + return; + } + if (param) { + napi_call_function(env, undefined, cb, 1, ¶m, &call_result); + } else { + napi_call_function(env, undefined, cb, 0, nullptr, &call_result); + } + }; + // make success or fail callback + if (resolved) { + make_callback(callbacks[0], param); + } else { + make_callback(callbacks[1], param); + } + make_callback(callbacks[2], nullptr); + } + napi_async_work async_work = nullptr; +protected: + napi_env env; +private: + napi_ref callbacks[3] = {nullptr}; + napi_deferred deferred = nullptr; +}; + +#endif + diff --git a/vibrator/interfaces/kits/js/napi/BUILD.gn b/vibrator/interfaces/kits/js/napi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..f6e4d8cc4d9d07642e76b4500a029d6f281be397 --- /dev/null +++ b/vibrator/interfaces/kits/js/napi/BUILD.gn @@ -0,0 +1,47 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_shared_library("vibrator") { + include_dirs = [ + "//drivers/peripheral/misc/vibrator/interfaces/include", + "//foundation/ace/napi/interfaces/kits", + "//third_party/node/src", + "//utils/system/safwk/native/include", + "./", + ] + cflags = [ + "-fPIC", + "-g3", + ] + sources = [ + "vibrator.cpp", + "native_module.cpp", + ] + + deps = [ + "//drivers/peripheral/misc/vibrator/hal:hdi_vibrator", + "//foundation/ace/napi:ace_napi", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + relative_install_dir = "module" + + subsystem_name = "sensors" + part_name = "sensors_miscdevice" +} diff --git a/vibrator/interfaces/kits/js/napi/native_module.cpp b/vibrator/interfaces/kits/js/napi/native_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6af5cdaca277dcef7229278a702bf05d9514344 --- /dev/null +++ b/vibrator/interfaces/kits/js/napi/native_module.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "vibrator.h" + +EXTERN_C_START +/* + * function for module exports + */ +static napi_value VibratorExport(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("vibrate", NAPIVibrator), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} +EXTERN_C_END + +/* + * Module define + */ +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = VibratorExport, + .nm_modname = "vibrator", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +/* + * Module register function + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} \ No newline at end of file diff --git a/vibrator/interfaces/kits/js/napi/util.h b/vibrator/interfaces/kits/js/napi/util.h new file mode 100644 index 0000000000000000000000000000000000000000..a6995df6cbf224e0263970e5b5fd94e1f3724005 --- /dev/null +++ b/vibrator/interfaces/kits/js/napi/util.h @@ -0,0 +1,98 @@ +/* + * Copyright 2021 Institute of Software Chinese Academy of Sciences, ISRC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NAPI_UTIL_H +#define NAPI_UTIL_H + +#include +#include +#include + +#define GET_PARAMS(env, info, num) \ + size_t argc = num; \ + napi_value argv[num]; \ + napi_value thisVar; \ + void* data; \ + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data) + +static inline napi_status util_value_to_stdstring(napi_env env, napi_value data, + std::string *str) +{ + napi_status status; + size_t size, size2; + + status = napi_get_value_string_utf8(env, data, nullptr, 0, &size); + if (status != napi_ok) + return status; + + str->resize(size); + status = napi_get_value_string_utf8(env, data, &(*str)[0], size + 1, &size2); + if (status != napi_ok) + return status; + + if (size2 != size) + str->resize(size2); + return napi_ok; +} + +static inline napi_status util_get_property_ref(napi_env env, napi_value object, + const char *key, napi_ref *data) +{ + napi_status status; + napi_value value; + + status = napi_get_named_property(env, object, key, &value); + if (status != napi_ok) { + return status; + } + return napi_create_reference(env, value, 1, data); +} + +static inline napi_status util_get_property_stdstring(napi_env env, napi_value object, + const char *key, std::string *data) +{ + napi_status status; + napi_value value; + + status = napi_get_named_property(env, object, key, &value); + if (status != napi_ok) + return status; + return util_value_to_stdstring(env, value, data); +} + +static inline napi_status util_set_property_stdstring(napi_env env, napi_value object, + const char *key, const std::string &data) +{ + napi_status status; + napi_value value; + + status = napi_create_string_utf8(env, data.c_str(), data.size(), &value); + if (status != napi_ok) + return status; + return napi_set_named_property(env, object, key, value); +} + +static inline napi_status util_set_property_int32(napi_env env, napi_value object, const char *key, int32_t data) +{ + napi_status status; + napi_value value; + + status = napi_create_int32(env, data, &value); + if (status != napi_ok) + return status; + return napi_set_named_property(env, object, key, value); +} + +#endif diff --git a/vibrator/interfaces/kits/js/napi/vibrator.cpp b/vibrator/interfaces/kits/js/napi/vibrator.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dd3f8af5680a35c70147f036f0dc0a2ce97e984 --- /dev/null +++ b/vibrator/interfaces/kits/js/napi/vibrator.cpp @@ -0,0 +1,77 @@ +/* + * Copyright 2021 Institute of Software Chinese Academy of Sciences, ISRC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "vibrator.h" +#include "util.h" +#include "AsyncWorkData.h" +#include "vibrator_if.h" +#include "vibrator_type.h" + +struct VibratorData : public AsyncWorkData +{ + explicit VibratorData(napi_env env) : AsyncWorkData {env} + {} + int code; + uint32_t duration; +}; + +napi_value NAPIVibrator(napi_env env, napi_callback_info info) +{ + GET_PARAMS(env, info, 2); + NAPI_ASSERT(env, argc >= 1, "requires 1 parameter"); + napi_value undefined, retval, resourceName; + + NAPI_CALL(env, napi_get_undefined(env, &undefined)); + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + struct VibratorData *callbackData = new VibratorData { env }; + std::unique_ptr cleaner { callbackData }; + if (!callbackData->parse_callback_args(argc - 1, argv + 1, &retval)) { + return nullptr; + } + NAPI_CALL(env, napi_get_value_uint32(env, argv[0], &callbackData->duration)); + NAPI_CALL(env, napi_create_string_latin1(env, __FUNCTION__, NAPI_AUTO_LENGTH, &resourceName)); + NAPI_CALL(env, napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + struct VibratorData *callbackData = (struct VibratorData *)data; + const VibratorInterface *vibratorDev = NewVibratorInterfaceInstance(); + if (!vibratorDev) { + callbackData->code = 1; + return; + } + if (vibratorDev->StartOnce(callbackData->duration) != 0) { + callbackData->code = 2; + return; + } + callbackData->code = 0; + }, + [](napi_env env, napi_status status, void *data) { + struct VibratorData *callbackData = (struct VibratorData *)data; + std::unique_ptr cleaner { callbackData }; + + if (callbackData->code == 0) { + callbackData->notify(nullptr, true); + } else { + callbackData->notify(nullptr, false); + } + }, + (void*)callbackData, + &callbackData->async_work + )); + NAPI_CALL(env, napi_queue_async_work(env, callbackData->async_work)); + cleaner.release(); + return retval; +} diff --git a/vibrator/interfaces/kits/js/napi/vibrator.h b/vibrator/interfaces/kits/js/napi/vibrator.h new file mode 100644 index 0000000000000000000000000000000000000000..a50e119a513f73e396c4af066e5ffed63e09a133 --- /dev/null +++ b/vibrator/interfaces/kits/js/napi/vibrator.h @@ -0,0 +1,24 @@ +/* + * Copyright 2021 Institute of Software Chinese Academy of Sciences, ISRC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NAPI_VIBRATOR_H +#define NAPI_VIBRATOR_H + +#include +#include + +napi_value NAPIVibrator(napi_env env, napi_callback_info info); + +#endif diff --git a/vibrator/ohos.build b/vibrator/ohos.build new file mode 100755 index 0000000000000000000000000000000000000000..accdf7bc592b5d47f349c4d9de081da07ed4ead3 --- /dev/null +++ b/vibrator/ohos.build @@ -0,0 +1,10 @@ +{ + "subsystem": "sensors", + "parts": { + "sensors_miscdevice": { + "module_list": [ + "//base/sensors/miscdevice/vibrator:sensors_miscdevice_packages" + ] + } + } +} diff --git a/vibrator/vibrator.gni b/vibrator/vibrator.gni new file mode 100755 index 0000000000000000000000000000000000000000..2b033ce5c259055cff4ec36ce759ad4a86e27cb7 --- /dev/null +++ b/vibrator/vibrator.gni @@ -0,0 +1,20 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +vibrator_path = "//base/sensors/miscdevice/vibrator" + +kits_path = "${vibrator_path}/kits" + +innerkits_path = "${vibrator_path}/interfaces/innerkits" + +adapter_path = "${vibrator_path}/adapter" \ No newline at end of file