예제 #1
0
파일: usbdevice.py 프로젝트: qilinkang/silk
def device_find_from_serial(device_type, serial, interface_number):
    devname = None

    devices = usbinfo.usbinfo()
    for device in devices:
        # Match the device serial number and interface number
        # from the config file.  This is wrapped in a try statement
        # because the interface number is sometimes returned as an
        # empty string.
        device_serial = device_get_serial(device)
        # print device_serial, serial
        if device_serial == serial:
            try:
                device_interface_number = device_get_interface_number(device)
                # print device_interface_number, interface_number
                if device_interface_number != interface_number:
                    continue
            except:
                continue

            devname = device_get_devname(device)
            if devname:
                break

    return devname, device
예제 #2
0
파일: __main__.py 프로젝트: yazici/usbinfo
def endpoints_total():
    """Return the total number of endpoints used.

    Returns:
        The total number of endpoints used
    """
    devices = usbinfo.usbinfo()
    return sum(int(dev['bNumEndpoints']) for dev in devices)
예제 #3
0
파일: usbdevice.py 프로젝트: qilinkang/silk
def get_all_connected_serial_devices():

    device_dict = collections.defaultdict(set)
    device_dict['Wpantund'] = {}

    devices = usbinfo.usbinfo()

    for device in devices:
        device_name = device['iProduct']

        if device_name in DEVICE_KNOWN_LIST:
            device_dict[device_name].add(device['iSerialNumber'])

    return device_dict
예제 #4
0
파일: smoke_test.py 프로젝트: zr000/usbinfo
    def test_usbinfo(self):
        """Tests that module can be imported and that ``usbinfo()`` can
        be executed."""
        attrs = [
            'idVendor',
            'idProduct',
            'iManufacturer',
            'iProduct',
            'iSerialNumber',
            'bInterfaceNumber',
            'devname',
        ]

        try:
            # Check that we can import
            import usbinfo

            # Check that we can execute usbinfo()
            results = usbinfo.usbinfo()
        except Exception as e:
            for line in e.message.split('\n'):
                logging.error(line)
            self.fail('Caught exception of type {0}'.format(type(e)))

        # Check that the result is a list
        self.assertTrue(isinstance(results, list))

        # Check each device
        for device in results:

            # Check that this device is a dictionary type
            self.assertTrue(isinstance(device, dict),
                            'Device is not a dict instance')

            # Check for each compulsory attribute
            for attr in attrs:
                self.assertTrue(attr in device,
                                'Device missing attribute %s' % attr)
                # basestring throw Exception NameError in python3
                try:
                    isinstance('', basestring)
                    self.assertTrue(isinstance(device.get(attr), basestring),
                                    'Attribute %s is not a basestring' % attr)
                except NameError:
                    self.assertTrue(isinstance(device.get(attr), str),
                                    'Attribute %s is not a basestring' % attr)
예제 #5
0
    def test_usbinfo(self):
        """Tests that module can be imported and that ``usbinfo()`` can
        be executed."""
        attrs = [
            'idVendor',
            'idProduct',
            'iManufacturer',
            'iProduct',
            'iSerialNumber',
            'bInterfaceNumber',
            'devname',
        ]

        try:
            # Check that we can import
            import usbinfo

            # Check that we can execute usbinfo()
            results = usbinfo.usbinfo()
        except Exception as e:
            for line in e.message.split('\n'):
                logging.error(line)
            self.fail('Caught exception of type {0}'.format(type(e)))

        # Check that the result is a list
        self.assertTrue(isinstance(results, list))

        # Check each device
        for device in results:

            # Check that this device is a dictionary type
            self.assertTrue(isinstance(device, dict),
                            'Device is not a dict instance')

            # Check for each compulsory attribute
            for attr in attrs:
                self.assertTrue(attr in device,
                                'Device missing attribute %s' % attr)
                # basestring throw Exception NameError in python3
                try:
                    isinstance('', basestring)
                    self.assertTrue(isinstance(device.get(attr), basestring),
                                    'Attribute %s is not a basestring' % attr)
                except NameError:
                    self.assertTrue(isinstance(device.get(attr), str),
                                    'Attribute %s is not a basestring' % attr)
