• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tag_session_stub.h"
16 
17 #include "loghelper.h"
18 #include "nfc_sdk_common.h"
19 #include "permission_tools.h"
20 
21 namespace OHOS {
22 namespace NFC {
23 namespace TAG {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int TagSessionStub::OnRemoteRequest(uint32_t code,         /* [in] */
25                                     MessageParcel& data,   /* [in] */
26                                     MessageParcel& reply,  /* [out] */
27                                     MessageOption& option) /* [in] */
28 {
29     DebugLog("TagSessionStub OnRemoteRequest occur, code is %d", code);
30     if (data.ReadInterfaceToken() != GetDescriptor()) {
31         ErrorLog("TagSessionStub OnRemoteRequest GetDescriptor failed");
32         return KITS::ErrorCode::ERR_TAG_PARAMETERS;
33     }
34 
35     switch (code) {
36         case KITS::COMMAND_CONNECT:
37             return HandleConnect(data, reply);
38         case KITS::COMMAND_RECONNECT:
39             return HandleReconnect(data, reply);
40         case KITS::COMMAND_DISCONNECT:
41             return HandleDisconnect(data, reply);
42         case KITS::COMMAND_SET_TIMEOUT:
43             return HandleSetTimeout(data, reply);
44         case KITS::COMMAND_GET_TIMEOUT:
45             return HandleGetTimeout(data, reply);
46         case KITS::COMMAND_GET_TECHLIST:
47             return HandleGetTechList(data, reply);
48         case KITS::COMMAND_IS_PRESENT:
49             return HandleIsTagFieldOn(data, reply);
50         case KITS::COMMAND_IS_NDEF:
51             return HandleIsNdef(data, reply);
52         case KITS::COMMAND_SEND_RAW_FRAME:
53             return HandleSendRawFrame(data, reply);
54         case KITS::COMMAND_NDEF_READ:
55             return HandleNdefRead(data, reply);
56         case KITS::COMMAND_NDEF_WRITE:
57             return HandleNdefWrite(data, reply);
58         case KITS::COMMAND_NDEF_MAKE_READ_ONLY:
59             return HandleNdefMakeReadOnly(data, reply);
60         case KITS::COMMAND_FORMAT_NDEF:
61             return HandleFormatNdef(data, reply);
62         case KITS::COMMAND_CAN_MAKE_READ_ONLY:
63             return HandleCanMakeReadOnly(data, reply);
64         case KITS::COMMAND_GET_MAX_TRANSCEIVE_LENGTH:
65             return HandleGetMaxTransceiveLength(data, reply);
66         case KITS::COMMAND_IS_SUPPORTED_APDUS_EXTENDED:
67             return HandleIsSupportedApdusExtended(data, reply);
68         default:
69             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70     }
71 }
HandleConnect(MessageParcel & data,MessageParcel & reply)72 int TagSessionStub::HandleConnect(MessageParcel& data, MessageParcel& reply)
73 {
74     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
75         ErrorLog("HandleConnect, ERR_NO_PERMISSION");
76         return KITS::ErrorCode::ERR_NO_PERMISSION;
77     }
78 
79     int tagRfDiscId = data.ReadInt32();
80     int tech = data.ReadInt32();
81     int statusCode = Connect(tagRfDiscId, tech);
82     reply.WriteInt32(statusCode);
83     return statusCode;
84 }
HandleReconnect(MessageParcel & data,MessageParcel & reply)85 int TagSessionStub::HandleReconnect(MessageParcel& data, MessageParcel& reply)
86 {
87     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
88         ErrorLog("HandleReconnect, ERR_NO_PERMISSION");
89         return KITS::ErrorCode::ERR_NO_PERMISSION;
90     }
91 
92     int tagRfDiscId = data.ReadInt32();
93     int statusCode = Reconnect(tagRfDiscId);
94     reply.WriteInt32(statusCode);
95     return statusCode;
96 }
HandleDisconnect(MessageParcel & data,MessageParcel & reply)97 int TagSessionStub::HandleDisconnect(MessageParcel& data, MessageParcel& reply)
98 {
99     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
100         ErrorLog("HandleDisconnect, ERR_NO_PERMISSION");
101         return KITS::ErrorCode::ERR_NO_PERMISSION;
102     }
103 
104     int tagRfDiscId = data.ReadInt32();
105     Disconnect(tagRfDiscId);
106     return ERR_NONE;
107 }
HandleSetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)108 int TagSessionStub::HandleSetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
109 {
110     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
111         ErrorLog("HandleSetTimeout, ERR_NO_PERMISSION");
112         return KITS::ErrorCode::ERR_NO_PERMISSION;
113     }
114     int tech = data.ReadInt32();
115     int timeout = data.ReadInt32();
116     int statusCode = SetTimeout(timeout, tech);
117     reply.WriteInt32(statusCode);
118     return statusCode;
119 }
HandleGetTimeout(OHOS::MessageParcel & data,OHOS::MessageParcel & reply)120 int TagSessionStub::HandleGetTimeout(OHOS::MessageParcel& data, OHOS::MessageParcel& reply)
121 {
122     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
123         ErrorLog("HandleGetTimeout, ERR_NO_PERMISSION");
124         return KITS::ErrorCode::ERR_NO_PERMISSION;
125     }
126     int timeout = 0;
127     int tech = data.ReadInt32();
128     int statusCode = GetTimeout(tech, timeout);
129     reply.WriteInt32(timeout);
130     return statusCode;
131 }
HandleGetTechList(MessageParcel & data,MessageParcel & reply)132 int TagSessionStub::HandleGetTechList(MessageParcel& data, MessageParcel& reply)
133 {
134     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
135         ErrorLog("HandleGetTechList, ERR_NO_PERMISSION");
136         return KITS::ErrorCode::ERR_NO_PERMISSION;
137     }
138 
139     int tagRfDiscId = data.ReadInt32();
140     std::vector<int32_t> techList = GetTechList(tagRfDiscId);
141     reply.WriteInt32Vector(techList);
142     return ERR_NONE;
143 }
HandleIsTagFieldOn(MessageParcel & data,MessageParcel & reply)144 int TagSessionStub::HandleIsTagFieldOn(MessageParcel& data, MessageParcel& reply)
145 {
146     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
147         ErrorLog("HandleIsTagFieldOn, ERR_NO_PERMISSION");
148         return KITS::ErrorCode::ERR_NO_PERMISSION;
149     }
150     int tagRfDiscId = data.ReadInt32();
151     reply.WriteBool(IsNdef(tagRfDiscId));
152     return ERR_NONE;
153 }
HandleIsNdef(MessageParcel & data,MessageParcel & reply)154 int TagSessionStub::HandleIsNdef(MessageParcel& data, MessageParcel& reply)
155 {
156     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
157         ErrorLog("HandleIsNdef, ERR_NO_PERMISSION");
158         return KITS::ErrorCode::ERR_NO_PERMISSION;
159     }
160     int tagRfDiscId = data.ReadInt32();
161     reply.WriteBool(IsNdef(tagRfDiscId));
162     return ERR_NONE;
163 }
HandleSendRawFrame(MessageParcel & data,MessageParcel & reply)164 int TagSessionStub::HandleSendRawFrame(MessageParcel& data, MessageParcel& reply)
165 {
166     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
167         ErrorLog("HandleSendRawFrame, ERR_NO_PERMISSION");
168         return KITS::ErrorCode::ERR_NO_PERMISSION;
169     }
170 
171     int tagRfDiscId = data.ReadInt32();
172     std::string hexCmdData = data.ReadString();
173     bool raw = data.ReadBool();
174     std::string hexRespData;
175     int statusCode = SendRawFrame(tagRfDiscId, hexCmdData, raw, hexRespData);
176     reply.WriteString(hexRespData);
177     return statusCode;
178 }
HandleNdefRead(MessageParcel & data,MessageParcel & reply)179 int TagSessionStub::HandleNdefRead(MessageParcel& data, MessageParcel& reply)
180 {
181     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
182         ErrorLog("HandleNdefRead, ERR_NO_PERMISSION");
183         return KITS::ErrorCode::ERR_NO_PERMISSION;
184     }
185 
186     int tagRfDiscId = data.ReadInt32();
187     std::string readData = NdefRead(tagRfDiscId);
188     reply.WriteString(readData);
189     return ERR_NONE;
190 }
HandleNdefWrite(MessageParcel & data,MessageParcel & reply)191 int TagSessionStub::HandleNdefWrite(MessageParcel& data, MessageParcel& reply)
192 {
193     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
194         ErrorLog("HandleNdefWrite, ERR_NO_PERMISSION");
195         return KITS::ErrorCode::ERR_NO_PERMISSION;
196     }
197 
198     int tagRfDiscId = data.ReadInt32();
199     std::string msg = data.ReadString();
200     int status = NdefWrite(tagRfDiscId, msg);
201     reply.WriteInt32(status);
202     return ERR_NONE;
203 }
HandleNdefMakeReadOnly(MessageParcel & data,MessageParcel & reply)204 int TagSessionStub::HandleNdefMakeReadOnly(MessageParcel& data, MessageParcel& reply)
205 {
206     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
207         ErrorLog("HandleNdefMakeReadOnly, ERR_NO_PERMISSION");
208         return KITS::ErrorCode::ERR_NO_PERMISSION;
209     }
210 
211     int tagRfDiscId = data.ReadInt32();
212     reply.WriteInt32(NdefMakeReadOnly(tagRfDiscId));
213     return ERR_NONE;
214 }
HandleFormatNdef(MessageParcel & data,MessageParcel & reply)215 int TagSessionStub::HandleFormatNdef(MessageParcel& data, MessageParcel& reply)
216 {
217     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
218         ErrorLog("HandleFormatNdef, ERR_NO_PERMISSION");
219         return KITS::ErrorCode::ERR_NO_PERMISSION;
220     }
221 
222     int tagRfDiscId = data.ReadInt32();
223     std::string key = data.ReadString();
224     reply.WriteInt32(FormatNdef(tagRfDiscId, key));
225     return ERR_NONE;
226 }
HandleCanMakeReadOnly(MessageParcel & data,MessageParcel & reply)227 int TagSessionStub::HandleCanMakeReadOnly(MessageParcel& data, MessageParcel& reply)
228 {
229     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
230         ErrorLog("HandleCanMakeReadOnly, ERR_NO_PERMISSION");
231         return KITS::ErrorCode::ERR_NO_PERMISSION;
232     }
233     int ndefType = data.ReadInt32();
234     bool canSetReadOnly = false;
235     int statusCode = CanMakeReadOnly(ndefType, canSetReadOnly);
236     reply.WriteBool(canSetReadOnly);
237     return statusCode;
238 }
HandleGetMaxTransceiveLength(MessageParcel & data,MessageParcel & reply)239 int TagSessionStub::HandleGetMaxTransceiveLength(MessageParcel& data, MessageParcel& reply)
240 {
241     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
242         ErrorLog("HandleGetMaxTransceiveLength, ERR_NO_PERMISSION");
243         return KITS::ErrorCode::ERR_NO_PERMISSION;
244     }
245     int maxSize = 0;
246     int tech = data.ReadInt32();
247     int statusCode = GetMaxTransceiveLength(tech, maxSize);
248     reply.WriteInt32(maxSize);
249     return statusCode;
250 }
HandleIsSupportedApdusExtended(MessageParcel & data,MessageParcel & reply)251 int TagSessionStub::HandleIsSupportedApdusExtended(MessageParcel& data, MessageParcel& reply)
252 {
253     if (!PermissionTools::IsGranted(OHOS::NFC::TAG_PERM)) {
254         ErrorLog("HandleIsSupportedApdusExtended, ERR_NO_PERMISSION");
255         return KITS::ErrorCode::ERR_NO_PERMISSION;
256     }
257     bool isSupported = false;
258     int statusCode = IsSupportedApdusExtended(isSupported);
259     reply.WriteBool(isSupported);
260     return statusCode;
261 }
262 }  // namespace TAG
263 }  // namespace NFC
264 }  // namespace OHOS
265