• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi/native_api.h"
17 #include "sensors_errors.h"
18 
19 #include <string>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 
23 #include "vibrator.h"
24 #include "vibrator_type.h"
25 
26 #undef LOG_TAG
27 #define LOG_TAG "VibratorNdkTest"
28 #define PARAMETER_SECOND 2
29 #define PARAMETER_THIRD 3
30 
IsMatchType(const napi_env & env,const napi_value & value,const napi_valuetype & type)31 bool IsMatchType(const napi_env &env, const napi_value &value, const napi_valuetype &type)
32 {
33     napi_valuetype paramType = napi_undefined;
34     napi_typeof(env, value, &paramType);
35     return paramType == type;
36 }
37 
StartVibrator(napi_env env,napi_callback_info info)38 static napi_value StartVibrator(napi_env env, napi_callback_info info)
39 {
40     MISC_HILOGD("+ StartVibrator begin-------------------");
41     size_t argc = 3;
42     napi_value args[3] = {nullptr};
43     napi_value result = {nullptr};
44 
45     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
46     if (status != napi_ok) {
47         MISC_HILOGE("napi_get_cb_info failed");
48         napi_create_int32(env, PARAMETER_ERROR, &result);
49         return result;
50     }
51 
52     if (!IsMatchType(env, args[0], napi_number) || !IsMatchType(env, args[1], napi_number)) {
53         MISC_HILOGE("IsMatchType failed");
54         napi_create_int32(env, PARAMETER_ERROR, &result);
55         return result;
56     }
57 
58     int32_t duration;
59     status = napi_get_value_int32(env, args[0], &duration);
60     if (status != napi_ok) {
61         MISC_HILOGE("napi_get_value_int32 failed");
62         napi_create_int32(env, PARAMETER_ERROR, &result);
63         return result;
64     }
65     int32_t usage;
66     status = napi_get_value_int32(env, args[1], &usage);
67     if (status != napi_ok) {
68         MISC_HILOGE("napi_get_value_int32 failed");
69         napi_create_int32(env, PARAMETER_ERROR, &result);
70         return result;
71     }
72     Vibrator_Attribute vibrateAttribute = {
73         .usage = static_cast<Vibrator_Usage>(usage)
74     };
75 
76     int32_t ret = OH_Vibrator_PlayVibration(duration, vibrateAttribute);
77     MISC_HILOGD(" StartVibrator end %{public}d", ret);
78     napi_create_int32(env, ret, &result);
79     return result;
80 }
81 
GetNativeInt32(const napi_env & env,const napi_value & value,int32_t & number)82 bool GetNativeInt32(const napi_env &env, const napi_value &value, int32_t &number)
83 {
84     CHKCF(napi_get_value_int32(env, value, &number) == napi_ok, "napi_get_value_int32 failed");
85     return true;
86 }
87 
StartVibratorCustom(napi_env env,napi_callback_info info)88 static napi_value StartVibratorCustom(napi_env env, napi_callback_info info)
89 {
90     MISC_HILOGD("+ StartVibrator begin-------------------");
91     size_t argc = 5;
92     napi_value args[5] = {nullptr};
93     napi_value result {nullptr};
94     napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
95     if (status != napi_ok) {
96         MISC_HILOGE("napi_get_cb_info failed");
97         napi_create_int32(env, PARAMETER_ERROR, &result);
98         return result;
99     }
100     if (!IsMatchType(env, args[0], napi_number) || !IsMatchType(env, args[1], napi_number) ||
101         !IsMatchType(env, args[PARAMETER_SECOND], napi_number) || !IsMatchType(env,
102         args[PARAMETER_THIRD], napi_number)) {
103         MISC_HILOGE("IsMatchType failed");
104         napi_create_int32(env, PARAMETER_ERROR, &result);
105         return result;
106     }
107     int32_t fd = 0;
108     int32_t offset = 0;
109     int32_t length = 0;
110     int32_t usage = 0;
111     if (!GetNativeInt32(env, args[0], fd) || !GetNativeInt32(env, args[1], offset) ||
112         !GetNativeInt32(env, args[PARAMETER_SECOND], length) || !GetNativeInt32(env, args[PARAMETER_THIRD], usage)) {
113         MISC_HILOGE("napi_get_value_int32 failed");
114         napi_create_int32(env, PARAMETER_ERROR, &result);
115         return result;
116     }
117     Vibrator_FileDescription fileDescription = {
118         .fd = fd,
119         .offset = offset,
120         .length = length
121     };
122     Vibrator_Attribute vibrateAttribute = {
123         .usage = static_cast<Vibrator_Usage>(usage)
124     };
125     int32_t ret = OH_Vibrator_PlayVibrationCustom(fileDescription, vibrateAttribute);
126     MISC_HILOGD("StartVibratorCustom end----------%{public}d", ret);
127     napi_create_int32(env, ret, &result);
128     return result;
129 }
130 
Cancel(napi_env env,napi_callback_info info)131 static napi_value Cancel(napi_env env, napi_callback_info info)
132 {
133     MISC_HILOGD("+ StartVibrator begin-------------------");
134     int32_t ret = OH_Vibrator_Cancel();
135     MISC_HILOGD("Cancel end--------%{public}d", ret);
136     napi_value result;
137     napi_create_int32(env, ret, &result);
138     return result;
139 }
140 
141 EXTERN_C_START
Init(napi_env env,napi_value exports)142 static napi_value Init(napi_env env, napi_value exports)
143 {
144     napi_property_descriptor desc[] = {
145         { "startVibrator", nullptr, StartVibrator, nullptr, nullptr, nullptr, napi_default, nullptr },
146         { "startVibratorCustom", nullptr, StartVibratorCustom, nullptr, nullptr, nullptr, napi_default, nullptr },
147         { "cancel", nullptr, Cancel, nullptr, nullptr, nullptr, napi_default, nullptr }
148     };
149     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
150     return exports;
151 }
152 EXTERN_C_END
153 
154 static napi_module demoModule = {
155     .nm_version =1,
156     .nm_flags = 0,
157     .nm_filename = nullptr,
158     .nm_register_func = Init,
159     .nm_modname = "entry",
160     .nm_priv = ((void*)0),
161     .reserved = { 0 },
162 };
163 
RegisterEntryModule(void)164 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
165 {
166     napi_module_register(&demoModule);
167 }