From 1fd9e2b6551253b075f3d6ea9aca5d271c96fbde Mon Sep 17 00:00:00 2001 From: Robert Martin Date: Wed, 29 Jan 2020 20:51:59 +0900 Subject: [PATCH] home button mash test --- buttons.py | 30 +++++++++++++++++++++++++++++ protocol.py | 14 ++++++++++++-- report.py | 43 +++++++++++++++++++++++------------------- run_and_pair_switch.py | 23 ++++++++++++++++++++++ 4 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 buttons.py diff --git a/buttons.py b/buttons.py new file mode 100644 index 0000000..de83b82 --- /dev/null +++ b/buttons.py @@ -0,0 +1,30 @@ + +def get_bit(value, n): + return (value >> n & 1) != 0 + + +def flip_bit(value, n): + return value ^ (1 << n) + + +class Buttons: + """ + Utility class to set buttons in the input report + TODO: More Buttons + """ + def __init__(self): + self.left = 0 + self.middle = 0 + self.right = 0 + + def home(self): + self.middle = flip_bit(self.middle, 4) + + def home_is_set(self): + return get_bit(self.middle, 4) + + def to_list(self): + return [self.left, self.middle, self.right] + + def clear(self): + self.left = self.middle = self.right = 0 diff --git a/protocol.py b/protocol.py index 0d0fb17..c0d09ad 100644 --- a/protocol.py +++ b/protocol.py @@ -47,7 +47,7 @@ class ControllerProtocol(BaseProtocol): return # classify sub command - sub_command = report.get_sub_command() + sc_byte, sub_command = report.get_sub_command() logging.info(f'received output report - {sub_command}') if sub_command == SubCommand.REQUEST_DEVICE_INFO: await self._command_request_device_info(report) @@ -65,7 +65,7 @@ class ControllerProtocol(BaseProtocol): await self._command_trigger_buttons_elapsed_time(report) elif sub_command == SubCommand.NOT_IMPLEMENTED: - logger.error(f'Sub command not implemented - ignoring') + logger.error(f'Sub command 0x{sc_byte:02x} not implemented - ignoring') async def _command_request_device_info(self, output_report): address = self.transport.get_extra_info('sockname') @@ -119,3 +119,13 @@ class ControllerProtocol(BaseProtocol): input_report.sub_0x04_trigger_buttons_elapsed_time() asyncio.ensure_future(self.transport.write(input_report)) + + async def _enable_6axis_sensor(self, output_report): + input_report = InputReport() + input_report.set_input_report_id(0x21) + input_report.set_misc() + input_report.set_ack(0x80) + + input_report.reply_to_subcommand_id(0x40) + + asyncio.ensure_future(self.transport.write(input_report)) diff --git a/report.py b/report.py index 0e37e4d..2ca619f 100644 --- a/report.py +++ b/report.py @@ -4,6 +4,10 @@ from controller import Controller class InputReport: + """ + Class to create Input Reports. Reference: + https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/bluetooth_hid_notes.md + """ def __init__(self): self.data = [0x00] * 50 # all input reports are prepended with 0xA1 @@ -11,13 +15,14 @@ class InputReport: def set_input_report_id(self, _id): """ - :param _id: e.g. 0x21 Standard input reports used for subcommand replies, etc... (TODO) + :param _id: e.g. 0x21 Standard input reports used for sub command replies + etc... (TODO) """ self.data[1] = _id def set_timer(self, timer): """ - Input report timer, usually set by the transport + Input report timer (0x00-0xFF), usually set by the transport """ self.data[2] = timer % 256 @@ -25,13 +30,6 @@ class InputReport: # battery level + connection info self.data[3] = 0x8E - def set_ack(self, ack): - """ - ACK byte for subcmd reply - TODO - """ - self.data[14] = ack - def set_button_status(self): """ TODO @@ -56,6 +54,13 @@ class InputReport: """ self.data[13] = 0x80 + def set_ack(self, ack): + """ + ACK byte for subcmd reply + TODO + """ + self.data[14] = ack + def sub_0x02_device_info(self, mac, fm_version=(0x03, 0x48), controller=Controller.JOYCON_L): """ Sub command 0x02 request device info response. @@ -81,22 +86,21 @@ class InputReport: self.data[offset + 10] = 0x01 self.data[offset + 11] = 0x01 + def reply_to_subcommand_id(self, id_): + self.data[15] = id_ + def sub_0x08_shipment(self): - # reply to sub command ID - self.data[15] = 0x08 + self.reply_to_subcommand_id(0x08) def sub_0x10_spi_flash_read(self, output_report): - # reply to sub command ID - self.data[15] = 0x10 + self.reply_to_subcommand_id(0x10) self.data[16:18] = output_report.data[12:14] def sub_0x03_set_input_report_mode(self): - # reply to sub command ID - self.data[15] = 0x03 + self.reply_to_subcommand_id(0x03) def sub_0x04_trigger_buttons_elapsed_time(self): - # reply to sub command ID - self.data[15] = 0x04 + self.reply_to_subcommand_id(0x04) # TODO blub = [0x00, 0xCC, 0x00, 0xEE, 0x00, 0xFF] @@ -112,6 +116,7 @@ class SubCommand(Enum): TRIGGER_BUTTONS_ELAPSED_TIME = 0x04 SET_SHIPMENT_STATE = 0x08 SPI_FLASH_READ = 0x10 + ENABLE_6AXIS_SENSOR = 0x40 NOT_IMPLEMENTED = 0xFF @@ -123,9 +128,9 @@ class OutputReport: def get_sub_command(self): try: - return SubCommand(self.data[11]) + return self.data[11], SubCommand(self.data[11]) except ValueError: - return SubCommand.NOT_IMPLEMENTED + return self.data[11], SubCommand.NOT_IMPLEMENTED def __bytes__(self): return bytes(self.data) diff --git a/run_and_pair_switch.py b/run_and_pair_switch.py index 2d2eae7..ef5f3c7 100644 --- a/run_and_pair_switch.py +++ b/run_and_pair_switch.py @@ -5,6 +5,7 @@ import socket import logging_default as log import utils +from buttons import Buttons from device import HidDevice from protocol import controller_protocol_factory, Controller from report import InputReport @@ -63,6 +64,24 @@ async def send_empty_input_reports(transport): await asyncio.sleep(1) +async def mash_home_button(transport): + report = InputReport() + report.set_input_report_id(0x21) + report.set_misc() + + buttons = Buttons() + + for _ in range(30): + buttons.home() + report.data[4:7] = buttons.to_list() + await transport.write(report) + await asyncio.sleep(0.1) + buttons.home() + report.data[4:7] = buttons.to_list() + await transport.write(report) + await asyncio.sleep(.3) + + async def main(): transport, protocol = await create_hid_server(controller_protocol_factory(Controller.PRO_CONTROLLER), 17, 19) @@ -75,6 +94,10 @@ async def main(): except asyncio.CancelledError: pass + await asyncio.sleep(5) + + await mash_home_button(transport) + # stop communication after some time await asyncio.sleep(60) logger.info('Stopping communication...')