diff --git a/interfaces/innerkits/native/include/iusb_srv.h b/interfaces/innerkits/native/include/iusb_srv.h index d8a7cea..c980b85 100644 --- a/interfaces/innerkits/native/include/iusb_srv.h +++ b/interfaces/innerkits/native/include/iusb_srv.h @@ -71,7 +71,8 @@ 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 ManageInterfaceType(InterfaceType interfaceType, 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, bool &unactivated) = 0; diff --git a/interfaces/innerkits/native/include/usb_interface_type.h b/interfaces/innerkits/native/include/usb_interface_type.h index 4a94cc7..52143fc 100644 --- a/interfaces/innerkits/native/include/usb_interface_type.h +++ b/interfaces/innerkits/native/include/usb_interface_type.h @@ -25,6 +25,78 @@ enum InterfaceType { TYPE_PHYSICAL, TYPE_IMAGE, TYPE_PRINTER, + TYPE_COMMUNICATION, + TYPE_FULL_SPEED_HUB, + TYPE_FULL_SPEED_HUB_S, + TYPE_FULL_SPEED_HUB_M, + TYPE_CDC_DATA, + TYPE_SMART_CARD, + TYPE_CONTENT_SECURTIY, + TYPE_VIDEO, + TYPE_PERSONAL_HEALTHCARE, + TYPE_AVCONTROL, + TYPE_AVV_STREAMING, + TYPE_AVA_STREAMING, + TYPE_BILLBOARD, + TYPE_TYPEC_BRIDGE, + TYPE_BDP, + TYPE_MCTP_MANA_1, + TYPE_MCTP_MANA_2, + TYPE_MCTP_HOST_1, + TYPE_MCTP_HOST_2, + TYPE_I3C, + TYPE_DIAGNOSTIC_1, + TYPE_DIAGNOSTIC_2, + TYPE_DIAGNOSTIC_3, + TYPE_DIAGNOSTIC_4, + TYPE_DIAGNOSTIC_5, + TYPE_DIAGNOSTIC_6, + TYPE_DIAGNOSTIC_7, + TYPE_DIAGNOSTIC_8, + TYPE_DIAGNOSTIC_9, + TYPE_DIAGNOSTIC_10, + TYPE_DIAGNOSTIC_11, + TYPE_DIAGNOSTIC_12, + TYPE_DIAGNOSTIC_13, + TYPE_DIAGNOSTIC_14, + TYPE_WIRLESS_CONTROLLER_1, + TYPE_WIRLESS_CONTROLLER_2, + TYPE_WIRLESS_CONTROLLER_3, + TYPE_WIRLESS_CONTROLLER_4, + TYPE_WIRLESS_CONTROLLER_5, + TYPE_WIRLESS_CONTROLLER_6, + TYPE_WIRLESS_CONTROLLER_7, + TYPE_MISCELLANEOUS_1, + TYPE_MISCELLANEOUS_2, + TYPE_MISCELLANEOUS_3, + TYPE_MISCELLANEOUS_4, + TYPE_MISCELLANEOUS_5, + TYPE_MISCELLANEOUS_6, + TYPE_MISCELLANEOUS_7, + TYPE_MISCELLANEOUS_8, + TYPE_MISCELLANEOUS_9, + TYPE_MISCELLANEOUS_10, + TYPE_MISCELLANEOUS_11, + TYPE_MISCELLANEOUS_12, + TYPE_MISCELLANEOUS_13, + TYPE_MISCELLANEOUS_14, + TYPE_MISCELLANEOUS_15, + TYPE_MISCELLANEOUS_16, + TYPE_MISCELLANEOUS_17, + TYPE_MISCELLANEOUS_18, + TYPE_MISCELLANEOUS_19, + TYPE_APPLICATION_SPE_1, + TYPE_APPLICATION_SPE_2, + TYPE_APPLICATION_SPE_3, + TYPE_APPLICATION_SPE_4, + TYPE_VENDOR_SPECIFIC, +}; + +struct UsbDeviceType { + int32_t baseClass; + int32_t subClass; + int32_t protocal; + bool isDeviceType; }; struct UsbDeviceId { @@ -32,6 +104,79 @@ struct UsbDeviceId { int32_t vendorId; }; +const std::unordered_map> g_typeMap = { + {InterfaceType::TYPE_AUDIO, {1, -1, -1}}, + {InterfaceType::TYPE_COMMUNICATION, {2, -1, -1}}, + {InterfaceType::TYPE_HID, {3, -1, -1}}, + {InterfaceType::TYPE_PHYSICAL, {5, -1, -1}}, + {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}}, + {InterfaceType::TYPE_VIDEO, {14, -1, -1}}, + {InterfaceType::TYPE_PERSONAL_HEALTHCARE, {15, -1, -1}}, + {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}}, + {InterfaceType::TYPE_MCTP_MANA_2, {20, 0, 2}}, + {InterfaceType::TYPE_MCTP_HOST_1, {20, 1, 1}}, + {InterfaceType::TYPE_MCTP_HOST_2, {20, 1, 2}}, + {InterfaceType::TYPE_I3C, {60, 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_WIRLESS_CONTROLLER_1, {224, 1, 1}}, + {InterfaceType::TYPE_WIRLESS_CONTROLLER_2, {224, 1, 2}}, + {InterfaceType::TYPE_WIRLESS_CONTROLLER_3, {224, 1, 3}}, + {InterfaceType::TYPE_WIRLESS_CONTROLLER_4, {224, 1, 4}}, + {InterfaceType::TYPE_WIRLESS_CONTROLLER_5, {224, 2, 1}}, + {InterfaceType::TYPE_WIRLESS_CONTROLLER_6, {224, 2, 2}}, + {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}}, + {InterfaceType::TYPE_MISCELLANEOUS_8, {239, 4, 3}}, + {InterfaceType::TYPE_MISCELLANEOUS_9, {239, 4, 4}}, + {InterfaceType::TYPE_MISCELLANEOUS_10, {239, 4, 5}}, + {InterfaceType::TYPE_MISCELLANEOUS_11, {239, 4, 6}}, + {InterfaceType::TYPE_MISCELLANEOUS_12, {239, 4, 7}}, + {InterfaceType::TYPE_MISCELLANEOUS_13, {239, 5, 0}}, + {InterfaceType::TYPE_MISCELLANEOUS_14, {239, 5, 1}}, + {InterfaceType::TYPE_MISCELLANEOUS_15, {239, 5, 2}}, + {InterfaceType::TYPE_MISCELLANEOUS_16, {239, 6, 1}}, + {InterfaceType::TYPE_MISCELLANEOUS_17, {239, 6, 2}}, + {InterfaceType::TYPE_MISCELLANEOUS_18, {239, 7, 1}}, + {InterfaceType::TYPE_MISCELLANEOUS_19, {239, 7, 2}}, + {InterfaceType::TYPE_APPLICATION_SPE_1, {254, 1, 1}}, + {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 #endif // USB_INTERFACE_TYPE_H diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index d459e73..8acf9ea 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -90,7 +90,8 @@ 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 ManageInterfaceType(InterfaceType interfaceType, bool disable); + int32_t ManageInterfaceStorage(InterfaceType interfaceType, bool disable); + int32_t ManageInterfaceType(const std::vector &disableType, bool disable); private: class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { public: diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp index 81e4592..fa87206 100644 --- a/interfaces/innerkits/native/src/usb_srv_client.cpp +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -475,10 +475,20 @@ int32_t UsbSrvClient::ManageDevice(int32_t vendorId, int32_t productId, bool dis return ret; } -int32_t UsbSrvClient::ManageInterfaceType(InterfaceType interfaceType, bool disable) +int32_t UsbSrvClient::ManageInterfaceStorage(InterfaceType interfaceType, bool disable) { RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT); - int32_t ret = proxy_->ManageInterfaceType(interfaceType, disable); + 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); + int32_t ret = proxy_->ManageInterfaceType(disableType, disable); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret); } diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index b444117..ded7eca 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -125,7 +125,8 @@ 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 ManageInterfaceType(InterfaceType interfaceType, 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; private: @@ -161,6 +162,7 @@ private: int32_t PreCallFunction(); 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, std::vector &trustUsbDeviceId); int32_t GetUsbPolicy(bool &IsGlobalDisabled, std::unordered_map &typeDisableMap, diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index 85d4485..28557c3 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -82,14 +82,6 @@ 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"; -std::unordered_map> g_typeMap = { - {InterfaceType::TYPE_STORAGE, {8, -1, -1}}, - {InterfaceType::TYPE_AUDIO, {1, -1, -1}}, - {InterfaceType::TYPE_HID, {3, -1, -1}}, - {InterfaceType::TYPE_PHYSICAL, {5, -1, -1}}, - {InterfaceType::TYPE_IMAGE, {6, 1, 1}}, - {InterfaceType::TYPE_PRINTER, {7, -1, -1}} -}; } // namespace auto g_serviceInstance = DelayedSpSingleton::GetInstance(); const bool G_REGISTER_RESULT = @@ -1167,6 +1159,36 @@ int32_t UsbService::GetUsbPolicy(bool &IsGlobalDisabled, std::unordered_map &disableType, bool disable) +{ + int32_t ret = UEC_INTERFACE_NO_MEMORY; + std::mapdevices; + usbHostManager_->GetDevices(devices); + USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu", devices.size()); + for (auto dev : disableType) { + for (auto& [interfaceTypeValues, typeValues] : g_typeMap) { + if ((!dev.isDeviceType) && + (typeValues[0] == dev.baseClass) && + (typeValues[1] == -1 || typeValues[1] == dev.subClass)&& + (typeValues[HALF] == -1 || typeValues[HALF] == dev.protocal)) { + ret = ManageInterfaceTypeImpl(interfaceTypeValues, disable); + } else { + USB_HILOGE(MODULE_USB_SERVICE, "ExecuteManageInterfaceType Invalid data"); + } + } + if (dev.isDeviceType) { + for (auto it = devices.begin(); it != devices.end(); ++it) { + ret = ManageDeviceImpl(it->second->GetVendorId(), it->second->GetProductId(), disable); + } + } + } + if (ret != UEC_OK) { + USB_HILOGI(MODULE_USB_SERVICE, "ExecuteManageInterfaceType failed"); + return UEC_SERVICE_EXECUTE_POLICY_FAILED; + } + return UEC_OK; +} + int32_t UsbService::ExecuteManageDevicePolicy(std::vector &whiteList) { std::map devices; @@ -1240,6 +1262,7 @@ void UsbService::ExecuteStrategy(UsbDevice *devInfo) USB_HILOGI(MODULE_USB_SERVICE, "Execute ManageInterfaceType finish"); return; } + if (trustUsbDeviceIds.empty()) { USB_HILOGI(MODULE_USB_SERVICE, "trustUsbDeviceIds is empty, no devices disable"); return; @@ -1709,7 +1732,7 @@ int32_t UsbService::ManageDevice(int32_t vendorId, int32_t productId, bool disab return ManageDeviceImpl(vendorId, productId, disable); } -int32_t UsbService::ManageInterfaceType(InterfaceType interfaceType, bool disable) +int32_t UsbService::ManageInterfaceStorage(InterfaceType interfaceType, bool disable) { if (PreCallFunction() != UEC_OK) { USB_HILOGE(MODULE_USB_SERVICE, "PreCallFunction failed"); @@ -1718,6 +1741,15 @@ int32_t UsbService::ManageInterfaceType(InterfaceType interfaceType, bool disabl return ManageInterfaceTypeImpl(interfaceType, disable); } +int32_t UsbService::ManageInterfaceType(const std::vector &disableType, bool disable) +{ + if (PreCallFunction() != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "PreCallFunction failed"); + return UEC_SERVICE_PRE_MANAGE_INTERFACE_FAILED; + } + return ExecuteManageInterfaceType(disableType, disable); +} + int32_t UsbService::ManageGlobalInterfaceImpl(bool disable) { std::map devices; diff --git a/services/zidl/include/usb_server_proxy.h b/services/zidl/include/usb_server_proxy.h index 9c6cab6..d1a969b 100644 --- a/services/zidl/include/usb_server_proxy.h +++ b/services/zidl/include/usb_server_proxy.h @@ -79,7 +79,8 @@ 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 ManageInterfaceType(InterfaceType interfaceType, 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; private: diff --git a/services/zidl/include/usb_server_stub.h b/services/zidl/include/usb_server_stub.h index c182a49..c8e1277 100644 --- a/services/zidl/include/usb_server_stub.h +++ b/services/zidl/include/usb_server_stub.h @@ -88,7 +88,9 @@ 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); int32_t DoGetInterfaceActiveStatus(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 4ebe548..c7d04b9 100644 --- a/services/zidl/include/usb_service_ipc_interface_code.h +++ b/services/zidl/include/usb_service_ipc_interface_code.h @@ -55,6 +55,7 @@ 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 a73adcb..34c39d9 100644 --- a/services/zidl/src/usb_srv_proxy.cpp +++ b/services/zidl/src/usb_srv_proxy.cpp @@ -1173,7 +1173,7 @@ int32_t UsbServerProxy::ManageDevice(int32_t vendorId, int32_t productId, bool d return ret; } -int32_t UsbServerProxy::ManageInterfaceType(InterfaceType interfaceType, bool disable) +int32_t UsbServerProxy::ManageInterfaceStorage(InterfaceType interfaceType, bool disable) { sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE); @@ -1187,6 +1187,37 @@ int32_t UsbServerProxy::ManageInterfaceType(InterfaceType interfaceType, bool di 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(); + 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 size = (int32_t)disableType.size(); + WRITE_PARCEL_WITH_RET(data, Int32, size, UEC_SERVICE_WRITE_PARCEL_ERROR); + + 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, Bool, type.isDeviceType, 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_TYPE), diff --git a/services/zidl/src/usb_srv_stub.cpp b/services/zidl/src/usb_srv_stub.cpp index ac2646b..5738329 100644 --- a/services/zidl/src/usb_srv_stub.cpp +++ b/services/zidl/src/usb_srv_stub.cpp @@ -210,6 +210,9 @@ 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; @@ -975,13 +978,35 @@ int32_t UsbServerStub::DoManageDevice(MessageParcel &data, MessageParcel &reply, return ret; } -int32_t UsbServerStub::DoManageInterfaceType(MessageParcel &data, MessageParcel &reply, MessageOption &option) +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 = ManageInterfaceType((InterfaceType)interfaceType, disable); + 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; + READ_PARCEL_WITH_RET(data, Int32, count, UEC_SERVICE_READ_PARCEL_ERROR); + bool disable = false; + std::vector disableType; + for (int32_t i = 0; i < count; ++i) { + 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, Bool, usbDeviceType.isDeviceType, UEC_SERVICE_READ_PARCEL_ERROR); + disableType.emplace_back(usbDeviceType); + } + READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR); + int32_t ret = ManageInterfaceType(disableType, disable); if (ret != UEC_OK) { USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret); } 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 7456b5d..67a7288 100644 --- a/test/native/service_unittest/src/usb_manage_interface_test.cpp +++ b/test/native/service_unittest/src/usb_manage_interface_test.cpp @@ -27,6 +27,7 @@ #include "usb_common_test.h" #include "usb_srv_client.h" #include "usb_errors.h" +#include "usb_interface_type.h" using namespace testing::ext; using namespace OHOS; @@ -40,13 +41,6 @@ namespace USB { namespace ManageInterface { constexpr int32_t SLEEP_TIME = 3; -std::unordered_map> typeMapInfo = { - {InterfaceType::TYPE_STORAGE, {8, -1, -1}}, - {InterfaceType::TYPE_AUDIO, {1, -1, -1}}, - {InterfaceType::TYPE_HID, {3, -1, -1}}, - {InterfaceType::TYPE_PRINTER, {7, -1, -1}} -}; - void UsbManageInterfaceTest::SetUpTestCase(void) { UsbCommonTest::GrantPermissionSysNative(); @@ -191,20 +185,20 @@ HWTEST_F(UsbManageInterfaceTest, ManageDevice003, TestSize.Level1) } /** - * @tc.name: ManageInterfaceType001 - * @tc.desc: Test functions toManageInterfaceType(InterfaceType interfaceType, bool disable); + * @tc.name: ManageInterfaceStorage001 + * @tc.desc: Test functions toManageInterfaceStorage(InterfaceType interfaceType, bool disable); * @tc.type: FUNC */ -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType001, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceStorage001, TestSize.Level1) { - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType001 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceStorage001 : ManageInterfaceStorage"); 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 = typeMapInfo.begin(); iter != typeMapInfo.end(); iter ++) { + 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)) { @@ -212,19 +206,19 @@ HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType001, TestSize.Level1) } } } - ret = client.ManageInterfaceType(interfaceType, true); + ret = client.ManageInterfaceStorage(interfaceType, true); ASSERT_EQ(ret, 0); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType001 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceStorage001 : ManageInterfaceStorage"); } /** - * @tc.name: ManageInterfaceType002 - * @tc.desc: Test functions to ManageInterfaceType(InterfaceType interfaceType, bool disable); + * @tc.name: ManageInterfaceStorage002 + * @tc.desc: Test functions to ManageInterfaceStorage(InterfaceType interfaceType, bool disable); * @tc.type: FUNC */ -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType002, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceStorage002, TestSize.Level1) { - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType002 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceStorage002 : ManageInterfaceStorage"); UsbCommonTest::GrantPermissionNormalNative(); auto &client = UsbSrvClient::GetInstance(); vector devi; @@ -240,18 +234,18 @@ HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType002, TestSize.Level1) } } } - ret = client.ManageInterfaceType(interfaceType, true); + ret = client.ManageInterfaceStorage(interfaceType, true); ASSERT_NE(ret, 0); UsbCommonTest::GrantPermissionSysNative(); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType002 : ManageInterfaceType"); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceStorage002 : ManageInterfaceStorage"); } /** - * @tc.name: ManageInterfaceType003 - * @tc.desc: Test functions to ManageInterfaceType(InterfaceType interfaceType, bool disable); + * @tc.name: ManageInterfaceStorage003 + * @tc.desc: Test functions to ManageInterfaceStorage(InterfaceType interfaceType, bool disable); * @tc.type: FUNC */ -HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType003, TestSize.Level1) +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceStorage003, TestSize.Level1) { auto &client = UsbSrvClient::GetInstance(); vector devi; @@ -259,7 +253,7 @@ HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType003, TestSize.Level1) EXPECT_TRUE(ret == 0); InterfaceType interfaceType = InterfaceType::TYPE_HID; if (devi.size() > 0) { - for (auto iter = typeMapInfo.begin(); iter != typeMapInfo.end(); iter ++) { + 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)) { @@ -267,10 +261,63 @@ HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType003, TestSize.Level1) } } } - ret = client.ManageInterfaceType(interfaceType, false); + ret = client.ManageInterfaceStorage(interfaceType, false); ASSERT_EQ(ret, 0); } +HWTEST_F(UsbManageInterfaceTest, ManageInterfaceType001, TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ManageInterfaceType001 : ManageInterfaceType"); + auto &client = UsbSrvClient::GetInstance(); + vector devi; + auto ret = client.GetDevices(devi); + EXPECT_TRUE(ret == 0); + vector disableType; + 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)) { + UsbDeviceType usbDeviceType; + usbDeviceType.baseClass = devi.at(0).GetClass(); + usbDeviceType.subClass = devi.at(0).GetSubclass(); + usbDeviceType.protocal = devi.at(0).GetProtocol(); + usbDeviceType.isDevicetype = 0; + disableType.emplace_back(usbDeviceType); + } + } + } + ret = client.ManageInterfaceType(disableType, true); + ASSERT_EQ(ret, 0); + USB_HILOGI(MODULE_USB_SERVICE, "Case End : ManageInterfaceType001 : ManageInterfaceType"); +} + +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); + vector disableType; + 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)) { + UsbDeviceType usbDeviceType; + usbDeviceType.baseClass = devi.at(0).GetClass(); + usbDeviceType.subClass = devi.at(0).GetSubclass(); + usbDeviceType.protocal = devi.at(0).GetProtocol(); + 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"); +} } // ManagerInterface } // USB } // OHOS