예제 #6
0
def print_standard(endpoints=False):
    """Print standard usbinfo results.

    :param bool endpoints:
        If True, print the number of endpoints.
    """
    devices = usbinfo.usbinfo()
    header = _print_header(endpoints)

    devices.insert(0, header)

    manufacturer_width = \
            _max_width(dev['iManufacturer'] for dev in devices)
    product_width = \
            _max_width(dev['iProduct'] for dev in devices)
    device_serial_width = \
            _max_width(dev['iSerialNumber']for dev in devices)

    header_format = u'{vid}:{pid} {manufacturer} {product} {serno} {ifcno}'

    if endpoints:
        header_format += u' {epnum}'

    header_format += u' {devname}'

    for dev in devices:
        values = dict(
            vid=dev['idVendor'].rjust(4),
            pid=dev['idProduct'].ljust(4),
            manufacturer=dev['iManufacturer'].ljust(manufacturer_width),
            product=dev['iProduct'].ljust(product_width),
            serno=dev['iSerialNumber'].ljust(device_serial_width),
            ifcno=dev['bInterfaceNumber'].ljust(3),
            devname=dev['devname']
        )

        if endpoints:
            values['epnum'] = dev['bNumEndpoints'].ljust(3)

        line = header_format.format(**values)

        if 'mount' in dev:
            line += ' => {mount}'.format(mount=dev['mount'])

        print line
예제 #7
0
파일: __main__.py 프로젝트: zr000/usbinfo
def print_standard(endpoints=False):
    """Print standard usbinfo results.

    :param bool endpoints:
        If True, print the number of endpoints.
    """
    devices = usbinfo.usbinfo()
    header = _print_header(endpoints)

    devices.insert(0, header)

    manufacturer_width = \
            _max_width(dev['iManufacturer'] for dev in devices)
    product_width = \
            _max_width(dev['iProduct'] for dev in devices)
    device_serial_width = \
            _max_width(dev['iSerialNumber']for dev in devices)

    header_format = u'{vid}:{pid} {manufacturer} {product} {serno} {ifcno}'

    if endpoints:
        header_format += u' {epnum}'

    header_format += u' {devname}'

    for dev in devices:
        values = dict(
            vid=dev['idVendor'].rjust(4),
            pid=dev['idProduct'].ljust(4),
            manufacturer=dev['iManufacturer'].ljust(manufacturer_width),
            product=dev['iProduct'].ljust(product_width),
            serno=dev['iSerialNumber'].ljust(device_serial_width),
            ifcno=dev['bInterfaceNumber'].ljust(3),
            devname=dev['devname'])

        if endpoints:
            values['epnum'] = dev['bNumEndpoints'].ljust(3)

        line = header_format.format(**values)

        if 'mount' in dev:
            line += ' => {mount}'.format(mount=dev['mount'])

        print(line)
예제 #8
0
def print_csv(endpoints=False):
    """Print usbinfo results as CSV.

    Args:
        endpoints: If True, print the number of endpoints.
    """
    devices = usbinfo.usbinfo()
    header = _print_header(endpoints)

    writer = csv.writer(sys.stdout, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(header.values())

    for dev in devices:
        # If key is not set or empty, make sure it's an empty string
        for key in header:
            if key not in dev or not dev[key]:
                dev[key] = ''

        writer.writerow([dev[key] for key in header])
예제 #9
0
파일: ftdi.py 프로젝트: max19931/copper
    def get_comm_port(self, bus):
        """Get the communications port of a given bus.

    Args:
      bus: ID of the bus to index. This can either be a cardinal bus index
        or a name like 'ADBUS'.
    Returns:
      Communications port of the given bus.
    """
        bus = self._get_bus_index(bus)
        endpoints = [
            ep for ep in usbinfo.usbinfo()
            if int(ep['idVendor'], 16) == self.ID_VENDOR
            and ep.get('iSerialNumber') == self.serial_number
            and ep.get('bInterfaceNumber', '').isdigit()
            and ep.get('devname', '').startswith('/dev/tty')
        ]
        for ep in endpoints:
            if int(ep['bInterfaceNumber'], 16) == bus:
                return ep['devname']
예제 #10
0
def print_csv(endpoints=False):
    """Print usbinfo results as CSV.

    :param bool endpoints:
        If True, print the number of endpoints.
    """
    devices = usbinfo.usbinfo()
    header = _print_header(endpoints)

    writer = csv.writer(sys.stdout,
                        delimiter=',',
                        quoting=csv.QUOTE_MINIMAL)

    writer.writerow(header.values())

    for dev in devices:
        # If key is not set or empty, make sure it's an empty string
        for key in header:
            if key not in dev or not dev[key]:
                dev[key] = ''

        writer.writerow([dev[key] for key in header])
예제 #11
0
def endpoints_total():
    """Return the total number of endpoints used"""
    devices = usbinfo.usbinfo()
    return sum(int(dev['bNumEndpoints']) for dev in devices)