Ejemplo n.º 1
0
    def __init__(self, gadget, report_desc):

        self.gadget = gadget
        self.report_desc = report_desc

        self._report_requested = False

        descriptors = [
            functionfs.getDescriptor(
                functionfs.USBInterfaceDescriptor,
                bInterfaceNumber=0,
                bAlternateSetting=0,
                bNumEndpoints=2,
                bInterfaceClass=functionfs.ch9.USB_CLASS_HID,
                bInterfaceSubClass=0,
                bInterfaceProtocol=0,
                iInterface=1,
            ),
            functionfs.getDescriptor(
                USBHIDDescriptor,
                bcdHID=0x0111,
                bCountryCode=0,
                bNumDescriptors=1,
                bDescriptorxType=34,
                wDescriptorLength=len(self.report_desc),
            ),
            functionfs.getDescriptor(
                functionfs.USBEndpointDescriptorNoAudio,
                bEndpointAddress=1 | functionfs.ch9.USB_DIR_IN,
                bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
                wMaxPacketSize=64,
                bInterval=5,
            ),
            functionfs.getDescriptor(
                functionfs.USBEndpointDescriptorNoAudio,
                bEndpointAddress=2 | functionfs.ch9.USB_DIR_OUT,
                bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
                wMaxPacketSize=64,
                bInterval=5,
            )
        ]

        super().__init__(
            self.gadget.mount_point,
            fs_list=descriptors,
            hs_list=descriptors,
            lang_dict={
                0x0409: [
                    u'HID Interface',
                ],
            },
        )

        # set ep0 to non-blocking
        orig_fl = fcntl.fcntl(self.ep0, fcntl.F_GETFL)
        fcntl.fcntl(self.ep0, fcntl.F_SETFL, orig_fl | os.O_NONBLOCK)
Ejemplo n.º 2
0
 def __init__(self, path, ep_pair_count=15):
     ep_list = sum(
         [[
             x | functionfs.ch9.USB_DIR_IN,
             x | functionfs.ch9.USB_DIR_OUT,
         ] for x in range(1, ep_pair_count + 1)],
         [],
     )
     INTERFACE_DESCRIPTOR = functionfs.getDescriptor(
         functionfs.USBInterfaceDescriptor,
         bInterfaceNumber=0,
         bAlternateSetting=0,
         bNumEndpoints=len(ep_list),
         bInterfaceClass=functionfs.ch9.USB_CLASS_VENDOR_SPEC,
         bInterfaceSubClass=0,
         bInterfaceProtocol=0,
         iInterface=1,
     )
     fs_list = [INTERFACE_DESCRIPTOR]
     hs_list = [INTERFACE_DESCRIPTOR]
     for endpoint in ep_list:
         fs_list.append(
             functionfs.getDescriptor(
                 functionfs.USBEndpointDescriptorNoAudio,
                 bEndpointAddress=endpoint,
                 bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                 wMaxPacketSize=FS_BULK_MAX_PACKET_SIZE,
                 bInterval=0,
             ))
         hs_list.append(
             functionfs.getDescriptor(
                 functionfs.USBEndpointDescriptorNoAudio,
                 bEndpointAddress=endpoint,
                 bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                 wMaxPacketSize=HS_BULK_MAX_PACKET_SIZE,
                 bInterval=0,
             ))
     super(FunctionFSTestDevice, self).__init__(
         path,
         fs_list=fs_list,
         hs_list=hs_list,
         #            ss_list=DESC_LIST,
         lang_dict={
             0x0409: [
                 common.INTERFACE_NAME,
             ],
         },
     )
     self.__ep_count = len(ep_list)
     self.__echo_payload = 'NOT SET'
