diff --git a/bundle.json b/bundle.json index ae32e68afb31f2ee2b672238cacfe83e498e6df7..17635bc3caf5f0874ef98815d4524bbeb34fbb08 100644 --- a/bundle.json +++ b/bundle.json @@ -76,6 +76,7 @@ "header": { "header_files": [ "usb_srv_client.h", + "usb_interface_type.h", "iusb_srv.h" ], "header_base": "//base/usb/usb_manager/interfaces/innerkits/native/include" diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index d4cb2d79d55c61c5b10a04ce0f09968eb3991b62..ec02c6283026afef3effa80e056890fcb7cf761a 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -34,6 +34,7 @@ ohos_shared_library("usbsrv_client") { sources = [ "${usb_manager_path}/services/zidl/src/usb_srv_proxy.cpp", "native/src/usb_device_pipe.cpp", + "native/src/usb_interface_type.cpp", "native/src/usb_request.cpp", "native/src/usb_srv_client.cpp", "native/src/usbd_bulk_callback.cpp", diff --git a/interfaces/innerkits/native/include/iusb_srv.h b/interfaces/innerkits/native/include/iusb_srv.h index 92ebde813191adb2b63af40a4daded5c00d520cb..7210ff789a8e54b207cbcbb21de4e0817d460f2d 100644 --- a/interfaces/innerkits/native/include/iusb_srv.h +++ b/interfaces/innerkits/native/include/iusb_srv.h @@ -73,7 +73,6 @@ public: virtual int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName) = 0; virtual int32_t ManageGlobalInterface(bool disable) = 0; virtual int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable) = 0; - virtual int32_t ManageInterfaceStorage(InterfaceType interfaceType, bool disable) = 0; virtual int32_t ManageInterfaceType(const std::vector &disableType, bool disable) = 0; virtual int32_t GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed) = 0; virtual int32_t GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, diff --git a/interfaces/innerkits/native/include/usb_interface_type.h b/interfaces/innerkits/native/include/usb_interface_type.h index e0517b7c376f5612262d05853b5f017388de40f5..bea3b40f86fb52eeac1f693b3abdb5259b4d8b0e 100644 --- a/interfaces/innerkits/native/include/usb_interface_type.h +++ b/interfaces/innerkits/native/include/usb_interface_type.h @@ -15,10 +15,13 @@ #ifndef USB_INTERFACE_TYPE_H #define USB_INTERFACE_TYPE_H +#include "usb_common.h" +#include "message_parcel.h" namespace OHOS { namespace USB { enum InterfaceType { + TYPE_INTERFACE, TYPE_STORAGE, TYPE_AUDIO, TYPE_HID, @@ -95,8 +98,26 @@ enum InterfaceType { struct UsbDeviceType { int32_t baseClass; int32_t subClass; - int32_t protocal; + int32_t protocol; bool isDeviceType; + bool operator<(const UsbDeviceType &other) const + { + if (baseClass != other.baseClass) { + return baseClass < other.baseClass; + } else if (subClass != other.subClass) { + return subClass < other.subClass; + } else { + return protocol < other.protocol; + } + } + bool operator == (const UsbDeviceType &other) const + { + return (baseClass == other.baseClass) && (subClass == other.subClass) && (protocol == other.protocol) && + (isDeviceType == other.isDeviceType); + } + bool Marshalling(MessageParcel &parcel) const; + static bool Unmarshalling(MessageParcel &parcel, UsbDeviceType &usbDeviceType); + bool ReadFromParcel(MessageParcel &parcel); }; struct UsbDeviceId { @@ -104,7 +125,35 @@ struct UsbDeviceId { int32_t vendorId; }; -const std::unordered_map> g_typeMap = { +const std::unordered_map> d_typeMap = { + {InterfaceType::TYPE_INTERFACE, {0, 0, 0}}, + {InterfaceType::TYPE_COMMUNICATION, {2, -1, -1}}, + {InterfaceType::TYPE_FULL_SPEED_HUB, {9, 0, 0}}, + {InterfaceType::TYPE_FULL_SPEED_HUB_S, {9, 0, 1}}, + {InterfaceType::TYPE_FULL_SPEED_HUB_M, {9, 0, 2}}, + {InterfaceType::TYPE_BILLBOARD, {17, 0, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_1, {220, 1, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_2, {220, 2, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_3, {220, 2, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_4, {220, 3, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_5, {220, 3, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_6, {220, 4, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_7, {220, 4, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_8, {220, 5, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_9, {220, 5, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_10, {220, 6, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_11, {220, 6, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_12, {220, 7, 0}}, + {InterfaceType::TYPE_DIAGNOSTIC_13, {220, 7, 1}}, + {InterfaceType::TYPE_DIAGNOSTIC_14, {220, 8, 0}}, + {InterfaceType::TYPE_MISCELLANEOUS_1, {239, 1, 1}}, + {InterfaceType::TYPE_MISCELLANEOUS_2, {239, 1, 2}}, + {InterfaceType::TYPE_MISCELLANEOUS_3, {239, 2, 1}}, + {InterfaceType::TYPE_MISCELLANEOUS_4, {239, 2, 2}}, + {InterfaceType::TYPE_VENDOR_SPECIFIC, {255, -1, -1}} +}; + +const std::unordered_map> g_typeMap = { {InterfaceType::TYPE_AUDIO, {1, -1, -1}}, {InterfaceType::TYPE_COMMUNICATION, {2, -1, -1}}, {InterfaceType::TYPE_HID, {3, -1, -1}}, @@ -112,9 +161,6 @@ const std::unordered_map> g_typeMap = { {InterfaceType::TYPE_IMAGE, {6, 1, 1}}, {InterfaceType::TYPE_PRINTER, {7, -1, -1}}, {InterfaceType::TYPE_STORAGE, {8, -1, -1}}, - {InterfaceType::TYPE_FULL_SPEED_HUB, {9, 0, 0}}, - {InterfaceType::TYPE_FULL_SPEED_HUB_S, {9, 0, 1}}, - {InterfaceType::TYPE_FULL_SPEED_HUB_M, {9, 0, 2}}, {InterfaceType::TYPE_CDC_DATA, {10, -1, -1}}, {InterfaceType::TYPE_SMART_CARD, {11, -1, -1}}, {InterfaceType::TYPE_CONTENT_SECURTIY, {13, 0, 0}}, @@ -123,7 +169,6 @@ const std::unordered_map> g_typeMap = { {InterfaceType::TYPE_AVCONTROL, {16, 1, 0}}, {InterfaceType::TYPE_AVV_STREAMING, {16, 2, 0}}, {InterfaceType::TYPE_AVA_STREAMING, {16, 3, 0}}, - {InterfaceType::TYPE_BILLBOARD, {17, 0, 0}}, {InterfaceType::TYPE_TYPEC_BRIDGE, {18, 0, 0}}, {InterfaceType::TYPE_BDP, {19, 0, 0}}, {InterfaceType::TYPE_MCTP_MANA_1, {20, 0, 1}}, @@ -154,8 +199,6 @@ const std::unordered_map> g_typeMap = { {InterfaceType::TYPE_WIRLESS_CONTROLLER_7, {224, 2, 3}}, {InterfaceType::TYPE_MISCELLANEOUS_1, {239, 1, 1}}, {InterfaceType::TYPE_MISCELLANEOUS_2, {239, 1, 2}}, - {InterfaceType::TYPE_MISCELLANEOUS_3, {239, 2, 1}}, - {InterfaceType::TYPE_MISCELLANEOUS_4, {239, 2, 2}}, {InterfaceType::TYPE_MISCELLANEOUS_5, {239, 3, 1}}, {InterfaceType::TYPE_MISCELLANEOUS_6, {239, 4, 1}}, {InterfaceType::TYPE_MISCELLANEOUS_7, {239, 4, 2}}, @@ -175,7 +218,6 @@ const std::unordered_map> g_typeMap = { {InterfaceType::TYPE_APPLICATION_SPE_2, {254, 2, 0}}, {InterfaceType::TYPE_APPLICATION_SPE_3, {254, 3, 0}}, {InterfaceType::TYPE_APPLICATION_SPE_4, {254, 3, 1}}, - {InterfaceType::TYPE_VENDOR_SPECIFIC, {255, -1, -1}} }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index 190fe40bf45a27c7b84081b0ded1fd1858501064..ec59ff75e5d12c20152f97cbe26d66e3385cb44f 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -89,7 +89,6 @@ public: int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName); int32_t ManageGlobalInterface(bool disable); int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable); - int32_t ManageInterfaceStorage(InterfaceType interfaceType, bool disable); int32_t ManageInterfaceType(const std::vector &disableType, bool disable); private: UsbSrvClient(); diff --git a/interfaces/innerkits/native/src/usb_interface_type.cpp b/interfaces/innerkits/native/src/usb_interface_type.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84142d4e1c55d2e37a44f03f45168977f8af7775 --- /dev/null +++ b/interfaces/innerkits/native/src/usb_interface_type.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "usb_interface_type.h" + +namespace OHOS { +namespace USB { +bool UsbDeviceType::Marshalling(MessageParcel &parcel) const +{ + WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, baseClass); + WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, subClass); + WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Int32, parcel, protocol); + WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(Bool, parcel, isDeviceType); + return true; +} + +bool UsbDeviceType::Unmarshalling(MessageParcel &parcel, UsbDeviceType &usbDeviceType) +{ + return usbDeviceType.ReadFromParcel(parcel); +} + +bool UsbDeviceType::ReadFromParcel(MessageParcel &parcel) +{ + baseClass = parcel.ReadInt32(); + subClass = parcel.ReadInt32(); + protocol = parcel.ReadInt32(); + isDeviceType = parcel.ReadBool(); + return true; +} +} // namespace USB +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp index 81e7f874d71584e71ee1f0594be0aa55220cd48c..b0a8f1961978105b6883e6acbe9f0b4ea05c82bb 100644 --- a/interfaces/innerkits/native/src/usb_srv_client.cpp +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -483,16 +483,6 @@ int32_t UsbSrvClient::ManageDevice(int32_t vendorId, int32_t productId, bool dis return ret; } -int32_t UsbSrvClient::ManageInterfaceStorage(InterfaceType interfaceType, bool disable) -{ - RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT); - int32_t ret = proxy_->ManageInterfaceStorage(interfaceType, disable); - if (ret != UEC_OK) { - USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret); - } - return ret; -} - int32_t UsbSrvClient::ManageInterfaceType(const std::vector &disableType, bool disable) { RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT); diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 76118a30f3d27ac46efff3f3d38e30b70ebf1135..e8e92497925656cb9a0b11257098793efd58683c 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -122,7 +122,6 @@ public: void UnLoadSelf(UnLoadSaType type); int32_t ManageGlobalInterface(bool disable) override; int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable) override; - int32_t ManageInterfaceStorage(InterfaceType interfaceType, bool disable) override; int32_t ManageInterfaceType(const std::vector &disableType, bool disable) override; int32_t GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, bool &unactivated) override; int32_t GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed) override; @@ -162,18 +161,21 @@ private: bool IsEdmEnabled(); int32_t ExecuteManageDevicePolicy(std::vector &whiteList); int32_t ExecuteManageInterfaceType(const std::vector &disableType, bool disable); - int32_t GetEdmPolicy(bool &IsGlobalDisabled, std::unordered_map &typeDisableMap, + int32_t GetEdmPolicy(bool &IsGlobalDisabled, std::vector &disableType, std::vector &trustUsbDeviceId); - int32_t GetUsbPolicy(bool &IsGlobalDisabled, std::unordered_map &typeDisableMap, + int32_t GetUsbPolicy(bool &IsGlobalDisabled, std::vector &disableType, std::vector &trustUsbDeviceId); void ExecuteStrategy(UsbDevice *devInfo); - int32_t GetEdmTypePolicy(sptr remote, std::unordered_map &typeDisableMap); + int32_t GetEdmTypePolicy(sptr remote, std::vector &disableType); int32_t GetEdmGlobalPolicy(sptr remote, bool &IsGlobalDisabled); 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); int32_t ManageGlobalInterfaceImpl(bool disable); int32_t ManageDeviceImpl(int32_t vendorId, int32_t productId, bool disable); int32_t ManageInterfaceTypeImpl(InterfaceType interfaceType, bool disable); + int32_t ManageDeviceTypeImpl(InterfaceType interfaceType, bool disable); void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; bool ready_ = false; int32_t commEventRetryTimes_ = 0; diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index e142286ae9b23d51ddbb85647543c4bcc5d97481..16b4ab8b9e2a53d3dbd17c40be21820d6c363d56 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -71,13 +71,12 @@ constexpr uint32_t WITHOUT_ADMIN = 1; constexpr uint32_t EMD_MASK_CODE = 20; constexpr uint32_t DISABLE_USB = 1043; constexpr uint32_t ALLOWED_USB_DEVICES = 1044; -constexpr uint32_t USB_STORAGE_DEVICE_ACCESS_POLICY = 1026; +constexpr uint32_t USB_STORAGE_DEVICE_ACCESS_POLICY = 1059; constexpr int32_t WHITELIST_POLICY_MAX_DEVICES = 1000; constexpr uint32_t EDM_SA_TIME_OUT_CODE = 9200007; constexpr int32_t BASECLASS_INDEX = 0; constexpr int32_t SUBCLASS_INDEX = 1; constexpr int32_t PROTOCAL_INDEX = 2; -constexpr int32_t GET_EDM_STORAGE_DISABLE_TYPE = 2; constexpr int32_t RANDOM_VALUE_INDICATE = -1; constexpr int32_t USB_RIGHT_USERID_INVALID = -1; constexpr const char *USB_DEFAULT_TOKEN = "UsbServiceTokenId"; @@ -130,13 +129,17 @@ UsbService::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener : usbdSubscriber_(usbdSubscriber) { } +// LCOV_EXCL_STOP +// LCOV_EXCL_START void UsbService::SystemAbilityStatusChangeListener::OnAddSystemAbility( int32_t systemAbilityId, const std::string &deviceId) { USB_HILOGI(MODULE_USB_SERVICE, "OnAddSystemAbility ID = %{public}d", systemAbilityId); } +// LCOV_EXCL_STOP +// LCOV_EXCL_START void UsbService::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( int32_t systemAbilityId, const std::string &deviceId) { @@ -148,7 +151,9 @@ void UsbService::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( } } } +// LCOV_EXCL_STOP +// LCOV_EXCL_START void UsbService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) { USB_HILOGI(MODULE_USB_SERVICE, "OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId); @@ -338,7 +343,9 @@ std::string UsbService::GetDeviceVidPidSerialNumber(std::string deviceName) } return strDesc; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::GetDeviceVidPidSerialNumber(std::string deviceName, std::string& strDesc) { int32_t isMatched = UEC_INTERFACE_INVALID_VALUE; @@ -957,7 +964,9 @@ std::string UsbService::GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, delete[] tbuf; return strDesc; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START static std::string BcdToString(const std::vector &bcd) { std::string tstr; @@ -967,7 +976,9 @@ static std::string BcdToString(const std::vector &bcd) } return tstr; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::FillDevStrings(UsbDevice &dev) { uint8_t busNum; @@ -1005,7 +1016,9 @@ int32_t UsbService::FillDevStrings(UsbDevice &dev) return UEC_OK; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &descriptor, UsbDevice &dev) { if (usbd_ == nullptr) { @@ -1036,7 +1049,9 @@ int32_t UsbService::GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &descriptor) { std::vector configs; @@ -1109,12 +1124,11 @@ int32_t UsbService::GetEdmGlobalPolicy(sptr remote, bool &IsGloba reply.ReadBool(IsGlobalDisabled); return UEC_OK; } +// LCOV_EXCL_STOP -int32_t UsbService::GetEdmTypePolicy(sptr remote, - std::unordered_map &typeDisableMap) +// LCOV_EXCL_START +int32_t UsbService::GetEdmTypePolicy(sptr remote, std::vector &disableType) { - int32_t StorageDisableType = 0; - bool IsStorageDisabled = false; MessageParcel data; MessageParcel reply; MessageOption option; @@ -1136,14 +1150,25 @@ int32_t UsbService::GetEdmTypePolicy(sptr remote, return UEC_SERVICE_EDM_SEND_REQUEST_FAILED; } - reply.ReadInt32(StorageDisableType); - if (StorageDisableType == GET_EDM_STORAGE_DISABLE_TYPE) { - IsStorageDisabled = true; + int32_t size = reply.ReadInt32(); + if (size > WHITELIST_POLICY_MAX_DEVICES) { + USB_HILOGE(MODULE_USB_SERVICE, "EdmTypeList size=[%{public}d] is too large", size); + return UEC_SERVICE_EDM_DEVICE_SIZE_EXCEED; + } + USB_HILOGI(MODULE_USB_SERVICE, "GetEdmTypePolicy return size:%{public}d", size); + for (int32_t i = 0; i < size; i++) { + UsbDeviceType usbDeviceType; + usbDeviceType.baseClass = reply.ReadInt32(); + usbDeviceType.subClass = reply.ReadInt32(); + usbDeviceType.protocol = reply.ReadInt32(); + usbDeviceType.isDeviceType = reply.ReadBool(); + disableType.emplace_back(usbDeviceType); } - typeDisableMap[InterfaceType::TYPE_STORAGE] = IsStorageDisabled; return UEC_OK; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::GetEdmWhiteListPolicy(sptr remote, std::vector &trustUsbDeviceIds) { MessageParcel data; @@ -1181,8 +1206,10 @@ int32_t UsbService::GetEdmWhiteListPolicy(sptr remote, std::vecto } return UEC_OK; } +// LCOV_EXCL_STOP -int32_t UsbService::GetEdmPolicy(bool &IsGlobalDisabled, std::unordered_map &typeDisableMap, +// LCOV_EXCL_START +int32_t UsbService::GetEdmPolicy(bool &IsGlobalDisabled, std::vector &disableType, std::vector &trustUsbDeviceIds) { sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -1201,7 +1228,7 @@ int32_t UsbService::GetEdmPolicy(bool &IsGlobalDisabled, std::unordered_map &typeDisableMap, +// LCOV_EXCL_START +int32_t UsbService::GetUsbPolicy(bool &IsGlobalDisabled, std::vector &disableType, std::vector &trustUsbDeviceIds) { auto startTime = std::chrono::steady_clock::now(); while (true) { - int32_t ret = GetEdmPolicy(IsGlobalDisabled, typeDisableMap, trustUsbDeviceIds); + int32_t ret = GetEdmPolicy(IsGlobalDisabled, disableType, trustUsbDeviceIds); if (ret == UEC_OK) { USB_HILOGI(MODULE_USB_SERVICE, "GetUsbPolicy succeed"); break; @@ -1240,38 +1269,68 @@ int32_t UsbService::GetUsbPolicy(bool &IsGlobalDisabled, std::unordered_map &disableType, bool disable) { - if (usbHostManager_ == nullptr) { - USB_HILOGE(MODULE_USB_SERVICE, "usbHostManager_ is nullptr"); - return UEC_SERVICE_INVALID_VALUE; + std::vector interfaceTypes; + for (auto dev : disableType) { + if (!dev.isDeviceType) { + ExecuteManageDeviceType(disableType, disable, g_typeMap, false); + } else { + ExecuteManageDeviceType(disableType, disable, d_typeMap, true); + } } - int32_t ret = UEC_INTERFACE_NO_MEMORY; - std::mapdevices; - usbHostManager_->GetDevices(devices); - USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu", devices.size()); + return UEC_OK; +} +// LCOV_EXCL_STOP + +// LCOV_EXCL_START +void UsbService::ExecuteManageDeviceType(const std::vector &disableType, bool disable, + const std::unordered_map> map, bool isDev) +{ + std::vector interfaceTypes; for (auto dev : disableType) { - for (auto& [interfaceTypeValues, typeValues] : g_typeMap) { - if ((!dev.isDeviceType) && - (typeValues[0] == dev.baseClass) && + bool isMatch = false; + for (auto& [interfaceTypeValues, typeValues] : map) { + if ((typeValues[0] == dev.baseClass) && (typeValues[1] == -1 || typeValues[1] == dev.subClass)&& - (typeValues[HALF] == -1 || typeValues[HALF] == dev.protocal)) { - ret = ManageInterfaceTypeImpl(interfaceTypeValues, disable); + (typeValues[HALF] == -1 || typeValues[HALF] == dev.protocol)) { + isMatch = true; + interfaceTypes.emplace_back(interfaceTypeValues); + break; } } - if (dev.isDeviceType) { - for (auto it = devices.begin(); it != devices.end(); ++it) { - ret = ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), disable); - } + if (!isMatch) { + USB_HILOGE(MODULE_USB_SERVICE, "is not in the type list, %{public}d, %{public}d, %{public}d", + dev.baseClass, dev.subClass, dev.protocol); } } - if (ret < UEC_OK) { - USB_HILOGI(MODULE_USB_SERVICE, "ExecuteManageInterfaceType failed"); - return UEC_SERVICE_EXECUTE_POLICY_FAILED; + + for (auto& [interfaceTypeValues, typeValues] : map) { + bool canFind = false; + for (auto disallowedValues : interfaceTypes) { + if (interfaceTypeValues == disallowedValues) { + canFind = true; + break; + } + } + if ((!isDev) && canFind) { + ManageInterfaceTypeImpl(interfaceTypeValues, disable); + } + if ((!isDev) && (!canFind)) { + ManageInterfaceTypeImpl(interfaceTypeValues, !disable); + } + if (isDev && canFind) { + ManageDeviceTypeImpl(interfaceTypeValues, disable); + } + if (isDev && !canFind) { + ManageDeviceTypeImpl(interfaceTypeValues, !disable); + } } - return UEC_OK; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::ExecuteManageDevicePolicy(std::vector &whiteList) { std::map devices; @@ -1299,14 +1358,18 @@ int32_t UsbService::ExecuteManageDevicePolicy(std::vector &whiteLis } return UEC_OK; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START bool UsbService::IsEdmEnabled() { std::string edmParaValue = OHOS::system::GetParameter("persist.edm.edm_enable", "false"); USB_HILOGI(MODULE_USB_SERVICE, "edmParaValue is %{public}s", edmParaValue.c_str()); return edmParaValue == "true"; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START void UsbService::ExecuteStrategy(UsbDevice *devInfo) { USB_HILOGI(MODULE_USB_SERVICE, "start"); @@ -1315,10 +1378,10 @@ void UsbService::ExecuteStrategy(UsbDevice *devInfo) return; } bool isGlobalDisabled = false; - std::unordered_map typeDisableMap{}; + std::vector disableType{}; std::vector trustUsbDeviceIds{}; - int32_t ret = GetUsbPolicy(isGlobalDisabled, typeDisableMap, trustUsbDeviceIds); + int32_t ret = GetUsbPolicy(isGlobalDisabled, disableType, trustUsbDeviceIds); if (ret == UEC_SERVICE_EDM_SA_TIME_OUT_FAILED || ret == UEC_SERVICE_PREPARE_EDM_SA_FAILED) { USB_HILOGE(MODULE_USB_SERVICE, "EDM sa time out or prepare failed, ret = %{public}d", ret); return; @@ -1331,18 +1394,12 @@ void UsbService::ExecuteStrategy(UsbDevice *devInfo) } return; } - bool flag = false; - for (auto result : typeDisableMap) { - flag = flag || result.second; - if (result.second) { - ret = ManageInterfaceTypeImpl(result.first, true); - } + + if (!disableType.empty()) { + ret = ExecuteManageInterfaceType(disableType, true); if (ret != UEC_OK) { - USB_HILOGE(MODULE_USB_SERVICE, "ManageInterfaceType failed, type is %{public}d", (int32_t)result.first); + USB_HILOGE(MODULE_USB_SERVICE, "ExecuteManageInterfaceType failed"); } - } - if (flag) { - USB_HILOGI(MODULE_USB_SERVICE, "Execute ManageInterfaceType finish"); return; } @@ -1356,6 +1413,7 @@ void UsbService::ExecuteStrategy(UsbDevice *devInfo) } return; } +// LCOV_EXCL_STOP // LCOV_EXCL_START bool UsbService::AddDevice(uint8_t busNum, uint8_t devAddr) @@ -1426,6 +1484,7 @@ bool UsbService::DelDevice(uint8_t busNum, uint8_t devAddr) } // LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::InitUsbRight() { if (usbRightManager_ == nullptr) { @@ -1448,6 +1507,7 @@ int32_t UsbService::InitUsbRight() } return ret; } +// LCOV_EXCL_STOP // LCOV_EXCL_START void UsbService::UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode) @@ -1473,6 +1533,7 @@ void UsbService::UpdateDeviceState(int32_t status) } // LCOV_EXCL_STOP +// LCOV_EXCL_START bool UsbService::GetBundleName(std::string &bundleName) { #ifdef USB_RIGHT_TEST @@ -1502,7 +1563,9 @@ bool UsbService::GetBundleName(std::string &bundleName) } return true; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START bool UsbService::GetCallingInfo(std::string &bundleName, std::string &tokenId, int32_t &userId) { OHOS::Security::AccessToken::AccessTokenID token = IPCSkeleton::GetCallingTokenID(); @@ -1520,7 +1583,9 @@ bool UsbService::GetCallingInfo(std::string &bundleName, std::string &tokenId, i ret, bundleName.c_str(), hapTokenInfoRes.userID); return true; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START bool UsbService::GetBundleInfo(std::string &tokenId, int32_t &userId) { OHOS::Security::AccessToken::AccessTokenID token = IPCSkeleton::GetCallingTokenID(); @@ -1534,7 +1599,9 @@ bool UsbService::GetBundleInfo(std::string &tokenId, int32_t &userId) userId = hapTokenInfoRes.userID; return true; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::RegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe, const sptr &cb) { if (cb == nullptr) { @@ -1556,7 +1623,9 @@ int32_t UsbService::RegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe, } return ret; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::UnRegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe) { if (usbd_ == nullptr) { @@ -1571,7 +1640,9 @@ int32_t UsbService::UnRegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe } return ret; } +// LCOV_EXCL_STOP +// LCOV_EXCL_START int32_t UsbService::BulkRead(const UsbDev &devInfo, const UsbPipe &pipe, sptr &ashmem) { if (ashmem == nullptr) { @@ -1589,7 +1660,9 @@ int32_t UsbService::BulkRead(const UsbDev &devInfo, const UsbPipe &pipe, sptr &ashmem) { if (ashmem == nullptr) { @@ -1607,6 +1680,7 @@ int32_t UsbService::BulkWrite(const UsbDev &devInfo, const UsbPipe &pipe, sptr &args) } // LCOV_EXCL_STOP +// LCOV_EXCL_START void UsbService::DumpHelp(int32_t fd) { dprintf(fd, "Refer to the following usage:\n"); @@ -1737,6 +1812,7 @@ void UsbService::DumpHelp(int32_t fd) usbDeviceManager_->GetDumpHelp(fd); usbPortManager_->GetDumpHelp(fd); } +// LCOV_EXCL_STOP // LCOV_EXCL_START void UsbService::UnLoadSelf(UnLoadSaType type) @@ -1863,15 +1939,6 @@ int32_t UsbService::ManageDevice(int32_t vendorId, int32_t productId, bool disab // LCOV_EXCL_STOP // LCOV_EXCL_START -int32_t UsbService::ManageInterfaceStorage(InterfaceType interfaceType, bool disable) -{ - if (PreCallFunction() != UEC_OK) { - USB_HILOGE(MODULE_USB_SERVICE, "PreCallFunction failed"); - return UEC_SERVICE_PRE_MANAGE_INTERFACE_FAILED; - } - return ManageInterfaceTypeImpl(interfaceType, disable); -} - int32_t UsbService::ManageInterfaceType(const std::vector &disableType, bool disable) { if (PreCallFunction() != UEC_OK) { @@ -1993,7 +2060,7 @@ int32_t UsbService::ManageInterfaceTypeImpl(InterfaceType interfaceType, bool di for (uint32_t i = 0; i < interfaces.size(); i++) { // 0 indicate base class, 1 indicate subclass, 2 indicate protocal. -1 indicate any value. - if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && (interfaces[i].GetClass() == + if ((interfaces[i].GetClass() == iterInterface->second[BASECLASS_INDEX]) && (interfaces[i].GetSubClass() == iterInterface->second[SUBCLASS_INDEX] || iterInterface->second[SUBCLASS_INDEX] == RANDOM_VALUE_INDICATE) && (interfaces[i].GetProtocol() == iterInterface->second[PROTOCAL_INDEX] || iterInterface->second[PROTOCAL_INDEX] == RANDOM_VALUE_INDICATE)) { @@ -2008,6 +2075,32 @@ int32_t UsbService::ManageInterfaceTypeImpl(InterfaceType interfaceType, bool di } // LCOV_EXCL_STOP +// LCOV_EXCL_START +int32_t UsbService::ManageDeviceTypeImpl(InterfaceType interfaceType, bool disable) +{ + 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); + std::this_thread::sleep_for(std::chrono::milliseconds(MANAGE_INTERFACE_INTERVAL)); + } + } + return UEC_OK; +} +// LCOV_EXCL_STOP + // LCOV_EXCL_START int32_t UsbService::ManageInterface(const HDI::Usb::V1_0::UsbDev &dev, uint8_t interfaceId, bool disable) { diff --git a/services/zidl/include/usb_server_proxy.h b/services/zidl/include/usb_server_proxy.h index 246230fd35212809f76027b107a0d33d7d8cf66c..258069caf174eed780c728033d0e9e839f0fd694 100644 --- a/services/zidl/include/usb_server_proxy.h +++ b/services/zidl/include/usb_server_proxy.h @@ -81,7 +81,6 @@ public: int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName) override; int32_t ManageGlobalInterface(bool disable) override; int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable) override; - int32_t ManageInterfaceStorage(InterfaceType interfaceType, bool disable) override; int32_t ManageInterfaceType(const std::vector &disableType, bool disable) override; int32_t GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed) override; int32_t GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, bool &unactivated) override; diff --git a/services/zidl/include/usb_server_stub.h b/services/zidl/include/usb_server_stub.h index 1071c27b3e6e795b16db0aef4917428c6ed1464e..860532c254641dbfa36592c75ec1ae1873122373 100644 --- a/services/zidl/include/usb_server_stub.h +++ b/services/zidl/include/usb_server_stub.h @@ -91,7 +91,6 @@ private: int32_t DoManageGlobalInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option); int32_t DoManageDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option); - int32_t DoManageInterfaceStorage(MessageParcel &data, MessageParcel &reply, MessageOption &option); int32_t DoManageInterfaceType(MessageParcel &data, MessageParcel &reply, MessageOption &option); int32_t DoGetDeviceSpeed(MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/zidl/include/usb_service_ipc_interface_code.h b/services/zidl/include/usb_service_ipc_interface_code.h index 937b664d75e8b10a5e7d7112bf583b2635d1e456..8bf0ab341562bc3eef425bb528aa547b85e048de 100644 --- a/services/zidl/include/usb_service_ipc_interface_code.h +++ b/services/zidl/include/usb_service_ipc_interface_code.h @@ -55,7 +55,6 @@ namespace USB { USB_FUN_ADD_RIGHT, USB_FUN_DISABLE_GLOBAL_INTERFACE, USB_FUN_DISABLE_DEVICE, - USB_FUN_DISABLE_INTERFACE_STORAGE, USB_FUN_DISABLE_INTERFACE_TYPE, USB_FUN_GET_DEVICE_SPEED, USB_FUN_GET_DRIVER_ACTIVE_STATUS, diff --git a/services/zidl/src/usb_srv_proxy.cpp b/services/zidl/src/usb_srv_proxy.cpp index e5234018f9f29c09d92d17e61cc6511ed3ec3beb..6c61c6003a9890a8d3578e5e926ff8833087bcbe 100644 --- a/services/zidl/src/usb_srv_proxy.cpp +++ b/services/zidl/src/usb_srv_proxy.cpp @@ -1207,30 +1207,6 @@ int32_t UsbServerProxy::ManageDevice(int32_t vendorId, int32_t productId, bool d return ret; } -int32_t UsbServerProxy::ManageInterfaceStorage(InterfaceType interfaceType, bool disable) -{ - sptr remote = Remote(); - RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE); - - MessageParcel data; - if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { - USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!"); - return ERR_ENOUGH_DATA; - } - int32_t ifaceType = (int32_t)interfaceType; - WRITE_PARCEL_WITH_RET(data, Int32, ifaceType, UEC_SERVICE_WRITE_PARCEL_ERROR); - WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR); - - MessageOption option; - MessageParcel reply; - int32_t ret = remote->SendRequest(static_cast(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_STORAGE), - data, reply, option); - if (ret != UEC_OK) { - USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret); - } - return ret; -} - int32_t UsbServerProxy::ManageInterfaceType(const std::vector &disableType, bool disable) { sptr remote = Remote(); @@ -1247,7 +1223,7 @@ int32_t UsbServerProxy::ManageInterfaceType(const std::vector &di for (const auto &type : disableType) { WRITE_PARCEL_WITH_RET(data, Int32, type.baseClass, UEC_SERVICE_WRITE_PARCEL_ERROR); WRITE_PARCEL_WITH_RET(data, Int32, type.subClass, UEC_SERVICE_WRITE_PARCEL_ERROR); - WRITE_PARCEL_WITH_RET(data, Int32, type.protocal, UEC_SERVICE_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, type.protocol, UEC_SERVICE_WRITE_PARCEL_ERROR); WRITE_PARCEL_WITH_RET(data, Bool, type.isDeviceType, UEC_SERVICE_WRITE_PARCEL_ERROR); } WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR); diff --git a/services/zidl/src/usb_srv_stub.cpp b/services/zidl/src/usb_srv_stub.cpp index f1b6753e1ca411727f3e31320af8805d0ba7322f..106ca8e0f0e2b35e773e93c766c1a0df915ba91f 100644 --- a/services/zidl/src/usb_srv_stub.cpp +++ b/services/zidl/src/usb_srv_stub.cpp @@ -202,9 +202,6 @@ bool UsbServerStub::StubHost( case static_cast(UsbInterfaceCode::USB_FUN_DISABLE_DEVICE): result = DoManageDevice(data, reply, option); return true; - case static_cast(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_STORAGE): - result = DoManageInterfaceStorage(data, reply, option); - return true; case static_cast(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_TYPE): result = DoManageInterfaceType(data, reply, option); return true; @@ -1025,19 +1022,6 @@ int32_t UsbServerStub::DoManageDevice(MessageParcel &data, MessageParcel &reply, return ret; } -int32_t UsbServerStub::DoManageInterfaceStorage(MessageParcel &data, MessageParcel &reply, MessageOption &option) -{ - int32_t interfaceType = 0; - bool disable = false; - READ_PARCEL_WITH_RET(data, Int32, interfaceType, UEC_SERVICE_READ_PARCEL_ERROR); - READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR); - int32_t ret = ManageInterfaceStorage((InterfaceType)interfaceType, disable); - if (ret != UEC_OK) { - USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret); - } - return ret; -} - int32_t UsbServerStub::DoManageInterfaceType(MessageParcel &data, MessageParcel &reply, MessageOption &option) { int32_t count; @@ -1052,7 +1036,7 @@ int32_t UsbServerStub::DoManageInterfaceType(MessageParcel &data, MessageParcel UsbDeviceType usbDeviceType; READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.baseClass, UEC_SERVICE_READ_PARCEL_ERROR); READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.subClass, UEC_SERVICE_READ_PARCEL_ERROR); - READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.protocal, UEC_SERVICE_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.protocol, UEC_SERVICE_READ_PARCEL_ERROR); READ_PARCEL_WITH_RET(data, Bool, usbDeviceType.isDeviceType, UEC_SERVICE_READ_PARCEL_ERROR); disableType.emplace_back(usbDeviceType); } diff --git a/test/native/service_unittest/src/usb_manage_interface_test.cpp b/test/native/service_unittest/src/usb_manage_interface_test.cpp index 0f203c96eca78ee9bf55a7d251211a5779cae488..1bdfa7818f7f9b4e7cf39c8efd65e34113b8053d 100644 --- a/test/native/service_unittest/src/usb_manage_interface_test.cpp +++ b/test/native/service_unittest/src/usb_manage_interface_test.cpp @@ -184,90 +184,47 @@ HWTEST_F(UsbManageInterfaceTest, ManageDevice003, TestSize.Level1) ASSERT_EQ(ret, 0); } -/** - * @tc.name: ManageInterfaceStorage001 - * @tc.desc: Test functions toManageInterfaceStorage(InterfaceType interfaceType, bool disable); - * @tc.type: FUNC - */ -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceStorage001, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType001, TestSize.Level1) { - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceStorage001 : ManageInterfaceStorage"); + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType001 : ManageInterfaceType"); auto &client = UsbSrvClient::GetInstance(); vector devi; auto ret = client.GetDevices(devi); EXPECT_TRUE(ret == 0); - InterfaceType interfaceType = InterfaceType::TYPE_HID; - if (devi.size() > 0) { - for (auto iter = g_typeMap.begin(); iter != g_typeMap.end(); iter ++) { - if (devi.at(0).GetClass() == iter->second[0] && - (devi.at(0).GetSubclass() == iter->second[1] || iter->second[1] == -1) && - (devi.at(0).GetProtocol() == iter->second[2] || iter->second[2] == -1)) { - interfaceType = iter->first; - } - } - } - ret = client.ManageInterfaceStorage(interfaceType, true); + vector disableType; + UsbDeviceType usbDeviceType; + usbDeviceType.baseClass = 3; + usbDeviceType.subClass = 1; + usbDeviceType.protocal = 2; + usbDeviceType.isDeviceType = 0; + disableType.emplace_back(usbDeviceType); + ret = client.ManageInterfaceType(disableType, true); ASSERT_EQ(ret, 0); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceStorage001 : ManageInterfaceStorage"); -} - -/** - * @tc.name: ManageInterfaceStorage002 - * @tc.desc: Test functions to ManageInterfaceStorage(InterfaceType interfaceType, bool disable); - * @tc.type: FUNC - */ -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceStorage002, TestSize.Level1) -{ - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceStorage002 : ManageInterfaceStorage"); - UsbCommonTest::GrantPermissionNormalNative(); - auto &client = UsbSrvClient::GetInstance(); - vector devi; - auto ret = client.GetDevices(devi); - EXPECT_TRUE(ret == 0); - InterfaceType interfaceType = InterfaceType::TYPE_HID; - if (devi.size() > 0) { - for (auto iter = g_typeMap.begin(); iter != g_typeMap.end(); iter ++) { - if (devi.at(0).GetClass() == iter->second[0] && - (devi.at(0).GetSubclass() == iter->second[1] || iter->second[1] == -1) && - (devi.at(0).GetProtocol() == iter->second[2] || iter->second[2] == -1)) { - interfaceType = iter->first; - } - } - } - ret = client.ManageInterfaceStorage(interfaceType, true); - ASSERT_NE(ret, 0); - UsbCommonTest::GrantPermissionSysNative(); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceStorage002 : ManageInterfaceStorage"); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType001 : ManageInterfaceType"); } -/** - * @tc.name: ManageInterfaceStorage003 - * @tc.desc: Test functions to ManageInterfaceStorage(InterfaceType interfaceType, bool disable); - * @tc.type: FUNC - */ -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceStorage003, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType002, TestSize.Level1) { + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType002 : ManageInterfaceType"); auto &client = UsbSrvClient::GetInstance(); vector devi; auto ret = client.GetDevices(devi); EXPECT_TRUE(ret == 0); - InterfaceType interfaceType = InterfaceType::TYPE_HID; - if (devi.size() > 0) { - for (auto iter = g_typeMap.begin(); iter != g_typeMap.end(); iter ++) { - if (devi.at(0).GetClass() == iter->second[0] && - (devi.at(0).GetSubclass() == iter->second[1] || iter->second[1] == -1) && - (devi.at(0).GetProtocol() == iter->second[2] || iter->second[2] == -1)) { - interfaceType = iter->first; - } - } - } - ret = client.ManageInterfaceStorage(interfaceType, false); + vector disableType; + UsbDeviceType usbDeviceType; + usbDeviceType.baseClass = 8; + usbDeviceType.subClass = 6; + usbDeviceType.protocal = 80; + usbDeviceType.isDeviceType = 0; + disableType.emplace_back(usbDeviceType); + ret = client.ManageInterfaceType(disableType, true); ASSERT_EQ(ret, 0); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType002 : ManageInterfaceType"); } -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType001, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType003, TestSize.Level1) { - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType001 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType003 : ManageInterfaceType"); auto &client = UsbSrvClient::GetInstance(); vector devi; auto ret = client.GetDevices(devi); @@ -279,28 +236,28 @@ HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType001, TestSize.Level1) usbDeviceType.protocal = 2; usbDeviceType.isDeviceType = 0; disableType.emplace_back(usbDeviceType); - ret = client.ManageInterfaceType(disableType, true); + ret = client.ManageInterfaceType(disableType, false); ASSERT_EQ(ret, 0); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType001 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType003 : ManageInterfaceType"); } -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType002, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType004, TestSize.Level1) { - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType002 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType004 : ManageInterfaceType"); auto &client = UsbSrvClient::GetInstance(); vector devi; auto ret = client.GetDevices(devi); EXPECT_TRUE(ret == 0); vector disableType; UsbDeviceType usbDeviceType; - usbDeviceType.baseClass = 3; - usbDeviceType.subClass = 1; - usbDeviceType.protocal = 2; + usbDeviceType.baseClass = 8; + usbDeviceType.subClass = 6; + usbDeviceType.protocal = 80; usbDeviceType.isDeviceType = 0; disableType.emplace_back(usbDeviceType); ret = client.ManageInterfaceType(disableType, false); ASSERT_EQ(ret, 0); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType002 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType004 : ManageInterfaceType"); } } // ManagerInterface } // USB diff --git a/utils/native/include/usb_common.h b/utils/native/include/usb_common.h index 3c42109c4619ee1373512e2227b679ec086da4ee..042858676c013d0c299bd9827dd3d811b9148bac 100644 --- a/utils/native/include/usb_common.h +++ b/utils/native/include/usb_common.h @@ -115,6 +115,14 @@ constexpr uint32_t USB_CFG_REMOTE_WAKEUP = 0x20; } \ } while (0) +#define WRITE_PARCEL_AND_RETURN_FALSE_WHEN_FAIL(type, parcel, data) \ + do { \ + if (!(parcel).Write##type(data)) { \ + USB_HILOGE(MODULE_COMMON, "%{public}s write " #data " failed", __func__); \ + return false; \ + } \ + } while (0) + #define READ_PARCEL_WITH_RET(parcel, type, out, retval) \ do { \ if (!(parcel).Read##type(out)) { \