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