Exemple #1
0
def get_indexed_string(device_handle, index):
	"""Get a string from a HID device, based on its string index.

	Note: currently not working in the ``hidraw`` native implementation.

	:param device_handle: a device handle returned by open() or open_path().
	:param index: the index of the string to get.
	"""
	if index not in _DEVICE_STRINGS:
		return None

	assert device_handle
	stat = _os.fstat(device_handle)
	dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
	if dev:
		hid_dev = dev.find_parent('hid')
		if hid_dev:
			assert 'HID_ID' in hid_dev
			bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')

			if bus == '0003':  # USB
				usb_dev = dev.find_parent('usb', 'usb_device')
				assert usb_dev
				key = _DEVICE_STRINGS[index]
				attrs = usb_dev.attributes
				if key in attrs:
					return attrs[key]

			elif bus == '0005':  # BLUETOOTH
				# TODO
				pass
Exemple #2
0
def get_indexed_string(device_handle, index):
    """Get a string from a HID device, based on its string index.

    Note: currently not working in the ``hidraw`` native implementation.

    :param device_handle: a device handle returned by open() or open_path().
    :param index: the index of the string to get.
    :returns: the value corresponding to index, or None if no value found
    :rtype: bytes or NoneType
    """
    try:
        key = _DEVICE_STRINGS[index]
    except KeyError:
        return None

    assert device_handle
    stat = _os.fstat(device_handle)
    try:
        dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
    except (DeviceNotFoundError, ValueError):
        return None

    hid_dev = dev.find_parent('hid')
    if hid_dev:
        assert 'HID_ID' in hid_dev
        bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')

        if bus == '0003':  # USB
            usb_dev = dev.find_parent('usb', 'usb_device')
            assert usb_dev
            return usb_dev.attributes.get(key)

        elif bus == '0005':  # BLUETOOTH
            # TODO
            pass
Exemple #3
0
def get_indexed_string(device_handle, index):
    """Get a string from a HID device, based on its string index.

	Note: currently not working in the ``hidraw`` native implementation.

	:param device_handle: a device handle returned by open() or open_path().
	:param index: the index of the string to get.
	"""
    if index not in _DEVICE_STRINGS:
        return None

    assert device_handle
    stat = _os.fstat(device_handle)
    dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
    if dev:
        hid_dev = dev.find_parent('hid')
        if hid_dev:
            assert 'HID_ID' in hid_dev
            bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')

            if bus == '0003':  # USB
                usb_dev = dev.find_parent('usb', 'usb_device')
                assert usb_dev
                key = _DEVICE_STRINGS[index]
                attrs = usb_dev.attributes
                if key in attrs:
                    return attrs[key]

            elif bus == '0005':  # BLUETOOTH
                # TODO
                pass
Exemple #4
0
def get_indexed_string(device_handle, index):
	"""Get a string from a HID device, based on its string index.

	Note: currently not working in the ``hidraw`` native implementation.

	:param device_handle: a device handle returned by open() or open_path().
	:param index: the index of the string to get.
	:returns: the value corresponding to index, or None if no value found
	:rtype: bytes or NoneType
	"""
	try:
	    key = _DEVICE_STRINGS[index]
	except KeyError:
	    return None

	assert device_handle
	stat = _os.fstat(device_handle)
	try:
		dev = _Device.from_device_number(_Context(), 'char', stat.st_rdev)
	except (DeviceNotFoundError, ValueError):
		return None

	hid_dev = dev.find_parent('hid')
	if hid_dev:
		assert 'HID_ID' in hid_dev
		bus, _ignore, _ignore = hid_dev['HID_ID'].split(':')

		if bus == '0003':  # USB
			usb_dev = dev.find_parent('usb', 'usb_device')
			assert usb_dev
			return usb_dev.attributes.get(key)

		elif bus == '0005':  # BLUETOOTH
			# TODO
			pass
Exemple #5
0
 def test_from_device_number(self, context, device_data):
     if not device_data.device_node:
         pytest.skip("no device node, no device number")
     mode = os.stat(device_data.device_node).st_mode
     type = "block" if stat.S_ISBLK(mode) else "char"
     device = Device.from_device_number(context, type, device_data.device_number)
     assert device.device_number == device_data.device_number
     # make sure, we are really referring to the same device
     assert device.device_path == device_data.device_path
Exemple #6
0
 def test_from_device_number(self, context, device_data):
     if not device_data.device_node:
         pytest.skip('no device node, no device number')
     mode = os.stat(device_data.device_node).st_mode
     type = 'block' if stat.S_ISBLK(mode) else 'char'
     device = Device.from_device_number(context, type,
                                        device_data.device_number)
     assert device.device_number == device_data.device_number
     # make sure, we are really referring to the same device
     assert device.device_path == device_data.device_path
Exemple #7
0
 def test_from_device_number(self, context, device_path, device_number,
                             device_node):
     if not device_node:
         pytest.skip('no device node, no device number')
     mode = os.stat(device_node).st_mode
     type = 'block' if stat.S_ISBLK(mode) else 'char'
     device = Device.from_device_number(context, type, device_number)
     assert device.device_number == device_number
     # make sure, we are really referring to the same device
     assert device.device_path == device_path
Exemple #8
0
 def test_from_device_number_wrong_type(self, context, device_data):
     if not device_data.device_node:
         pytest.skip("no device node, no device number")
     mode = os.stat(device_data.device_node).st_mode
     # deliberately use the wrong type here to cause either failure or at
     # least device mismatch
     type = "char" if stat.S_ISBLK(mode) else "block"
     try:
         # this either fails, in which case the caught exception is raised,
         # or succeeds, but returns a wrong device (device numbers are not
         # unique across device types)
         device = Device.from_device_number(context, type, device_data.device_number)
         # if it succeeds, the resulting device must not match the one, we
         # are actually looking for!
         assert device.device_path != device_data.device_path
     except DeviceNotFoundByNumberError as error:
         # check the correctness of the exception attributes
         assert error.device_type == type
         assert error.device_number == device_data.device_number
Exemple #9
0
 def test_from_device_number_wrong_type(self, context, device_data):
     if not device_data.device_node:
         pytest.skip('no device node, no device number')
     mode = os.stat(device_data.device_node).st_mode
     # deliberately use the wrong type here to cause either failure or at
     # least device mismatch
     type = 'char' if stat.S_ISBLK(mode) else 'block'
     try:
         # this either fails, in which case the caught exception is raised,
         # or succeeds, but returns a wrong device (device numbers are not
         # unique across device types)
         device = Device.from_device_number(context, type,
                                            device_data.device_number)
         # if it succeeds, the resulting device must not match the one, we
         # are actually looking for!
         assert device.device_path != device_data.device_path
     except DeviceNotFoundByNumberError as error:
         # check the correctness of the exception attributes
         assert error.device_type == type
         assert error.device_number == device_data.device_number
Exemple #10
0
 def test_from_device_number_invalid_type(self, context):
     with pytest.raises(ValueError) as exc_info:
         Device.from_device_number(context, "foobar", 100)
     assert str(exc_info.value) == ("Invalid type: {0!r}. Must be one of " '"char" or "block".'.format("foobar"))
Exemple #11
0
 def test_from_device_number_invalid_type(self, context):
     with pytest.raises(ValueError) as exc_info:
         Device.from_device_number(context, 'foobar', 100)
     assert str(exc_info.value) == ('Invalid type: {0!r}. Must be one of '
                                    '"char" or "block".'.format('foobar'))