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_stub.h"
16
17 #include "ipc_skeleton.h"
18 #include "loghelper.h"
19 #include "ndef_msg_callback_proxy.h"
20 #include "nfc_sdk_common.h"
21 #include "nfc_service_ipc_interface_code.h"
22 #include "nfc_controller_death_recipient.h"
23 #include "nfc_permission_checker.h"
24 #ifdef VENDOR_APPLICATIONS_ENABLED
25 #include "on_card_emulation_notify_cb_proxy.h"
26 #include "query_app_info_callback_proxy.h"
27 #endif
28 #include "external_deps_proxy.h"
29
30 namespace OHOS {
31 namespace NFC {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int NfcControllerStub::OnRemoteRequest(uint32_t code, /* [in] */
33 MessageParcel& data, /* [in] */
34 MessageParcel& reply, /* [out] */
35 MessageOption& option) /* [in] */
36 {
37 InfoLog("NfcControllerStub OnRemoteRequest occur, code is %{public}d", code);
38 if (data.ReadInterfaceToken() != GetDescriptor()) {
39 ErrorLog("NfcControllerStub OnRemoteRequest GetDescriptor failed");
40 return KITS::ERR_NFC_PARAMETERS;
41 }
42 switch (code) {
43 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_STATE):
44 return HandleGetState(data, reply);
45 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_TURN_ON):
46 return HandleTurnOn(data, reply);
47 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_TURN_OFF):
48 return HandleTurnOff(data, reply);
49 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REGISTER_CALLBACK):
50 return HandleRegisterCallBack(data, reply);
51 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_UNREGISTER_CALLBACK):
52 return HandleUnRegisterCallBack(data, reply);
53 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_NFC_OPEN):
54 return HandleIsNfcOpen(data, reply);
55 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TAG_INTERFACE):
56 return HandleGetNfcTagInterface(data, reply);
57 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REG_NDEF_MSG_CALLBACK):
58 return HandleRegNdefMsgCb(data, reply);
59 #ifdef VENDOR_APPLICATIONS_ENABLED
60 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_QUERY_APP_INFO_MSG_CALLBACK):
61 return HandleRegQueryApplicationCb(data, reply);
62 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_CARD_EMULATION_NOTIFY):
63 return HandleRegCardEmulationNotifyCb(data, reply);
64 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_VENDOR_NOTIFY):
65 return HandleNotifyEventStatus(data, reply);
66 #endif
67 case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_HCE_INTERFACE):
68 return HandleGetNfcHceInterface(data, reply);
69 default:
70 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71 }
72 }
73
HandleGetState(MessageParcel & data,MessageParcel & reply)74 int NfcControllerStub::HandleGetState(MessageParcel& data, MessageParcel& reply)
75 {
76 int state = GetState();
77 reply.WriteInt32(state);
78 return ERR_NONE;
79 }
80
HandleTurnOn(MessageParcel & data,MessageParcel & reply)81 int NfcControllerStub::HandleTurnOn(MessageParcel& data, MessageParcel& reply)
82 {
83 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::SYS_PERM)) {
84 ErrorLog("HandleTurnOn no permission");
85 reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
86 return KITS::ErrorCode::ERR_NO_PERMISSION;
87 }
88 int statusCode = TurnOn();
89 reply.WriteInt32(statusCode);
90 return statusCode;
91 }
92
HandleTurnOff(MessageParcel & data,MessageParcel & reply)93 int NfcControllerStub::HandleTurnOff(MessageParcel& data, MessageParcel& reply)
94 {
95 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::SYS_PERM)) {
96 ErrorLog("HandleTurnOff no permission");
97 reply.WriteInt32(KITS::ErrorCode::ERR_NO_PERMISSION);
98 return KITS::ErrorCode::ERR_NO_PERMISSION;
99 }
100 int statusCode = TurnOff();
101 reply.WriteInt32(statusCode);
102 return statusCode;
103 }
104
HandleIsNfcOpen(MessageParcel & data,MessageParcel & reply)105 int NfcControllerStub::HandleIsNfcOpen(MessageParcel& data, MessageParcel& reply)
106 {
107 bool isOpen = false;
108 int statusCode = IsNfcOpen(isOpen);
109 reply.WriteBool(isOpen);
110 return statusCode;
111 }
112
HandleRegisterCallBack(MessageParcel & data,MessageParcel & reply)113 int NfcControllerStub::HandleRegisterCallBack(MessageParcel &data, MessageParcel &reply)
114 {
115 std::string type = data.ReadString();
116 int exception = data.ReadInt32();
117 if (exception) {
118 return KITS::ERR_NFC_PARAMETERS;
119 }
120 KITS::ErrorCode ret = KITS::ERR_NFC_PARAMETERS;
121 do {
122 sptr<IRemoteObject> remote = data.ReadRemoteObject();
123 if (remote == nullptr) {
124 DebugLog("Failed to readRemoteObject!");
125 break;
126 }
127
128 std::unique_ptr<NfcControllerDeathRecipient> recipient
129 = std::make_unique<NfcControllerDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
130 if (recipient == nullptr) {
131 ErrorLog("recipient is null");
132 return ERR_NONE;
133 }
134 sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
135 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(dr))) {
136 ErrorLog("Failed to add death recipient");
137 return ERR_NONE;
138 }
139
140 {
141 std::lock_guard<std::mutex> guard(mutex_);
142 deathRecipient_ = dr;
143 callback_ = iface_cast<INfcControllerCallback>(remote);
144 if (callback_ == nullptr) {
145 callback_ = new (std::nothrow) NfcControllerCallBackProxy(remote);
146 DebugLog("create new `NfcControllerCallBackProxy`!");
147 }
148 ret = RegisterCallBack(callback_, type);
149 }
150 } while (0);
151
152 reply.WriteInt32(ret);
153 return ERR_NONE;
154 }
155
RemoveNfcDeathRecipient(const wptr<IRemoteObject> & remote)156 void NfcControllerStub::RemoveNfcDeathRecipient(const wptr<IRemoteObject> &remote)
157 {
158 std::lock_guard<std::mutex> guard(mutex_);
159 if (callback_ == nullptr) {
160 ErrorLog("OnRemoteDied callback_ is nullptr");
161 return;
162 }
163 auto serviceRemote = callback_->AsObject();
164 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
165 serviceRemote->RemoveDeathRecipient(deathRecipient_);
166 callback_ = nullptr;
167 ErrorLog("on remote died");
168 }
169 }
170
HandleUnRegisterCallBack(MessageParcel & data,MessageParcel & reply)171 int NfcControllerStub::HandleUnRegisterCallBack(MessageParcel &data, MessageParcel &reply)
172 {
173 InfoLog("OnUnRegisterCallBack");
174 std::string type = data.ReadString();
175 int exception = data.ReadInt32();
176 if (exception) {
177 return KITS::ERR_NFC_PARAMETERS;
178 }
179 KITS::ErrorCode ret = UnRegisterCallBack(type);
180 DebugLog("OnUnRegisterCallBack::OnUnRegisterCallBack end##ret=%{public}d\n", ret);
181 reply.WriteInt32(ret);
182 return ERR_NONE;
183 }
184
RegisterCallBack(const sptr<INfcControllerCallback> & callback,const std::string & type)185 KITS::ErrorCode NfcControllerStub::RegisterCallBack(const sptr<INfcControllerCallback> &callback,
186 const std::string& type)
187 {
188 return RegisterCallBack(callback, type, IPCSkeleton::GetCallingTokenID());
189 }
190
UnRegisterCallBack(const std::string & type)191 KITS::ErrorCode NfcControllerStub::UnRegisterCallBack(const std::string& type)
192 {
193 return UnRegisterCallBack(type, IPCSkeleton::GetCallingTokenID());
194 }
195
HandleGetNfcTagInterface(MessageParcel & data,MessageParcel & reply)196 int NfcControllerStub::HandleGetNfcTagInterface(MessageParcel& data, MessageParcel& reply)
197 {
198 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
199 ErrorLog("HandleGetNfcTagInterface no permission");
200 return KITS::ErrorCode::ERR_NO_PERMISSION;
201 }
202 OHOS::sptr<IRemoteObject> remoteOjbect = GetTagServiceIface();
203 if (remoteOjbect == nullptr) {
204 ErrorLog("HandleGetNfcTagInterface remoteOjbect null!");
205 return KITS::ERR_NFC_PARAMETERS;
206 }
207
208 reply.WriteRemoteObject(remoteOjbect);
209 return ERR_NONE;
210 }
211
HandleGetNfcHceInterface(MessageParcel & data,MessageParcel & reply)212 int NfcControllerStub::HandleGetNfcHceInterface(MessageParcel& data, MessageParcel& reply)
213 {
214 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
215 ErrorLog("HandleGetNfcHceInterface no permission");
216 return KITS::ErrorCode::ERR_NO_PERMISSION;
217 }
218 OHOS::sptr<IRemoteObject> remoteOjbect = GetHceServiceIface();
219 if (remoteOjbect == nullptr) {
220 ErrorLog("HandleGetNfcHceInterface remoteOjbect null!");
221 return KITS::ERR_NFC_PARAMETERS;
222 }
223
224 reply.WriteRemoteObject(remoteOjbect);
225 return ERR_NONE;
226 }
227
HandleRegNdefMsgCb(MessageParcel & data,MessageParcel & reply)228 int NfcControllerStub::HandleRegNdefMsgCb(MessageParcel& data, MessageParcel& reply)
229 {
230 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
231 ErrorLog("HandleRegNdefMsgCb no permission");
232 return KITS::ErrorCode::ERR_NO_PERMISSION;
233 }
234 InfoLog("NfcControllerStub::HandleRegNdefMsgCb");
235 KITS::ErrorCode ret = KITS::ERR_NFC_PARAMETERS;
236 do {
237 sptr<IRemoteObject> remote = data.ReadRemoteObject();
238 if (remote == nullptr) {
239 DebugLog("Failed to readRemoteObject!");
240 break;
241 }
242 {
243 std::lock_guard<std::mutex> guard(mutex_);
244 ndefCallback_ = iface_cast<INdefMsgCallback>(remote);
245 if (ndefCallback_ == nullptr) {
246 ndefCallback_ = new (std::nothrow) NdefMsgCallbackProxy(remote);
247 DebugLog("NfcControllerStub::HandleRegNdefMsgCb, create new `NdefMsgCallbackProxy`!");
248 }
249 ret = RegNdefMsgCallback(ndefCallback_);
250 }
251 } while (0);
252 reply.WriteInt32(ret);
253 return ERR_NONE;
254 }
255
256 #ifdef VENDOR_APPLICATIONS_ENABLED
HandleRegQueryApplicationCb(MessageParcel & data,MessageParcel & reply)257 int NfcControllerStub::HandleRegQueryApplicationCb(MessageParcel& data, MessageParcel& reply)
258 {
259 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
260 ErrorLog("HandleRegQueryApplicationCb no permission");
261 return KITS::ErrorCode::ERR_NO_PERMISSION;
262 }
263 InfoLog("NfcControllerStub::HandleRegQueryApplicationCb");
264 sptr<IRemoteObject> remote = data.ReadRemoteObject();
265 if (remote == nullptr) {
266 ErrorLog("Failed to readRemoteObject!");
267 return KITS::ERR_NFC_PARAMETERS;
268 }
269 {
270 std::lock_guard<std::mutex> guard(mutex_);
271 queryAppInfoCallback_ = iface_cast<IQueryAppInfoCallback>(remote);
272 if (queryAppInfoCallback_ == nullptr) {
273 queryAppInfoCallback_ = new (std::nothrow) QueryAppInfoCallbackProxy(remote);
274 DebugLog("NfcControllerStub::HandleRegQueryApplicationCb, create new `QueryAppInfoCallbackProxy`!");
275 }
276 int ret = RegQueryApplicationCb(queryAppInfoCallback_);
277 reply.WriteInt32(ret);
278 }
279 return ERR_NONE;
280 }
281
HandleRegCardEmulationNotifyCb(MessageParcel & data,MessageParcel & reply)282 int NfcControllerStub::HandleRegCardEmulationNotifyCb(MessageParcel& data, MessageParcel& reply)
283 {
284 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
285 ErrorLog("HandleRegCardEmulationNotifyCb no permission");
286 return KITS::ErrorCode::ERR_NO_PERMISSION;
287 }
288 InfoLog("NfcControllerStub::HandleRegCardEmulationNotifyCb");
289 sptr<IRemoteObject> remote = data.ReadRemoteObject();
290 if (remote == nullptr) {
291 ErrorLog("Failed to readRemoteObject!");
292 return KITS::ERR_NFC_PARAMETERS;
293 }
294 {
295 std::lock_guard<std::mutex> guard(mutex_);
296 onCardEmulationNotifyCb_ = iface_cast<IOnCardEmulationNotifyCb>(remote);
297 if (onCardEmulationNotifyCb_ == nullptr) {
298 onCardEmulationNotifyCb_ = new (std::nothrow) OnCardEmulationNotifyCbProxy(remote);
299 DebugLog("NfcControllerStub::HandleRegCardEmulationNotifyCb, create new `OnCardEmulationNotifyCbProxy`!");
300 }
301 int ret = RegCardEmulationNotifyCb(onCardEmulationNotifyCb_);
302 reply.WriteInt32(ret);
303 }
304 return ERR_NONE;
305 }
HandleNotifyEventStatus(MessageParcel & data,MessageParcel & reply)306 int NfcControllerStub::HandleNotifyEventStatus(MessageParcel& data, MessageParcel& reply)
307 {
308 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
309 ErrorLog("HandleNotifyEventStatus no permission");
310 return KITS::ErrorCode::ERR_NO_PERMISSION;
311 }
312 int eventType = data.ReadInt32();
313 int arg1 = data.ReadInt32();
314 std::string arg2 = data.ReadString();
315 int exception = data.ReadInt32();
316 if (exception) {
317 ErrorLog("HandleNotifyEventStatus::read param failed.");
318 return KITS::ERR_NFC_PARAMETERS;
319 }
320 KITS::ErrorCode ret = NotifyEventStatus(eventType, arg1, arg2);
321 reply.WriteInt32(ret);
322 return ERR_NONE;
323 }
324 #endif
325
RegNdefMsgCb(const sptr<INdefMsgCallback> & callback)326 KITS::ErrorCode NfcControllerStub::RegNdefMsgCb(const sptr<INdefMsgCallback> &callback)
327 {
328 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
329 ErrorLog("RegNdefMsgCb no permission");
330 return KITS::ErrorCode::ERR_NO_PERMISSION;
331 }
332 InfoLog("NfcControllerStub::RegNdefMsgCb");
333 return RegNdefMsgCallback(callback);
334 }
335 } // namespace NFC
336 } // namespace OHOS
337