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