def _device_command_bus_write(self, address, *data): address = ord(address) data = [ord(c) for c in data] self._bus_log.debug('Device(0x%X) write(%s)', address / 2, hex_data(data)) try: device = self._bus_device(address) device.handle(data) response = device.get_response() self._bus_log.debug('Generated response %s', hex_data(bytearray(response))) except LatchBusError: self._bus_log.warn('Device request latching bus') self._command(I2CMock.CMD_I2C_BUS_LATCH)
def _device_command_bus_request_response(self, address): address = ord(address) self._bus_log.debug('Device(0x%X) read', address / 2) device = self._bus_device(address) response = device.get_response() or [] self._command(I2CMock.CMD_I2C_BUS_RESPONSE, response) self._bus_log.debug('Response written (%s)', hex_data(bytearray(response)))
def _device_command_write(self, address, *data): address = ord(address) data = [ord(c) for c in data] self._log.info('Device(%X) write(%s)', address, hex_data(data)) device = self._device(address) device.freeze_end = self._freeze_end response = device.handle(data) or [] self._log.debug('Generated response %r', response) self._command(I2CMock.CMD_I2C_RESPONSE, response) self._log.debug('Response written')
def handle(self, data): self.response = None handler = None args = [] for h in self.handlers: if h.bytes == data[:len(h.bytes)]: handler = h args = data[len(h.bytes):] if handler is None: return self._missing_handler(data) self.log.info("%s.%s(%s)", type(self).__name__, handler.__name__, hex_data(args)) self.response = handler(self, *args) or []
def _reader_run(self): self._log.debug("Worker thread starting") self._port.reset_input_buffer() self._port.reset_output_buffer() self._reader_started.set() try: while self._port.is_open: (cmd, data) = self._read_command() self._log.debug("Received command %X (%s)", cmd, hex_data(data)) self._command_handlers[cmd](self, *data) except DeviceMockStopped: pass except Exception as e: self._log.error("Handle command exception %s", traceback.format_exc(e)) self._log.debug("Finished worker thread")
def _reader_run(self): self._log.debug("Worker thread starting") self._port.reset_input_buffer() self._port.reset_output_buffer() self._reader_started.set() try: while self._port.is_open: (cmd, data) = self._read_command() self._log.debug("Received command 0x%X (%s)", cmd, hex_data(data)) self._command_handlers[cmd](self, *data) except DeviceMockStopped: pass except Exception as e: self._log.error("Handle command exception %s", traceback.format_exc(e)) self._log.debug("Finished worker thread")