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