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