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