1/*
2 * Copyright (C) 2022 - 2023 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_proxy.h"
16
17#ifdef VENDOR_APPLICATIONS_ENABLED
18#include "iquery_app_info_callback.h"
19#endif
20
21#include "loghelper.h"
22#include "ndef_msg_callback_stub.h"
23#include "nfc_controller_callback_stub.h"
24#include "nfc_sdk_common.h"
25#include "nfc_service_ipc_interface_code.h"
26
27namespace OHOS {
28namespace NFC {
29const std::string NFC_INTERFACE_TOKEN = "ohos.nfc.INfcController";
30static sptr<NfcControllerCallBackStub> g_nfcControllerCallbackStub =
31    sptr<NfcControllerCallBackStub>(new (std::nothrow) NfcControllerCallBackStub());
32static sptr<NdefMsgCallbackStub> g_ndefMsgCallbackStub =
33    sptr<NdefMsgCallbackStub>(new (std::nothrow) NdefMsgCallbackStub());
34
35NfcControllerProxy ::~NfcControllerProxy() {}
36
37int NfcControllerProxy::TurnOn()
38{
39    MessageParcel data;
40    MessageParcel reply;
41    if (!data.WriteInterfaceToken(GetDescriptor())) {
42        ErrorLog("Write interface token error");
43        return KITS::ERR_NFC_PARAMETERS;
44    }
45    MessageOption option;
46    int statusCode = SendRequestExpectReplyNoneAndStatusCode(
47        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_TURN_ON), data, reply, option);
48    if (statusCode == ERR_NONE) {
49        statusCode = reply.ReadInt32();
50    }
51    InfoLog("NfcControllerProxy::TurnOn statusCode = 0x%{public}X", statusCode);
52    return statusCode;
53}
54
55int NfcControllerProxy::TurnOff()
56{
57    MessageParcel data;
58    MessageParcel reply;
59    if (!data.WriteInterfaceToken(GetDescriptor())) {
60        ErrorLog("Write interface token error");
61        return KITS::ERR_NFC_PARAMETERS;
62    }
63    MessageOption option;
64    int statusCode = SendRequestExpectReplyNoneAndStatusCode(
65        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_TURN_OFF), data, reply, option);
66    if (statusCode == ERR_NONE) {
67        statusCode = reply.ReadInt32();
68    }
69    InfoLog("NfcControllerProxy::TurnOff statusCode = 0x%{public}X", statusCode);
70    return statusCode;
71}
72
73int NfcControllerProxy::GetState()
74{
75    int state = NFC::KITS::STATE_OFF;
76    MessageParcel data;
77    MessageOption option;
78    if (!data.WriteInterfaceToken(GetDescriptor())) {
79        ErrorLog("Write interface token error");
80        return KITS::ERR_NFC_PARAMETERS;
81    }
82    int res = SendRequestExpectReplyInt(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_STATE),
83        data, option, state);
84    if (res != ERR_NONE) {
85        ErrorLog("It is failed To Get State with Res(%{public}d).", res);
86        return NFC::KITS::STATE_OFF;
87    }
88    return state;
89}
90
91int NfcControllerProxy::IsNfcOpen(bool &isOpen)
92{
93    MessageParcel data;
94    MessageOption option;
95    if (!data.WriteInterfaceToken(GetDescriptor())) {
96        ErrorLog("Write interface token error");
97        return KITS::ERR_NFC_PARAMETERS;
98    }
99    return SendRequestExpectReplyBool(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_IS_NFC_OPEN),
100        data, option, isOpen);
101}
102
103KITS::ErrorCode NfcControllerProxy::RegisterCallBack(
104    const sptr<INfcControllerCallback> &callback,
105    const std::string& type)
106{
107    MessageParcel data;
108    MessageParcel reply;
109    MessageOption option(MessageOption::TF_SYNC);
110    if (g_nfcControllerCallbackStub == nullptr) {
111        ErrorLog("%{public}s:g_nfcControllerCallbackStub is nullptr", __func__);
112        return KITS::ERR_NFC_PARAMETERS;
113    }
114    g_nfcControllerCallbackStub->RegisterCallBack(callback);
115    if (!data.WriteInterfaceToken(GetDescriptor())) {
116        ErrorLog("Write interface token error");
117        return KITS::ERR_NFC_PARAMETERS;
118    }
119    if (!data.WriteString(type)) {
120        ErrorLog("Write type error");
121        return KITS::ERR_NFC_PARAMETERS;
122    }
123    data.WriteInt32(0);
124    if (!data.WriteRemoteObject(g_nfcControllerCallbackStub->AsObject())) {
125        ErrorLog("RegisterCallBack WriteRemoteObject failed!");
126        return KITS::ERR_NFC_PARAMETERS;
127    }
128
129    int error = SendRequestExpectReplyNone(
130        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REGISTER_CALLBACK),
131        data, option);
132    if (error != ERR_NONE) {
133        ErrorLog("RegisterCallBack failed, error code is %{public}d", error);
134        return KITS::ERR_NFC_PARAMETERS;
135    }
136    return KITS::ERR_NONE;
137}
138
139KITS::ErrorCode NfcControllerProxy::UnRegisterCallBack(const std::string& type)
140{
141    MessageParcel data;
142    MessageParcel reply;
143    MessageOption option(MessageOption::TF_SYNC);
144    if (!data.WriteInterfaceToken(GetDescriptor())) {
145        ErrorLog("Write interface token error");
146        return KITS::ERR_NFC_PARAMETERS;
147    }
148    if (!data.WriteString(type)) {
149        ErrorLog("Write type error");
150        return KITS::ERR_NFC_PARAMETERS;
151    }
152    data.WriteInt32(0);
153    int error = SendRequestExpectReplyNone(
154        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_UNREGISTER_CALLBACK),
155        data, option);
156    if (error != ERR_NONE) {
157        ErrorLog("RegisterCallBack failed, error code is %{public}d", error);
158        return KITS::ERR_NFC_PARAMETERS;
159    }
160    return KITS::ERR_NONE;
161}
162
163OHOS::sptr<IRemoteObject> NfcControllerProxy::GetTagServiceIface()
164{
165    DebugLog("GetTagServiceIface start!");
166    MessageParcel reply;
167    MessageOption option(MessageOption::TF_SYNC);
168    MessageParcel data;
169    if (!data.WriteInterfaceToken(GetDescriptor())) {
170        ErrorLog("GetTagServiceIface, Write interface token error");
171        return nullptr;
172    }
173    int32_t res = Remote()->SendRequest(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_TAG_INTERFACE),
174        data, reply, option);
175    if (res != ERR_NONE) {
176        ErrorLog("GetTagServiceIface SendRequest err %{public}d", res);
177        return nullptr;
178    }
179    sptr<OHOS::IRemoteObject> remoteObject = reply.ReadRemoteObject();
180    return remoteObject;
181}
182
183KITS::ErrorCode NfcControllerProxy::RegNdefMsgCb(const sptr<INdefMsgCallback> &callback)
184{
185    MessageParcel data;
186    MessageParcel reply;
187    MessageOption option(MessageOption::TF_SYNC);
188    if (g_ndefMsgCallbackStub == nullptr) {
189        ErrorLog("NfcControllerProxy::RegNdefMsgCb:g_ndefMsgCallbackStub is nullptr");
190        return KITS::ERR_NFC_PARAMETERS;
191    }
192    g_ndefMsgCallbackStub->RegisterCallback(callback);
193    if (!data.WriteInterfaceToken(GetDescriptor())) {
194        ErrorLog("NfcControllerProxy::RegNdefMsgCb Write interface token error");
195        return KITS::ERR_NFC_PARAMETERS;
196    }
197    if (!data.WriteRemoteObject(g_ndefMsgCallbackStub->AsObject())) {
198        ErrorLog("NfcControllerProxy::RegNdefMsgCb WriteRemoteObject failed!");
199        return KITS::ERR_NFC_PARAMETERS;
200    }
201
202    int error = SendRequestExpectReplyNone(
203        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_REG_NDEF_MSG_CALLBACK),
204        data, option);
205    if (error != ERR_NONE) {
206        ErrorLog("NfcControllerProxy::RegNdefMsgCb failed, error code is %{public}d", error);
207        return KITS::ERR_NFC_PARAMETERS;
208    }
209    return KITS::ERR_NONE;
210}
211
212#ifdef VENDOR_APPLICATIONS_ENABLED
213KITS::ErrorCode NfcControllerProxy::RegQueryApplicationCb(sptr<IQueryAppInfoCallback> callback)
214{
215    MessageParcel data;
216    MessageParcel reply;
217    MessageOption option(MessageOption::TF_SYNC);
218    if (callback == nullptr) {
219        ErrorLog("NfcControllerProxy::RegQueryApplicationCb failed, callback is null.");
220        return KITS::ERR_NFC_PARAMETERS;
221    }
222    if (!data.WriteInterfaceToken(GetDescriptor())) {
223        ErrorLog("NfcControllerProxy::RegQueryApplicationCb failed, write interface token error.");
224        return KITS::ERR_NFC_PARAMETERS;
225    }
226    if (!data.WriteRemoteObject(callback->AsObject())) {
227        ErrorLog("NfcControllerProxy::RegQueryApplicationCb WriteRemoteObject failed!");
228        return KITS::ERR_NFC_PARAMETERS;
229    }
230    int error = SendRequestExpectReplyNone(
231        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_QUERY_APP_INFO_MSG_CALLBACK),
232        data, option);
233    if (error != ERR_NONE) {
234        ErrorLog("NfcControllerProxy::RegQueryApplicationCb failed, error code: %{public}d.", error);
235        return KITS::ERR_NFC_PARAMETERS;
236    }
237    return KITS::ERR_NONE;
238}
239
240KITS::ErrorCode NfcControllerProxy::RegCardEmulationNotifyCb(sptr<IOnCardEmulationNotifyCb> callback)
241{
242    MessageParcel data;
243    MessageParcel reply;
244    MessageOption option(MessageOption::TF_SYNC);
245    if (callback == nullptr) {
246        ErrorLog("NfcControllerProxy::RegCardEmulationNotifyCb failed, callback is null.");
247        return KITS::ERR_NFC_PARAMETERS;
248    }
249    if (!data.WriteInterfaceToken(GetDescriptor())) {
250        ErrorLog("NfcControllerProxy::RegCardEmulationNotifyCb failed, write interface token error.");
251        return KITS::ERR_NFC_PARAMETERS;
252    }
253    if (!data.WriteRemoteObject(callback->AsObject())) {
254        ErrorLog("NfcControllerProxy::RegCardEmulationNotifyCb WriteRemoteObject failed!");
255        return KITS::ERR_NFC_PARAMETERS;
256    }
257    int error = SendRequestExpectReplyNone(
258        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_ON_CARD_EMULATION_NOTIFY),
259        data, option);
260    if (error != ERR_NONE) {
261        ErrorLog("NfcControllerProxy::RegCardEmulationNotifyCb failed, error code: %{public}d.", error);
262        return KITS::ERR_NFC_PARAMETERS;
263    }
264    return KITS::ERR_NONE;
265}
266KITS::ErrorCode NfcControllerProxy::NotifyEventStatus(int eventType, int arg1, std::string arg2)
267{
268    MessageParcel data;
269    MessageParcel reply;
270    MessageOption option(MessageOption::TF_SYNC);
271    if (!data.WriteInterfaceToken(GetDescriptor())) {
272        ErrorLog("NfcControllerProxy::NotifyEventStatus failed, write interface token error.");
273        return KITS::ERR_NFC_PARAMETERS;
274    }
275    if (!data.WriteInt32(eventType)) {
276        ErrorLog("NfcControllerProxy::NotifyEventStatus Write event type failed!");
277        return KITS::ERR_NFC_PARAMETERS;
278    }
279    if (!data.WriteInt32(arg1)) {
280        ErrorLog("NfcControllerProxy::NotifyEventStatus Write arg1 failed!");
281        return KITS::ERR_NFC_PARAMETERS;
282    }
283
284    if (!data.WriteString(arg2)) {
285        ErrorLog("NfcControllerProxy::NotifyEventStatus Write arg2 failed!");
286        return KITS::ERR_NFC_PARAMETERS;
287    }
288    data.WriteInt32(0);
289    int error = SendRequestExpectReplyNone(
290        static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_VENDOR_NOTIFY),
291        data, option);
292    if (error != ERR_NONE) {
293        ErrorLog("NfcControllerProxy::NotifyEventStatus failed, error code: %{public}d.", error);
294        return KITS::ERR_NFC_PARAMETERS;
295    }
296    return KITS::ERR_NONE;
297}
298#endif
299
300OHOS::sptr<IRemoteObject> NfcControllerProxy::GetHceServiceIface()
301{
302    DebugLog("GetHceServiceIface start!");
303    MessageParcel reply;
304    MessageOption option(MessageOption::TF_SYNC);
305    MessageParcel data;
306    if (!data.WriteInterfaceToken(GetDescriptor())) {
307        ErrorLog("GetHceServiceIface, Write interface token error");
308        return nullptr;
309    }
310    int32_t res = Remote()->SendRequest(static_cast<uint32_t>(NfcServiceIpcInterfaceCode::COMMAND_GET_HCE_INTERFACE),
311        data, reply, option);
312    if (res != ERR_NONE) {
313        ErrorLog("GetHceServiceIface SendRequest err %{public}d", res);
314        return nullptr;
315    }
316    sptr<OHOS::IRemoteObject> remoteObject = reply.ReadRemoteObject();
317    return remoteObject;
318}
319}  // namespace NFC
320}  // namespace OHOS
321