1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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
16 #include "nfc_controller_callback_stub.h"
17
18 #include "nfc_sdk_common.h"
19 #include "loghelper.h"
20
21 namespace OHOS {
22 namespace NFC {
NfcControllerCallBackStub()23 NfcControllerCallBackStub::NfcControllerCallBackStub() : callback_(nullptr), mRemoteDied(false)
24 {}
25
~NfcControllerCallBackStub()26 NfcControllerCallBackStub::~NfcControllerCallBackStub()
27 {}
28
GetInstance()29 NfcControllerCallBackStub& NfcControllerCallBackStub::GetInstance()
30 {
31 static NfcControllerCallBackStub NfcControllerCallBackStub;
32 return NfcControllerCallBackStub;
33 }
34
OnNfcStateChanged(int nfcRfState)35 void NfcControllerCallBackStub::OnNfcStateChanged(int nfcRfState)
36 {
37 if (callback_) {
38 DebugLog("NfcControllerCallBackStub callback_");
39 callback_->OnNfcStateChanged(nfcRfState);
40 }
41 }
42
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)43 int NfcControllerCallBackStub::OnRemoteRequest(
44 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
45 {
46 DebugLog("NfcControllerCallBackStub::OnRemoteRequest,code = %{public}d", code);
47 if (mRemoteDied) {
48 return KITS::ERR_NFC_STATE_UNBIND;
49 }
50 if (data.ReadInterfaceToken() != GetDescriptor()) {
51 ErrorLog("nfc callback stub token verification error");
52 return KITS::ERR_NFC_PARAMETERS;
53 }
54 int exception = data.ReadInt32();
55 if (exception) {
56 ErrorLog("ConnectedTagCallBackStub::OnRemoteRequest, got exception: (%{public}d))", exception);
57 return exception;
58 }
59 int ret = KITS::ERR_NFC_STATE_UNBIND;
60 switch (code) {
61 case KITS::COMMAND_ON_NOTIFY: {
62 ret = RemoteNfcStateChanged(data, reply);
63 break;
64 }
65 default: {
66 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 break;
68 }
69 }
70 return ret;
71 }
72
RegisterCallBack(const sptr<INfcControllerCallback> & callBack)73 KITS::ErrorCode NfcControllerCallBackStub::RegisterCallBack(const sptr<INfcControllerCallback> &callBack)
74 {
75 DebugLog("NfcControllerCallBackStub RegisterCallBack");
76 std::shared_lock<std::shared_mutex> guard(callbackMutex);
77 if (callBack == nullptr) {
78 ErrorLog("RegisterUserCallBack:callBack is nullptr!");
79 callback_ = callBack;
80 return KITS::ERR_NFC_PARAMETERS;
81 }
82 callback_ = callBack;
83 return KITS::ERR_NONE;
84 }
85
RemoteNfcStateChanged(MessageParcel & data,MessageParcel & reply)86 int NfcControllerCallBackStub::RemoteNfcStateChanged(MessageParcel &data, MessageParcel &reply)
87 {
88 InfoLog("run %{public}zu datasize ", data.GetRawDataSize());
89 int state = data.ReadInt32();
90 std::shared_lock<std::shared_mutex> guard(callbackMutex);
91 OnNfcStateChanged(state);
92 reply.WriteInt32(KITS::ERR_NONE); /* Reply 0 to indicate that no exception occurs. */
93 return KITS::ERR_NONE;
94 }
95 } // namespace NFC
96 } // namespace OHOS