From 1a26b30d82984c44e0c5a5cb62fc24ce076a8590 Mon Sep 17 00:00:00 2001 From: Robert Martin Date: Sat, 4 Apr 2020 01:35:14 +0900 Subject: [PATCH] removed unused imports, cleanup --- joycontrol/server.py | 37 +++++++++++++++++-------------------- run_controller_cli.py | 23 ++++++++++------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/joycontrol/server.py b/joycontrol/server.py index cd31c8d..f3e10ae 100644 --- a/joycontrol/server.py +++ b/joycontrol/server.py @@ -21,22 +21,8 @@ async def _send_empty_input_reports(transport): await asyncio.sleep(1) -async def _run_protocol_on_connection(protocol, client_itr, capture_file=None): - transport = L2CAP_Transport(asyncio.get_event_loop(), protocol, client_itr, 50, capture_file=capture_file) - protocol.connection_made(transport) - - # send some empty input reports until the Switch decides to reply - future = asyncio.ensure_future(_send_empty_input_reports(transport)) - await protocol.wait_for_output_report() - future.cancel() - try: - await future - except asyncio.CancelledError: - pass - - -async def create_hid_server(protocol_factory, - ctl_psm=17, itr_psm=19, device_id=None, reconnect_bt_addr=None, capture_file=None): +async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=None, reconnect_bt_addr=None, + capture_file=None): """ :param protocol_factory: Factory function returning a ControllerProtocol instance :param ctl_psm: hid control channel port @@ -46,10 +32,10 @@ async def create_hid_server(protocol_factory, Bluetooth mac address in string notation of the adapter (e.g. "FF:FF:FF:FF:FF:FF"). If None, choose any device. Note: Selection of adapters may currently not work if the bluez "input" plugin is enabled. - :param reconnect_bt_addr: the Bluetooth address of the console that was previously connected. Defaults to None. + :param reconnect_bt_addr: The Bluetooth address of the console that was previously connected. Defaults to None. If None, a new hid server will be started for the initial paring. - Otherwise, the function assumes an initial pairing with the console was already done and reconnects - to the provided Bluetooth address. + Otherwise, the function assumes an initial pairing with the console was already done + and reconnects to the provided Bluetooth address. :param capture_file: opened file to log incoming and outgoing messages :returns transport for input reports and protocol which handles incoming output reports """ @@ -124,6 +110,17 @@ async def create_hid_server(protocol_factory, client_ctl.setblocking(False) client_itr.setblocking(False) - await _run_protocol_on_connection(protocol, client_itr, capture_file=capture_file) + # create transport for the established connection and activate the HID protocol + transport = L2CAP_Transport(asyncio.get_event_loop(), protocol, client_itr, 50, capture_file=capture_file) + protocol.connection_made(transport) + + # send some empty input reports until the Switch decides to reply + future = asyncio.ensure_future(_send_empty_input_reports(transport)) + await protocol.wait_for_output_report() + future.cancel() + try: + await future + except asyncio.CancelledError: + pass return protocol.transport, protocol diff --git a/run_controller_cli.py b/run_controller_cli.py index cf1935b..a99a42a 100644 --- a/run_controller_cli.py +++ b/run_controller_cli.py @@ -2,7 +2,6 @@ import argparse import asyncio import logging import os -import socket from contextlib import contextmanager from joycontrol import logging_default as log @@ -11,7 +10,6 @@ from joycontrol.controller import Controller from joycontrol.memory import FlashMemory from joycontrol.protocol import controller_protocol_factory from joycontrol.server import create_hid_server -from joycontrol.report import InputReport logger = logging.getLogger(__name__) @@ -19,9 +17,8 @@ logger = logging.getLogger(__name__) async def _main(controller, reconnect_bt_addr=None, capture_file=None, spi_flash=None, device_id=None): factory = controller_protocol_factory(controller, spi_flash=spi_flash) ctl_psm, itr_psm = 17, 19 - transport, protocol = await create_hid_server(factory, - reconnect_bt_addr=reconnect_bt_addr, - ctl_psm=ctl_psm, itr_psm=itr_psm, capture_file=capture_file, device_id=device_id) + transport, protocol = await create_hid_server(factory, reconnect_bt_addr=reconnect_bt_addr, ctl_psm=ctl_psm, + itr_psm=itr_psm, capture_file=capture_file, device_id=device_id) controller_state = protocol.get_controller_state() @@ -46,8 +43,8 @@ if __name__ == '__main__': parser.add_argument('-l', '--log') parser.add_argument('-d', '--device_id') parser.add_argument('--spi_flash') - parser.add_argument('-r', '--reconnect_bt_addr', type=str, default=None, - help='The Switch console bluetooth address, for reconnecting as an already paired controller') + parser.add_argument('-r', '--reconnect_bt_addr', type=str, default=None, + help='The Switch console Bluetooth address, for reconnecting as an already paired controller') args = parser.parse_args() if args.controller == 'JOYCON_R': @@ -80,10 +77,10 @@ if __name__ == '__main__': with get_output(args.log) as capture_file: loop = asyncio.get_event_loop() loop.run_until_complete( - _main(controller, - reconnect_bt_addr=args.reconnect_bt_addr, - capture_file=capture_file, - spi_flash=spi_flash, - device_id=args.device_id - ) + _main(controller, + reconnect_bt_addr=args.reconnect_bt_addr, + capture_file=capture_file, + spi_flash=spi_flash, + device_id=args.device_id + ) )