diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 9a0f75e10c52cec493429cdd9f34db5ad9244b74..91e52306c1e18aeb1dfb8a385bd0c6e4db67cb05 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -177,7 +177,7 @@ private: int32_t GetEdmWhiteListPolicy(sptr remote, std::vector &trustUsbDeviceId); int32_t ManageInterface(const HDI::Usb::V1_0::UsbDev &dev, uint8_t interfaceId, bool disable); void ExecuteManageDeviceType(const std::vector &disableType, bool disable, - const std::unordered_map> map, bool isDev); + const std::unordered_map> &map, bool isDev); int32_t ManageGlobalInterfaceImpl(bool disable); int32_t ManageDeviceImpl(int32_t vendorId, int32_t productId, bool disable); int32_t ManageInterfaceTypeImpl(InterfaceType interfaceType, bool disable); diff --git a/services/native/src/usb_right_manager.cpp b/services/native/src/usb_right_manager.cpp index a520bafb93b32292e5ba513702c908223bc0dc0f..3372378f63735a793684f4b17d5d3cd0eea06309 100644 --- a/services/native/src/usb_right_manager.cpp +++ b/services/native/src/usb_right_manager.cpp @@ -128,7 +128,7 @@ bool UsbRightManager::HasRight(const std::string &deviceName, const std::string // no record or expired record: expired true, has right false, add right next time // valid record: expired false, has right true, no need add right if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } return !helper->IsRecordExpired(userId, deviceName, bundleName, tokenId, nowTime); @@ -183,7 +183,7 @@ bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::s std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } ret = helper->AddOrUpdateRightRecord(uid, deviceName, hapTokenInfoRes.bundleName, tokenIdStr, info); @@ -218,7 +218,7 @@ bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::s std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } auto ret = helper->AddOrUpdateRightRecord(userId, deviceName, bundleName, tokenId, info); @@ -239,7 +239,7 @@ bool UsbRightManager::RemoveDeviceRight(const std::string &deviceName, const std } std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } int32_t ret = helper->DeleteRightRecord(userId, deviceName, bundleName, tokenId); @@ -516,7 +516,7 @@ int32_t UsbRightManager::CleanUpRightAppUninstalled(int32_t uid, int32_t &totalA std::vector apps; std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } int32_t ret = helper->QueryRightRecordApps(uid, apps); @@ -528,7 +528,7 @@ int32_t UsbRightManager::CleanUpRightAppUninstalled(int32_t uid, int32_t &totalA deleteApps = 0; for (int32_t i = 0; i < totalApps; i++) { std::string app = apps.at(i); - if (helper != nullptr && (!IsAppInstalled(uid, app))) { + if (!IsAppInstalled(uid, app)) { ret = helper->DeleteAppRightRecord(uid, app); if (ret != USB_RIGHT_OK) { USB_HILOGW(MODULE_USB_SERVICE, "clean failed: app=%{public}s, ret=%{public}d", app.c_str(), ret); @@ -547,7 +547,7 @@ int32_t UsbRightManager::CleanUpRightAppUninstalled(int32_t uid, const std::stri std::vector apps; std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } int32_t ret = helper->QueryRightRecordApps(uid, apps); @@ -591,7 +591,7 @@ int32_t UsbRightManager::CleanUpRightAppReinstalled(int32_t uid, uint32_t &total std::vector apps; std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } int32_t ret = helper->QueryRightRecordApps(uid, apps); @@ -642,7 +642,7 @@ int32_t UsbRightManager::CleanUpRightUserDeleted(int32_t &totalUsers, int32_t &d bool isAccountExists = false; std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } int32_t ret = helper->QueryRightRecordUids(rightRecordUids); @@ -678,7 +678,7 @@ int32_t UsbRightManager::CleanUpRightTemporaryExpired(const std::string &deviceN { std::shared_ptr helper = UsbRightDbHelper::GetInstance(); if (helper == nullptr) { - USB_HILOGW(MODULE_USB_SERVICE, "helper is nullptr, false"); + USB_HILOGE(MODULE_USB_SERVICE, "helper is nullptr, false"); return false; } int32_t ret = helper->DeleteValidPeriodRightRecord(USB_RIGHT_VALID_PERIOD_MIN, deviceName); diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index faff38fcc0735593b7bda190f5e3e1837a6b657e..d7293af0385bdfda33435c69d46b8110beee0727 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -1364,7 +1364,7 @@ int32_t UsbService::GetUsbPolicy(bool &IsGlobalDisabled, std::vector &disableType, bool disable) { std::vector interfaceTypes; - for (auto dev : disableType) { + for (const auto &dev : disableType) { if (!dev.isDeviceType) { ExecuteManageDeviceType(disableType, disable, g_typeMap, false); } else { @@ -1377,10 +1377,10 @@ int32_t UsbService::ExecuteManageInterfaceType(const std::vector // LCOV_EXCL_START void UsbService::ExecuteManageDeviceType(const std::vector &disableType, bool disable, - const std::unordered_map> map, bool isDev) + const std::unordered_map> &map, bool isDev) { std::vector interfaceTypes; - for (auto dev : disableType) { + for (const auto &dev : disableType) { bool isMatch = false; for (auto& [interfaceTypeValues, typeValues] : map) { if ((typeValues[0] == dev.baseClass) && @@ -2157,7 +2157,7 @@ int32_t UsbService::ManageInterfaceTypeImpl(InterfaceType interfaceType, bool di iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { ManageInterface(dev, interfaces[i].GetId(), disable); USB_HILOGI(MODULE_USB_SERVICE, "size %{public}zu, interfaceType: %{public}d, disable: %{public}d", - devices.size(), (int32_t)interfaceType, disable); + devices.size(), static_cast(interfaceType), disable); std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); } } @@ -2169,22 +2169,22 @@ int32_t UsbService::ManageInterfaceTypeImpl(InterfaceType interfaceType, bool di // LCOV_EXCL_START int32_t UsbService::ManageDeviceTypeImpl(InterfaceType interfaceType, bool disable) { - auto iterInterface = g_typeMap .find(interfaceType); - if (iterInterface == g_typeMap .end()) { + auto iterInterface = g_typeMap.find(interfaceType); + if (iterInterface == g_typeMap.end()) { USB_HILOGE(MODULE_USB_SERVICE, "UsbService::not find interface type"); return UEC_SERVICE_INVALID_VALUE; } std::map devices; usbHostManager_->GetDevices(devices); - USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", - devices.size(), (int32_t)interfaceType, disable); for (auto it = devices.begin(); it != devices.end(); ++it) { if ((it->second->GetClass() == iterInterface->second[BASECLASS_INDEX]) && (it->second->GetSubclass() == iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && (it->second->GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), disable); + USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu, interfaceType: %{public}d, disable: %{public}d", + devices.size(), static_cast(interfaceType), disable); std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); } } diff --git a/test/native/service_unittest/src/usb_device_status_test.cpp b/test/native/service_unittest/src/usb_device_status_test.cpp index 683b11ca05e16a3b315f772823ede47f21fef7ef..4a488a268ceaa17889da00af9c3912e3f66f849d 100644 --- a/test/native/service_unittest/src/usb_device_status_test.cpp +++ b/test/native/service_unittest/src/usb_device_status_test.cpp @@ -612,9 +612,12 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver001, TestSize.Level1) ret); EXPECT_TRUE(ret == 0); UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver001 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); + EXPECT_TRUE(ret == 0); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver001 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver001 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret == 0); bool close = UsbSrvClient.Close(pipe); USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver001 %{public}d close=%{public}d", __LINE__, close); @@ -648,8 +651,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver002, TestSize.Level1) UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver002 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver002 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetBusNum(device.GetBusNum()); bool close = UsbSrvClient.Close(pipe); @@ -684,8 +687,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver003, TestSize.Level1) UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0); pipe.SetDevAddr(BUFFER_SIZE); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver003 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver003 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); bool close = UsbSrvClient.Close(pipe); @@ -718,9 +721,13 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver004, TestSize.Level1) ret); EXPECT_TRUE(ret == 0); UsbInterface interface = device.GetConfigs().at(0).GetInterfaces().at(1); + ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver004 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); + EXPECT_TRUE(ret == 0); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver004 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver004 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret == 0); bool close = UsbSrvClient.Close(pipe); USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver004 %{public}d close=%{public}d", __LINE__, close); @@ -754,7 +761,7 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver005, TestSize.Level1) UsbInterface interface = device.GetConfigs().at(0).GetInterfaces().at(1); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver005 %{public}d GetInterfaceStatus=%{public}d", + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver005 %{public}d UsbDetachKernelDriver=%{public}d", __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetBusNum(device.GetBusNum()); @@ -790,8 +797,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver006, TestSize.Level1) UsbInterface interface = device.GetConfigs().at(0).GetInterfaces().at(1); pipe.SetDevAddr(BUFFER_SIZE); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver006 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver006 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); bool close = UsbSrvClient.Close(pipe); @@ -827,8 +834,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver007, TestSize.Level1) pipe.SetDevAddr(BUFFER_SIZE); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver007 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver007 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); pipe.SetBusNum(device.GetBusNum()); @@ -865,8 +872,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbDetachKernelDriver008, TestSize.Level1) pipe.SetDevAddr(BUFFER_SIZE); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbDetachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver008 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDetachKernelDriver008 %{public}d UsbDetachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); pipe.SetBusNum(device.GetBusNum()); @@ -901,8 +908,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver001, TestSize.Level1) EXPECT_TRUE(ret == 0); UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver001 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver001 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret == 0); bool close = UsbSrvClient.Close(pipe); USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver001 %{public}d close=%{public}d", __LINE__, close); @@ -936,8 +943,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver002, TestSize.Level1) UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver002 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver002 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetBusNum(device.GetBusNum()); bool close = UsbSrvClient.Close(pipe); @@ -972,8 +979,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver003, TestSize.Level1) UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0); pipe.SetDevAddr(BUFFER_SIZE); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver003 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver003 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); bool close = UsbSrvClient.Close(pipe); @@ -1007,8 +1014,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver004, TestSize.Level1) EXPECT_TRUE(ret == 0); UsbInterface interface = device.GetConfigs().at(0).GetInterfaces().at(1); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver004 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver004 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret == 0); bool close = UsbSrvClient.Close(pipe); USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver004 %{public}d close=%{public}d", __LINE__, close); @@ -1042,7 +1049,7 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver005, TestSize.Level1) UsbInterface interface = device.GetConfigs().at(0).GetInterfaces().at(1); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver005 %{public}d GetInterfaceStatus=%{public}d", + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver005 %{public}d UsbAttachKernelDriver=%{public}d", __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetBusNum(device.GetBusNum()); @@ -1078,8 +1085,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver006, TestSize.Level1) UsbInterface interface = device.GetConfigs().at(0).GetInterfaces().at(1); pipe.SetDevAddr(BUFFER_SIZE); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver006 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver006 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); bool close = UsbSrvClient.Close(pipe); @@ -1115,8 +1122,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver007, TestSize.Level1) pipe.SetDevAddr(BUFFER_SIZE); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver007 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver007 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); pipe.SetBusNum(device.GetBusNum()); @@ -1153,8 +1160,8 @@ HWTEST_F(UsbDeviceStatusTest, UsbAttachKernelDriver008, TestSize.Level1) pipe.SetDevAddr(BUFFER_SIZE); pipe.SetBusNum(BUFFER_SIZE); ret = UsbSrvClient.UsbAttachKernelDriver(pipe, interface); - USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver008 %{public}d GetInterfaceStatus=%{public}d", - __LINE__, ret); + USB_HILOGI(MODULE_USB_SERVICE, "UsbAttachKernelDriver008 %{public}d UsbAttachKernelDriver=%{public}d", + __LINE__, ret); EXPECT_TRUE(ret != 0); pipe.SetDevAddr(device.GetDevAddr()); pipe.SetBusNum(device.GetBusNum());