Ejemplo n.º 3
0
 def __init__(self, path):
     ep_list = sum(
         [[
             x | functionfs.ch9.USB_DIR_IN,
             x | functionfs.ch9.USB_DIR_OUT,
         ] for x in range(1, 16)],
         [],
     )
     # Try allocating 15 endpoint pairs, then one EP less until success.
     # Note: crude, will miss maximum EP count if there is more than one EP
     # available in a direction than EPs available in the other.
     while ep_list:
         INTERFACE_DESCRIPTOR = functionfs.getDescriptor(
             functionfs.USBInterfaceDescriptor,
             bInterfaceNumber=0,
             bAlternateSetting=0,
             bNumEndpoints=len(ep_list),
             bInterfaceClass=functionfs.ch9.USB_CLASS_VENDOR_SPEC,
             bInterfaceSubClass=0,
             bInterfaceProtocol=0,
             iInterface=1,
         )
         fs_list = [INTERFACE_DESCRIPTOR]
         hs_list = [INTERFACE_DESCRIPTOR]
         for endpoint in ep_list:
             fs_list.append(
                 functionfs.getDescriptor(
                     functionfs.USBEndpointDescriptorNoAudio,
                     bEndpointAddress=endpoint,
                     bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                     wMaxPacketSize=FS_BULK_MAX_PACKET_SIZE,
                     bInterval=0,
                 ))
             hs_list.append(
                 functionfs.getDescriptor(
                     functionfs.USBEndpointDescriptorNoAudio,
                     bEndpointAddress=endpoint,
                     bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                     wMaxPacketSize=HS_BULK_MAX_PACKET_SIZE,
                     bInterval=0,
                 ))
         try:
             super(FunctionFSTestDevice, self).__init__(
                 path,
                 fs_list=fs_list,
                 hs_list=hs_list,
                 #                    ss_list=DESC_LIST,
                 lang_dict={
                     0x0409: [
                         common.INTERFACE_NAME,
                     ],
                 },
             )
         except IOError as exc:
             if exc.errno != errno.EINVAL:
                 raise
             ep_list.pop()
         else:
             print('Succeeded with', len(ep_list), 'endpoints')
             break
     if not ep_list:
         # pylint: disable=misplaced-bare-raise
         raise
         # pylint: enable=misplaced-bare-raise
     self.__echo_payload = 'NOT SET'
     assert len(self._ep_list) == len(ep_list) + 1
     thread_list = self.__thread_list = []
     for ep_file in self._ep_list[1:]:
         thread_list.append(
             EPThread(
                 name=ep_file.name,
                 ep_file=ep_file,
                 method=getattr(
                     ep_file,
                     'readinto' if ep_file.readable() else 'write'),
             ))
