Example #1
0
 def _end_transaction_and_read(self, length=_READ_LENGTH):
     """End the transaction by reading from the device.
     According to the official documentation, as well as Craig's open-source
     implementation (libSiUSBXp), it should be necessary to check the queue
     size and read data in chunks.  However, leviathan and its derivatives
     seem to work fine without this complexity; we currently try the same
     approach.
     """
     msg = self.device.read(_PRO_READ_ENDPOINT, length, _READ_TIMEOUT)
     _LOGGER.debug('received %s', LazyHexRepr(msg))
     self.device.release()
     return msg
Example #2
0
    def write(self, data):
        """Write raw report to HID.

        The buffer should follow the semantics of the Linux HIDRAW API.

        > The first byte of the buffer passed to write() should be set to the
        > report number.  If the device does not use numbered reports, the
        > first byte should be set to 0. The report data itself should begin
        > at the second byte.
        """
        LOGGER.debug('writting report 0x%02x with %d bytes: %r', data[0],
                     len(data) - 1, LazyHexRepr(data, start=1))
        return self.hiddev.write(data)
Example #3
0
    def get_feature_report(self, report_id, length):
        """Get feature report that matches `report_id` from HID.

        If the device does not use numbered reports, set `report_id` to 0.

        Unlike `read`, the returned data follows semantics similar to `write`
        and `send_feature_report`: the first byte will always contain the
        report ID (or 0), and the report data itself will being at the second
        byte.
        """
        data = self.hiddev.get_feature_report(report_id, length)
        _LOGGER.debug('got feature report 0x%02x with %d bytes: %r', data[0],
                      len(data) - 1, LazyHexRepr(data, start=1))
        return data
Example #4
0
    def read(self, length):
        """Read raw report from HID.

        The returned data follows the semantics of the Linux HIDRAW API.

        > On a device which uses numbered reports, the first byte of the
        > returned data will be the report number; the report data follows,
        > beginning in the second byte. For devices which do not use numbered
        > reports, the report data will begin at the first byte.
        """
        self.hiddev.set_nonblocking(False)
        data = self.hiddev.read(length)
        _LOGGER.debug('read %d bytes: %r', len(data), LazyHexRepr(data))
        return data
Example #5
0
    def send_feature_report(self, data):
        """Send feature report to HID.

        The buffer should follow the semantics of `write`.

        > The first byte of the buffer passed to write() should be set to the
        > report number.  If the device does not use numbered reports, the
        > first byte should be set to 0. The report data itself should begin
        > at the second byte.
        """
        LOGGER.debug('sending feature report 0x%02x with %d bytes: %r',
                     data[0],
                     len(data) - 1, LazyHexRepr(data, start=1))
        return self.hiddev.send_feature_report(data)
Example #6
0
    def write(self, data):
        """Write raw report to HID.

        The buffer should follow the semantics of the Linux HIDRAW API.

        > The first byte of the buffer passed to write() should be set to the
        > report number.  If the device does not use numbered reports, the
        > first byte should be set to 0. The report data itself should begin
        > at the second byte.
        """
        _LOGGER.debug('writting report 0x%02x with %d bytes: %r', data[0],
                      len(data) - 1, LazyHexRepr(data, start=1))
        res = self.hiddev.write(data)
        if res < 0:
            raise OSError('Could not write to device')
        if res != len(data):
            _LOGGER.debug('wrote %d total bytes, expected %d', res, len(data))
        return res
Example #7
0
    def send_feature_report(self, data):
        """Send feature report to HID.

        The buffer should follow the semantics of `write`.

        > The first byte of the buffer passed to write() should be set to the
        > report number.  If the device does not use numbered reports, the
        > first byte should be set to 0. The report data itself should begin
        > at the second byte.
        """
        _LOGGER.debug('sending feature report 0x%02x with %d bytes: %r',
                      data[0],
                      len(data) - 1, LazyHexRepr(data, start=1))
        res = self.hiddev.send_feature_report(data)
        if res < 0:
            raise OSError('Could not send feature report to device')
        if res != len(data):
            _LOGGER.debug('sent %d total bytes, expected %d', res, len(data))
        return res
Example #8
0
 def write_block_data(self, address, register, data):
     """Write a block of byte data to a given register."""
     _LOGGER.debug('writing block data @ 0x%02x:0x%02x: %r', address,
                   register, LazyHexRepr(data))
     return self._smbus.write_block_data(address, register, data)
Example #9
0
 def read_block_data(self, address, register):
     """Read a block of up to  32 bytes from a given register."""
     data = self._smbus.read_block_data(address, register)
     _LOGGER.debug('read block data @ 0x%02x:0x%02x: %r', address,
                   register, LazyHexRepr(data))
     return data
Example #10
0
 def _write(self, data):
     """Write data to the AIO and log"""
     _LOGGER.debug('write %s', LazyHexRepr(data))
     self.device.write(_PRO_WRITE_ENDPOINT, data, _WRITE_TIMEOUT)
Example #11
0
 def write(self, endpoint, data, timeout=None):
     """Write to endpoint."""
     _LOGGER.debug('writting %d bytes: %r', len(data), LazyHexRepr(data))
     return self.usbdev.write(endpoint, data, timeout=timeout)
Example #12
0
 def read(self, endpoint, length, timeout=None):
     """Read from endpoint."""
     data = self.usbdev.read(endpoint, length, timeout=timeout)
     _LOGGER.debug('read %d bytes: %r', len(data), LazyHexRepr(data))
     return data