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 <cstring>
17 #include "cardEmulation.h"
18 #include "loghelper.h"
19 #include "hce_service.h"
20 #include "ability_info.h"
21 #include "element_name.h"
22 #include "nfc_cardEmulation_controller.h"
23 #include "nfc_cardEmulation_ffi.h"
24
25 namespace OHOS {
26 namespace NFC {
27 namespace KITS {
28 const int8_t HCE_CMD = 0;
29
30 class CjHceCmdListenerEvent : public IHceCmdCallback {
31 public:
CjHceCmdListenerEvent()32 CjHceCmdListenerEvent() {}
33
~CjHceCmdListenerEvent()34 virtual ~CjHceCmdListenerEvent() {}
35
36 public:
OnCeApduData(const std::vector<uint8_t> & data)37 void OnCeApduData(const std::vector<uint8_t> &data) override
38 {
39 CjNfcCardEmulationController::GetInstance()->HceCmd(data);
40 }
41
AsObject()42 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
43 {
44 return nullptr;
45 }
46 };
47
48 sptr<CjHceCmdListenerEvent> cjHceCmdListenerEvent =
49 sptr<CjHceCmdListenerEvent>(new (std::nothrow) CjHceCmdListenerEvent());
50 static bool g_isEventRegistered = false;
51
CharPtrToVector(char ** charPtr,int32_t size)52 std::vector<std::string> CharPtrToVector(char **charPtr, int32_t size)
53 {
54 std::vector<std::string> result;
55 for (int32_t i = 0; i < size; i++) {
56 if (charPtr) {
57 result.push_back(std::string(charPtr[i]));
58 }
59 }
60 return result;
61 }
62
63 extern "C" {
FfiNfcCardEmulationisDefaultService(char * cBundleName,char * cAbilityName,char * cModuleName,char * cardTypeName,bool * ret)64 int32_t FfiNfcCardEmulationisDefaultService(
65 char *cBundleName, char *cAbilityName, char *cModuleName, char *cardTypeName, bool *ret)
66 {
67 bool isDefaultService = false;
68 std::string type(cardTypeName);
69 std::string bundleName(cBundleName);
70 std::string moduleName(cModuleName);
71 std::string abilityName(cAbilityName);
72 ElementName element;
73 element.SetBundleName(bundleName);
74 element.SetModuleName(moduleName);
75 element.SetAbilityName(abilityName);
76 HceService hceService = HceService::GetInstance();
77 int32_t errorCode = hceService.IsDefaultService(element, type, isDefaultService);
78 *ret = isDefaultService;
79 return errorCode;
80 }
81
FfiNfcCardEmulationstart(char * cBundleName,char * cAbilityName,char * cModuleName,CArrString cAidList)82 int32_t FfiNfcCardEmulationstart(char *cBundleName, char *cAbilityName, char *cModuleName, CArrString cAidList)
83 {
84 std::string bundleName(cBundleName);
85 std::string moduleName(cModuleName);
86 std::string abilityName(cAbilityName);
87 ElementName element;
88 element.SetBundleName(bundleName);
89 element.SetModuleName(moduleName);
90 element.SetAbilityName(abilityName);
91 std::vector<std::string> aidVec;
92 aidVec = CharPtrToVector(cAidList.head, cAidList.size);
93 HceService hceService = HceService::GetInstance();
94 int32_t errorCode = hceService.StartHce(element, aidVec);
95 return errorCode;
96 }
97
FfiNfcCardEmulationOn(int8_t eventType,int64_t id)98 int32_t FfiNfcCardEmulationOn(int8_t eventType, int64_t id)
99 {
100 if (!g_isEventRegistered) {
101 HceService hceService = HceService::GetInstance();
102 ErrorCode ret = hceService.RegHceCmdCallback(cjHceCmdListenerEvent, KITS::EVENT_HCE_CMD);
103 if (ret != KITS::ERR_NONE) {
104 return ret;
105 }
106 g_isEventRegistered = true;
107 }
108 auto controller = CjNfcCardEmulationController::GetInstance();
109 if (controller == nullptr) {
110 return ERR_NO_MEMORY;
111 }
112 return controller->Subscribe(eventType, id);
113 }
114
FfiNfcCardEmulationstop(char * cBundleName,char * cAbilityName,char * cModuleName)115 int32_t FfiNfcCardEmulationstop(char *cBundleName, char *cAbilityName, char *cModuleName)
116 {
117 std::string bundleName(cBundleName);
118 std::string moduleName(cModuleName);
119 std::string abilityName(cAbilityName);
120 ElementName element;
121 element.SetBundleName(bundleName);
122 element.SetModuleName(moduleName);
123 element.SetAbilityName(abilityName);
124 HceService hceService = HceService::GetInstance();
125 ErrorCode ret = hceService.StopHce(element);
126 if (ret != KITS::ERR_NONE) {
127 return ret;
128 }
129 g_isEventRegistered = false;
130 auto controller = CjNfcCardEmulationController::GetInstance();
131 if (controller == nullptr) {
132 return ERR_NO_MEMORY;
133 }
134 return controller->UnSubscribe(HCE_CMD);
135 }
136
FfiNfcCardEmulationTransmit(CArrUI8 cResponseApdu)137 int32_t FfiNfcCardEmulationTransmit(CArrUI8 cResponseApdu)
138 {
139 std::string dataBytes = NfcSdkCommon::BytesVecToHexString(cResponseApdu.head, cResponseApdu.size);
140 std::string hexRespData;
141 HceService hceService = HceService::GetInstance();
142 int32_t errorCode = hceService.SendRawFrame(dataBytes, true, hexRespData);
143 return errorCode;
144 }
145 }
146
147 } // namespace KITS
148 } // namespace NFC
149 } // namespace OHOS
150