• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "napi_esim.h"
17 
18 #include <memory>
19 #include <string>
20 #include <string_view>
21 #include "cancel_session_callback.h"
22 #include "delete_profile_callback.h"
23 #include "download_profile_callback.h"
24 #include "esim_state_type.h"
25 #include "esim_service_client.h"
26 #include "get_default_smdp_address_callback.h"
27 #include "get_downloadable_profile_metadata_callback.h"
28 #include "get_downloadable_profiles_callback.h"
29 #include "get_eid_callback.h"
30 #include "get_euicc_info_callback.h"
31 #include "get_euicc_profile_info_list_callback.h"
32 #include "napi_parameter_util.h"
33 #include "napi_util.h"
34 #include "network_state.h"
35 #include "reset_memory_callback.h"
36 #include "set_default_smdp_address_callback.h"
37 #include "set_profile_nick_name_callback.h"
38 #include "start_osu_callback.h"
39 #include "switch_to_profile.h"
40 #include "telephony_log_wrapper.h"
41 #include "telephony_permission.h"
42 
43 namespace OHOS {
44 namespace Telephony {
45 namespace {
46 const int32_t UNDEFINED_VALUE = -1;
47 const int32_t PARAMETER_COUNT_ONE = 1;
48 const int32_t PARAMETER_COUNT_TWO = 2;
49 struct AsyncPara {
50     std::string funcName = "";
51     napi_env env = nullptr;
52     napi_callback_info info = nullptr;
53     napi_async_execute_callback execute = nullptr;
54     napi_async_complete_callback complete = nullptr;
55 };
56 struct PermissionPara {
57     std::string func = "";
58     std::string permission = "";
59 };
60 size_t resetParameterCount = 0;
61 
IsValidSlotId(int32_t slotId)62 static inline bool IsValidSlotId(int32_t slotId)
63 {
64     return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT));
65 }
66 
67 template<typename T, napi_async_execute_callback exec, napi_async_complete_callback complete>
NapiCreateAsyncWork(napi_env env,napi_callback_info info,std::string_view funcName)68 napi_value NapiCreateAsyncWork(napi_env env, napi_callback_info info, std::string_view funcName)
69 {
70     size_t argc = PARAMETER_COUNT_TWO;
71     napi_value argv[]{nullptr, nullptr};
72     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
73 
74     std::unique_ptr<AsyncContext<T>> asyncContext = std::make_unique<AsyncContext<T>>();
75     BaseContext &context = asyncContext->context;
76     auto inParaTp = std::make_tuple(&asyncContext->slotId, &context.callbackRef);
77     std::optional<NapiError> errCode = MatchParameters(env, argv, argc, inParaTp);
78     if (errCode.has_value()) {
79         JsError error = NapiUtil::ConverEsimErrorMessageForJs(errCode.value());
80         NapiUtil::ThrowError(env, error.errorCode, error.errorMessage);
81         return nullptr;
82     }
83 
84     napi_value result = nullptr;
85     if (context.callbackRef == nullptr) {
86         NAPI_CALL(env, napi_create_promise(env, &context.deferred, &result));
87     } else {
88         NAPI_CALL(env, napi_get_undefined(env, &result));
89     }
90 
91     napi_value resourceName = nullptr;
92     NAPI_CALL(env, napi_create_string_utf8(env, funcName.data(), funcName.length(), &resourceName));
93     AsyncContext<T> *pContext = asyncContext.release();
94     NAPI_CALL(env, napi_create_async_work(
95         env, nullptr, resourceName, exec, complete, static_cast<void *>(pContext), &context.work));
96     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) != napi_ok) {
97         delete pContext;
98         result = nullptr;
99     }
100     return result;
101 }
102 
103 template<typename AsyncContextType, typename... Ts>
NapiCreateAsyncWork2(const AsyncPara & para,AsyncContextType * asyncContext,std::tuple<Ts...> & theTuple)104 napi_value NapiCreateAsyncWork2(const AsyncPara &para, AsyncContextType *asyncContext, std::tuple<Ts...> &theTuple)
105 {
106     if (asyncContext == nullptr) {
107         return nullptr;
108     }
109 
110     napi_env env = para.env;
111     BaseContext &context = asyncContext->asyncContext.context;
112 
113     size_t argc = sizeof...(Ts);
114     napi_value argv[sizeof...(Ts)]{nullptr};
115     NAPI_CALL(env, napi_get_cb_info(env, para.info, &argc, argv, nullptr, nullptr));
116 
117     std::optional<NapiError> errCode = MatchParameters(env, argv, argc, theTuple);
118     if (errCode.has_value()) {
119         JsError error = NapiUtil::ConverEsimErrorMessageForJs(errCode.value());
120         NapiUtil::ThrowError(env, error.errorCode, error.errorMessage);
121         return nullptr;
122     }
123 
124     napi_value result = nullptr;
125     if (context.callbackRef == nullptr) {
126         NAPI_CALL(env, napi_create_promise(env, &context.deferred, &result));
127     } else {
128         NAPI_CALL(env, napi_get_undefined(env, &result));
129     }
130 
131     napi_value resourceName = nullptr;
132     NAPI_CALL(env, napi_create_string_utf8(env, para.funcName.c_str(), para.funcName.length(), &resourceName));
133     NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, para.execute, para.complete,
134         static_cast<void *>(asyncContext), &context.work));
135     return result;
136 }
137 
138 template<typename T>
NapiAsyncBaseCompleteCallback(napi_env env,const AsyncContext<T> & asyncContext,JsError error,bool funcIgnoreReturnVal=false)139 void NapiAsyncBaseCompleteCallback(
140     napi_env env, const AsyncContext<T> &asyncContext, JsError error, bool funcIgnoreReturnVal = false)
141 {
142     const BaseContext &context = asyncContext.context;
143     if (context.deferred != nullptr && !context.resolved) {
144         napi_value errorMessage = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
145         NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, context.deferred, errorMessage));
146         NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, context.work));
147         return;
148     }
149 
150     if (context.deferred != nullptr && context.resolved) {
151         napi_value resValue =
152             (funcIgnoreReturnVal ? NapiUtil::CreateUndefined(env) : GetNapiValue(env, asyncContext.callbackVal));
153         NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, context.deferred, resValue));
154         NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, context.work));
155         return;
156     }
157 
158     napi_value res =
159         (funcIgnoreReturnVal ? NapiUtil::CreateUndefined(env) : GetNapiValue(env, asyncContext.callbackVal));
160     napi_value callbackValue[] { NapiUtil::CreateUndefined(env), res };
161     if (!context.resolved) {
162         callbackValue[0] = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
163         callbackValue[1] = NapiUtil::CreateUndefined(env);
164     }
165     napi_value undefined = nullptr;
166     napi_value callback = nullptr;
167     napi_value result = nullptr;
168     NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined));
169     NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, context.callbackRef, &callback));
170     NAPI_CALL_RETURN_VOID(
171         env, napi_call_function(env, undefined, callback, std::size(callbackValue), callbackValue, &result));
172     NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, context.callbackRef));
173     NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, context.work));
174 }
175 
176 template<typename T>
NapiAsyncPermissionCompleteCallback(napi_env env,napi_status status,const AsyncContext<T> & asyncContext,bool funcIgnoreReturnVal,PermissionPara permissionPara)177 void NapiAsyncPermissionCompleteCallback(napi_env env, napi_status status, const AsyncContext<T> &asyncContext,
178     bool funcIgnoreReturnVal, PermissionPara permissionPara)
179 {
180     if (status != napi_ok) {
181         napi_throw_type_error(env, nullptr, "excute failed");
182         return;
183     }
184 
185     JsError error = NapiUtil::ConverEsimErrorMessageWithPermissionForJs(
186         asyncContext.context.errorCode, permissionPara.func, permissionPara.permission);
187     NapiAsyncBaseCompleteCallback(env, asyncContext, error, funcIgnoreReturnVal);
188 }
189 
EuiccInfoConversion(napi_env env,const EuiccInfo & resultInfo)190 napi_value EuiccInfoConversion(napi_env env, const EuiccInfo &resultInfo)
191 {
192     napi_value val = nullptr;
193     napi_create_object(env, &val);
194     SetPropertyToNapiObject(env, val, "osVersion", NapiUtil::ToUtf8(resultInfo.osVersion_));
195 
196     return val;
197 }
198 
DownloadProfileResultConversion(napi_env env,const DownloadProfileResult & resultInfo)199 napi_value DownloadProfileResultConversion(napi_env env, const DownloadProfileResult &resultInfo)
200 {
201     napi_value val = nullptr;
202     napi_create_object(env, &val);
203     SetPropertyToNapiObject(env, val, "responseResult", static_cast<int32_t>(resultInfo.result_));
204     SetPropertyToNapiObject(env, val, "solvableErrors", static_cast<int32_t>(resultInfo.resolvableErrors_));
205     SetPropertyToNapiObject(env, val, "cardId", resultInfo.cardId_);
206 
207     return val;
208 }
209 
AccessRuleInfoConversion(napi_env env,const AccessRule & accessInfo)210 napi_value AccessRuleInfoConversion(napi_env env, const AccessRule &accessInfo)
211 {
212     napi_value val = nullptr;
213     napi_create_object(env, &val);
214     SetPropertyToNapiObject(env, val, "certificateHashHexStr", NapiUtil::ToUtf8(accessInfo.certificateHashHexStr_));
215     SetPropertyToNapiObject(env, val, "packageName", NapiUtil::ToUtf8(accessInfo.packageName_));
216     SetPropertyToNapiObject(env, val, "accessType", accessInfo.accessType_);
217 
218     return val;
219 }
220 
ProfileInfoConversion(napi_env env,const DownloadableProfile & profileInfo)221 napi_value ProfileInfoConversion(napi_env env, const DownloadableProfile &profileInfo)
222 {
223     napi_value val = nullptr;
224     napi_create_object(env, &val);
225     SetPropertyToNapiObject(env, val, "activationCode", NapiUtil::ToUtf8(profileInfo.encodedActivationCode_));
226     SetPropertyToNapiObject(env, val, "confirmationCode", NapiUtil::ToUtf8(profileInfo.confirmationCode_));
227     SetPropertyToNapiObject(env, val, "carrierName", NapiUtil::ToUtf8(profileInfo.carrierName_));
228     napi_value resultArray = nullptr;
229     napi_create_array(env, &resultArray);
230     for (size_t i = 0; i < profileInfo.accessRules_.size(); i++) {
231         napi_value res = AccessRuleInfoConversion(env, profileInfo.accessRules_.at(i));
232         napi_set_element(env, resultArray, i, res);
233     }
234     napi_set_named_property(env, val, "accessRules", resultArray);
235 
236     return val;
237 }
238 
ProfileResultListConversion(napi_env env,const GetDownloadableProfilesResult & resultListInfo)239 napi_value ProfileResultListConversion(napi_env env, const GetDownloadableProfilesResult &resultListInfo)
240 {
241     napi_value val = nullptr;
242     napi_create_object(env, &val);
243     SetPropertyToNapiObject(env, val, "responseResult", static_cast<int32_t>(resultListInfo.result_));
244     napi_value resultArray = nullptr;
245     napi_create_array(env, &resultArray);
246     for (size_t i = 0; i < resultListInfo.downloadableProfiles_.size(); i++) {
247         napi_value res = ProfileInfoConversion(env, resultListInfo.downloadableProfiles_.at(i));
248         napi_set_element(env, resultArray, i, res);
249     }
250     napi_set_named_property(env, val, "downloadableProfiles", resultArray);
251 
252     return val;
253 }
254 
MetadataResultConversion(napi_env env,const GetDownloadableProfileMetadataResult & metadataInfo)255 napi_value MetadataResultConversion(napi_env env, const GetDownloadableProfileMetadataResult &metadataInfo)
256 {
257     napi_value val = nullptr;
258     napi_create_object(env, &val);
259     napi_value res = ProfileInfoConversion(env, metadataInfo.downloadableProfiles_);
260     napi_set_named_property(env, val, "downloadableProfile", res);
261     SetPropertyToNapiObject(env, val, "pprType", metadataInfo.pprType_);
262     SetPropertyToNapiObject(env, val, "pprFlag", metadataInfo.pprFlag_);
263     SetPropertyToNapiObject(env, val, "iccid", NapiUtil::ToUtf8(metadataInfo.iccId_));
264     SetPropertyToNapiObject(env, val, "serviceProviderName", NapiUtil::ToUtf8(metadataInfo.serviceProviderName_));
265     SetPropertyToNapiObject(env, val, "profileName", NapiUtil::ToUtf8(metadataInfo.profileName_));
266     SetPropertyToNapiObject(env, val, "profileClass", static_cast<int32_t>(metadataInfo.profileClass_));
267     SetPropertyToNapiObject(env, val, "solvableErrors", static_cast<int32_t>(metadataInfo.resolvableErrors_));
268     SetPropertyToNapiObject(env, val, "responseResult", static_cast<int32_t>(metadataInfo.result_));
269 
270     return val;
271 }
272 
OperatorIdConversion(napi_env env,const OperatorId & operatorId)273 napi_value OperatorIdConversion(napi_env env, const OperatorId &operatorId)
274 {
275     napi_value val = nullptr;
276     napi_create_object(env, &val);
277     SetPropertyToNapiObject(env, val, "mcc", NapiUtil::ToUtf8(operatorId.mcc_));
278     SetPropertyToNapiObject(env, val, "mnc", NapiUtil::ToUtf8(operatorId.mnc_));
279     SetPropertyToNapiObject(env, val, "gid1", NapiUtil::ToUtf8(operatorId.gid1_));
280     SetPropertyToNapiObject(env, val, "gid2", NapiUtil::ToUtf8(operatorId.gid2_));
281 
282     return val;
283 }
284 
EuiccProfileInfoConversion(napi_env env,const EuiccProfile & euiccProfileInfo)285 napi_value EuiccProfileInfoConversion(napi_env env, const EuiccProfile &euiccProfileInfo)
286 {
287     napi_value val = nullptr;
288     napi_create_object(env, &val);
289     SetPropertyToNapiObject(env, val, "iccid", NapiUtil::ToUtf8(euiccProfileInfo.iccId_));
290     SetPropertyToNapiObject(env, val, "nickName", NapiUtil::ToUtf8(euiccProfileInfo.nickName_));
291     SetPropertyToNapiObject(env, val, "serviceProviderName", NapiUtil::ToUtf8(euiccProfileInfo.serviceProviderName_));
292     SetPropertyToNapiObject(env, val, "profileName", NapiUtil::ToUtf8(euiccProfileInfo.profileName_));
293     SetPropertyToNapiObject(env, val, "state", static_cast<int32_t>(euiccProfileInfo.state_));
294     SetPropertyToNapiObject(env, val, "profileClass", static_cast<int32_t>(euiccProfileInfo.profileClass_));
295     napi_value res = OperatorIdConversion(env, euiccProfileInfo.carrierId_);
296     napi_set_named_property(env, val, "operatorId", res);
297     SetPropertyToNapiObject(env, val, "policyRules", static_cast<int32_t>(euiccProfileInfo.policyRules_));
298     napi_value resultArray = nullptr;
299     napi_create_array(env, &resultArray);
300     for (size_t i = 0; i < euiccProfileInfo.accessRules_.size(); i++) {
301         napi_value res = AccessRuleInfoConversion(env, euiccProfileInfo.accessRules_.at(i));
302         napi_set_element(env, resultArray, i, res);
303     }
304     napi_set_named_property(env, val, "accessRules", resultArray);
305 
306     return val;
307 }
308 
EuiccProfileListConversion(napi_env env,const GetEuiccProfileInfoListResult & euiccListInfo)309 napi_value EuiccProfileListConversion(napi_env env, const GetEuiccProfileInfoListResult &euiccListInfo)
310 {
311     napi_value val = nullptr;
312     napi_create_object(env, &val);
313     SetPropertyToNapiObject(env, val, "responseResult", static_cast<int32_t>(euiccListInfo.result_));
314     SetPropertyToNapiObject(env, val, "isRemovable", euiccListInfo.isRemovable_);
315     napi_value resultArray = nullptr;
316     napi_create_array(env, &resultArray);
317     for (size_t i = 0; i < euiccListInfo.profiles_.size(); i++) {
318         napi_value res = EuiccProfileInfoConversion(env, euiccListInfo.profiles_.at(i));
319         napi_set_element(env, resultArray, i, res);
320     }
321     napi_set_named_property(env, val, "profiles", resultArray);
322 
323     return val;
324 }
325 
GetAccessRuleInfo(AsyncAccessRule & accessType)326 AccessRule GetAccessRuleInfo(AsyncAccessRule &accessType)
327 {
328     AccessRule access;
329     access.certificateHashHexStr_ = NapiUtil::ToUtf16(accessType.certificateHashHexStr.data());
330     access.packageName_ = NapiUtil::ToUtf16(accessType.packageName.data());
331     access.accessType_ = accessType.accessType;
332 
333     return access;
334 }
335 
GetProfileInfo(AsyncDownloadableProfile & profileInfo)336 DownloadableProfile GetProfileInfo(AsyncDownloadableProfile &profileInfo)
337 {
338     if (profileInfo.activationCode.length() == 0) {
339         TELEPHONY_LOGE("GetProfileInfo activationCode is null.");
340     }
341     if (profileInfo.confirmationCode.length() == 0) {
342         TELEPHONY_LOGE("GetProfileInfo confirmationCode is null.");
343     }
344     if (profileInfo.carrierName.length() == 0) {
345         TELEPHONY_LOGE("GetProfileInfo carrierName is null.");
346     }
347     if (profileInfo.accessRules.size() == 0) {
348         TELEPHONY_LOGE("GetProfileInfo accessRules is null.");
349     }
350     DownloadableProfile profile;
351     profile.encodedActivationCode_ = NapiUtil::ToUtf16(profileInfo.activationCode.data());
352     profile.confirmationCode_ = NapiUtil::ToUtf16(profileInfo.confirmationCode.data());
353     profile.carrierName_ = NapiUtil::ToUtf16(profileInfo.carrierName.data());
354 
355     for (size_t i = 0; i < profileInfo.accessRules.size(); i++) {
356         AccessRule access = GetAccessRuleInfo(profileInfo.accessRules.at(i));
357         profile.accessRules_.push_back(std::move(access));
358     }
359 
360     return profile;
361 }
362 
AccessRuleInfoAnalyze(napi_env env,napi_value arg,AsyncAccessRule & accessType)363 void AccessRuleInfoAnalyze(napi_env env, napi_value arg, AsyncAccessRule &accessType)
364 {
365     napi_value hashState = NapiUtil::GetNamedProperty(env, arg, "certificateHashHexStr");
366     if (hashState) {
367         std::array<char, ARRAY_SIZE> hashHexStr = {0};
368         NapiValueToCppValue(env, hashState, napi_string, std::data(hashHexStr));
369         accessType.certificateHashHexStr = std::string(hashHexStr.data());
370     }
371 
372     napi_value nameState = NapiUtil::GetNamedProperty(env, arg, "packageName");
373     if (nameState) {
374         std::array<char, ARRAY_SIZE> nameStr = {0};
375         NapiValueToCppValue(env, nameState, napi_string, std::data(nameStr));
376         accessType.packageName = std::string(nameStr.data());
377     }
378 
379     napi_value type = NapiUtil::GetNamedProperty(env, arg, "accessType");
380     if (type) {
381         NapiValueToCppValue(env, type, napi_number, &accessType.accessType);
382     }
383 }
384 
ProfileInfoAnalyze(napi_env env,napi_value arg,AsyncDownloadableProfile & profileInfo)385 void ProfileInfoAnalyze(napi_env env, napi_value arg, AsyncDownloadableProfile &profileInfo)
386 {
387     napi_value activateState = NapiUtil::GetNamedProperty(env, arg, "activationCode");
388     if (activateState) {
389         std::array<char, ARRAY_SIZE> activationStr = {0};
390         NapiValueToCppValue(env, activateState, napi_string, std::data(activationStr));
391         profileInfo.activationCode = std::string(activationStr.data());
392     }
393 
394     napi_value confirmState = NapiUtil::GetNamedProperty(env, arg, "confirmationCode");
395     if (confirmState) {
396         std::array<char, ARRAY_SIZE> confirmationStr = {0};
397         NapiValueToCppValue(env, confirmState, napi_string, std::data(confirmationStr));
398         profileInfo.confirmationCode = std::string(confirmationStr.data());
399     }
400 
401     napi_value nameState = NapiUtil::GetNamedProperty(env, arg, "carrierName");
402     if (nameState) {
403         std::array<char, ARRAY_SIZE> carrierStr = {0};
404         NapiValueToCppValue(env, nameState, napi_string, std::data(carrierStr));
405         profileInfo.carrierName = std::string(carrierStr.data());
406     }
407 
408     napi_value ruleState = NapiUtil::GetNamedProperty(env, arg, "accessRules");
409     if (ruleState) {
410         uint32_t array_length;
411         napi_get_array_length(env, ruleState, &array_length);
412         for (uint32_t i = 0; i < array_length; i++) {
413             napi_value name;
414             if (napi_get_element(env, ruleState, i, &name) != napi_ok) {
415                 TELEPHONY_LOGE("accessRules get element fail");
416             }
417             AsyncAccessRule accessRuleInfo;
418             AccessRuleInfoAnalyze(env, name, accessRuleInfo);
419             profileInfo.accessRules.push_back(std::move(accessRuleInfo));
420         }
421     }
422 }
423 
ConfigurationInfoAnalyze(napi_env env,napi_value arg,AsyncDownloadConfiguration & configuration)424 void ConfigurationInfoAnalyze(napi_env env, napi_value arg, AsyncDownloadConfiguration &configuration)
425 {
426     napi_value switchState = NapiUtil::GetNamedProperty(env, arg, "switchAfterDownload");
427     if (switchState) {
428         NapiValueToCppValue(env, switchState, napi_boolean, &configuration.switchAfterDownload);
429     }
430 
431     napi_value forceState = NapiUtil::GetNamedProperty(env, arg, "forceDisableProfile");
432     if (forceState) {
433         NapiValueToCppValue(env, forceState, napi_boolean, &configuration.forceDisableProfile);
434     }
435 
436     napi_value alowState = NapiUtil::GetNamedProperty(env, arg, "isPprAllowed");
437     if (alowState) {
438         NapiValueToCppValue(env, alowState, napi_boolean, &configuration.isPprAllowed);
439     }
440 }
441 
GetDefaultResetOption(void)442 ResetOption GetDefaultResetOption(void)
443 {
444     return ResetOption::DELETE_OPERATIONAL_PROFILES;
445 }
446 
NativeGetEid(napi_env env,void * data)447 void NativeGetEid(napi_env env, void *data)
448 {
449     if (data == nullptr) {
450         return;
451     }
452     auto euiccEidContext = static_cast<AsyncContext<std::string> *>(data);
453     if (!IsValidSlotId(euiccEidContext->slotId)) {
454         TELEPHONY_LOGE("NativeGetEid slotId is invalid");
455         euiccEidContext->context.errorCode = ERROR_SLOT_ID_INVALID;
456         return;
457     }
458     std::unique_ptr<GetEidResultCallback> callback = std::make_unique<GetEidResultCallback>(euiccEidContext);
459     std::unique_lock<std::mutex> callbackLock(euiccEidContext->callbackMutex);
460     int32_t errorCode =
461         DelayedRefSingleton<EsimServiceClient>::GetInstance().GetEid(euiccEidContext->slotId, callback.release());
462     TELEPHONY_LOGI("NAPI NativeGetEid %{public}d", errorCode);
463     euiccEidContext->context.errorCode = errorCode;
464     if (errorCode == TELEPHONY_SUCCESS) {
465         euiccEidContext->cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
466             [euiccEidContext] { return euiccEidContext->isCallbackEnd; });
467     }
468 }
469 
GetEidCallback(napi_env env,napi_status status,void * data)470 void GetEidCallback(napi_env env, napi_status status, void *data)
471 {
472     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
473     std::unique_ptr<AsyncContext<std::string>> context(static_cast<AsyncContext<std::string> *>(data));
474     if (context == nullptr) {
475         TELEPHONY_LOGE("GetEidCallback context is nullptr");
476         return;
477     }
478     if ((!context->isCallbackEnd) && (context->context.errorCode == TELEPHONY_SUCCESS)) {
479         TELEPHONY_LOGE("GetEidCallback get result timeout.");
480         context->context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
481     }
482     NapiAsyncPermissionCompleteCallback(
483         env, status, *context, false, { "GetEid", Permission::GET_TELEPHONY_ESIM_STATE });
484 }
485 
GetEid(napi_env env,napi_callback_info info)486 napi_value GetEid(napi_env env, napi_callback_info info)
487 {
488     return NapiCreateAsyncWork<std::string, NativeGetEid, GetEidCallback>(env, info, "GetEid");
489 }
490 
IsSupported(napi_env env,napi_callback_info info)491 napi_value IsSupported(napi_env env, napi_callback_info info)
492 {
493     size_t parameterCount = PARAMETER_COUNT_ONE;
494     napi_value parameters[] = { nullptr };
495     napi_get_cb_info(env, info, &parameterCount, parameters, nullptr, nullptr);
496     bool isSupported = false;
497     napi_value value = nullptr;
498     if (parameterCount != PARAMETER_COUNT_ONE ||
499         !NapiUtil::MatchParameters(env, parameters, { napi_number })) {
500         TELEPHONY_LOGE("isSupported parameter count is incorrect");
501         NapiUtil::ThrowParameterError(env);
502         return nullptr;
503     }
504     int32_t slotId = UNDEFINED_VALUE;
505     if (napi_get_value_int32(env, parameters[0], &slotId) != napi_ok) {
506         TELEPHONY_LOGE("isSupported convert parameter fail");
507         NAPI_CALL(env, napi_create_int32(env, isSupported, &value));
508         return value;
509     }
510 
511     if (!IsValidSlotId(slotId)) {
512         NapiUtil::ThrowParameterError(env);
513         return nullptr;
514     }
515     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().IsSupported(slotId);
516     if (errorCode != TELEPHONY_SUCCESS) {
517         JsError error = NapiUtil::ConverEsimErrorMessageForJs(errorCode);
518         NapiUtil::ThrowError(env, error.errorCode, error.errorMessage);
519         return nullptr;
520     } else {
521         isSupported = true;
522     }
523     NAPI_CALL(env, napi_get_boolean(env, isSupported, &value));
524     return value;
525 }
526 
NativeAddProfile(napi_env env,void * data)527 void NativeAddProfile(napi_env env, void *data)
528 {
529     if (data == nullptr) {
530         return;
531     }
532     AsyncAddProfileInfo *addProfileContext = static_cast<AsyncAddProfileInfo *>(data);
533     int32_t slotId = GetDefaultEsimSlotId<int32_t>();
534     DownloadableProfile profile = GetProfileInfo(addProfileContext->profile);
535     int32_t errcode = DelayedRefSingleton<EsimServiceClient>::GetInstance().AddProfile(slotId, profile);
536     TELEPHONY_LOGI("NAPI NativeAddProfile %{public}d", errcode);
537     addProfileContext->asyncContext.context.errorCode = errcode;
538     if (errcode == ERROR_NONE) {
539         addProfileContext->asyncContext.context.resolved = true;
540         addProfileContext->asyncContext.callbackVal = true;
541     } else {
542         addProfileContext->asyncContext.context.resolved = false;
543         addProfileContext->asyncContext.callbackVal = false;
544     }
545 }
546 
AddProfileCallback(napi_env env,napi_status status,void * data)547 void AddProfileCallback(napi_env env, napi_status status, void *data)
548 {
549     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
550     std::unique_ptr<AsyncAddProfileInfo> context(static_cast<AsyncAddProfileInfo *>(data));
551     if (context == nullptr) {
552         TELEPHONY_LOGE("AddProfileCallback context is nullptr");
553         return;
554     }
555     NapiAsyncPermissionCompleteCallback(
556         env, status, context->asyncContext, false, { "AddProfile", Permission::SET_TELEPHONY_ESIM_STATE_OPEN });
557 }
558 
AddProfile(napi_env env,napi_callback_info info)559 napi_value AddProfile(napi_env env, napi_callback_info info)
560 {
561     auto addProfile = std::make_unique<AsyncAddProfileInfo>();
562     if (addProfile == nullptr) {
563         return nullptr;
564     }
565     BaseContext &context = addProfile->asyncContext.context;
566     napi_value object = NapiUtil::CreateUndefined(env);
567     auto initPara = std::make_tuple(&object, &context.callbackRef);
568     AsyncPara para{
569         .funcName = "AddProfile",
570         .env = env,
571         .info = info,
572         .execute = NativeAddProfile,
573         .complete = AddProfileCallback,
574     };
575     napi_value result = NapiCreateAsyncWork2<AsyncAddProfileInfo>(para, addProfile.get(), initPara);
576     if (result == nullptr) {
577         TELEPHONY_LOGE("creat asyncwork failed!");
578         return nullptr;
579     }
580     ProfileInfoAnalyze(env, object, addProfile->profile);
581     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
582         addProfile.release();
583     }
584     return result;
585 }
586 
NativeGetEuiccInfo(napi_env env,void * data)587 void NativeGetEuiccInfo(napi_env env, void *data)
588 {
589     if (data == nullptr) {
590         return;
591     }
592     auto euiccContext = static_cast<AsyncEuiccInfo *>(data);
593     if (!IsValidSlotId(euiccContext->asyncContext.slotId)) {
594         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
595         euiccContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
596         return;
597     }
598     std::unique_ptr<GetEuiccInformationCallback> callback = std::make_unique<GetEuiccInformationCallback>(euiccContext);
599     std::unique_lock<std::mutex> callbackLock(euiccContext->asyncContext.callbackMutex);
600     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().GetEuiccInfo(
601         euiccContext->asyncContext.slotId, callback.release());
602 
603     TELEPHONY_LOGI("NAPI NativeGetEuiccInfo %{public}d", errorCode);
604     euiccContext->asyncContext.context.errorCode = errorCode;
605     if (errorCode == TELEPHONY_SUCCESS) {
606         euiccContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
607             [euiccContext] { return euiccContext->asyncContext.isCallbackEnd; });
608     }
609 }
610 
GetEuiccInfoCallback(napi_env env,napi_status status,void * data)611 void GetEuiccInfoCallback(napi_env env, napi_status status, void *data)
612 {
613     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
614     std::unique_ptr<AsyncEuiccInfo> context(static_cast<AsyncEuiccInfo *>(data));
615     if (context == nullptr) {
616         TELEPHONY_LOGE("GetEuiccInfoCallback context is nullptr");
617         return;
618     }
619     AsyncContext<napi_value> &asyncContext = context->asyncContext;
620     if (asyncContext.context.resolved) {
621         asyncContext.callbackVal = EuiccInfoConversion(env, context->result);
622     }
623     if ((!asyncContext.isCallbackEnd) && (asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
624         TELEPHONY_LOGE("GetEuiccInfoCallback get result timeout.");
625         asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
626     }
627     NapiAsyncPermissionCompleteCallback(
628         env, status, context->asyncContext, false, { "GetEuiccInfo", Permission::GET_TELEPHONY_ESIM_STATE });
629 }
630 
GetEuiccInfo(napi_env env,napi_callback_info info)631 napi_value GetEuiccInfo(napi_env env, napi_callback_info info)
632 {
633     auto euiccInfo = std::make_unique<AsyncEuiccInfo>();
634     if (euiccInfo == nullptr) {
635         return nullptr;
636     }
637     BaseContext &context = euiccInfo->asyncContext.context;
638 
639     auto initPara = std::make_tuple(&euiccInfo->asyncContext.slotId, &context.callbackRef);
640     AsyncPara para {
641         .funcName = "GetEuiccInfo",
642         .env = env,
643         .info = info,
644         .execute = NativeGetEuiccInfo,
645         .complete = GetEuiccInfoCallback,
646     };
647     napi_value result = NapiCreateAsyncWork2<AsyncEuiccInfo>(para, euiccInfo.get(), initPara);
648     if (result == nullptr) {
649         TELEPHONY_LOGE("creat asyncwork failed!");
650         return nullptr;
651     }
652     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
653         euiccInfo.release();
654     }
655     return result;
656 }
657 
NativeGetDefaultSmdpAddress(napi_env env,void * data)658 void NativeGetDefaultSmdpAddress(napi_env env, void *data)
659 {
660     if (data == nullptr) {
661         return;
662     }
663     auto contextInfo = static_cast<AsyncContext<std::string> *>(data);
664     if (!IsValidSlotId(contextInfo->slotId)) {
665         TELEPHONY_LOGE("NativeGetDefaultSmdpAddress slotId is invalid");
666         contextInfo->context.errorCode = ERROR_SLOT_ID_INVALID;
667         return;
668     }
669     std::unique_ptr<GetDefaultSmdpAddressResultCallback> callback =
670         std::make_unique<GetDefaultSmdpAddressResultCallback>(contextInfo);
671     std::unique_lock<std::mutex> callbackLock(contextInfo->callbackMutex);
672     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().GetDefaultSmdpAddress(
673         contextInfo->slotId, callback.release());
674 
675     TELEPHONY_LOGI("NAPI NativeGetDefaultSmdpAddress %{public}d", errorCode);
676     contextInfo->context.errorCode = errorCode;
677     if (errorCode == TELEPHONY_SUCCESS) {
678         contextInfo->cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
679             [contextInfo] { return contextInfo->isCallbackEnd; });
680     }
681 }
682 
GetDefaultSmdpAddressCallback(napi_env env,napi_status status,void * data)683 void GetDefaultSmdpAddressCallback(napi_env env, napi_status status, void *data)
684 {
685     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
686     std::unique_ptr<AsyncContext<std::string>> context(static_cast<AsyncContext<std::string> *>(data));
687     if (context == nullptr) {
688         TELEPHONY_LOGE("GetDefaultSmdpAddressCallback context is nullptr");
689         return;
690     }
691     if ((!context->isCallbackEnd) && (context->context.errorCode == TELEPHONY_SUCCESS)) {
692         TELEPHONY_LOGE("GetDefaultSmdpAddressCallback get result timeout.");
693         context->context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
694     }
695     NapiAsyncPermissionCompleteCallback(
696         env, status, *context, false, { "GetDefaultSmdpAddress", Permission::GET_TELEPHONY_ESIM_STATE });
697 }
698 
GetDefaultSmdpAddress(napi_env env,napi_callback_info info)699 napi_value GetDefaultSmdpAddress(napi_env env, napi_callback_info info)
700 {
701     return NapiCreateAsyncWork<std::string, NativeGetDefaultSmdpAddress, GetDefaultSmdpAddressCallback>(env,
702         info, "GetDefaultSmdpAddress");
703 }
704 
NativeSetDefaultSmdpAddress(napi_env env,void * data)705 void NativeSetDefaultSmdpAddress(napi_env env, void *data)
706 {
707     if (data == nullptr) {
708         return;
709     }
710     auto context = static_cast<AsyncContextInfo *>(data);
711     if (!IsValidSlotId(context->asyncContext.slotId)) {
712         TELEPHONY_LOGE("NativeSetDefaultSmdpAddress slotId is invalid");
713         context->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
714         return;
715     }
716     std::unique_ptr<SetDefaultSmdpAddressResultCallback> callback =
717         std::make_unique<SetDefaultSmdpAddressResultCallback>(context);
718     std::unique_lock<std::mutex> callbackLock(context->asyncContext.callbackMutex);
719     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().SetDefaultSmdpAddress(
720         context->asyncContext.slotId, context->inputStr, callback.release());
721 
722     TELEPHONY_LOGI("NAPI NativeSetDefaultSmdpAddress %{public}d", errorCode);
723     if (errorCode == TELEPHONY_SUCCESS) {
724         context->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
725             [context] { return context->asyncContext.isCallbackEnd; });
726     }
727     context->asyncContext.context.errorCode = errorCode;
728 }
729 
SetDefaultSmdpAddressCallback(napi_env env,napi_status status,void * data)730 void SetDefaultSmdpAddressCallback(napi_env env, napi_status status, void *data)
731 {
732     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
733     std::unique_ptr<AsyncContextInfo> context(static_cast<AsyncContextInfo *>(data));
734     if (context == nullptr) {
735         TELEPHONY_LOGE("SetDefaultSmdpAddressCallback context is nullptr");
736         return;
737     }
738     if ((!context->asyncContext.isCallbackEnd) && (context->asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
739         TELEPHONY_LOGE("SetDefaultSmdpAddressCallback get result timeout.");
740         context->asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
741     }
742     NapiAsyncPermissionCompleteCallback(
743         env, status, context->asyncContext, false, { "SetDefaultSmdpAddress", Permission::SET_TELEPHONY_ESIM_STATE });
744 }
745 
SetDefaultSmdpAddress(napi_env env,napi_callback_info info)746 napi_value SetDefaultSmdpAddress(napi_env env, napi_callback_info info)
747 {
748     auto asyncContext = std::make_unique<AsyncContextInfo>();
749     if (asyncContext == nullptr) {
750         return nullptr;
751     }
752     BaseContext &context = asyncContext->asyncContext.context;
753 
754     std::array<char, ARRAY_SIZE> inputTepStr = {0};
755     auto initPara = std::make_tuple(&asyncContext->asyncContext.slotId, std::data(inputTepStr), &context.callbackRef);
756     AsyncPara para {
757         .funcName = "SetDefaultSmdpAddress",
758         .env = env,
759         .info = info,
760         .execute = NativeSetDefaultSmdpAddress,
761         .complete = SetDefaultSmdpAddressCallback,
762     };
763     napi_value result = NapiCreateAsyncWork2<AsyncContextInfo>(para, asyncContext.get(), initPara);
764     if (result == nullptr) {
765         TELEPHONY_LOGE("creat asyncwork failed!");
766         return nullptr;
767     }
768     asyncContext->inputStr = std::string(inputTepStr.data());
769     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
770         asyncContext.release();
771     }
772     return result;
773 }
774 
NativeSwitchToProfile(napi_env env,void * data)775 void NativeSwitchToProfile(napi_env env, void *data)
776 {
777     if (data == nullptr) {
778         return;
779     }
780     auto profileContext = static_cast<AsyncSwitchProfileInfo *>(data);
781     if (!IsValidSlotId(profileContext->asyncContext.slotId)) {
782         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
783         profileContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
784         return;
785     }
786     std::unique_ptr<SwitchToProfileResultCallback> callback =
787         std::make_unique<SwitchToProfileResultCallback>(profileContext);
788     std::unique_lock<std::mutex> callbackLock(profileContext->asyncContext.callbackMutex);
789     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().SwitchToProfile(
790         profileContext->asyncContext.slotId, profileContext->portIndex, profileContext->iccid,
791         profileContext->forceDisableProfile, callback.release());
792 
793     TELEPHONY_LOGI("NAPI NativeSwitchToProfile %{public}d", errorCode);
794     profileContext->asyncContext.context.errorCode = errorCode;
795     if (errorCode == TELEPHONY_SUCCESS) {
796         profileContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
797             [profileContext] { return profileContext->asyncContext.isCallbackEnd; });
798     }
799 }
800 
SwitchToProfileCallback(napi_env env,napi_status status,void * data)801 void SwitchToProfileCallback(napi_env env, napi_status status, void *data)
802 {
803     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
804     std::unique_ptr<AsyncSwitchProfileInfo> context(static_cast<AsyncSwitchProfileInfo *>(data));
805     if (context == nullptr) {
806         TELEPHONY_LOGE("SwitchToProfileCallback context is nullptr");
807         return;
808     }
809     if ((!context->asyncContext.isCallbackEnd) && (context->asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
810         TELEPHONY_LOGE("SwitchToProfileCallback get result timeout.");
811         context->asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
812     }
813     NapiAsyncPermissionCompleteCallback(
814         env, status, context->asyncContext, false, { "SwitchToProfile", Permission::SET_TELEPHONY_ESIM_STATE });
815 }
816 
SwitchToProfile(napi_env env,napi_callback_info info)817 napi_value SwitchToProfile(napi_env env, napi_callback_info info)
818 {
819     auto profileContext = std::make_unique<AsyncSwitchProfileInfo>();
820     if (profileContext == nullptr) {
821         return nullptr;
822     }
823     BaseContext &context = profileContext->asyncContext.context;
824 
825     std::array<char, ARRAY_SIZE> iccIdStr = {0};
826     auto initPara = std::make_tuple(&profileContext->asyncContext.slotId, &profileContext->portIndex,
827         std::data(iccIdStr), &profileContext->forceDisableProfile, &context.callbackRef);
828 
829     AsyncPara para {
830         .funcName = "SwitchToProfile",
831         .env = env,
832         .info = info,
833         .execute = NativeSwitchToProfile,
834         .complete = SwitchToProfileCallback,
835     };
836     napi_value result = NapiCreateAsyncWork2<AsyncSwitchProfileInfo>(para, profileContext.get(), initPara);
837     if (result == nullptr) {
838         TELEPHONY_LOGE("creat asyncwork failed!");
839         return nullptr;
840     }
841     profileContext->iccid = std::string(iccIdStr.data());
842     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
843         profileContext.release();
844     }
845     return result;
846 }
847 
NativeDeleteProfile(napi_env env,void * data)848 void NativeDeleteProfile(napi_env env, void *data)
849 {
850     if (data == nullptr) {
851         return;
852     }
853     auto context = static_cast<AsyncContextInfo *>(data);
854     if (!IsValidSlotId(context->asyncContext.slotId)) {
855         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
856         context->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
857         return;
858     }
859     std::unique_ptr<DeleteProfileResultCallback> callback = std::make_unique<DeleteProfileResultCallback>(context);
860     std::unique_lock<std::mutex> callbackLock(context->asyncContext.callbackMutex);
861     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().DeleteProfile(
862         context->asyncContext.slotId, context->inputStr, callback.release());
863 
864     TELEPHONY_LOGI("NAPI NativeDeleteProfile %{public}d", errorCode);
865     context->asyncContext.context.errorCode = errorCode;
866     if (errorCode == TELEPHONY_SUCCESS) {
867         context->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
868             [context] { return context->asyncContext.isCallbackEnd; });
869     }
870 }
871 
DeleteProfileCallback(napi_env env,napi_status status,void * data)872 void DeleteProfileCallback(napi_env env, napi_status status, void *data)
873 {
874     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
875     std::unique_ptr<AsyncContextInfo> context(static_cast<AsyncContextInfo *>(data));
876     if (context == nullptr) {
877         TELEPHONY_LOGE("DeleteProfileCallback context is nullptr");
878         return;
879     }
880     if ((!context->asyncContext.isCallbackEnd) && (context->asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
881         TELEPHONY_LOGE("DeleteProfileCallback get result timeout.");
882         context->asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
883     }
884     NapiAsyncPermissionCompleteCallback(
885         env, status, context->asyncContext, false, { "DeleteProfile", Permission::SET_TELEPHONY_ESIM_STATE });
886 }
887 
DeleteProfile(napi_env env,napi_callback_info info)888 napi_value DeleteProfile(napi_env env, napi_callback_info info)
889 {
890     auto asyncContext = std::make_unique<AsyncContextInfo>();
891     if (asyncContext == nullptr) {
892         return nullptr;
893     }
894     BaseContext &context = asyncContext->asyncContext.context;
895 
896     std::array<char, ARRAY_SIZE> inputTmpStr = {0};
897     auto initPara = std::make_tuple(&asyncContext->asyncContext.slotId, std::data(inputTmpStr), &context.callbackRef);
898     AsyncPara para {
899         .funcName = "DeleteProfile",
900         .env = env,
901         .info = info,
902         .execute = NativeDeleteProfile,
903         .complete = DeleteProfileCallback,
904     };
905     napi_value result = NapiCreateAsyncWork2<AsyncContextInfo>(para, asyncContext.get(), initPara);
906     if (result == nullptr) {
907         TELEPHONY_LOGE("creat asyncwork failed!");
908         return nullptr;
909     }
910     asyncContext->inputStr = std::string(inputTmpStr.data());
911     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
912         asyncContext.release();
913     }
914     return result;
915 }
916 
NativeResetMemory(napi_env env,void * data)917 void NativeResetMemory(napi_env env, void *data)
918 {
919     if (data == nullptr) {
920         return;
921     }
922     auto profileContext = static_cast<AsyncResetMemory *>(data);
923     if (!IsValidSlotId(profileContext->asyncContext.slotId)) {
924         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
925         profileContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
926         return;
927     }
928     std::unique_ptr<ResetMemoryResultCallback> callback = std::make_unique<ResetMemoryResultCallback>(profileContext);
929     if (resetParameterCount == PARAMETER_COUNT_ONE) {
930         profileContext->option = static_cast<int32_t>(GetDefaultResetOption());
931     }
932     std::unique_lock<std::mutex> callbackLock(profileContext->asyncContext.callbackMutex);
933     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().ResetMemory(
934         profileContext->asyncContext.slotId, profileContext->option, callback.release());
935 
936     TELEPHONY_LOGI("NAPI NativeResetMemory %{public}d", errorCode);
937     profileContext->asyncContext.context.errorCode = errorCode;
938     if (errorCode == TELEPHONY_SUCCESS) {
939         profileContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
940             [profileContext] { return profileContext->asyncContext.isCallbackEnd; });
941     }
942 }
943 
ResetMemoryCallback(napi_env env,napi_status status,void * data)944 void ResetMemoryCallback(napi_env env, napi_status status, void *data)
945 {
946     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
947     std::unique_ptr<AsyncResetMemory> context(static_cast<AsyncResetMemory *>(data));
948     if (context == nullptr) {
949         TELEPHONY_LOGE("ResetMemoryCallback context is nullptr");
950         return;
951     }
952     if ((!context->asyncContext.isCallbackEnd) && (context->asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
953         TELEPHONY_LOGE("ResetMemoryCallback get result timeout.");
954         context->asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
955     }
956     NapiAsyncPermissionCompleteCallback(
957         env, status, context->asyncContext, false, { "ResetMemory", Permission::SET_TELEPHONY_ESIM_STATE });
958 }
959 
ResetMemory(napi_env env,napi_callback_info info)960 napi_value ResetMemory(napi_env env, napi_callback_info info)
961 {
962     resetParameterCount = 0;
963     napi_value parameters[PARAMETER_COUNT_TWO] = { 0 };
964     napi_value thisVar = nullptr;
965     void *data = nullptr;
966     napi_get_cb_info(env, info, &resetParameterCount, parameters, &thisVar, &data);
967     if (resetParameterCount == PARAMETER_COUNT_ONE) {
968         return NapiCreateAsyncWork<int32_t, NativeResetMemory, ResetMemoryCallback>(env, info, "ResetMemory");
969     }
970 
971     auto profileContext = std::make_unique<AsyncResetMemory>();
972     if (profileContext == nullptr) {
973         return nullptr;
974     }
975     BaseContext &context = profileContext->asyncContext.context;
976     auto initPara = std::make_tuple(&profileContext->asyncContext.slotId,
977         &profileContext->option, &context.callbackRef);
978 
979     AsyncPara para {
980         .funcName = "ResetMemory",
981         .env = env,
982         .info = info,
983         .execute = NativeResetMemory,
984         .complete = ResetMemoryCallback,
985     };
986     napi_value result = NapiCreateAsyncWork2<AsyncResetMemory>(para, profileContext.get(), initPara);
987     if (result == nullptr) {
988         TELEPHONY_LOGE("creat asyncwork failed!");
989         return nullptr;
990     }
991     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
992         profileContext.release();
993     }
994     return result;
995 }
996 
NativeDownloadProfile(napi_env env,void * data)997 void NativeDownloadProfile(napi_env env, void *data)
998 {
999     if (data == nullptr) {
1000         return;
1001     }
1002     auto profileContext = static_cast<AsyncDownloadProfileInfo *>(data);
1003     if (!IsValidSlotId(profileContext->asyncContext.slotId)) {
1004         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
1005         profileContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1006         return;
1007     }
1008     std::unique_ptr<DownloadProfileResultCallback> callback =
1009         std::make_unique<DownloadProfileResultCallback>(profileContext);
1010     DownloadProfileResult result;
1011     DownloadProfileConfigInfo configInfo;
1012     configInfo.portIndex_ = profileContext->portIndex;
1013     configInfo.isSwitchAfterDownload_ = profileContext->configuration.switchAfterDownload;
1014     configInfo.isForceDeactivateSim_ = profileContext->configuration.forceDisableProfile;
1015     configInfo.isPprAllowed_ = profileContext->configuration.isPprAllowed;
1016     DownloadableProfile profile = GetProfileInfo(profileContext->profile);
1017     std::unique_lock<std::mutex> callbackLock(profileContext->asyncContext.callbackMutex);
1018     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().DownloadProfile(
1019         profileContext->asyncContext.slotId, configInfo, profile, callback.release());
1020 
1021     TELEPHONY_LOGI("NAPI NativeDownloadProfile %{public}d", errorCode);
1022     profileContext->asyncContext.context.errorCode = errorCode;
1023     if (errorCode == TELEPHONY_SUCCESS) {
1024         profileContext->asyncContext.cv.wait_for(
1025             callbackLock,
1026             std::chrono::seconds(WAIT_LONG_TERM_TASK_SECOND),
1027             [profileContext] { return profileContext->asyncContext.isCallbackEnd; });
1028     }
1029 }
1030 
DownloadProfileCallback(napi_env env,napi_status status,void * data)1031 void DownloadProfileCallback(napi_env env, napi_status status, void *data)
1032 {
1033     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1034     std::unique_ptr<AsyncDownloadProfileInfo> context(static_cast<AsyncDownloadProfileInfo *>(data));
1035     if (context == nullptr) {
1036         TELEPHONY_LOGE("DownloadProfileCallback context is nullptr");
1037         return;
1038     }
1039     AsyncContext<napi_value> &asyncContext = context->asyncContext;
1040     if (asyncContext.context.resolved) {
1041         asyncContext.callbackVal =  DownloadProfileResultConversion(env, context->result);
1042     }
1043     if ((!asyncContext.isCallbackEnd) && (asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
1044         TELEPHONY_LOGE("DownloadProfileCallback get result timeout.");
1045         asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1046     }
1047     NapiAsyncPermissionCompleteCallback(
1048         env, status, context->asyncContext, false, { "DownloadProfile", Permission::SET_TELEPHONY_ESIM_STATE });
1049 }
1050 
DownloadProfile(napi_env env,napi_callback_info info)1051 napi_value DownloadProfile(napi_env env, napi_callback_info info)
1052 {
1053     auto profileContext = std::make_unique<AsyncDownloadProfileInfo>();
1054     if (profileContext == nullptr) {
1055         return nullptr;
1056     }
1057     BaseContext &context = profileContext->asyncContext.context;
1058     napi_value profileObject = NapiUtil::CreateUndefined(env);
1059     napi_value configurationObject = NapiUtil::CreateUndefined(env);
1060     auto initPara = std::make_tuple(&profileContext->asyncContext.slotId, &profileContext->portIndex, &profileObject,
1061         &configurationObject, &context.callbackRef);
1062 
1063     AsyncPara para {
1064         .funcName = "DownloadProfile",
1065         .env = env,
1066         .info = info,
1067         .execute = NativeDownloadProfile,
1068         .complete = DownloadProfileCallback,
1069     };
1070     napi_value result = NapiCreateAsyncWork2<AsyncDownloadProfileInfo>(para, profileContext.get(), initPara);
1071     if (result == nullptr) {
1072         TELEPHONY_LOGE("creat asyncwork failed!");
1073         return nullptr;
1074     }
1075     ProfileInfoAnalyze(env, profileObject, profileContext->profile);
1076     ConfigurationInfoAnalyze(env, configurationObject, profileContext->configuration);
1077     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1078         profileContext.release();
1079     }
1080     return result;
1081 }
1082 
NativeGetDownloadableProfiles(napi_env env,void * data)1083 void NativeGetDownloadableProfiles(napi_env env, void *data)
1084 {
1085     if (data == nullptr) {
1086         return;
1087     }
1088     auto profileContext = static_cast<AsyncDefaultProfileList *>(data);
1089     if (!IsValidSlotId(profileContext->asyncContext.slotId)) {
1090         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
1091         profileContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1092         return;
1093     }
1094     std::unique_ptr<GetDownloadableProfilesResultCallback> callback =
1095         std::make_unique<GetDownloadableProfilesResultCallback>(profileContext);
1096     std::unique_lock<std::mutex> callbackLock(profileContext->asyncContext.callbackMutex);
1097     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().GetDownloadableProfiles(
1098         profileContext->asyncContext.slotId, profileContext->portIndex,
1099         profileContext->forceDisableProfile, callback.release());
1100 
1101     TELEPHONY_LOGI("NAPI NativeGetDownloadableProfiles %{public}d", errorCode);
1102     profileContext->asyncContext.context.errorCode = errorCode;
1103     if (errorCode == TELEPHONY_SUCCESS) {
1104         profileContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
1105             [profileContext] { return profileContext->asyncContext.isCallbackEnd; });
1106     }
1107 }
1108 
GetDownloadableProfilesCallback(napi_env env,napi_status status,void * data)1109 void GetDownloadableProfilesCallback(napi_env env, napi_status status, void *data)
1110 {
1111     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1112     std::unique_ptr<AsyncDefaultProfileList> context(static_cast<AsyncDefaultProfileList *>(data));
1113     if (context == nullptr) {
1114         TELEPHONY_LOGE("GetDownloadableProfilesCallback context is nullptr");
1115         return;
1116     }
1117     AsyncContext<napi_value> &asyncContext = context->asyncContext;
1118     if (asyncContext.context.resolved) {
1119         asyncContext.callbackVal = ProfileResultListConversion(env, context->result);
1120     }
1121     if ((!asyncContext.isCallbackEnd) && (asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
1122         TELEPHONY_LOGE("GetDownloadableProfilesCallback get result timeout.");
1123         asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1124     }
1125     NapiAsyncPermissionCompleteCallback(env, status, context->asyncContext, false,
1126         { "GetDownloadableProfiles", Permission::GET_TELEPHONY_ESIM_STATE });
1127 }
1128 
GetDownloadableProfiles(napi_env env,napi_callback_info info)1129 napi_value GetDownloadableProfiles(napi_env env, napi_callback_info info)
1130 {
1131     auto profileContext = std::make_unique<AsyncDefaultProfileList>();
1132     if (profileContext == nullptr) {
1133         return nullptr;
1134     }
1135     BaseContext &context = profileContext->asyncContext.context;
1136 
1137     auto initPara = std::make_tuple(&profileContext->asyncContext.slotId, &profileContext->portIndex,
1138         &profileContext->forceDisableProfile, &context.callbackRef);
1139 
1140     AsyncPara para {
1141         .funcName = "GetDownloadableProfiles",
1142         .env = env,
1143         .info = info,
1144         .execute = NativeGetDownloadableProfiles,
1145         .complete = GetDownloadableProfilesCallback,
1146     };
1147     napi_value result = NapiCreateAsyncWork2<AsyncDefaultProfileList>(para, profileContext.get(), initPara);
1148     if (result == nullptr) {
1149         TELEPHONY_LOGE("creat asyncwork failed!");
1150         return nullptr;
1151     }
1152     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1153         profileContext.release();
1154     }
1155     return result;
1156 }
1157 
NativeGetOsuStatus(napi_env env,void * data)1158 void NativeGetOsuStatus(napi_env env, void *data)
1159 {
1160     if (data == nullptr) {
1161         return;
1162     }
1163     AsyncContext<int32_t> *asyncContext = static_cast<AsyncContext<int32_t> *>(data);
1164     if (!IsValidSlotId(asyncContext->slotId)) {
1165         TELEPHONY_LOGE("NativeGetOsuStatus slotId is invalid");
1166         asyncContext->context.errorCode = ERROR_SLOT_ID_INVALID;
1167         return;
1168     }
1169     int32_t result = UNDEFINED_VALUE;
1170     int32_t errorCode =
1171         DelayedRefSingleton<EsimServiceClient>::GetInstance().GetOsuStatus(asyncContext->slotId, result);
1172     TELEPHONY_LOGI("NAPI NativeGetOsuStatus %{public}d", errorCode);
1173     if (errorCode == ERROR_NONE) {
1174         asyncContext->callbackVal = result;
1175         asyncContext->context.resolved = true;
1176     } else {
1177         asyncContext->context.resolved = false;
1178     }
1179     asyncContext->context.errorCode = errorCode;
1180 }
1181 
GetOsuStatusCallback(napi_env env,napi_status status,void * data)1182 void GetOsuStatusCallback(napi_env env, napi_status status, void *data)
1183 {
1184     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1185     std::unique_ptr<AsyncContext<int32_t>> context(static_cast<AsyncContext<int32_t> *>(data));
1186     if (context == nullptr) {
1187         TELEPHONY_LOGE("GetOsuStatusCallback context is nullptr");
1188         return;
1189     }
1190     NapiAsyncPermissionCompleteCallback(
1191         env, status, *context, false, { "GetOsuStatus", Permission::GET_TELEPHONY_ESIM_STATE });
1192 }
1193 
GetOsuStatus(napi_env env,napi_callback_info info)1194 napi_value GetOsuStatus(napi_env env, napi_callback_info info)
1195 {
1196     return NapiCreateAsyncWork<int32_t, NativeGetOsuStatus, GetOsuStatusCallback>(env, info, "GetOsuStatus");
1197 }
1198 
NativeStartOsu(napi_env env,void * data)1199 void NativeStartOsu(napi_env env, void *data)
1200 {
1201     if (data == nullptr) {
1202         return;
1203     }
1204 
1205     auto profileContext = static_cast<AsyncContext<int32_t> *>(data);
1206     if (!IsValidSlotId(profileContext->slotId)) {
1207         TELEPHONY_LOGE("NativeGetEuiccInfo slotId is invalid");
1208         profileContext->context.errorCode = ERROR_SLOT_ID_INVALID;
1209         return;
1210     }
1211 
1212     std::unique_ptr<StartOsuResultCallback> callback = std::make_unique<StartOsuResultCallback>(profileContext);
1213     std::unique_lock<std::mutex> callbackLock(profileContext->callbackMutex);
1214     int32_t errorCode =
1215         DelayedRefSingleton<EsimServiceClient>::GetInstance().StartOsu(profileContext->slotId, callback.release());
1216 
1217     TELEPHONY_LOGI("NAPI NativeStartOsu %{public}d", errorCode);
1218     profileContext->context.errorCode = errorCode;
1219     if (errorCode == TELEPHONY_SUCCESS) {
1220         profileContext->cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
1221             [profileContext] { return profileContext->isCallbackEnd; });
1222     }
1223 }
1224 
StartOsuCallback(napi_env env,napi_status status,void * data)1225 void StartOsuCallback(napi_env env, napi_status status, void *data)
1226 {
1227     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1228     std::unique_ptr<AsyncContext<int32_t>> context(static_cast<AsyncContext<int32_t> *>(data));
1229     if (context == nullptr) {
1230         TELEPHONY_LOGE("StartOsuCallback context is nullptr");
1231         return;
1232     }
1233     if ((!context->isCallbackEnd) && (context->context.errorCode == TELEPHONY_SUCCESS)) {
1234         TELEPHONY_LOGE("StartOsuCallback get result timeout.");
1235         context->context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1236     }
1237     NapiAsyncPermissionCompleteCallback(
1238         env, status, *context, false, { "StartOsu", Permission::SET_TELEPHONY_ESIM_STATE });
1239 }
1240 
StartOsu(napi_env env,napi_callback_info info)1241 napi_value StartOsu(napi_env env, napi_callback_info info)
1242 {
1243     return NapiCreateAsyncWork<int32_t, NativeStartOsu, StartOsuCallback>(env, info, "StartOsu");
1244 }
1245 
NativeSetProfileNickname(napi_env env,void * data)1246 void NativeSetProfileNickname(napi_env env, void *data)
1247 {
1248     if (data == nullptr) {
1249         return;
1250     }
1251     AsyncProfileNickname *setprofileNickNameContext = static_cast<AsyncProfileNickname *>(data);
1252     if (!IsValidSlotId(setprofileNickNameContext->asyncContext.slotId)) {
1253         TELEPHONY_LOGE("NativeSetProfileNickname slotId is invalid");
1254         setprofileNickNameContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1255         return;
1256     }
1257 
1258     std::unique_ptr<SetProfileNickNameResultCallback> callback =
1259         std::make_unique<SetProfileNickNameResultCallback>(setprofileNickNameContext);
1260     std::unique_lock<std::mutex> callbackLock(setprofileNickNameContext->asyncContext.callbackMutex);
1261     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().SetProfileNickname(
1262         setprofileNickNameContext->asyncContext.slotId, setprofileNickNameContext->iccid,
1263         setprofileNickNameContext->nickname, callback.release());
1264 
1265     TELEPHONY_LOGI("NAPI NativeSetProfileNickname %{public}d", errorCode);
1266     setprofileNickNameContext->asyncContext.context.errorCode = errorCode;
1267     if (errorCode == TELEPHONY_SUCCESS) {
1268         setprofileNickNameContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
1269             [setprofileNickNameContext] { return setprofileNickNameContext->asyncContext.isCallbackEnd; });
1270     }
1271 }
1272 
SetProfileNicknameCallback(napi_env env,napi_status status,void * data)1273 void SetProfileNicknameCallback(napi_env env, napi_status status, void *data)
1274 {
1275     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1276     std::unique_ptr<AsyncProfileNickname> context(static_cast<AsyncProfileNickname *>(data));
1277     if (context == nullptr) {
1278         TELEPHONY_LOGE("SetProfileNicknameCallback context is nullptr");
1279         return;
1280     }
1281     if ((!context->asyncContext.isCallbackEnd) && (context->asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
1282         TELEPHONY_LOGE("SetProfileNicknameCallback get result timeout.");
1283         context->asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1284     }
1285     NapiAsyncPermissionCompleteCallback(
1286         env, status, context->asyncContext, false, { "SetProfileNickname", Permission::SET_TELEPHONY_ESIM_STATE });
1287 }
1288 
SetProfileNickname(napi_env env,napi_callback_info info)1289 napi_value SetProfileNickname(napi_env env, napi_callback_info info)
1290 {
1291     auto profileContext = std::make_unique<AsyncProfileNickname>();
1292     if (profileContext == nullptr) {
1293         return nullptr;
1294     }
1295     BaseContext &context = profileContext->asyncContext.context;
1296 
1297     std::array<char, ARRAY_SIZE> iccIdStr = {0};
1298     std::array<char, ARRAY_SIZE> nicknameStr = {0};
1299     auto initPara = std::make_tuple(&profileContext->asyncContext.slotId,
1300         std::data(iccIdStr), std::data(nicknameStr), &context.callbackRef);
1301 
1302     AsyncPara para {
1303         .funcName = "SetProfileNickname",
1304         .env = env,
1305         .info = info,
1306         .execute = NativeSetProfileNickname,
1307         .complete = SetProfileNicknameCallback,
1308     };
1309     napi_value result = NapiCreateAsyncWork2<AsyncProfileNickname>(para, profileContext.get(), initPara);
1310     if (result == nullptr) {
1311         TELEPHONY_LOGE("creat asyncwork failed!");
1312         return nullptr;
1313     }
1314     profileContext->iccid = std::string(iccIdStr.data());
1315     profileContext->nickname = std::string(nicknameStr.data());
1316     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1317         profileContext.release();
1318     }
1319     return result;
1320 }
1321 
NativeCancelSession(napi_env env,void * data)1322 void NativeCancelSession(napi_env env, void *data)
1323 {
1324     if (data == nullptr) {
1325         return;
1326     }
1327     auto cancelSessionContext = static_cast<AsyncCancelSession *>(data);
1328     if (!IsValidSlotId(cancelSessionContext->asyncContext.slotId)) {
1329         TELEPHONY_LOGE("NativeCancelSession slotId is invalid");
1330         cancelSessionContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1331         return;
1332     }
1333     std::unique_ptr<CancelSessionCallback> callback = std::make_unique<CancelSessionCallback>(cancelSessionContext);
1334     std::unique_lock<std::mutex> callbackLock(cancelSessionContext->asyncContext.callbackMutex);
1335     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().CancelSession(
1336         cancelSessionContext->asyncContext.slotId, cancelSessionContext->transactionId,
1337         cancelSessionContext->cancelReason, callback.release());
1338     TELEPHONY_LOGI("NAPI NativeCancelSession %{public}d", errorCode);
1339     cancelSessionContext->asyncContext.context.errorCode = errorCode;
1340     if (errorCode == TELEPHONY_SUCCESS) {
1341         cancelSessionContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
1342             [cancelSessionContext] { return cancelSessionContext->asyncContext.isCallbackEnd; });
1343     }
1344 }
1345 
CancelSessionCallback(napi_env env,napi_status status,void * data)1346 void CancelSessionCallback(napi_env env, napi_status status, void *data)
1347 {
1348     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1349     std::unique_ptr<AsyncCancelSession> context(static_cast<AsyncCancelSession *>(data));
1350     if (context == nullptr) {
1351         TELEPHONY_LOGE("CancelSessionCallback context is nullptr");
1352         return;
1353     }
1354     if ((!context->asyncContext.isCallbackEnd) && (context->asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
1355         TELEPHONY_LOGE("CancelSessionCallback get result timeout.");
1356         context->asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1357     }
1358     NapiAsyncPermissionCompleteCallback(
1359         env, status, context->asyncContext, false, { "CancelSession", Permission::SET_TELEPHONY_ESIM_STATE });
1360 }
1361 
CancelSession(napi_env env,napi_callback_info info)1362 napi_value CancelSession(napi_env env, napi_callback_info info)
1363 {
1364     auto sessionContext = std::make_unique<AsyncCancelSession>();
1365     if (sessionContext == nullptr) {
1366         return nullptr;
1367     }
1368     BaseContext &context = sessionContext->asyncContext.context;
1369 
1370     std::array<char, ARRAY_SIZE> transactionIdStr = {0};
1371     auto initPara = std::make_tuple(&sessionContext->asyncContext.slotId, std::data(transactionIdStr),
1372         &sessionContext->cancelReason, &context.callbackRef);
1373 
1374     AsyncPara para {
1375         .funcName = "CancelSession",
1376         .env = env,
1377         .info = info,
1378         .execute = NativeCancelSession,
1379         .complete = CancelSessionCallback,
1380     };
1381     napi_value result = NapiCreateAsyncWork2<AsyncCancelSession>(para, sessionContext.get(), initPara);
1382     if (result == nullptr) {
1383         TELEPHONY_LOGE("creat asyncwork failed!");
1384         return nullptr;
1385     }
1386     sessionContext->transactionId = std::string(transactionIdStr.data());
1387     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1388         sessionContext.release();
1389     }
1390     return result;
1391 }
1392 
NativeGetDownloadableProfileMetadata(napi_env env,void * data)1393 void NativeGetDownloadableProfileMetadata(napi_env env, void *data)
1394 {
1395     if (data == nullptr) {
1396         return;
1397     }
1398     auto metadata = static_cast<AsyncProfileMetadataInfo *>(data);
1399     if (!IsValidSlotId(metadata->asyncContext.slotId)) {
1400         TELEPHONY_LOGE("NativeGetDownloadableProfileMetadata slotId is invalid");
1401         metadata->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1402         return;
1403     }
1404     std::unique_ptr<GetDownloadableProfileMetadataResultCallback> callback =
1405         std::make_unique<GetDownloadableProfileMetadataResultCallback>(metadata);
1406     DownloadableProfile profile = GetProfileInfo(metadata->profile);
1407     std::unique_lock<std::mutex> callbackLock(metadata->asyncContext.callbackMutex);
1408     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().GetDownloadableProfileMetadata(
1409         metadata->asyncContext.slotId, metadata->portIndex, profile, metadata->forceDisableProfile, callback.release());
1410     TELEPHONY_LOGI("NAPI NativeGetDownloadableProfileMetadata %{public}d", errorCode);
1411     metadata->asyncContext.context.errorCode = errorCode;
1412     if (errorCode == TELEPHONY_SUCCESS) {
1413         metadata->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_LONG_TERM_TASK_SECOND),
1414             [metadata] { return metadata->asyncContext.isCallbackEnd; });
1415     }
1416 }
1417 
GetDownloadableProfileMetadataCallback(napi_env env,napi_status status,void * data)1418 void GetDownloadableProfileMetadataCallback(napi_env env, napi_status status, void *data)
1419 {
1420     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1421     std::unique_ptr<AsyncProfileMetadataInfo> context(static_cast<AsyncProfileMetadataInfo *>(data));
1422     if (context == nullptr) {
1423         TELEPHONY_LOGE("GetDownloadableProfileMetadataCallback context is nullptr");
1424         return;
1425     }
1426     AsyncContext<napi_value> &asyncContext = context->asyncContext;
1427     if (asyncContext.context.resolved) {
1428         asyncContext.callbackVal = MetadataResultConversion(env, context->result);
1429     }
1430     if ((!asyncContext.isCallbackEnd) && (asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
1431         TELEPHONY_LOGE("GetDownloadableProfileMetadataCallback get result timeout.");
1432         asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1433     }
1434     NapiAsyncPermissionCompleteCallback(env, status, context->asyncContext, false,
1435         { "GetDownloadableProfileMetadata", Permission::SET_TELEPHONY_ESIM_STATE });
1436 }
1437 
GetDownloadableProfileMetadata(napi_env env,napi_callback_info info)1438 napi_value GetDownloadableProfileMetadata(napi_env env, napi_callback_info info)
1439 {
1440     auto metadata = std::make_unique<AsyncProfileMetadataInfo>();
1441     if (metadata == nullptr) {
1442         return nullptr;
1443     }
1444     BaseContext &context = metadata->asyncContext.context;
1445     napi_value object = NapiUtil::CreateUndefined(env);
1446     auto initPara = std::make_tuple(&metadata->asyncContext.slotId, &metadata->portIndex,
1447         &object, &metadata->forceDisableProfile, &context.callbackRef);
1448 
1449     AsyncPara para {
1450         .funcName = "GetDownloadableProfileMetadata",
1451         .env = env,
1452         .info = info,
1453         .execute = NativeGetDownloadableProfileMetadata,
1454         .complete = GetDownloadableProfileMetadataCallback,
1455     };
1456     napi_value result = NapiCreateAsyncWork2<AsyncProfileMetadataInfo>(para, metadata.get(), initPara);
1457     if (result == nullptr) {
1458         TELEPHONY_LOGE("creat asyncwork failed!");
1459         return nullptr;
1460     }
1461     ProfileInfoAnalyze(env, object, metadata->profile);
1462     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1463         metadata.release();
1464     }
1465     return result;
1466 }
1467 
NativeGetEuiccProfileInfoList(napi_env env,void * data)1468 void NativeGetEuiccProfileInfoList(napi_env env, void *data)
1469 {
1470     if (data == nullptr) {
1471         return;
1472     }
1473     auto profileContext = static_cast<AsyncEuiccProfileInfoList *>(data);
1474     if (!IsValidSlotId(profileContext->asyncContext.slotId)) {
1475         TELEPHONY_LOGE("NativeGetEuiccProfileInfoList slotId is invalid");
1476         profileContext->asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1477         return;
1478     }
1479     std::unique_ptr<GetEuiccProfileInfoListResultCallback> callback =
1480         std::make_unique<GetEuiccProfileInfoListResultCallback>(profileContext);
1481     std::unique_lock<std::mutex> callbackLock(profileContext->asyncContext.callbackMutex);
1482     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().GetEuiccProfileInfoList(
1483         profileContext->asyncContext.slotId, callback.release());
1484     TELEPHONY_LOGI("NAPI NativeGetEuiccProfileInfoList %{public}d", errorCode);
1485     profileContext->asyncContext.context.errorCode = errorCode;
1486     if (errorCode == TELEPHONY_SUCCESS) {
1487         profileContext->asyncContext.cv.wait_for(callbackLock, std::chrono::seconds(WAIT_TIME_SECOND),
1488             [profileContext] { return profileContext->asyncContext.isCallbackEnd; });
1489     }
1490 }
1491 
GetEuiccProfileInfoListCallback(napi_env env,napi_status status,void * data)1492 void GetEuiccProfileInfoListCallback(napi_env env, napi_status status, void *data)
1493 {
1494     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1495     std::unique_ptr<AsyncEuiccProfileInfoList> context(static_cast<AsyncEuiccProfileInfoList *>(data));
1496     if (context == nullptr) {
1497         TELEPHONY_LOGE("GetEuiccProfileInfoListCallback context is nullptr");
1498         return;
1499     }
1500     AsyncContext<napi_value> &asyncContext = context->asyncContext;
1501     if (asyncContext.context.resolved) {
1502         asyncContext.callbackVal = EuiccProfileListConversion(env, context->result);
1503     }
1504     if ((!asyncContext.isCallbackEnd) && (asyncContext.context.errorCode == TELEPHONY_SUCCESS)) {
1505         TELEPHONY_LOGE("GetEuiccProfileInfoListCallback get result timeout.");
1506         asyncContext.context.errorCode = TELEPHONY_ERR_ESIM_GET_RESULT_TIMEOUT;
1507     }
1508     NapiAsyncPermissionCompleteCallback(
1509         env, status, asyncContext, false, { "GetEuiccProfileInfoList", Permission::GET_TELEPHONY_ESIM_STATE });
1510 }
1511 
GetEuiccProfileInfoList(napi_env env,napi_callback_info info)1512 napi_value GetEuiccProfileInfoList(napi_env env, napi_callback_info info)
1513 {
1514     auto euiccInfo = std::make_unique<AsyncEuiccProfileInfoList>();
1515     if (euiccInfo == nullptr) {
1516         return nullptr;
1517     }
1518     BaseContext &context = euiccInfo->asyncContext.context;
1519 
1520     auto initPara = std::make_tuple(&euiccInfo->asyncContext.slotId, &context.callbackRef);
1521     AsyncPara para {
1522         .funcName = "GetEuiccProfileInfoList",
1523         .env = env,
1524         .info = info,
1525         .execute = NativeGetEuiccProfileInfoList,
1526         .complete = GetEuiccProfileInfoListCallback,
1527     };
1528     napi_value result = NapiCreateAsyncWork2<AsyncEuiccProfileInfoList>(para, euiccInfo.get(), initPara);
1529     if (result == nullptr) {
1530         TELEPHONY_LOGE("creat asyncwork failed!");
1531         return nullptr;
1532     }
1533     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1534         euiccInfo.release();
1535     }
1536     return result;
1537 }
1538 
NativeReserveProfilesForFactoryRestore(napi_env env,void * data)1539 void NativeReserveProfilesForFactoryRestore(napi_env env, void *data)
1540 {
1541     if (data == nullptr) {
1542         return;
1543     }
1544 
1545     AsyncCommonInfo *profileContext = static_cast<AsyncCommonInfo *>(data);
1546     AsyncContext<int32_t> &asyncContext = profileContext->asyncContext;
1547     if (!IsValidSlotId(asyncContext.slotId)) {
1548         TELEPHONY_LOGE("NativeReserveProfilesForFactoryRestore slotId is invalid");
1549         asyncContext.context.errorCode = ERROR_SLOT_ID_INVALID;
1550         return;
1551     }
1552 
1553     int32_t result = UNDEFINED_VALUE;
1554     int32_t errorCode = DelayedRefSingleton<EsimServiceClient>::GetInstance().ReserveProfilesForFactoryRestore(
1555         asyncContext.slotId, result);
1556     TELEPHONY_LOGI("NAPI NativeReserveProfilesForFactoryRestore %{public}d", errorCode);
1557     if (errorCode == ERROR_NONE) {
1558         asyncContext.callbackVal = result;
1559         asyncContext.context.resolved = true;
1560     } else {
1561         asyncContext.context.resolved = false;
1562     }
1563     asyncContext.context.errorCode = errorCode;
1564 }
1565 
ReserveProfilesForFactoryRestoreCallback(napi_env env,napi_status status,void * data)1566 void ReserveProfilesForFactoryRestoreCallback(napi_env env, napi_status status, void *data)
1567 {
1568     NAPI_CALL_RETURN_VOID(env, (data == nullptr ? napi_invalid_arg : napi_ok));
1569     std::unique_ptr<AsyncCommonInfo> context(static_cast<AsyncCommonInfo *>(data));
1570     if (context == nullptr) {
1571         TELEPHONY_LOGE("ReserveProfilesForFactoryRestoreCallback context is nullptr");
1572         return;
1573     }
1574     NapiAsyncPermissionCompleteCallback(env, status, context->asyncContext, false,
1575         { "ReserveProfilesForFactoryRestore", Permission::SET_TELEPHONY_ESIM_STATE});
1576 }
1577 
ReserveProfilesForFactoryRestore(napi_env env,napi_callback_info info)1578 napi_value ReserveProfilesForFactoryRestore(napi_env env, napi_callback_info info)
1579 {
1580     auto asyncContext = std::make_unique<AsyncCommonInfo>();
1581     if (asyncContext == nullptr) {
1582         return nullptr;
1583     }
1584     BaseContext &context = asyncContext->asyncContext.context;
1585 
1586     auto initPara = std::make_tuple(&asyncContext->asyncContext.slotId, &context.callbackRef);
1587     AsyncPara para {
1588         .funcName = "ReserveProfilesForFactoryRestore",
1589         .env = env,
1590         .info = info,
1591         .execute = NativeReserveProfilesForFactoryRestore,
1592         .complete = ReserveProfilesForFactoryRestoreCallback,
1593     };
1594     napi_value result = NapiCreateAsyncWork2<AsyncCommonInfo>(para, asyncContext.get(), initPara);
1595     if (result == nullptr) {
1596         TELEPHONY_LOGE("creat asyncwork failed!");
1597         return nullptr;
1598     }
1599     if (napi_queue_async_work_with_qos(env, context.work, napi_qos_default) == napi_ok) {
1600         asyncContext.release();
1601     }
1602     return result;
1603 }
1604 
InitEnumResetOption(napi_env env,napi_value exports)1605 napi_status InitEnumResetOption(napi_env env, napi_value exports)
1606 {
1607     napi_property_descriptor desc[] = {
1608         DECLARE_NAPI_STATIC_PROPERTY("DELETE_OPERATIONAL_PROFILES",
1609             GetNapiValue(env, static_cast<int32_t>(ResetOption::DELETE_OPERATIONAL_PROFILES))),
1610         DECLARE_NAPI_STATIC_PROPERTY("DELETE_FIELD_LOADED_TEST_PROFILES",
1611             GetNapiValue(env, static_cast<int32_t>(ResetOption::DELETE_FIELD_LOADED_TEST_PROFILES))),
1612         DECLARE_NAPI_STATIC_PROPERTY("RESET_DEFAULT_SMDP_ADDRESS",
1613             GetNapiValue(env, static_cast<int32_t>(ResetOption::RESET_DEFAULT_SMDP_ADDRESS))),
1614     };
1615 
1616     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1617     NapiUtil::DefineEnumClassByName(env, exports, "ResetOption", arrSize, desc);
1618     return napi_define_properties(env, exports, arrSize, desc);
1619 }
1620 
InitEnumCancelReason(napi_env env,napi_value exports)1621 napi_status InitEnumCancelReason(napi_env env, napi_value exports)
1622 {
1623     napi_property_descriptor desc[] = {
1624         DECLARE_NAPI_STATIC_PROPERTY("CANCEL_REASON_END_USER_REJECTION",
1625             GetNapiValue(env, static_cast<int32_t>(CancelReason::CANCEL_REASON_END_USER_REJECTION))),
1626         DECLARE_NAPI_STATIC_PROPERTY("CANCEL_REASON_POSTPONED",
1627             GetNapiValue(env, static_cast<int32_t>(CancelReason::CANCEL_REASON_POSTPONED))),
1628         DECLARE_NAPI_STATIC_PROPERTY("CANCEL_REASON_TIMEOUT",
1629             GetNapiValue(env, static_cast<int32_t>(CancelReason::CANCEL_REASON_TIMEOUT))),
1630         DECLARE_NAPI_STATIC_PROPERTY("CANCEL_REASON_PPR_NOT_ALLOWED",
1631             GetNapiValue(env, static_cast<int32_t>(CancelReason::CANCEL_REASON_PPR_NOT_ALLOWED))),
1632     };
1633 
1634     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1635     NapiUtil::DefineEnumClassByName(env, exports, "CancelReason", arrSize, desc);
1636     return napi_define_properties(env, exports, arrSize, desc);
1637 }
1638 
InitEnumOsuStatus(napi_env env,napi_value exports)1639 napi_status InitEnumOsuStatus(napi_env env, napi_value exports)
1640 {
1641     napi_property_descriptor desc[] = {
1642         DECLARE_NAPI_STATIC_PROPERTY("EUICC_UPGRADE_IN_PROGRESS",
1643             GetNapiValue(env, static_cast<int32_t>(OsuStatus::EUICC_UPGRADE_IN_PROGRESS))),
1644         DECLARE_NAPI_STATIC_PROPERTY("EUICC_UPGRADE_FAILED",
1645             GetNapiValue(env, static_cast<int32_t>(OsuStatus::EUICC_UPGRADE_FAILED))),
1646         DECLARE_NAPI_STATIC_PROPERTY("EUICC_UPGRADE_SUCCESSFUL",
1647             GetNapiValue(env, static_cast<int32_t>(OsuStatus::EUICC_UPGRADE_SUCCESSFUL))),
1648         DECLARE_NAPI_STATIC_PROPERTY("EUICC_UPGRADE_ALREADY_LATEST",
1649             GetNapiValue(env, static_cast<int32_t>(OsuStatus::EUICC_UPGRADE_ALREADY_LATEST))),
1650         DECLARE_NAPI_STATIC_PROPERTY("EUICC_UPGRADE_SERVICE_UNAVAILABLE",
1651             GetNapiValue(env, static_cast<int32_t>(OsuStatus::EUICC_UPGRADE_SERVICE_UNAVAILABLE))),
1652     };
1653 
1654     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1655     NapiUtil::DefineEnumClassByName(env, exports, "OsuStatus", arrSize, desc);
1656     return napi_define_properties(env, exports, arrSize, desc);
1657 }
1658 
InitEnumProfileState(napi_env env,napi_value exports)1659 napi_status InitEnumProfileState(napi_env env, napi_value exports)
1660 {
1661     napi_property_descriptor desc[] = {
1662         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_STATE_UNSPECIFIED",
1663             GetNapiValue(env, static_cast<int32_t>(ProfileState::PROFILE_STATE_UNSPECIFIED))),
1664         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_STATE_DISABLED",
1665             GetNapiValue(env, static_cast<int32_t>(ProfileState::PROFILE_STATE_DISABLED))),
1666         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_STATE_ENABLED",
1667             GetNapiValue(env, static_cast<int32_t>(ProfileState::PROFILE_STATE_ENABLED))),
1668     };
1669 
1670     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1671     NapiUtil::DefineEnumClassByName(env, exports, "ProfileState", arrSize, desc);
1672     return napi_define_properties(env, exports, arrSize, desc);
1673 }
1674 
InitEnumProfileClass(napi_env env,napi_value exports)1675 napi_status InitEnumProfileClass(napi_env env, napi_value exports)
1676 {
1677     napi_property_descriptor desc[] = {
1678         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_CLASS_UNSPECIFIED",
1679             GetNapiValue(env, static_cast<int32_t>(ProfileClass::PROFILE_CLASS_UNSPECIFIED))),
1680         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_CLASS_TEST",
1681             GetNapiValue(env, static_cast<int32_t>(ProfileClass::PROFILE_CLASS_TEST))),
1682         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_CLASS_PROVISIONING",
1683             GetNapiValue(env, static_cast<int32_t>(ProfileClass::PROFILE_CLASS_PROVISIONING))),
1684         DECLARE_NAPI_STATIC_PROPERTY("PROFILE_CLASS_OPERATIONAL",
1685             GetNapiValue(env, static_cast<int32_t>(ProfileClass::PROFILE_CLASS_OPERATIONAL))),
1686     };
1687 
1688     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1689     NapiUtil::DefineEnumClassByName(env, exports, "ProfileClass", arrSize, desc);
1690     return napi_define_properties(env, exports, arrSize, desc);
1691 }
1692 
InitEnumPolicyRules(napi_env env,napi_value exports)1693 napi_status InitEnumPolicyRules(napi_env env, napi_value exports)
1694 {
1695     napi_property_descriptor desc[] = {
1696         DECLARE_NAPI_STATIC_PROPERTY("POLICY_RULE_DISABLE_NOT_ALLOWED",
1697             GetNapiValue(env, static_cast<int32_t>(PolicyRules::POLICY_RULE_DISABLE_NOT_ALLOWED))),
1698         DECLARE_NAPI_STATIC_PROPERTY("POLICY_RULE_DELETE_NOT_ALLOWED",
1699             GetNapiValue(env, static_cast<int32_t>(PolicyRules::POLICY_RULE_DELETE_NOT_ALLOWED))),
1700         DECLARE_NAPI_STATIC_PROPERTY("POLICY_RULE_DISABLE_AND_DELETE",
1701             GetNapiValue(env, static_cast<int32_t>(PolicyRules::POLICY_RULE_DISABLE_AND_DELETE))),
1702     };
1703 
1704     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1705     NapiUtil::DefineEnumClassByName(env, exports, "PolicyRules", arrSize, desc);
1706     return napi_define_properties(env, exports, arrSize, desc);
1707 }
1708 
InitEnumResultCode(napi_env env,napi_value exports)1709 napi_status InitEnumResultCode(napi_env env, napi_value exports)
1710 {
1711     napi_property_descriptor desc[] = {
1712         DECLARE_NAPI_STATIC_PROPERTY(
1713             "RESULT_SOLVABLE_ERRORS", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_SOLVABLE_ERRORS))),
1714         DECLARE_NAPI_STATIC_PROPERTY(
1715             "RESULT_MUST_DISABLE_PROFILE",
1716             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_MUST_DISABLE_PROFILE))),
1717         DECLARE_NAPI_STATIC_PROPERTY("RESULT_OK", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_OK))),
1718         DECLARE_NAPI_STATIC_PROPERTY(
1719             "RESULT_GET_EID_FAILED", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_GET_EID_FAILED))),
1720         DECLARE_NAPI_STATIC_PROPERTY(
1721             "RESULT_ACTIVATION_CODE_CHANGED",
1722             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_ACTIVATION_CODE_CHANGED))),
1723         DECLARE_NAPI_STATIC_PROPERTY(
1724             "RESULT_ACTIVATION_CODE_INVALID",
1725             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_ACTIVATION_CODE_INVALID))),
1726         DECLARE_NAPI_STATIC_PROPERTY(
1727             "RESULT_SMDP_ADDRESS_INVALID",
1728             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_SMDP_ADDRESS_INVALID))),
1729         DECLARE_NAPI_STATIC_PROPERTY(
1730             "RESULT_EUICC_INFO_INVALID",
1731             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_EUICC_INFO_INVALID))),
1732         DECLARE_NAPI_STATIC_PROPERTY(
1733             "RESULT_TLS_HANDSHAKE_FAILED",
1734             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_TLS_HANDSHAKE_FAILED))),
1735         DECLARE_NAPI_STATIC_PROPERTY(
1736             "RESULT_CERTIFICATE_IO_ERROR",
1737             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_CERTIFICATE_IO_ERROR))),
1738         DECLARE_NAPI_STATIC_PROPERTY(
1739             "RESULT_CERTIFICATE_RESPONSE_TIMEOUT",
1740             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_CERTIFICATE_RESPONSE_TIMEOUT))),
1741         DECLARE_NAPI_STATIC_PROPERTY(
1742             "RESULT_AUTHENTICATION_FAILED",
1743             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_AUTHENTICATION_FAILED))),
1744         DECLARE_NAPI_STATIC_PROPERTY(
1745             "RESULT_RESPONSE_HTTP_FAILED",
1746             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_RESPONSE_HTTP_FAILED))),
1747         DECLARE_NAPI_STATIC_PROPERTY(
1748             "RESULT_CONFIRMATION_CODE_INCORRECT",
1749             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_CONFIRMATION_CODE_INCORRECT))),
1750         DECLARE_NAPI_STATIC_PROPERTY(
1751             "RESULT_EXCEEDED_CONFIRMATION_CODE_TRY_LIMIT",
1752             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_EXCEEDED_CONFIRMATION_CODE_TRY_LIMIT))),
1753         DECLARE_NAPI_STATIC_PROPERTY(
1754             "RESULT_NO_PROFILE_ON_SERVER",
1755             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_NO_PROFILE_ON_SERVER))),
1756         DECLARE_NAPI_STATIC_PROPERTY(
1757             "RESULT_TRANSACTION_ID_INVALID",
1758             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_TRANSACTION_ID_INVALID))),
1759         DECLARE_NAPI_STATIC_PROPERTY(
1760             "RESULT_SERVER_ADDRESS_INVALID",
1761             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_SERVER_ADDRESS_INVALID))),
1762         DECLARE_NAPI_STATIC_PROPERTY(
1763             "RESULT_GET_BOUND_PROFILE_PACKAGE_FAILED",
1764             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_GET_BOUND_PROFILE_PACKAGE_FAILED))),
1765         DECLARE_NAPI_STATIC_PROPERTY(
1766             "RESULT_USER_CANCEL_DOWNLOAD",
1767             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_USER_CANCEL_DOWNLOAD))),
1768         DECLARE_NAPI_STATIC_PROPERTY(
1769             "RESULT_SERVER_UNAVAILABLE",
1770             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_SERVER_UNAVAILABLE))),
1771         DECLARE_NAPI_STATIC_PROPERTY(
1772             "RESULT_PROFILE_NON_DELETE",
1773             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_PROFILE_NON_DELETE))),
1774         DECLARE_NAPI_STATIC_PROPERTY(
1775             "RESULT_SMDP_ADDRESS_INCORRECT",
1776             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_SMDP_ADDRESS_INCORRECT))),
1777         DECLARE_NAPI_STATIC_PROPERTY(
1778             "RESULT_ANALYZE_AUTHENTICATION_SERVER_RESPONSE_FAILED",
1779             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_ANALYZE_AUTHENTICATION_SERVER_RESPONSE_FAILED))),
1780         DECLARE_NAPI_STATIC_PROPERTY(
1781             "RESULT_ANALYZE_AUTHENTICATION_CLIENT_RESPONSE_FAILED",
1782             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_ANALYZE_AUTHENTICATION_CLIENT_RESPONSE_FAILED))),
1783         DECLARE_NAPI_STATIC_PROPERTY(
1784             "RESULT_ANALYZE_AUTHENTICATION_CLIENT_MATCHING_ID_REFUSED",
1785             GetNapiValue(
1786                 env, static_cast<int32_t>(ResultCode::RESULT_ANALYZE_AUTHENTICATION_CLIENT_MATCHING_ID_REFUSED))),
1787         DECLARE_NAPI_STATIC_PROPERTY(
1788             "RESULT_PROFILE_TYPE_ERROR_AUTHENTICATION_STOPPED",
1789             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_PROFILE_TYPE_ERROR_AUTHENTICATION_STOPPED))),
1790         DECLARE_NAPI_STATIC_PROPERTY(
1791             "RESULT_CARRIER_SERVER_REFUSED_ERRORS",
1792             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_CARRIER_SERVER_REFUSED_ERRORS))),
1793         DECLARE_NAPI_STATIC_PROPERTY(
1794             "RESULT_CERTIFICATE_INVALID",
1795             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_CERTIFICATE_INVALID))),
1796         DECLARE_NAPI_STATIC_PROPERTY(
1797             "RESULT_OUT_OF_MEMORY",
1798             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_OUT_OF_MEMORY))),
1799         DECLARE_NAPI_STATIC_PROPERTY(
1800             "RESULT_PPR_FORBIDDEN", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_PPR_FORBIDDEN))),
1801         DECLARE_NAPI_STATIC_PROPERTY(
1802             "RESULT_NOTHING_TO_DELETE", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_NOTHING_TO_DELETE))),
1803         DECLARE_NAPI_STATIC_PROPERTY(
1804             "RESULT_PPR_NOT_MATCH", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_PPR_NOT_MATCH))),
1805         DECLARE_NAPI_STATIC_PROPERTY(
1806             "RESULT_CAT_BUSY", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_CAT_BUSY))),
1807         DECLARE_NAPI_STATIC_PROPERTY(
1808             "RESULT_PROFILE_EID_INVALID",
1809             GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_PROFILE_EID_INVALID))),
1810         DECLARE_NAPI_STATIC_PROPERTY(
1811             "RESULT_DOWNLOAD_TIMEOUT", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_DOWNLOAD_TIMEOUT))),
1812         DECLARE_NAPI_STATIC_PROPERTY(
1813             "RESULT_SGP_22_OTHER", GetNapiValue(env, static_cast<int32_t>(ResultCode::RESULT_SGP_22_OTHER))),
1814     };
1815 
1816     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1817     NapiUtil::DefineEnumClassByName(env, exports, "ResultCode", arrSize, desc);
1818     return napi_define_properties(env, exports, arrSize, desc);
1819 }
1820 
InitEnumResolvableErrors(napi_env env,napi_value exports)1821 napi_status InitEnumResolvableErrors(napi_env env, napi_value exports)
1822 {
1823     napi_property_descriptor desc[] = {
1824         DECLARE_NAPI_STATIC_PROPERTY("SOLVABLE_ERROR_NEED_CONFIRMATION_CODE",
1825             GetNapiValue(env, static_cast<int32_t>(SolvableErrors::SOLVABLE_ERROR_NEED_CONFIRMATION_CODE))),
1826         DECLARE_NAPI_STATIC_PROPERTY("SOLVABLE_ERROR_NEED_POLICY_RULE",
1827             GetNapiValue(env, static_cast<int32_t>(SolvableErrors::SOLVABLE_ERROR_NEED_POLICY_RULE))),
1828     };
1829 
1830     constexpr size_t arrSize = sizeof(desc) / sizeof(desc[0]);
1831     NapiUtil::DefineEnumClassByName(env, exports, "SolvableErrors", arrSize, desc);
1832     return napi_define_properties(env, exports, arrSize, desc);
1833 }
1834 
InitEuiccServiceInterface(napi_env env,napi_value exports)1835 napi_status InitEuiccServiceInterface(napi_env env, napi_value exports)
1836 {
1837     napi_property_descriptor desc[] = {
1838         DECLARE_NAPI_FUNCTION("isSupported", IsSupported),
1839         DECLARE_NAPI_FUNCTION("addProfile", AddProfile),
1840         DECLARE_NAPI_FUNCTION("getEid", GetEid),
1841         DECLARE_NAPI_FUNCTION("getOsuStatus", GetOsuStatus),
1842         DECLARE_NAPI_FUNCTION("startOsu", StartOsu),
1843         DECLARE_NAPI_FUNCTION("getDownloadableProfileMetadata", GetDownloadableProfileMetadata),
1844         DECLARE_NAPI_FUNCTION("getDownloadableProfiles", GetDownloadableProfiles),
1845         DECLARE_NAPI_FUNCTION("downloadProfile", DownloadProfile),
1846         DECLARE_NAPI_FUNCTION("getEuiccProfileInfoList", GetEuiccProfileInfoList),
1847         DECLARE_NAPI_FUNCTION("getEuiccInfo", GetEuiccInfo),
1848         DECLARE_NAPI_FUNCTION("deleteProfile", DeleteProfile),
1849         DECLARE_NAPI_FUNCTION("switchToProfile", SwitchToProfile),
1850         DECLARE_NAPI_FUNCTION("setProfileNickname", SetProfileNickname),
1851         DECLARE_NAPI_FUNCTION("resetMemory", ResetMemory),
1852         DECLARE_NAPI_FUNCTION("reserveProfilesForFactoryRestore", ReserveProfilesForFactoryRestore),
1853         DECLARE_NAPI_FUNCTION("setDefaultSmdpAddress", SetDefaultSmdpAddress),
1854         DECLARE_NAPI_FUNCTION("getDefaultSmdpAddress", GetDefaultSmdpAddress),
1855         DECLARE_NAPI_FUNCTION("cancelSession", CancelSession),
1856     };
1857     return napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1858 }
1859 } // namespace
1860 
1861 EXTERN_C_START
InitNapiEsim(napi_env env,napi_value exports)1862 napi_value InitNapiEsim(napi_env env, napi_value exports)
1863 {
1864     NAPI_CALL(env, InitEuiccServiceInterface(env, exports));
1865     NAPI_CALL(env, InitEnumResetOption(env, exports));
1866     NAPI_CALL(env, InitEnumCancelReason(env, exports));
1867     NAPI_CALL(env, InitEnumOsuStatus(env, exports));
1868     NAPI_CALL(env, InitEnumProfileState(env, exports));
1869     NAPI_CALL(env, InitEnumProfileClass(env, exports));
1870     NAPI_CALL(env, InitEnumPolicyRules(env, exports));
1871     NAPI_CALL(env, InitEnumResultCode(env, exports));
1872     NAPI_CALL(env, InitEnumResolvableErrors(env, exports));
1873     return exports;
1874 }
1875 EXTERN_C_END
1876 
1877 static napi_module _esimModule = {
1878     .nm_version = 1,
1879     .nm_flags = 0,
1880     .nm_filename = nullptr,
1881     .nm_register_func = InitNapiEsim,
1882     .nm_modname = "telephony.esim",
1883     .nm_priv = ((void *)0),
1884     .reserved = {0},
1885 };
1886 
RegisterEsimCardModule(void)1887 extern "C" __attribute__((constructor)) void RegisterEsimCardModule(void)
1888 {
1889     napi_module_register(&_esimModule);
1890 }
1891 } // namespace Telephony
1892 } // namespace OHOS