Ejemplo n.º 4
0
    def __init__(self, path, args):
        # TODO: endpoints aren't unique to interfaces (each ep shouldn't require a unique address)
        # TODO: functionfs doesn't support interfaces with 0 endpoints (required to function? unknown)
        self.max_packet_size = 0x0020  # 32 bytes

        iface0 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=0,
            bAlternateSetting=0,
            bNumEndpoints=2,
            bInterfaceClass=255,  #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93,  #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=1,
            iInterface=0,
        )
        fs_list = [iface0]
        hs_list = [iface0]

        if0ep1 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=1 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=4,
        )
        if0ep2 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=2 | functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=8,
        )

        fs_list += [if0ep1, if0ep2]
        hs_list += [if0ep1, if0ep2]

        iface1 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=1,
            bAlternateSetting=0,
            bNumEndpoints=4,
            bInterfaceClass=255,  #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93,  #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=3,
            iInterface=0,
        )
        if1ep1 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=3 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=2,
        )
        if1ep2 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=4 | functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=4,
        )
        if1ep3 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=5 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=64,
        )
        if1ep4 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=6 | functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=16,
        )

        fs_list += [iface1, if1ep1, if1ep2, if1ep3, if1ep4]
        hs_list += [iface1, if1ep1, if1ep2, if1ep3, if1ep4]

        iface2 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=2,
            bAlternateSetting=0,
            bNumEndpoints=1,
            bInterfaceClass=255,  #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93,  #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=2,
            iInterface=0,
        )
        if2ep1 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=7 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=16,
        )

        fs_list += [iface2, if2ep1]
        hs_list += [iface2, if2ep1]

        iface3 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=3,
            bAlternateSetting=0,
            bNumEndpoints=0,
            bInterfaceClass=255,  #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=253,  #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=19,
            iInterface=4,
            # UNRECOGNIZED:  06 41 00 01 01 03
        )

        #INT_DESCRIPTOR = functionfs.getDescriptor(
        #functionfs.USBEndpointDescriptorNoAudio,
        #bEndpointAddress=2|functionfs.ch9.USB_DIR_IN,
        #bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
        #wMaxPacketSize=28,
        #bInterval=6,
        #)

        #hs_list.append(iface3)
        #fs_list.append(iface3)

        self.loop = asyncio.get_event_loop()
        self.loop.set_exception_handler(self.exception)

        try:
            # If anything goes wrong from here on we MUST not
            # hold any file descriptors open, else there is a
            # good chance the kernel will deadlock.
            super(XFunction, self).__init__(
                path,
                fs_list=fs_list,
                hs_list=hs_list,
                lang_dict={
                    0x0409: [
                        u'MTP',
                    ],
                },
            )

            self.loop.add_reader(self.ep0, self.processEvents)

            print(len(self._ep_list))
            # assert len(self._ep_list) == 4

            print(self._ep_list)
            self.cmdep = KAIOWriter(self._ep_list[1])
            #self.inep = self._ep_list[1]
            self.outep = KAIOReader(self._ep_list[2])
            #self.intep = KAIOWriter(self._ep_list[3])

            self.outep.maxpkt = 512
            #self.inep.maxpkt = 512

            #self.responder = MTPResponder(
            #outep=self.outep,
            #inep=self.inep,
            #intep=self.intep,
            #loop=self.loop,
            #args=args
            #)

        except:
            # Catch ANY exception, close all file descriptors
            # and then re-raise.
            self.close()
            raise
Ejemplo n.º 5
0
    def __init__(self, udc, device_params, device_strings, report_desc):

        self.udc = udc

        self.report_desc = report_desc
        self.device_strings = device_strings
        self._report_requested = False

        self.devdes = functionfs.getDescriptor(
            functionfs.ch9.USBDeviceDescriptor,
            **{k: int(v, 16) for k, v in device_params.items()},
            bMaxPacketSize0=255,
            iManufacturer=1,
            iProduct=2,
            iSerialNumber=0,
            bNumConfigurations=1,
        )

        self.intdes = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=0,
            bAlternateSetting=0,
            bNumEndpoints=2,
            bInterfaceClass=functionfs.ch9.USB_CLASS_HID,
            bInterfaceSubClass=0,
            bInterfaceProtocol=0,
            iInterface=0,
        )

        self.hiddes = functionfs.getDescriptor(
            USBHIDDescriptor,
            bcdHID=0x0111,
            bCountryCode=0,
            bNumDescriptors=1,
            bDescriptorxType=34,
            wDescriptorLength=len(self.report_desc),
        )

        self.ep1des = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=1 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
            wMaxPacketSize=64,
            bInterval=5,
        )

        self.ep2des = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=2 | functionfs.ch9.USB_DIR_OUT,
            bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
            wMaxPacketSize=64,
            bInterval=5,
        )

        self.confdes = functionfs.getDescriptor(
            functionfs.ch9.USBConfigDescriptor,
            wTotalLength=0x29,
            bNumInterfaces=1,
            bConfigurationValue=1,
            iConfiguration=0,
            bmAttributes=functionfs.ch9.USB_CONFIG_ATT_ONE,
            bMaxPower=125,
        )

        self._path = '/dev/gadget'
        ep0 = functionfs.Endpoint0File(os.path.join(self._path, udc), 'r+')
        self._ep_list = [ep0]
        self._ep_address_dict = {}


        descs = b''.join(bytes(x) for x in [
            b'\x00\x00\x00\x00',
            self.confdes,
            self.intdes,
            self.hiddes,
            self.ep1des,
            self.ep2des,
            self.confdes,
            self.intdes,
            self.hiddes,
            self.ep1des,
            self.ep2des,
            self.devdes,
        ])

        self.ep0.write(descs)
        logger.warning('go')
        # set ep0 to non-blocking
        orig_fl = fcntl.fcntl(self.ep0, fcntl.F_GETFL)
        fcntl.fcntl(self.ep0, fcntl.F_SETFL, orig_fl | os.O_NONBLOCK)
