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); 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 bool CreateNapiArray(const napi_env &env, float *data, int32_t dataLength, napi_value &result); 53 bool ConvertToSensorInfos(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 54 bool ConvertToSingleSensor(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 55 bool ConvertToBodyData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 56 bool CreateFailMessage(CallbackDataType type, int32_t code, string message, 57 sptr<AsyncCallbackInfo> &asyncCallbackInfo); 58 bool ConvertToBodyData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 59 bool ConvertToCompass(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 60 void ReleaseCallback(sptr<AsyncCallbackInfo> asyncCallbackInfo); 61 62 63 #define CHKNCF(env, cond, message) \ 64 do { \ 65 if (!(cond)) { \ 66 SEN_HILOGE("(%{public}s)", #message); \ 67 return false; \ 68 } \ 69 } while (0) 70 71 #define CHKNRP(env, state, message) \ 72 do { \ 73 if ((state) != napi_ok) { \ 74 SEN_HILOGE("(%{public}s) fail", #message); \ 75 return nullptr; \ 76 } \ 77 } while (0) 78 79 #define CHKNRF(env, state, message) \ 80 do { \ 81 if ((state) != napi_ok) { \ 82 SEN_HILOGE("(%{public}s) fail", #message); \ 83 return false; \ 84 } \ 85 } while (0) 86 87 #define CHKNRF(env, state, message) \ 88 do { \ 89 if ((state) != napi_ok) { \ 90 SEN_HILOGE("(%{public}s) fail", #message); \ 91 return false; \ 92 } \ 93 } while (0) 94 } // namespace Sensors 95 } // namespace OHOS 96 #endif // SENSOR_NAPI_UTILS_H 97