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