diff --git a/hisysevent.yaml b/hisysevent.yaml index d7f8deb2e3e5b3797c34deb00e6448b0659ff2ca..b4aa380f92a59213d7502e2e09c4ff5e0a76b82a 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -17,7 +17,10 @@ PLUG_IN_OUT_HOST_MODE: __BASE: {type: BEHAVIOR, level: MINOR, tag: UsbManager, desc: UsbDevicePlugInHostMode} DEVICE_NAME: {type: STRING, desc: device name} DEVICE_PROTOCOL: {type: INT32, desc: device protocol} + DEVICE_SUBCLASS: {type: INT32, desc: device subclass} DEVICE_CLASS: {type: INT32, desc: device class} + DEVICE_CLASS_DESCRIPTION: {type: STRING, desc: device class description} + INTERFACE_CLASS_DESCRIPTION: {type: STRING, desc: interface class description} VENDOR_ID: {type: INT32, desc: vendor id} PRODUCT_ID: {type: INT32, desc: product id} VERSION: {type: STRING, desc: device version} diff --git a/interfaces/innerkits/native/include/usb_interface_type.h b/interfaces/innerkits/native/include/usb_interface_type.h index bea3b40f86fb52eeac1f693b3abdb5259b4d8b0e..77fb6e5bd76906c4baa1b477026a2186e2c7241b 100644 --- a/interfaces/innerkits/native/include/usb_interface_type.h +++ b/interfaces/innerkits/native/include/usb_interface_type.h @@ -115,6 +115,9 @@ struct UsbDeviceType { return (baseClass == other.baseClass) && (subClass == other.subClass) && (protocol == other.protocol) && (isDeviceType == other.isDeviceType); } + UsbDeviceType (int32_t deviceBaseClass, int32_t sub, int32_t prot, bool deviceType) + : baseClass(deviceBaseClass), subClass(sub), protocol(prot), isDeviceType(deviceType) {}; + UsbDeviceType (): baseClass(0), subClass(0), protocol(0), isDeviceType(0) {}; bool Marshalling(MessageParcel &parcel) const; static bool Unmarshalling(MessageParcel &parcel, UsbDeviceType &usbDeviceType); bool ReadFromParcel(MessageParcel &parcel); diff --git a/services/native/include/usb_host_manager.h b/services/native/include/usb_host_manager.h index eddbd394cd899fd596cf1c20f899cabf1e3baefc..545d357859971cc9fa9789c22d34c6edc1582695 100644 --- a/services/native/include/usb_host_manager.h +++ b/services/native/include/usb_host_manager.h @@ -23,11 +23,19 @@ #include "system_ability.h" #include "usb_device.h" #include "usb_right_manager.h" +#include "usb_interface_type.h" namespace OHOS { namespace USB { -typedef std::map MAP_STR_DEVICE; +struct DeviceClassUsage { + uint8_t usage; + std::string description; + + DeviceClassUsage(int8_t uage, std::string des) + : usage(uage), description(des) {}; +}; +typedef std::map MAP_STR_DEVICE; class UsbHostManager { public: explicit UsbHostManager(SystemAbility *systemAbility); @@ -41,7 +49,10 @@ public: private: bool PublishCommonEvent(const std::string &event, const UsbDevice &dev); void ReportHostPlugSysEvent(const std::string &event, const UsbDevice &dev); - + std::string ConcatenateToDescription(const UsbDeviceType &interfaceType, const std::string& str); + int32_t GetDeviceDescription(int32_t baseClass, std::string &description, uint8_t &usage); + int32_t GetInterfaceDescription(const UsbDevice &dev, std::string &description, int32_t &baseClass); + std::string GetInterfaceUsageDescription(const UsbDeviceType &interfaceType); MAP_STR_DEVICE devices_; SystemAbility *systemAbility_; }; diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index 78f81476bd194a6a0c7293133f891b9f354dc72a..86ddf8a83abadf18c3099bc6b633db12310a0d89 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -12,6 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include +#include +#include +#include +#include #include "usb_host_manager.h" #include "common_event_data.h" @@ -21,6 +26,10 @@ #include "hisysevent.h" #include "cJSON.h" #include "usb_serial_reader.h" +#include "usb_device.h" +#include "usb_config.h" +#include "usb_interface.h" +#include "usb_errors.h" #ifdef USB_NOTIFICATION_ENABLE #include "usb_mass_storage_notification.h" @@ -32,6 +41,33 @@ using namespace OHOS::HiviewDFX; namespace OHOS { namespace USB { +constexpr int32_t CLASS_PRINT_LENGTH = 2; +constexpr int32_t USAGE_IN_INTERFACE_CLASS = 0; +constexpr uint8_t DES_USAGE_IN_INTERFACE = 0x02; +std::map deviceUsageMap = { + {0x00, {DeviceClassUsage(2, "Use class information in the Interface Descriptors")}}, + {0x01, {DeviceClassUsage(2, "Audio")}}, + {0x02, {DeviceClassUsage(3, "Communications and CDC Control")}}, + {0x03, {DeviceClassUsage(2, "HID(Human Interface Device)")}}, + {0x05, {DeviceClassUsage(2, "Physical")}}, + {0x06, {DeviceClassUsage(2, "Image")}}, + {0x07, {DeviceClassUsage(2, "Printer")}}, + {0x08, {DeviceClassUsage(2, "Mass Storage")}}, + {0x09, {DeviceClassUsage(1, "Hub")}}, + {0x0a, {DeviceClassUsage(2, "CDC-Data")}}, + {0x0b, {DeviceClassUsage(2, "Smart Card")}}, + {0x0d, {DeviceClassUsage(2, "Content Security")}}, + {0x0e, {DeviceClassUsage(2, "Video")}}, + {0x0f, {DeviceClassUsage(2, "Personal Healthcare")}}, + {0x10, {DeviceClassUsage(2, "Audio/Video Device")}}, + {0x11, {DeviceClassUsage(1, "Billboard Device Class")}}, + {0x12, {DeviceClassUsage(2, "USB Type-C Bridge Class")}} +}; + +std::map interfaceUsageMap = { + {{UsbDeviceType(0x03, 0x01, 0x01, 0)}, "KeyBoard"}, + {{UsbDeviceType(0x03, 0x01, 0x02, 0)}, "Mouse/Table/Touch screen"}, +}; UsbHostManager::UsbHostManager(SystemAbility *systemAbility) { systemAbility_ = systemAbility; @@ -158,13 +194,86 @@ bool UsbHostManager::Dump(int fd, const std::string &args) return true; } +int32_t UsbHostManager::GetDeviceDescription(int32_t baseClass, std::string &description, uint8_t &usage) +{ + auto iter = deviceUsageMap.find(baseClass); + if (iter != deviceUsageMap.end()) { + description = iter->second.description; + usage = iter->second.usage; + } else { + description = "NA"; + usage = 1; + } + return UEC_OK; +} + + +std::string UsbHostManager::ConcatenateToDescription(const UsbDeviceType &interfaceType, const std::string& str) +{ + std::stringstream ss; + ss << std::setw(CLASS_PRINT_LENGTH) << std::setfill('0') << std::hex << interfaceType.baseClass << "_"; + ss << std::setw(CLASS_PRINT_LENGTH) << std::setfill('0') << std::hex << interfaceType.subClass << "_"; + ss << std::setw(CLASS_PRINT_LENGTH) << std::setfill('0') << std::hex << interfaceType.protocol << ","; + ss << str; + return ss.str(); +} + +std::string UsbHostManager::GetInterfaceUsageDescription(const UsbDeviceType &interfaceType) +{ + std::string infUsageDes = "NA"; + auto infUsageIter = interfaceUsageMap.find(interfaceType); + if (infUsageIter != interfaceUsageMap.end()) { + return infUsageIter->second; + } + return infUsageDes; +} + +int32_t UsbHostManager::GetInterfaceDescription(const UsbDevice &dev, std::string &description, int32_t &baseClass) +{ + std::set useInterfaceType; + for (int32_t i = 0; i < dev.GetConfigCount(); i++) { + USBConfig config; + dev.GetConfig(i, config); + for (uint32_t j = 0; j < config.GetInterfaceCount(); j++) { + if (i != 0 || j != 0) { + description += ";"; + } + UsbInterface interface; + config.GetInterface(j, interface); + baseClass = interface.GetClass(); + UsbDeviceType interfaceType = {interface.GetClass(), + interface.GetSubClass(), interface.GetProtocol(), 0}; + if (useInterfaceType.find(interfaceType) == useInterfaceType.end()) { + useInterfaceType.insert(interfaceType); + std::string infUsageDes = GetInterfaceUsageDescription(interfaceType); + description += ConcatenateToDescription(interfaceType, infUsageDes); + } + } + } + return UEC_OK; +} + void UsbHostManager::ReportHostPlugSysEvent(const std::string &event, const UsbDevice &dev) { + std::string deviceUsageDes; + uint8_t deviceUsage = 0; + GetDeviceDescription(dev.GetClass(), deviceUsageDes, deviceUsage); + std::string extUsageDes; + int32_t intfBaseClass = 0; + if (deviceUsage & DES_USAGE_IN_INTERFACE) { + GetInterfaceDescription(dev, extUsageDes, intfBaseClass); + } + + if (dev.GetClass() == USAGE_IN_INTERFACE_CLASS) { + GetDeviceDescription(intfBaseClass, deviceUsageDes, deviceUsage); + } USB_HILOGI(MODULE_SERVICE, "Host mode Indicates the insertion and removal information"); HiSysEventWrite(HiSysEvent::Domain::USB, "PLUG_IN_OUT_HOST_MODE", HiSysEvent::EventType::BEHAVIOR, - "DEVICE_NAME", dev.GetName(), "DEVICE_PROTOCOL", dev.GetProtocol(), "DEVICE_CLASS", dev.GetClass(), - "VENDOR_ID", dev.GetVendorId(), "PRODUCT_ID", dev.GetProductId(), "VERSION", dev.GetVersion(), - "EVENT_NAME", event); + "DEVICE_NAME", dev.GetName(), "DEVICE_PROTOCOL", dev.GetProtocol(), + "DEVICE_SUBCLASS", dev.GetSubclass(), "DEVICE_CLASS", dev.GetClass(), + "DEVICE_CLASS_DESCRIPTION", deviceUsageDes, "INTERFACE_CLASS_DESCRIPTION", extUsageDes, + "VENDOR_ID", dev.GetVendorId(), "PRODUCT_ID", dev.GetProductId(), + "VERSION", dev.GetVersion(), "EVENT_NAME", event); } } // namespace USB } // namespace OHOS