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