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_sa_client.h"
20 #include "nfc_sdk_common.h"
21 #include "indef_msg_callback.h"
22 #include "infc_controller_callback.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "nfc_state_change_callback.h"
26 #ifdef VENDOR_APPLICATIONS_ENABLED
27 #include "on_card_emulation_notify_cb_stub.h"
28 #include "query_app_info_callback_stub.h"
29 #endif
30 
31 namespace OHOS {
32 namespace NFC {
33 namespace KITS {
34 std::shared_ptr<OHOS::NFC::NfcControllerProxy> NfcController::nfcControllerProxy_;
35 std::weak_ptr<INfcControllerService> NfcController::nfcControllerService_;
36 sptr<IRemoteObject::DeathRecipient> NfcController::deathRecipient_;
37 sptr<IRemoteObject> NfcController::remote_;
38 bool NfcController::initialized_ = false;
39 bool NfcController::remoteDied_ = true;
40 std::mutex NfcController::mutex_;
41 static sptr<NfcStateChangeCallback> dataRdbObserver_;
42 #ifdef VENDOR_APPLICATIONS_ENABLED
43 static sptr<QueryAppInfoCallbackStub> g_queryAppInfoCallbackStub =
44     sptr<QueryAppInfoCallbackStub>(new (std::nothrow) QueryAppInfoCallbackStub());
45 static sptr<OnCardEmulationNotifyCbStub> g_onCardEmulationNotifyCbStub =
46     sptr<OnCardEmulationNotifyCbStub>(new (std::nothrow) OnCardEmulationNotifyCbStub());
47 #endif
48 
NfcController()49 NfcController::NfcController()
50 {
51     DebugLog("[NfcController::NfcController] new ability manager");
52     deathRecipient_ = new (std::nothrow) NfcServiceDeathRecipient(*this);
53 }
54 
~NfcController()55 NfcController::~NfcController()
56 {
57     DebugLog("destruct NfcController");
58 }
59 
InitNfcRemoteSA()60 void NfcController::InitNfcRemoteSA()
61 {
62     DebugLog("NfcController::%{public}s in, initialized_ = %{public}d, nfcControllerService_ = %{public}d",
63         __func__, initialized_, nfcControllerService_.expired());
64     std::lock_guard<std::mutex> guard(mutex_);
65     if (!initialized_ || nfcControllerService_.expired() || remoteDied_) {
66         remote_ = NfcSaClient::GetInstance().LoadNfcSa(NFC_MANAGER_SYS_ABILITY_ID);
67         if (remote_ == nullptr) {
68             ErrorLog("Nfc Controller Is Unexist.");
69             return;
70         }
71         if (deathRecipient_ == nullptr) {
72             WarnLog("deathRecipient_ is nullptr!");
73         }
74         remote_->AddDeathRecipient(deathRecipient_);
75         InfoLog("%{public}s:add remote death listener", __func__);
76         nfcControllerProxy_ = std::make_shared<NfcControllerProxy>(remote_);
77         nfcControllerService_ = nfcControllerProxy_;
78 
79         initialized_ = true;
80         remoteDied_ = false;
81     }
82     DebugLog("NfcController::%{public}s success.", __func__);
83 }
84 
GetInstance()85 NfcController &NfcController::GetInstance()
86 {
87     DebugLog("NfcController::GetInstance in.");
88     static NfcController instance;
89     return instance;
90 }
91 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)92 void NfcController::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
93 {
94     WarnLog("%{public}s:Remote service is died!", __func__);
95     std::lock_guard<std::mutex> lock(mutex_);
96     remoteDied_ = true;
97     initialized_ = false;
98     if (deathRecipient_ == nullptr || remoteObject == nullptr) {
99         ErrorLog("deathRecipient_ is nullptr!");
100         return;
101     }
102     if (remote_ == nullptr) {
103         ErrorLog("remote_ is nullptr!");
104         return;
105     }
106     remote_->RemoveDeathRecipient(deathRecipient_);
107 
108     nfcControllerService_.reset();
109     nfcControllerProxy_ = nullptr;
110     remote_ = nullptr;
111 }
112 
113 // Open NFC
TurnOn()114 int NfcController::TurnOn()
115 {
116     InitNfcRemoteSA();
117     if (nfcControllerService_.expired()) {
118         return ErrorCode::ERR_NFC_STATE_UNBIND;
119     }
120     return nfcControllerService_.lock()->TurnOn();
121 }
122 
123 // Close NFC
TurnOff()124 int NfcController::TurnOff()
125 {
126     InitNfcRemoteSA();
127     if (nfcControllerService_.expired()) {
128         return ErrorCode::ERR_NFC_STATE_UNBIND;
129     }
130     return nfcControllerService_.lock()->TurnOff();
131 }
132 
133 // get NFC state
GetNfcState()134 int NfcController::GetNfcState()
135 {
136     int state = NfcState::STATE_OFF;
137     if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) {
138         WarnLog("Nfc SA not started yet.");
139         return state;
140     }
141     InitNfcRemoteSA();
142     if (nfcControllerService_.expired()) {
143         ErrorLog("Nfc controller service expired.");
144         return state;
145     }
146     state = nfcControllerService_.lock()->GetState();
147     InfoLog("nfc state: %{public}d.", state);
148     return state;
149 }
150 
151 // check whether NFC is supported
IsNfcAvailable()152 bool NfcController::IsNfcAvailable()
153 {
154     return true;
155 }
156 
157 // check whether NFC is enabled
IsNfcOpen(bool & isOpen)158 int NfcController::IsNfcOpen(bool &isOpen)
159 {
160     isOpen = (GetNfcState() == NfcState::STATE_ON);
161     return ErrorCode::ERR_NONE;
162 }
163 
164 // register NFC state change callback
RegListener(const sptr<INfcControllerCallback> & callback,const std::string & type)165 ErrorCode NfcController::RegListener(const sptr<INfcControllerCallback> &callback,
166     const std::string& type)
167 {
168     InfoLog("NfcController::RegListener");
169     if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) {
170         WarnLog("nfc SA not started yet.");
171         return ErrorCode::ERR_NFC_STATE_UNBIND;
172     }
173     InitNfcRemoteSA();
174     if (nfcControllerService_.expired()) {
175         ErrorLog("nfcControllerService_ expired.");
176         return ErrorCode::ERR_NFC_STATE_UNBIND;
177     }
178     return nfcControllerService_.lock()->RegisterCallBack(callback, type);
179 }
180 
181 // unregister NFC state change
UnregListener(const std::string & type)182 ErrorCode NfcController::UnregListener(const std::string& type)
183 {
184     InfoLog("NfcController::UnregListener");
185     if (!NfcSaClient::GetInstance().CheckNfcSystemAbility()) {
186         WarnLog("nfc SA not started yet.");
187         return ErrorCode::ERR_NFC_STATE_UNBIND;
188     }
189     InitNfcRemoteSA();
190     if (nfcControllerService_.expired()) {
191         ErrorLog("nfcControllerService_ expired.");
192         return ErrorCode::ERR_NFC_STATE_UNBIND;
193     }
194     return nfcControllerService_.lock()->UnRegisterCallBack(type);
195 }
196 
GetTagServiceIface()197 OHOS::sptr<IRemoteObject> NfcController::GetTagServiceIface()
198 {
199     InitNfcRemoteSA();
200     if (nfcControllerService_.expired()) {
201         ErrorLog("NfcController::GetTagServiceIface nfcControllerService_ expired");
202         return nullptr;
203     }
204     return nfcControllerService_.lock()->GetTagServiceIface();
205 }
206 
RegNdefMsgCb(const sptr<INdefMsgCallback> & callback)207 ErrorCode NfcController::RegNdefMsgCb(const sptr<INdefMsgCallback> &callback)
208 {
209     DebugLog("NfcController::RegNdefMsgCb");
210     InitNfcRemoteSA();
211     if (nfcControllerService_.expired()) {
212         ErrorLog("NfcController::RegNdefMsgCb nfcControllerService_ expired");
213         return ErrorCode::ERR_NFC_STATE_UNBIND;
214     }
215     return nfcControllerService_.lock()->RegNdefMsgCb(callback);
216 }
217 
218 #ifdef VENDOR_APPLICATIONS_ENABLED
RegQueryApplicationCb(const std::string & type,QueryApplicationByVendor tagCallback,QueryHceAppByVendor hceCallback)219 ErrorCode NfcController::RegQueryApplicationCb(const std::string& type,
220     QueryApplicationByVendor tagCallback, QueryHceAppByVendor hceCallback)
221 {
222     DebugLog("NfcController::RegQueryApplicationCb");
223     InitNfcRemoteSA();
224     if (nfcControllerService_.expired()) {
225         ErrorLog("NfcController::RegQueryApplicationCb nfcControllerService_ expired");
226         return ErrorCode::ERR_NFC_STATE_UNBIND;
227     }
228     if (type.compare(KEY_TAG_APP) == 0) {
229         g_queryAppInfoCallbackStub->RegisterQueryTagAppCallback(tagCallback);
230     } else if (type.compare(KEY_HCE_APP) == 0) {
231         g_queryAppInfoCallbackStub->RegisterQueryHceAppCallback(hceCallback);
232     }
233     return nfcControllerService_.lock()->RegQueryApplicationCb(g_queryAppInfoCallbackStub);
234 }
235 
RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback)236 ErrorCode NfcController::RegCardEmulationNotifyCb(OnCardEmulationNotifyCb callback)
237 {
238     DebugLog("NfcController::RegCardEmulationNotifyCb");
239     InitNfcRemoteSA();
240     if (nfcControllerService_.expired()) {
241         ErrorLog("NfcController::RegCardEmulationNotifyCb nfcControllerService_ expired");
242         return ErrorCode::ERR_NFC_STATE_UNBIND;
243     }
244     g_onCardEmulationNotifyCbStub->RegisterCallback(callback);
245     return nfcControllerService_.lock()->RegCardEmulationNotifyCb(g_onCardEmulationNotifyCbStub);
246 }
NotifyEventStatus(int eventType,int arg1,std::string arg2)247 ErrorCode NfcController::NotifyEventStatus(int eventType, int arg1, std::string arg2)
248 {
249     DebugLog("NfcController::NotifyEventStatus");
250     InitNfcRemoteSA();
251     if (nfcControllerService_.expired()) {
252         ErrorLog("NfcController::NotifyEventStatus nfcControllerService_ expired");
253         return ErrorCode::ERR_NFC_STATE_UNBIND;
254     }
255     return nfcControllerService_.lock()->NotifyEventStatus(eventType, arg1, arg2);
256 }
257 #endif
258 
GetHceServiceIface()259 OHOS::sptr<IRemoteObject> NfcController::GetHceServiceIface()
260 {
261     InitNfcRemoteSA();
262     if (nfcControllerService_.expired()) {
263         ErrorLog("NfcController::GetHceServiceIface nfcControllerService_ expired");
264         return nullptr;
265     }
266     return nfcControllerService_.lock()->GetHceServiceIface();
267 }
268 }  // namespace KITS
269 }  // namespace NFC
270 }  // namespace OHOS