forked from mirror/joycontrol
Merge branch 'master' into pause_reading_fix
This commit is contained in:
+71
-54
@@ -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,68 +32,85 @@ 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
|
||||||
"""
|
"""
|
||||||
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)
|
|
||||||
itr_sock.setblocking(False)
|
|
||||||
|
|
||||||
try:
|
|
||||||
hid = HidDevice(device_id=device_id)
|
|
||||||
|
|
||||||
ctl_sock.bind((hid.address, ctl_psm))
|
|
||||||
itr_sock.bind((hid.address, itr_psm))
|
|
||||||
except OSError as err:
|
|
||||||
logger.warning(err)
|
|
||||||
# If the ports are already taken, this probably means that the bluez "input" plugin is enabled.
|
|
||||||
logger.warning('Fallback: Restarting bluetooth due to incompatibilities with the bluez "input" plugin. '
|
|
||||||
'Disable the plugin to avoid issues. See https://github.com/mart1nro/joycontrol/issues/8.')
|
|
||||||
# HACK: To circumvent incompatibilities with the bluetooth "input" plugin, we need to restart Bluetooth here.
|
|
||||||
# The Switch does not connect to the sockets if we don't.
|
|
||||||
# For more info see: https://github.com/mart1nro/joycontrol/issues/8
|
|
||||||
logger.info('Restarting bluetooth service...')
|
|
||||||
await utils.run_system_command('systemctl restart bluetooth.service')
|
|
||||||
await asyncio.sleep(1)
|
|
||||||
|
|
||||||
hid = HidDevice(device_id=device_id)
|
|
||||||
|
|
||||||
ctl_sock.bind((socket.BDADDR_ANY, ctl_psm))
|
|
||||||
itr_sock.bind((socket.BDADDR_ANY, itr_psm))
|
|
||||||
|
|
||||||
ctl_sock.listen(1)
|
|
||||||
itr_sock.listen(1)
|
|
||||||
|
|
||||||
protocol = protocol_factory()
|
protocol = protocol_factory()
|
||||||
|
|
||||||
hid.powered(True)
|
if reconnect_bt_addr is None:
|
||||||
# setting bluetooth adapter name and class to the device we wish to emulate
|
ctl_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
|
||||||
await hid.set_name(protocol.controller.device_name())
|
itr_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
|
||||||
await hid.set_class()
|
ctl_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)
|
||||||
|
|
||||||
logger.info('Advertising the Bluetooth SDP record...')
|
try:
|
||||||
try:
|
hid = HidDevice(device_id=device_id)
|
||||||
HidDevice.register_sdp_record(PROFILE_PATH)
|
|
||||||
except dbus.exceptions.DBusException as dbus_err:
|
|
||||||
# Already registered (If multiple controllers are being emulated and this method is called consecutive times)
|
|
||||||
logger.debug(dbus_err)
|
|
||||||
|
|
||||||
# start advertising
|
ctl_sock.bind((hid.address, ctl_psm))
|
||||||
hid.discoverable()
|
itr_sock.bind((hid.address, itr_psm))
|
||||||
|
except OSError as err:
|
||||||
|
logger.warning(err)
|
||||||
|
# If the ports are already taken, this probably means that the bluez "input" plugin is enabled.
|
||||||
|
logger.warning('Fallback: Restarting bluetooth due to incompatibilities with the bluez "input" plugin. '
|
||||||
|
'Disable the plugin to avoid issues. See https://github.com/mart1nro/joycontrol/issues/8.')
|
||||||
|
# HACK: To circumvent incompatibilities with the bluetooth "input" plugin, we need to restart Bluetooth here.
|
||||||
|
# The Switch does not connect to the sockets if we don't.
|
||||||
|
# For more info see: https://github.com/mart1nro/joycontrol/issues/8
|
||||||
|
logger.info('Restarting bluetooth service...')
|
||||||
|
await utils.run_system_command('systemctl restart bluetooth.service')
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
logger.info('Waiting for Switch to connect... Please open the "Change Grip/Order" menu.')
|
hid = HidDevice(device_id=device_id)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
ctl_sock.bind((socket.BDADDR_ANY, ctl_psm))
|
||||||
client_ctl, ctl_address = await loop.sock_accept(ctl_sock)
|
itr_sock.bind((socket.BDADDR_ANY, itr_psm))
|
||||||
logger.info(f'Accepted connection at psm {ctl_psm} from {ctl_address}')
|
|
||||||
client_itr, itr_address = await loop.sock_accept(itr_sock)
|
|
||||||
logger.info(f'Accepted connection at psm {itr_psm} from {itr_address}')
|
|
||||||
assert ctl_address[0] == itr_address[0]
|
|
||||||
|
|
||||||
# stop advertising
|
ctl_sock.listen(1)
|
||||||
hid.discoverable(False)
|
itr_sock.listen(1)
|
||||||
|
|
||||||
|
hid.powered(True)
|
||||||
|
# setting bluetooth adapter name and class to the device we wish to emulate
|
||||||
|
await hid.set_name(protocol.controller.device_name())
|
||||||
|
await hid.set_class()
|
||||||
|
|
||||||
|
logger.info('Advertising the Bluetooth SDP record...')
|
||||||
|
try:
|
||||||
|
HidDevice.register_sdp_record(PROFILE_PATH)
|
||||||
|
except dbus.exceptions.DBusException as dbus_err:
|
||||||
|
# Already registered (If multiple controllers are being emulated and this method is called consecutive times)
|
||||||
|
logger.debug(dbus_err)
|
||||||
|
|
||||||
|
# start advertising
|
||||||
|
hid.discoverable()
|
||||||
|
|
||||||
|
logger.info('Waiting for Switch to connect... Please open the "Change Grip/Order" menu.')
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
client_ctl, ctl_address = await loop.sock_accept(ctl_sock)
|
||||||
|
logger.info(f'Accepted connection at psm {ctl_psm} from {ctl_address}')
|
||||||
|
client_itr, itr_address = await loop.sock_accept(itr_sock)
|
||||||
|
logger.info(f'Accepted connection at psm {itr_psm} from {itr_address}')
|
||||||
|
assert ctl_address[0] == itr_address[0]
|
||||||
|
|
||||||
|
# stop advertising
|
||||||
|
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
@@ -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
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user