1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #include "nfc_napi_cardEmulation_adapter.h"
17 #include "cardEmulation.h"
18 #include "loghelper.h"
19 #include "hce_service.h"
20 #include "ability_info.h"
21 #include "nfc_napi_common_utils.h"
22 #include "element_name.h"
23 
24 
25 namespace OHOS {
26 namespace NFC {
27 namespace KITS {
28 using AppExecFwk::AbilityInfo;
29 using AppExecFwk::ElementName;
IsSupported(napi_env env,napi_callback_info cbinfo)30 napi_value IsSupported(napi_env env, napi_callback_info cbinfo)
31 {
32     bool isSupported = false;
33     size_t argc = ARGV_NUM_1;
34     napi_value argv[ARGV_NUM_1] = {0};
35     napi_value thisVar = 0;
36     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr);
37     int32_t type;
38     if (!CheckArgCountAndThrow(env, argc, ARGV_NUM_1) || !ParseInt32(env, type, argv[ARGV_INDEX_0])) {
39         ErrorLog("IsSupported: parse args failed");
40         return CreateUndefined(env);
41     }
42     isSupported = type == FeatureType::HCE;
43     napi_value result;
44     napi_get_boolean(env, isSupported, &result);
45     return result;
46 }
47 
HasHceCapability(napi_env env,napi_callback_info info)48 napi_value HasHceCapability(napi_env env, napi_callback_info info)
49 {
50     bool hasHceCapability = true;
51     napi_value result;
52     napi_get_boolean(env, hasHceCapability, &result);
53     return result;
54 }
55 
IsDefaultService(napi_env env,napi_callback_info cbinfo)56 napi_value IsDefaultService(napi_env env, napi_callback_info cbinfo)
57 {
58     bool isDefaultService = false;
59 
60     size_t argc = ARGV_NUM_2;
61     napi_value argv[ARGV_NUM_2] = {0};
62     napi_value thisVar = 0;
63     napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr);
64     ElementName element;
65     std::string type;
66     if (!CheckArgCountAndThrow(env, argc, ARGV_NUM_2) || !ParseElementName(env, element, argv[ARGV_INDEX_0]) ||
67         !ParseString(env, type, argv[ARGV_INDEX_1])) {
68         ErrorLog("IsDefaultService: parse args failed");
69         return CreateUndefined(env);
70     }
71     if (type != KITS::TYPE_PAYMENT) {
72         ErrorLog("IsDefaultService: unsupported card type");
73         return CreateUndefined(env);
74     }
75 
76     HceService hceService = HceService::GetInstance();
77     int statusCode = hceService.IsDefaultService(element, type, isDefaultService);
78     if (!CheckHceStatusCodeAndThrow(env, statusCode, "isDefaultService")) {
79         ErrorLog("IsDefaultService, statusCode = %{public}d", statusCode);
80         return CreateUndefined(env);
81     }
82     napi_value result;
83     napi_get_boolean(env, isDefaultService, &result);
84     return result;
85 }
86 
ConvertAbilityInfoToJS(napi_env env,napi_value & result,AbilityInfo & abilityInfo)87 void ConvertAbilityInfoToJS(napi_env env, napi_value &result, AbilityInfo &abilityInfo)
88 {
89     // std::string name;  // ability name, only the main class name
90     // std::string label;
91     // std::string bundleName;
92     // std::string iconPath;
93     napi_create_object(env, &result);
94 
95     napi_value name;
96     napi_create_string_utf8(env, abilityInfo.name.c_str(), NAPI_AUTO_LENGTH, &name);
97     napi_set_named_property(env, result, "name", name);
98 
99     napi_value labelId;
100     napi_create_int32(env, abilityInfo.labelId, &labelId);
101     napi_set_named_property(env, result, "labelId", labelId);
102 
103     napi_value bundleName;
104     napi_create_string_utf8(env, abilityInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &bundleName);
105     napi_set_named_property(env, result, "bundleName", bundleName);
106 
107     napi_value iconId;
108     napi_create_int32(env, abilityInfo.iconId, &iconId);
109     napi_set_named_property(env, result, "iconId", iconId);
110 }
111 
ConvertAbilityInfoVectorToJS(napi_env env,napi_value & result,std::vector<AbilityInfo> & paymentAbilityInfos)112 void ConvertAbilityInfoVectorToJS(napi_env env, napi_value &result, std::vector<AbilityInfo> &paymentAbilityInfos)
113 {
114     napi_create_array(env, &result);
115     if (paymentAbilityInfos.empty()) {
116         WarnLog("ConvertAbilityInfoVectorToJS ability infos is empty.");
117         return;
118     }
119     size_t idx = 0;
120     for (auto &abilityInfo : paymentAbilityInfos) {
121         napi_value obj = nullptr;
122         ConvertAbilityInfoToJS(env, obj, abilityInfo);
123         napi_set_element(env, result, idx, obj);
124         idx++;
125     }
126 }
GetPaymentServices(napi_env env,napi_callback_info info)127 napi_value GetPaymentServices(napi_env env, napi_callback_info info)
128 {
129     DebugLog("GetPaymentServices ability start.");
130     HceService hceService = HceService::GetInstance();
131     std::vector<AbilityInfo> paymentAbilityInfos;
132     int statusCode = hceService.GetPaymentServices(paymentAbilityInfos);
133     DebugLog("GetPaymentServices ability size %{public}zu.", paymentAbilityInfos.size());
134 
135     if (!CheckHceStatusCodeAndThrow(env, statusCode, "getPaymentServices")) {
136         ErrorLog("GetPaymentServices, statusCode = %{public}d", statusCode);
137         return CreateUndefined(env);
138     }
139     napi_value result = nullptr;
140     ConvertAbilityInfoVectorToJS(env, result, paymentAbilityInfos);
141     DebugLog("GetPaymentServices ability end.");
142     return result;
143 }
144 } // namespace KITS
145 } // namespace NFC
146 } // namespace OHOS