Ejemplo n.º 6
0
    def __init__(self, path, args):
        # TODO: endpoints aren't unique to interfaces (each ep shouldn't require a unique address)
        # TODO: functionfs doesn't support interfaces with 0 endpoints (required to function? unknown)
        self.max_packet_size = 0x0020 # 32 bytes
        iface0 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=0,
            bAlternateSetting=0,
            bNumEndpoints=2,
            bInterfaceClass=255, #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93, #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=1,
            iInterface=0,
        )
        fs_list = [iface0]
        hs_list = [iface0]

        if0ep1 =  functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=1|functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=4,
        )
        if0ep2 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=2|functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=8,
        )

        fs_list += [if0ep1,if0ep2]
        hs_list += [if0ep1,if0ep2]


        iface1 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=1,
            bAlternateSetting=0,
            bNumEndpoints=4,
            bInterfaceClass=255, #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93, #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=3,
            iInterface=0,
        )
        if1ep1 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=3|functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=2,
        )
        if1ep2 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=4|functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=4,
        )
        if1ep3 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=5|functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=64,
        )
        if1ep4 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=6|functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=16,
        )

        fs_list += [iface1,if1ep1,if1ep2,if1ep3,if1ep4]
        hs_list += [iface1,if1ep1,if1ep2,if1ep3,if1ep4]

        iface2 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=2,
            bAlternateSetting=0,
            bNumEndpoints=1,
            bInterfaceClass=255, #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93, #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=2,
            iInterface=0,
        )
        if2ep1 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=7|functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,#functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=16,
        )

        fs_list += [iface2,if2ep1]
        hs_list += [iface2,if2ep1]


        iface3 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=3,
            bAlternateSetting=0,
            bNumEndpoints=0,
            bInterfaceClass=255, #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=253, #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=19,
            iInterface=4,
            # UNRECOGNIZED:  06 41 00 01 01 03
        )
        
        import ctypes
        def barray(data):
            BArray = ctypes.c_ubyte*(len(data))
            return BArray(*data)
        osc = functionfs.OSExtCompatDesc( # hid interface
            bFirstInterfaceNumber=0,
            Reserved1=1,
            CompatibleID=barray([0,0,0,0,0,0,0,0]),
            SubCompatibleID=barray([0,0,0,0,0,0,0,0]),
            Reserved2=barray([0,0,0,0,0,0])
        )
        osp1 = functionfs.getOSExtPropDesc(
                0x00000002, #unicode str w/ env variables
                'Icons'.encode('utf8'),
                '%SystemRoot%\system32\shell32.dll,-233'.encode('utf8'),
        )
        osp2 = functionfs.getOSExtPropDesc(
                0x00000001, # unicode str
                'Label'.encode('utf8'),
                'XYZ Device'.encode('utf8'),
        )
        ospd1 = functionfs.getOSDesc(1, [osp1])
        ospd2 = functionfs.getOSDesc(1, [osp2])
        oscd =  functionfs.getOSDesc(1, [osc])
        os_list = [oscd,ospd1,ospd2]

        #INT_DESCRIPTOR = functionfs.getDescriptor(
            #functionfs.USBEndpointDescriptorNoAudio,
            #bEndpointAddress=2|functionfs.ch9.USB_DIR_IN,
            #bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
            #wMaxPacketSize=28,
            #bInterval=6,
        #)

        #hs_list.append(iface3)
        #fs_list.append(iface3)

        print('building')
        self.loop = asyncio.get_event_loop()
        self.loop.set_exception_handler(self.exception)

        try:
            # If anything goes wrong from here on we MUST not
            # hold any file descriptors open, else there is a
            # good chance the kernel will deadlock.
            super(XFunction, self).__init__(
                path,
                fs_list=fs_list,
                hs_list=hs_list,
                os_list=os_list,
                lang_dict={
                    0x0409: [
                        u'MTP',
                    ],
                },
            )

            self.loop.add_reader(self.ep0, self.processEvents)

            print(len(self._ep_list))
            # assert len(self._ep_list) == 4

            self.inep = self._ep_list[1]
            self.outep = KAIOReader(self._ep_list[2])
            self.intep = KAIOWriter(self._ep_list[3])

            self.outep.maxpkt = 512
            self.inep.maxpkt = 512

            self.responder = MTPResponder(
                outep=self.outep,
                inep=self.inep,
                intep=self.intep,
                loop=self.loop,
                args=args
            )

        except:
            # Catch ANY exception, close all file descriptors
            # and then re-raise.
            self.close()
            raise
