reconnect paired device

This commit is contained in:
Cambridge Yang
2020-04-03 10:48:52 -04:00
parent d3fbd441d8
commit 3897d97696
2 changed files with 37 additions and 20 deletions
+32 -13
View File
@@ -16,12 +16,25 @@ logger = logging.getLogger(__name__)
async def _send_empty_input_reports(transport): async def _send_empty_input_reports(transport):
report = InputReport() report = InputReport()
while True: while True:
await transport.write(report) await transport.write(report)
await asyncio.sleep(1) 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, capture_file=None): async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=None, capture_file=None):
""" """
:param protocol_factory: Factory function returning a ControllerProtocol instance :param protocol_factory: Factory function returning a ControllerProtocol instance
@@ -96,20 +109,26 @@ async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=
# stop advertising # stop advertising
hid.discoverable(False) hid.discoverable(False)
await run_protocol_on_connection(protocol, client_itr, capture_file=capture_file) await _run_protocol_on_connection(protocol, client_itr, capture_file=capture_file)
return protocol.transport, protocol return protocol.transport, protocol
async def run_protocol_on_connection(protocol, client_itr, capture_file=None): async def create_reconnection(protocol_factory, console_bt_addr, ctl_psm=17, itr_psm=19, capture_file=None):
transport = L2CAP_Transport(asyncio.get_event_loop(), protocol, client_itr, 50, capture_file=capture_file) """Setup a running protocal by reconnecting to a pairsed console.
protocol.connection_made(transport)
:param console_bt_addr: a bluetooth address for the Switch console.
:param *args, **kwargs: see `create_hid_server`, except that `create_reconnection` does not require device_id.
:returns: see `create_hid_server`
"""
protocol = protocol_factory()
client_ctl = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
client_itr = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
client_ctl.connect((console_bt_addr, ctl_psm))
client_itr.connect((console_bt_addr, itr_psm))
client_ctl.setblocking(False)
client_itr.setblocking(False)
# send some empty input reports until the Switch decides to reply await _run_protocol_on_connection(protocol, client_itr)
future = asyncio.ensure_future(_send_empty_input_reports(transport)) transport = protocol.transport
await protocol.wait_for_output_report() return transport, protocol
future.cancel()
try:
await future
except asyncio.CancelledError:
pass
+5 -7
View File
@@ -10,7 +10,8 @@ from joycontrol.command_line_interface import ControllerCLI
from joycontrol.controller import Controller from joycontrol.controller import Controller
from joycontrol.memory import FlashMemory from joycontrol.memory import FlashMemory
from joycontrol.protocol import controller_protocol_factory from joycontrol.protocol import controller_protocol_factory
from joycontrol.server import create_hid_server, run_protocol_on_connection from joycontrol.server import create_hid_server, create_reconnection
from joycontrol.report import InputReport
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -22,12 +23,9 @@ async def _main(controller, console_bt_addr=None, capture_file=None, spi_flash=N
transport, protocol = await create_hid_server(factory, transport, protocol = await create_hid_server(factory,
ctl_psm=ctl_psm, itr_psm=itr_psm, capture_file=capture_file, device_id=device_id) ctl_psm=ctl_psm, itr_psm=itr_psm, capture_file=capture_file, device_id=device_id)
else: else:
protocol = factory() transport, protocol = await create_reconnection(factory,
client_ctl = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) console_bt_addr,
# client_ctl.setblocking(False) ctl_psm=ctl_psm, itr_psm=itr_psm, capture_file=capture_file)
client_ctl.connect((console_bt_addr, ctl_psm))
await run_protocol_on_connection(protocol, client_ctl)
transport = protocol.transport
controller_state = protocol.get_controller_state() controller_state = protocol.get_controller_state()