1 /*
2 * Copyright (c) 2021 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 "usbd_client.h"
17 #include "hdf_log.h"
18 #include "iservmgr_hdi.h"
19 #include "message_parcel.h"
20 #include "usb_errors.h"
21 #include "usbd_type.h"
22
23 #define HDF_LOG_TAG UsbdClient
24
25 namespace OHOS {
26 namespace USB {
27 using OHOS::HDI::ServiceManager::V1_0::IServiceManager;
28
29 namespace {
30 const std::string USBD_SERVICE = "usbd";
31
32 #define WRITE_PARCEL_WITH_RET(parcel, type, data, retval) \
33 do { \
34 if (!(parcel).Write##type(data)) { \
35 HDF_LOGE("%{public}s write " #data " failed", __func__); \
36 return (retval); \
37 } \
38 } while (0)
39
40 #define READ_PARCEL_WITH_RET(parcel, type, out, retval) \
41 do { \
42 if (!(parcel).Read##type(out)) { \
43 HDF_LOGE("%{public}s read " #out " failed", __func__); \
44 return (retval); \
45 } \
46 } while (0)
47 } // namespace
48
GetInstance()49 UsbdClient &UsbdClient::GetInstance()
50 {
51 static UsbdClient instance;
52 return instance;
53 }
54
GetUsbdService()55 sptr<IRemoteObject> UsbdClient::GetUsbdService()
56 {
57 auto serviceManager = IServiceManager::Get();
58 if (serviceManager == nullptr) {
59 HDF_LOGE("service manager is nullptr");
60 return nullptr;
61 }
62 auto usbdService = serviceManager->GetService(USBD_SERVICE.c_str());
63 if (usbdService == nullptr) {
64 HDF_LOGE("Usbd service is nullptr");
65 return nullptr;
66 }
67 return usbdService;
68 }
69
BindUsbdSubscriber(const sptr<UsbdSubscriber> & subscriber)70 int32_t UsbdClient::BindUsbdSubscriber(const sptr<UsbdSubscriber> &subscriber)
71 {
72 if (subscriber == nullptr) {
73 HDF_LOGE("subscriber is nullptr");
74 return UEC_SERVICE_INVALID_VALUE;
75 }
76
77 MessageParcel data;
78 MessageParcel reply;
79
80 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
81 HDF_LOGE(" WriteInterfaceToken failed.");
82 return UEC_HDF_FAILURE;
83 }
84
85 data.WriteRemoteObject(subscriber);
86 return DoDispatch(CMD_BIND_USB_SUBSCRIBER, data, reply);
87 }
88
UnbindUsbdSubscriber(const sptr<UsbdSubscriber> & subscriber)89 int32_t UsbdClient::UnbindUsbdSubscriber(const sptr<UsbdSubscriber> &subscriber)
90 {
91 MessageParcel data;
92 MessageParcel reply;
93
94 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
95 HDF_LOGE(" WriteInterfaceToken failed.");
96 return UEC_HDF_FAILURE;
97 }
98
99 data.WriteRemoteObject(subscriber);
100 return DoDispatch(CMD_UNBIND_USB_SUBSCRIBER, data, reply);
101 }
102
OpenDevice(const UsbDev & dev)103 int32_t UsbdClient::OpenDevice(const UsbDev &dev)
104 {
105 MessageParcel data;
106 MessageParcel reply;
107
108 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
109 HDF_LOGE(" WriteInterfaceToken failed.");
110 return UEC_HDF_FAILURE;
111 }
112
113 UsbdClient::SetDeviceMessage(data, dev);
114 int32_t ret = DoDispatch(CMD_FUN_OPEN_DEVICE, data, reply);
115 if (ret != UEC_OK) {
116 HDF_LOGE("UsbdClient::%{public}s:%{public}d OpenDevice failed ret:%{public}d bus:%{public}d dev:%{public}d",
117 __func__, __LINE__, ret, dev.busNum, dev.devAddr);
118 }
119 return ret;
120 }
121
GetCurrentFunctions(int32_t & funcs)122 int32_t UsbdClient::GetCurrentFunctions(int32_t &funcs)
123 {
124 MessageParcel data;
125 MessageParcel reply;
126
127 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
128 HDF_LOGE(" WriteInterfaceToken failed.");
129 return UEC_HDF_FAILURE;
130 }
131
132 int32_t ret = DoDispatch(CMD_FUN_GET_CURRENT_FUNCTIONS, data, reply);
133 if (FAILED(ret)) {
134 HDF_LOGE("CMD_FUN_GET_CURRENT_FUNCTIONS failed, return INVALID_USB_INT_VALUE");
135 return ret;
136 }
137 READ_PARCEL_WITH_RET(reply, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
138 return UEC_OK;
139 }
140
SetCurrentFunctions(int32_t funcs)141 int32_t UsbdClient::SetCurrentFunctions(int32_t funcs)
142 {
143 MessageParcel data;
144 MessageParcel reply;
145
146 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
147 HDF_LOGE(" WriteInterfaceToken failed.");
148 return UEC_HDF_FAILURE;
149 }
150
151 WRITE_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_WRITE_PARCEL_ERROR);
152 int32_t ret = DoDispatch(CMD_FUN_SET_CURRENT_FUNCTIONS, data, reply);
153 if (FAILED(ret)) {
154 HDF_LOGE("CMD_FUN_SET_CURRENT_FUNCTIONS failed");
155 return ret;
156 }
157 return UEC_OK;
158 }
159
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)160 int32_t UsbdClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
161 {
162 MessageParcel data;
163 MessageParcel reply;
164
165 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
166 HDF_LOGE(" WriteInterfaceToken failed.");
167 return UEC_HDF_FAILURE;
168 }
169
170 WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_WRITE_PARCEL_ERROR);
171 WRITE_PARCEL_WITH_RET(data, Int32, powerRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
172 WRITE_PARCEL_WITH_RET(data, Int32, dataRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
173
174 int32_t ret = DoDispatch(CMD_SET_ROLE, data, reply);
175 if (FAILED(ret)) {
176 HDF_LOGE("CMD_SET_ROLE failed, return INVALID_STRING_VALUE");
177 return ret;
178 }
179 return UEC_OK;
180 }
181
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)182 int32_t UsbdClient::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
183 {
184 MessageParcel data;
185 MessageParcel reply;
186
187 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
188 HDF_LOGE(" WriteInterfaceToken failed.");
189 return UEC_HDF_FAILURE;
190 }
191
192 int32_t ret = DoDispatch(CMD_QUERY_PORT, data, reply);
193 READ_PARCEL_WITH_RET(reply, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
194 READ_PARCEL_WITH_RET(reply, Int32, powerRole, UEC_SERVICE_READ_PARCEL_ERROR);
195 READ_PARCEL_WITH_RET(reply, Int32, dataRole, UEC_SERVICE_READ_PARCEL_ERROR);
196 READ_PARCEL_WITH_RET(reply, Int32, mode, UEC_SERVICE_READ_PARCEL_ERROR);
197 if (FAILED(ret)) {
198 HDF_LOGE("CMD_QUERY_PORT failed, return INVALID_STRING_VALUE");
199 return ret;
200 }
201 return UEC_OK;
202 }
203
DoDispatch(uint32_t cmd,MessageParcel & data,MessageParcel & reply)204 int32_t UsbdClient::DoDispatch(uint32_t cmd, MessageParcel &data, MessageParcel &reply)
205 {
206 auto usbd = GetUsbdService();
207 if (usbd == nullptr) {
208 HDF_LOGE(" get usbd service failed.");
209 return UEC_SERVICE_NO_INIT;
210 }
211
212 MessageOption option;
213 auto ret = usbd->SendRequest(cmd, data, reply, option);
214 if (ret != UEC_OK) {
215 HDF_LOGE("failed to send request, cmd: %{public}d, ret: %{public}d", cmd, ret);
216 return ret;
217 }
218 return UEC_OK;
219 }
220
GetRawDescriptor(const UsbDev & dev,std::vector<uint8_t> & decriptor)221 int32_t UsbdClient::GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor)
222 {
223 MessageParcel data;
224 MessageParcel reply;
225
226 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
227 HDF_LOGE(" WriteInterfaceToken failed.");
228 return UEC_HDF_FAILURE;
229 }
230
231 UsbdClient::SetDeviceMessage(data, dev);
232 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_GET_DESCRIPTOR, data, reply);
233 if (ret != UEC_OK) {
234 HDF_LOGE("%{public}s:%{public}d DoDispatch failed:%{public}d", __func__, __LINE__, ret);
235 return ret;
236 }
237 ret = UsbdClient::GetBufferMessage(reply, decriptor);
238 if (ret != UEC_OK) {
239 HDF_LOGE("%{public}s:%{public}d GetBufferMessage failed:%{public}d", __func__, __LINE__, ret);
240 }
241 return ret;
242 }
243
GetFileDescriptor(const UsbDev & dev,int32_t & fd)244 int32_t UsbdClient::GetFileDescriptor(const UsbDev &dev, int32_t &fd)
245 {
246 MessageParcel data;
247 MessageParcel reply;
248
249 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
250 HDF_LOGE(" WriteInterfaceToken failed.");
251 return UEC_HDF_FAILURE;
252 }
253
254 UsbdClient::SetDeviceMessage(data, dev);
255 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_GET_FILEDESCRIPTOR, data, reply);
256 if (ret != UEC_OK) {
257 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
258 return ret;
259 }
260 READ_PARCEL_WITH_RET(reply, Int32, fd, UEC_SERVICE_READ_PARCEL_ERROR);
261 return ret;
262 }
263
GetDeviceDescriptor(const UsbDev & dev,std::vector<uint8_t> & decriptor)264 int32_t UsbdClient::GetDeviceDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor)
265 {
266 MessageParcel data;
267 MessageParcel reply;
268
269 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
270 HDF_LOGE(" WriteInterfaceToken failed.");
271 return UEC_HDF_FAILURE;
272 }
273
274 UsbdClient::SetDeviceMessage(data, dev);
275 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_GET_DEVICE_DESCRIPTOR, data, reply);
276 if (ret != UEC_OK) {
277 HDF_LOGE("%{public}s failed", __func__);
278 return ret;
279 }
280 ret = UsbdClient::GetBufferMessage(reply, decriptor);
281 return ret;
282 }
283
GetStringDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & decriptor)284 int32_t UsbdClient::GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor)
285 {
286 MessageParcel data;
287 MessageParcel reply;
288
289 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
290 HDF_LOGE(" WriteInterfaceToken failed.");
291 return UEC_HDF_FAILURE;
292 }
293
294 UsbdClient::SetDeviceMessage(data, dev);
295 WRITE_PARCEL_WITH_RET(data, Uint8, descId, UEC_SERVICE_WRITE_PARCEL_ERROR);
296 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_GET_STRING_DESCRIPTOR, data, reply);
297 if (ret != UEC_OK) {
298 HDF_LOGE("%{public}s:%{public}d strId:%{public}d failed:%{public}d", __func__, __LINE__, descId, ret);
299 return ret;
300 }
301 ret = UsbdClient::GetBufferMessage(reply, decriptor);
302 if (ret != UEC_OK) {
303 HDF_LOGE("%{public}s:%{public}d strId:%{public}d failed:%{public}d", __func__, __LINE__, descId, ret);
304 }
305 return ret;
306 }
307
GetConfigDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & decriptor)308 int32_t UsbdClient::GetConfigDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor)
309 {
310 MessageParcel data;
311 MessageParcel reply;
312
313 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
314 HDF_LOGE(" WriteInterfaceToken failed.");
315 return UEC_HDF_FAILURE;
316 }
317
318 UsbdClient::SetDeviceMessage(data, dev);
319 WRITE_PARCEL_WITH_RET(data, Uint8, descId, UEC_SERVICE_WRITE_PARCEL_ERROR);
320 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_GET_CONFIG_DESCRIPTOR, data, reply);
321 if (ret != UEC_OK) {
322 HDF_LOGE("%{public}s:%{public}d cfgId:%{public}d failed:%{public}d", __func__, __LINE__, descId, ret);
323 return ret;
324 }
325 ret = UsbdClient::GetBufferMessage(reply, decriptor);
326 if (ret != UEC_OK) {
327 HDF_LOGE("%{public}s:%{public}d strId:%{public}d failed:%{public}d", __func__, __LINE__, descId, ret);
328 }
329 return ret;
330 }
331
SetConfig(const UsbDev & dev,uint8_t configIndex)332 int32_t UsbdClient::SetConfig(const UsbDev &dev, uint8_t configIndex)
333 {
334 MessageParcel data;
335 MessageParcel reply;
336
337 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
338 HDF_LOGE(" WriteInterfaceToken failed.");
339 return UEC_HDF_FAILURE;
340 }
341
342 UsbdClient::SetDeviceMessage(data, dev);
343 WRITE_PARCEL_WITH_RET(data, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
344 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SET_CONFIG, data, reply);
345 if (ret != UEC_OK) {
346 HDF_LOGE("%{public}s failed", __func__);
347 }
348 return ret;
349 }
350
GetConfig(const UsbDev & dev,uint8_t & configIndex)351 int32_t UsbdClient::GetConfig(const UsbDev &dev, uint8_t &configIndex)
352 {
353 MessageParcel data;
354 MessageParcel reply;
355
356 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
357 HDF_LOGE(" WriteInterfaceToken failed.");
358 return UEC_HDF_FAILURE;
359 }
360
361 UsbdClient::SetDeviceMessage(data, dev);
362 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_GET_CONFIG, data, reply);
363 if (ret != UEC_OK) {
364 HDF_LOGE("%{public}s failed", __func__);
365 return ret;
366 }
367 READ_PARCEL_WITH_RET(reply, Uint8, configIndex, UEC_SERVICE_READ_PARCEL_ERROR);
368 return ret;
369 }
370
ClaimInterface(const UsbDev & dev,uint8_t interfaceIndex,uint8_t force)371 int32_t UsbdClient::ClaimInterface(const UsbDev &dev, uint8_t interfaceIndex, uint8_t force)
372 {
373 MessageParcel data;
374 MessageParcel reply;
375
376 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
377 HDF_LOGE(" WriteInterfaceToken failed.");
378 return UEC_HDF_FAILURE;
379 }
380
381 UsbdClient::SetDeviceMessage(data, dev);
382 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
383 WRITE_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_WRITE_PARCEL_ERROR);
384 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_CLAIM_INTERFACE, data, reply);
385 if (ret != UEC_OK) {
386 HDF_LOGE("%{public}s failed", __func__);
387 }
388 return ret;
389 }
390
ReleaseInterface(const UsbDev & dev,uint8_t interfaceIndex)391 int32_t UsbdClient::ReleaseInterface(const UsbDev &dev, uint8_t interfaceIndex)
392 {
393 MessageParcel data;
394 MessageParcel reply;
395
396 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
397 HDF_LOGE(" WriteInterfaceToken failed.");
398 return UEC_HDF_FAILURE;
399 }
400
401 UsbdClient::SetDeviceMessage(data, dev);
402 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
403 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_RELEASE_INTERFACE, data, reply);
404 if (ret != UEC_OK) {
405 HDF_LOGE("%{public}s failed", __func__);
406 }
407 return ret;
408 }
409
SetInterface(const UsbDev & dev,uint8_t interfaceIndex,uint8_t altIndex)410 int32_t UsbdClient::SetInterface(const UsbDev &dev, uint8_t interfaceIndex, uint8_t altIndex)
411 {
412 MessageParcel data;
413 MessageParcel reply;
414
415 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
416 HDF_LOGE(" WriteInterfaceToken failed.");
417 return UEC_HDF_FAILURE;
418 }
419
420 UsbdClient::SetDeviceMessage(data, dev);
421 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
422 WRITE_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
423 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SET_INTERFACE, data, reply);
424 if (ret != UEC_OK) {
425 HDF_LOGE("%{public}s failed", __func__);
426 }
427 return ret;
428 }
429
BulkTransferRead(const UsbDev & devInfo,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & bufferData)430 int32_t UsbdClient::BulkTransferRead(const UsbDev &devInfo, const UsbPipe &pipe, int32_t timeout,
431 std::vector<uint8_t> &bufferData)
432 {
433 MessageParcel data;
434 MessageParcel reply;
435
436 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
437 HDF_LOGE(" WriteInterfaceToken failed.");
438 return UEC_HDF_FAILURE;
439 }
440
441 SetDeviceMessage(data, devInfo);
442 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
443 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
444 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
445 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SEND_BULK_READ_SYNC, data, reply);
446 if (ret != UEC_OK) {
447 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
448 return ret;
449 }
450 ret = UsbdClient::GetBufferMessage(reply, bufferData);
451 if (ret != UEC_OK) {
452 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
453 }
454 return ret;
455 }
456
BulkTransferWrite(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,const std::vector<uint8_t> & bufferData)457 int32_t UsbdClient::BulkTransferWrite(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout,
458 const std::vector<uint8_t> &bufferData)
459 {
460 MessageParcel data;
461 MessageParcel reply;
462
463 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
464 HDF_LOGE(" WriteInterfaceToken failed.");
465 return UEC_HDF_FAILURE;
466 }
467
468 UsbdClient::SetDeviceMessage(data, dev);
469 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
470 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
471 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
472 int32_t ret = SetBufferMessage(data, bufferData);
473 if (ret != UEC_OK) {
474 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
475 return ret;
476 }
477 ret = UsbdClient::DoDispatch(CMD_FUN_SEND_BULK_WRITE_SYNC, data, reply);
478 if (ret != UEC_OK) {
479 HDF_LOGE("%{public}s failed", __func__);
480 }
481 return ret;
482 }
483
ControlTransfer(const UsbDev & dev,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & bufferData)484 int32_t UsbdClient::ControlTransfer(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
485 {
486 int32_t ret;
487 MessageParcel data;
488 MessageParcel reply;
489
490 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
491 HDF_LOGE(" WriteInterfaceToken failed.");
492 return UEC_HDF_FAILURE;
493 }
494
495 UsbdClient::SetDeviceMessage(data, dev);
496 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
497 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestCmd, UEC_SERVICE_WRITE_PARCEL_ERROR);
498 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.value, UEC_SERVICE_WRITE_PARCEL_ERROR);
499 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.index, UEC_SERVICE_WRITE_PARCEL_ERROR);
500 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
501 bool isWrite = (ctrl.requestType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT;
502 if (isWrite) {
503 ret = SetBufferMessage(data, bufferData);
504 if (ret != UEC_OK) {
505 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
506 return ret;
507 }
508 }
509 ret = UsbdClient::DoDispatch(CMD_FUN_SEND_CTRL_REQUEST_SYNC, data, reply);
510 if (ret != UEC_OK) {
511 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
512 return ret;
513 }
514 if (!isWrite) {
515 ret = GetBufferMessage(reply, bufferData);
516 if (ret != UEC_OK) {
517 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
518 }
519 }
520 return ret;
521 }
522
InterruptTransferRead(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & bufferData)523 int32_t UsbdClient::InterruptTransferRead(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout,
524 std::vector<uint8_t> &bufferData)
525 {
526 MessageParcel data;
527 MessageParcel reply;
528
529 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
530 HDF_LOGE(" WriteInterfaceToken failed.");
531 return UEC_HDF_FAILURE;
532 }
533
534 UsbdClient::SetDeviceMessage(data, dev);
535 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
536 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
537 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
538 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SEND_INTERRUPT_READ_SYNC, data, reply);
539 if (ret != UEC_OK) {
540 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
541 return ret;
542 }
543
544 ret = GetBufferMessage(reply, bufferData);
545 if (ret != UEC_OK) {
546 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
547 }
548 return ret;
549 }
550
InterruptTransferWrite(const UsbDev & dev,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & bufferData)551 int32_t UsbdClient::InterruptTransferWrite(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout,
552 std::vector<uint8_t> &bufferData)
553 {
554 MessageParcel data;
555 MessageParcel reply;
556
557 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
558 HDF_LOGE(" WriteInterfaceToken failed.");
559 return UEC_HDF_FAILURE;
560 }
561
562 UsbdClient::SetDeviceMessage(data, dev);
563 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
564 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
565 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
566 int32_t ret = SetBufferMessage(data, bufferData);
567 if (ret != UEC_OK) {
568 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
569 return ret;
570 }
571 ret = UsbdClient::DoDispatch(CMD_FUN_SEND_INTERRUPT_WRITE_SYNC, data, reply);
572 if (ret != UEC_OK) {
573 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
574 }
575 return ret;
576 }
577
IsoTransferRead(const UsbDev & devInfo,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & bufferData)578 int32_t UsbdClient::IsoTransferRead(const UsbDev &devInfo, const UsbPipe &pipe, int32_t timeout,
579 std::vector<uint8_t> &bufferData)
580 {
581 MessageParcel data;
582 MessageParcel reply;
583
584 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
585 HDF_LOGE(" WriteInterfaceToken failed.");
586 return UEC_HDF_FAILURE;
587 }
588
589 SetDeviceMessage(data, devInfo);
590 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
591 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
592 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
593 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SEND_ISO_READ_SYNC, data, reply);
594 if (ret != UEC_OK) {
595 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
596 return ret;
597 }
598
599 ret = GetBufferMessage(reply, bufferData);
600 if (ret != UEC_OK) {
601 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
602 }
603 return ret;
604 }
605
IsoTransferWrite(const UsbDev & devInfo,const UsbPipe & pipe,int32_t timeout,std::vector<uint8_t> & bufferData)606 int32_t UsbdClient::IsoTransferWrite(const UsbDev &devInfo, const UsbPipe &pipe, int32_t timeout,
607 std::vector<uint8_t> &bufferData)
608 {
609 MessageParcel data;
610 MessageParcel reply;
611
612 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
613 HDF_LOGE(" WriteInterfaceToken failed.");
614 return UEC_HDF_FAILURE;
615 }
616
617 SetDeviceMessage(data, devInfo);
618 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
619 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
620 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
621 int32_t ret = SetBufferMessage(data, bufferData);
622 if (ret != UEC_OK) {
623 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
624 return ret;
625 }
626 ret = UsbdClient::DoDispatch(CMD_FUN_SEND_ISO_WRITE_SYNC, data, reply);
627 if (ret != UEC_OK) {
628 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
629 }
630 return ret;
631 }
632
CloseDevice(const UsbDev & dev)633 int32_t UsbdClient::CloseDevice(const UsbDev &dev)
634 {
635 MessageParcel data;
636 MessageParcel reply;
637
638 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
639 HDF_LOGE(" WriteInterfaceToken failed.");
640 return UEC_HDF_FAILURE;
641 }
642
643 UsbdClient::SetDeviceMessage(data, dev);
644 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_CLOSE_DEVICE, data, reply);
645 if (ret != UEC_OK) {
646 HDF_LOGE("%{public}s:%{public}d CloseDevice failed ret:%{public}d bus:%{public}d dev:%{public}d", __func__,
647 __LINE__, ret, dev.busNum, dev.devAddr);
648 }
649 return ret;
650 }
651
RequestQueue(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & clientData,const std::vector<uint8_t> & buffer)652 int32_t UsbdClient::RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
653 const std::vector<uint8_t> &buffer)
654 {
655 MessageParcel data;
656 MessageParcel reply;
657
658 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
659 HDF_LOGE(" WriteInterfaceToken failed.");
660 return UEC_HDF_FAILURE;
661 }
662
663 UsbdClient::SetDeviceMessage(data, dev);
664 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
665 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
666
667 int32_t ret = UsbdClient::SetBufferMessage(data, clientData);
668 if (ret != UEC_OK) {
669 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
670 return ret;
671 }
672 ret = UsbdClient::SetBufferMessage(data, buffer);
673 if (ret != UEC_OK) {
674 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
675 return ret;
676 }
677
678 ret = UsbdClient::DoDispatch(CMD_FUN_REQUEST_QUEUE, data, reply);
679 if (ret != UEC_OK) {
680 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
681 }
682 return ret;
683 }
684
RequestWait(const UsbDev & dev,std::vector<uint8_t> & clientData,std::vector<uint8_t> & buffer,int32_t timeout)685 int32_t UsbdClient::RequestWait(const UsbDev &dev, std::vector<uint8_t> &clientData, std::vector<uint8_t> &buffer,
686 int32_t timeout)
687 {
688 MessageParcel data;
689 MessageParcel reply;
690
691 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
692 HDF_LOGE(" WriteInterfaceToken failed.");
693 return UEC_HDF_FAILURE;
694 }
695
696 UsbdClient::SetDeviceMessage(data, dev);
697 WRITE_PARCEL_WITH_RET(data, Int32, timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
698 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_REQUEST_WAIT, data, reply);
699 if (ret != UEC_OK) {
700 HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret);
701 return ret;
702 }
703
704 ret = UsbdClient::GetBufferMessage(reply, clientData);
705 if (ret != UEC_OK) {
706 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
707 return ret;
708 }
709
710 ret = UsbdClient::GetBufferMessage(reply, buffer);
711 if (ret != UEC_OK) {
712 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
713 }
714
715 return ret;
716 }
717
RequestCancel(const UsbDev & dev,const UsbPipe & pipe)718 int32_t UsbdClient::RequestCancel(const UsbDev &dev, const UsbPipe &pipe)
719 {
720 MessageParcel data;
721 MessageParcel reply;
722
723 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
724 HDF_LOGE(" WriteInterfaceToken failed.");
725 return UEC_HDF_FAILURE;
726 }
727
728 UsbdClient::SetDeviceMessage(data, dev);
729 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
730 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
731 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_REQUEST_CANCEL, data, reply);
732 if (ret != UEC_OK) {
733 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
734 }
735 return ret;
736 }
737
SetBufferMessage(MessageParcel & data,const std::vector<uint8_t> & bufferData)738 int32_t UsbdClient::SetBufferMessage(MessageParcel &data, const std::vector<uint8_t> &bufferData)
739 {
740 uint32_t length = bufferData.size();
741 const uint8_t *ptr = bufferData.data();
742 if (ptr == nullptr) {
743 length = 0;
744 }
745
746 if (!data.WriteUint32(length)) {
747 HDF_LOGE("%{public}s:%{public}d failed length:%{public}d", __func__, __LINE__, length);
748 return UEC_SERVICE_WRITE_PARCEL_ERROR;
749 }
750 if ((ptr != nullptr) && (length > 0) && !data.WriteBuffer(ptr, length)) {
751 HDF_LOGE("%{public}s:%{public}d failed length:%{public}d", __func__, __LINE__, length);
752 return UEC_SERVICE_WRITE_PARCEL_ERROR;
753 }
754 return UEC_OK;
755 }
756
GetBufferMessage(MessageParcel & data,std::vector<uint8_t> & bufferData)757 int32_t UsbdClient::GetBufferMessage(MessageParcel &data, std::vector<uint8_t> &bufferData)
758 {
759 uint32_t dataSize = 0;
760 bufferData.clear();
761 if (!data.ReadUint32(dataSize)) {
762 HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__);
763 return UEC_SERVICE_READ_PARCEL_ERROR;
764 }
765 if (dataSize == 0) {
766 HDF_LOGE("%{public}s:%{public}d size:%{public}d", __func__, __LINE__, dataSize);
767 return UEC_OK;
768 }
769
770 const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
771 if (readData == nullptr) {
772 HDF_LOGE("%{public}s:%{public}d failed size:%{public}d", __func__, __LINE__, dataSize);
773 return UEC_SERVICE_READ_PARCEL_ERROR;
774 }
775 std::vector<uint8_t> tData(readData, readData + dataSize);
776 bufferData.swap(tData);
777
778 return UEC_OK;
779 }
780
SetDeviceMessage(MessageParcel & data,const UsbDev & dev)781 int32_t UsbdClient::SetDeviceMessage(MessageParcel &data, const UsbDev &dev)
782 {
783 WRITE_PARCEL_WITH_RET(data, Uint8, dev.busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
784 WRITE_PARCEL_WITH_RET(data, Uint8, dev.devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
785 return UEC_OK;
786 }
787
RegBulkCallback(const UsbDev & dev,const UsbPipe & pipe,const sptr<IRemoteObject> & cb)788 int32_t UsbdClient::RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb)
789 {
790 MessageParcel data;
791 MessageParcel reply;
792
793 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
794 HDF_LOGE(" WriteInterfaceToken failed.");
795 return UEC_HDF_FAILURE;
796 }
797
798 UsbdClient::SetDeviceMessage(data, dev);
799 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
800 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
801 WRITE_PARCEL_WITH_RET(data, RemoteObject, cb, UEC_SERVICE_WRITE_PARCEL_ERROR);
802 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_REG_BULK_CALLBACK, data, reply);
803 if (ret != UEC_OK) {
804 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
805 }
806 return ret;
807 }
808
UnRegBulkCallback(const UsbDev & dev,const UsbPipe & pipe)809 int32_t UsbdClient::UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe)
810 {
811 MessageParcel data;
812 MessageParcel reply;
813
814 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
815 HDF_LOGE(" WriteInterfaceToken failed.");
816 return UEC_HDF_FAILURE;
817 }
818
819 UsbdClient::SetDeviceMessage(data, dev);
820 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
821 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
822 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_UNREG_BULK_CALLBACK, data, reply);
823 if (ret != UEC_OK) {
824 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
825 return ret;
826 }
827 return ret;
828 }
829
BulkRead(const UsbDev & dev,const UsbPipe & pipe,sptr<Ashmem> & ashmem)830 int32_t UsbdClient::BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
831 {
832 MessageParcel data;
833 MessageParcel reply;
834 if (ashmem == nullptr) {
835 HDF_LOGE("%{public}s:%{public}d BulkRead error ashmem", __func__, __LINE__);
836 return UEC_HDF_ERR_INVALID_PARAM;
837 }
838
839 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
840 HDF_LOGE(" WriteInterfaceToken failed.");
841 return UEC_HDF_FAILURE;
842 }
843
844 UsbdClient::SetDeviceMessage(data, dev);
845 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
846 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
847 WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
848 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SEND_BULK_READ_ASYNC, data, reply);
849 if (ret != UEC_OK) {
850 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
851 return ret;
852 }
853 return ret;
854 }
855
BulkWrite(const UsbDev & dev,const UsbPipe & pipe,sptr<Ashmem> & ashmem)856 int32_t UsbdClient::BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
857 {
858 MessageParcel data;
859 MessageParcel reply;
860 if (ashmem == nullptr) {
861 HDF_LOGE("%{public}s:%{public}d BulkWrite error ashmem", __func__, __LINE__);
862 return UEC_HDF_ERR_INVALID_PARAM;
863 }
864
865 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
866 HDF_LOGE(" WriteInterfaceToken failed.");
867 return UEC_HDF_FAILURE;
868 }
869
870 UsbdClient::SetDeviceMessage(data, dev);
871 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
872 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
873 WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
874 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_SEND_BULK_WRITE_ASYNC, data, reply);
875 if (ret != UEC_OK) {
876 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
877 return ret;
878 }
879 return ret;
880 }
881
BulkCancel(const UsbDev & dev,const UsbPipe & pipe)882 int32_t UsbdClient::BulkCancel(const UsbDev &dev, const UsbPipe &pipe)
883 {
884 MessageParcel data;
885 MessageParcel reply;
886
887 if (data.WriteInterfaceToken(GetDescriptor()) == false) {
888 HDF_LOGE(" WriteInterfaceToken failed.");
889 return UEC_HDF_FAILURE;
890 }
891
892 UsbdClient::SetDeviceMessage(data, dev);
893 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
894 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
895 int32_t ret = UsbdClient::DoDispatch(CMD_FUN_BULK_CANCEL, data, reply);
896 if (ret != UEC_OK) {
897 HDF_LOGE("%{public}s:%{public}d failed:%{public}d", __func__, __LINE__, ret);
898 return ret;
899 }
900 return ret;
901 }
902 } // namespace USB
903 } // namespace OHOS
904