Merge pull request #17 from thisiscam/reconnect

Reconnect
This commit is contained in:
Robert Martin
2020-04-04 01:36:18 +09:00
committed by GitHub
2 changed files with 83 additions and 57 deletions
+22 -5
View File
@@ -16,13 +16,13 @@ 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 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, reconnect_bt_addr=None,
capture_file=None):
""" """
:param protocol_factory: Factory function returning a ControllerProtocol instance :param protocol_factory: Factory function returning a ControllerProtocol instance
:param ctl_psm: hid control channel port :param ctl_psm: hid control channel port
@@ -32,13 +32,22 @@ async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=
Bluetooth mac address in string notation of the adapter (e.g. "FF:FF:FF:FF:FF:FF"). Bluetooth mac address in string notation of the adapter (e.g. "FF:FF:FF:FF:FF:FF").
If None, choose any device. If None, choose any device.
Note: Selection of adapters may currently not work if the bluez "input" plugin is enabled. 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.
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.
:param capture_file: opened file to log incoming and outgoing messages :param capture_file: opened file to log incoming and outgoing messages
:returns transport for input reports and protocol which handles incoming output reports :returns transport for input reports and protocol which handles incoming output reports
""" """
protocol = protocol_factory()
if reconnect_bt_addr is None:
ctl_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) ctl_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
itr_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP) itr_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
ctl_sock.setblocking(False) ctl_sock.setblocking(False)
itr_sock.setblocking(False) itr_sock.setblocking(False)
ctl_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
itr_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try: try:
hid = HidDevice(device_id=device_id) hid = HidDevice(device_id=device_id)
@@ -65,8 +74,6 @@ async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=
ctl_sock.listen(1) ctl_sock.listen(1)
itr_sock.listen(1) itr_sock.listen(1)
protocol = protocol_factory()
hid.powered(True) hid.powered(True)
# setting bluetooth adapter name and class to the device we wish to emulate # setting bluetooth adapter name and class to the device we wish to emulate
await hid.set_name(protocol.controller.device_name()) await hid.set_name(protocol.controller.device_name())
@@ -94,6 +101,16 @@ 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)
else:
# Reconnection to reconnect_bt_addr
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((reconnect_bt_addr, ctl_psm))
client_itr.connect((reconnect_bt_addr, itr_psm))
client_ctl.setblocking(False)
client_itr.setblocking(False)
# 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) transport = L2CAP_Transport(asyncio.get_event_loop(), protocol, client_itr, 50, capture_file=capture_file)
protocol.connection_made(transport) protocol.connection_made(transport)
@@ -106,4 +123,4 @@ async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
return transport, protocol return protocol.transport, protocol
+12 -3
View File
@@ -14,9 +14,11 @@ from joycontrol.server import create_hid_server
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
async def _main(controller, capture_file=None, spi_flash=None, device_id=None): 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) factory = controller_protocol_factory(controller, spi_flash=spi_flash)
transport, protocol = await create_hid_server(factory, 17, 19, capture_file=capture_file, device_id=device_id) 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)
controller_state = protocol.get_controller_state() controller_state = protocol.get_controller_state()
@@ -41,6 +43,8 @@ if __name__ == '__main__':
parser.add_argument('-l', '--log') parser.add_argument('-l', '--log')
parser.add_argument('-d', '--device_id') parser.add_argument('-d', '--device_id')
parser.add_argument('--spi_flash') 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')
args = parser.parse_args() args = parser.parse_args()
if args.controller == 'JOYCON_R': if args.controller == 'JOYCON_R':
@@ -73,5 +77,10 @@ if __name__ == '__main__':
with get_output(args.log) as capture_file: with get_output(args.log) as capture_file:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete( loop.run_until_complete(
_main(controller, 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
)
) )