Exemple #1
0
 def test_print_hid_descriptor(self):
     hid_desc = usb_descriptors.HidDescriptor()
     hid_desc.AddDescriptor(hid_constants.DescriptorType.REPORT, 0x80)
     hid_desc.AddDescriptor(hid_constants.DescriptorType.PHYSICAL, 0x60)
     string = str(hid_desc)
     self.assertIn('0x22', string)
     self.assertIn('0x23', string)
    def __init__(self,
                 report_desc,
                 features,
                 packet_size=64,
                 interval_ms=10,
                 interface_number=0,
                 interface_string=0,
                 in_endpoint=0x81,
                 out_endpoint=0x01):
        """Create a composite device feature implementing the HID protocol.

    Args:
      report_desc: HID report descriptor.
      features: Map between Report IDs and HidFeature objects to handle them.
      packet_size: Maximum interrupt packet size.
      interval_ms: Interrupt transfer interval in milliseconds.
      interface_number: Interface number for this feature (default 0).
      in_endpoint: Endpoint number for the IN endpoint (defualt 0x81).
      out_endpoint: Endpoint number for the OUT endpoint or None to disable
          the endpoint (default 0x01).

    Raises:
      ValueError: If any of the parameters are out of range.
    """
        fs_interface_desc = usb_descriptors.InterfaceDescriptor(
            bInterfaceNumber=interface_number,
            bInterfaceClass=usb_constants.DeviceClass.HID,
            bInterfaceSubClass=0,  # Non-bootable.
            bInterfaceProtocol=0,  # None.
            iInterface=interface_string,
        )

        hs_interface_desc = usb_descriptors.InterfaceDescriptor(
            bInterfaceNumber=interface_number,
            bInterfaceClass=usb_constants.DeviceClass.HID,
            bInterfaceSubClass=0,  # Non-bootable.
            bInterfaceProtocol=0,  # None.
            iInterface=interface_string,
        )

        hid_desc = usb_descriptors.HidDescriptor()
        hid_desc.AddDescriptor(hid_constants.DescriptorType.REPORT,
                               len(report_desc))
        fs_interface_desc.Add(hid_desc)
        hs_interface_desc.Add(hid_desc)

        fs_interval = math.ceil(math.log(interval_ms, 2)) + 1
        if fs_interval < 1 or fs_interval > 16:
            raise ValueError(
                'Full speed interval out of range: {} ({} ms)'.format(
                    fs_interval, interval_ms))

        fs_interface_desc.AddEndpoint(
            usb_descriptors.EndpointDescriptor(
                bEndpointAddress=in_endpoint,
                bmAttributes=usb_constants.TransferType.INTERRUPT,
                wMaxPacketSize=packet_size,
                bInterval=fs_interval))

        hs_interval = math.ceil(math.log(interval_ms, 2)) + 4
        if hs_interval < 1 or hs_interval > 16:
            raise ValueError(
                'High speed interval out of range: {} ({} ms)'.format(
                    hs_interval, interval_ms))

        hs_interface_desc.AddEndpoint(
            usb_descriptors.EndpointDescriptor(
                bEndpointAddress=in_endpoint,
                bmAttributes=usb_constants.TransferType.INTERRUPT,
                wMaxPacketSize=packet_size,
                bInterval=hs_interval))

        if out_endpoint is not None:
            fs_interface_desc.AddEndpoint(
                usb_descriptors.EndpointDescriptor(
                    bEndpointAddress=out_endpoint,
                    bmAttributes=usb_constants.TransferType.INTERRUPT,
                    wMaxPacketSize=packet_size,
                    bInterval=fs_interval))
            hs_interface_desc.AddEndpoint(
                usb_descriptors.EndpointDescriptor(
                    bEndpointAddress=out_endpoint,
                    bmAttributes=usb_constants.TransferType.INTERRUPT,
                    wMaxPacketSize=packet_size,
                    bInterval=hs_interval))

        super(HidCompositeFeature, self).__init__([fs_interface_desc],
                                                  [hs_interface_desc])
        self._report_desc = report_desc
        self._features = features
        self._interface_number = interface_number
        self._in_endpoint = in_endpoint
        self._out_endpoint = out_endpoint
