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 "async_callback_info.h" 21 #include "refbase.h" 22 namespace OHOS { 23 namespace Sensors { 24 using std::vector; 25 using std::string; 26 using ConvertDataFunc = bool(*)(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, 27 napi_value result[2]); 28 29 bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs); 30 bool IsMatchType(const napi_env &env, const napi_value &value, const napi_valuetype &type); 31 bool IsMatchArrayType(const napi_env &env, const napi_value &value); 32 bool GetCppInt32(const napi_env &env, const napi_value &value, int32_t &number); 33 bool GetCppDouble(const napi_env &env, const napi_value &value, double &number); 34 bool GetCppBool(const napi_env &env, const napi_value &value); 35 bool GetFloatArray(const napi_env &env, const napi_value &value, vector<float> &array); 36 bool GetCppInt64(const napi_env &env, const napi_value &value, int64_t &number); 37 bool RegisterNapiCallback(const napi_env &env, const napi_value &value, napi_ref &callback); 38 napi_value GetNamedProperty(const napi_env &env, const napi_value &object, string name); 39 bool GetCppFloat(const napi_env &env, const napi_value &value, float &number); 40 napi_value GetNapiInt32(const napi_env &env, int32_t number); 41 bool GetStringValue(const napi_env &env, const napi_value &value, string &result); 42 void EmitAsyncCallbackWork(sptr<AsyncCallbackInfo> asyncCallbackInfo); 43 void EmitUvEventLoop(sptr<AsyncCallbackInfo> asyncCallbackInfo); 44 void EmitPromiseWork(sptr<AsyncCallbackInfo> asyncCallbackInfo); 45 46 napi_value GreateBusinessError(const napi_env &env, int32_t errCode, string errMessage, 47 string errName, string errStack); 48 bool ConvertToFailData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 49 bool ConvertToGeomagneticData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 50 bool ConvertToNumber(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 51 bool ConvertToArray(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 52 bool ConvertToRotationMatrix(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 53 bool ConvertToSensorData(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 54 bool CreateNapiArray(const napi_env &env, float *data, int32_t dataLength, napi_value &result); 55 bool ConvertToSensorInfos(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 56 bool ConvertToSingleSensor(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[2]); 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 63 #define GET_AND_THROW_NAPI_ERROR(env, message) \ 64 do { \ 65 const napi_extended_error_info* errorInfo = nullptr; \ 66 napi_get_last_error_info((env), &errorInfo); \ 67 bool isPending = false; \ 68 napi_is_exception_pending((env), &isPending); \ 69 if (!isPending && errorInfo != nullptr) { \ 70 std::string errDesc = std::string(__FUNCTION__) + ": " + #message + " fail. "; \ 71 std::string errorMessage = \ 72 errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message"; \ 73 errDesc += errorMessage; \ 74 napi_throw_error((env), nullptr, errDesc.c_str()); \ 75 } \ 76 } while (0) 77 78 #define CHKNCR(env, cond, message, retVal) \ 79 do { \ 80 if (!(cond)) { \ 81 SEN_HILOGE("(%{public}s)", #message); \ 82 auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ 83 napi_throw_error(env, nullptr, errDesc.c_str()); \ 84 return retVal; \ 85 } \ 86 } while (0) 87 88 #define CHKNCP(env, cond, message) \ 89 do { \ 90 if (!(cond)) { \ 91 SEN_HILOGE("(%{public}s)", #message); \ 92 auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ 93 napi_throw_error(env, nullptr, errDesc.c_str()); \ 94 return nullptr; \ 95 } \ 96 } while (0) 97 98 #define CHKNCF(env, cond, message) \ 99 do { \ 100 if (!(cond)) { \ 101 SEN_HILOGE("(%{public}s)", #message); \ 102 auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ 103 napi_throw_error(env, nullptr, errDesc.c_str()); \ 104 return false; \ 105 } \ 106 } while (0) 107 108 #define CHKNCV(env, cond, message) \ 109 do { \ 110 if (!(cond)) { \ 111 SEN_HILOGE("(%{public}s)", #message); \ 112 auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ 113 napi_throw_error(env, nullptr, errDesc.c_str()); \ 114 return; \ 115 } \ 116 } while (0) 117 118 #define CHKNCC(env, cond, message) \ 119 { \ 120 if (!(cond)) { \ 121 SEN_HILOGW("(%{public}s)", #message); \ 122 auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ 123 napi_throw_error(env, nullptr, errDesc.c_str()); \ 124 continue; \ 125 } \ 126 } 127 128 #define CHKNRR(env, state, message, retVal) \ 129 do { \ 130 if ((state) != napi_ok) { \ 131 SEN_HILOGE("(%{public}s) fail", #message); \ 132 GET_AND_THROW_NAPI_ERROR((env), (message)); \ 133 return retVal; \ 134 } \ 135 } while (0) 136 137 #define CHKNRP(env, state, message) \ 138 do { \ 139 if ((state) != napi_ok) { \ 140 SEN_HILOGE("(%{public}s) fail", #message); \ 141 GET_AND_THROW_NAPI_ERROR((env), (message)); \ 142 return nullptr; \ 143 } \ 144 } while (0) 145 146 #define CHKNRF(env, state, message) \ 147 do { \ 148 if ((state) != napi_ok) { \ 149 SEN_HILOGE("(%{public}s) fail", #message); \ 150 GET_AND_THROW_NAPI_ERROR((env), (message)); \ 151 return false; \ 152 } \ 153 } while (0) 154 155 #define CHKNRV(env, state, message) \ 156 do { \ 157 if ((state) != napi_ok) { \ 158 SEN_HILOGE("(%{public}s) fail", #message); \ 159 GET_AND_THROW_NAPI_ERROR((env), (message)); \ 160 return; \ 161 } \ 162 } while (0) 163 164 #define CHKNRC(env, state, message) \ 165 { \ 166 if ((state) != napi_ok) { \ 167 SEN_HILOGW("(%{public}s) fail", #message); \ 168 GET_AND_THROW_NAPI_ERROR((env), (message)); \ 169 continue; \ 170 } \ 171 } 172 } // namespace Sensors 173 } // namespace OHOS 174 #endif // SENSOR_NAPI_UTILS_H 175