• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "message_parcel.h"
17 #include "securec.h"
18 #include "string_ex.h"
19 #include "usb_common.h"
20 #include "usb_errors.h"
21 #include "usb_server_stub.h"
22 #include "usb_interface_type.h"
23 #include "v1_1/iusb_interface.h"
24 #include "usb_report_sys_event.h"
25 #include "hitrace_meter.h"
26 using namespace OHOS::HDI::Usb::V1_1;
27 namespace OHOS {
28 namespace USB {
29 constexpr int32_t MAX_EDM_LIST_SIZE = 200;
GetDeviceMessage(MessageParcel & data,uint8_t & busNum,uint8_t & devAddr)30 int32_t UsbServerStub::GetDeviceMessage(MessageParcel &data, uint8_t &busNum, uint8_t &devAddr)
31 {
32     if (!data.ReadUint8(busNum)) {
33         return UEC_SERVICE_READ_PARCEL_ERROR;
34     }
35     if (!data.ReadUint8(devAddr)) {
36         return UEC_SERVICE_READ_PARCEL_ERROR;
37     }
38     return UEC_OK;
39 }
40 
SetBufferMessage(MessageParcel & data,const std::vector<uint8_t> & bufferData)41 int32_t UsbServerStub::SetBufferMessage(MessageParcel &data, const std::vector<uint8_t> &bufferData)
42 {
43     uint32_t length = bufferData.size();
44     const uint8_t *ptr = bufferData.data();
45     if (!ptr) {
46         length = 0;
47     }
48 
49     if (!data.WriteUint32(length)) {
50         USB_HILOGE(MODULE_USBD, "write length failed length:%{public}u", length);
51         return UEC_SERVICE_WRITE_PARCEL_ERROR;
52     }
53     if ((ptr) && (length > 0) && !data.WriteBuffer(reinterpret_cast<const void *>(ptr), length)) {
54         USB_HILOGE(MODULE_USBD, "writer buffer failed length:%{public}u", length);
55         return UEC_SERVICE_WRITE_PARCEL_ERROR;
56     }
57     return UEC_OK;
58 }
59 
GetBufferMessage(MessageParcel & data,std::vector<uint8_t> & bufferData)60 int32_t UsbServerStub::GetBufferMessage(MessageParcel &data, std::vector<uint8_t> &bufferData)
61 {
62     uint32_t dataSize = 0;
63     bufferData.clear();
64     if (!data.ReadUint32(dataSize)) {
65         USB_HILOGE(MODULE_USBD, "read dataSize failed");
66         return UEC_SERVICE_READ_PARCEL_ERROR;
67     }
68     if (dataSize == 0) {
69         USB_HILOGW(MODULE_USBD, "size:%{public}u", dataSize);
70         return UEC_OK;
71     }
72 
73     const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
74     if (readData == nullptr) {
75         USB_HILOGE(MODULE_USBD, "failed size:%{public}u", dataSize);
76         return UEC_SERVICE_READ_PARCEL_ERROR;
77     }
78     std::vector<uint8_t> tdata(readData, readData + dataSize);
79     bufferData.swap(tdata);
80     return UEC_OK;
81 }
82 
WriteFileDescriptor(MessageParcel & data,int fd)83 bool UsbServerStub::WriteFileDescriptor(MessageParcel &data, int fd)
84 {
85     if (!data.WriteBool(fd >= 0 ? true : false)) {
86         USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to write fd vailed", __func__);
87         return false;
88     }
89     if (fd < 0) {
90         return true;
91     }
92     if (!data.WriteFileDescriptor(fd)) {
93         USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to write fd", __func__);
94         return false;
95     }
96     return true;
97 }
98 
StubDevice(uint32_t code,int32_t & result,MessageParcel & data,MessageParcel & reply,MessageOption & option)99 bool UsbServerStub::StubDevice(
100     uint32_t code, int32_t &result, MessageParcel &data, MessageParcel &reply, MessageOption &option)
101 {
102     switch (code) {
103         case static_cast<int>(UsbInterfaceCode::USB_FUN_OPEN_DEVICE):
104             result = DoOpenDevice(data, reply, option);
105             return true;
106         case static_cast<int>(UsbInterfaceCode::USB_FUN_HAS_RIGHT):
107             result = DoHasRight(data, reply, option);
108             return true;
109         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_RIGHT):
110             result = DoRequestRight(data, reply, option);
111             return true;
112         case static_cast<int>(UsbInterfaceCode::USB_FUN_REMOVE_RIGHT):
113             result = DoRemoveRight(data, reply, option);
114             return true;
115         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_PORTS):
116             result = DoGetPorts(data, reply, option);
117             return true;
118         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_SUPPORTED_MODES):
119             result = DoGetSupportedModes(data, reply, option);
120             return true;
121         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_PORT_ROLE):
122             result = DoSetPortRole(data, reply, option);
123             return true;
124         case static_cast<int>(UsbInterfaceCode::USB_FUN_CLAIM_INTERFACE):
125             result = DoClaimInterface(data, reply, option);
126             return true;
127         case static_cast<int>(UsbInterfaceCode::USB_FUN_RELEASE_INTERFACE):
128             result = DoReleaseInterface(data, reply, option);
129             return true;
130         case static_cast<int>(UsbInterfaceCode::USB_FUN_REG_BULK_CALLBACK):
131             result = DoRegBulkCallback(data, reply, option);
132             return true;
133         case static_cast<int>(UsbInterfaceCode::USB_FUN_UNREG_BULK_CALLBACK):
134             result = DoUnRegBulkCallback(data, reply, option);
135             return true;
136         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_FILEDESCRIPTOR):
137             result = DoGetFileDescriptor(data, reply, option);
138             return true;
139         case static_cast<int>(UsbInterfaceCode::USB_FUN_ADD_RIGHT):
140             result = DoAddRight(data, reply, option);
141             return true;
142         case static_cast<int>(UsbInterfaceCode::USB_FUN_ADD_ACCESS_RIGHT):
143             result = DoAddAccessRight(data, reply, option);
144             return true;
145         default:;
146     }
147     return false;
148 }
149 
StubHost(uint32_t code,int32_t & result,MessageParcel & data,MessageParcel & reply,MessageOption & option)150 bool UsbServerStub::StubHost(
151     uint32_t code, int32_t &result, MessageParcel &data, MessageParcel &reply, MessageOption &option)
152 {
153     switch (code) {
154         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DEVICES):
155             result = DoGetDevices(data, reply, option);
156             return true;
157         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_CURRENT_FUNCTIONS):
158             result = DoGetCurrentFunctions(data, reply, option);
159             return true;
160         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_CURRENT_FUNCTIONS):
161             result = DoSetCurrentFunctions(data, reply, option);
162             return true;
163         case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_FROM_STRING):
164             result = DoUsbFunctionsFromString(data, reply, option);
165             return true;
166         case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_TO_STRING):
167             result = DoUsbFunctionsToString(data, reply, option);
168             return true;
169         case static_cast<int>(UsbInterfaceCode::USB_FUN_CLOSE_DEVICE):
170             result = DoClose(data, reply, option);
171             return true;
172         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_QUEUE):
173             result = DoRequestQueue(data, reply, option);
174             return true;
175         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_WAIT):
176             result = DoRequestWait(data, reply, option);
177             return true;
178         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_INTERFACE):
179             result = DoSetInterface(data, reply, option);
180             return true;
181         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_ACTIVE_CONFIG):
182             result = DoSetActiveConfig(data, reply, option);
183             return true;
184         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_CANCEL):
185             result = DoRequestCancel(data, reply, option);
186             return true;
187         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_READ):
188             result = DoBulkRead(data, reply, option);
189             return true;
190         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_WRITE):
191             result = DoBulkWrite(data, reply, option);
192             return true;
193         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_CANCEL):
194             result = DoBulkCancel(data, reply, option);
195             return true;
196         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DESCRIPTOR):
197             result = DoGetRawDescriptor(data, reply, option);
198             return true;
199         case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_GLOBAL_INTERFACE):
200             result = DoManageGlobalInterface(data, reply, option);
201             return true;
202         case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_DEVICE):
203             result = DoManageDevice(data, reply, option);
204             return true;
205         case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_STORAGE):
206             result = DoManageInterfaceStorage(data, reply, option);
207             return true;
208         case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_TYPE):
209             result = DoManageInterfaceType(data, reply, option);
210             return true;
211         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DEVICE_SPEED):
212             result = DoGetDeviceSpeed(data, reply, option);
213             return true;
214         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DRIVER_ACTIVE_STATUS):
215             result = DoGetInterfaceActiveStatus(data, reply, option);
216             return true;
217         default:;
218     }
219     return false;
220 }
221 
StubHostTransfer(uint32_t code,int32_t & result,MessageParcel & data,MessageParcel & reply,MessageOption & option)222 bool UsbServerStub::StubHostTransfer(uint32_t code, int32_t &result,
223     MessageParcel &data, MessageParcel &reply, MessageOption &option)
224 {
225     switch (code) {
226         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ):
227             result = DoBulkTransferRead(data, reply, option);
228             return true;
229         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ_WITH_LENGTH):
230             result = DoBulkTransferReadwithLength(data, reply, option);
231             return true;
232         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_WRITE):
233             result = DoBulkTransferWrite(data, reply, option);
234             return true;
235         case static_cast<int>(UsbInterfaceCode::USB_FUN_CONTROL_TRANSFER):
236             result = DoControlTransfer(data, reply, option);
237             return true;
238         case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_CONTROL_TRANSFER):
239             result = DoUsbControlTransfer(data, reply, option);
240             return true;
241         default:;
242     }
243     return false;
244 }
245 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)246 int32_t UsbServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
247 {
248     USB_HILOGD(MODULE_USB_SERVICE, "UsbServerStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code,
249         option.GetFlags());
250     std::u16string descriptor = UsbServerStub::GetDescriptor();
251     std::u16string remoteDescriptor = data.ReadInterfaceToken();
252     if (descriptor != remoteDescriptor) {
253         USB_HILOGE(MODULE_SERVICE, "UsbServerStub::OnRemoteRequest failed, descriptor is not matched!");
254         return UEC_SERVICE_INNER_ERR;
255     }
256 
257     int32_t ret = 0;
258     if (StubHost(code, ret, data, reply, option)) {
259         return ret;
260     } else if (StubDevice(code, ret, data, reply, option)) {
261         return ret;
262     } else if (StubHostTransfer(code, ret, data, reply, option)) {
263         return ret;
264     } else {
265         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
266     }
267 
268     return UEC_OK;
269 }
270 
DoGetCurrentFunctions(MessageParcel & data,MessageParcel & reply,MessageOption & option)271 int32_t UsbServerStub::DoGetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option)
272 {
273     int32_t functions;
274     int32_t ret = GetCurrentFunctions(functions);
275     if (ret != UEC_OK) {
276         UsbReportSysEvent::ReportTransforFaultSysEvent("GetCurrentFunctions", {0, 0}, {0, 0}, ret);
277         return ret;
278     }
279     WRITE_PARCEL_WITH_RET(reply, Int32, functions, UEC_SERVICE_WRITE_PARCEL_ERROR);
280     return UEC_OK;
281 }
282 
DoSetCurrentFunctions(MessageParcel & data,MessageParcel & reply,MessageOption & option)283 int32_t UsbServerStub::DoSetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option)
284 {
285     HITRACE_METER_NAME(HITRACE_TAG_USB, "SetCurrentFunctions");
286     int32_t funcs;
287     READ_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
288     int32_t ret = SetCurrentFunctions(funcs);
289     if (ret != UEC_OK) {
290         UsbReportSysEvent::ReportTransforFaultSysEvent("SetCurrentFunctions", {0, 0}, {0, 0}, ret);
291     }
292     return ret;
293 }
294 
DoUsbFunctionsFromString(MessageParcel & data,MessageParcel & reply,MessageOption & option)295 int32_t UsbServerStub::DoUsbFunctionsFromString(MessageParcel &data, MessageParcel &reply, MessageOption &option)
296 {
297     std::string funcs;
298     READ_PARCEL_WITH_RET(data, String, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
299     WRITE_PARCEL_WITH_RET(reply, Int32, UsbFunctionsFromString(funcs), UEC_SERVICE_WRITE_PARCEL_ERROR);
300     return UEC_OK;
301 }
302 
DoUsbFunctionsToString(MessageParcel & data,MessageParcel & reply,MessageOption & option)303 int32_t UsbServerStub::DoUsbFunctionsToString(MessageParcel &data, MessageParcel &reply, MessageOption &option)
304 {
305     int32_t funcs;
306     READ_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
307     WRITE_PARCEL_WITH_RET(reply, String, UsbFunctionsToString(funcs), UEC_SERVICE_WRITE_PARCEL_ERROR);
308     return UEC_OK;
309 }
310 
DoOpenDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)311 int32_t UsbServerStub::DoOpenDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
312 {
313     uint8_t busNum = 0;
314     uint8_t devAddr = 0;
315     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
316     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
317     int32_t ret = OpenDevice(busNum, devAddr);
318     if (ret != UEC_OK) {
319         UsbReportSysEvent::ReportTransforFaultSysEvent("OpenDevice", {busNum, devAddr}, {0, 0}, ret);
320         return ret;
321     }
322 
323     return UEC_OK;
324 }
325 
DoHasRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)326 int32_t UsbServerStub::DoHasRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
327 {
328     std::u16string deviceName = u"";
329     READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
330     WRITE_PARCEL_WITH_RET(reply, Bool, HasRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
331 
332     return UEC_OK;
333 }
334 
DoRequestRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)335 int32_t UsbServerStub::DoRequestRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
336 {
337     std::u16string deviceName = u"";
338     READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
339     WRITE_PARCEL_WITH_RET(reply, Int32, RequestRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
340     return UEC_OK;
341 }
342 
DoRemoveRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)343 int32_t UsbServerStub::DoRemoveRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
344 {
345     std::u16string deviceName = u"";
346     READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
347     WRITE_PARCEL_WITH_RET(reply, Int32, RemoveRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
348     return UEC_OK;
349 }
350 
DoGetPorts(MessageParcel & data,MessageParcel & reply,MessageOption & option)351 int32_t UsbServerStub::DoGetPorts(MessageParcel &data, MessageParcel &reply, MessageOption &option)
352 {
353     std::vector<UsbPort> ports;
354     int32_t ret = GetPorts(ports);
355     USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts ret %{public}d ", ret);
356     if (ret != UEC_OK) {
357         UsbReportSysEvent::ReportTransforFaultSysEvent("GetPorts", {0, 0}, {0, 0}, ret);
358         return ret;
359     }
360     uint32_t size = ports.size();
361     USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts size %{public}d ", size);
362     WRITE_PARCEL_WITH_RET(reply, Int32, size, UEC_SERVICE_WRITE_PARCEL_ERROR);
363     for (uint32_t i = 0; i < size; ++i) {
364         ret = WriteUsbPort(reply, ports[i]);
365         if (ret) {
366             return ret;
367         }
368     }
369     return ret;
370 }
371 
WriteUsbPort(MessageParcel & reply,const UsbPort & port)372 int32_t UsbServerStub::WriteUsbPort(MessageParcel &reply, const UsbPort &port)
373 {
374     WRITE_PARCEL_WITH_RET(reply, Int32, port.id, UEC_SERVICE_WRITE_PARCEL_ERROR);
375     WRITE_PARCEL_WITH_RET(reply, Int32, port.supportedModes, UEC_SERVICE_WRITE_PARCEL_ERROR);
376     WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentMode, UEC_SERVICE_WRITE_PARCEL_ERROR);
377     WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentPowerRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
378     WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentDataRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
379     USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts supportedModes %{public}d ", port.supportedModes);
380     return UEC_OK;
381 }
382 
DoGetSupportedModes(MessageParcel & data,MessageParcel & reply,MessageOption & option)383 int32_t UsbServerStub::DoGetSupportedModes(MessageParcel &data, MessageParcel &reply, MessageOption &option)
384 {
385     int32_t supportedModes = 0;
386     int32_t portId = 0;
387     READ_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
388     int32_t ret = GetSupportedModes(portId, supportedModes);
389     if (ret != UEC_OK) {
390         return ret;
391     }
392     WRITE_PARCEL_WITH_RET(reply, Int32, supportedModes, UEC_SERVICE_WRITE_PARCEL_ERROR);
393     return ret;
394 }
395 
DoSetPortRole(MessageParcel & data,MessageParcel & reply,MessageOption & option)396 int32_t UsbServerStub::DoSetPortRole(MessageParcel &data, MessageParcel &reply, MessageOption &option)
397 {
398     HITRACE_METER_NAME(HITRACE_TAG_USB, "SetPortRole");
399     int32_t portId = 0;
400     int32_t powerRole = 0;
401     int32_t dataRole = 0;
402     READ_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
403     READ_PARCEL_WITH_RET(data, Int32, powerRole, UEC_SERVICE_READ_PARCEL_ERROR);
404     READ_PARCEL_WITH_RET(data, Int32, dataRole, UEC_SERVICE_READ_PARCEL_ERROR);
405     int32_t ret = SetPortRole(portId, powerRole, dataRole);
406     if (ret != UEC_OK) {
407         UsbReportSysEvent::ReportTransforFaultSysEvent("SetPortRole", {0, 0}, {0, 0}, ret);
408         return ret;
409     }
410     return ret;
411 }
412 
DoClaimInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)413 int32_t UsbServerStub::DoClaimInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
414 {
415     HITRACE_METER_NAME(HITRACE_TAG_USB, "ClaimInterface");
416     uint8_t busNum = 0;
417     uint8_t devAddr = 0;
418     uint8_t interface = 0;
419     uint8_t force = 0;
420     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
421     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
422     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
423     READ_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_READ_PARCEL_ERROR);
424     WRITE_PARCEL_WITH_RET(
425         reply, Int32, ClaimInterface(busNum, devAddr, interface, force), UEC_SERVICE_WRITE_PARCEL_ERROR);
426     return UEC_OK;
427 }
428 
DoReleaseInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)429 int32_t UsbServerStub::DoReleaseInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
430 {
431     HITRACE_METER_NAME(HITRACE_TAG_USB, "ReleaseInterface");
432     uint8_t busNum = 0;
433     uint8_t devAddr = 0;
434     uint8_t interface = 0;
435     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
436     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
437     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
438     WRITE_PARCEL_WITH_RET(reply, Int32, ReleaseInterface(busNum, devAddr, interface), UEC_SERVICE_WRITE_PARCEL_ERROR);
439     return UEC_OK;
440 }
441 
DoBulkTransferRead(MessageParcel & data,MessageParcel & reply,MessageOption & option)442 int32_t UsbServerStub::DoBulkTransferRead(MessageParcel &data, MessageParcel &reply, MessageOption &option)
443 {
444     HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkTransferRead");
445     uint8_t busNum = 0;
446     uint8_t devAddr = 0;
447     uint8_t interface = 0;
448     uint8_t endpoint = 0;
449     int32_t timeOut = 0;
450     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
451     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
452     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
453     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
454     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
455     std::vector<uint8_t> bufferData;
456     const UsbDev tmpDev = {busNum, devAddr};
457     const UsbPipe tmpPipe = {interface, endpoint};
458     int32_t ret = BulkTransferRead(tmpDev, tmpPipe, bufferData, timeOut);
459     if (ret != UEC_OK) {
460         USB_HILOGE(MODULE_USBD, "read failed ret:%{public}d", ret);
461         UsbReportSysEvent::ReportTransforFaultSysEvent("BulkTransferRead", tmpDev, tmpPipe, ret);
462         return ret;
463     }
464     ret = SetBufferMessage(reply, bufferData);
465     if (ret != UEC_OK) {
466         USB_HILOGE(MODULE_USBD, "set buffer failed ret:%{public}d", ret);
467     }
468     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
469     return ret;
470 }
471 
DoBulkTransferReadwithLength(MessageParcel & data,MessageParcel & reply,MessageOption & option)472 int32_t UsbServerStub::DoBulkTransferReadwithLength(MessageParcel &data, MessageParcel &reply, MessageOption &option)
473 {
474     HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkTransferRead");
475     uint8_t busNum = 0;
476     uint8_t devAddr = 0;
477     uint8_t interface = 0;
478     uint8_t endpoint = 0;
479     int32_t length = 0;
480     int32_t timeOut = 0;
481     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
482     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
483     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
484     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
485     READ_PARCEL_WITH_RET(data, Int32, length, UEC_SERVICE_WRITE_PARCEL_ERROR);
486     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
487     std::vector<uint8_t> bufferData;
488     const UsbDev tmpDev = {busNum, devAddr};
489     const UsbPipe tmpPipe = {interface, endpoint};
490     int32_t ret = BulkTransferReadwithLength(tmpDev, tmpPipe, length, bufferData, timeOut);
491     if (ret != UEC_OK) {
492         USB_HILOGE(MODULE_USBD, "read failed ret:%{public}d", ret);
493         UsbReportSysEvent::ReportTransforFaultSysEvent("BulkTransferRead", tmpDev, tmpPipe, ret);
494         return ret;
495     }
496     ret = SetBufferMessage(reply, bufferData);
497     if (ret != UEC_OK) {
498         USB_HILOGE(MODULE_USBD, "set buffer failed ret:%{public}d", ret);
499     }
500     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
501     return ret;
502 }
503 
DoBulkTransferWrite(MessageParcel & data,MessageParcel & reply,MessageOption & option)504 int32_t UsbServerStub::DoBulkTransferWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option)
505 {
506     HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkTransferWrite");
507     uint8_t busNum = 0;
508     uint8_t devAddr = 0;
509     uint8_t interface = 0;
510     uint8_t endpoint = 0;
511     int32_t timeOut = 0;
512     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
513     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
514     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
515     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
516     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
517     std::vector<uint8_t> bufferData;
518     const UsbDev tmpDev = {busNum, devAddr};
519     const UsbPipe tmpPipe = {interface, endpoint};
520     int32_t ret = GetBufferMessage(data, bufferData);
521     if (ret != UEC_OK) {
522         USB_HILOGE(MODULE_USBD, "GetBufferMessage failedret:%{public}d", ret);
523         return ret;
524     }
525     ret = BulkTransferWrite(tmpDev, tmpPipe, bufferData, timeOut);
526     if (ret != UEC_OK) {
527         USB_HILOGE(MODULE_USBD, "BulkTransferWrite error ret:%{public}d", ret);
528         UsbReportSysEvent::ReportTransforFaultSysEvent("BulkTransferWrite", tmpDev, tmpPipe, ret);
529     }
530     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
531     return ret;
532 }
533 
DoControlTransfer(MessageParcel & data,MessageParcel & reply,MessageOption & option)534 int32_t UsbServerStub::DoControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
535 {
536     HITRACE_METER_NAME(HITRACE_TAG_USB, "ControlTransfer");
537     uint8_t busNum = 0;
538     uint8_t devAddr = 0;
539     int32_t requestType;
540     int32_t request;
541     int32_t value;
542     int32_t index;
543     int32_t timeOut;
544 
545     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
546     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
547     READ_PARCEL_WITH_RET(data, Int32, requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
548     READ_PARCEL_WITH_RET(data, Int32, request, UEC_SERVICE_WRITE_PARCEL_ERROR);
549     READ_PARCEL_WITH_RET(data, Int32, value, UEC_SERVICE_WRITE_PARCEL_ERROR);
550     READ_PARCEL_WITH_RET(data, Int32, index, UEC_SERVICE_WRITE_PARCEL_ERROR);
551     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
552     std::vector<uint8_t> bufferData;
553     int32_t ret = GetBufferMessage(data, bufferData);
554     if (ret != UEC_OK) {
555         USB_HILOGE(MODULE_USBD, "get error ret:%{public}d", ret);
556         return ret;
557     }
558 
559     bool bWrite = ((static_cast<uint32_t>(requestType) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
560     const UsbDev tmpDev = {busNum, devAddr};
561     const UsbCtrlTransfer tctrl = {requestType, request, value, index, timeOut};
562     ret = ControlTransfer(tmpDev, tctrl, bufferData);
563     if (ret != UEC_OK) {
564         UsbReportSysEvent::ReportTransforFaultSysEvent("ControlTransfer", tmpDev, {0, 0}, ret);
565         USB_HILOGE(MODULE_USBD, "ControlTransfer error ret:%{public}d", ret);
566         return ret;
567     }
568 
569     if (!bWrite) {
570         ret = SetBufferMessage(reply, bufferData);
571         if (ret != UEC_OK) {
572             USB_HILOGE(MODULE_USBD, "Set buffer message error length = %{public}d", ret);
573         }
574     }
575     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
576     return UEC_OK;
577 }
578 
DoUsbControlTransfer(MessageParcel & data,MessageParcel & reply,MessageOption & option)579 int32_t UsbServerStub::DoUsbControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
580 {
581     HITRACE_METER_NAME(HITRACE_TAG_USB, "ControlTransfer");
582     uint8_t busNum = 0;
583     uint8_t devAddr = 0;
584     int32_t requestType;
585     int32_t request;
586     int32_t value;
587     int32_t index;
588     int32_t length;
589     int32_t timeOut;
590 
591     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
592     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
593     READ_PARCEL_WITH_RET(data, Int32, requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
594     READ_PARCEL_WITH_RET(data, Int32, request, UEC_SERVICE_WRITE_PARCEL_ERROR);
595     READ_PARCEL_WITH_RET(data, Int32, value, UEC_SERVICE_WRITE_PARCEL_ERROR);
596     READ_PARCEL_WITH_RET(data, Int32, index, UEC_SERVICE_WRITE_PARCEL_ERROR);
597     READ_PARCEL_WITH_RET(data, Int32, length, UEC_SERVICE_WRITE_PARCEL_ERROR);
598     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
599     std::vector<uint8_t> bufferData;
600     int32_t ret = GetBufferMessage(data, bufferData);
601     if (ret != UEC_OK) {
602         USB_HILOGE(MODULE_USBD, "get error ret:%{public}d", ret);
603         return ret;
604     }
605 
606     bool bWrite = ((static_cast<uint32_t>(requestType) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
607     const UsbDev tmpDev = {busNum, devAddr};
608     const UsbCtrlTransferParams tctrlParams = {requestType, request, value, index, length, timeOut};
609     ret = UsbControlTransfer(tmpDev, tctrlParams, bufferData);
610     if (ret != UEC_OK) {
611         UsbReportSysEvent::ReportTransforFaultSysEvent("ControlTransfer", tmpDev, {0, 0}, ret);
612         USB_HILOGE(MODULE_USBD, "ControlTransfer error ret:%{public}d", ret);
613         return ret;
614     }
615 
616     if (!bWrite) {
617         ret = SetBufferMessage(reply, bufferData);
618         if (ret != UEC_OK) {
619             USB_HILOGE(MODULE_USBD, "Set buffer message error length = %{public}d", ret);
620         }
621     }
622     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
623     return UEC_OK;
624 }
625 
DoSetActiveConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)626 int32_t UsbServerStub::DoSetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
627 {
628     HITRACE_METER_NAME(HITRACE_TAG_USB, "SetActiveConfig");
629     uint8_t busNum = 0;
630     uint8_t devAddr = 0;
631     uint8_t config = 0;
632     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
633     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
634     READ_PARCEL_WITH_RET(data, Uint8, config, UEC_SERVICE_WRITE_PARCEL_ERROR);
635     WRITE_PARCEL_WITH_RET(reply, Int32, SetActiveConfig(busNum, devAddr, config), UEC_SERVICE_WRITE_PARCEL_ERROR);
636     return UEC_OK;
637 }
638 
DoGetActiveConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)639 int32_t UsbServerStub::DoGetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
640 {
641     uint8_t busNum = 0;
642     uint8_t devAddr = 0;
643     uint8_t config = 0;
644     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
645     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
646     int32_t ret = GetActiveConfig(busNum, devAddr, config);
647     if (ret == UEC_OK) {
648         WRITE_PARCEL_WITH_RET(reply, Uint8, config, UEC_SERVICE_WRITE_PARCEL_ERROR);
649     }
650     return ret;
651 }
652 
DoSetInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)653 int32_t UsbServerStub::DoSetInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
654 {
655     HITRACE_METER_NAME(HITRACE_TAG_USB, "SetInterface");
656     uint8_t busNum = 0;
657     uint8_t devAddr = 0;
658     uint8_t interfaceId = 0;
659     uint8_t altIndex = 0;
660     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
661     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
662     READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
663     READ_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
664     WRITE_PARCEL_WITH_RET(
665         reply, Int32, SetInterface(busNum, devAddr, interfaceId, altIndex), UEC_SERVICE_WRITE_PARCEL_ERROR);
666     return UEC_OK;
667 }
668 
DoGetRawDescriptor(MessageParcel & data,MessageParcel & reply,MessageOption & option)669 int32_t UsbServerStub::DoGetRawDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option)
670 {
671     uint8_t busNum = 0;
672     uint8_t devAddr = 0;
673     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
674     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
675     std::vector<uint8_t> bufferData;
676     int32_t ret = GetRawDescriptor(busNum, devAddr, bufferData);
677     if (ret == UEC_OK) {
678         ret = SetBufferMessage(reply, bufferData);
679         if (ret != UEC_OK) {
680             USB_HILOGE(MODULE_USBD, "SetBufferMessage failed ret:%{public}d", ret);
681         }
682     } else {
683         USB_HILOGW(MODULE_USBD, "GetRawDescriptor failed ret:%{public}d", ret);
684         UsbReportSysEvent::ReportTransforFaultSysEvent("GetRawDescriptor", {busNum, devAddr}, {0, 0}, ret);
685     }
686     return ret;
687 }
688 
DoGetFileDescriptor(MessageParcel & data,MessageParcel & reply,MessageOption & option)689 int32_t UsbServerStub::DoGetFileDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option)
690 {
691     uint8_t busNum = 0;
692     uint8_t devAddr = 0;
693     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
694     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
695     int32_t fd = -1;
696     int32_t ret = GetFileDescriptor(busNum, devAddr, fd);
697     if (ret == UEC_OK) {
698         if (!WriteFileDescriptor(reply, fd)) {
699             USB_HILOGW(MODULE_USB_SERVICE, "%{public}s: write fd failed!", __func__);
700             return UEC_INTERFACE_WRITE_PARCEL_ERROR;
701         }
702     } else {
703         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
704     }
705     return ret;
706 }
707 
DoRequestQueue(MessageParcel & data,MessageParcel & reply,MessageOption & option)708 int32_t UsbServerStub::DoRequestQueue(MessageParcel &data, MessageParcel &reply, MessageOption &option)
709 {
710     uint8_t busNum = 0;
711     uint8_t devAddr = 0;
712     uint8_t ifId = 0;
713     uint8_t endpoint = 0;
714     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
715     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
716     READ_PARCEL_WITH_RET(data, Uint8, ifId, UEC_SERVICE_WRITE_PARCEL_ERROR);
717     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
718     std::vector<uint8_t> clientData;
719     std::vector<uint8_t> bufferData;
720 
721     int32_t ret = UsbServerStub::GetBufferMessage(data, clientData);
722     if (ret != UEC_OK) {
723         USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed  ret:%{public}d", ret);
724         return ret;
725     }
726     ret = UsbServerStub::GetBufferMessage(data, bufferData);
727     if (ret != UEC_OK) {
728         USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed  ret:%{public}d", ret);
729         return ret;
730     }
731     const UsbDev tmpDev = {busNum, devAddr};
732     const UsbPipe tmpPipe = {ifId, endpoint};
733     ret = RequestQueue(tmpDev, tmpPipe, clientData, bufferData);
734     if (ret != UEC_OK) {
735         USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed ret:%{public}d", ret);
736     }
737     return ret;
738 }
739 
DoRequestWait(MessageParcel & data,MessageParcel & reply,MessageOption & option)740 int32_t UsbServerStub::DoRequestWait(MessageParcel &data, MessageParcel &reply, MessageOption &option)
741 {
742     uint8_t busNum = 0;
743     uint8_t devAddr = 0;
744     int32_t timeOut = 0;
745     std::vector<uint8_t> clientData;
746     std::vector<uint8_t> bufferData;
747     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
748     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
749     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
750 
751     const UsbDev tmpDev = {busNum, devAddr};
752     int32_t ret = RequestWait(tmpDev, timeOut, clientData, bufferData);
753     if (ret != UEC_OK) {
754         USB_HILOGE(MODULE_USB_INNERKIT, "RequestWait failed ret:%{public}d", ret);
755         return ret;
756     }
757 
758     ret = SetBufferMessage(reply, clientData);
759     if (ret != UEC_OK) {
760         USB_HILOGE(MODULE_USB_INNERKIT, "Set clientData failed ret:%{public}d", ret);
761         return ret;
762     }
763 
764     ret = SetBufferMessage(reply, bufferData);
765     if (ret != UEC_OK) {
766         USB_HILOGE(MODULE_USB_INNERKIT, "Set bufferData failed ret:%{public}d", ret);
767         return ret;
768     }
769     return ret;
770 }
771 
DoRequestCancel(MessageParcel & data,MessageParcel & reply,MessageOption & option)772 int32_t UsbServerStub::DoRequestCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option)
773 {
774     uint8_t busNum = 0;
775     uint8_t devAddr = 0;
776     uint8_t interfaceId = 0;
777     uint8_t endpointId = 0;
778     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
779     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
780     READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
781     READ_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
782     int32_t ret = RequestCancel(busNum, devAddr, interfaceId, endpointId);
783     if (ret != UEC_OK) {
784         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret:%{public}d", ret);
785     }
786     return ret;
787 }
788 
DoClose(MessageParcel & data,MessageParcel & reply,MessageOption & option)789 int32_t UsbServerStub::DoClose(MessageParcel &data, MessageParcel &reply, MessageOption &option)
790 {
791     uint8_t busNum = 0;
792     uint8_t devAddr = 0;
793     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
794     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
795     int32_t ret = Close(busNum, devAddr);
796     if (ret != UEC_OK) {
797         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret:%{public}d", ret);
798         UsbReportSysEvent::ReportTransforFaultSysEvent("CloseDevice", {busNum, devAddr}, {0, 0}, ret);
799     }
800     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
801     return ret;
802 }
803 
DoGetDevices(MessageParcel & data,MessageParcel & reply,MessageOption & option)804 int32_t UsbServerStub::DoGetDevices(MessageParcel &data, MessageParcel &reply, MessageOption &option)
805 {
806     std::vector<UsbDevice> deviceList;
807     int32_t ret = GetDevices(deviceList);
808     if (ret != UEC_OK) {
809         USB_HILOGE(MODULE_SERVICE, "GetDevices failed ret = %{public}d", ret);
810         UsbReportSysEvent::ReportTransforFaultSysEvent("GetDevices", {0, 0}, {0, 0}, ret);
811         return ret;
812     }
813     USB_HILOGI(MODULE_SERVICE, "list size = %{public}zu", deviceList.size());
814     ret = SetDeviceListMessageParcel(deviceList, reply);
815     if (ret != UEC_OK) {
816         USB_HILOGE(MODULE_USB_INNERKIT, "SetDeviceListMessageParcel failed ret:%{public}d", ret);
817     }
818     return ret;
819 }
820 
SetDeviceListMessageParcel(std::vector<UsbDevice> & deviceList,MessageParcel & data)821 int32_t UsbServerStub::SetDeviceListMessageParcel(std::vector<UsbDevice> &deviceList, MessageParcel &data)
822 {
823     int32_t deviceCount = (int32_t)deviceList.size();
824     WRITE_PARCEL_WITH_RET(data, Int32, deviceCount, UEC_SERVICE_WRITE_PARCEL_ERROR);
825     for (int32_t i = 0; i < deviceCount; ++i) {
826         UsbDevice &devInfo = deviceList[i];
827         int32_t ret = SetDeviceMessageParcel(devInfo, data);
828         if (ret) {
829             return ret;
830         }
831     }
832     return UEC_OK;
833 }
834 
SetDeviceMessageParcel(UsbDevice & devInfo,MessageParcel & data)835 int32_t UsbServerStub::SetDeviceMessageParcel(UsbDevice &devInfo, MessageParcel &data)
836 {
837     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetBusNum(), UEC_SERVICE_WRITE_PARCEL_ERROR);
838     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetDevAddr(), UEC_SERVICE_WRITE_PARCEL_ERROR);
839 
840     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetVendorId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
841     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetProductId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
842     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
843     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetSubclass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
844     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetProtocol(), UEC_SERVICE_WRITE_PARCEL_ERROR);
845     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiManufacturer(), UEC_SERVICE_WRITE_PARCEL_ERROR);
846     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiProduct(), UEC_SERVICE_WRITE_PARCEL_ERROR);
847     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiSerialNumber(), UEC_SERVICE_WRITE_PARCEL_ERROR);
848     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetbMaxPacketSize0(), UEC_SERVICE_WRITE_PARCEL_ERROR);
849     WRITE_PARCEL_WITH_RET(data, Uint16, devInfo.GetbcdUSB(), UEC_SERVICE_WRITE_PARCEL_ERROR);
850     WRITE_PARCEL_WITH_RET(data, Uint16, devInfo.GetbcdDevice(), UEC_SERVICE_WRITE_PARCEL_ERROR);
851     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
852     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetManufacturerName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
853     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetProductName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
854     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetVersion()), UEC_SERVICE_WRITE_PARCEL_ERROR);
855     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetmSerial()), UEC_SERVICE_WRITE_PARCEL_ERROR);
856 
857     USB_HILOGE(MODULE_USB_INNERKIT, "devInfo:%{public}s", devInfo.ToString().c_str());
858     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetConfigCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
859     return SetDeviceConfigsMessageParcel(devInfo.GetConfigs(), data);
860 }
861 
SetDeviceConfigsMessageParcel(std::vector<USBConfig> & configs,MessageParcel & data)862 int32_t UsbServerStub::SetDeviceConfigsMessageParcel(std::vector<USBConfig> &configs, MessageParcel &data)
863 {
864     for (auto it = configs.begin(); it != configs.end(); ++it) {
865         USBConfig config = *it;
866         WRITE_PARCEL_WITH_RET(data, Int32, config.GetId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
867         WRITE_PARCEL_WITH_RET(data, Uint32, config.GetAttributes(), UEC_SERVICE_WRITE_PARCEL_ERROR);
868         WRITE_PARCEL_WITH_RET(data, Int32, config.GetMaxPower(), UEC_SERVICE_WRITE_PARCEL_ERROR);
869 
870         WRITE_PARCEL_WITH_RET(data, Uint8, config.GetiConfiguration(), UEC_SERVICE_WRITE_PARCEL_ERROR);
871         WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(config.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
872 
873         WRITE_PARCEL_WITH_RET(data, Uint32, config.GetInterfaceCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
874         USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", config.ToString().c_str());
875         int32_t ret = SetDeviceInterfacesMessageParcel(config.GetInterfaces(), data);
876         if (ret) {
877             return ret;
878         }
879     }
880     return UEC_OK;
881 }
882 
SetDeviceInterfacesMessageParcel(std::vector<UsbInterface> & interfaces,MessageParcel & data)883 int32_t UsbServerStub::SetDeviceInterfacesMessageParcel(std::vector<UsbInterface> &interfaces, MessageParcel &data)
884 {
885     for (auto it = interfaces.begin(); it != interfaces.end(); ++it) {
886         UsbInterface interface = *it;
887         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
888         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
889         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetSubClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
890         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetAlternateSetting(), UEC_SERVICE_WRITE_PARCEL_ERROR);
891         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetProtocol(), UEC_SERVICE_WRITE_PARCEL_ERROR);
892 
893         WRITE_PARCEL_WITH_RET(data, Uint8, interface.GetiInterface(), UEC_SERVICE_WRITE_PARCEL_ERROR);
894         WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(interface.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
895 
896         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetEndpointCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
897         USB_HILOGI(MODULE_USB_SERVICE, "interface=%{public}s", interface.ToString().c_str());
898         int32_t ret = SetDeviceEndpointsMessageParcel(interface.GetEndpoints(), data);
899         if (ret) {
900             return ret;
901         }
902     }
903     return UEC_OK;
904 }
905 
SetDeviceEndpointsMessageParcel(std::vector<USBEndpoint> & eps,MessageParcel & data)906 int32_t UsbServerStub::SetDeviceEndpointsMessageParcel(std::vector<USBEndpoint> &eps, MessageParcel &data)
907 {
908     for (auto it = eps.begin(); it != eps.end(); ++it) {
909         USBEndpoint ep = *it;
910         WRITE_PARCEL_WITH_RET(data, Uint32, ep.GetAddress(), UEC_SERVICE_WRITE_PARCEL_ERROR);
911         WRITE_PARCEL_WITH_RET(data, Uint32, ep.GetAttributes(), UEC_SERVICE_WRITE_PARCEL_ERROR);
912         WRITE_PARCEL_WITH_RET(data, Int32, ep.GetInterval(), UEC_SERVICE_WRITE_PARCEL_ERROR);
913         WRITE_PARCEL_WITH_RET(data, Int32, ep.GetMaxPacketSize(), UEC_SERVICE_WRITE_PARCEL_ERROR);
914         USB_HILOGI(MODULE_USB_SERVICE, "ep=%{public}s", ep.ToString().c_str());
915     }
916     return UEC_OK;
917 }
918 
DoRegBulkCallback(MessageParcel & data,MessageParcel & reply,MessageOption & option)919 int32_t UsbServerStub::DoRegBulkCallback(MessageParcel &data, MessageParcel &reply, MessageOption &option)
920 {
921     uint8_t busNum = 0;
922     uint8_t devAddr = 0;
923     uint8_t interface = 0;
924     uint8_t endpoint = 0;
925     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
926     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
927     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
928     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
929     const sptr<IRemoteObject> cb = data.ReadRemoteObject();
930     const UsbDev tmpDev = {busNum, devAddr};
931     const UsbPipe tmpPipe = {interface, endpoint};
932     int32_t ret = RegBulkCallback(tmpDev, tmpPipe, cb);
933     if (ret != UEC_OK) {
934         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
935         return ret;
936     }
937     return ret;
938 }
939 
DoUnRegBulkCallback(MessageParcel & data,MessageParcel & reply,MessageOption & option)940 int32_t UsbServerStub::DoUnRegBulkCallback(MessageParcel &data, MessageParcel &reply, MessageOption &option)
941 {
942     uint8_t busNum = 0;
943     uint8_t devAddr = 0;
944     uint8_t interface = 0;
945     uint8_t endpoint = 0;
946     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
947     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
948     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
949     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
950     const UsbDev tmpDev = {busNum, devAddr};
951     const UsbPipe tmpPipe = {interface, endpoint};
952     int32_t ret = UnRegBulkCallback(tmpDev, tmpPipe);
953     if (ret != UEC_OK) {
954         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
955         return ret;
956     }
957     return ret;
958 }
959 
DoBulkRead(MessageParcel & data,MessageParcel & reply,MessageOption & option)960 int32_t UsbServerStub::DoBulkRead(MessageParcel &data, MessageParcel &reply, MessageOption &option)
961 {
962     HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkRead");
963     uint8_t busNum = 0;
964     uint8_t devAddr = 0;
965     uint8_t interface = 0;
966     uint8_t endpoint = 0;
967     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
968     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
969     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
970     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
971     sptr<Ashmem> ashmem = data.ReadAshmem();
972     const UsbDev tmpDev = {busNum, devAddr};
973     const UsbPipe tmpPipe = {interface, endpoint};
974     int32_t ret = BulkRead(tmpDev, tmpPipe, ashmem);
975     if (ret != UEC_OK) {
976         USB_HILOGE(MODULE_USBD, "BulkRead failed ret:%{public}d", ret);
977         UsbReportSysEvent::ReportTransforFaultSysEvent("BulkRead", tmpDev, tmpPipe, ret);
978         return ret;
979     }
980     return ret;
981 }
982 
DoBulkWrite(MessageParcel & data,MessageParcel & reply,MessageOption & option)983 int32_t UsbServerStub::DoBulkWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option)
984 {
985     HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkWrite");
986     uint8_t busNum = 0;
987     uint8_t devAddr = 0;
988     uint8_t interface = 0;
989     uint8_t endpoint = 0;
990     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
991     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
992     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
993     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
994     sptr<Ashmem> ashmem = data.ReadAshmem();
995     const UsbDev tmpDev = {busNum, devAddr};
996     const UsbPipe tmpPipe = {interface, endpoint};
997     int32_t ret = BulkWrite(tmpDev, tmpPipe, ashmem);
998     if (ret != UEC_OK) {
999         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1000         UsbReportSysEvent::ReportTransforFaultSysEvent("BulkWrite", tmpDev, tmpPipe, ret);
1001         return ret;
1002     }
1003     return ret;
1004 }
1005 
DoBulkCancel(MessageParcel & data,MessageParcel & reply,MessageOption & option)1006 int32_t UsbServerStub::DoBulkCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1007 {
1008     uint8_t busNum = 0;
1009     uint8_t devAddr = 0;
1010     uint8_t interface = 0;
1011     uint8_t endpoint = 0;
1012     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
1013     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
1014     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
1015     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
1016     const UsbDev tmpDev = {busNum, devAddr};
1017     const UsbPipe tmpPipe = {interface, endpoint};
1018     int32_t ret = BulkCancel(tmpDev, tmpPipe);
1019     if (ret != UEC_OK) {
1020         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1021         return ret;
1022     }
1023     return ret;
1024 }
1025 
DoAddRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)1026 int32_t UsbServerStub::DoAddRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1027 {
1028     std::string bundleName;
1029     std::string deviceName;
1030     READ_PARCEL_WITH_RET(data, String, bundleName, UEC_SERVICE_READ_PARCEL_ERROR);
1031     READ_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
1032     int32_t ret = AddRight(bundleName, deviceName);
1033     if (ret != UEC_OK) {
1034         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1035     }
1036     return ret;
1037 }
1038 
DoAddAccessRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)1039 int32_t UsbServerStub::DoAddAccessRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1040 {
1041     std::string tokenId;
1042     std::string deviceName;
1043     READ_PARCEL_WITH_RET(data, String, tokenId, UEC_SERVICE_READ_PARCEL_ERROR);
1044     READ_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
1045     int32_t ret = AddAccessRight(tokenId, deviceName);
1046     if (ret != UEC_OK) {
1047         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1048     }
1049     return ret;
1050 }
1051 
DoManageGlobalInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)1052 int32_t UsbServerStub::DoManageGlobalInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1053 {
1054     bool disable = false;
1055     READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
1056     int32_t ret = ManageGlobalInterface(disable);
1057     if (ret != UEC_OK) {
1058         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1059     }
1060     return ret;
1061 }
1062 
DoManageDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)1063 int32_t UsbServerStub::DoManageDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1064 {
1065     int32_t vendorId = 0;
1066     int32_t productId = 0;
1067     bool disable = false;
1068     READ_PARCEL_WITH_RET(data, Int32, vendorId, UEC_SERVICE_READ_PARCEL_ERROR);
1069     READ_PARCEL_WITH_RET(data, Int32, productId, UEC_SERVICE_READ_PARCEL_ERROR);
1070     READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
1071     int32_t ret = ManageDevice(vendorId, productId, disable);
1072     if (ret != UEC_OK) {
1073         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1074     }
1075     return ret;
1076 }
1077 
DoManageInterfaceStorage(MessageParcel & data,MessageParcel & reply,MessageOption & option)1078 int32_t UsbServerStub::DoManageInterfaceStorage(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1079 {
1080     int32_t interfaceType = 0;
1081     bool disable = false;
1082     READ_PARCEL_WITH_RET(data, Int32, interfaceType, UEC_SERVICE_READ_PARCEL_ERROR);
1083     READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
1084     int32_t ret = ManageInterfaceStorage((InterfaceType)interfaceType, disable);
1085     if (ret != UEC_OK) {
1086         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1087     }
1088     return ret;
1089 }
1090 
DoManageInterfaceType(MessageParcel & data,MessageParcel & reply,MessageOption & option)1091 int32_t UsbServerStub::DoManageInterfaceType(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1092 {
1093     int32_t count;
1094     READ_PARCEL_WITH_RET(data, Int32, count, UEC_SERVICE_READ_PARCEL_ERROR);
1095     bool disable = false;
1096     std::vector<UsbDeviceType> disableType;
1097     if (count > MAX_EDM_LIST_SIZE) {
1098         USB_HILOGE(MODULE_USBD, "count:%{public}d", count);
1099         return UEC_SERVICE_READ_PARCEL_ERROR;
1100     }
1101     for (int32_t i = 0; i < count; ++i) {
1102         UsbDeviceType usbDeviceType;
1103         READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.baseClass, UEC_SERVICE_READ_PARCEL_ERROR);
1104         READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.subClass, UEC_SERVICE_READ_PARCEL_ERROR);
1105         READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.protocol, UEC_SERVICE_READ_PARCEL_ERROR);
1106         READ_PARCEL_WITH_RET(data, Bool, usbDeviceType.isDeviceType, UEC_SERVICE_READ_PARCEL_ERROR);
1107         disableType.emplace_back(usbDeviceType);
1108     }
1109     READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
1110     int32_t ret = ManageInterfaceType(disableType, disable);
1111     if (ret != UEC_OK) {
1112         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1113     }
1114     return ret;
1115 }
1116 
DoGetInterfaceActiveStatus(MessageParcel & data,MessageParcel & reply,MessageOption & option)1117 int32_t UsbServerStub::DoGetInterfaceActiveStatus(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1118 {
1119     uint8_t busNum = 0;
1120     uint8_t devAddr = 0;
1121     uint8_t interfaceId = 0;
1122     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
1123     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
1124     READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1125     bool unactivated;
1126     int32_t ret = GetInterfaceActiveStatus(busNum, devAddr, interfaceId, unactivated);
1127     if (ret == UEC_OK) {
1128         WRITE_PARCEL_WITH_RET(reply, Bool, unactivated, UEC_SERVICE_WRITE_PARCEL_ERROR);
1129     } else {
1130         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1131     }
1132     return ret;
1133 }
1134 
DoGetDeviceSpeed(MessageParcel & data,MessageParcel & reply,MessageOption & option)1135 int32_t UsbServerStub::DoGetDeviceSpeed(MessageParcel &data, MessageParcel &reply, MessageOption &option)
1136 {
1137     uint8_t busNum = 0;
1138     uint8_t devAddr = 0;
1139     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
1140     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
1141     uint8_t speed;
1142     int32_t ret = GetDeviceSpeed(busNum, devAddr, speed);
1143     if (ret == UEC_OK) {
1144         WRITE_PARCEL_WITH_RET(reply, Uint8, speed, UEC_SERVICE_WRITE_PARCEL_ERROR);
1145     } else {
1146         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
1147     }
1148     USB_HILOGE(MODULE_USBD, "DoGetDeviceSpeed speed:%{public}u", speed);
1149     return ret;
1150 }
1151 
1152 } // namespace USB
1153 } // namespace OHOS
1154