• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "sensor_js.h"
16 
17 #include <cinttypes>
18 #include <cstdlib>
19 #include <map>
20 #include <cmath>
21 #include <string>
22 #include <unistd.h>
23 
24 #include "refbase.h"
25 #include "securec.h"
26 
27 #include "geomagnetic_field.h"
28 #include "sensor_algorithm.h"
29 #include "sensor_napi_error.h"
30 #include "sensor_napi_utils.h"
31 #include "sensor_system_js.h"
32 
33 namespace OHOS {
34 namespace Sensors {
35 namespace {
36 constexpr int32_t QUATERNION_LENGTH = 4;
37 constexpr int32_t ROTATION_VECTOR_LENGTH = 3;
38 constexpr int32_t REPORTING_INTERVAL = 200000000;
39 constexpr int32_t INVALID_SENSOR_ID = -1;
40 constexpr int32_t SENSOR_SUBSCRIBE_FAILURE = 1001;
41 constexpr int32_t INPUT_ERROR = 202;
42 constexpr float BODY_STATE_EXCEPT = 1.0f;
43 constexpr float THREESHOLD = 0.000001f;
44 }
45 static std::map<std::string, int64_t> g_samplingPeriod = {
46     {"normal", 200000000},
47     {"ui", 60000000},
48     {"game", 20000000},
49 };
50 static std::mutex mutex_;
51 static std::mutex bodyMutex_;
52 static float g_bodyState = -1.0f;
53 static std::map<int32_t, sptr<AsyncCallbackInfo>> g_subscribeCallbacks;
54 static std::mutex onMutex_;
55 static std::mutex onceMutex_;
56 static std::map<int32_t, std::vector<sptr<AsyncCallbackInfo>>> g_onceCallbackInfos;
57 static std::map<int32_t, std::vector<sptr<AsyncCallbackInfo>>> g_onCallbackInfos;
58 
CheckSubscribe(int32_t sensorTypeId)59 static bool CheckSubscribe(int32_t sensorTypeId)
60 {
61     std::lock_guard<std::mutex> onCallbackLock(onMutex_);
62     auto iter = g_onCallbackInfos.find(sensorTypeId);
63     return iter != g_onCallbackInfos.end();
64 }
65 
copySensorData(sptr<AsyncCallbackInfo> callbackInfo,SensorEvent * event)66 static bool copySensorData(sptr<AsyncCallbackInfo> callbackInfo, SensorEvent *event)
67 {
68     CHKPF(callbackInfo);
69     CHKPF(event);
70     int32_t sensorTypeId = event->sensorTypeId;
71     callbackInfo->data.sensorData.sensorTypeId = sensorTypeId;
72     callbackInfo->data.sensorData.dataLength = event->dataLen;
73     callbackInfo->data.sensorData.timestamp = event->timestamp;
74     CHKPF(event->data);
75     auto data = reinterpret_cast<float *>(event->data);
76     if (sensorTypeId == SENSOR_TYPE_ID_WEAR_DETECTION && callbackInfo->type == SUBSCRIBE_CALLBACK) {
77         std::lock_guard<std::mutex> onBodyLock(bodyMutex_);
78         g_bodyState = *data;
79         callbackInfo->data.sensorData.data[0] =
80             (fabs(g_bodyState - BODY_STATE_EXCEPT) < THREESHOLD) ? true : false;
81         return true;
82     }
83     if (memcpy_s(callbackInfo->data.sensorData.data, event->dataLen, data, event->dataLen) != EOK) {
84         SEN_HILOGE("Copy data failed");
85         return false;
86     }
87     return true;
88 }
89 
CheckSystemSubscribe(int32_t sensorTypeId)90 static bool CheckSystemSubscribe(int32_t sensorTypeId)
91 {
92     std::lock_guard<std::mutex> subscribeLock(mutex_);
93     auto iter = g_subscribeCallbacks.find(sensorTypeId);
94     if (iter == g_subscribeCallbacks.end()) {
95         return false;
96     }
97     return true;
98 }
99 
EmitSubscribeCallback(SensorEvent * event)100 static void EmitSubscribeCallback(SensorEvent *event)
101 {
102     CHKPV(event);
103     int32_t sensorTypeId = event->sensorTypeId;
104     if (!CheckSystemSubscribe(sensorTypeId)) {
105         return;
106     }
107     std::lock_guard<std::mutex> subscribeLock(mutex_);
108     auto callback = g_subscribeCallbacks[sensorTypeId];
109     CHKCV(copySensorData(callback, event), "Copy sensor data failed");
110     EmitUvEventLoop(callback);
111 }
112 
EmitOnCallback(SensorEvent * event)113 static void EmitOnCallback(SensorEvent *event)
114 {
115     CHKPV(event);
116     int32_t sensorTypeId = event->sensorTypeId;
117     if (!CheckSubscribe(sensorTypeId)) {
118         return;
119     }
120     std::lock_guard<std::mutex> onCallbackLock(onMutex_);
121     auto onCallbackInfos = g_onCallbackInfos[sensorTypeId];
122     for (auto &onCallbackInfo : onCallbackInfos) {
123         if (!copySensorData(onCallbackInfo, event)) {
124             SEN_HILOGE("Copy sensor data failed");
125             continue;
126         }
127         EmitUvEventLoop(onCallbackInfo);
128     }
129 }
130 
EmitOnceCallback(SensorEvent * event)131 static void EmitOnceCallback(SensorEvent *event)
132 {
133     CHKPV(event);
134     int32_t sensorTypeId = event->sensorTypeId;
135     std::lock_guard<std::mutex> onceCallbackLock(onceMutex_);
136     auto iter = g_onceCallbackInfos.find(sensorTypeId);
137     if (iter == g_onceCallbackInfos.end()) {
138         return;
139     }
140     auto& onceCallbackInfos = iter->second;
141     while (!onceCallbackInfos.empty()) {
142         auto onceCallbackInfo = onceCallbackInfos.front();
143         auto beginIter = onceCallbackInfos.begin();
144         onceCallbackInfos.erase(beginIter);
145         if (!copySensorData(onceCallbackInfo, event)) {
146             SEN_HILOGE("Copy sensor data failed");
147             continue;
148         }
149         EmitUvEventLoop(std::move(onceCallbackInfo));
150     }
151     g_onceCallbackInfos.erase(sensorTypeId);
152 
153     CHKCV((!CheckSubscribe(sensorTypeId)), "Has client subscribe, not need cancel subscribe");
154     CHKCV((!CheckSystemSubscribe(sensorTypeId)), "Has client subscribe system api, not need cancel subscribe");
155     UnsubscribeSensor(sensorTypeId);
156 }
157 
DataCallbackImpl(SensorEvent * event)158 void DataCallbackImpl(SensorEvent *event)
159 {
160     CHKPV(event);
161     EmitOnCallback(event);
162     EmitSubscribeCallback(event);
163     EmitOnceCallback(event);
164 }
165 
166 const SensorUser user = {
167     .callback = DataCallbackImpl
168 };
169 
UnsubscribeSensor(int32_t sensorTypeId)170 int32_t UnsubscribeSensor(int32_t sensorTypeId)
171 {
172     CALL_LOG_ENTER;
173     int32_t ret = DeactivateSensor(sensorTypeId, &user);
174     if (ret != ERR_OK) {
175         SEN_HILOGE("DeactivateSensor failed");
176         return ret;
177     }
178     return UnsubscribeSensor(sensorTypeId, &user);
179 }
180 
SubscribeSensor(int32_t sensorTypeId,int64_t interval,RecordSensorCallback callback)181 int32_t SubscribeSensor(int32_t sensorTypeId, int64_t interval, RecordSensorCallback callback)
182 {
183     CALL_LOG_ENTER;
184     int32_t ret = SubscribeSensor(sensorTypeId, &user);
185     if (ret != ERR_OK) {
186         SEN_HILOGE("SubscribeSensor failed");
187         return ret;
188     }
189     ret = SetBatch(sensorTypeId, &user, interval, 0);
190     if (ret != ERR_OK) {
191         SEN_HILOGE("SetBatch failed");
192         return ret;
193     }
194     return ActivateSensor(sensorTypeId, &user);
195 }
196 
IsOnceSubscribed(napi_env env,int32_t sensorTypeId,napi_value callback)197 static bool IsOnceSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback)
198 {
199     CALL_LOG_ENTER;
200     if (auto iter = g_onceCallbackInfos.find(sensorTypeId); iter == g_onceCallbackInfos.end()) {
201         SEN_HILOGW("already subscribed, sensorTypeId:%{public}d", sensorTypeId);
202         return false;
203     }
204     std::vector<sptr<AsyncCallbackInfo>> callbackInfos = g_onceCallbackInfos[sensorTypeId];
205     for (auto callbackInfo : callbackInfos) {
206         CHKPC(callbackInfo);
207         if (callbackInfo->env != env) {
208             continue;
209         }
210         napi_value sensorCallback = nullptr;
211         CHKNRF(env, napi_get_reference_value(env, callbackInfo->callback[0], &sensorCallback),
212             "napi_get_reference_value");
213         if (IsSameValue(env, callback, sensorCallback)) {
214             return true;
215         }
216     }
217     return false;
218 }
219 
UpdateOnceCallback(napi_env env,int32_t sensorTypeId,napi_value callback)220 static void UpdateOnceCallback(napi_env env, int32_t sensorTypeId, napi_value callback)
221 {
222     CALL_LOG_ENTER;
223     std::lock_guard<std::mutex> onceCallbackLock(onceMutex_);
224     CHKCV((!IsOnceSubscribed(env, sensorTypeId, callback)), "The callback has been subscribed");
225     sptr<AsyncCallbackInfo> asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, ONCE_CALLBACK);
226     CHKPV(asyncCallbackInfo);
227     napi_status status = napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]);
228     if (status != napi_ok) {
229         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
230         return;
231     }
232     std::vector<sptr<AsyncCallbackInfo>> callbackInfos = g_onceCallbackInfos[sensorTypeId];
233     callbackInfos.push_back(asyncCallbackInfo);
234     g_onceCallbackInfos[sensorTypeId] = callbackInfos;
235 }
236 
Once(napi_env env,napi_callback_info info)237 static napi_value Once(napi_env env, napi_callback_info info)
238 {
239     CALL_LOG_ENTER;
240     size_t argc = 2;
241     napi_value args[2] = { 0 };
242     napi_value thisVar = nullptr;
243     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
244     if (status != napi_ok || argc < 2) {
245         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
246         return nullptr;
247     }
248     if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_function))) {
249         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type");
250         return nullptr;
251     }
252     int32_t sensorTypeId = INVALID_SENSOR_ID;
253     if (!GetNativeInt32(env, args[0], sensorTypeId)) {
254         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail");
255         return nullptr;
256     }
257     if (!CheckSubscribe(sensorTypeId)) {
258         SEN_HILOGD("No subscription to change sensor data, registration is required");
259         int32_t ret = SubscribeSensor(sensorTypeId, REPORTING_INTERVAL, DataCallbackImpl);
260         if (ret != ERR_OK) {
261             ThrowErr(env, ret, "SubscribeSensor fail");
262             return nullptr;
263         }
264     }
265     UpdateOnceCallback(env, sensorTypeId, args[1]);
266     return nullptr;
267 }
268 
IsSubscribed(napi_env env,int32_t sensorTypeId,napi_value callback)269 static bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback)
270 {
271     CALL_LOG_ENTER;
272     if (auto iter = g_onCallbackInfos.find(sensorTypeId); iter == g_onCallbackInfos.end()) {
273         SEN_HILOGW("no client subscribe, sensorTypeId:%{public}d", sensorTypeId);
274         return false;
275     }
276     std::vector<sptr<AsyncCallbackInfo>> callbackInfos = g_onCallbackInfos[sensorTypeId];
277     for (auto callbackInfo : callbackInfos) {
278         CHKPC(callbackInfo);
279         if (callbackInfo->env != env) {
280             continue;
281         }
282         napi_value sensorCallback = nullptr;
283         CHKNRF(env, napi_get_reference_value(env, callbackInfo->callback[0], &sensorCallback),
284             "napi_get_reference_value");
285         if (IsSameValue(env, callback, sensorCallback)) {
286             return true;
287         }
288     }
289     return false;
290 }
291 
UpdateCallbackInfos(napi_env env,int32_t sensorTypeId,napi_value callback)292 static void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value callback)
293 {
294     CALL_LOG_ENTER;
295     std::lock_guard<std::mutex> onCallbackLock(onMutex_);
296     CHKCV((!IsSubscribed(env, sensorTypeId, callback)), "The callback has been subscribed");
297     sptr<AsyncCallbackInfo> asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, ON_CALLBACK);
298     CHKPV(asyncCallbackInfo);
299     napi_status status = napi_create_reference(env, callback, 1, &asyncCallbackInfo->callback[0]);
300     if (status != napi_ok) {
301         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
302         return;
303     }
304     std::vector<sptr<AsyncCallbackInfo>> callbackInfos = g_onCallbackInfos[sensorTypeId];
305     callbackInfos.push_back(asyncCallbackInfo);
306     g_onCallbackInfos[sensorTypeId] = callbackInfos;
307 }
308 
On(napi_env env,napi_callback_info info)309 static napi_value On(napi_env env, napi_callback_info info)
310 {
311     CALL_LOG_ENTER;
312     size_t argc = 3;
313     napi_value args[3] = { 0 };
314     napi_value thisVar = nullptr;
315     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
316     if (status != napi_ok || argc < 2) {
317         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
318         return nullptr;
319     }
320     if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_function))) {
321         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type");
322         return nullptr;
323     }
324     int32_t sensorTypeId = INVALID_SENSOR_ID;
325     if (!GetNativeInt32(env, args[0], sensorTypeId)) {
326         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail");
327         return nullptr;
328     }
329     int64_t interval = REPORTING_INTERVAL;
330     if (argc >= 3) {
331         if (!IsMatchType(env, args[2], napi_object)) {
332             ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be object");
333             return nullptr;
334         }
335         napi_value value = GetNamedProperty(env, args[2], "interval");
336         if ((value == nullptr) || (!IsMatchType(env, value, napi_number))) {
337             ThrowErr(env, PARAMETER_ERROR, "Get interval fail or wrong argument type");
338             return nullptr;
339         }
340         if (!GetNativeInt64(env, value, interval)) {
341             ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail");
342             return nullptr;
343         }
344         SEN_HILOGD("Interval is %{public}" PRId64, interval);
345     }
346     int32_t ret = SubscribeSensor(sensorTypeId, interval, DataCallbackImpl);
347     if (ret != ERR_OK) {
348         ThrowErr(env, ret, "SubscribeSensor fail");
349         return nullptr;
350     }
351     UpdateCallbackInfos(env, sensorTypeId, args[1]);
352     return nullptr;
353 }
354 
RemoveAllCallback(napi_env env,int32_t sensorTypeId)355 static int32_t RemoveAllCallback(napi_env env, int32_t sensorTypeId)
356 {
357     CALL_LOG_ENTER;
358     std::lock_guard<std::mutex> onCallbackLock(onMutex_);
359     std::vector<sptr<AsyncCallbackInfo>> callbackInfos = g_onCallbackInfos[sensorTypeId];
360     for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) {
361         CHKPC(*iter);
362         if ((*iter)->env != env) {
363             ++iter;
364             continue;
365         }
366         iter = callbackInfos.erase(iter);
367     }
368     if (callbackInfos.empty()) {
369         SEN_HILOGD("No subscription to change sensor data");
370         g_onCallbackInfos.erase(sensorTypeId);
371         return 0;
372     }
373     g_onCallbackInfos[sensorTypeId] = callbackInfos;
374     return callbackInfos.size();
375 }
376 
RemoveCallback(napi_env env,int32_t sensorTypeId,napi_value callback)377 static int32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback)
378 {
379     CALL_LOG_ENTER;
380     std::lock_guard<std::mutex> onCallbackLock(onMutex_);
381     std::vector<sptr<AsyncCallbackInfo>> callbackInfos = g_onCallbackInfos[sensorTypeId];
382     for (auto iter = callbackInfos.begin(); iter != callbackInfos.end(); ++iter) {
383         CHKPC(*iter);
384         if ((*iter)->env != env) {
385             continue;
386         }
387         napi_value sensorCallback = nullptr;
388         if (napi_get_reference_value(env, (*iter)->callback[0], &sensorCallback) != napi_ok) {
389             SEN_HILOGE("napi_get_reference_value fail");
390             continue;
391         }
392         if (IsSameValue(env, callback, sensorCallback)) {
393             callbackInfos.erase(iter++);
394             SEN_HILOGD("Remove callback success");
395             break;
396         }
397     }
398     if (callbackInfos.empty()) {
399         SEN_HILOGD("No subscription to change sensor data");
400         g_onCallbackInfos.erase(sensorTypeId);
401         return 0;
402     }
403     g_onCallbackInfos[sensorTypeId] = callbackInfos;
404     return callbackInfos.size();
405 }
406 
Off(napi_env env,napi_callback_info info)407 static napi_value Off(napi_env env, napi_callback_info info)
408 {
409     CALL_LOG_ENTER;
410     size_t argc = 2;
411     napi_value args[2] = { 0 };
412     napi_value thisVar = nullptr;
413     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
414     if (status != napi_ok || argc == 0) {
415         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
416         return nullptr;
417     }
418     int32_t sensorTypeId = INVALID_SENSOR_ID;
419     if ((!IsMatchType(env, args[0], napi_number)) || (!GetNativeInt32(env, args[0], sensorTypeId))) {
420         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type or get number fail");
421         return nullptr;
422     }
423     int32_t subscribeSize = -1;
424     if (argc == 1) {
425         subscribeSize = RemoveAllCallback(env, sensorTypeId);
426     } else {
427         if (!IsMatchType(env, args[1], napi_function)) {
428             ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
429             return nullptr;
430         }
431         subscribeSize = RemoveCallback(env, sensorTypeId, args[1]);
432     }
433     if (CheckSystemSubscribe(sensorTypeId) || (subscribeSize > 0)) {
434         SEN_HILOGW("There are other client subscribe system js api as well, not need unsubscribe");
435         return nullptr;
436     }
437     int32_t ret = UnsubscribeSensor(sensorTypeId);
438     if (ret == PARAMETER_ERROR || ret == PERMISSION_DENIED) {
439         ThrowErr(env, ret, "UnsubscribeSensor fail");
440     }
441     return nullptr;
442 }
443 
GetGeomagneticField(napi_env env,napi_callback_info info)444 static napi_value GetGeomagneticField(napi_env env, napi_callback_info info)
445 {
446     CALL_LOG_ENTER;
447     size_t argc = 3;
448     napi_value args[3] = { 0 };
449     napi_value thisVar = nullptr;
450     if ((napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr) != napi_ok) || (argc < 2)) {
451         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
452         return nullptr;
453     }
454     if ((!IsMatchType(env, args[0], napi_object)) || (!IsMatchType(env, args[1], napi_number))) {
455         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type");
456         return nullptr;
457     }
458     napi_value napiLatitude = GetNamedProperty(env, args[0], "latitude");
459     if (napiLatitude == nullptr) {
460         ThrowErr(env, PARAMETER_ERROR, "napiLatitude is null");
461         return nullptr;
462     }
463     double latitude = 0;
464     if (!GetNativeDouble(env, napiLatitude, latitude)) {
465         ThrowErr(env, PARAMETER_ERROR, "Get latitude fail");
466         return nullptr;
467     }
468     napi_value napiLongitude = GetNamedProperty(env, args[0], "longitude");
469     if (napiLongitude == nullptr) {
470         ThrowErr(env, PARAMETER_ERROR, "napiLongitude is null");
471         return nullptr;
472     }
473     double longitude = 0;
474     if (!GetNativeDouble(env, napiLongitude, longitude)) {
475         ThrowErr(env, PARAMETER_ERROR, "Get longitude fail");
476         return nullptr;
477     }
478     napi_value napiAltitude = GetNamedProperty(env, args[0], "altitude");
479     if (napiAltitude == nullptr) {
480         ThrowErr(env, PARAMETER_ERROR, "napiAltitude is null");
481         return nullptr;
482     }
483     double altitude = 0;
484     if (!GetNativeDouble(env, napiAltitude, altitude)) {
485         ThrowErr(env, PARAMETER_ERROR, "Get altitude fail");
486         return nullptr;
487     }
488     int64_t timeMillis = 0;
489     if (!GetNativeInt64(env, args[1], timeMillis)) {
490         ThrowErr(env, PARAMETER_ERROR, "Get timeMillis fail");
491         return nullptr;
492     }
493     GeomagneticField geomagneticField(latitude, longitude, altitude, timeMillis);
494     sptr<AsyncCallbackInfo> asyncCallbackInfo =
495         new (std::nothrow) AsyncCallbackInfo(env, GET_GEOMAGNETIC_FIELD);
496     CHKPP(asyncCallbackInfo);
497     asyncCallbackInfo->data.geomagneticData = {
498         .x = geomagneticField.ObtainX(),
499         .y = geomagneticField.ObtainY(),
500         .z = geomagneticField.ObtainZ(),
501         .geomagneticDip = geomagneticField.ObtainGeomagneticDip(),
502         .deflectionAngle = geomagneticField.ObtainDeflectionAngle(),
503         .levelIntensity = geomagneticField.ObtainLevelIntensity(),
504         .totalIntensity = geomagneticField.ObtainTotalIntensity(),
505     };
506     if (argc == 2) {
507         napi_value promise = nullptr;
508         if ((napi_create_promise(env, &asyncCallbackInfo->deferred, &promise)) != napi_ok) {
509             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
510             return nullptr;
511         }
512         EmitPromiseWork(asyncCallbackInfo);
513         return promise;
514     }
515     if (!IsMatchType(env, args[2], napi_function) ||
516         (napi_create_reference(env, args[2], 1, &asyncCallbackInfo->callback[0]) != napi_ok)) {
517         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type or napi_create_reference fail ");
518         return nullptr;
519     }
520     EmitAsyncCallbackWork(asyncCallbackInfo);
521     return nullptr;
522 }
523 
GetAxisX(napi_env env,napi_value value)524 static napi_value GetAxisX(napi_env env, napi_value value)
525 {
526     return GetNamedProperty(env, value, "x");
527 }
528 
GetAxisY(napi_env env,napi_value value)529 static napi_value GetAxisY(napi_env env, napi_value value)
530 {
531     return GetNamedProperty(env, value, "y");
532 }
533 
TransformCoordinateSystem(napi_env env,napi_callback_info info)534 static napi_value TransformCoordinateSystem(napi_env env, napi_callback_info info)
535 {
536     CALL_LOG_ENTER;
537     size_t argc = 3;
538     napi_value args[3]  = { 0 };
539     napi_value thisVar = nullptr;
540     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
541     if ((status != napi_ok) || (argc < 2)) {
542         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
543         return nullptr;
544     }
545     if ((!IsMatchArrayType(env, args[0])) || (!IsMatchType(env, args[1], napi_object))) {
546         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type");
547         return nullptr;
548     }
549     std::vector<float> inRotationVector;
550     if (!GetFloatArray(env, args[0], inRotationVector)) {
551         ThrowErr(env, PARAMETER_ERROR, "Get inRotationVector fail");
552         return nullptr;
553     }
554     size_t length = inRotationVector.size();
555     if ((length != DATA_LENGTH) && (length != THREE_DIMENSIONAL_MATRIX_LENGTH)) {
556         ThrowErr(env, PARAMETER_ERROR, "Wrong inRotationVector length");
557         return nullptr;
558     }
559     napi_value napiAxisX = GetAxisX(env, args[1]);
560     if (napiAxisX == nullptr) {
561         ThrowErr(env, PARAMETER_ERROR, "napiAxisX is null");
562         return nullptr;
563     }
564     int32_t axisX = 0;
565     if (!GetNativeInt32(env, napiAxisX, axisX)) {
566         ThrowErr(env, PARAMETER_ERROR, "Get axisY fail");
567         return nullptr;
568     }
569     napi_value napiAxisY = GetAxisY(env, args[1]);
570     if (napiAxisY == nullptr) {
571         ThrowErr(env, PARAMETER_ERROR, "napiAxisY is null");
572         return nullptr;
573     }
574     int32_t axisY = 0;
575     if (!GetNativeInt32(env, napiAxisY, axisY)) {
576         ThrowErr(env, PARAMETER_ERROR, "Get axisY fail");
577         return nullptr;
578     }
579     sptr<AsyncCallbackInfo> asyncCallbackInfo =
580         new (std::nothrow) AsyncCallbackInfo(env, TRANSFORM_COORDINATE_SYSTEM);
581     CHKPP(asyncCallbackInfo);
582     std::vector<float> outRotationVector(length);
583     SensorAlgorithm sensorAlgorithm;
584     int32_t ret = sensorAlgorithm.TransformCoordinateSystem(inRotationVector, axisX, axisY, outRotationVector);
585     if (ret != OHOS::ERR_OK) {
586         ThrowErr(env, ret, "Transform coordinate system fail");
587         return nullptr;
588     } else {
589         for (size_t i = 0; i < length; ++i) {
590             asyncCallbackInfo->data.reserveData.reserve[i] = outRotationVector[i];
591         }
592         asyncCallbackInfo->data.reserveData.length = static_cast<int32_t>(length);
593     }
594     if (argc == 2) {
595         napi_value promise = nullptr;
596         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
597         if (status != napi_ok) {
598             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
599             return nullptr;
600         }
601         EmitPromiseWork(asyncCallbackInfo);
602         return promise;
603     }
604     if (!IsMatchType(env, args[2], napi_function)) {
605         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
606         return nullptr;
607     }
608     status = napi_create_reference(env, args[2], 1, &asyncCallbackInfo->callback[0]);
609     if (status != napi_ok) {
610         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
611         return nullptr;
612     }
613     EmitAsyncCallbackWork(asyncCallbackInfo);
614     return nullptr;
615 }
616 
GetAngleModify(napi_env env,napi_callback_info info)617 static napi_value GetAngleModify(napi_env env, napi_callback_info info)
618 {
619     CALL_LOG_ENTER;
620     size_t argc = 3;
621     napi_value args[3] = { 0 };
622     napi_value thisVar = nullptr;
623     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
624     if (status != napi_ok || argc < 2) {
625         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
626         return nullptr;
627     }
628     if (!IsMatchArrayType(env, args[0])) {
629         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
630         return nullptr;
631     }
632     if (!IsMatchArrayType(env, args[1])) {
633         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
634         return nullptr;
635     }
636     sptr<AsyncCallbackInfo> asyncCallbackInfo =
637         new (std::nothrow) AsyncCallbackInfo(env, GET_ANGLE_MODIFY);
638     CHKPP(asyncCallbackInfo);
639     std::vector<float> curRotationVector;
640     if (!GetFloatArray(env, args[0], curRotationVector)) {
641         ThrowErr(env, PARAMETER_ERROR, "Get curRotationVector fail");
642         return nullptr;
643     }
644     std::vector<float> preRotationVector;
645     if (!GetFloatArray(env, args[1], preRotationVector)) {
646         ThrowErr(env, PARAMETER_ERROR, "Get preRotationVector fail");
647         return nullptr;
648     }
649     std::vector<float> angleChange(ROTATION_VECTOR_LENGTH);
650     SensorAlgorithm sensorAlgorithm;
651     int32_t ret = sensorAlgorithm.GetAngleModify(curRotationVector, preRotationVector, angleChange);
652     if (ret != OHOS::ERR_OK) {
653         ThrowErr(env, ret, "Get angle modify fail");
654         return nullptr;
655     } else {
656         asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH;
657         for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) {
658             asyncCallbackInfo->data.reserveData.reserve[i] = angleChange[i];
659         }
660     }
661     if (argc == 2) {
662         napi_value promise = nullptr;
663         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
664         if (status != napi_ok) {
665             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
666             return nullptr;
667         }
668         EmitPromiseWork(asyncCallbackInfo);
669         return promise;
670     }
671     if (!IsMatchType(env, args[2], napi_function)) {
672         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
673         return nullptr;
674     }
675     status = napi_create_reference(env, args[2], 1, &asyncCallbackInfo->callback[0]);
676     if (status != napi_ok) {
677         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
678         return nullptr;
679     }
680     EmitAsyncCallbackWork(asyncCallbackInfo);
681     return nullptr;
682 }
683 
GetDirection(napi_env env,napi_callback_info info)684 static napi_value GetDirection(napi_env env, napi_callback_info info)
685 {
686     CALL_LOG_ENTER;
687     size_t argc = 3;
688     napi_value args[3] = { 0 };
689     napi_value thisVar = nullptr;
690     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
691     if (status != napi_ok || argc == 0) {
692         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
693         return nullptr;
694     }
695     if (!IsMatchArrayType(env, args[0])) {
696         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
697         return nullptr;
698     }
699     sptr<AsyncCallbackInfo> asyncCallbackInfo =
700         new (std::nothrow) AsyncCallbackInfo(env, GET_DIRECTION);
701     CHKPP(asyncCallbackInfo);
702     std::vector<float> rotationMatrix;
703     if (!GetFloatArray(env, args[0], rotationMatrix)) {
704         ThrowErr(env, PARAMETER_ERROR, "Get rotationMatrix fail");
705         return nullptr;
706     }
707     std::vector<float> rotationAngle(ROTATION_VECTOR_LENGTH);
708     SensorAlgorithm sensorAlgorithm;
709     int32_t ret = sensorAlgorithm.GetDirection(rotationMatrix, rotationAngle);
710     if (ret != OHOS::ERR_OK) {
711         ThrowErr(env, ret, "Get direction fail");
712         return nullptr;
713     } else {
714         asyncCallbackInfo->data.reserveData.length = ROTATION_VECTOR_LENGTH;
715         for (int32_t i = 0; i < ROTATION_VECTOR_LENGTH; ++i) {
716             asyncCallbackInfo->data.reserveData.reserve[i] = rotationAngle[i];
717         }
718     }
719     if (argc == 1) {
720         napi_value promise = nullptr;
721         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
722         if (status != napi_ok) {
723             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
724             return nullptr;
725         }
726         EmitPromiseWork(asyncCallbackInfo);
727         return promise;
728     }
729     if (!IsMatchType(env, args[1], napi_function)) {
730         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
731         return nullptr;
732     }
733     status = napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]);
734     if (status != napi_ok) {
735         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
736         return nullptr;
737     }
738     EmitAsyncCallbackWork(asyncCallbackInfo);
739     return nullptr;
740 }
741 
CreateQuaternion(napi_env env,napi_callback_info info)742 static napi_value CreateQuaternion(napi_env env, napi_callback_info info)
743 {
744     CALL_LOG_ENTER;
745     size_t argc = 2;
746     napi_value args[2] = { 0 };
747     napi_value thisVar = nullptr;
748     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
749     if (status != napi_ok || argc == 0) {
750         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
751         return nullptr;
752     }
753     if (!IsMatchArrayType(env, args[0])) {
754         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
755         return nullptr;
756     }
757     sptr<AsyncCallbackInfo> asyncCallbackInfo =
758         new (std::nothrow) AsyncCallbackInfo(env, CREATE_QUATERNION);
759     CHKPP(asyncCallbackInfo);
760     std::vector<float> rotationVector;
761     if (!GetFloatArray(env, args[0], rotationVector)) {
762         ThrowErr(env, PARAMETER_ERROR, "Get rotationVector fail");
763         return nullptr;
764     }
765     std::vector<float> quaternion(QUATERNION_LENGTH);
766     SensorAlgorithm sensorAlgorithm;
767     int32_t ret = sensorAlgorithm.CreateQuaternion(rotationVector, quaternion);
768     if (ret != OHOS::ERR_OK) {
769         ThrowErr(env, ret, "CreateQuaternion fail");
770         return nullptr;
771     } else {
772         asyncCallbackInfo->data.reserveData.length = QUATERNION_LENGTH;
773         for (int32_t i = 0; i < QUATERNION_LENGTH; ++i) {
774             asyncCallbackInfo->data.reserveData.reserve[i] = quaternion[i];
775         }
776     }
777     if (argc == 1) {
778         napi_value promise = nullptr;
779         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
780         if (status != napi_ok) {
781             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
782             return nullptr;
783         }
784         EmitPromiseWork(asyncCallbackInfo);
785         return promise;
786     }
787     if (!IsMatchType(env, args[1], napi_function)) {
788         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
789         return nullptr;
790     }
791     status = napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]);
792     if (status != napi_ok) {
793         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
794         return nullptr;
795     }
796     EmitAsyncCallbackWork(asyncCallbackInfo);
797     return nullptr;
798 }
799 
GetAltitude(napi_env env,napi_callback_info info)800 static napi_value GetAltitude(napi_env env, napi_callback_info info)
801 {
802     CALL_LOG_ENTER;
803     size_t argc = 3;
804     napi_value args[3] = { 0 };
805     napi_value thisVar = nullptr;
806     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
807     if (status != napi_ok || argc < 2) {
808         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
809         return nullptr;
810     }
811     if ((!IsMatchType(env, args[0], napi_number)) || (!IsMatchType(env, args[1], napi_number))) {
812         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type");
813         return nullptr;
814     }
815     sptr<AsyncCallbackInfo> asyncCallbackInfo =
816         new (std::nothrow) AsyncCallbackInfo(env, GET_ALTITUDE);
817     CHKPP(asyncCallbackInfo);
818     float seaPressure = 0;
819     if (!GetNativeFloat(env, args[0], seaPressure)) {
820         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get seaPressure fail");
821         return nullptr;
822     }
823     float currentPressure = 0;
824     if (!GetNativeFloat(env, args[1], currentPressure)) {
825         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get currentPressure fail");
826         return nullptr;
827     }
828     float altitude = 0;
829     SensorAlgorithm sensorAlgorithm;
830     int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, &altitude);
831     if (ret != OHOS::ERR_OK) {
832         ThrowErr(env, ret, "Get altitude fail");
833         return nullptr;
834     } else {
835         asyncCallbackInfo->data.reserveData.reserve[0] = altitude;
836     }
837     if (argc == 2) {
838         napi_value promise = nullptr;
839         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
840         if (status != napi_ok) {
841             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
842             return nullptr;
843         }
844         EmitPromiseWork(asyncCallbackInfo);
845         return promise;
846     }
847     if (!IsMatchType(env, args[2], napi_function)) {
848         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
849         return nullptr;
850     }
851     status = napi_create_reference(env, args[2], 1, &asyncCallbackInfo->callback[0]);
852     if (status != napi_ok) {
853         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
854         return nullptr;
855     }
856     EmitAsyncCallbackWork(asyncCallbackInfo);
857     return nullptr;
858 }
859 
GetGeomagneticDip(napi_env env,napi_callback_info info)860 static napi_value GetGeomagneticDip(napi_env env, napi_callback_info info)
861 {
862     CALL_LOG_ENTER;
863     size_t argc = 2;
864     napi_value args[2] = { 0 };
865     napi_value thisVar = nullptr;
866     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
867     if (status != napi_ok || argc == 0) {
868         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
869         return nullptr;
870     }
871     if (!IsMatchArrayType(env, args[0])) {
872         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
873         return nullptr;
874     }
875     sptr<AsyncCallbackInfo> asyncCallbackInfo =
876         new (std::nothrow) AsyncCallbackInfo(env, GET_GEOMAGNITIC_DIP);
877     CHKPP(asyncCallbackInfo);
878     std::vector<float> inclinationMatrix;
879     if (!GetFloatArray(env, args[0], inclinationMatrix)) {
880         ThrowErr(env, PARAMETER_ERROR, "Get inclinationMatrix fail");
881         return nullptr;
882     }
883     float geomagneticDip = 0;
884     SensorAlgorithm sensorAlgorithm;
885     int32_t ret = sensorAlgorithm.GetGeomagneticDip(inclinationMatrix, &geomagneticDip);
886     if (ret != OHOS::ERR_OK) {
887         ThrowErr(env, ret, "Get geomagnetic dip fail");
888         return nullptr;
889     } else {
890         asyncCallbackInfo->data.reserveData.reserve[0] = geomagneticDip;
891     }
892     if (argc == 1) {
893         napi_value promise = nullptr;
894         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
895         if (status != napi_ok) {
896             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
897             return nullptr;
898         }
899         EmitPromiseWork(asyncCallbackInfo);
900         return promise;
901     }
902     if (!IsMatchType(env, args[1], napi_function)) {
903         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
904         return nullptr;
905     }
906     status = napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]);
907     if (status != napi_ok) {
908         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
909         return nullptr;
910     }
911     EmitAsyncCallbackWork(asyncCallbackInfo);
912     return nullptr;
913 }
914 
CreateRotationAndInclination(const napi_env & env,napi_value args[],size_t argc)915 static napi_value CreateRotationAndInclination(const napi_env &env, napi_value args[], size_t argc)
916 {
917     CALL_LOG_ENTER;
918     if (argc < 2) {
919         ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid");
920         return nullptr;
921     }
922     std::vector<float> gravity;
923     if (!GetFloatArray(env, args[0], gravity)) {
924         ThrowErr(env, PARAMETER_ERROR, "Get gravity fail");
925         return nullptr;
926     }
927     std::vector<float> geomagnetic;
928     if (!GetFloatArray(env, args[1], geomagnetic)) {
929         ThrowErr(env, PARAMETER_ERROR, "Get geomagnetic fail");
930         return nullptr;
931     }
932     std::vector<float> rotation(THREE_DIMENSIONAL_MATRIX_LENGTH);
933     std::vector<float> inclination(THREE_DIMENSIONAL_MATRIX_LENGTH);
934     sptr<AsyncCallbackInfo> asyncCallbackInfo =
935         new (std::nothrow) AsyncCallbackInfo(env, ROTATION_INCLINATION_MATRIX);
936     CHKPP(asyncCallbackInfo);
937     SensorAlgorithm sensorAlgorithm;
938     int32_t ret = sensorAlgorithm.CreateRotationAndInclination(gravity, geomagnetic, rotation, inclination);
939     if (ret != OHOS::ERR_OK) {
940         ThrowErr(env, ret, "Create rotation and inclination matrix fail");
941         return nullptr;
942     } else {
943         asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH;
944         for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) {
945             asyncCallbackInfo->data.rationMatrixData.rotationMatrix[i] = rotation[i];
946         }
947         for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) {
948             asyncCallbackInfo->data.rationMatrixData.inclinationMatrix[i] = inclination[i];
949         }
950     }
951     napi_status status;
952     if (argc == 2) {
953         napi_value promise = nullptr;
954         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
955         if (status != napi_ok) {
956             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
957             return nullptr;
958         }
959         EmitPromiseWork(asyncCallbackInfo);
960         return promise;
961     }
962     if (!IsMatchType(env, args[2], napi_function)) {
963         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
964         return nullptr;
965     }
966     status = napi_create_reference(env, args[2], 1, &asyncCallbackInfo->callback[0]);
967     if (status != napi_ok) {
968         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
969         return nullptr;
970     }
971     EmitAsyncCallbackWork(asyncCallbackInfo);
972     return nullptr;
973 }
974 
GetRotationMatrix(const napi_env & env,napi_value args[],size_t argc)975 static napi_value GetRotationMatrix(const napi_env &env, napi_value args[], size_t argc)
976 {
977     CALL_LOG_ENTER;
978     if (argc < 1) {
979         ThrowErr(env, PARAMETER_ERROR, "The number of parameters is not valid");
980         return nullptr;
981     }
982     std::vector<float> rotationVector;
983     if (!GetFloatArray(env, args[0], rotationVector)) {
984         ThrowErr(env, PARAMETER_ERROR, "Get rotationVector fail");
985         return nullptr;
986     }
987     sptr<AsyncCallbackInfo> asyncCallbackInfo =
988         new (std::nothrow) AsyncCallbackInfo(env, CREATE_ROTATION_MATRIX);
989     CHKPP(asyncCallbackInfo);
990     std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
991     SensorAlgorithm sensorAlgorithm;
992     int32_t ret = sensorAlgorithm.CreateRotationMatrix(rotationVector, rotationMatrix);
993     if (ret != OHOS::ERR_OK) {
994         ThrowErr(env, ret, "Create rotation matrix fail");
995         return nullptr;
996     } else {
997         asyncCallbackInfo->data.reserveData.length = THREE_DIMENSIONAL_MATRIX_LENGTH;
998         for (int32_t i = 0; i < THREE_DIMENSIONAL_MATRIX_LENGTH; ++i) {
999             asyncCallbackInfo->data.reserveData.reserve[i] = rotationMatrix[i];
1000         }
1001     }
1002     napi_status status;
1003     if (argc == 1) {
1004         napi_value promise = nullptr;
1005         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
1006         if (status != napi_ok) {
1007             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
1008             return nullptr;
1009         }
1010         EmitPromiseWork(asyncCallbackInfo);
1011         return promise;
1012     }
1013     if (!IsMatchType(env, args[1], napi_function)) {
1014         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
1015         return nullptr;
1016     }
1017     status = napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]);
1018     if (status != napi_ok) {
1019         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
1020         return nullptr;
1021     }
1022     EmitAsyncCallbackWork(asyncCallbackInfo);
1023     return nullptr;
1024 }
1025 
CreateRotationMatrix(napi_env env,napi_callback_info info)1026 static napi_value CreateRotationMatrix(napi_env env, napi_callback_info info)
1027 {
1028     CALL_LOG_ENTER;
1029     size_t argc = 3;
1030     napi_value args[3] = { 0 };
1031     napi_value thisVar = nullptr;
1032     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
1033     if (status != napi_ok || argc == 0) {
1034         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid");
1035         return nullptr;
1036     }
1037     if (!IsMatchArrayType(env, args[0])) {
1038         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
1039         return nullptr;
1040     }
1041     if (argc == 1 || (argc == 2 && IsMatchType(env, args[1], napi_function))) {
1042         return GetRotationMatrix(env, args, argc);
1043     } else if (IsMatchArrayType(env, args[1])) {
1044         return CreateRotationAndInclination(env, args, argc);
1045     } else {
1046         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be array");
1047         return nullptr;
1048     }
1049     return nullptr;
1050 }
1051 
GetSensorList(napi_env env,napi_callback_info info)1052 static napi_value GetSensorList(napi_env env, napi_callback_info info)
1053 {
1054     CALL_LOG_ENTER;
1055     size_t argc = 1;
1056     napi_value args[1] = { 0 };
1057     napi_value thisVar = nullptr;
1058     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
1059     if (status != napi_ok) {
1060         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail");
1061         return nullptr;
1062     }
1063     sptr<AsyncCallbackInfo> asyncCallbackInfo =
1064         new (std::nothrow) AsyncCallbackInfo(env, GET_SENSOR_LIST);
1065     CHKPP(asyncCallbackInfo);
1066     SensorInfo *sensorInfos = nullptr;
1067     int32_t count = 0;
1068     int32_t ret = GetAllSensors(&sensorInfos, &count);
1069     if (ret != OHOS::ERR_OK) {
1070         SEN_HILOGE("Get sensor list fail");
1071         asyncCallbackInfo->type = FAIL;
1072         asyncCallbackInfo->error.code = ret;
1073     } else {
1074         for (int32_t i = 0; i < count; ++i) {
1075             asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i));
1076         }
1077     }
1078     if (argc == 0) {
1079         napi_value promise = nullptr;
1080         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
1081         if (status != napi_ok) {
1082             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
1083             return nullptr;
1084         }
1085         EmitPromiseWork(asyncCallbackInfo);
1086         return promise;
1087     }
1088     if (!IsMatchType(env, args[0], napi_function)) {
1089         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
1090         return nullptr;
1091     }
1092     status = napi_create_reference(env, args[0], 1, &asyncCallbackInfo->callback[0]);
1093     if (status != napi_ok) {
1094         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
1095         return nullptr;
1096     }
1097     EmitAsyncCallbackWork(asyncCallbackInfo);
1098     return nullptr;
1099 }
1100 
GetSingleSensor(napi_env env,napi_callback_info info)1101 static napi_value GetSingleSensor(napi_env env, napi_callback_info info)
1102 {
1103     CALL_LOG_ENTER;
1104     size_t argc = 2;
1105     napi_value args[2] = { 0 };
1106     napi_value thisVar = nullptr;
1107     napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
1108     if (status != napi_ok) {
1109         ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail");
1110         return nullptr;
1111     }
1112     int32_t sensorTypeId = INVALID_SENSOR_ID;
1113     if (!GetNativeInt32(env, args[0], sensorTypeId)) {
1114         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail");
1115         return nullptr;
1116     }
1117     sptr<AsyncCallbackInfo> asyncCallbackInfo =
1118         new (std::nothrow) AsyncCallbackInfo(env, GET_SINGLE_SENSOR);
1119     CHKPP(asyncCallbackInfo);
1120     SensorInfo *sensorInfos = nullptr;
1121     int32_t count = 0;
1122     int32_t ret = GetAllSensors(&sensorInfos, &count);
1123     if (ret != OHOS::ERR_OK) {
1124         SEN_HILOGE("Get sensor list fail");
1125         asyncCallbackInfo->type = FAIL;
1126         asyncCallbackInfo->error.code = ret;
1127     } else {
1128         for (int32_t i = 0; i < count; ++i) {
1129             if (sensorInfos[i].sensorTypeId == sensorTypeId) {
1130                 asyncCallbackInfo->sensorInfos.push_back(*(sensorInfos + i));
1131                 break;
1132             }
1133         }
1134         if (asyncCallbackInfo->sensorInfos.empty()) {
1135             ThrowErr(env, PARAMETER_ERROR, "Can't find the sensorId");
1136             return nullptr;
1137         }
1138     }
1139     if (argc == 1) {
1140         napi_value promise = nullptr;
1141         status = napi_create_promise(env, &asyncCallbackInfo->deferred, &promise);
1142         if (status != napi_ok) {
1143             ThrowErr(env, PARAMETER_ERROR, "napi_create_promise fail");
1144             return nullptr;
1145         }
1146         EmitPromiseWork(asyncCallbackInfo);
1147         return promise;
1148     }
1149     if (!IsMatchType(env, args[1], napi_function)) {
1150         ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, should be function");
1151         return nullptr;
1152     }
1153     status = napi_create_reference(env, args[1], 1, &asyncCallbackInfo->callback[0]);
1154     if (status != napi_ok) {
1155         ThrowErr(env, PARAMETER_ERROR, "napi_create_reference fail");
1156         return nullptr;
1157     }
1158     EmitAsyncCallbackWork(asyncCallbackInfo);
1159     return nullptr;
1160 }
1161 
Subscribe(napi_env env,napi_callback_info info,int32_t sensorTypeId,CallbackDataType type)1162 napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId, CallbackDataType type)
1163 {
1164     CALL_LOG_ENTER;
1165     size_t argc = 1;
1166     napi_value args[1] = { 0 };
1167     napi_value thisVar = nullptr;
1168     CHKNRP(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr), "napi_get_cb_info");
1169     CHKCP((argc == 1), "The number of parameters is not valid");
1170     CHKCP(IsMatchType(env, args[0], napi_object), "Wrong argument type, should be object");
1171     string interval = "normal";
1172     if ((sensorTypeId == SENSOR_TYPE_ID_ACCELEROMETER) ||
1173         ((sensorTypeId == SENSOR_TYPE_ID_ORIENTATION) && (type != SUBSCRIBE_COMPASS))
1174         || (sensorTypeId == SENSOR_TYPE_ID_GYROSCOPE)) {
1175         napi_value napiInterval = GetNamedProperty(env, args[0], "interval");
1176         CHKCP(GetStringValue(env, napiInterval, interval), "get interval fail");
1177     }
1178     sptr<AsyncCallbackInfo> asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, type);
1179     CHKPP(asyncCallbackInfo);
1180     napi_value napiSuccess = GetNamedProperty(env, args[0], "success");
1181     CHKCP((napiSuccess != nullptr), "get napiSuccess fail");
1182     CHKCP(RegisterNapiCallback(env, napiSuccess, asyncCallbackInfo->callback[0]),
1183         "register callback fail");
1184     napi_value napiFail = GetNamedProperty(env, args[0], "fail");
1185     if (napiFail != nullptr) {
1186         SEN_HILOGD("has fail callback");
1187         CHKCP(RegisterNapiCallback(env, napiFail, asyncCallbackInfo->callback[1]),
1188             "register callback fail");
1189     }
1190     if (auto iter = g_samplingPeriod.find(interval); iter == g_samplingPeriod.end()) {
1191         CHKCP((napiFail != nullptr), "input error, interval is invalid");
1192         CreateFailMessage(SUBSCRIBE_FAIL, INPUT_ERROR, "input error", asyncCallbackInfo);
1193         EmitAsyncCallbackWork(asyncCallbackInfo);
1194         return nullptr;
1195     }
1196     std::lock_guard<std::mutex> subscribeCallbackLock(mutex_);
1197     int32_t ret = SubscribeSensor(sensorTypeId, g_samplingPeriod[interval], DataCallbackImpl);
1198     if (ret != OHOS::ERR_OK) {
1199         CHKCP((napiFail != nullptr), "subscribe fail");
1200         CreateFailMessage(SUBSCRIBE_FAIL, SENSOR_SUBSCRIBE_FAILURE, "subscribe fail", asyncCallbackInfo);
1201         EmitAsyncCallbackWork(asyncCallbackInfo);
1202         return nullptr;
1203     }
1204     g_subscribeCallbacks[sensorTypeId] = asyncCallbackInfo;
1205     return nullptr;
1206 }
1207 
Unsubscribe(napi_env env,napi_callback_info info,int32_t sensorTypeId)1208 napi_value Unsubscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId)
1209 {
1210     CALL_LOG_ENTER;
1211     size_t argc = 1;
1212     napi_value args[1] = { 0 };
1213     napi_value thisVar = nullptr;
1214     CHKNRP(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr), "napi_get_cb_info");
1215     CHKCP((argc == 0), "The number of parameters is not valid");
1216     std::lock_guard<std::mutex> subscribeCallbackLock(mutex_);
1217     g_subscribeCallbacks[sensorTypeId] = nullptr;
1218     if (CheckSubscribe(sensorTypeId)) {
1219         SEN_HILOGW("There are other client subscribe as well, not need unsubscribe");
1220         return nullptr;
1221     }
1222     if (UnsubscribeSensor(sensorTypeId) != OHOS::ERR_OK) {
1223         SEN_HILOGW("UnsubscribeSensor failed");
1224         return nullptr;
1225     }
1226     return nullptr;
1227 }
1228 
GetBodyState(napi_env env,napi_callback_info info)1229 napi_value GetBodyState(napi_env env, napi_callback_info info)
1230 {
1231     CALL_LOG_ENTER;
1232     size_t argc = 1;
1233     napi_value args[1] = { 0 };
1234     napi_value thisVar = nullptr;
1235     CHKNRP(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr), "napi_get_cb_info");
1236     CHKCP((argc == 1), "The number of parameters is not valid");
1237     CHKCP(IsMatchType(env, args[0], napi_object), "Wrong argument type, should be object");
1238     sptr<AsyncCallbackInfo> asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_BODY_STATE);
1239     CHKPP(asyncCallbackInfo);
1240     napi_value napiSuccess = GetNamedProperty(env, args[0], "success");
1241     CHKCP(RegisterNapiCallback(env, napiSuccess, asyncCallbackInfo->callback[0]),
1242         "register success callback fail");
1243     std::lock_guard<std::mutex> onBodyLock(bodyMutex_);
1244     asyncCallbackInfo->data.sensorData.data[0] =
1245         (fabs(g_bodyState - BODY_STATE_EXCEPT) < THREESHOLD) ? true : false;
1246     EmitUvEventLoop(asyncCallbackInfo);
1247     return nullptr;
1248 }
1249 
EnumClassConstructor(napi_env env,napi_callback_info info)1250 static napi_value EnumClassConstructor(napi_env env, napi_callback_info info)
1251 {
1252     size_t argc = 0;
1253     napi_value args[1] = {0};
1254     napi_value ret = nullptr;
1255     void *data = nullptr;
1256     CHKNRP(env, napi_get_cb_info(env, info, &argc, args, &ret, &data), "napi_get_cb_info");
1257     return ret;
1258 }
1259 
CreateEnumSensorType(napi_env env,napi_value exports)1260 static napi_value CreateEnumSensorType(napi_env env, napi_value exports)
1261 {
1262     napi_property_descriptor desc[] = {
1263         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_ACCELEROMETER", GetNapiInt32(env, SENSOR_TYPE_ID_ACCELEROMETER)),
1264         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_GYROSCOPE", GetNapiInt32(env, SENSOR_TYPE_ID_GYROSCOPE)),
1265         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_AMBIENT_LIGHT", GetNapiInt32(env, SENSOR_TYPE_ID_AMBIENT_LIGHT)),
1266         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_MAGNETIC_FIELD", GetNapiInt32(env, SENSOR_TYPE_ID_MAGNETIC_FIELD)),
1267         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_BAROMETER", GetNapiInt32(env, SENSOR_TYPE_ID_BAROMETER)),
1268         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_HALL", GetNapiInt32(env, SENSOR_TYPE_ID_HALL)),
1269         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_PROXIMITY", GetNapiInt32(env, SENSOR_TYPE_ID_PROXIMITY)),
1270         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_HUMIDITY", GetNapiInt32(env, SENSOR_TYPE_ID_HUMIDITY)),
1271         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_ORIENTATION", GetNapiInt32(env, SENSOR_TYPE_ID_ORIENTATION)),
1272         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_GRAVITY", GetNapiInt32(env, SENSOR_TYPE_ID_GRAVITY)),
1273         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_LINEAR_ACCELERATION",
1274             GetNapiInt32(env, SENSOR_TYPE_ID_LINEAR_ACCELERATION)),
1275         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_ROTATION_VECTOR",
1276             GetNapiInt32(env, SENSOR_TYPE_ID_ROTATION_VECTOR)),
1277         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_AMBIENT_TEMPERATURE",
1278             GetNapiInt32(env, SENSOR_TYPE_ID_AMBIENT_TEMPERATURE)),
1279         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED",
1280             GetNapiInt32(env, SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED)),
1281         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED",
1282             GetNapiInt32(env, SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED)),
1283         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_SIGNIFICANT_MOTION",
1284             GetNapiInt32(env, SENSOR_TYPE_ID_SIGNIFICANT_MOTION)),
1285         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_PEDOMETER_DETECTION",
1286             GetNapiInt32(env, SENSOR_TYPE_ID_PEDOMETER_DETECTION)),
1287         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_PEDOMETER", GetNapiInt32(env, SENSOR_TYPE_ID_PEDOMETER)),
1288         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_HEART_RATE", GetNapiInt32(env, SENSOR_TYPE_ID_HEART_RATE)),
1289         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_WEAR_DETECTION", GetNapiInt32(env, SENSOR_TYPE_ID_WEAR_DETECTION)),
1290         DECLARE_NAPI_STATIC_PROPERTY("SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED",
1291             GetNapiInt32(env, SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED)),
1292     };
1293     napi_value result = nullptr;
1294     CHKNRP(env, napi_define_class(env, "SensorType", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
1295         sizeof(desc) / sizeof(*desc), desc, &result), "napi_define_class");
1296     CHKNRP(env, napi_set_named_property(env, exports, "SensorType", result), "napi_set_named_property fail");
1297     return exports;
1298 }
1299 
CreateEnumSensorId(napi_env env,napi_value exports)1300 static napi_value CreateEnumSensorId(napi_env env, napi_value exports)
1301 {
1302     napi_property_descriptor desc[] = {
1303         DECLARE_NAPI_STATIC_PROPERTY("ACCELEROMETER", GetNapiInt32(env, SENSOR_TYPE_ID_ACCELEROMETER)),
1304         DECLARE_NAPI_STATIC_PROPERTY("GYROSCOPE", GetNapiInt32(env, SENSOR_TYPE_ID_GYROSCOPE)),
1305         DECLARE_NAPI_STATIC_PROPERTY("AMBIENT_LIGHT", GetNapiInt32(env, SENSOR_TYPE_ID_AMBIENT_LIGHT)),
1306         DECLARE_NAPI_STATIC_PROPERTY("MAGNETIC_FIELD", GetNapiInt32(env, SENSOR_TYPE_ID_MAGNETIC_FIELD)),
1307         DECLARE_NAPI_STATIC_PROPERTY("BAROMETER", GetNapiInt32(env, SENSOR_TYPE_ID_BAROMETER)),
1308         DECLARE_NAPI_STATIC_PROPERTY("HALL", GetNapiInt32(env, SENSOR_TYPE_ID_HALL)),
1309         DECLARE_NAPI_STATIC_PROPERTY("PROXIMITY", GetNapiInt32(env, SENSOR_TYPE_ID_PROXIMITY)),
1310         DECLARE_NAPI_STATIC_PROPERTY("HUMIDITY", GetNapiInt32(env, SENSOR_TYPE_ID_HUMIDITY)),
1311         DECLARE_NAPI_STATIC_PROPERTY("ORIENTATION", GetNapiInt32(env, SENSOR_TYPE_ID_ORIENTATION)),
1312         DECLARE_NAPI_STATIC_PROPERTY("GRAVITY", GetNapiInt32(env, SENSOR_TYPE_ID_GRAVITY)),
1313         DECLARE_NAPI_STATIC_PROPERTY("LINEAR_ACCELEROMETER", GetNapiInt32(env, SENSOR_TYPE_ID_LINEAR_ACCELERATION)),
1314         DECLARE_NAPI_STATIC_PROPERTY("ROTATION_VECTOR", GetNapiInt32(env, SENSOR_TYPE_ID_ROTATION_VECTOR)),
1315         DECLARE_NAPI_STATIC_PROPERTY("AMBIENT_TEMPERATURE", GetNapiInt32(env, SENSOR_TYPE_ID_AMBIENT_TEMPERATURE)),
1316         DECLARE_NAPI_STATIC_PROPERTY("MAGNETIC_FIELD_UNCALIBRATED",
1317             GetNapiInt32(env, SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED)),
1318         DECLARE_NAPI_STATIC_PROPERTY("GYROSCOPE_UNCALIBRATED",
1319             GetNapiInt32(env, SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED)),
1320         DECLARE_NAPI_STATIC_PROPERTY("SIGNIFICANT_MOTION", GetNapiInt32(env, SENSOR_TYPE_ID_SIGNIFICANT_MOTION)),
1321         DECLARE_NAPI_STATIC_PROPERTY("PEDOMETER_DETECTION", GetNapiInt32(env, SENSOR_TYPE_ID_PEDOMETER_DETECTION)),
1322         DECLARE_NAPI_STATIC_PROPERTY("PEDOMETER", GetNapiInt32(env, SENSOR_TYPE_ID_PEDOMETER)),
1323         DECLARE_NAPI_STATIC_PROPERTY("HEART_RATE", GetNapiInt32(env, SENSOR_TYPE_ID_HEART_RATE)),
1324         DECLARE_NAPI_STATIC_PROPERTY("WEAR_DETECTION", GetNapiInt32(env, SENSOR_TYPE_ID_WEAR_DETECTION)),
1325         DECLARE_NAPI_STATIC_PROPERTY("ACCELEROMETER_UNCALIBRATED",
1326             GetNapiInt32(env, SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED)),
1327     };
1328     napi_value result = nullptr;
1329     CHKNRP(env, napi_define_class(env, "SensorId", NAPI_AUTO_LENGTH, EnumClassConstructor, nullptr,
1330         sizeof(desc) / sizeof(*desc), desc, &result), "napi_define_class");
1331     CHKNRP(env, napi_set_named_property(env, exports, "SensorId", result), "napi_set_named_property fail");
1332     return exports;
1333 }
1334 
Init(napi_env env,napi_value exports)1335 static napi_value Init(napi_env env, napi_value exports)
1336 {
1337     napi_property_descriptor desc[] = {
1338         DECLARE_NAPI_FUNCTION("on", On),
1339         DECLARE_NAPI_FUNCTION("once", Once),
1340         DECLARE_NAPI_FUNCTION("off", Off),
1341         DECLARE_NAPI_FUNCTION("getGeomagneticField", GetGeomagneticField),
1342         DECLARE_NAPI_FUNCTION("getGeomagneticInfo", GetGeomagneticField),
1343         DECLARE_NAPI_FUNCTION("transformCoordinateSystem", TransformCoordinateSystem),
1344         DECLARE_NAPI_FUNCTION("transformRotationMatrix", TransformCoordinateSystem),
1345         DECLARE_NAPI_FUNCTION("getAngleModify", GetAngleModify),
1346         DECLARE_NAPI_FUNCTION("getAngleVariation", GetAngleModify),
1347         DECLARE_NAPI_FUNCTION("getDirection", GetDirection),
1348         DECLARE_NAPI_FUNCTION("getOrientation", GetDirection),
1349         DECLARE_NAPI_FUNCTION("createQuaternion", CreateQuaternion),
1350         DECLARE_NAPI_FUNCTION("getQuaternion", CreateQuaternion),
1351         DECLARE_NAPI_FUNCTION("getAltitude", GetAltitude),
1352         DECLARE_NAPI_FUNCTION("getDeviceAltitude", GetAltitude),
1353         DECLARE_NAPI_FUNCTION("getGeomagneticDip", GetGeomagneticDip),
1354         DECLARE_NAPI_FUNCTION("getInclination", GetGeomagneticDip),
1355         DECLARE_NAPI_FUNCTION("createRotationMatrix", CreateRotationMatrix),
1356         DECLARE_NAPI_FUNCTION("getRotationMatrix", CreateRotationMatrix),
1357         DECLARE_NAPI_FUNCTION("getSensorList", GetSensorList),
1358         DECLARE_NAPI_FUNCTION("getSingleSensor", GetSingleSensor),
1359         DECLARE_NAPI_FUNCTION("subscribeAccelerometer", SubscribeAccelerometer),
1360         DECLARE_NAPI_FUNCTION("unsubscribeAccelerometer", UnsubscribeAccelerometer),
1361         DECLARE_NAPI_FUNCTION("subscribeCompass", SubscribeCompass),
1362         DECLARE_NAPI_FUNCTION("unsubscribeCompass", UnsubscribeCompass),
1363         DECLARE_NAPI_FUNCTION("subscribeProximity", SubscribeProximity),
1364         DECLARE_NAPI_FUNCTION("unsubscribeProximity", UnsubscribeProximity),
1365         DECLARE_NAPI_FUNCTION("subscribeLight", SubscribeLight),
1366         DECLARE_NAPI_FUNCTION("unsubscribeLight", UnsubscribeLight),
1367         DECLARE_NAPI_FUNCTION("subscribeStepCounter", SubscribeStepCounter),
1368         DECLARE_NAPI_FUNCTION("unsubscribeStepCounter", UnsubscribeStepCounter),
1369         DECLARE_NAPI_FUNCTION("subscribeBarometer", SubscribeBarometer),
1370         DECLARE_NAPI_FUNCTION("unsubscribeBarometer", UnsubscribeBarometer),
1371         DECLARE_NAPI_FUNCTION("subscribeHeartRate", SubscribeHeartRate),
1372         DECLARE_NAPI_FUNCTION("unsubscribeHeartRate", UnsubscribeHeartRate),
1373         DECLARE_NAPI_FUNCTION("subscribeOnBodyState", SubscribeOnBodyState),
1374         DECLARE_NAPI_FUNCTION("unsubscribeOnBodyState", UnsubscribeOnBodyState),
1375         DECLARE_NAPI_FUNCTION("getOnBodyState", GetOnBodyState),
1376         DECLARE_NAPI_FUNCTION("subscribeDeviceOrientation", SubscribeDeviceOrientation),
1377         DECLARE_NAPI_FUNCTION("unsubscribeDeviceOrientation", UnsubscribeDeviceOrientation),
1378         DECLARE_NAPI_FUNCTION("subscribeGyroscope", SubscribeGyroscope),
1379         DECLARE_NAPI_FUNCTION("unsubscribeGyroscope", UnsubscribeGyroscope),
1380         DECLARE_NAPI_FUNCTION("subscribeGravity", SubscribeGravity),
1381         DECLARE_NAPI_FUNCTION("unsubscribeGravity", UnsubscribeGravity),
1382         DECLARE_NAPI_FUNCTION("subscribeMagnetic", SubscribeMagnetic),
1383         DECLARE_NAPI_FUNCTION("unsubscribeMagnetic", UnsubscribeMagnetic),
1384         DECLARE_NAPI_FUNCTION("subscribeHall", SubscribeHall),
1385         DECLARE_NAPI_FUNCTION("unsubscribeHall", UnsubscribeHall),
1386     };
1387     CHKNRP(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc),
1388         "napi_define_properties");
1389     CHKCP(CreateEnumSensorType(env, exports), "Create enum sensor type fail");
1390     CHKCP(CreateEnumSensorId(env, exports), "Create enum sensor id fail");
1391     return exports;
1392 }
1393 
1394 static napi_module _module = {
1395     .nm_version = 1,
1396     .nm_flags = 0,
1397     .nm_filename = nullptr,
1398     .nm_register_func = Init,
1399     .nm_modname = "sensor",
1400     .nm_priv = ((void *)0),
1401     .reserved = {0}
1402 };
1403 
RegisterModule(void)1404 extern "C" __attribute__((constructor)) void RegisterModule(void)
1405 {
1406     napi_module_register(&_module);
1407 }
1408 }  // namespace Sensors
1409 }  // namespace OHOS