diff --git a/protocol.py b/protocol.py index 8979ae1..14e2e6c 100644 --- a/protocol.py +++ b/protocol.py @@ -49,20 +49,23 @@ class ControllerProtocol(BaseProtocol): # classify sub command sub_command = report.get_sub_command() logging.info(f'received output report - {sub_command}') - if sub_command is None: - logger.error(f'No sub command found') - elif sub_command == SubCommand.REQUEST_DEVICE_INFO: + if sub_command == SubCommand.REQUEST_DEVICE_INFO: await self._command_request_device_info(report) + elif sub_command == SubCommand.SET_SHIPMENT_STATE: await self._command_set_shipment_state(report) + elif sub_command == SubCommand.SPI_FLASH_READ: await self._command_spi_flash_read(report) + elif sub_command == SubCommand.SET_INPUT_REPORT_MODE: await self._command_set_input_report_mode(report) + elif sub_command == SubCommand.TRIGGER_BUTTONS_ELAPSED_TIME: await self._command_trigger_buttons_elapsed_time(report) + elif sub_command == SubCommand.NOT_IMPLEMENTED: - logger.error(f'Sub command not implemented') + logger.error(f'Sub command not implemented - ignoring') async def _command_request_device_info(self, output_report): address = self.transport.get_extra_info('sockname') diff --git a/report.py b/report.py index 5ad36bd..0e37e4d 100644 --- a/report.py +++ b/report.py @@ -1,11 +1,5 @@ -from enum import Enum, auto +from enum import Enum -#[0xA1 0, 0x21 1, 0x05 2, 0x8E 3, -# 0x84, 0x00, 0x12, button -# 0x01, 0x18, 0x80, left analog -# 0x01, 0x18, 0x80, right analog -# 0x80, vibrator? -# 0x82, 0x02, 0x03, 0x48, 0x01, 0x02, 0xDC, 0xA6, 0x32, 0x71, 0x58, 0xBB, 0x01, 0x01] from controller import Controller @@ -113,12 +107,12 @@ class InputReport: class SubCommand(Enum): - REQUEST_DEVICE_INFO = auto() - SET_SHIPMENT_STATE = auto() - SPI_FLASH_READ = auto() - SET_INPUT_REPORT_MODE = auto() - NOT_IMPLEMENTED = auto() - TRIGGER_BUTTONS_ELAPSED_TIME = auto() + REQUEST_DEVICE_INFO = 0x02 + SET_INPUT_REPORT_MODE = 0x03 + TRIGGER_BUTTONS_ELAPSED_TIME = 0x04 + SET_SHIPMENT_STATE = 0x08 + SPI_FLASH_READ = 0x10 + NOT_IMPLEMENTED = 0xFF class OutputReport: @@ -128,20 +122,10 @@ class OutputReport: self.data = data def get_sub_command(self): - print('subcommand:', self.data[11]) - sub_command_byte = self.data[11] - if sub_command_byte == 0x02: - return SubCommand.REQUEST_DEVICE_INFO - elif sub_command_byte == 0x08: - return SubCommand.SET_SHIPMENT_STATE - elif sub_command_byte == 0x10: - return SubCommand.SPI_FLASH_READ - elif sub_command_byte == 0x03: - return SubCommand.SET_INPUT_REPORT_MODE - elif sub_command_byte == 0x04: - return SubCommand.TRIGGER_BUTTONS_ELAPSED_TIME - else: - return None + try: + return SubCommand(self.data[11]) + except ValueError: + return SubCommand.NOT_IMPLEMENTED def __bytes__(self): return bytes(self.data) diff --git a/transport.py b/transport.py index 2793905..02cdf94 100644 --- a/transport.py +++ b/transport.py @@ -60,10 +60,10 @@ class L2CAP_Transport(asyncio.Transport): return super().get_write_buffer_size() async def write(self, data: Any) -> None: - if isinstance(data, bytes): _bytes = data elif isinstance(data, InputReport): + # set timer byte of input report data.set_timer(self._input_report_timer) self._input_report_timer = (self._input_report_timer + 1) % 256 _bytes = bytes(data) @@ -99,4 +99,4 @@ class L2CAP_Transport(asyncio.Transport): self._protocol = protocol def get_protocol(self) -> asyncio.BaseProtocol: - return self._protocol \ No newline at end of file + return self._protocol