forked from mirror/joycontrol
cleanup
This commit is contained in:
+16
-21
@@ -35,7 +35,8 @@ async def _run_protocol_on_connection(protocol, client_itr, capture_file=None):
|
||||
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, reconnect_bt_addr=None, capture_file=None):
|
||||
"""
|
||||
:param protocol_factory: Factory function returning a ControllerProtocol instance
|
||||
:param ctl_psm: hid control channel port
|
||||
@@ -45,9 +46,16 @@ 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").
|
||||
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.
|
||||
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
|
||||
: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)
|
||||
itr_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
|
||||
ctl_sock.setblocking(False)
|
||||
@@ -80,8 +88,6 @@ async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=
|
||||
ctl_sock.listen(1)
|
||||
itr_sock.listen(1)
|
||||
|
||||
protocol = protocol_factory()
|
||||
|
||||
hid.powered(True)
|
||||
# setting bluetooth adapter name and class to the device we wish to emulate
|
||||
await hid.set_name(protocol.controller.device_name())
|
||||
@@ -109,26 +115,15 @@ async def create_hid_server(protocol_factory, ctl_psm=17, itr_psm=19, device_id=
|
||||
# stop advertising
|
||||
hid.discoverable(False)
|
||||
|
||||
await _run_protocol_on_connection(protocol, client_itr, capture_file=capture_file)
|
||||
|
||||
return protocol.transport, protocol
|
||||
|
||||
|
||||
async def create_reconnection(protocol_factory, console_bt_addr, ctl_psm=17, itr_psm=19, capture_file=None):
|
||||
"""Setup a running protocal by reconnecting to a pairsed console.
|
||||
|
||||
: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()
|
||||
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((console_bt_addr, ctl_psm))
|
||||
client_itr.connect((console_bt_addr, itr_psm))
|
||||
client_ctl.connect((reconnect_bt_addr, ctl_psm))
|
||||
client_itr.connect((reconnect_bt_addr, itr_psm))
|
||||
client_ctl.setblocking(False)
|
||||
client_itr.setblocking(False)
|
||||
|
||||
await _run_protocol_on_connection(protocol, client_itr)
|
||||
transport = protocol.transport
|
||||
return transport, protocol
|
||||
await _run_protocol_on_connection(protocol, client_itr, capture_file=capture_file)
|
||||
|
||||
return protocol.transport, protocol
|
||||
|
||||
@@ -10,22 +10,18 @@ from joycontrol.command_line_interface import ControllerCLI
|
||||
from joycontrol.controller import Controller
|
||||
from joycontrol.memory import FlashMemory
|
||||
from joycontrol.protocol import controller_protocol_factory
|
||||
from joycontrol.server import create_hid_server, create_reconnection
|
||||
from joycontrol.server import create_hid_server
|
||||
from joycontrol.report import InputReport
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def _main(controller, console_bt_addr=None, 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)
|
||||
ctl_psm, itr_psm = 17, 19
|
||||
if console_bt_addr is None:
|
||||
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)
|
||||
else:
|
||||
transport, protocol = await create_reconnection(factory,
|
||||
console_bt_addr,
|
||||
ctl_psm=ctl_psm, itr_psm=itr_psm, capture_file=capture_file)
|
||||
|
||||
controller_state = protocol.get_controller_state()
|
||||
|
||||
@@ -50,7 +46,8 @@ if __name__ == '__main__':
|
||||
parser.add_argument('-l', '--log')
|
||||
parser.add_argument('-d', '--device_id')
|
||||
parser.add_argument('--spi_flash')
|
||||
parser.add_argument('--console-bt-addr', type=str, default=None)
|
||||
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':
|
||||
@@ -84,7 +81,7 @@ if __name__ == '__main__':
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(
|
||||
_main(controller,
|
||||
console_bt_addr=args.console_bt_addr,
|
||||
reconnect_bt_addr=args.reconnect_bt_addr,
|
||||
capture_file=capture_file,
|
||||
spi_flash=spi_flash,
|
||||
device_id=args.device_id
|
||||
|
||||
Reference in New Issue
Block a user