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_proxy.h"
16
17 #include "loghelper.h"
18 #include "nfc_controller_callback_stub.h"
19 #include "nfc_sdk_common.h"
20
21 namespace OHOS {
22 namespace NFC {
23 const std::string NFC_INTERFACE_TOKEN = "ohos.nfc.INfcController";
24 static NfcControllerCallBackStub* g_nfcControllerCallbackStub = new NfcControllerCallBackStub;
25
~NfcControllerProxy()26 NfcControllerProxy ::~NfcControllerProxy() {}
27
TurnOn()28 int NfcControllerProxy::TurnOn()
29 {
30 MessageParcel data;
31 MessageOption option;
32 if (!data.WriteInterfaceToken(GetDescriptor())) {
33 ErrorLog("Write interface token error");
34 return KITS::ERR_NFC_PARAMETERS;
35 }
36 return SendRequestExpectReplyNone(KITS::COMMAND_TURN_ON, data, option);
37 }
38
TurnOff()39 int NfcControllerProxy::TurnOff()
40 {
41 MessageParcel data;
42 if (!data.WriteInterfaceToken(GetDescriptor())) {
43 ErrorLog("Write interface token error");
44 return KITS::ERR_NFC_PARAMETERS;
45 }
46 MessageOption option;
47 return SendRequestExpectReplyNone(KITS::COMMAND_TURN_OFF, data, option);
48 }
49
GetState()50 int NfcControllerProxy::GetState()
51 {
52 int state = NFC::KITS::STATE_OFF;
53 MessageParcel data;
54 MessageOption option;
55 if (!data.WriteInterfaceToken(GetDescriptor())) {
56 ErrorLog("Write interface token error");
57 return KITS::ERR_NFC_PARAMETERS;
58 }
59 int res = SendRequestExpectReplyInt(KITS::COMMAND_GET_STATE, data, option, state);
60 if (res != ERR_NONE) {
61 ErrorLog("It is failed To Get State with Res(%{public}d).", res);
62 return NFC::KITS::STATE_OFF;
63 }
64 return state;
65 }
66
IsNfcOpen(bool & isOpen)67 int NfcControllerProxy::IsNfcOpen(bool &isOpen)
68 {
69 MessageParcel data;
70 MessageOption option;
71 if (!data.WriteInterfaceToken(GetDescriptor())) {
72 ErrorLog("Write interface token error");
73 return KITS::ERR_NFC_PARAMETERS;
74 }
75 return SendRequestExpectReplyBool(KITS::COMMAND_IS_NFC_OPEN, data, option, isOpen);
76 }
77
RegisterCallBack(const sptr<INfcControllerCallback> & callback,const std::string & type)78 KITS::ErrorCode NfcControllerProxy::RegisterCallBack(
79 const sptr<INfcControllerCallback> &callback,
80 const std::string& type)
81 {
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option(MessageOption::TF_SYNC);
85
86 g_nfcControllerCallbackStub->RegisterCallBack(callback);
87 if (!data.WriteInterfaceToken(GetDescriptor())) {
88 ErrorLog("Write interface token error");
89 return KITS::ERR_NFC_PARAMETERS;
90 }
91 if (!data.WriteString(type)) {
92 ErrorLog("Write type error");
93 return KITS::ERR_NFC_PARAMETERS;
94 }
95 data.WriteInt32(0);
96 if (!data.WriteRemoteObject(g_nfcControllerCallbackStub->AsObject())) {
97 ErrorLog("RegisterCallBack WriteRemoteObject failed!");
98 return KITS::ERR_NFC_PARAMETERS;
99 }
100
101 int error = SendRequestExpectReplyNone(KITS::COMMAND_REGISTER_CALLBACK, data, option);
102 if (error != ERR_NONE) {
103 ErrorLog("RegisterCallBack failed, error code is %{public}d", error);
104 return KITS::ERR_NFC_PARAMETERS;
105 }
106 return KITS::ERR_NONE;
107 }
108
UnRegisterCallBack(const std::string & type)109 KITS::ErrorCode NfcControllerProxy::UnRegisterCallBack(const std::string& type)
110 {
111 MessageParcel data;
112 MessageParcel reply;
113 MessageOption option(MessageOption::TF_SYNC);
114 if (!data.WriteInterfaceToken(GetDescriptor())) {
115 ErrorLog("Write interface token error");
116 return KITS::ERR_NFC_PARAMETERS;
117 }
118 if (!data.WriteString(type)) {
119 ErrorLog("Write type error");
120 return KITS::ERR_NFC_PARAMETERS;
121 }
122 data.WriteInt32(0);
123 int error = SendRequestExpectReplyNone(KITS::COMMAND_UNREGISTER_CALLBACK, data, option);
124 if (error != ERR_NONE) {
125 ErrorLog("RegisterCallBack failed, error code is %{public}d", error);
126 return KITS::ERR_NFC_PARAMETERS;
127 }
128 return KITS::ERR_NONE;
129 }
130
GetTagServiceIface()131 OHOS::sptr<IRemoteObject> NfcControllerProxy::GetTagServiceIface()
132 {
133 DebugLog("GetTagServiceIface start!");
134 MessageParcel reply;
135 MessageOption option(MessageOption::TF_SYNC);
136 MessageParcel data;
137 if (!data.WriteInterfaceToken(GetDescriptor())) {
138 ErrorLog("GetTagServiceIface, Write interface token error");
139 return nullptr;
140 }
141 int32_t res = Remote()->SendRequest(KITS::COMMAND_GET_TAG_INTERFACE, data, reply, option);
142 if (res != ERR_NONE) {
143 ErrorLog("GetTagServiceIface SendRequest err %{public}d", res);
144 return nullptr;
145 }
146 sptr<OHOS::IRemoteObject> remoteObject = reply.ReadRemoteObject();
147 return remoteObject;
148 }
149 } // namespace NFC
150 } // namespace OHOS
151