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