def __init__(self): """Create an echo gadget. """ device_desc = usb_descriptors.DeviceDescriptor( idVendor=usb_constants.VendorID.GOOGLE, idProduct=usb_constants.ProductID.GOOGLE_ECHO_GADGET, bcdUSB=0x0200, iManufacturer=1, iProduct=2, iSerialNumber=3, bcdDevice=0x0100) feature = EchoCompositeFeature( endpoints=[(0, 4, 0x81, 0x01), (1, 5, 0x82, 0x02), (2, 6, 0x83, 0x03)]) super(EchoGadget, self).__init__(device_desc, [feature]) self.AddStringDescriptor(1, 'Google Inc.') self.AddStringDescriptor(2, 'Echo Gadget') self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode())) self.AddStringDescriptor(4, 'Interrupt Echo') self.AddStringDescriptor(5, 'Bulk Echo') self.AddStringDescriptor(6, 'Isochronous Echo') # Enable Microsoft OS Descriptors for Windows 8 and above. self.EnableMicrosoftOSDescriptorsV1(vendor_code=0x01) # These are used to force Windows to load WINUSB.SYS for the echo functions. self.SetMicrosoftCompatId(0, 'WINUSB') self.SetMicrosoftCompatId(1, 'WINUSB') self.SetMicrosoftCompatId(2, 'WINUSB') self.AddDeviceCapabilityDescriptor(usb_descriptors.ContainerIdDescriptor( ContainerID=uuid.uuid4().bytes_le))
def __init__(self): device_desc = usb_descriptors.DeviceDescriptor( idVendor=usb_constants.VendorID.GOOGLE, idProduct=usb_constants.ProductID.GOOGLE_TEST_GADGET, bcdUSB=0x0200, iManufacturer=1, iProduct=2, iSerialNumber=3, bcdDevice=0x0100) fs_config_desc = usb_descriptors.ConfigurationDescriptor( bmAttributes=0x80, MaxPower=50) hs_config_desc = usb_descriptors.ConfigurationDescriptor( bmAttributes=0x80, MaxPower=50) interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=0) fs_config_desc.AddInterface(interface_desc) hs_config_desc.AddInterface(interface_desc) super(DefaultGadget, self).__init__(device_desc, fs_config_desc, hs_config_desc) self.AddStringDescriptor(1, "Google Inc.") self.AddStringDescriptor(2, "Test Gadget (default state)")
def test_device_descriptor(self): device_desc = usb_descriptors.DeviceDescriptor(idVendor=0xDEAD, idProduct=0xBEEF, bcdDevice=0x0100, bNumConfigurations=1) self.assertEquals( device_desc.Encode(), '\x12\x01\x00\x02\x00\x00\x00\x40\xAD\xDE\xEF\xBE\x00\x01\x00\x00\x00' '\x01')
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) if out_endpoint: out_endpoint = 0x01 else: out_endpoint = None self._hid_feature = HidCompositeFeature(report_desc=report_desc, features=features, packet_size=packet_size, interval_ms=interval_ms, out_endpoint=out_endpoint) super(HidGadget, self).__init__(device_desc, [self._hid_feature]) self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode()))
def __init__(self): device_desc = usb_descriptors.DeviceDescriptor( idVendor=usb_constants.VendorID.GOOGLE, idProduct=usb_constants.ProductID.GOOGLE_COMPOSITE_ECHO_GADGET, bcdUSB=0x0210, # USB 2.1 to indicate support for BOS descriptors. iManufacturer=1, iProduct=2, iSerialNumber=3, bcdDevice=0x0100) echo_feature = echo_gadget.EchoCompositeFeature( endpoints=[(0, 5, 0x81, 0x01), (1, 6, 0x82, 0x02), (2, 7, 0x83, 0x03)]) hid_echo_feature = hid_echo_gadget.EchoFeature() hid_feature = hid_gadget.HidCompositeFeature( report_desc=hid_echo_gadget.EchoFeature.REPORT_DESC, features={0: hid_echo_feature}, interface_number=3, interface_string=4, in_endpoint=0x84, out_endpoint=0x04) super(CompositeEchoGadget, self).__init__(device_desc, [echo_feature, hid_feature]) self.AddStringDescriptor(1, 'Google Inc.') self.AddStringDescriptor(2, 'Echo Gadget') self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode())) self.AddStringDescriptor(4, 'HID Echo') self.AddStringDescriptor(5, 'Interrupt Echo') self.AddStringDescriptor(6, 'Bulk Echo') self.AddStringDescriptor(7, 'Isochronous Echo') # Enable Microsoft OS 2.0 Descriptors for Windows 8.1 and above. self.EnableMicrosoftOSDescriptorsV2(vendor_code=0x02) # These are used to force Windows to load WINUSB.SYS for the echo functions. self.SetMicrosoftCompatId(0, 'WINUSB') self.SetMicrosoftCompatId(1, 'WINUSB') self.SetMicrosoftCompatId(2, 'WINUSB')
def __init__(self): """Create an echo gadget. """ device_desc = usb_descriptors.DeviceDescriptor( idVendor=usb_constants.VendorID.GOOGLE, idProduct=usb_constants.ProductID.GOOGLE_ECHO_GADGET, bcdUSB=0x0200, iManufacturer=1, iProduct=2, iSerialNumber=3, bcdDevice=0x0100) fs_config_desc = usb_descriptors.ConfigurationDescriptor( bmAttributes=0x80, MaxPower=50) fs_intr_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=0, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=4, ) fs_intr_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x01, bmAttributes=usb_constants.TransferType.INTERRUPT, wMaxPacketSize=64, bInterval=1 # 1ms )) fs_intr_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x81, bmAttributes=usb_constants.TransferType.INTERRUPT, wMaxPacketSize=64, bInterval=1 # 1ms )) fs_config_desc.AddInterface(fs_intr_interface_desc) fs_bulk_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=1, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=5) fs_bulk_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x02, bmAttributes=usb_constants.TransferType.BULK, wMaxPacketSize=64, bInterval=0)) fs_bulk_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x82, bmAttributes=usb_constants.TransferType.BULK, wMaxPacketSize=64, bInterval=0)) fs_config_desc.AddInterface(fs_bulk_interface_desc) fs_config_desc.AddInterface( usb_descriptors.InterfaceDescriptor( bInterfaceNumber=2, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=6)) fs_isoc_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=2, bAlternateSetting=1, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=6) fs_isoc_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x03, bmAttributes=usb_constants.TransferType.ISOCHRONOUS, wMaxPacketSize=1023, bInterval=1 # 1ms )) fs_isoc_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x83, bmAttributes=usb_constants.TransferType.ISOCHRONOUS, wMaxPacketSize=1023, bInterval=1 # 1ms )) fs_config_desc.AddInterface(fs_isoc_interface_desc) hs_config_desc = usb_descriptors.ConfigurationDescriptor( bmAttributes=0x80, MaxPower=50) hs_intr_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=0, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=4) hs_intr_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x01, bmAttributes=usb_constants.TransferType.INTERRUPT, wMaxPacketSize=64, bInterval=4 # 1ms )) hs_intr_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x81, bmAttributes=usb_constants.TransferType.INTERRUPT, wMaxPacketSize=64, bInterval=4 # 1ms )) hs_config_desc.AddInterface(hs_intr_interface_desc) hs_bulk_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=1, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=5) hs_bulk_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x02, bmAttributes=usb_constants.TransferType.BULK, wMaxPacketSize=512, bInterval=0)) hs_bulk_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x82, bmAttributes=usb_constants.TransferType.BULK, wMaxPacketSize=512, bInterval=0)) hs_config_desc.AddInterface(hs_bulk_interface_desc) hs_config_desc.AddInterface( usb_descriptors.InterfaceDescriptor( bInterfaceNumber=2, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=6)) hs_isoc_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=2, bAlternateSetting=1, bInterfaceClass=usb_constants.DeviceClass.VENDOR, bInterfaceSubClass=0, bInterfaceProtocol=0, iInterface=6) hs_isoc_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x03, bmAttributes=usb_constants.TransferType.ISOCHRONOUS, wMaxPacketSize=1024, bInterval=4 # 1ms )) hs_isoc_interface_desc.AddEndpoint( usb_descriptors.EndpointDescriptor( bEndpointAddress=0x83, bmAttributes=usb_constants.TransferType.ISOCHRONOUS, wMaxPacketSize=1024, bInterval=4 # 1ms )) hs_config_desc.AddInterface(hs_isoc_interface_desc) super(EchoGadget, self).__init__(device_desc, fs_config_desc, hs_config_desc) self.AddStringDescriptor(1, 'Google Inc.') self.AddStringDescriptor(2, 'Echo Gadget') self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode())) self.AddStringDescriptor(4, 'Interrupt Echo') self.AddStringDescriptor(5, 'Bulk Echo') self.AddStringDescriptor(6, 'Isochronous Echo')
import unittest import uuid import mock import gadget import usb_constants import usb_descriptors device_desc = usb_descriptors.DeviceDescriptor( idVendor=0x18D1, # Google Inc. idProduct=0xFF00, bcdUSB=0x0200, iManufacturer=1, iProduct=2, iSerialNumber=3, bNumConfigurations=1, bcdDevice=0x0100) fs_config_desc = usb_descriptors.ConfigurationDescriptor( bmAttributes=0xC0, MaxPower=50) fs_interface_desc = usb_descriptors.InterfaceDescriptor( bInterfaceNumber=0 ) fs_config_desc.AddInterface(fs_interface_desc) fs_bulk_in_endpoint_desc = usb_descriptors.EndpointDescriptor(
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