kellyjonbrazil / jc

CLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
MIT License
7.91k stars 210 forks source link

New Parser: lsusb #174

Closed bobpaul closed 3 years ago

bobpaul commented 3 years ago

Verbose I filtered this one to a single device. Without the filter the output is the same, but a block like this for each device.

$ lsusb -d 1915:521a -v

Bus 003 Device 090: ID 1915:521a Nordic Semiconductor ASA nRF52 USB CDC BLE Demo
Couldn't open device, some information will be missing
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x1915 Nordic Semiconductor ASA
  idProduct          0x521a 
  bcdDevice            1.00
  iManufacturer           1 Nordic Semiconductor
  iProduct                2 nRF52 USB CDC BLE Demo
  iSerial                 3 F42DE60BE261
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x004b
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          4 
    bmAttributes         0xc0
      Self Powered
    MaxPower              100mA
    Interface Association:
      bLength                 8
      bDescriptorType        11
      bFirstInterface         0
      bInterfaceCount         2
      bFunctionClass          2 Communications
      bFunctionSubClass       2 Abstract (modem)
      bFunctionProtocol       1 AT-commands (v.25ter)
      iFunction               0 
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         2 Communications
      bInterfaceSubClass      2 Abstract (modem)
      bInterfaceProtocol      1 AT-commands (v.25ter)
      iInterface              0 
      CDC Header:
        bcdCDC               1.10
      CDC Call Management:
        bmCapabilities       0x03
          call management
          use DataInterface
        bDataInterface          1
      CDC ACM:
        bmCapabilities       0x02
          line coding and serial state
      CDC Union:
        bMasterInterface        0
        bSlaveInterface         1 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval              16
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass        10 CDC Data
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0

Normal

$ lsusb -d 1915:521a
Bus 003 Device 090: ID 1915:521a Nordic Semiconductor ASA nRF52 USB CDC BLE Demo

Tree

$ lsusb -t
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 5000M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 2: Dev 90, If 0, Class=Communications, Driver=cdc_acm, 12M
        |__ Port 2: Dev 90, If 1, Class=CDC Data, Driver=cdc_acm, 12M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 1: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 2: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 6: Dev 66, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 1: Dev 67, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            |__ Port 1: Dev 67, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M

Verbose Tree

$ lsusb -tv
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 5000M
    ID 1d6b:0003 Linux Foundation 3.0 root hub
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        ID 0409:005a NEC Corp. HighSpeed Hub
        |__ Port 2: Dev 90, If 0, Class=Communications, Driver=cdc_acm, 12M
            ID 1915:521a Nordic Semiconductor ASA 
        |__ Port 2: Dev 90, If 1, Class=CDC Data, Driver=cdc_acm, 12M
            ID 1915:521a Nordic Semiconductor ASA 
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/8p, 480M
        ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/3p, 480M
    ID 1d6b:0002 Linux Foundation 2.0 root hub
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
        |__ Port 1: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            ID 045e:00d1 Microsoft Corp. Optical Mouse with Tilt Wheel
        |__ Port 2: Dev 4, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            ID 051d:0002 American Power Conversion Uninterruptible Power Supply
        |__ Port 6: Dev 66, If 0, Class=Hub, Driver=hub/4p, 480M
            ID 0424:2504 Microchip Technology, Inc. (formerly SMSC) 
            |__ Port 1: Dev 67, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
                ID 0518:0001 EzKEY Corp. USB to PS2 Adaptor v1.09
            |__ Port 1: Dev 67, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
                ID 0518:0001 EzKEY Corp. USB to PS2 Adaptor v1.09
kellyjonbrazil commented 3 years ago

Looks like a great candidate! We’ll get this in the queue.

kellyjonbrazil commented 3 years ago

The odd output structure of lsusb makes this a hairy one, but I think I found a somewhat sane way to do it. This will be a fun one, for sure. :)

kellyjonbrazil commented 3 years ago

I am making progress on this parser. It's definitely a tough one, but it's over 80% complete now. Just need to finish a couple of items and then do some fine tuning. I hope to have a version to test in the next week or so.

kellyjonbrazil commented 3 years ago

I have a working version of the lsusb parser in the dev branch. Could you copy/paste the code from the link below into a file called lsusb.py in your App data directory to test it?

https://github.com/kellyjonbrazil/jc/blob/dev/jc/parsers/lsusb.py

This is probably one of the most complex parsers I've written due to the many state changes required. Maybe there was a simpler way to do it, but I ended up at this this solution. I think match/case functionality in Python 3.10 may make something like this easier in the future.

p.s. I only implemented lsusb and lsusb -v.

kellyjonbrazil commented 3 years ago

added in v1.17.1