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
24 namespace OHOS {
25 namespace NFC {
26 namespace KITS {
27 using OHOS::AppExecFwk::ElementName;
28
29 // business error code, throw these errors to applcation.
30 const static int BUSI_ERR_PERM = 201; // Permission denied.
31 const static int BUSI_ERR_NOT_SYSTEM_APP = 202; // not system app.
32 const static int BUSI_ERR_PARAM = 401; // The parameter check failed.
33 const static int BUSI_ERR_CAPABILITY = 801; // Capability not supported.
34
35 const static int BUSI_ERR_TAG_STATE_INVALID = 3100201; // nfc tag state invalid.
36 const static int BUSI_ERR_ELEMENT_STATE_INVALID = 3100202; // The element state is invalid.
37 const static int BUSI_ERR_REGISTER_STATE_INVALID = 3100203; // The off() can be called only when the on()
38 // has been called.
39 const static int BUSI_ERR_IO_OPERATION_INVALID = 3100204; // Tag I/O operation failed.
40 const static int BUSI_ERR_TAG_LEAVES_FIELD = 3100205; // The tag leaves the field.
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 };
89
90 class AsyncContext {
91 public:
92 napi_env env;
93 napi_async_work work;
94 napi_deferred deferred;
95 napi_ref callback[2] = {0};
96 std::function<void(void *)> executeFunc;
97 std::function<void(void *)> completeFunc;
98 napi_value resourceName;
99 napi_value result;
100 int errorCode = 0;
101
102 explicit AsyncContext(napi_env e, napi_async_work w = nullptr, napi_deferred d = nullptr)
env(e)103 :env(e),
104 work(w),
105 deferred(d),
106 executeFunc(nullptr),
107 completeFunc(nullptr),
108 result(nullptr) {};
109
110 AsyncContext() = delete;
111
~AsyncContext()112 virtual ~AsyncContext() {}
113 };
114
115 template<typename T, typename D>
116 struct CallBackContext : BaseContext {
117 T value;
118 D *objectInfo;
119 };
120 template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int32_t> = 0>
GetNapiValue(napi_env env,T val)121 napi_value GetNapiValue(napi_env env, T val)
122 {
123 napi_value result = nullptr;
124 NAPI_CALL(env, napi_get_boolean(env, val, &result));
125 return result;
126 }
127
128 template<typename T, std::enable_if_t<std::is_same_v<T, int32_t>, int32_t> = 0>
GetNapiValue(napi_env env,T val)129 napi_value GetNapiValue(napi_env env, T val)
130 {
131 napi_value result = nullptr;
132 NAPI_CALL(env, napi_create_int32(env, val, &result));
133 return result;
134 }
135
136 template<typename T, std::enable_if_t<std::is_same_v<T, int64_t>, int64_t> = 0>
GetNapiValue(napi_env env,T val)137 napi_value GetNapiValue(napi_env env, T val)
138 {
139 napi_value result = nullptr;
140 NAPI_CALL(env, napi_create_int64(env, val, &result));
141 return result;
142 }
143
144 template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int32_t> = 0>
GetNapiValue(napi_env env,const T & val)145 napi_value GetNapiValue(napi_env env, const T &val)
146 {
147 napi_value result = nullptr;
148 NAPI_CALL(env, napi_create_string_utf8(env, val.c_str(), val.length(), &result));
149 return result;
150 }
151
152 template<typename T, std::enable_if_t<std::is_same_v<T, char>, int32_t> = 0>
GetNapiValue(napi_env env,const T * val)153 napi_value GetNapiValue(napi_env env, const T *val)
154 {
155 napi_value result = nullptr;
156 NAPI_CALL(env, napi_create_string_utf8(env, val, NAPI_AUTO_LENGTH, &result));
157 return result;
158 }
159
160 template<typename T, std::enable_if_t<std::is_same_v<T, napi_value>, int32_t> = 0>
GetNapiValue(napi_env env,T val)161 napi_value GetNapiValue(napi_env env, T val)
162 {
163 return val;
164 }
165
166 bool ParseString(napi_env env, std::string ¶m, napi_value args);
167 bool ParseInt32(napi_env env, int32_t ¶m, napi_value args);
168 bool ParseBool(napi_env env, bool ¶m, napi_value args);
169 bool ParseBytesVector(napi_env env, std::vector<unsigned char> &vec, napi_value args);
170 bool ParseUInt32Vector(napi_env &env, std::vector<uint32_t> &vec, napi_value &args);
171 bool ParseStringVector(napi_env& env, std::vector<std::string>& vec, napi_value &args, uint32_t maxLen);
172 bool ParseElementName(napi_env &env, ElementName &element, napi_value &args);
173 bool ParseArrayBuffer(napi_env env, uint8_t **data, size_t &size, napi_value args);
174 std::vector<std::string> ConvertStringVector(napi_env env, napi_value jsValue);
175 napi_value CreateErrorMessage(napi_env env, const std::string &msg, int32_t errorCode = 0);
176 napi_value CreateUndefined(napi_env env);
177 std::string GetNapiStringValue(
178 napi_env env, napi_value napiValue, const std::string &name, const std::string &defValue = "");
179 std::string GetStringFromValue(napi_env env, napi_value value);
180 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
181 int32_t GetNapiInt32Value(napi_env env, napi_value napiValue, const std::string &name, const int32_t &defValue = 0);
182 std::string UnwrapStringFromJS(napi_env env, napi_value param);
183 void ConvertStringVectorToJS(napi_env env, napi_value &result, std::vector<std::string> &stringVector);
184 void JsStringToBytesVector(napi_env env, napi_value &src, std::vector<unsigned char> &values);
185 void BytesVectorToJS(napi_env env, napi_value &result, std::vector<unsigned char> &src);
186 void ConvertStringToNumberArray(napi_env env, napi_value &result, std::string srcValue);
187 void ConvertNdefRecordVectorToJS(napi_env env, napi_value &result,
188 std::vector<std::shared_ptr<NdefRecord>> &ndefRecords);
189 void ConvertNdefRecordToJS(napi_env env, napi_value &result, std::shared_ptr<NdefRecord> &ndefRecord);
190 bool MatchParameters(napi_env env, const napi_value parameters[], std::initializer_list<napi_valuetype> valueTypes);
191 napi_value HandleAsyncWork(napi_env env, BaseContext *baseContext, const std::string &workName,
192 napi_async_execute_callback execute, napi_async_complete_callback complete);
193 void DoAsyncCallbackOrPromise(const napi_env &env, BaseContext *baseContext, napi_value callbackValue);
194 void ThrowAsyncError(const napi_env &env, BaseContext *baseContext, int errCode, const std::string &errMsg);
195 bool IsNumberArray(const napi_env &env, const napi_value ¶m);
196 bool IsObjectArray(const napi_env &env, const napi_value ¶m);
197 bool IsArray(const napi_env &env, const napi_value ¶m);
198 bool IsNumber(const napi_env &env, const napi_value ¶m);
199 bool IsString(const napi_env &env, const napi_value ¶m);
200 bool IsObject(const napi_env &env, const napi_value ¶m);
201 bool IsFunction(const napi_env &env, const napi_value ¶m);
202 int BuildOutputErrorCode(int errCode);
203 int BuildOutputErrorCodeHce(int errCode);
204 std::string BuildErrorMessage(int errCode, std::string funcName, std::string forbiddenPerm,
205 std::string paramName, std::string expertedType);
206 napi_value GenerateBusinessError(const napi_env &env, int errCode, const std::string &errMessage);
207 bool CheckUnwrapStatusAndThrow(const napi_env &env, napi_status status, int errCode);
208 bool CheckContextAndThrow(const napi_env &env, const BaseContext *context, int errCode);
209 bool CheckParametersAndThrow(const napi_env &env, const napi_value parameters[],
210 std::initializer_list<napi_valuetype> types, const std::string &argName, const std::string &argType);
211 bool CheckArrayNumberAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
212 const std::string &argType);
213 bool CheckNumberAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
214 const std::string &argType);
215 bool CheckStringAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
216 const std::string &argType);
217 bool CheckObjectAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
218 const std::string &argType);
219 bool CheckFunctionAndThrow(const napi_env &env, const napi_value ¶m, const std::string &argName,
220 const std::string &argType);
221 bool CheckArgCountAndThrow(const napi_env &env, int argCount, int expCount);
222 bool CheckTagStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string &funcName);
223 bool CheckHceStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string &funcName);
224 } // namespace KITS
225 } // namespace NFC
226 } // namespace OHOS
227 #endif