1 /*
2 * Copyright (c) 2021-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
16 #include "ipc_types.h"
17 #include "message_parcel.h"
18 #include "securec.h"
19 #include "string_ex.h"
20 #include "usb_common.h"
21 #include "usb_errors.h"
22 #include "usb_request.h"
23 #include "usb_server_proxy.h"
24
25 using namespace OHOS::HDI::Usb::V1_0;
26 namespace OHOS {
27 namespace USB {
SetDeviceMessage(MessageParcel & data,uint8_t busNum,uint8_t devAddr)28 int32_t UsbServerProxy::SetDeviceMessage(MessageParcel &data, uint8_t busNum, uint8_t devAddr)
29 {
30 WRITE_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
31 WRITE_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
32 return UEC_OK;
33 }
34
SetBufferMessage(MessageParcel & data,const std::vector<uint8_t> & bufferData)35 int32_t UsbServerProxy::SetBufferMessage(MessageParcel &data, const std::vector<uint8_t> &bufferData)
36 {
37 uint32_t length = bufferData.size();
38 const uint8_t *ptr = bufferData.data();
39 if (!ptr) {
40 length = 0;
41 }
42
43 if (!data.WriteUint32(length)) {
44 USB_HILOGE(MODULE_USBD, "write length failed:%{public}u", length);
45 return UEC_SERVICE_WRITE_PARCEL_ERROR;
46 }
47 if ((ptr) && (length > 0) && !data.WriteBuffer(reinterpret_cast<const void *>(ptr), length)) {
48 USB_HILOGE(MODULE_USBD, "write buffer failed length:%{public}u", length);
49 return UEC_SERVICE_WRITE_PARCEL_ERROR;
50 }
51
52 USB_HILOGI(MODULE_USBD, "success length:%{public}u", length);
53 return UEC_OK;
54 }
55
GetBufferMessage(MessageParcel & data,std::vector<uint8_t> & bufferData)56 int32_t UsbServerProxy::GetBufferMessage(MessageParcel &data, std::vector<uint8_t> &bufferData)
57 {
58 uint32_t dataSize = 0;
59 bufferData.clear();
60 if (!data.ReadUint32(dataSize)) {
61 USB_HILOGE(MODULE_USBD, "read dataSize failed");
62 return UEC_SERVICE_READ_PARCEL_ERROR;
63 }
64 if (dataSize == 0) {
65 USB_HILOGI(MODULE_USBD, "invalid size:%{public}u", dataSize);
66 return UEC_OK;
67 }
68
69 const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
70 if (readData == nullptr) {
71 USB_HILOGE(MODULE_USBD, "failed size:%{public}u", dataSize);
72 return UEC_SERVICE_READ_PARCEL_ERROR;
73 }
74 std::vector<uint8_t> tdata(readData, readData + dataSize);
75 bufferData.swap(tdata);
76 return UEC_OK;
77 }
78
GetDevices(std::vector<UsbDevice> & deviceList)79 int32_t UsbServerProxy::GetDevices(std::vector<UsbDevice> &deviceList)
80 {
81 int32_t ret;
82 sptr<IRemoteObject> remote = Remote();
83 if (remote == nullptr) {
84 USB_HILOGE(MODULE_USB_INNERKIT, "remote is failed");
85 return ERR_INVALID_VALUE;
86 }
87 MessageParcel data;
88 MessageParcel reply;
89 MessageOption option;
90
91 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
92 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
93 return ERR_INVALID_VALUE;
94 }
95
96 ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_DEVICES), data, reply, option);
97 if (ret != UEC_OK) {
98 USB_HILOGE(MODULE_USB_INNERKIT, "failed code: %{public}d", ret);
99 return ret;
100 }
101 ret = GetDeviceListMessageParcel(reply, deviceList);
102 return ret;
103 }
104
GetDeviceListMessageParcel(MessageParcel & data,std::vector<UsbDevice> & deviceList)105 int32_t UsbServerProxy::GetDeviceListMessageParcel(MessageParcel &data, std::vector<UsbDevice> &deviceList)
106 {
107 int32_t count;
108 READ_PARCEL_WITH_RET(data, Int32, count, UEC_SERVICE_READ_PARCEL_ERROR);
109
110 for (int32_t i = 0; i < count; ++i) {
111 UsbDevice devInfo;
112 GetDeviceMessageParcel(data, devInfo);
113 deviceList.push_back(devInfo);
114 }
115 return UEC_OK;
116 }
117
GetDeviceMessageParcel(MessageParcel & data,UsbDevice & devInfo)118 int32_t UsbServerProxy::GetDeviceMessageParcel(MessageParcel &data, UsbDevice &devInfo)
119 {
120 int32_t tmp;
121 uint8_t tui8;
122 uint16_t tui16;
123 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
124 devInfo.SetBusNum(tmp);
125 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
126 devInfo.SetDevAddr(tmp);
127
128 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
129 devInfo.SetVendorId(tmp);
130 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
131 devInfo.SetProductId(tmp);
132 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
133 devInfo.SetClass(tmp);
134 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
135 devInfo.SetSubclass(tmp);
136 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
137 devInfo.SetProtocol(tmp);
138 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
139 devInfo.SetiManufacturer(tui8);
140 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
141 devInfo.SetiProduct(tui8);
142 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
143 devInfo.SetiSerialNumber(tui8);
144 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
145 devInfo.SetbMaxPacketSize0(tui8);
146 READ_PARCEL_WITH_RET(data, Uint16, tui16, UEC_SERVICE_READ_PARCEL_ERROR);
147 devInfo.SetbcdUSB(tui16);
148 READ_PARCEL_WITH_RET(data, Uint16, tui16, UEC_SERVICE_READ_PARCEL_ERROR);
149 devInfo.SetbcdDevice(tui16);
150 std::u16string tstr;
151 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
152 devInfo.SetName(Str16ToStr8(tstr));
153 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
154 devInfo.SetManufacturerName(Str16ToStr8(tstr));
155 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
156 devInfo.SetProductName(Str16ToStr8(tstr));
157 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
158 devInfo.SetVersion(Str16ToStr8(tstr));
159 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
160 devInfo.SetmSerial(Str16ToStr8(tstr));
161
162 USB_HILOGI(MODULE_USB_INNERKIT, "devName:%{public}s Bus:%{public}d dev:%{public}d ", devInfo.GetName().c_str(),
163 devInfo.GetBusNum(), devInfo.GetDevAddr());
164 std::vector<USBConfig> configs;
165 GetDeviceConfigsMessageParcel(data, configs);
166 devInfo.SetConfigs(configs);
167 USB_HILOGI(MODULE_USB_INNERKIT, "ToString : %{public}s", devInfo.ToString().c_str());
168 return UEC_OK;
169 }
170
GetDeviceConfigsMessageParcel(MessageParcel & data,std::vector<USBConfig> & configs)171 int32_t UsbServerProxy::GetDeviceConfigsMessageParcel(MessageParcel &data, std::vector<USBConfig> &configs)
172 {
173 uint32_t configCount;
174 uint8_t tui8;
175 std::u16string tstr;
176 data.ReadUint32(configCount);
177
178 int32_t tmp;
179 uint32_t attributes;
180 for (uint32_t i = 0; i < configCount; ++i) {
181 USBConfig config;
182 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
183 config.SetId(tmp);
184 READ_PARCEL_WITH_RET(data, Uint32, attributes, UEC_SERVICE_READ_PARCEL_ERROR);
185 config.SetAttribute(attributes);
186 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
187 config.SetMaxPower(tmp);
188
189 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
190 config.SetiConfiguration(tui8);
191 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
192 config.SetName(Str16ToStr8(tstr));
193
194 std::vector<UsbInterface> interfaces;
195 if (int32_t ret = GetDeviceInterfacesMessageParcel(data, interfaces); ret != UEC_OK) {
196 USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInterfacesMessageParcel failed ret:%{public}d", ret);
197 return ret;
198 }
199
200 config.SetInterfaces(interfaces);
201 configs.push_back(config);
202 USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", config.ToString().c_str());
203 }
204
205 return UEC_OK;
206 }
207
GetDeviceInterfacesMessageParcel(MessageParcel & data,std::vector<UsbInterface> & interfaces)208 int32_t UsbServerProxy::GetDeviceInterfacesMessageParcel(MessageParcel &data, std::vector<UsbInterface> &interfaces)
209 {
210 int32_t tmp;
211 int32_t interfaceCount;
212 uint8_t tui8;
213 std::u16string tstr;
214 data.ReadInt32(tmp);
215 interfaceCount = tmp;
216 for (int32_t i = 0; i < interfaceCount; ++i) {
217 UsbInterface interface;
218 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
219 interface.SetId(tmp);
220 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
221 interface.SetClass(tmp);
222 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
223 interface.SetSubClass(tmp);
224 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
225 interface.SetAlternateSetting(tmp);
226 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
227 interface.SetProtocol(tmp);
228
229 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
230 interface.SetiInterface(tui8);
231 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
232 interface.SetName(Str16ToStr8(tstr));
233
234 std::vector<USBEndpoint> eps;
235 if (int32_t ret = GetDeviceEndpointsMessageParcel(data, eps); ret != UEC_OK) {
236 USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceEndpointsMessageParcel failed ret:%{public}d", ret);
237 return ret;
238 }
239
240 for (size_t j = 0; j < eps.size(); ++j) {
241 eps[j].SetInterfaceId(interface.GetId());
242 }
243 interface.SetEndpoints(eps);
244 interfaces.push_back(interface);
245 USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", interface.ToString().c_str());
246 }
247 return UEC_OK;
248 }
249
GetDeviceEndpointsMessageParcel(MessageParcel & data,std::vector<USBEndpoint> & eps)250 int32_t UsbServerProxy::GetDeviceEndpointsMessageParcel(MessageParcel &data, std::vector<USBEndpoint> &eps)
251 {
252 int32_t tmp;
253 int32_t epCount;
254 uint32_t attributes;
255 uint32_t address;
256 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
257 epCount = tmp;
258 for (int32_t i = 0; i < epCount; ++i) {
259 USBEndpoint ep;
260 READ_PARCEL_WITH_RET(data, Uint32, address, UEC_SERVICE_READ_PARCEL_ERROR);
261 ep.SetAddr(address);
262 READ_PARCEL_WITH_RET(data, Uint32, attributes, UEC_SERVICE_READ_PARCEL_ERROR);
263 ep.SetAttr(attributes);
264 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
265 ep.SetInterval(tmp);
266 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
267 ep.SetMaxPacketSize(tmp);
268 eps.push_back(ep);
269 USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", ep.ToString().c_str());
270 }
271 return UEC_OK;
272 }
273
OpenDevice(uint8_t busNum,uint8_t devAddr)274 int32_t UsbServerProxy::OpenDevice(uint8_t busNum, uint8_t devAddr)
275 {
276 MessageParcel data;
277 MessageParcel reply;
278 MessageOption option;
279 sptr<IRemoteObject> remote = Remote();
280 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
281 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
282 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
283 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
284 }
285
286 int32_t ret = SetDeviceMessage(data, busNum, devAddr);
287 if (ret != UEC_OK) {
288 USB_HILOGE(MODULE_USB_INNERKIT, "SetDeviceMessage failed, ret:%{public}d", ret);
289 return ret;
290 }
291
292 ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_OPEN_DEVICE), data, reply, option);
293 if (ret != UEC_OK) {
294 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
295 }
296 return ret;
297 }
298
HasRight(std::string deviceName)299 bool UsbServerProxy::HasRight(std::string deviceName)
300 {
301 MessageParcel data;
302 MessageOption option;
303 MessageParcel reply;
304 sptr<IRemoteObject> remote = Remote();
305 RETURN_IF_WITH_RET(remote == nullptr, false);
306 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
307 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
308 return false;
309 }
310
311 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), false);
312 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_HAS_RIGHT), data, reply, option);
313 if (ret != UEC_OK) {
314 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
315 return false;
316 }
317
318 bool result = false;
319 READ_PARCEL_WITH_RET(reply, Bool, result, false);
320
321 return result;
322 }
323
RequestRight(std::string deviceName)324 int32_t UsbServerProxy::RequestRight(std::string deviceName)
325 {
326 MessageParcel reply;
327 MessageOption option;
328 MessageParcel data;
329 sptr<IRemoteObject> remote = Remote();
330 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
331 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
332 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
333 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
334 }
335 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), UEC_INTERFACE_WRITE_PARCEL_ERROR);
336 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_REQUEST_RIGHT), data, reply, option);
337 if (ret != UEC_OK) {
338 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
339 return ret;
340 }
341 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
342 return ret;
343 }
344
RemoveRight(std::string deviceName)345 int32_t UsbServerProxy::RemoveRight(std::string deviceName)
346 {
347 MessageParcel reply;
348 MessageOption option;
349 MessageParcel data;
350 sptr<IRemoteObject> remote = Remote();
351 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
352 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
353 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
354 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
355 }
356 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), UEC_INTERFACE_WRITE_PARCEL_ERROR);
357 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_REMOVE_RIGHT), data, reply, option);
358 if (ret != UEC_OK) {
359 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
360 return ret;
361 }
362 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
363 return ret;
364 }
365
GetCurrentFunctions(int32_t & funcs)366 int32_t UsbServerProxy::GetCurrentFunctions(int32_t &funcs)
367 {
368 sptr<IRemoteObject> remote = Remote();
369 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
370
371 MessageParcel data;
372 MessageParcel reply;
373 MessageOption option;
374
375 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
376 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
377 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
378 }
379
380 int32_t ret =
381 remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_CURRENT_FUNCTIONS), data, reply, option);
382 if (ret != UEC_OK) {
383 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %d", ret);
384 return ret;
385 }
386 READ_PARCEL_WITH_RET(reply, Int32, funcs, UEC_INTERFACE_READ_PARCEL_ERROR);
387 return ret;
388 }
389
SetCurrentFunctions(int32_t funcs)390 int32_t UsbServerProxy::SetCurrentFunctions(int32_t funcs)
391 {
392 sptr<IRemoteObject> remote = Remote();
393 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
394
395 MessageOption option;
396 MessageParcel data;
397 MessageParcel reply;
398
399 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
400 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
401 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
402 }
403 WRITE_PARCEL_WITH_RET(data, Int32, funcs, UEC_INTERFACE_WRITE_PARCEL_ERROR);
404 int32_t ret =
405 remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_SET_CURRENT_FUNCTIONS), data, reply, option);
406 if (ret != UEC_OK) {
407 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %d", ret);
408 }
409 return ret;
410 }
411
UsbFunctionsFromString(std::string_view funcs)412 int32_t UsbServerProxy::UsbFunctionsFromString(std::string_view funcs)
413 {
414 sptr<IRemoteObject> remote = Remote();
415 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
416 MessageOption option;
417 MessageParcel data;
418 MessageParcel reply;
419
420 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
421 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
422 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
423 }
424 WRITE_PARCEL_WITH_RET(data, String, std::string {funcs}, UEC_INTERFACE_WRITE_PARCEL_ERROR);
425 int32_t ret =
426 remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_USB_FUNCTIONS_FROM_STRING), data, reply, option);
427 if (ret != UEC_OK) {
428 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %d", ret);
429 return UEC_INTERFACE_INVALID_VALUE;
430 }
431 int32_t result = 0;
432 READ_PARCEL_WITH_RET(reply, Int32, result, INVALID_USB_INT_VALUE);
433 return result;
434 }
435
UsbFunctionsToString(int32_t funcs)436 std::string UsbServerProxy::UsbFunctionsToString(int32_t funcs)
437 {
438 sptr<IRemoteObject> remote = Remote();
439
440 MessageParcel data;
441 MessageOption option;
442 MessageParcel reply;
443
444 RETURN_IF_WITH_RET(remote == nullptr, INVALID_STRING_VALUE);
445
446 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
447 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
448 return INVALID_STRING_VALUE;
449 }
450 WRITE_PARCEL_WITH_RET(data, Int32, funcs, INVALID_STRING_VALUE);
451 int32_t ret =
452 remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_USB_FUNCTIONS_TO_STRING), data, reply, option);
453 if (ret != UEC_OK) {
454 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %d", ret);
455 return INVALID_STRING_VALUE;
456 }
457 std::string result;
458 READ_PARCEL_WITH_RET(reply, String, result, INVALID_STRING_VALUE);
459 return result;
460 }
461
GetPorts(std::vector<UsbPort> & ports)462 int32_t UsbServerProxy::GetPorts(std::vector<UsbPort> &ports)
463 {
464 MessageOption option;
465 sptr<IRemoteObject> remote = Remote();
466
467 MessageParcel data;
468 MessageParcel reply;
469 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
470 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
471 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
472 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
473 }
474 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_PORTS), data, reply, option);
475 if (ret != UEC_OK) {
476 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %d", ret);
477 return ret;
478 }
479 int32_t size;
480 READ_PARCEL_WITH_RET(reply, Int32, size, UEC_INTERFACE_READ_PARCEL_ERROR);
481 USB_HILOGI(MODULE_USB_INNERKIT, "GetPorts size %{public}d", size);
482 for (int32_t i = 0; i < size; ++i) {
483 USB_HILOGI(MODULE_USB_INNERKIT, "ParseUsbPort : %{public}d", i);
484 ret = ParseUsbPort(reply, ports);
485 if (ret) {
486 return ret;
487 }
488 }
489 return ret;
490 }
491
ParseUsbPort(MessageParcel & reply,std::vector<UsbPort> & ports)492 int32_t UsbServerProxy::ParseUsbPort(MessageParcel &reply, std::vector<UsbPort> &ports)
493 {
494 UsbPort port;
495 UsbPortStatus status;
496 READ_PARCEL_WITH_RET(reply, Int32, port.id, UEC_INTERFACE_READ_PARCEL_ERROR);
497 USB_HILOGI(MODULE_USB_INNERKIT, "UsbServerProxy::port->id %{public}d", port.id);
498 port.supportedModes = reply.ReadInt32();
499 status.currentMode = reply.ReadInt32();
500 status.currentPowerRole = reply.ReadInt32();
501 status.currentDataRole = reply.ReadInt32();
502 port.usbPortStatus = status;
503 USB_HILOGI(MODULE_USB_INNERKIT, "UsbServerProxy::port.usbPortStatus.currentMode %{public}d",
504 port.usbPortStatus.currentMode);
505 ports.push_back(port);
506 return UEC_OK;
507 }
508
GetSupportedModes(int32_t portId,int32_t & supportedModes)509 int32_t UsbServerProxy::GetSupportedModes(int32_t portId, int32_t &supportedModes)
510 {
511 MessageParcel data;
512 MessageParcel reply;
513 MessageOption option;
514 sptr<IRemoteObject> remote = Remote();
515 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
516 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
517 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
518 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
519 }
520 WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_INTERFACE_WRITE_PARCEL_ERROR);
521 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_SUPPORTED_MODES), data, reply, option);
522 if (ret) {
523 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %d", ret);
524 return ret;
525 }
526 READ_PARCEL_WITH_RET(reply, Int32, supportedModes, UEC_INTERFACE_READ_PARCEL_ERROR);
527 return ret;
528 }
529
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)530 int32_t UsbServerProxy::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
531 {
532 MessageParcel data;
533 MessageParcel reply;
534 MessageOption option;
535 sptr<IRemoteObject> remote = Remote();
536 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
537 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
538 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
539 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
540 }
541 WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_INTERFACE_WRITE_PARCEL_ERROR);
542 WRITE_PARCEL_WITH_RET(data, Int32, powerRole, UEC_INTERFACE_WRITE_PARCEL_ERROR);
543 WRITE_PARCEL_WITH_RET(data, Int32, dataRole, UEC_INTERFACE_WRITE_PARCEL_ERROR);
544 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_SET_PORT_ROLE), data, reply, option);
545 if (ret) {
546 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %d", ret);
547 return ret;
548 }
549 return ret;
550 }
551
ClaimInterface(uint8_t busNum,uint8_t devAddr,uint8_t interface,uint8_t force)552 int32_t UsbServerProxy::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t force)
553 {
554 sptr<IRemoteObject> remote = Remote();
555 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
556 MessageParcel data;
557 MessageParcel reply;
558 MessageOption option;
559 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
560 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
561 return ERR_ENOUGH_DATA;
562 }
563 SetDeviceMessage(data, busNum, devAddr);
564 WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
565 WRITE_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_WRITE_PARCEL_ERROR);
566 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_CLAIM_INTERFACE), data, reply, option);
567 if (ret != UEC_OK) {
568 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
569 return ret;
570 }
571 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
572 return ret;
573 }
574
ReleaseInterface(uint8_t busNum,uint8_t devAddr,uint8_t interface)575 int32_t UsbServerProxy::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface)
576 {
577 sptr<IRemoteObject> remote = Remote();
578 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
579 MessageParcel data;
580 MessageParcel reply;
581 MessageOption option;
582 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
583 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
584 return ERR_ENOUGH_DATA;
585 }
586 SetDeviceMessage(data, busNum, devAddr);
587 WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
588 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_RELEASE_INTERFACE), data, reply, option);
589 if (ret != UEC_OK) {
590 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %d", ret);
591 return ret;
592 }
593 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
594 return ret;
595 }
BulkTransferRead(const UsbDev & dev,const UsbPipe & pipe,std::vector<uint8_t> & bufferData,int32_t timeOut)596 int32_t UsbServerProxy::BulkTransferRead(
597 const UsbDev &dev, const UsbPipe &pipe, std::vector<uint8_t> &bufferData, int32_t timeOut)
598 {
599 sptr<IRemoteObject> remote = Remote();
600 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
601 MessageParcel data;
602 MessageParcel reply;
603 MessageOption option;
604 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
605 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
606 return ERR_ENOUGH_DATA;
607 }
608 SetDeviceMessage(data, dev.busNum, dev.devAddr);
609 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
610 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
611 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
612 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_BULK_TRANSFER_READ), data, reply, option);
613 if (ret != UEC_OK) {
614 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %d", ret);
615 return ret;
616 }
617 ret = GetBufferMessage(reply, bufferData);
618 if (ret != UEC_OK) {
619 USB_HILOGE(MODULE_USB_INNERKIT, "get buffer is failed, error code: %d", ret);
620 return ret;
621 }
622 USB_HILOGI(MODULE_USBD, "Set buffer message. length = %{public}zu", bufferData.size());
623 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
624 return ret;
625 }
BulkTransferWrite(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & bufferData,int32_t timeOut)626 int32_t UsbServerProxy::BulkTransferWrite(
627 const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData, int32_t timeOut)
628 {
629 sptr<IRemoteObject> remote = Remote();
630 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
631 MessageParcel data;
632 MessageParcel reply;
633 MessageOption option;
634 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
635 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
636 return ERR_ENOUGH_DATA;
637 }
638 SetDeviceMessage(data, dev.busNum, dev.devAddr);
639 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
640 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
641 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
642 int32_t ret = SetBufferMessage(data, bufferData);
643 if (UEC_OK != ret) {
644 USB_HILOGE(MODULE_INNERKIT, "SetBufferMessage ret:%{public}d", ret);
645 return ret;
646 }
647 ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_BULK_TRANSFER_WRITE), data, reply, option);
648 if (UEC_OK != ret) {
649 USB_HILOGE(MODULE_INNERKIT, "SendRequest ret:%{public}d", ret);
650 return ret;
651 }
652 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
653 return ret;
654 }
655
ControlTransfer(const UsbDev & dev,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & bufferData)656 int32_t UsbServerProxy::ControlTransfer(
657 const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
658 {
659 sptr<IRemoteObject> remote = Remote();
660 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
661 MessageParcel data;
662 MessageParcel reply;
663 MessageOption option;
664 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
665 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
666 return UEC_SERVICE_INNER_ERR;
667 }
668 SetDeviceMessage(data, dev.busNum, dev.devAddr);
669 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
670 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestCmd, UEC_SERVICE_WRITE_PARCEL_ERROR);
671 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.value, UEC_SERVICE_WRITE_PARCEL_ERROR);
672 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.index, UEC_SERVICE_WRITE_PARCEL_ERROR);
673 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
674 int32_t ret = SetBufferMessage(data, bufferData);
675 if (UEC_OK != ret) {
676 USB_HILOGE(MODULE_INNERKIT, "write failed! len:%{public}d", ret);
677 return ret;
678 }
679
680 uint32_t reqType = static_cast<uint32_t>(ctrl.requestType);
681 bool isWrite = ((reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
682 ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_CONTROL_TRANSFER), data, reply, option);
683 if (ret != UEC_OK) {
684 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_CONTROL_TRANSFER ret:%{public}d", ret);
685 return ret;
686 }
687 if (!isWrite) {
688 ret = GetBufferMessage(reply, bufferData);
689 if (UEC_OK != ret) {
690 USB_HILOGE(MODULE_USBD, "Get buffer message error. ret = %{public}d", ret);
691 return ret;
692 }
693 USB_HILOGI(MODULE_USBD, "Get buffer message. length = %{public}zu", bufferData.size());
694 }
695 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
696 return ret;
697 }
SetActiveConfig(uint8_t busNum,uint8_t devAddr,uint8_t configIndex)698 int32_t UsbServerProxy::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex)
699 {
700 sptr<IRemoteObject> remote = Remote();
701 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
702 MessageParcel data;
703 MessageParcel reply;
704 MessageOption option;
705 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
706 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
707 return ERR_ENOUGH_DATA;
708 }
709 SetDeviceMessage(data, busNum, devAddr);
710 WRITE_PARCEL_WITH_RET(data, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
711 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_SET_ACTIVE_CONFIG), data, reply, option);
712 if (UEC_OK != ret) {
713 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_SET_ACTIVE_CONFIG ret:%{public}d", ret);
714 return ret;
715 }
716 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
717 return ret;
718 }
GetActiveConfig(uint8_t busNum,uint8_t devAddr,uint8_t & configIndex)719 int32_t UsbServerProxy::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex)
720 {
721 sptr<IRemoteObject> remote = Remote();
722 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
723 MessageParcel data;
724 MessageParcel reply;
725 MessageOption option;
726 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
727 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
728 return ERR_ENOUGH_DATA;
729 }
730 SetDeviceMessage(data, busNum, devAddr);
731 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_ACTIVE_CONFIG), data, reply, option);
732 if (ret != UEC_OK) {
733 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_GET_ACTIVE_CONFIG ret:%{public}d", ret);
734 return ret;
735 }
736 READ_PARCEL_WITH_RET(reply, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
737 return ret;
738 }
SetInterface(uint8_t busNum,uint8_t devAddr,uint8_t interfaceid,uint8_t altIndex)739 int32_t UsbServerProxy::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex)
740 {
741 sptr<IRemoteObject> remote = Remote();
742 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
743 MessageParcel data;
744 MessageParcel reply;
745 MessageOption option;
746 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
747 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
748 return ERR_ENOUGH_DATA;
749 }
750 SetDeviceMessage(data, busNum, devAddr);
751 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
752 WRITE_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
753 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_SET_INTERFACE), data, reply, option);
754 if (UEC_OK != ret) {
755 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_SET_INTERFACE ret:%{public}d", ret);
756 return ret;
757 }
758 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
759 return ret;
760 }
GetRawDescriptor(uint8_t busNum,uint8_t devAddr,std::vector<uint8_t> & bufferData)761 int32_t UsbServerProxy::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData)
762 {
763 sptr<IRemoteObject> remote = Remote();
764 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
765 MessageParcel data;
766 MessageParcel reply;
767 MessageOption option;
768 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
769 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
770 return ERR_ENOUGH_DATA;
771 }
772 SetDeviceMessage(data, busNum, devAddr);
773 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_DESCRIPTOR), data, reply, option);
774 if (ret == UEC_OK) {
775 ret = GetBufferMessage(reply, bufferData);
776 if (UEC_OK != ret) {
777 USB_HILOGE(MODULE_INNERKIT, "get failed ret:%{public}d", ret);
778 }
779 }
780 return ret;
781 }
782
GetFileDescriptor(uint8_t busNum,uint8_t devAddr,int32_t & fd)783 int32_t UsbServerProxy::GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd)
784 {
785 sptr<IRemoteObject> remote = Remote();
786 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
787 MessageParcel data;
788 MessageParcel reply;
789 MessageOption option;
790 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
791 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
792 return ERR_ENOUGH_DATA;
793 }
794 SetDeviceMessage(data, busNum, devAddr);
795 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_GET_FILEDESCRIPTOR), data, reply, option);
796 if (ret == UEC_OK) {
797 READ_PARCEL_WITH_RET(reply, Int32, fd, UEC_INTERFACE_READ_PARCEL_ERROR);
798 }
799 return ret;
800 }
801
RequestQueue(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & clientData,const std::vector<uint8_t> & bufferData)802 int32_t UsbServerProxy::RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
803 const std::vector<uint8_t> &bufferData)
804 {
805 sptr<IRemoteObject> remote = Remote();
806 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
807 MessageParcel data;
808 MessageParcel reply;
809 MessageOption option;
810 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
811 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
812 return ERR_ENOUGH_DATA;
813 }
814 SetDeviceMessage(data, dev.busNum, dev.devAddr);
815 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
816 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
817
818 int32_t ret = UsbServerProxy::SetBufferMessage(data, clientData);
819 if (UEC_OK != ret) {
820 USB_HILOGE(MODULE_INNERKIT, "set clientData failed ret:%{public}d", ret);
821 return ERR_INVALID_VALUE;
822 }
823
824 ret = UsbServerProxy::SetBufferMessage(data, bufferData);
825 if (UEC_OK != ret) {
826 USB_HILOGE(MODULE_INNERKIT, "setBuffer failed ret:%{public}d", ret);
827 return ERR_INVALID_VALUE;
828 }
829
830 ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_REQUEST_QUEUE), data, reply, option);
831 if (ret != UEC_OK) {
832 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
833 return ret;
834 }
835 return ret;
836 }
837
RequestWait(const UsbDev & dev,int32_t timeOut,std::vector<uint8_t> & clientData,std::vector<uint8_t> & bufferData)838 int32_t UsbServerProxy::RequestWait(
839 const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData, std::vector<uint8_t> &bufferData)
840 {
841 sptr<IRemoteObject> remote = Remote();
842 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
843 MessageParcel data;
844 MessageParcel reply;
845 MessageOption option;
846
847 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
848 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
849 return ERR_ENOUGH_DATA;
850 }
851
852 SetDeviceMessage(data, dev.busNum, dev.devAddr);
853 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
854 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_REQUEST_WAIT), data, reply, option);
855 if (ret != UEC_OK) {
856 USB_HILOGE(MODULE_INNERKIT, "queue failed! ret:%{public}d", ret);
857 return ret;
858 }
859
860 ret = UsbServerProxy::GetBufferMessage(reply, clientData);
861 if (ret != UEC_OK) {
862 USB_HILOGE(MODULE_INNERKIT, "get clientData failed! ret:%{public}d", ret);
863 return ret;
864 }
865
866 ret = UsbServerProxy::GetBufferMessage(reply, bufferData);
867 if (ret != UEC_OK) {
868 USB_HILOGE(MODULE_INNERKIT, "get buffer failed! ret:%{public}d", ret);
869 return ret;
870 }
871
872 return ret;
873 }
874
RequestCancel(uint8_t busNum,uint8_t devAddr,uint8_t interfaceid,uint8_t endpointId)875 int32_t UsbServerProxy::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId)
876 {
877 int32_t ret;
878 sptr<IRemoteObject> remote = Remote();
879 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
880 MessageParcel data;
881 MessageParcel reply;
882 MessageOption option;
883 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
884 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
885 return ERR_ENOUGH_DATA;
886 }
887
888 SetDeviceMessage(data, busNum, devAddr);
889 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
890 WRITE_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
891 ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_REQUEST_CANCEL), data, reply, option);
892 if (ret != UEC_OK) {
893 USB_HILOGE(MODULE_INNERKIT, "request cancel failed!");
894 }
895
896 return ret;
897 }
898
Close(uint8_t busNum,uint8_t devAddr)899 int32_t UsbServerProxy::Close(uint8_t busNum, uint8_t devAddr)
900 {
901 sptr<IRemoteObject> remote = Remote();
902 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
903 MessageOption option;
904 MessageParcel data;
905 MessageParcel reply;
906 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
907 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
908 return ERR_ENOUGH_DATA;
909 }
910
911 SetDeviceMessage(data, busNum, devAddr);
912 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_CLOSE_DEVICE), data, reply, option);
913 if (ret != UEC_OK) {
914 USB_HILOGE(MODULE_INNERKIT, "queue failed!");
915 return ret;
916 }
917 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
918 return ret;
919 }
920
RegBulkCallback(const UsbDev & dev,const UsbPipe & pipe,const sptr<IRemoteObject> & cb)921 int32_t UsbServerProxy::RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb)
922 {
923 sptr<IRemoteObject> remote = Remote();
924 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
925 MessageParcel data;
926 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
927 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
928 return ERR_ENOUGH_DATA;
929 }
930 SetDeviceMessage(data, dev.busNum, dev.devAddr);
931 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
932 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
933 WRITE_PARCEL_WITH_RET(data, RemoteObject, cb, UEC_SERVICE_WRITE_PARCEL_ERROR);
934 MessageOption option;
935 MessageParcel reply;
936 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_REG_BULK_CALLBACK), data, reply, option);
937 if (ret != UEC_OK) {
938 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
939 return ret;
940 }
941 return ret;
942 }
943
UnRegBulkCallback(const UsbDev & dev,const UsbPipe & pipe)944 int32_t UsbServerProxy::UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe)
945 {
946 sptr<IRemoteObject> remote = Remote();
947 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
948 MessageParcel data;
949 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
950 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
951 return ERR_ENOUGH_DATA;
952 }
953 SetDeviceMessage(data, dev.busNum, dev.devAddr);
954 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
955 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
956 MessageOption option;
957 MessageParcel reply;
958 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_UNREG_BULK_CALLBACK), data, reply, option);
959 if (ret != UEC_OK) {
960 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
961 return ret;
962 }
963 return ret;
964 }
965
BulkRead(const UsbDev & dev,const UsbPipe & pipe,sptr<Ashmem> & ashmem)966 int32_t UsbServerProxy::BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
967 {
968 sptr<IRemoteObject> remote = Remote();
969 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
970 MessageParcel data;
971 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
972 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
973 return ERR_ENOUGH_DATA;
974 }
975 SetDeviceMessage(data, dev.busNum, dev.devAddr);
976 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
977 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
978 WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
979 MessageOption option;
980 MessageParcel reply;
981 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_BULK_AYSNC_READ), data, reply, option);
982 if (ret != UEC_OK) {
983 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
984 return ret;
985 }
986 return ret;
987 }
988
BulkWrite(const UsbDev & dev,const UsbPipe & pipe,sptr<Ashmem> & ashmem)989 int32_t UsbServerProxy::BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
990 {
991 sptr<IRemoteObject> remote = Remote();
992 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
993 MessageParcel data;
994 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
995 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
996 return ERR_ENOUGH_DATA;
997 }
998 SetDeviceMessage(data, dev.busNum, dev.devAddr);
999 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1000 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1001 WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
1002 MessageOption option;
1003 MessageParcel reply;
1004 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_BULK_AYSNC_WRITE), data, reply, option);
1005 if (ret != UEC_OK) {
1006 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1007 return ret;
1008 }
1009 return ret;
1010 }
1011
BulkCancel(const UsbDev & dev,const UsbPipe & pipe)1012 int32_t UsbServerProxy::BulkCancel(const UsbDev &dev, const UsbPipe &pipe)
1013 {
1014 sptr<IRemoteObject> remote = Remote();
1015 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1016 MessageParcel data;
1017 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1018 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1019 return ERR_ENOUGH_DATA;
1020 }
1021 SetDeviceMessage(data, dev.busNum, dev.devAddr);
1022 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1023 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1024 MessageOption option;
1025 MessageParcel reply;
1026 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_BULK_AYSNC_CANCEL), data, reply, option);
1027 if (ret != UEC_OK) {
1028 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1029 return ret;
1030 }
1031 return ret;
1032 }
1033
AddRight(const std::string & bundleName,const std::string & deviceName)1034 int32_t UsbServerProxy::AddRight(const std::string &bundleName, const std::string &deviceName)
1035 {
1036 sptr<IRemoteObject> remote = Remote();
1037 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1038
1039 MessageParcel data;
1040 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1041 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1042 return ERR_ENOUGH_DATA;
1043 }
1044 WRITE_PARCEL_WITH_RET(data, String, bundleName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1045 WRITE_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1046
1047 MessageOption option;
1048 MessageParcel reply;
1049 int32_t ret = remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_ADD_RIGHT), data, reply, option);
1050 if (ret != UEC_OK) {
1051 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %d", ret);
1052 }
1053 return ret;
1054 }
1055 } // namespace USB
1056 } // namespace OHOS
1057