Ejemplo n.º 7
0
    def __init__(self, path, args):
        # TODO: endpoints aren't unique to interfaces (each ep shouldn't require a unique address)
        # TODO: functionfs doesn't support interfaces with 0 endpoints (required to function? unknown)
        self.max_packet_size = 0x0040  # 64 bytes

        #self._aio_context = libaio.AIOContext(
        #    PENDING_READ_COUNT + MAX_PENDING_WRITE_COUNT,
        #)
        #self.eventfd = eventfd = libaio.EventFD()
        iface0 = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=0,
            bAlternateSetting=0,
            bNumEndpoints=2,
            bInterfaceClass=255,  #functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=93,  #functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=1,
            iInterface=0,
        )
        fs_list = [iface0]
        hs_list = [iface0]

        if0ep1 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=1 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=4,
        )
        if0ep2 = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=2 | functionfs.ch9.USB_DIR_OUT,
            bmAttributes=3,  #functionfs.ch9.USB_ENDPOINT_XFER_BULK,
            wMaxPacketSize=self.max_packet_size,
            bInterval=8,
        )

        import ctypes

        def barray(data):
            BArray = ctypes.c_ubyte * (len(data))
            return BArray(*data)

        osc = functionfs.OSExtCompatDesc(  # hid interface
            bFirstInterfaceNumber=0,
            Reserved1=1,
            CompatibleID=barray('XUSB10'.encode('utf8') + bytes([0, 0])),
            SubCompatibleID=barray([0, 0, 0, 0, 0, 0, 0, 0]),
            Reserved2=barray([0, 0, 0, 0, 0, 0]))
        osp1 = functionfs.getOSExtPropDesc(
            0x00000002,  #unicode str w/ env variables
            'Icons'.encode('utf8'),
            '%SystemRoot%\system32\shell32.dll,-233'.encode('utf8'),
        )
        osp2 = functionfs.getOSExtPropDesc(
            0x00000001,  # unicode str
            'Label'.encode('utf8'),
            'XYZ Device'.encode('utf8'),
        )
        ospd1 = functionfs.getOSDesc(0, [osp1])
        ospd2 = functionfs.getOSDesc(0, [osp2])
        oscd = functionfs.getOSDesc(0, [osc])
        os_list = [oscd]  #,ospd1,ospd2]

        fs_list += [if0ep1, if0ep2]
        hs_list += [if0ep1, if0ep2]

        self.loop = asyncio.get_event_loop()
        self.loop.set_exception_handler(self.exception)

        try:
            # If anything goes wrong from here on we MUST not
            # hold any file descriptors open, else there is a
            # good chance the kernel will deadlock.
            super(XFunction, self).__init__(
                path,
                fs_list=fs_list,
                hs_list=hs_list,
                os_list=os_list,
                lang_dict={
                    0x0409: [
                        u'English',
                    ],
                },
            )

            self.loop.add_reader(self.ep0, self.processEventsForever)

            print(len(self._ep_list))
            # assert len(self._ep_list) == 4

            self.inep = self._ep_list[1]
            self.cmdep = KAIOWriter(self._ep_list[1])
            self.outep = KAIOReader(self._ep_list[2])
            self.intep = KAIOWriter(self._ep_list[2])

            self.outep.maxpkt = 512
            self.inep.maxpkt = 512

            self.responder = MTPResponder(outep=self.outep,
                                          inep=self.inep,
                                          intep=self.intep,
                                          loop=self.loop,
                                          args=args)

        except:
            # Catch ANY exception, close all file descriptors
            # and then re-raise.
            self.close()
            raise