Exemple #3
0
 def test_encode_hid_descriptor(self):
     hid_desc = usb_descriptors.HidDescriptor()
     hid_desc.AddDescriptor(hid_constants.DescriptorType.REPORT, 0x80)
     hid_desc.AddDescriptor(hid_constants.DescriptorType.PHYSICAL, 0x60)
     encoded_desc = '\x0C\x21\x11\x01\x00\x02\x22\x80\x00\x23\x60\x00'
     self.assertEquals(hid_desc.Encode(), encoded_desc)
    def __init__(self,
                 report_desc,
                 features,
                 vendor_id,
                 product_id,
                 packet_size=64,
                 interval_ms=10,
                 out_endpoint=True,
                 device_version=0x0100):
        """Create a HID gadget.

    Args:
      report_desc: HID report descriptor.
      features: Map between Report IDs and HidFeature objects to handle them.
      vendor_id: Device Vendor ID.
      product_id: Device Product ID.
      packet_size: Maximum interrupt packet size.
      interval_ms: Interrupt transfer interval in milliseconds.
      out_endpoint: Should this device have an interrupt OUT endpoint?
      device_version: Device version number.

    Raises:
      ValueError: If any of the parameters are out of range.
    """
        device_desc = usb_descriptors.DeviceDescriptor(
            idVendor=vendor_id,
            idProduct=product_id,
            bcdUSB=0x0200,
            iManufacturer=1,
            iProduct=2,
            iSerialNumber=3,
            bcdDevice=device_version)

        fs_config_desc = usb_descriptors.ConfigurationDescriptor(
            bmAttributes=0x80, MaxPower=50)
        fs_interface_desc = usb_descriptors.InterfaceDescriptor(
            bInterfaceNumber=0,
            bInterfaceClass=usb_constants.DeviceClass.HID,
            bInterfaceSubClass=0,  # Non-bootable.
            bInterfaceProtocol=0,  # None.
        )
        fs_config_desc.AddInterface(fs_interface_desc)

        hs_config_desc = usb_descriptors.ConfigurationDescriptor(
            bmAttributes=0x80, MaxPower=50)
        hs_interface_desc = usb_descriptors.InterfaceDescriptor(
            bInterfaceNumber=0,
            bInterfaceClass=usb_constants.DeviceClass.HID,
            bInterfaceSubClass=0,  # Non-bootable.
            bInterfaceProtocol=0,  # None.
        )
        hs_config_desc.AddInterface(hs_interface_desc)

        hid_desc = usb_descriptors.HidDescriptor()
        hid_desc.AddDescriptor(hid_constants.DescriptorType.REPORT,
                               len(report_desc))
        fs_interface_desc.Add(hid_desc)
        hs_interface_desc.Add(hid_desc)

        fs_interval = math.ceil(math.log(interval_ms, 2)) + 1
        if fs_interval < 1 or fs_interval > 16:
            raise ValueError(
                'Full speed interval out of range: {} ({} ms)'.format(
                    fs_interval, interval_ms))

        fs_interface_desc.AddEndpoint(
            usb_descriptors.EndpointDescriptor(
                bEndpointAddress=0x81,
                bmAttributes=usb_constants.TransferType.INTERRUPT,
                wMaxPacketSize=packet_size,
                bInterval=fs_interval))

        hs_interval = math.ceil(math.log(interval_ms, 2)) + 4
        if hs_interval < 1 or hs_interval > 16:
            raise ValueError(
                'High speed interval out of range: {} ({} ms)'.format(
                    hs_interval, interval_ms))

        hs_interface_desc.AddEndpoint(
            usb_descriptors.EndpointDescriptor(
                bEndpointAddress=0x81,
                bmAttributes=usb_constants.TransferType.INTERRUPT,
                wMaxPacketSize=packet_size,
                bInterval=hs_interval))

        if out_endpoint:
            fs_interface_desc.AddEndpoint(
                usb_descriptors.EndpointDescriptor(
                    bEndpointAddress=0x01,
                    bmAttributes=usb_constants.TransferType.INTERRUPT,
                    wMaxPacketSize=packet_size,
                    bInterval=fs_interval))
            hs_interface_desc.AddEndpoint(
                usb_descriptors.EndpointDescriptor(
                    bEndpointAddress=0x01,
                    bmAttributes=usb_constants.TransferType.INTERRUPT,
                    wMaxPacketSize=packet_size,
                    bInterval=hs_interval))

        super(HidGadget, self).__init__(device_desc, fs_config_desc,
                                        hs_config_desc)
        self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode()))
        self._report_desc = report_desc
        self._features = features