1 /*
2 * Copyright (C) 2023 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
16 #ifndef NFC_NAPI_COMMON_UTILS_H
17 #define NFC_NAPI_COMMON_UTILS_H
18 #include <chrono>
19 #include "element_name.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "ndef_message.h"
23 #include "nfc_ha_event_report.h"
24
25 namespace OHOS {
26 namespace NFC {
27 namespace KITS {
28 using OHOS::AppExecFwk::ElementName;
29
30 // business error code, throw these errors to applcation.
31 const static int BUSI_ERR_PERM = 201; // Permission denied.
32 const static int BUSI_ERR_NOT_SYSTEM_APP = 202; // not system app.
33 const static int BUSI_ERR_PARAM = 401; // The parameter check failed.
34 const static int BUSI_ERR_CAPABILITY = 801; // Capability not supported.
35
36 const static int BUSI_ERR_TAG_STATE_INVALID = 3100201; // nfc tag state invalid.
37 const static int BUSI_ERR_ELEMENT_STATE_INVALID = 3100202; // The element state is invalid.
38 const static int BUSI_ERR_REGISTER_STATE_INVALID = 3100203; // The off() can be called only when the on()
39 // has been called.
40 const static int BUSI_ERR_IO_OPERATION_INVALID = 3100204; // Tag I/O operation failed.
41 const static uint32_t MAX_NUM_TECH_LIST = 12;
42
43 const static int BUSI_ERR_HCE_STATE_INVALID = 3100301; // nfc hce state invalid.
44
45 const std::string KEY_CODE = "code";
46 const std::string TAG_PERM_DESC = "ohos.permission.NFC_TAG";
47 const std::string CARD_EMULATION_PERM_DESC = "ohos.permission.NFC_CARD_EMULATION";
48 const std::string ERR_INIT_CONTEXT = "Initialize context failed.";
49
50 enum JS_CALLBACK_ARGV : size_t {
51 CALLBACK_ARGV_INDEX_0 = 0,
52 CALLBACK_ARGV_INDEX_1,
53 CALLBACK_ARGV_CNT,
54 };
55
56 enum JS_ARGV_NUM : size_t {
57 ARGV_NUM_0 = 0,
58 ARGV_NUM_1 = 1,
59 ARGV_NUM_2 = 2,
60 ARGV_NUM_3 = 3,
61 ARGV_NUM_4 = 4,
62 ARGV_NUM_5 = 5,
63 };
64
65 enum JS_ARGV_INDEX : size_t {
66 ARGV_INDEX_0 = 0,
67 ARGV_INDEX_1,
68 ARGV_INDEX_2,
69 ARGV_INDEX_3,
70 ARGV_INDEX_4,
71 };
72
73 struct NfcAsyncContext {
74 napi_async_work work = nullptr;
75 napi_deferred deferred = nullptr;
76 napi_ref callbackRef = nullptr;
77 int32_t result;
78 int32_t uid = 0;
79 bool flag = false;
80 };
81
82 struct BaseContext {
83 napi_async_work work = nullptr;
84 napi_deferred deferred = nullptr;
85 napi_ref callbackRef = nullptr;
86 bool resolved = false;
87 int32_t errorCode = 0;
88 std::shared_ptr<NfcHaEventReport> eventReport = nullptr;
89 };
90
91 class AsyncContext {
92 public:
93 napi_env env;
94 napi_async_work work;
95 napi_deferred deferred;
96 napi_ref callback[2] = {0};
97 std::function<void(void *)> executeFunc;
98 std::function<void(void *)> completeFunc;
99 napi_value resourceName;
100 napi_value result;
101 int errorCode = 0;
102
103 explicit AsyncContext(napi_env e, napi_async_work w = nullptr, napi_deferred d = nullptr)
env(e)104 :env(e),
105 work(w),
106 deferred(d),
107 executeFunc(nullptr),
108 completeFunc(nullptr),
109 result(nullptr) {};
110
111 AsyncContext() = delete;
112
~AsyncContext()113 virtual ~AsyncContext() {}
114 };
115
116 template<typename T, typename D>
117 struct CallBackContext : BaseContext {
118 T value;
119 D *objectInfo;
120 };
121 template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int32_t> = 0>
GetNapiValue(napi_env env,T val)122 napi_value GetNapiValue(napi_env env, T val)
123 {
124 napi_value result = nullptr;
125 NAPI_CALL(env, napi_get_boolean(env, val, &result));
126 return result;
127 }
128
129 template<typename T, std::enable_if_t<std::is_same_v<T, int32_t>, int32_t> = 0>
GetNapiValue(napi_env env,T val)130 napi_value GetNapiValue(napi_env env, T val)
131 {
132 napi_value result = nullptr;
133 NAPI_CALL(env, napi_create_int32(env, val, &result));
134 return result;
135 }
136
137 template<typename T, std::enable_if_t<std::is_same_v<T, int64_t>, int64_t> = 0>
GetNapiValue(napi_env env,T val)138 napi_value GetNapiValue(napi_env env, T val)
139 {
140 napi_value result = nullptr;
141 NAPI_CALL(env, napi_create_int64(env, val, &result));
142 return result;
143 }
144
145 template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int32_t> = 0>
GetNapiValue(napi_env env,const T & val)146 napi_value GetNapiValue(napi_env env, const T &val)
147 {
148 napi_value result = nullptr;
149 NAPI_CALL(env, napi_create_string_utf8(env, val.c_str(), val.length(), &result));
150 return result;
151 }
152
153 template<typename T, std::enable_if_t<std::is_same_v<T, char>, int32_t> = 0>
GetNapiValue(napi_env env,const T * val)154 napi_value GetNapiValue(napi_env env, const T *val)
155 {
156 napi_value result = nullptr;
157 NAPI_CALL(env, napi_create_string_utf8(env, val, NAPI_AUTO_LENGTH, &result));
158 return result;
159 }
160
161 template<typename T, std::enable_if_t<std::is_same_v<T, napi_value>, int32_t> = 0>
GetNapiValue(napi_env env,T val)162 napi_value GetNapiValue(napi_env env, T val)
163 {
164 return val;
165 }
166
167 bool ParseString(napi_env env, std::string ¶m, napi_value args);
168 bool ParseInt32(napi_env env, int32_t ¶m, napi_value args);
169 bool ParseBool(napi_env env, bool ¶m, napi_value args);
170 bool ParseBytesVector(napi_env env, std::vector<unsigned char> &vec, napi_value args);
171 bool ParseUInt32Vector(napi_env &env, std::vector<uint32_t> &vec, napi_value &args);
172 bool ParseStringVector(napi_env& env, std::vector<std::string>& vec, napi_value &args, uint32_t maxLen);
173 bool ParseElementName(napi_env &env, ElementName &element, napi_value &args);
174 bool ParseArrayBuffer(napi_env env, uint8_t **data, size_t &size, napi_value args);
175 std::vector<std::string> ConvertStringVector(napi_env env, napi_value jsValue);
176 napi_value CreateErrorMessage(napi_env env, const std::string &msg, int32_t errorCode = 0);
177 napi_value CreateUndefined(napi_env env);
178 std::string GetNapiStringValue(
179 napi_env env, napi_value napiValue, const std::string &name, const std::string &defValue = "");
180 std::string GetStringFromValue(napi_env env, napi_value value);
181 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
182 int32_t GetNapiInt32Value(napi_env env, napi_value napiValue, const std::string &name, const int32_t &defValue = 0);
183 std::string UnwrapStringFromJS(napi_env env, napi_value param);
184 void ConvertStringVectorToJS(napi_env env, napi_value &result, std::vector<std::string> &stringVector);
185 void JsStringToBytesVector(napi_env env, napi_value &src, std::vector<unsigned char> &values);
186 void BytesVectorToJS(napi_env env, napi_value &result, std::vector<unsigned char> &src);
187 void ConvertStringToNumberArray(napi_env env, napi_value &result, std::string srcValue);
188 void ConvertNdefRecordVectorToJS(napi_env env, napi_value &result,
189 std::vector<std::shared_ptr<NdefRecord>> &ndefRecords);
190 void ConvertNdefRecordToJS(napi_env env, napi_value &result, std::shared_ptr<NdefRecord> &ndefRecord);
191 bool MatchParameters(napi_env env, const napi_value parameters[], std::initializer_list<napi_valuetype> valueTypes);
192 napi_value HandleAsyncWork(napi_env env, BaseContext *baseContext, const std::string &workName,
193 napi_async_execute_callback execute, napi_async_complete_callback complete);
194 void DoAsyncCallbackOrPromise(const napi_env &env, BaseContext *baseContext, napi_value callbackValue);
195 void ThrowAsyncError(const napi_env &env, BaseContext *baseContext, int errCode, const std::string &errMsg);
196 bool IsNumberArray(const napi_env &env, const napi_value ¶m);
197 bool IsObjectArray(const napi_env &env, const napi_value ¶m);
198 bool IsArray(const napi_env &env, const napi_value ¶m);
199 bool IsNumber(const napi_env &env, const napi_value ¶m);
200 bool IsString(const napi_env &env, const napi_value ¶m);
201 bool IsObject(const napi_env &env, const napi_value ¶m);
202 bool IsFunction(const napi_env &env, const napi_value ¶m);
203 int BuildOutputErrorCode(int errCode);
204 int BuildOutputErrorCodeHce(int errCode);
205 std::string BuildErrorMessage(int errCode, std::string funcName, std::string forbiddenPerm,
206 std::string paramName, std::string expertedType);
207 napi_value GenerateBusinessError(const napi_env &env, int errCode, const std::string &errMessage);
208 bool CheckUnwrapStatusAndThrow(const napi_env &env, napi_status status, int errCode);
209 bool CheckContextAndThrow(const napi_env &env, const BaseContext *context, int errCode);
210 bool CheckParametersAndThrow(const napi_env &env, const napi_value parameters[],
211 std::initializer_list<napi_valuetype> types, const std::string &argName, const std::string &argType);
212 bool CheckArrayNumberAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
213 const std::string &argType);
214 bool CheckNumberAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
215 const std::string &argType);
216 bool CheckStringAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
217 const std::string &argType);
218 bool CheckObjectAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
219 const std::string &argType);
220 bool CheckFunctionAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
221 const std::string &argType);
222 bool CheckArgCountAndThrow(const napi_env &env, int argCount, int expCount);
223 bool CheckTagStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string &funcName);
224 bool CheckHceStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string &funcName);
225 } // namespace KITS
226 } // namespace NFC
227 } // namespace OHOS
228 #endif