From d5d176befbe197b12a734def6e94d439e1d6c097 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 11 Sep 2025 20:39:31 +0800 Subject: [PATCH 1/2] set default authorize for enterprise device Signed-off-by: Anduin1109 --- usb/hdi_service/include/usb_device_impl.h | 2 + usb/hdi_service/src/usb_device_impl.cpp | 57 +++++++++++++++++-- usb/test/benchmarktest/BUILD.gn | 2 +- .../usb_benchmark_authorize_test.cpp | 6 +- .../hal/src/usbd_device_authorize_test.cpp | 11 ++-- .../hal/src/usbd_interface_authorize_test.cpp | 17 +++--- 6 files changed, 71 insertions(+), 24 deletions(-) diff --git a/usb/hdi_service/include/usb_device_impl.h b/usb/hdi_service/include/usb_device_impl.h index 34e89afa0c..a2c5e29bd7 100644 --- a/usb/hdi_service/include/usb_device_impl.h +++ b/usb/hdi_service/include/usb_device_impl.h @@ -74,6 +74,8 @@ private: std::string GetDeviceDirName(uint8_t devNum, uint8_t devAddr); std::string GetInterfaceDirName(uint8_t devNum, uint8_t devAddr, uint8_t configId, uint8_t interfaceId); int32_t SetAuthorize(const std::string &filePath, bool authorized); + int32_t SetDefaultAuthorize(bool authorized); + int32_t SetGlobalDefaultAuthorize(bool authorized); static UsbdSubscriber subscribers_[MAX_SUBSCRIBER]; static bool isGadgetConnected_; static bool isEdmExist_; diff --git a/usb/hdi_service/src/usb_device_impl.cpp b/usb/hdi_service/src/usb_device_impl.cpp index b66b9c2bef..427b92d477 100644 --- a/usb/hdi_service/src/usb_device_impl.cpp +++ b/usb/hdi_service/src/usb_device_impl.cpp @@ -359,14 +359,18 @@ int32_t UsbDeviceImpl::UsbdEventHandleRelease(void) int32_t UsbDeviceImpl::UsbDeviceAuthorize(uint8_t busNum, uint8_t devAddr, bool authorized) { HDF_LOGI("%{public}s: enter", __func__); - std::string dev_dirname = GetDeviceDirName(busNum, devAddr); - if (dev_dirname.length() == 0) { + if (busNum == 0 && devAddr == 0) { + return SetDefaultAuthorize(authorized); + } + + std::string devDirname = GetDeviceDirName(busNum, devAddr); + if (devDirname.length() == 0) { HDF_LOGE("%{public}s: failed to reach busNum: %{public}d, devAddr: %{public}d", __func__, busNum, devAddr); return HDF_ERR_INVALID_PARAM; } - auto dev_dir = std::filesystem::path(SYSFS_DEVICES_DIR) / std::filesystem::path(dev_dirname); - dev_dir /= AUTH_PATH; - return SetAuthorize(dev_dir.generic_string(), authorized); + auto devDir = std::filesystem::path(SYSFS_DEVICES_DIR) / std::filesystem::path(devDirname); + devDir /= AUTH_PATH; + return SetAuthorize(devDir.generic_string(), authorized); } static bool ConvertToInt32(const std::string& str, int32_t& value) @@ -540,6 +544,49 @@ int32_t UsbDeviceImpl::SetAuthorize(const std::string &filePath, bool authorized return HDF_SUCCESS; } +int32_t UsbDeviceImpl::SetDefaultAuthorize(bool authorized) +{ + int32_t ret = SetGlobalDefaultAuthorize(authorized); + std::string hubDir; + struct dirent *entry; + DIR *dir = opendir(SYSFS_DEVICES_DIR); + if (dir == nullptr) { + HDF_LOGE("%{public}s: dir is empty", __func__); + return HDF_FAILURE; + } + while ((entry = readdir(dir)) != nullptr) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; + } + hubDir = std::string(entry->d_name); + // set all authorized values (hubs have "authorized_default" attribute), and just ignore any failure here + (void)SetAuthorize(SYSFS_DEVICES_DIR + hubDir + "/authorized_default", authorized); + (void)SetAuthorize(SYSFS_DEVICES_DIR + hubDir + "/authorized_default", authorized); + } + closedir(dir); + return ret; +} + +int32_t UsbDeviceImpl::SetGlobalDefaultAuthorize(bool authorized) +{ + int32_t fd; + int32_t ret; + std::string content = (authorized)? "-1" : "0"; + fd = open("/sys/module/usbcore/parameters/authorized_default", O_WRONLY | O_TRUNC); + if (fd < 0) { + HDF_LOGE("%{public}s: failed to reach authorized_default, errno = %{public}d", __func__, errno); + return HDF_FAILURE; + } + ret = write(fd, content.c_str(), content.length()); + close(fd); + if (ret < 0) { + HDF_LOGE("%{public}s: failed to set usb default authorize, errno = %{public}d", __func__, errno); + return HDF_FAILURE; + } + HDF_LOGI("%{public}s: set usb global default authorize %{public}s finished", __func__, content.c_str()); + return HDF_SUCCESS; +} + } // namespace v2_0 } // namespace Usb } // namespace HDI diff --git a/usb/test/benchmarktest/BUILD.gn b/usb/test/benchmarktest/BUILD.gn index 56fcd452bf..ed39a9f3ef 100644 --- a/usb/test/benchmarktest/BUILD.gn +++ b/usb/test/benchmarktest/BUILD.gn @@ -171,7 +171,7 @@ ohos_benchmarktest("hdi_usb_benchmark_driver_test") { ohos_benchmarktest("hdf_usb_benchmark_authorize_test") { module_out_path = module_output_path - include_dirs = ["../UsbSubscriberTest"] + include_dirs = [ "../UsbSubscriberTest" ] sources = [ "../UsbSubscriberTest/UsbSubscriberV2Test.cpp", diff --git a/usb/test/benchmarktest/usb_benchmark_authorize_test.cpp b/usb/test/benchmarktest/usb_benchmark_authorize_test.cpp index 8313de4fd2..96ec73ff65 100644 --- a/usb/test/benchmarktest/usb_benchmark_authorize_test.cpp +++ b/usb/test/benchmarktest/usb_benchmark_authorize_test.cpp @@ -17,9 +17,9 @@ #include #include "hdf_log.h" #include "UsbSubscriberV2Test.h" -#include "V2_0/iusb_device_interface.h" -#include "V2_0/iusb_host_interface.h" -#include "V2_0/usb_types.h" +#include "v2_0/iusb_device_interface.h" +#include "v2_0/iusb_host_interface.h" +#include "v2_0/usb_types.h" using namespace OHOS; using namespace OHOS::HDI::Usb::V2_0; diff --git a/usb/test/unittest/hal/src/usbd_device_authorize_test.cpp b/usb/test/unittest/hal/src/usbd_device_authorize_test.cpp index f8feffba27..7693190ebe 100644 --- a/usb/test/unittest/hal/src/usbd_device_authorize_test.cpp +++ b/usb/test/unittest/hal/src/usbd_device_authorize_test.cpp @@ -34,7 +34,7 @@ namespace OHOS { namespace USB { namespace UsbDeviceAuthorize { const uint8_t BUS_NUM_INVALID = 255; -cosnt uint8_t DEV_ADDR_INVALID = 255; +const uint8_t DEV_ADDR_INVALID = 255; UsbDev dev_ = {0, 0}; sptr UsbdDeviceAuthorizeTest::subscriber_ = nullptr; sptr g_usbHostInterface = nullptr; @@ -43,7 +43,7 @@ sptr g_usbDeviceInterface = nullptr; void UsbdDeviceAuthorizeTest::SetUpTestCase(void) { g_usbHostInterface = HDI::Usb::V2_0::IUsbHostInterface::Get(); - g_usbDeviceInterface = HDI::Usb::V2_0::IUsbHostInterface::Get(); + g_usbDeviceInterface = HDI::Usb::V2_0::IUsbDeviceInterface::Get(); if (g_usbHostInterface == nullptr) { HDF_LOGE("%{public}s:IUsbHostInterface::Get() failed.", __func__); exit(0); @@ -58,7 +58,6 @@ void UsbdDeviceAuthorizeTest::SetUpTestCase(void) HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__); exit(0); } - std::cout << "please connect device, press enter to continue" << std::endl; int c; while ((c = getchar()) != '\n' && c != EOF) { @@ -103,7 +102,7 @@ HWTEST_F(UsbdDeviceAuthorizeTest, UsbdDeviceAuthorize001, TestSize.Level1) * @tc.name: UsbdDeviceAuthorize002 * @tc.desc: Test functions to UsbDeviceAuthorize * @tc.desc: int32_t UsbDeviceAuthorize(uint8_t devNum, uint8_t devAddr, bool authorized); - * @tc.desc: Positive test: parameters exception, busNum error + * @tc.desc: Negative test: parameters exception, busNum error * @tc.type: FUNC */ HWTEST_F(UsbdDeviceAuthorizeTest, UsbdDeviceAuthorize002, TestSize.Level1) @@ -120,7 +119,7 @@ HWTEST_F(UsbdDeviceAuthorizeTest, UsbdDeviceAuthorize002, TestSize.Level1) * @tc.name: UsbdDeviceAuthorize003 * @tc.desc: Test functions to UsbDeviceAuthorize * @tc.desc: int32_t UsbDeviceAuthorize(uint8_t devNum, uint8_t devAddr, bool authorized); - * @tc.desc: Positive test: parameters exception, devAddr error + * @tc.desc: Negative test: parameters exception, devAddr error * @tc.type: FUNC */ HWTEST_F(UsbdDeviceAuthorizeTest, UsbdDeviceAuthorize003, TestSize.Level1) @@ -137,7 +136,7 @@ HWTEST_F(UsbdDeviceAuthorizeTest, UsbdDeviceAuthorize003, TestSize.Level1) * @tc.name: UsbdDeviceAuthorize004 * @tc.desc: Test functions to UsbDeviceAuthorize * @tc.desc: int32_t UsbDeviceAuthorize(uint8_t devNum, uint8_t devAddr, bool authorized); - * @tc.desc: Positive test: parameters exception, busNum && devAddr error + * @tc.desc: Negative test: parameters exception, busNum && devAddr error * @tc.type: FUNC */ HWTEST_F(UsbdDeviceAuthorizeTest, UsbdDeviceAuthorize004, TestSize.Level1) diff --git a/usb/test/unittest/hal/src/usbd_interface_authorize_test.cpp b/usb/test/unittest/hal/src/usbd_interface_authorize_test.cpp index 252e6376e6..acb99798b9 100644 --- a/usb/test/unittest/hal/src/usbd_interface_authorize_test.cpp +++ b/usb/test/unittest/hal/src/usbd_interface_authorize_test.cpp @@ -33,11 +33,11 @@ namespace OHOS { namespace USB { namespace UsbInterfaceAuthorize { const uint8_t BUS_NUM_INVALID = 255; -cosnt uint8_t DEV_ADDR_INVALID = 255; -cosnt uint8_t CONFIG_ID_OK = 1; -cosnt uint8_t CONFIG_ID_INVALID = 255; -cosnt uint8_t INTERFACEID_OK_NEW = 0; -cosnt uint8_t INTERFACEID_INVALID = 255; +const uint8_t DEV_ADDR_INVALID = 255; +const uint8_t CONFIG_ID_OK = 1; +const uint8_t CONFIG_ID_INVALID = 255; +const uint8_t INTERFACEID_OK_NEW = 0; +const uint8_t INTERFACEID_INVALID = 255; uint8_t g_configId = CONFIG_ID_OK; uint8_t g_interfaceId = INTERFACEID_OK_NEW; UsbDev dev_ = {0, 0}; @@ -48,7 +48,7 @@ sptr g_usbDeviceInterface = nullptr; void UsbdInterfaceAuthorizeTest::SetUpTestCase(void) { g_usbHostInterface = IUsbHostInterface::Get(true); - g_usbDeviceInterface = IUsbHostInterface::Get(); + g_usbDeviceInterface = IUsbDeviceInterface::Get(); if (g_usbHostInterface == nullptr) { HDF_LOGE("%{public}s:IUsbHostInterface::Get() failed.", __func__); exit(0); @@ -63,7 +63,6 @@ void UsbdInterfaceAuthorizeTest::SetUpTestCase(void) HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__); exit(0); } - std::cout << "please connect device, press enter to continue" << std::endl; int c; while ((c = getchar()) != '\n' && c != EOF) { @@ -412,7 +411,7 @@ HWTEST_F(UsbdInterfaceAuthorizeTest, UsbdInterfaceAuthorize016, TestSize.Level1) * @tc.name: UsbdInterfaceAuthorize017 * @tc.desc: Test functions to UsbInterfaceAuthorize * @tc.desc: int32_t UsbInterfaceAuthorize(const UsbDev &dev, uint8_t configId, uint8_t interfaceId, bool authorized); - * @tc.desc: Negative test: parameters correctly, authorized = true + * @tc.desc: Positive test: parameters correctly, authorized = true * @tc.type: FUNC */ HWTEST_F(UsbdInterfaceAuthorizeTest, UsbdInterfaceAuthorize017, TestSize.Level1) @@ -424,7 +423,7 @@ HWTEST_F(UsbdInterfaceAuthorizeTest, UsbdInterfaceAuthorize017, TestSize.Level1) HDF_LOGI( "UsbdInterfaceAuthorizeTest::UsbdInterfaceAuthorize017 %{public}d UsbInterfaceAuthorize=%{public}d", __LINE__, ret); - EXPECT_NE(0, ret); + EXPECT_EQ(0, ret); } } // UsbInterfaceAuthorize -- Gitee From 9f2d43c98bb700ce40c221ede76250778f7f6ef4 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 11 Sep 2025 21:44:22 +0800 Subject: [PATCH 2/2] set initial authorized_default for enterprise device Signed-off-by: Anduin1109 --- usb/hdi_service/src/usb_device_impl.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/usb/hdi_service/src/usb_device_impl.cpp b/usb/hdi_service/src/usb_device_impl.cpp index 427b92d477..489767e969 100644 --- a/usb/hdi_service/src/usb_device_impl.cpp +++ b/usb/hdi_service/src/usb_device_impl.cpp @@ -518,21 +518,19 @@ std::string UsbDeviceImpl::GetInterfaceDirName( int32_t UsbDeviceImpl::SetAuthorize(const std::string &filePath, bool authorized) { - int32_t fd; - int32_t ret; std::string content = (authorized)? ENABLE_AUTH_STR : DISABLE_AUTH_STR; char realPathStr[MAX_BUFFER] = {'\0'}; if (filePath.length() >= MAX_BUFFER || realpath(filePath.c_str(), realPathStr) == nullptr) { HDF_LOGE("%{public}s: realpath failed. ret = %{public}s", __func__, strerror(errno)); return HDF_FAILURE; } - fd = open(realPathStr, O_WRONLY | O_TRUNC); + int32_t fd = open(realPathStr, O_WRONLY | O_TRUNC); if (fd < 0) { HDF_LOGE("%{public}s: failed to reach %{public}s, errno = %{public}d", __func__, filePath.c_str(), errno); return HDF_FAILURE; } - ret = write(fd, content.c_str(), content.length()); + int32_t ret = write(fd, content.c_str(), content.length()); close(fd); if (ret < 0) { HDF_LOGE("%{public}s: failed to authorize usb %{public}s, errno = %{public}d", @@ -569,15 +567,13 @@ int32_t UsbDeviceImpl::SetDefaultAuthorize(bool authorized) int32_t UsbDeviceImpl::SetGlobalDefaultAuthorize(bool authorized) { - int32_t fd; - int32_t ret; std::string content = (authorized)? "-1" : "0"; - fd = open("/sys/module/usbcore/parameters/authorized_default", O_WRONLY | O_TRUNC); + int32_t fd = open("/sys/module/usbcore/parameters/authorized_default", O_WRONLY | O_TRUNC); if (fd < 0) { HDF_LOGE("%{public}s: failed to reach authorized_default, errno = %{public}d", __func__, errno); return HDF_FAILURE; } - ret = write(fd, content.c_str(), content.length()); + int32_t ret = write(fd, content.c_str(), content.length()); close(fd); if (ret < 0) { HDF_LOGE("%{public}s: failed to set usb default authorize, errno = %{public}d", __func__, errno); -- Gitee