Ejemplo n.º 8
0
    def __init__(self, path, args):

        INTERFACE_DESCRIPTOR = functionfs.getDescriptor(
            functionfs.USBInterfaceDescriptor,
            bInterfaceNumber=0,
            bAlternateSetting=0,
            bNumEndpoints=3,
            bInterfaceClass=functionfs.ch9.USB_CLASS_VENDOR_SPEC,
            bInterfaceSubClass=functionfs.ch9.USB_SUBCLASS_VENDOR_SPEC,
            bInterfaceProtocol=0,
            iInterface=1,
        )
        fs_list = [INTERFACE_DESCRIPTOR]
        hs_list = [INTERFACE_DESCRIPTOR]
        fs_list.append(
            functionfs.getDescriptor(
                functionfs.USBEndpointDescriptorNoAudio,
                bEndpointAddress=1 | functionfs.ch9.USB_DIR_IN,
                bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                wMaxPacketSize=FS_BULK_MAX_PACKET_SIZE,
                bInterval=0,
            ))
        fs_list.append(
            functionfs.getDescriptor(
                functionfs.USBEndpointDescriptorNoAudio,
                bEndpointAddress=2 | functionfs.ch9.USB_DIR_OUT,
                bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                wMaxPacketSize=FS_BULK_MAX_PACKET_SIZE,
                bInterval=0,
            ))
        hs_list.append(
            functionfs.getDescriptor(
                functionfs.USBEndpointDescriptorNoAudio,
                bEndpointAddress=1 | functionfs.ch9.USB_DIR_IN,
                bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                wMaxPacketSize=HS_BULK_MAX_PACKET_SIZE,
                bInterval=0,
            ))
        hs_list.append(
            functionfs.getDescriptor(
                functionfs.USBEndpointDescriptorNoAudio,
                bEndpointAddress=2 | functionfs.ch9.USB_DIR_OUT,
                bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_BULK,
                wMaxPacketSize=HS_BULK_MAX_PACKET_SIZE,
                bInterval=0,
            ))

        INT_DESCRIPTOR = functionfs.getDescriptor(
            functionfs.USBEndpointDescriptorNoAudio,
            bEndpointAddress=2 | functionfs.ch9.USB_DIR_IN,
            bmAttributes=functionfs.ch9.USB_ENDPOINT_XFER_INT,
            wMaxPacketSize=28,
            bInterval=6,
        )

        hs_list.append(INT_DESCRIPTOR)
        fs_list.append(INT_DESCRIPTOR)

        self.loop = asyncio.get_event_loop()
        self.loop.set_exception_handler(self.exception)

        try:
            # If anything goes wrong from here on we MUST not
            # hold any file descriptors open, else there is a
            # good chance the kernel will deadlock.
            super(MTPFunction, self).__init__(
                path,
                fs_list=fs_list,
                hs_list=hs_list,
                lang_dict={
                    0x0409: [
                        u'MTP',
                    ],
                },
            )

            self.loop.add_reader(self.ep0, self.processEvents)

            assert len(self._ep_list) == 4

            self.inep = self._ep_list[1]
            self.outep = KAIOReader(self._ep_list[2])
            self.intep = KAIOWriter(self._ep_list[3])

            self.outep.maxpkt = 512
            self.inep.maxpkt = 512

            self.responder = MTPResponder(outep=self.outep,
                                          inep=self.inep,
                                          intep=self.intep,
                                          loop=self.loop,
                                          args=args)

        except:
            # Catch ANY exception, close all file descriptors
            # and then re-raise.
            self.close()
            raise