• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "nfc_controller.h"
16 
17 #include "loghelper.h"
18 #include "nfc_controller_callback_stub.h"
19 #include "nfc_controller_proxy.h"
20 #include "nfc_sa_client.h"
21 #include "nfc_sdk_common.h"
22 #include "indef_msg_callback.h"
23 #include "infc_controller_callback.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace NFC {
29 namespace KITS {
30 static const uint8_t MAX_RETRY_TIMES = 3;
31 
32 sptr<IRemoteObject::DeathRecipient> NfcController::deathRecipient_;
33 sptr<IRemoteObject> NfcController::remote_;
34 bool NfcController::initialized_ = false;
35 bool NfcController::remoteDied_ = true;
36 std::mutex NfcController::mutex_;
37 
38 static sptr<NfcControllerCallBackStub> g_nfcControllerCallbackStub =
39     sptr<NfcControllerCallBackStub>(new NfcControllerCallBackStub());
40 static sptr<NdefMsgCallbackStub> g_ndefMsgCallbackStub =
41     sptr<NdefMsgCallbackStub>(new NdefMsgCallbackStub());
42 
43 #ifdef VENDOR_APPLICATIONS_ENABLED
44 static sptr<QueryAppInfoCallbackStub> g_queryAppInfoCallbackStub =
45     sptr<QueryAppInfoCallbackStub>(new QueryAppInfoCallbackStub());
46 static sptr<OnCardEmulationNotifyCbStub> g_onCardEmulationNotifyCbStub =
47     sptr<OnCardEmulationNotifyCbStub>(new OnCardEmulationNotifyCbStub());
48 #endif
49 
NfcController()50 NfcController::NfcController()
51 {
52     DebugLog("[NfcController::NfcController] new ability manager");
53     deathRecipient_ = new (std::nothrow) NfcServiceDeathRecipient(*this);
54 }
55 
~NfcController()56 NfcController::~NfcController()
57 {
58     DebugLog("destruct NfcController");
59 }
60 
InitNfcRemoteSA()61 void NfcController::InitNfcRemoteSA()
62 {
63     DebugLog("initialized_ = %{public}d, remote_ = %{public}d", initialized_, remote_ == nullptr);
64     std::lock_guard<std::mutex> guard(mutex_);
65     if (!initialized_ || remote_ == nullptr || remoteDied_) {
66         for (uint8_t i = 0; i < MAX_RETRY_TIMES; ++i) {
67             remote_ = NfcSaClient::GetInstance().LoadNfcSa(NFC_MANAGER_SYS_ABILITY_ID);
68             if (remote_ == nullptr) {
69                 ErrorLog("Nfc Controller Is Unexist...retrying...");
70                 sleep(1);
71                 continue;
72             }
73             break;
74         }
75         if (remote_ == nullptr) {
76             ErrorLog("Nfc Controller Is Unexist.");
77             return;
78         }
79         if (deathRecipient_ == nullptr) {
80             WarnLog("deathRecipient_ is nullptr!");
81         }
82         remote_->AddDeathRecipient(deathRecipient_);
83         InfoLog("%{public}s:add remote death listener", __func__);
84 
85         initialized_ = true;
86         remoteDied_ = false;
87     }
88     DebugLog("NfcController::%{public}s success.", __func__);
89 }
90 
GetInstance()91 NfcController &NfcController::GetInstance()
92 {
93     DebugLog("NfcController::GetInstance in.");
94     static NfcController instance;
95     return instance;
96 }
97 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)98 void NfcController::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
99 {
100     WarnLog("%{public}s:Remote service is died!", __func__);
101     std::lock_guard<std::mutex> lock(mutex_);
102     remoteDied_ = true;
103     initialized_ = false;
104     if (deathRecipient_ == nullptr || remoteObject == nullptr) {
105         ErrorLog("deathRecipient_ is nullptr!");
106         return;
107     }
108     if (remote_ == nullptr) {
109         ErrorLog("remote_ is nullptr!");
110         return;
111     }
112     remote_->RemoveDeathRecipient(deathRecipient_);
113     remote_ = nullptr;
114 }
115 
116 // Open NFC
TurnOn()117 int NfcController::TurnOn()
118 {
119     InitNfcRemoteSA();
120     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
121     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
122         ErrorLog("nfc controller proxy nullptr.");
123         return ErrorCode::ERR_NFC_STATE_UNBIND;
124     }
125     ErrCode errCode = controllerProxy->TurnOn();
126     InfoLog("errCode = %{public}d", errCode);
127     return static_cast<int>(errCode);
128 }
129 
130 // Close NFC
TurnOff()131 int NfcController::TurnOff()
132 {
133     InitNfcRemoteSA();
134     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
135     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
136         ErrorLog("nfc controller proxy nullptr.");
137         return ErrorCode::ERR_NFC_STATE_UNBIND;
138     }
139     ErrCode errCode = controllerProxy->TurnOff();
140     InfoLog("errCode = %{public}d", errCode);
141     return static_cast<int>(errCode);
142 }
143 
144 // get NFC state
GetNfcState()145 int NfcController::GetNfcState()
146 {
147     int state = NfcState::STATE_OFF;
148     if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) {
149         WarnLog("Nfc SA not started yet.");
150         return state;
151     }
152     InitNfcRemoteSA();
153     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
154     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
155         ErrorLog("nfc controller proxy nullptr.");
156         return state;
157     }
158     controllerProxy->GetState(state);
159     InfoLog("nfc state: %{public}d.", state);
160     return state;
161 }
162 
163 // check whether NFC is supported
IsNfcAvailable()164 bool NfcController::IsNfcAvailable()
165 {
166     return true;
167 }
168 
169 // check whether NFC is enabled
IsNfcOpen(bool & isOpen)170 int NfcController::IsNfcOpen(bool &isOpen)
171 {
172     isOpen = (GetNfcState() == NfcState::STATE_ON);
173     return ErrorCode::ERR_NONE;
174 }
175 
176 // register NFC state change callback
RegListener(const sptr<INfcControllerCallback> & callback,const std::string & type)177 ErrorCode NfcController::RegListener(const sptr<INfcControllerCallback> &callback,
178     const std::string& type)
179 {
180     InfoLog("NfcController::RegListener");
181     if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) {
182         WarnLog("nfc SA not started yet.");
183         return ErrorCode::ERR_NFC_STATE_UNBIND;
184     }
185     if (g_nfcControllerCallbackStub == nullptr) {
186         ErrorLog("g_nfcControllerCallbackStub is nullptr");
187         return KITS::ERR_NFC_PARAMETERS;
188     }
189     g_nfcControllerCallbackStub->RegisterCallBack(callback);
190     InitNfcRemoteSA();
191     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
192     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
193         ErrorLog("nfc controller proxy nullptr.");
194         return ErrorCode::ERR_NFC_STATE_UNBIND;
195     }
196     return static_cast<ErrorCode>(controllerProxy->RegisterNfcStatusCallBack(g_nfcControllerCallbackStub, type));
197 }
198 
199 // unregister NFC state change
UnregListener(const std::string & type)200 ErrorCode NfcController::UnregListener(const std::string& type)
201 {
202     InfoLog("NfcController::UnregListener");
203     if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) {
204         WarnLog("nfc SA not started yet.");
205         return ErrorCode::ERR_NFC_STATE_UNBIND;
206     }
207     InitNfcRemoteSA();
208     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
209     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
210         ErrorLog("nfc controller proxy nullptr.");
211         return ErrorCode::ERR_NFC_STATE_UNBIND;
212     }
213     return static_cast<ErrorCode>(controllerProxy->UnregisterNfcStatusCallBack(type));
214 }
215 
GetTagServiceIface()216 OHOS::sptr<IRemoteObject> NfcController::GetTagServiceIface()
217 {
218     InitNfcRemoteSA();
219     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
220     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
221         ErrorLog("nfc controller proxy nullptr.");
222         return nullptr;
223     }
224     OHOS::sptr<IRemoteObject> remoteObj = nullptr;
225     controllerProxy->GetTagServiceIface(remoteObj);
226     return remoteObj;
227 }
228 
RegNdefMsgCb(const sptr<INdefMsgCallback> & callback)229 ErrorCode NfcController::RegNdefMsgCb(const sptr<INdefMsgCallback> &callback)
230 {
231     DebugLog("NfcController::RegNdefMsgCb");
232     if (g_ndefMsgCallbackStub == nullptr) {
233         ErrorLog("g_ndefMsgCallbackStub is nullptr");
234         return KITS::ERR_NFC_PARAMETERS;
235     }
236     g_ndefMsgCallbackStub->RegisterCallback(callback);
237 
238     InitNfcRemoteSA();
239     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
240     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
241         ErrorLog("nfc controller proxy nullptr.");
242         return ErrorCode::ERR_NFC_STATE_UNBIND;
243     }
244     return static_cast<ErrorCode>(controllerProxy->RegNdefMsgCb(g_ndefMsgCallbackStub));
245 }
246 
247 #ifdef VENDOR_APPLICATIONS_ENABLED
RegQueryApplicationCb(const std::string & type,QueryApplicationByVendor tagCallback,QueryHceAppByVendor hceCallback)248 ErrorCode NfcController::RegQueryApplicationCb(const std::string& type,
249     QueryApplicationByVendor tagCallback, QueryHceAppByVendor hceCallback)
250 {
251     DebugLog("NfcController::RegQueryApplicationCb");
252     if (g_queryAppInfoCallbackStub == nullptr) {
253         ErrorLog("g_queryAppInfoCallbackStub is nullptr");
254         return KITS::ERR_NFC_PARAMETERS;
255     }
256 
257     InitNfcRemoteSA();
258     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
259     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
260         ErrorLog("nfc controller proxy nullptr.");
261         return ErrorCode::ERR_NFC_STATE_UNBIND;
262     }
263     if (type.compare(KEY_TAG_APP) == 0) {
264         g_queryAppInfoCallbackStub->RegisterQueryTagAppCallback(tagCallback);
265     } else if (type.compare(KEY_HCE_APP) == 0) {
266         g_queryAppInfoCallbackStub->RegisterQueryHceAppCallback(hceCallback);
267     }
268     return static_cast<ErrorCode>(controllerProxy->RegQueryApplicationCb(g_queryAppInfoCallbackStub));
269 }
270 
RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback)271 ErrorCode NfcController::RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback)
272 {
273     DebugLog("NfcController::RegCardEmulationNotifyCb");
274     if (g_onCardEmulationNotifyCbStub == nullptr) {
275         ErrorLog("g_onCardEmulationNotifyCbStub is nullptr");
276         return KITS::ERR_NFC_PARAMETERS;
277     }
278 
279     InitNfcRemoteSA();
280     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
281     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
282         ErrorLog("nfc controller proxy nullptr.");
283         return ErrorCode::ERR_NFC_STATE_UNBIND;
284     }
285     g_onCardEmulationNotifyCbStub->RegisterCallback(callback);
286     return static_cast<ErrorCode>(controllerProxy->RegCardEmulationNotifyCb(g_onCardEmulationNotifyCbStub));
287 }
288 
NotifyEventStatus(int eventType,int arg1,std::string arg2)289 ErrorCode NfcController::NotifyEventStatus(int eventType, int arg1, std::string arg2)
290 {
291     DebugLog("NfcController::NotifyEventStatus");
292     InitNfcRemoteSA();
293     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
294     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
295         ErrorLog("nfc controller proxy nullptr.");
296         return ErrorCode::ERR_NFC_STATE_UNBIND;
297     }
298     return static_cast<ErrorCode>(controllerProxy->NotifyEventStatus(eventType, arg1, arg2));
299 }
300 #endif // VENDOR_APPLICATIONS_ENABLED
301 
GetHceServiceIface(int32_t & res)302 OHOS::sptr<IRemoteObject> NfcController::GetHceServiceIface(int32_t &res)
303 {
304     InitNfcRemoteSA();
305     sptr<INfcController> controllerProxy = iface_cast<INfcController>(remote_);
306     if (controllerProxy == nullptr || controllerProxy->AsObject() == nullptr) {
307         ErrorLog("nfc controller proxy nullptr.");
308         return nullptr;
309     }
310     OHOS::sptr<IRemoteObject> remoteObj = nullptr;
311     controllerProxy->GetHceServiceIface(remoteObj);
312     return remoteObj;
313 }
314 }  // namespace KITS
315 }  // namespace NFC
316 }  // namespace OHOS