diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 1c504020332b11b3396f3b3fddf43aa202dd19ca..8fbbd89366b7651acce85cd1d0976b45fc1814af 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -259,7 +259,7 @@ private: bool InitSerial(); int32_t GetDeviceVidPidSerialNumber(int32_t portId, std::string& deviceName, std::string& strDesc); void UpdateDeviceVidPidMap(std::vector& serialPortList); - bool DoDump(int fd, const std::vector &argList); + int DoDump(int fd, const std::vector &argList); void FreeTokenId(int32_t portId, uint32_t tokenId); int32_t ValidateUsbSerialManagerAndPort(int32_t portId); int32_t CheckDbAbility(int32_t portId); diff --git a/services/native/src/usb_device_manager.cpp b/services/native/src/usb_device_manager.cpp index 4892d5bbaeadcc3ca3de1b289a6c9cecac78d86d..a0304e9ea4db03e30351518e6047f1c31fc24a28 100644 --- a/services/native/src/usb_device_manager.cpp +++ b/services/native/src/usb_device_manager.cpp @@ -547,6 +547,19 @@ void UsbDeviceManager::GetDumpHelp(int32_t fd) dprintf(fd, "========= dump the all device function =========\n"); dprintf(fd, "usb_device -a: Query all function\n"); dprintf(fd, "usb_device -f Q: Query Current function\n"); +#ifdef USB_MANAGER_HIDUMPER_SET + dprintf(fd, "usb_device -f 0: Switch to function:none\n"); + dprintf(fd, "usb_device -f 1: Switch to function:acm\n"); + dprintf(fd, "usb_device -f 2: Switch to function:ecm\n"); + dprintf(fd, "usb_device -f 3: Switch to function:acm&ecm\n"); + dprintf(fd, "usb_device -f 4: Switch to function:hdc\n"); + dprintf(fd, "usb_device -f 5: Switch to function:acm&hdc\n"); + dprintf(fd, "usb_device -f 6: Switch to function:ecm&hdc\n"); + dprintf(fd, "usb_device -f 32: Switch to function:rndis\n"); + dprintf(fd, "usb_device -f 512:Switch to function:storage\n"); + dprintf(fd, "usb_device -f 36: Switch to function:rndis&hdc\n"); + dprintf(fd, "usb_device -f 516:Switch to function:storage&hdc\n"); +#endif // USB_MANAGER_HIDUMPER_SET dprintf(fd, "------------------------------------------------\n"); } @@ -592,8 +605,34 @@ void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args) dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str()); return; } +#ifdef USB_MANAGER_HIDUMPER_SET + if (!std::regex_match(args, std::regex("^[0-9]+$"))) { + dprintf(fd, "Invalid input, please enter a valid integer\n"); + GetDumpHelp(fd); + return; + } + int32_t mode; + if (!StringToInteger(args, mode)) { + dprintf(fd, "Invalid input, the number is out of range\n"); + GetDumpHelp(fd); + return; + } + ret = usbDeviceInterface_->SetCurrentFunctions(mode); + if (ret != UEC_OK) { + dprintf(fd, "SetCurrentFunctions failed"); + return; + } + ret = usbDeviceInterface_->GetCurrentFunctions(currentFunction); + if (ret != UEC_OK) { + dprintf(fd, "GetCurrentFunctions failed: %d\n", ret); + return; + } + + dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str()); +#else dprintf(fd, "Invalid input, please enter a valid argument\n"); GetDumpHelp(fd); +#endif // USB_MANAGER_HIDUMPER_SET } #else void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args) @@ -613,8 +652,33 @@ void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args) dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str()); return; } +#ifdef USB_MANAGER_HIDUMPER_SET + if (!std::regex_match(args, std::regex("^[0-9]+$"))) { + dprintf(fd, "Invalid input, please enter a valid integer\n"); + GetDumpHelp(fd); + return; + } + int32_t mode; + if (!StringToInteger(args, mode)) { + dprintf(fd, "Invalid input, the number is out of range\n"); + GetDumpHelp(fd); + return; + } + ret = usbd_->SetCurrentFunctions(mode); + if (ret != UEC_OK) { + dprintf(fd, "SetCurrentFunctions failed"); + return; + } + ret = usbd_->GetCurrentFunctions(currentFunction); + if (ret != UEC_OK) { + dprintf(fd, "GetCurrentFunctions failed: %d\n", ret); + return; + } + dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str()); +#else dprintf(fd, "Invalid input, please enter a valid argument\n"); GetDumpHelp(fd); +#endif // USB_MANAGER_HIDUMPER_SET } #endif // USB_MANAGER_V2_0 diff --git a/services/native/src/usb_port_manager.cpp b/services/native/src/usb_port_manager.cpp index 5904b22793eb447af038bde0adf0639537f1f61a..48d65b98cfa41f9d73d975383d4098d5187e1e14 100644 --- a/services/native/src/usb_port_manager.cpp +++ b/services/native/src/usb_port_manager.cpp @@ -36,6 +36,9 @@ constexpr int32_t SUPPORTED_MODES = 3; constexpr int32_t PARAM_COUNT_TWO = 2; constexpr int32_t PARAM_COUNT_THR = 3; constexpr int32_t DEFAULT_ROLE_HOST = 1; +#ifdef USB_MANAGER_HIDUMPER_SET +constexpr int32_t DEFAULT_ROLE_DEVICE = 2; +#endif // USB_MANAGER_HIDUMPER_SET constexpr uint32_t CMD_INDEX = 1; constexpr uint32_t PARAM_INDEX = 2; constexpr int32_t HOST_MODE = 2; @@ -415,6 +418,10 @@ void UsbPortManager::GetDumpHelp(int32_t fd) dprintf(fd, "=========== dump the all device port ===========\n"); dprintf(fd, "usb_port -a: Query All Port List\n"); dprintf(fd, "usb_port -p Q: Query Port\n"); +#ifdef USB_MANAGER_HIDUMPER_SET + dprintf(fd, "usb_port -p 1: Switch to host\n"); + dprintf(fd, "usb_port -p 2: Switch to device\n"); +#endif // USB_MANAGER_HIDUMPER_SET dprintf(fd, "------------------------------------------------\n"); } @@ -440,12 +447,47 @@ void UsbPortManager::DumpGetSupportPort(int32_t fd) void UsbPortManager::DumpSetPortRoles(int32_t fd, const std::string &args) { +#ifdef USB_MANAGER_V2_0 + if (usbPortInterface_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::DumpSetPortRoles usbPortInterface_ is nullptr"); + return; + } +#else + if (usbd_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::DumpSetPortRoles usbd_ is nullptr"); + return; + } +#endif // USB_MANAGER_V2_0 if (args.compare("Q") == 0) { GetPortsInfo(fd); return; } +#ifdef USB_MANAGER_HIDUMPER_SET + if (!std::regex_match(args, std::regex("^[0-9]+$"))) { + dprintf(fd, "Invalid input, please enter a valid integer\n"); + GetDumpHelp(fd); + return; + } + int32_t mode = stoi(args); + switch (mode) { + case DEFAULT_ROLE_HOST: + SetPortRole( + UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SOURCE, UsbSrvSupport::DATA_ROLE_HOST); + GetPortsInfo(fd); + break; + case DEFAULT_ROLE_DEVICE: + SetPortRole( + UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SINK, UsbSrvSupport::DATA_ROLE_DEVICE); + GetPortsInfo(fd); + break; + default: + dprintf(fd, "port param error, please enter again\n"); + GetDumpHelp(fd); + } +#else dprintf(fd, "port param error, please enter again\n"); GetDumpHelp(fd); +#endif // USB_MANAGER_HIDUMPER_SET } void UsbPortManager::Dump(int32_t fd, const std::vector &args) diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index 2c764ea846046570ef14f5f02ea8929396851c72..ee31c5327f6be7af9a9db1d13e6a8a6d738207b0 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -2198,36 +2198,51 @@ bool UsbService::GetBundleName(std::string &bundleName) // LCOV_EXCL_STOP // LCOV_EXCL_START -bool UsbService::DoDump(int fd, const std::vector &argList) +int UsbService::DoDump(int fd, const std::vector &argList) { #ifdef USB_MANAGER_FEATURE_HOST - if (usbHostManager_ == nullptr) { - USB_HILOGE(MODULE_USB_SERVICE, "usbHostManager_ is nullptr"); - return false; - } if (argList[0] == USB_HOST) { + if (usbHostManager_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "usbHostManager_ is nullptr"); + return UEC_SERVICE_INVALID_VALUE; + } usbHostManager_->Dump(fd, argList[1]); + return UEC_OK; } #endif // USB_MANAGER_FEATURE_HOST #ifdef USB_MANAGER_FEATURE_DEVICE - if (usbDeviceManager_ == nullptr) { - USB_HILOGE(MODULE_USB_SERVICE, "usbDeviceManager_ is nullptr"); - return false; - } if (argList[0] == USB_DEVICE) { + if (usbDeviceManager_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "usbDeviceManager_ is nullptr"); + return UEC_SERVICE_INVALID_VALUE; + } usbDeviceManager_->Dump(fd, argList); + return UEC_OK; } #endif // USB_MANAGER_FEATURE_DEVICE #ifdef USB_MANAGER_FEATURE_PORT - if (usbPortManager_ == nullptr) { - USB_HILOGE(MODULE_USB_SERVICE, "usbPortManager_ is nullptr"); - return false; - } if (argList[0] == USB_PORT) { + if (usbPortManager_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "usbPortManager_ is nullptr"); + return UEC_SERVICE_INVALID_VALUE; + } usbPortManager_->Dump(fd, argList); + return UEC_OK; } #endif // USB_MANAGER_FEATURE_PORT - return true; + if (argList[0] == USB_HELP) { + DumpHelp(fd); + usbSerialManager_->ListGetDumpHelp(fd); + } else if (argList[0] == USB_LIST) { + usbSerialManager_->SerialPortListDump(fd, argList); + } else if (argList[0] == USB_GETT) { + usbSerialManager_->SerialGetAttributeDump(fd, argList); + } else { + dprintf(fd, "Usb Dump service:invalid parameter.\n"); + DumpHelp(fd); + usbSerialManager_->ListGetDumpHelp(fd); + } + return UEC_OK; } // LCOV_EXCL_STOP @@ -2258,22 +2273,7 @@ int UsbService::Dump(int fd, const std::vector &args) usbSerialManager_->ListGetDumpHelp(fd); return UEC_SERVICE_INVALID_VALUE; } - if (!DoDump(fd, argList)) { - return UEC_SERVICE_INVALID_VALUE; - } - if (argList[0] == USB_HELP) { - DumpHelp(fd); - usbSerialManager_->ListGetDumpHelp(fd); - } else if (argList[0] == USB_LIST) { - usbSerialManager_->SerialPortListDump(fd, argList); - } else if (argList[0] == USB_GETT) { - usbSerialManager_->SerialGetAttributeDump(fd, argList); - } else { - dprintf(fd, "Usb Dump service:invalid parameter.\n"); - DumpHelp(fd); - usbSerialManager_->ListGetDumpHelp(fd); - } - return UEC_OK; + return DoDump(fd, argList); } // LCOV_EXCL_STOP