• 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 #ifndef SENSOR_NAPI_UTILS_H
16 #define SENSOR_NAPI_UTILS_H
17 
18 #include <iostream>
19 
20 #include "refbase.h"
21 
22 #include "async_callback_info.h"
23 
24 namespace OHOS {
25 namespace Sensors {
26 using std::vector;
27 using std::string;
28 using ConvertDataFunc = bool(*)(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo,
29     napi_value result[2]);
30 
31 bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs);
32 bool IsMatchType(const napi_env &env, const napi_value &value, const napi_valuetype &type);
33 bool IsMatchArrayType(const napi_env &env, const napi_value &value);
34 bool GetNativeInt32(const napi_env &env, const napi_value &value, int32_t &number);
35 bool GetNativeDouble(const napi_env &env, const napi_value &value, double &number);
36 bool GetFloatArray(const napi_env &env, const napi_value &value, vector<float> &array);
37 bool GetNativeInt64(const napi_env &env, const napi_value &value, int64_t &number);
38 bool RegisterNapiCallback(const napi_env &env, const napi_value &value, napi_ref &callback);
39 napi_value GetNamedProperty(const napi_env &env, const napi_value &object, string name);
40 bool GetNativeFloat(const napi_env &env, const napi_value &value, float &number);
41 napi_value GetNapiInt32(const napi_env &env, int32_t number);
42 bool GetStringValue(const napi_env &env, const napi_value &value, string &result);
43 void EmitAsyncCallbackWork(sptr<AsyncCallbackInfo> asyncCallbackInfo);
44 void EmitUvEventLoop(sptr<AsyncCallbackInfo> asyncCallbackInfo, std::shared_ptr<CallbackSensorData> cb);
45 void EmitPromiseWork(sptr<AsyncCallbackInfo> asyncCallbackInfo);
46 bool ConvertToFailData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
47 bool ConvertToGeomagneticData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
48 bool ConvertToNumber(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
49 bool ConvertToArray(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
50 bool ConvertToRotationMatrix(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
51 bool ConvertToSensorData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2],
52     int32_t resultSize, std::shared_ptr<CallbackSensorData> data);
53 bool CreateNapiArray(const napi_env &env, float *data, int32_t dataLength, napi_value &result);
54 bool ConvertToSensorInfos(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
55 bool ConvertToSingleSensor(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
56 bool ConvertToSensorInfo(const napi_env &env, const SensorInfo &sensorInfo, napi_value &result);
57 bool ConvertToBodyData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
58 bool CreateFailMessage(CallbackDataType type, int32_t code, string message,
59     sptr<AsyncCallbackInfo> &asyncCallbackInfo);
60 bool ConvertToBodyData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
61 bool ConvertToCompass(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
62 void ReleaseCallback(sptr<AsyncCallbackInfo> asyncCallbackInfo);
63 bool GetSelfTargetVersion(uint32_t &targetVersion);
64 bool ConvertToSensorState(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]);
65 
66 
67 #define CHKNCF(env, cond, message) \
68     do { \
69         if (!(cond)) { \
70             SEN_HILOGE("(%{public}s)", #message); \
71             return false; \
72         } \
73     } while (0)
74 
75 #define CHKNRP(env, state, message) \
76     do { \
77         if ((state) != napi_ok) { \
78             SEN_HILOGE("(%{public}s) fail", #message); \
79             return nullptr; \
80         } \
81     } while (0)
82 
83 #define CHKNRF(env, state, message) \
84     do { \
85         if ((state) != napi_ok) { \
86             SEN_HILOGE("(%{public}s) fail", #message); \
87             return false; \
88         } \
89     } while (0)
90 
91 #define CHKNRF(env, state, message) \
92     do { \
93         if ((state) != napi_ok) { \
94             SEN_HILOGE("(%{public}s) fail", #message); \
95             return false; \
96         } \
97     } while (0)
98 } // namespace Sensors
99 } // namespace OHOS
100 #endif // SENSOR_NAPI_UTILS_H
101