• 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_stub.h"
16 #include "ipc_skeleton.h"
17 #include "loghelper.h"
18 #include "nfc_sdk_common.h"
19 #include "nfc_service_ipc_interface_code.h"
20 #include "nfc_controller_death_recipient.h"
21 #include "permission_tools.h"
22 
23 namespace OHOS {
24 namespace NFC {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)25 int NfcControllerStub::OnRemoteRequest(uint32_t code,         /* [in] */
26                                        MessageParcel& data,   /* [in] */
27                                        MessageParcel& reply,  /* [out] */
28                                        MessageOption& option) /* [in] */
29 {
30     InfoLog("NfcControllerStub OnRemoteRequest occur, code is %{public}d", code);
31     if (data.ReadInterfaceToken() != GetDescriptor()) {
32         ErrorLog("NfcControllerStub OnRemoteRequest GetDescriptor failed");
33         return KITS::ERR_NFC_PARAMETERS;
34     }
35     switch (code) {
36         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_STATE):
37             return HandleGetState(data, reply);
38         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_TURN_ON):
39             return HandleTurnOn(data, reply);
40         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_TURN_OFF):
41             return HandleTurnOff(data, reply);
42         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REGISTER_CALLBACK):
43             return HandleRegisterCallBack(data, reply);
44         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_UNREGISTER_CALLBACK):
45             return HandleUnRegisterCallBack(data, reply);
46         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_NFC_OPEN):
47             return HandleIsNfcOpen(data, reply);
48         case static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TAG_INTERFACE):
49             return HandleGetNfcTagInterface(data, reply);
50         default:
51             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52     }
53 }
54 
HandleGetState(MessageParcel & data,MessageParcel & reply)55 int NfcControllerStub::HandleGetState(MessageParcel& data, MessageParcel& reply)
56 {
57     int state = GetState();
58     reply.WriteInt32(state);
59     return ERR_NONE;
60 }
61 
HandleTurnOn(MessageParcel & data,MessageParcel & reply)62 int NfcControllerStub::HandleTurnOn(MessageParcel& data, MessageParcel& reply)
63 {
64     if (!PermissionTools::IsGranted(OHOS::NFC::SYS_PERM)) {
65         return KITS::ErrorCode::ERR_NO_PERMISSION;
66     }
67     return TurnOn();
68 }
69 
HandleTurnOff(MessageParcel & data,MessageParcel & reply)70 int NfcControllerStub::HandleTurnOff(MessageParcel& data, MessageParcel& reply)
71 {
72     if (!PermissionTools::IsGranted(OHOS::NFC::SYS_PERM)) {
73         return KITS::ErrorCode::ERR_NO_PERMISSION;
74     }
75     return TurnOff();
76 }
77 
HandleIsNfcOpen(MessageParcel & data,MessageParcel & reply)78 int NfcControllerStub::HandleIsNfcOpen(MessageParcel& data, MessageParcel& reply)
79 {
80     bool isOpen = false;
81     int statusCode = IsNfcOpen(isOpen);
82     reply.WriteBool(isOpen);
83     return statusCode;
84 }
85 
HandleRegisterCallBack(MessageParcel & data,MessageParcel & reply)86 int NfcControllerStub::HandleRegisterCallBack(MessageParcel &data, MessageParcel &reply)
87 {
88     InfoLog("datasize %{public}zu", data.GetRawDataSize());
89     std::string type = data.ReadString();
90     int exception = data.ReadInt32();
91     if (exception) {
92         return KITS::ERR_NFC_PARAMETERS;
93     }
94     KITS::ErrorCode ret = KITS::ERR_NFC_PARAMETERS;
95     do {
96         sptr<IRemoteObject> remote = data.ReadRemoteObject();
97         if (remote == nullptr) {
98             DebugLog("Failed to readRemoteObject!");
99             break;
100         }
101 
102         std::unique_ptr<NfcControllerDeathRecipient> recipient
103             = std::make_unique<NfcControllerDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
104         if (recipient == nullptr) {
105             ErrorLog("recipient is null");
106             return ERR_NONE;
107         }
108         sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
109         if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(dr))) {
110             ErrorLog("Failed to add death recipient");
111             return ERR_NONE;
112         }
113 
114         {
115             std::lock_guard<std::mutex> guard(mutex_);
116             deathRecipient_ = dr;
117             callback_ = iface_cast<INfcControllerCallback>(remote);
118             if (callback_ == nullptr) {
119                 callback_ = new (std::nothrow) NfcControllerCallBackProxy(remote);
120                 DebugLog("create new `NfcControllerCallBackProxy`!");
121             }
122             ret = RegisterCallBack(callback_, type);
123         }
124     } while (0);
125 
126     reply.WriteInt32(ret);
127     return ERR_NONE;
128 }
129 
RemoveNfcDeathRecipient(const wptr<IRemoteObject> & remote)130 void NfcControllerStub::RemoveNfcDeathRecipient(const wptr<IRemoteObject> &remote)
131 {
132     std::lock_guard<std::mutex> guard(mutex_);
133     if (callback_ == nullptr) {
134         ErrorLog("OnRemoteDied callback_ is nullptr");
135         return;
136     }
137     auto serviceRemote = callback_->AsObject();
138     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
139         serviceRemote->RemoveDeathRecipient(deathRecipient_);
140         callback_ = nullptr;
141         ErrorLog("on remote died");
142     }
143 }
144 
HandleUnRegisterCallBack(MessageParcel & data,MessageParcel & reply)145 int NfcControllerStub::HandleUnRegisterCallBack(MessageParcel &data, MessageParcel &reply)
146 {
147     InfoLog("OnUnRegisterCallBack");
148     std::string type = data.ReadString();
149     int exception = data.ReadInt32();
150     if (exception) {
151         return KITS::ERR_NFC_PARAMETERS;
152     }
153     KITS::ErrorCode ret = UnRegisterCallBack(type);
154     DebugLog("OnUnRegisterCallBack::OnUnRegisterCallBack end##ret=%{public}d\n", ret);
155     reply.WriteInt32(ret);
156     return ERR_NONE;
157 }
158 
RegisterCallBack(const sptr<INfcControllerCallback> & callback,const std::string & type)159 KITS::ErrorCode NfcControllerStub::RegisterCallBack(const sptr<INfcControllerCallback> &callback,
160     const std::string& type)
161 {
162     return RegisterCallBack(callback, type, IPCSkeleton::GetCallingTokenID());
163 }
164 
UnRegisterCallBack(const std::string & type)165 KITS::ErrorCode NfcControllerStub::UnRegisterCallBack(const std::string& type)
166 {
167     return UnRegisterCallBack(type, IPCSkeleton::GetCallingTokenID());
168 }
169 
HandleGetNfcTagInterface(MessageParcel & data,MessageParcel & reply)170 int NfcControllerStub::HandleGetNfcTagInterface(MessageParcel& data, MessageParcel& reply)
171 {
172     OHOS::sptr<IRemoteObject> remoteOjbect = GetTagServiceIface();
173     if (remoteOjbect == nullptr) {
174         ErrorLog("HandleGetNfcTagInterface remoteOjbect null!");
175         return KITS::ERR_NFC_PARAMETERS;
176     }
177 
178     reply.WriteRemoteObject(remoteOjbect);
179     return ERR_NONE;
180 }
181 }  // namespace NFC
182 }  // namespace OHOS
183