From 0a403405361ad4e89df039f7da5fd459c3502037 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 15 Aug 2025 11:13:41 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhidumper=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=92=8C=E5=8A=9F=E8=83=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/BUILD.gn | 3 + services/native/include/usb_service.h | 2 +- services/native/src/usb_device_manager.cpp | 64 ++++++++++++++++++++++ services/native/src/usb_port_manager.cpp | 42 ++++++++++++++ services/native/src/usb_service.cpp | 60 ++++++++++---------- usbmgr.gni | 1 + 6 files changed, 141 insertions(+), 31 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index f1f81cba..993cb780 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -57,6 +57,9 @@ ohos_shared_library("usbservice") { public_configs = [ ":usbsrv_public_config" ] deps = [ "${usb_manager_path}/interfaces/innerkits:usb_server_stub" ] defines = [] + if (usb_manager_hidumper_set) { + defines += [ "USB_MANAGER_HIDUMPER_SET" ] + } if (usb_manager_pass_through) { defines += [ "USB_MANAGER_PASS_THROUGH", diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 1c504020..8fbbd893 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 4892d5bb..a0304e9e 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 5904b227..ffb60c26 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,10 +447,45 @@ 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); + } + return +#endif // USB_MANAGER_HIDUMPER_SET dprintf(fd, "port param error, please enter again\n"); GetDumpHelp(fd); } diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index 2c764ea8..ee31c532 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 diff --git a/usbmgr.gni b/usbmgr.gni index 6dbbf41c..2e9bdc26 100644 --- a/usbmgr.gni +++ b/usbmgr.gni @@ -22,6 +22,7 @@ declare_args() { usb_manager_feature_host = true usb_manager_feature_device = true usb_manager_feature_port = true + usb_manager_hidumper_set = false usb_manager_pass_through = true usb_manager_peripheral_fault_notifier = false } -- Gitee From ed1821eae75b4e74adba55f83a0aa1f568cde651 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 15 Aug 2025 11:21:44 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhidumper=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=92=8C=E5=8A=9F=E8=83=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_device_manager.cpp | 1 + services/native/src/usb_port_manager.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/services/native/src/usb_device_manager.cpp b/services/native/src/usb_device_manager.cpp index a0304e9e..d19673d3 100644 --- a/services/native/src/usb_device_manager.cpp +++ b/services/native/src/usb_device_manager.cpp @@ -675,6 +675,7 @@ void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args) return; } dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str()); + return; #else dprintf(fd, "Invalid input, please enter a valid argument\n"); GetDumpHelp(fd); diff --git a/services/native/src/usb_port_manager.cpp b/services/native/src/usb_port_manager.cpp index ffb60c26..ff07b71d 100644 --- a/services/native/src/usb_port_manager.cpp +++ b/services/native/src/usb_port_manager.cpp @@ -484,7 +484,7 @@ void UsbPortManager::DumpSetPortRoles(int32_t fd, const std::string &args) dprintf(fd, "port param error, please enter again\n"); GetDumpHelp(fd); } - return + return; #endif // USB_MANAGER_HIDUMPER_SET dprintf(fd, "port param error, please enter again\n"); GetDumpHelp(fd); -- Gitee From b344c5d834889c5e077180193205a885dc063a1a Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 15 Aug 2025 11:23:28 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhidumper=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=92=8C=E5=8A=9F=E8=83=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/native/src/usb_device_manager.cpp | 1 - services/native/src/usb_port_manager.cpp | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/services/native/src/usb_device_manager.cpp b/services/native/src/usb_device_manager.cpp index d19673d3..a0304e9e 100644 --- a/services/native/src/usb_device_manager.cpp +++ b/services/native/src/usb_device_manager.cpp @@ -675,7 +675,6 @@ void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args) return; } dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str()); - return; #else dprintf(fd, "Invalid input, please enter a valid argument\n"); GetDumpHelp(fd); diff --git a/services/native/src/usb_port_manager.cpp b/services/native/src/usb_port_manager.cpp index ff07b71d..48d65b98 100644 --- a/services/native/src/usb_port_manager.cpp +++ b/services/native/src/usb_port_manager.cpp @@ -484,10 +484,10 @@ void UsbPortManager::DumpSetPortRoles(int32_t fd, const std::string &args) dprintf(fd, "port param error, please enter again\n"); GetDumpHelp(fd); } - return; -#endif // USB_MANAGER_HIDUMPER_SET +#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) -- Gitee From 53473c98e20a4ae4218e09c76c99c87a15f39275 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Fri, 15 Aug 2025 11:35:26 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhidumper=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=92=8C=E5=8A=9F=E8=83=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anduin1109 --- services/BUILD.gn | 3 --- usbmgr.gni | 1 - 2 files changed, 4 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 993cb780..f1f81cba 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -57,9 +57,6 @@ ohos_shared_library("usbservice") { public_configs = [ ":usbsrv_public_config" ] deps = [ "${usb_manager_path}/interfaces/innerkits:usb_server_stub" ] defines = [] - if (usb_manager_hidumper_set) { - defines += [ "USB_MANAGER_HIDUMPER_SET" ] - } if (usb_manager_pass_through) { defines += [ "USB_MANAGER_PASS_THROUGH", diff --git a/usbmgr.gni b/usbmgr.gni index 2e9bdc26..6dbbf41c 100644 --- a/usbmgr.gni +++ b/usbmgr.gni @@ -22,7 +22,6 @@ declare_args() { usb_manager_feature_host = true usb_manager_feature_device = true usb_manager_feature_port = true - usb_manager_hidumper_set = false usb_manager_pass_through = true usb_manager_peripheral_fault_notifier = false } -- Gitee