• 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 
23 using namespace OHOS::HDI::Usb::V1_0;
24 namespace OHOS {
25 namespace USB {
GetDeviceMessage(MessageParcel & data,uint8_t & busNum,uint8_t & devAddr)26 int32_t UsbServerStub::GetDeviceMessage(MessageParcel &data, uint8_t &busNum, uint8_t &devAddr)
27 {
28     if (!data.ReadUint8(busNum)) {
29         return UEC_SERVICE_READ_PARCEL_ERROR;
30     }
31     if (!data.ReadUint8(devAddr)) {
32         return UEC_SERVICE_READ_PARCEL_ERROR;
33     }
34     return UEC_OK;
35 }
36 
SetBufferMessage(MessageParcel & data,const std::vector<uint8_t> & bufferData)37 int32_t UsbServerStub::SetBufferMessage(MessageParcel &data, const std::vector<uint8_t> &bufferData)
38 {
39     uint32_t length = bufferData.size();
40     const uint8_t *ptr = bufferData.data();
41     if (!ptr) {
42         length = 0;
43     }
44 
45     if (!data.WriteUint32(length)) {
46         USB_HILOGE(MODULE_USBD, "write length failed length:%{public}u", length);
47         return UEC_SERVICE_WRITE_PARCEL_ERROR;
48     }
49     if ((ptr) && (length > 0) && !data.WriteBuffer(reinterpret_cast<const void *>(ptr), length)) {
50         USB_HILOGE(MODULE_USBD, "writer buffer failed length:%{public}u", length);
51         return UEC_SERVICE_WRITE_PARCEL_ERROR;
52     }
53     return UEC_OK;
54 }
55 
GetBufferMessage(MessageParcel & data,std::vector<uint8_t> & bufferData)56 int32_t UsbServerStub::GetBufferMessage(MessageParcel &data, std::vector<uint8_t> &bufferData)
57 {
58     uint32_t dataSize = 0;
59     bufferData.clear();
60     if (!data.ReadUint32(dataSize)) {
61         USB_HILOGE(MODULE_USBD, "read dataSize failed");
62         return UEC_SERVICE_READ_PARCEL_ERROR;
63     }
64     if (dataSize == 0) {
65         USB_HILOGW(MODULE_USBD, "size:%{public}u", dataSize);
66         return UEC_OK;
67     }
68 
69     const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
70     if (readData == nullptr) {
71         USB_HILOGE(MODULE_USBD, "failed size:%{public}u", dataSize);
72         return UEC_SERVICE_READ_PARCEL_ERROR;
73     }
74     std::vector<uint8_t> tdata(readData, readData + dataSize);
75     bufferData.swap(tdata);
76     return UEC_OK;
77 }
78 
StubDevice(uint32_t code,int32_t & result,MessageParcel & data,MessageParcel & reply,MessageOption & option)79 bool UsbServerStub::StubDevice(
80     uint32_t code, int32_t &result, MessageParcel &data, MessageParcel &reply, MessageOption &option)
81 {
82     switch (code) {
83         case static_cast<int>(UsbInterfaceCode::USB_FUN_OPEN_DEVICE):
84             result = DoOpenDevice(data, reply, option);
85             return true;
86         case static_cast<int>(UsbInterfaceCode::USB_FUN_HAS_RIGHT):
87             result = DoHasRight(data, reply, option);
88             return true;
89         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_RIGHT):
90             result = DoRequestRight(data, reply, option);
91             return true;
92         case static_cast<int>(UsbInterfaceCode::USB_FUN_REMOVE_RIGHT):
93             result = DoRemoveRight(data, reply, option);
94             return true;
95         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_PORTS):
96             result = DoGetPorts(data, reply, option);
97             return true;
98         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_SUPPORTED_MODES):
99             result = DoGetSupportedModes(data, reply, option);
100             return true;
101         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_PORT_ROLE):
102             result = DoSetPortRole(data, reply, option);
103             return true;
104         case static_cast<int>(UsbInterfaceCode::USB_FUN_CLAIM_INTERFACE):
105             result = DoClaimInterface(data, reply, option);
106             return true;
107         case static_cast<int>(UsbInterfaceCode::USB_FUN_RELEASE_INTERFACE):
108             result = DoReleaseInterface(data, reply, option);
109             return true;
110         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ):
111             result = DoBulkTransferRead(data, reply, option);
112             return true;
113         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_WRITE):
114             result = DoBulkTransferWrite(data, reply, option);
115             return true;
116         case static_cast<int>(UsbInterfaceCode::USB_FUN_CONTROL_TRANSFER):
117             result = DoControlTransfer(data, reply, option);
118             return true;
119         case static_cast<int>(UsbInterfaceCode::USB_FUN_REG_BULK_CALLBACK):
120             result = DoRegBulkCallback(data, reply, option);
121             return true;
122         case static_cast<int>(UsbInterfaceCode::USB_FUN_UNREG_BULK_CALLBACK):
123             result = DoUnRegBulkCallback(data, reply, option);
124             return true;
125         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_FILEDESCRIPTOR):
126             result = DoGetFileDescriptor(data, reply, option);
127             return true;
128         case static_cast<int>(UsbInterfaceCode::USB_FUN_ADD_RIGHT):
129             result = DoAddRight(data, reply, option);
130             return true;
131         default:;
132     }
133     return false;
134 }
135 
StubHost(uint32_t code,int32_t & result,MessageParcel & data,MessageParcel & reply,MessageOption & option)136 bool UsbServerStub::StubHost(
137     uint32_t code, int32_t &result, MessageParcel &data, MessageParcel &reply, MessageOption &option)
138 {
139     switch (code) {
140         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DEVICES):
141             result = DoGetDevices(data, reply, option);
142             return true;
143         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_CURRENT_FUNCTIONS):
144             result = DoGetCurrentFunctions(data, reply, option);
145             return true;
146         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_CURRENT_FUNCTIONS):
147             result = DoSetCurrentFunctions(data, reply, option);
148             return true;
149         case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_FROM_STRING):
150             result = DoUsbFunctionsFromString(data, reply, option);
151             return true;
152         case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_TO_STRING):
153             result = DoUsbFunctionsToString(data, reply, option);
154             return true;
155         case static_cast<int>(UsbInterfaceCode::USB_FUN_CLOSE_DEVICE):
156             result = DoClose(data, reply, option);
157             return true;
158         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_QUEUE):
159             result = DoRequestQueue(data, reply, option);
160             return true;
161         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_WAIT):
162             result = DoRequestWait(data, reply, option);
163             return true;
164         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_INTERFACE):
165             result = DoSetInterface(data, reply, option);
166             return true;
167         case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_ACTIVE_CONFIG):
168             result = DoSetActiveConfig(data, reply, option);
169             return true;
170         case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_CANCEL):
171             result = DoRequestCancel(data, reply, option);
172             return true;
173         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_READ):
174             result = DoBulkRead(data, reply, option);
175             return true;
176         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_WRITE):
177             result = DoBulkWrite(data, reply, option);
178             return true;
179         case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_CANCEL):
180             result = DoBulkCancel(data, reply, option);
181             return true;
182         case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DESCRIPTOR):
183             result = DoGetRawDescriptor(data, reply, option);
184             return true;
185         default:;
186     }
187     return false;
188 }
189 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)190 int32_t UsbServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
191 {
192     USB_HILOGD(MODULE_USB_SERVICE, "UsbServerStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code,
193         option.GetFlags());
194     std::u16string descriptor = UsbServerStub::GetDescriptor();
195     std::u16string remoteDescriptor = data.ReadInterfaceToken();
196     if (descriptor != remoteDescriptor) {
197         USB_HILOGE(MODULE_SERVICE, "UsbServerStub::OnRemoteRequest failed, descriptor is not matched!");
198         return UEC_SERVICE_INNER_ERR;
199     }
200 
201     int32_t ret = 0;
202     if (StubHost(code, ret, data, reply, option)) {
203         return ret;
204     } else if (StubDevice(code, ret, data, reply, option)) {
205         return ret;
206     } else {
207         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
208     }
209 
210     return UEC_OK;
211 }
212 
DoGetCurrentFunctions(MessageParcel & data,MessageParcel & reply,MessageOption & option)213 int32_t UsbServerStub::DoGetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option)
214 {
215     int32_t functions;
216     int32_t ret = GetCurrentFunctions(functions);
217     if (ret != UEC_OK) {
218         return ret;
219     }
220     WRITE_PARCEL_WITH_RET(reply, Int32, functions, UEC_SERVICE_WRITE_PARCEL_ERROR);
221     return UEC_OK;
222 }
223 
DoSetCurrentFunctions(MessageParcel & data,MessageParcel & reply,MessageOption & option)224 int32_t UsbServerStub::DoSetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option)
225 {
226     int32_t funcs;
227     READ_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
228     return SetCurrentFunctions(funcs);
229 }
230 
DoUsbFunctionsFromString(MessageParcel & data,MessageParcel & reply,MessageOption & option)231 int32_t UsbServerStub::DoUsbFunctionsFromString(MessageParcel &data, MessageParcel &reply, MessageOption &option)
232 {
233     std::string funcs;
234     READ_PARCEL_WITH_RET(data, String, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
235     WRITE_PARCEL_WITH_RET(reply, Int32, UsbFunctionsFromString(funcs), UEC_SERVICE_WRITE_PARCEL_ERROR);
236     return UEC_OK;
237 }
238 
DoUsbFunctionsToString(MessageParcel & data,MessageParcel & reply,MessageOption & option)239 int32_t UsbServerStub::DoUsbFunctionsToString(MessageParcel &data, MessageParcel &reply, MessageOption &option)
240 {
241     int32_t funcs;
242     READ_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
243     WRITE_PARCEL_WITH_RET(reply, String, UsbFunctionsToString(funcs), UEC_SERVICE_WRITE_PARCEL_ERROR);
244     return UEC_OK;
245 }
246 
DoOpenDevice(MessageParcel & data,MessageParcel & reply,MessageOption & option)247 int32_t UsbServerStub::DoOpenDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
248 {
249     uint8_t busNum = 0;
250     uint8_t devAddr = 0;
251     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
252     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
253     int32_t ret = OpenDevice(busNum, devAddr);
254     if (ret != UEC_OK) {
255         return ret;
256     }
257 
258     return UEC_OK;
259 }
260 
DoHasRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)261 int32_t UsbServerStub::DoHasRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
262 {
263     std::u16string deviceName = u"";
264     READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
265     WRITE_PARCEL_WITH_RET(reply, Bool, HasRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
266 
267     return UEC_OK;
268 }
269 
DoRequestRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)270 int32_t UsbServerStub::DoRequestRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
271 {
272     std::u16string deviceName = u"";
273     READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
274     WRITE_PARCEL_WITH_RET(reply, Int32, RequestRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
275     return UEC_OK;
276 }
277 
DoRemoveRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)278 int32_t UsbServerStub::DoRemoveRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
279 {
280     std::u16string deviceName = u"";
281     READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
282     WRITE_PARCEL_WITH_RET(reply, Int32, RemoveRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
283     return UEC_OK;
284 }
285 
DoGetPorts(MessageParcel & data,MessageParcel & reply,MessageOption & option)286 int32_t UsbServerStub::DoGetPorts(MessageParcel &data, MessageParcel &reply, MessageOption &option)
287 {
288     std::vector<UsbPort> ports;
289     int32_t ret = GetPorts(ports);
290     USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts ret %{public}d ", ret);
291     if (ret != UEC_OK) {
292         return ret;
293     }
294     uint32_t size = ports.size();
295     USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts size %{public}d ", size);
296     WRITE_PARCEL_WITH_RET(reply, Int32, size, UEC_SERVICE_WRITE_PARCEL_ERROR);
297     for (uint32_t i = 0; i < size; ++i) {
298         ret = WriteUsbPort(reply, ports[i]);
299         if (ret) {
300             return ret;
301         }
302     }
303     return ret;
304 }
305 
WriteUsbPort(MessageParcel & reply,const UsbPort & port)306 int32_t UsbServerStub::WriteUsbPort(MessageParcel &reply, const UsbPort &port)
307 {
308     WRITE_PARCEL_WITH_RET(reply, Int32, port.id, UEC_SERVICE_WRITE_PARCEL_ERROR);
309     WRITE_PARCEL_WITH_RET(reply, Int32, port.supportedModes, UEC_SERVICE_WRITE_PARCEL_ERROR);
310     WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentMode, UEC_SERVICE_WRITE_PARCEL_ERROR);
311     WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentPowerRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
312     WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentDataRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
313     USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts supportedModes %{public}d ", port.supportedModes);
314     return UEC_OK;
315 }
316 
DoGetSupportedModes(MessageParcel & data,MessageParcel & reply,MessageOption & option)317 int32_t UsbServerStub::DoGetSupportedModes(MessageParcel &data, MessageParcel &reply, MessageOption &option)
318 {
319     int32_t supportedModes = 0;
320     int32_t portId = 0;
321     READ_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
322     int32_t ret = GetSupportedModes(portId, supportedModes);
323     if (ret != UEC_OK) {
324         return ret;
325     }
326     WRITE_PARCEL_WITH_RET(reply, Int32, supportedModes, UEC_SERVICE_WRITE_PARCEL_ERROR);
327     return ret;
328 }
329 
DoSetPortRole(MessageParcel & data,MessageParcel & reply,MessageOption & option)330 int32_t UsbServerStub::DoSetPortRole(MessageParcel &data, MessageParcel &reply, MessageOption &option)
331 {
332     int32_t portId = 0;
333     int32_t powerRole = 0;
334     int32_t dataRole = 0;
335     READ_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
336     READ_PARCEL_WITH_RET(data, Int32, powerRole, UEC_SERVICE_READ_PARCEL_ERROR);
337     READ_PARCEL_WITH_RET(data, Int32, dataRole, UEC_SERVICE_READ_PARCEL_ERROR);
338     return SetPortRole(portId, powerRole, dataRole);
339 }
340 
DoClaimInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)341 int32_t UsbServerStub::DoClaimInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
342 {
343     uint8_t busNum = 0;
344     uint8_t devAddr = 0;
345     uint8_t interface = 0;
346     uint8_t force = 0;
347     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
348     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
349     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
350     READ_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_READ_PARCEL_ERROR);
351     WRITE_PARCEL_WITH_RET(
352         reply, Int32, ClaimInterface(busNum, devAddr, interface, force), UEC_SERVICE_WRITE_PARCEL_ERROR);
353     return UEC_OK;
354 }
355 
DoReleaseInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)356 int32_t UsbServerStub::DoReleaseInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
357 {
358     uint8_t busNum = 0;
359     uint8_t devAddr = 0;
360     uint8_t interface = 0;
361     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
362     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
363     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
364     WRITE_PARCEL_WITH_RET(reply, Int32, ReleaseInterface(busNum, devAddr, interface), UEC_SERVICE_WRITE_PARCEL_ERROR);
365     return UEC_OK;
366 }
367 
DoBulkTransferRead(MessageParcel & data,MessageParcel & reply,MessageOption & option)368 int32_t UsbServerStub::DoBulkTransferRead(MessageParcel &data, MessageParcel &reply, MessageOption &option)
369 {
370     uint8_t busNum = 0;
371     uint8_t devAddr = 0;
372     uint8_t interface = 0;
373     uint8_t endpoint = 0;
374     int32_t timeOut = 0;
375     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
376     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
377     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
378     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
379     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
380     std::vector<uint8_t> bufferData;
381     const UsbDev tmpDev = {busNum, devAddr};
382     const UsbPipe tmpPipe = {interface, endpoint};
383     int32_t ret = BulkTransferRead(tmpDev, tmpPipe, bufferData, timeOut);
384     if (ret != UEC_OK) {
385         USB_HILOGE(MODULE_USBD, "read failed ret:%{public}d", ret);
386         return ret;
387     }
388     ret = SetBufferMessage(reply, bufferData);
389     if (ret != UEC_OK) {
390         USB_HILOGE(MODULE_USBD, "set buffer failed ret:%{public}d", ret);
391     }
392     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
393     return ret;
394 }
395 
DoBulkTransferWrite(MessageParcel & data,MessageParcel & reply,MessageOption & option)396 int32_t UsbServerStub::DoBulkTransferWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option)
397 {
398     uint8_t busNum = 0;
399     uint8_t devAddr = 0;
400     uint8_t interface = 0;
401     uint8_t endpoint = 0;
402     int32_t timeOut = 0;
403     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
404     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
405     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
406     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
407     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
408     std::vector<uint8_t> bufferData;
409     const UsbDev tmpDev = {busNum, devAddr};
410     const UsbPipe tmpPipe = {interface, endpoint};
411     int32_t ret = GetBufferMessage(data, bufferData);
412     if (ret != UEC_OK) {
413         USB_HILOGE(MODULE_USBD, "GetBufferMessage failedret:%{public}d", ret);
414         return ret;
415     }
416     ret = BulkTransferWrite(tmpDev, tmpPipe, bufferData, timeOut);
417     if (ret != UEC_OK) {
418         USB_HILOGE(MODULE_USBD, "BulkTransferWrite error ret:%{public}d", ret);
419     }
420     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
421     return ret;
422 }
423 
DoControlTransfer(MessageParcel & data,MessageParcel & reply,MessageOption & option)424 int32_t UsbServerStub::DoControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
425 {
426     uint8_t busNum = 0;
427     uint8_t devAddr = 0;
428     int32_t requestType;
429     int32_t request;
430     int32_t value;
431     int32_t index;
432     int32_t timeOut;
433 
434     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
435     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
436     READ_PARCEL_WITH_RET(data, Int32, requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
437     READ_PARCEL_WITH_RET(data, Int32, request, UEC_SERVICE_WRITE_PARCEL_ERROR);
438     READ_PARCEL_WITH_RET(data, Int32, value, UEC_SERVICE_WRITE_PARCEL_ERROR);
439     READ_PARCEL_WITH_RET(data, Int32, index, UEC_SERVICE_WRITE_PARCEL_ERROR);
440     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
441     std::vector<uint8_t> bufferData;
442     int32_t ret = GetBufferMessage(data, bufferData);
443     if (ret != UEC_OK) {
444         USB_HILOGE(MODULE_USBD, "get error ret:%{public}d", ret);
445         return ret;
446     }
447 
448     bool bWrite = ((static_cast<uint32_t>(requestType) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
449     const UsbDev tmpDev = {busNum, devAddr};
450     const UsbCtrlTransfer tctrl = {requestType, request, value, index, timeOut};
451     ret = ControlTransfer(tmpDev, tctrl, bufferData);
452     if (ret != UEC_OK) {
453         USB_HILOGE(MODULE_USBD, "ControlTransfer error ret:%{public}d", ret);
454         return ret;
455     }
456 
457     if (!bWrite) {
458         ret = SetBufferMessage(reply, bufferData);
459         if (ret != UEC_OK) {
460             USB_HILOGE(MODULE_USBD, "Set buffer message error length = %{public}d", ret);
461         }
462     }
463     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
464     return UEC_OK;
465 }
466 
DoSetActiveConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)467 int32_t UsbServerStub::DoSetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
468 {
469     uint8_t busNum = 0;
470     uint8_t devAddr = 0;
471     uint8_t config = 0;
472     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
473     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
474     READ_PARCEL_WITH_RET(data, Uint8, config, UEC_SERVICE_WRITE_PARCEL_ERROR);
475     WRITE_PARCEL_WITH_RET(reply, Int32, SetActiveConfig(busNum, devAddr, config), UEC_SERVICE_WRITE_PARCEL_ERROR);
476     return UEC_OK;
477 }
478 
DoGetActiveConfig(MessageParcel & data,MessageParcel & reply,MessageOption & option)479 int32_t UsbServerStub::DoGetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
480 {
481     uint8_t busNum = 0;
482     uint8_t devAddr = 0;
483     uint8_t config = 0;
484     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
485     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
486     int32_t ret = GetActiveConfig(busNum, devAddr, config);
487     if (ret == UEC_OK) {
488         WRITE_PARCEL_WITH_RET(reply, Uint8, config, UEC_SERVICE_WRITE_PARCEL_ERROR);
489     }
490     return ret;
491 }
492 
DoSetInterface(MessageParcel & data,MessageParcel & reply,MessageOption & option)493 int32_t UsbServerStub::DoSetInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
494 {
495     uint8_t busNum = 0;
496     uint8_t devAddr = 0;
497     uint8_t interfaceId = 0;
498     uint8_t altIndex = 0;
499     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
500     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
501     READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
502     READ_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
503     WRITE_PARCEL_WITH_RET(
504         reply, Int32, SetInterface(busNum, devAddr, interfaceId, altIndex), UEC_SERVICE_WRITE_PARCEL_ERROR);
505     return UEC_OK;
506 }
507 
DoGetRawDescriptor(MessageParcel & data,MessageParcel & reply,MessageOption & option)508 int32_t UsbServerStub::DoGetRawDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option)
509 {
510     uint8_t busNum = 0;
511     uint8_t devAddr = 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     std::vector<uint8_t> bufferData;
515     int32_t ret = GetRawDescriptor(busNum, devAddr, bufferData);
516     if (ret == UEC_OK) {
517         ret = SetBufferMessage(reply, bufferData);
518         if (ret != UEC_OK) {
519             USB_HILOGE(MODULE_USBD, "SetBufferMessage failed ret:%{public}d", ret);
520         }
521     } else {
522         USB_HILOGW(MODULE_USBD, "GetRawDescriptor failed ret:%{public}d", ret);
523     }
524     return ret;
525 }
526 
DoGetFileDescriptor(MessageParcel & data,MessageParcel & reply,MessageOption & option)527 int32_t UsbServerStub::DoGetFileDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option)
528 {
529     uint8_t busNum = 0;
530     uint8_t devAddr = 0;
531     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
532     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
533     int32_t fd = -1;
534     int32_t ret = GetFileDescriptor(busNum, devAddr, fd);
535     if (ret == UEC_OK) {
536         WRITE_PARCEL_WITH_RET(reply, Int32, fd, UEC_SERVICE_WRITE_PARCEL_ERROR);
537     } else {
538         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
539     }
540     return ret;
541 }
542 
DoRequestQueue(MessageParcel & data,MessageParcel & reply,MessageOption & option)543 int32_t UsbServerStub::DoRequestQueue(MessageParcel &data, MessageParcel &reply, MessageOption &option)
544 {
545     uint8_t busNum = 0;
546     uint8_t devAddr = 0;
547     uint8_t ifId = 0;
548     uint8_t endpoint = 0;
549     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
550     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
551     READ_PARCEL_WITH_RET(data, Uint8, ifId, UEC_SERVICE_WRITE_PARCEL_ERROR);
552     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
553     std::vector<uint8_t> clientData;
554     std::vector<uint8_t> bufferData;
555 
556     int32_t ret = UsbServerStub::GetBufferMessage(data, clientData);
557     if (ret != UEC_OK) {
558         USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed  ret:%{public}d", ret);
559         return ret;
560     }
561     ret = UsbServerStub::GetBufferMessage(data, bufferData);
562     if (ret != UEC_OK) {
563         USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed  ret:%{public}d", ret);
564         return ret;
565     }
566     const UsbDev tmpDev = {busNum, devAddr};
567     const UsbPipe tmpPipe = {ifId, endpoint};
568     ret = RequestQueue(tmpDev, tmpPipe, clientData, bufferData);
569     if (ret != UEC_OK) {
570         USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed ret:%{public}d", ret);
571     }
572     return ret;
573 }
574 
DoRequestWait(MessageParcel & data,MessageParcel & reply,MessageOption & option)575 int32_t UsbServerStub::DoRequestWait(MessageParcel &data, MessageParcel &reply, MessageOption &option)
576 {
577     uint8_t busNum = 0;
578     uint8_t devAddr = 0;
579     int32_t timeOut = 0;
580     std::vector<uint8_t> clientData;
581     std::vector<uint8_t> bufferData;
582     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
583     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
584     READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
585 
586     const UsbDev tmpDev = {busNum, devAddr};
587     int32_t ret = RequestWait(tmpDev, timeOut, clientData, bufferData);
588     if (ret != UEC_OK) {
589         USB_HILOGE(MODULE_USB_INNERKIT, "RequestWait failed ret:%{public}d", ret);
590         return ret;
591     }
592 
593     ret = SetBufferMessage(reply, clientData);
594     if (ret != UEC_OK) {
595         USB_HILOGE(MODULE_USB_INNERKIT, "Set clientData failed ret:%{public}d", ret);
596         return ret;
597     }
598 
599     ret = SetBufferMessage(reply, bufferData);
600     if (ret != UEC_OK) {
601         USB_HILOGE(MODULE_USB_INNERKIT, "Set bufferData failed ret:%{public}d", ret);
602         return ret;
603     }
604     return ret;
605 }
606 
DoRequestCancel(MessageParcel & data,MessageParcel & reply,MessageOption & option)607 int32_t UsbServerStub::DoRequestCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option)
608 {
609     uint8_t busNum = 0;
610     uint8_t devAddr = 0;
611     uint8_t interfaceId = 0;
612     uint8_t endpointId = 0;
613     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
614     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
615     READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
616     READ_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
617     int32_t ret = RequestCancel(busNum, devAddr, interfaceId, endpointId);
618     if (ret != UEC_OK) {
619         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret:%{public}d", ret);
620     }
621     return ret;
622 }
623 
DoClose(MessageParcel & data,MessageParcel & reply,MessageOption & option)624 int32_t UsbServerStub::DoClose(MessageParcel &data, MessageParcel &reply, MessageOption &option)
625 {
626     uint8_t busNum = 0;
627     uint8_t devAddr = 0;
628     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
629     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
630     int32_t ret = Close(busNum, devAddr);
631     if (ret != UEC_OK) {
632         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret:%{public}d", ret);
633     }
634     WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
635     return ret;
636 }
637 
DoGetDevices(MessageParcel & data,MessageParcel & reply,MessageOption & option)638 int32_t UsbServerStub::DoGetDevices(MessageParcel &data, MessageParcel &reply, MessageOption &option)
639 {
640     std::vector<UsbDevice> deviceList;
641     int32_t ret = GetDevices(deviceList);
642     if (ret != UEC_OK) {
643         USB_HILOGE(MODULE_SERVICE, "GetDevices failed ret = %{public}d", ret);
644         return ret;
645     }
646     USB_HILOGI(MODULE_SERVICE, "list size = %{public}zu", deviceList.size());
647     ret = SetDeviceListMessageParcel(deviceList, reply);
648     if (ret != UEC_OK) {
649         USB_HILOGE(MODULE_USB_INNERKIT, "SetDeviceListMessageParcel failed ret:%{public}d", ret);
650     }
651     return ret;
652 }
653 
SetDeviceListMessageParcel(std::vector<UsbDevice> & deviceList,MessageParcel & data)654 int32_t UsbServerStub::SetDeviceListMessageParcel(std::vector<UsbDevice> &deviceList, MessageParcel &data)
655 {
656     int32_t deviceCount = (int32_t)deviceList.size();
657     WRITE_PARCEL_WITH_RET(data, Int32, deviceCount, UEC_SERVICE_WRITE_PARCEL_ERROR);
658     for (int32_t i = 0; i < deviceCount; ++i) {
659         UsbDevice &devInfo = deviceList[i];
660         int32_t ret = SetDeviceMessageParcel(devInfo, data);
661         if (ret) {
662             return ret;
663         }
664     }
665     return UEC_OK;
666 }
667 
SetDeviceMessageParcel(UsbDevice & devInfo,MessageParcel & data)668 int32_t UsbServerStub::SetDeviceMessageParcel(UsbDevice &devInfo, MessageParcel &data)
669 {
670     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetBusNum(), UEC_SERVICE_WRITE_PARCEL_ERROR);
671     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetDevAddr(), UEC_SERVICE_WRITE_PARCEL_ERROR);
672 
673     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetVendorId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
674     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetProductId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
675     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
676     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetSubclass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
677     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetProtocol(), UEC_SERVICE_WRITE_PARCEL_ERROR);
678     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiManufacturer(), UEC_SERVICE_WRITE_PARCEL_ERROR);
679     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiProduct(), UEC_SERVICE_WRITE_PARCEL_ERROR);
680     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiSerialNumber(), UEC_SERVICE_WRITE_PARCEL_ERROR);
681     WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetbMaxPacketSize0(), UEC_SERVICE_WRITE_PARCEL_ERROR);
682     WRITE_PARCEL_WITH_RET(data, Uint16, devInfo.GetbcdUSB(), UEC_SERVICE_WRITE_PARCEL_ERROR);
683     WRITE_PARCEL_WITH_RET(data, Uint16, devInfo.GetbcdDevice(), UEC_SERVICE_WRITE_PARCEL_ERROR);
684     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
685     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetManufacturerName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
686     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetProductName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
687     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetVersion()), UEC_SERVICE_WRITE_PARCEL_ERROR);
688     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetmSerial()), UEC_SERVICE_WRITE_PARCEL_ERROR);
689 
690     USB_HILOGE(MODULE_USB_INNERKIT, "devInfo:%{public}s", devInfo.ToString().c_str());
691     WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetConfigCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
692     return SetDeviceConfigsMessageParcel(devInfo.GetConfigs(), data);
693 }
694 
SetDeviceConfigsMessageParcel(std::vector<USBConfig> & configs,MessageParcel & data)695 int32_t UsbServerStub::SetDeviceConfigsMessageParcel(std::vector<USBConfig> &configs, MessageParcel &data)
696 {
697     for (auto it = configs.begin(); it != configs.end(); ++it) {
698         USBConfig config = *it;
699         WRITE_PARCEL_WITH_RET(data, Int32, config.GetId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
700         WRITE_PARCEL_WITH_RET(data, Uint32, config.GetAttributes(), UEC_SERVICE_WRITE_PARCEL_ERROR);
701         WRITE_PARCEL_WITH_RET(data, Int32, config.GetMaxPower(), UEC_SERVICE_WRITE_PARCEL_ERROR);
702 
703         WRITE_PARCEL_WITH_RET(data, Uint8, config.GetiConfiguration(), UEC_SERVICE_WRITE_PARCEL_ERROR);
704         WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(config.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
705 
706         WRITE_PARCEL_WITH_RET(data, Uint32, config.GetInterfaceCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
707         USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", config.ToString().c_str());
708         int32_t ret = SetDeviceInterfacesMessageParcel(config.GetInterfaces(), data);
709         if (ret) {
710             return ret;
711         }
712     }
713     return UEC_OK;
714 }
715 
SetDeviceInterfacesMessageParcel(std::vector<UsbInterface> & interfaces,MessageParcel & data)716 int32_t UsbServerStub::SetDeviceInterfacesMessageParcel(std::vector<UsbInterface> &interfaces, MessageParcel &data)
717 {
718     for (auto it = interfaces.begin(); it != interfaces.end(); ++it) {
719         UsbInterface interface = *it;
720         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
721         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
722         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetSubClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
723         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetAlternateSetting(), UEC_SERVICE_WRITE_PARCEL_ERROR);
724         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetProtocol(), UEC_SERVICE_WRITE_PARCEL_ERROR);
725 
726         WRITE_PARCEL_WITH_RET(data, Uint8, interface.GetiInterface(), UEC_SERVICE_WRITE_PARCEL_ERROR);
727         WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(interface.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
728 
729         WRITE_PARCEL_WITH_RET(data, Int32, interface.GetEndpointCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
730         USB_HILOGI(MODULE_USB_SERVICE, "interface=%{public}s", interface.ToString().c_str());
731         int32_t ret = SetDeviceEndpointsMessageParcel(interface.GetEndpoints(), data);
732         if (ret) {
733             return ret;
734         }
735     }
736     return UEC_OK;
737 }
738 
SetDeviceEndpointsMessageParcel(std::vector<USBEndpoint> & eps,MessageParcel & data)739 int32_t UsbServerStub::SetDeviceEndpointsMessageParcel(std::vector<USBEndpoint> &eps, MessageParcel &data)
740 {
741     for (auto it = eps.begin(); it != eps.end(); ++it) {
742         USBEndpoint ep = *it;
743         WRITE_PARCEL_WITH_RET(data, Uint32, ep.GetAddress(), UEC_SERVICE_WRITE_PARCEL_ERROR);
744         WRITE_PARCEL_WITH_RET(data, Uint32, ep.GetAttributes(), UEC_SERVICE_WRITE_PARCEL_ERROR);
745         WRITE_PARCEL_WITH_RET(data, Int32, ep.GetInterval(), UEC_SERVICE_WRITE_PARCEL_ERROR);
746         WRITE_PARCEL_WITH_RET(data, Int32, ep.GetMaxPacketSize(), UEC_SERVICE_WRITE_PARCEL_ERROR);
747         USB_HILOGI(MODULE_USB_SERVICE, "ep=%{public}s", ep.ToString().c_str());
748     }
749     return UEC_OK;
750 }
751 
DoRegBulkCallback(MessageParcel & data,MessageParcel & reply,MessageOption & option)752 int32_t UsbServerStub::DoRegBulkCallback(MessageParcel &data, MessageParcel &reply, MessageOption &option)
753 {
754     uint8_t busNum = 0;
755     uint8_t devAddr = 0;
756     uint8_t interface = 0;
757     uint8_t endpoint = 0;
758     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
759     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
760     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
761     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
762     const sptr<IRemoteObject> cb = data.ReadRemoteObject();
763     const UsbDev tmpDev = {busNum, devAddr};
764     const UsbPipe tmpPipe = {interface, endpoint};
765     int32_t ret = RegBulkCallback(tmpDev, tmpPipe, cb);
766     if (ret != UEC_OK) {
767         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
768         return ret;
769     }
770     return ret;
771 }
772 
DoUnRegBulkCallback(MessageParcel & data,MessageParcel & reply,MessageOption & option)773 int32_t UsbServerStub::DoUnRegBulkCallback(MessageParcel &data, MessageParcel &reply, MessageOption &option)
774 {
775     uint8_t busNum = 0;
776     uint8_t devAddr = 0;
777     uint8_t interface = 0;
778     uint8_t endpoint = 0;
779     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
780     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
781     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
782     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
783     const UsbDev tmpDev = {busNum, devAddr};
784     const UsbPipe tmpPipe = {interface, endpoint};
785     int32_t ret = UnRegBulkCallback(tmpDev, tmpPipe);
786     if (ret != UEC_OK) {
787         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
788         return ret;
789     }
790     return ret;
791 }
792 
DoBulkRead(MessageParcel & data,MessageParcel & reply,MessageOption & option)793 int32_t UsbServerStub::DoBulkRead(MessageParcel &data, MessageParcel &reply, MessageOption &option)
794 {
795     uint8_t busNum = 0;
796     uint8_t devAddr = 0;
797     uint8_t interface = 0;
798     uint8_t endpoint = 0;
799     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
800     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
801     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
802     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
803     sptr<Ashmem> ashmem = data.ReadAshmem();
804     const UsbDev tmpDev = {busNum, devAddr};
805     const UsbPipe tmpPipe = {interface, endpoint};
806     int32_t ret = BulkRead(tmpDev, tmpPipe, ashmem);
807     if (ret != UEC_OK) {
808         USB_HILOGE(MODULE_USBD, "BulkRead failed ret:%{public}d", ret);
809         return ret;
810     }
811     return ret;
812 }
813 
DoBulkWrite(MessageParcel & data,MessageParcel & reply,MessageOption & option)814 int32_t UsbServerStub::DoBulkWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option)
815 {
816     uint8_t busNum = 0;
817     uint8_t devAddr = 0;
818     uint8_t interface = 0;
819     uint8_t endpoint = 0;
820     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
821     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
822     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
823     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
824     sptr<Ashmem> ashmem = data.ReadAshmem();
825     const UsbDev tmpDev = {busNum, devAddr};
826     const UsbPipe tmpPipe = {interface, endpoint};
827     int32_t ret = BulkWrite(tmpDev, tmpPipe, ashmem);
828     if (ret != UEC_OK) {
829         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
830         return ret;
831     }
832     return ret;
833 }
834 
DoBulkCancel(MessageParcel & data,MessageParcel & reply,MessageOption & option)835 int32_t UsbServerStub::DoBulkCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option)
836 {
837     uint8_t busNum = 0;
838     uint8_t devAddr = 0;
839     uint8_t interface = 0;
840     uint8_t endpoint = 0;
841     READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
842     READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
843     READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
844     READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
845     const UsbDev tmpDev = {busNum, devAddr};
846     const UsbPipe tmpPipe = {interface, endpoint};
847     int32_t ret = BulkCancel(tmpDev, tmpPipe);
848     if (ret != UEC_OK) {
849         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
850         return ret;
851     }
852     return ret;
853 }
854 
DoAddRight(MessageParcel & data,MessageParcel & reply,MessageOption & option)855 int32_t UsbServerStub::DoAddRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
856 {
857     std::string bundleName;
858     std::string deviceName;
859     READ_PARCEL_WITH_RET(data, String, bundleName, UEC_SERVICE_READ_PARCEL_ERROR);
860     READ_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
861     int32_t ret = AddRight(bundleName, deviceName);
862     if (ret != UEC_OK) {
863         USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
864     }
865     return ret;
866 }
867 } // namespace USB
868 } // namespace OHOS
869