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_impl.h"
16
17 #include "ipc_skeleton.h"
18 #include "nfc_controller_death_recipient.h"
19 #include "nfc_sdk_common.h"
20 #include "nfc_service.h"
21 #include "loghelper.h"
22 #include "external_deps_proxy.h"
23 #include "parameter.h"
24
25 namespace OHOS {
26 namespace NFC {
NfcControllerImpl(std::weak_ptr<NfcService> nfcService)27 NfcControllerImpl::NfcControllerImpl(std::weak_ptr<NfcService> nfcService)
28 : NfcControllerStub(), nfcService_(nfcService)
29 {
30 }
31
~NfcControllerImpl()32 NfcControllerImpl::~NfcControllerImpl()
33 {
34 }
35
CallbackEnter(uint32_t code)36 int32_t NfcControllerImpl::CallbackEnter(uint32_t code)
37 {
38 InfoLog("NfcControllerImpl, code[%{public}u]", code);
39 return ERR_NONE;
40 }
41
CallbackExit(uint32_t code,int32_t result)42 int32_t NfcControllerImpl::CallbackExit(uint32_t code, int32_t result)
43 {
44 InfoLog("NfcControllerImpl, code[%{public}u], result[%{public}d]", code, result);
45 return ERR_NONE;
46 }
47
GetState(int32_t & funcResult)48 ErrCode NfcControllerImpl::GetState(int32_t& funcResult)
49 {
50 funcResult = KITS::STATE_OFF;
51 if (nfcService_.expired()) {
52 ErrorLog("nfcService_ expired.");
53 return KITS::ERR_NFC_PARAMETERS;
54 }
55 funcResult = nfcService_.lock()->GetNfcState();
56 return KITS::ERR_NONE;
57 }
58
IsNfcEdmDisallowed()59 inline bool IsNfcEdmDisallowed()
60 {
61 const char* nfcEdmKey = "persist.edm.nfc_disable";
62 const uint32_t paramTrueLen = 4; // "true" 4 bytes
63 const uint32_t paramFalseLen = 5; // "false" 5 bytes
64 char result[paramFalseLen + 1] = {0};
65 // Returns the number of bytes of the system parameter if the operation is successful.
66 int len = GetParameter(nfcEdmKey, "false", result, paramFalseLen + 1);
67 if (len != paramFalseLen && len != paramTrueLen) {
68 ErrorLog("GetParameter edm len is invalid.");
69 return false;
70 }
71 if (strncmp(result, "true", paramTrueLen) == 0) {
72 WarnLog("nfc is prohibited by EDM. You won't be able to turn on nfc!");
73 return true;
74 }
75 return false;
76 }
77
TurnOn()78 ErrCode NfcControllerImpl::TurnOn()
79 {
80 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::SYS_PERM)) {
81 ErrorLog("TurnOn no permission");
82 return KITS::ERR_NO_PERMISSION;
83 }
84 std::string appPackageName = ExternalDepsProxy::GetInstance().GetBundleNameByUid(IPCSkeleton::GetCallingUid());
85 ExternalDepsProxy::GetInstance().WriteAppBehaviorHiSysEvent(SubErrorCode::TURN_ON_NFC, appPackageName);
86
87 if (IsNfcEdmDisallowed()) {
88 ErrorLog("nfc edm disallowed");
89 return KITS::ERR_NFC_EDM_DISALLOWED;
90 }
91 if (nfcService_.expired()) {
92 ErrorLog("nfcService_ expired.");
93 return KITS::ERR_NFC_PARAMETERS;
94 }
95 return nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_ON);
96 }
97
TurnOff()98 ErrCode NfcControllerImpl::TurnOff()
99 {
100 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::SYS_PERM)) {
101 ErrorLog("TurnOff no permission");
102 return KITS::ERR_NO_PERMISSION;
103 }
104 std::string appPackageName = ExternalDepsProxy::GetInstance().GetBundleNameByUid(IPCSkeleton::GetCallingUid());
105 ExternalDepsProxy::GetInstance().WriteAppBehaviorHiSysEvent(SubErrorCode::TURN_OFF_NFC, appPackageName);
106
107 if (nfcService_.expired()) {
108 ErrorLog("nfcService_ expired.");
109 return KITS::ERR_NFC_PARAMETERS;
110 }
111 return nfcService_.lock()->ExecuteTask(KITS::TASK_TURN_OFF);
112 }
113
RegisterNfcStatusCallBack(const sptr<INfcControllerCallback> & cb,const std::string & type)114 ErrCode NfcControllerImpl::RegisterNfcStatusCallBack(const sptr<INfcControllerCallback>& cb, const std::string& type)
115 {
116 if (cb == nullptr || cb->AsObject() == nullptr) {
117 ErrorLog("input callback nullptr.");
118 return KITS::ERR_NFC_PARAMETERS;
119 }
120 if (nfcService_.expired()) {
121 ErrorLog("nfcService_ expired.");
122 return KITS::ERR_NFC_PARAMETERS;
123 }
124
125 std::unique_ptr<NfcControllerDeathRecipient> recipient
126 = std::make_unique<NfcControllerDeathRecipient>(this, IPCSkeleton::GetCallingTokenID());
127 sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
128 if (!cb->AsObject()->AddDeathRecipient(dr)) {
129 ErrorLog("Failed to add death recipient");
130 return KITS::ERR_NFC_PARAMETERS;
131 }
132
133 std::lock_guard<std::mutex> guard(mutex_);
134 deathRecipient_ = dr;
135 callback_ = cb;
136 return nfcService_.lock()->SetRegisterCallBack(cb, type, IPCSkeleton::GetCallingTokenID());
137 }
138
UnregisterNfcStatusCallBack(const std::string & type)139 ErrCode NfcControllerImpl::UnregisterNfcStatusCallBack(const std::string& type)
140 {
141 if (nfcService_.expired()) {
142 ErrorLog("nfcService_ expired.");
143 return KITS::ERR_NFC_PARAMETERS;
144 }
145 return nfcService_.lock()->RemoveRegisterCallBack(type, IPCSkeleton::GetCallingTokenID());
146 }
147
UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken)148 KITS::ErrorCode NfcControllerImpl::UnRegisterAllCallBack(Security::AccessToken::AccessTokenID callerToken)
149 {
150 if (nfcService_.expired()) {
151 ErrorLog("nfcService_ expired.");
152 return KITS::ERR_NFC_PARAMETERS;
153 }
154 if (!nfcService_.lock()->RemoveAllRegisterCallBack(callerToken)) {
155 return KITS::ERR_NONE;
156 }
157 return KITS::ERR_NFC_PARAMETERS;
158 }
159
GetTagServiceIface(sptr<IRemoteObject> & funcResult)160 ErrCode NfcControllerImpl::GetTagServiceIface(sptr<IRemoteObject>& funcResult)
161 {
162 funcResult == nullptr;
163 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
164 ErrorLog("GetTagServiceIface no permission");
165 return KITS::ERR_NO_PERMISSION;
166 }
167
168 if (nfcService_.expired()) {
169 ErrorLog("nfcService_ expired.");
170 return KITS::ERR_NFC_PARAMETERS;
171 }
172 funcResult = nfcService_.lock()->GetTagServiceIface();
173 return KITS::ERR_NONE;
174 }
175
RegNdefMsgCb(const sptr<INdefMsgCallback> & cb)176 ErrCode NfcControllerImpl::RegNdefMsgCb(const sptr<INdefMsgCallback>& cb)
177 {
178 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::TAG_PERM)) {
179 ErrorLog("RegNdefMsgCb no permission");
180 return KITS::ERR_NO_PERMISSION;
181 }
182 if (cb == nullptr) {
183 ErrorLog("input callback nullptr.");
184 return KITS::ERR_NFC_PARAMETERS;
185 }
186 if (nfcService_.expired()) {
187 ErrorLog("nfcService_ expired");
188 return KITS::ERR_NFC_PARAMETERS;
189 }
190 if (nfcService_.lock()->RegNdefMsgCb(cb)) {
191 return KITS::ERR_NONE;
192 }
193 return KITS::ERR_NFC_PARAMETERS;
194 }
195
RegQueryApplicationCb(const sptr<IQueryAppInfoCallback> & cb)196 ErrCode NfcControllerImpl::RegQueryApplicationCb(const sptr<IQueryAppInfoCallback>& cb)
197 {
198 #ifdef VENDOR_APPLICATIONS_ENABLED
199 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
200 ErrorLog("RegQueryApplicationCb no permission");
201 return KITS::ERR_NO_PERMISSION;
202 }
203 ExternalDepsProxy::GetInstance().RegQueryApplicationCb(cb);
204 #endif
205 return KITS::ERR_NONE;
206 }
207
RegCardEmulationNotifyCb(const sptr<IOnCardEmulationNotifyCb> & cb)208 ErrCode NfcControllerImpl::RegCardEmulationNotifyCb(const sptr<IOnCardEmulationNotifyCb>& cb)
209 {
210 #ifdef VENDOR_APPLICATIONS_ENABLED
211 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
212 ErrorLog("RegCardEmulationNotifyCb no permission");
213 return KITS::ERR_NO_PERMISSION;
214 }
215 ExternalDepsProxy::GetInstance().RegCardEmulationNotifyCb(cb);
216 #endif
217 return KITS::ERR_NONE;
218 }
219
NotifyEventStatus(int32_t eventType,int32_t arg1,const std::string & arg2)220 ErrCode NfcControllerImpl::NotifyEventStatus(int32_t eventType, int32_t arg1, const std::string& arg2)
221 {
222 #ifdef VENDOR_APPLICATIONS_ENABLED
223 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
224 ErrorLog("NotifyEventStatus no permission");
225 return KITS::ERR_NO_PERMISSION;
226 }
227 if (nfcService_.expired()) {
228 ErrorLog("nfcService_ expired");
229 return KITS::ERR_NFC_PARAMETERS;
230 }
231
232 nfcService_.lock()->OnVendorEvent(eventType, arg1, arg2);
233 #endif
234 return KITS::ERR_NONE;
235 }
236
GetHceServiceIface(sptr<IRemoteObject> & funcResult)237 ErrCode NfcControllerImpl::GetHceServiceIface(sptr<IRemoteObject>& funcResult)
238 {
239 funcResult == nullptr;
240 if (!ExternalDepsProxy::GetInstance().IsGranted(OHOS::NFC::CARD_EMU_PERM)) {
241 ErrorLog("GetHceServiceIface no permission");
242 return KITS::ERR_NO_PERMISSION;
243 }
244 if (nfcService_.expired()) {
245 ErrorLog("nfcService_ expired");
246 return KITS::ERR_NFC_PARAMETERS;
247 }
248 funcResult = nfcService_.lock()->GetHceServiceIface();
249 return KITS::ERR_NONE;
250 }
251
RemoveNfcDeathRecipient(const wptr<IRemoteObject> & remote)252 void NfcControllerImpl::RemoveNfcDeathRecipient(const wptr<IRemoteObject> &remote)
253 {
254 std::lock_guard<std::mutex> guard(mutex_);
255 if (callback_ == nullptr) {
256 ErrorLog("OnRemoteDied callback_ is nullptr");
257 return;
258 }
259 auto serviceRemote = callback_->AsObject();
260 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
261 serviceRemote->RemoveDeathRecipient(deathRecipient_);
262 callback_ = nullptr;
263 ErrorLog("on remote died");
264 }
265 }
266 } // namespace NFC
267 } // namespace OHOS
268