Skip to content

Commit a5a07a1

Browse files
authored
Merge pull request #85 from quaxalber/development
Check gadget state on relay init
2 parents 9be8f1d + 9762f9b commit a5a07a1

File tree

4 files changed

+64
-59
lines changed

4 files changed

+64
-59
lines changed

README.md

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ sudo ~/bluetooth_2_usb/scripts/update.sh
212212
```
213213

214214
> [!NOTE]
215-
> The update script performs a clean reinstallation, that is run `uninstall.sh`, delete the repo folder, clone again and run the install script.
215+
> The update script performs a clean reinstallation, that is run `uninstall.sh`, delete the repo folder, clone again and run the install script. The current branch will be maintained.
216216
217217
## 7. Uninstallation
218218

@@ -365,58 +365,55 @@ Here's a few things you could try:
365365
<p>
366366

367367
```console
368-
user@pi0w:~ $ sudo service bluetooth_2_usb stop && sudo bluetooth_2_usb -i logi,a1:b2:c3:d4:e5:f6,/dev/input/event3 -d ; sudo service bluetooth_2_usb start
369-
23-12-06 14:19:36 [DEBUG] CLI args: Namespace(device_ids=['logi', 'a1:b2:c3:d4:e5:f6', '/dev/input/event3'], auto_discover=False, debug=True, log_to_file=False, log_path='/var/log/bluetooth_2_usb/bluetooth_2_usb.log', version=False, list_devices=False)
370-
23-12-06 14:19:36 [DEBUG] Logging to stdout
371-
23-12-06 14:19:36 [INFO] Launching Bluetooth 2 USB v0.6.7
372-
23-12-06 14:19:36 [DEBUG] Available USB devices: [Mouse gadget (/dev/hidg0), Keyboard gadget (/dev/hidg1), Consumer control gadget (/dev/hidg2)]
373-
23-12-06 14:19:36 [INFO] Discovering input devices...
374-
23-12-06 14:19:36 [DEBUG] Relaying devices with matching name "logi" or MAC "a1:b2:c3:d4:e5:f6" or path "/dev/input/event3"
375-
23-12-06 14:19:39 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "a1:b2:c3:d4:e5:f6"
376-
23-12-06 14:19:42 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "a1:b2:c3:d4:e5:f6"
368+
user@pi0w:~ $ sudo service bluetooth_2_usb stop && sudo bluetooth_2_usb -ad ; sudo service bluetooth_2_usb start
369+
23-12-12 13:03:28 [DEBUG] CLI args: Namespace(device_ids=None, auto_discover=True, debug=True, log_to_file=False, log_path='/var/log/bluetooth_2_usb/bluetooth_2_usb.log', version=False, list_devices=False)
370+
23-12-12 13:03:28 [DEBUG] Logging to stdout
371+
23-12-12 13:03:28 [INFO] Launching Bluetooth 2 USB v0.6.7
372+
23-12-12 13:03:28 [INFO] Discovering input devices...
373+
23-12-12 13:03:28 [DEBUG] Auto-discovery enabled. Relaying all input devices.
374+
23-12-12 13:03:28 [DEBUG] Initializing USB gadgets...
375+
23-12-12 13:03:31 [DEBUG] Enabled USB gadgets: [Mouse gadget (/dev/hidg0), Keyboard gadget (/dev/hidg1), Consumer control gadget (/dev/hidg2)]
376+
23-12-12 13:03:31 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "b8:27:eb:be:dc:81"
377+
23-12-12 13:03:31 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "b8:27:eb:be:dc:81"
378+
23-12-12 13:03:31 [INFO] Activated relay for device /dev/input/event0, name "vc4-hdmi", phys "vc4-hdmi/input0"
377379
>>> Manually switched Pi's Bluetooth off <<<
378-
23-12-06 14:20:44 [CRITICAL] Connection to AceRK Keyboard lost [OSError(19, 'No such device')]
379-
23-12-06 14:20:44 [CRITICAL] Connection to AceRK Mouse lost [OSError(19, 'No such device')]
380+
23-12-12 13:04:28 [CRITICAL] Connection to AceRK Keyboard lost [OSError(19, 'No such device')]
381+
23-12-12 13:04:28 [CRITICAL] Connection to AceRK Mouse lost [OSError(19, 'No such device')]
380382
>>> Manually switched Pi's Bluetooth back on <<<
381-
23-12-06 14:20:53 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "a1:b2:c3:d4:e5:f6"
382-
23-12-06 14:20:56 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "a1:b2:c3:d4:e5:f6"
383-
23-12-06 14:21:08 [DEBUG] Received event at 1701868868.638665, code 02, type 17, val 01 from AceRK Keyboard
384-
23-12-06 14:21:08 [DEBUG] Received event at 1701868868.638665, code 04, type 04, val 458756 from AceRK Keyboard
385-
23-12-06 14:21:08 [DEBUG] Received key event at 1701868868.638665, 30 (KEY_A), down from AceRK Keyboard
386-
23-12-06 14:21:08 [DEBUG] Converted evdev scancode 0x1E (KEY_A) to HID UsageID 0x04 (A)
387-
23-12-06 14:21:08 [DEBUG] Pressing A (0x04) on /dev/hidg1
388-
23-12-06 14:21:08 [DEBUG] Received synchronization event at 1701868868.638665, SYN_REPORT from AceRK Keyboard
389-
23-12-06 14:21:08 [DEBUG] Received event at 1701868868.639487, code 04, type 04, val 458756 from AceRK Keyboard
390-
23-12-06 14:21:08 [DEBUG] Received key event at 1701868868.639487, 30 (KEY_A), up from AceRK Keyboard
391-
23-12-06 14:21:08 [DEBUG] Converted evdev scancode 0x1E (KEY_A) to HID UsageID 0x04 (A)
392-
23-12-06 14:21:08 [DEBUG] Releasing A (0x04) on /dev/hidg1
393-
23-12-06 14:21:08 [DEBUG] Received synchronization event at 1701868868.639487, SYN_REPORT from AceRK Keyboard
394-
23-12-06 14:21:58 [DEBUG] Received event at 1701868918.851380, code 04, type 04, val 786666 from AceRK Keyboard
395-
23-12-06 14:21:58 [DEBUG] Received key event at 1701868918.851380, 114 (KEY_VOLUMEDOWN), down from AceRK Keyboard
396-
23-12-06 14:21:58 [DEBUG] Converted evdev scancode 0x72 (KEY_VOLUMEDOWN) to HID UsageID 0xEA (VOLUME_DECREMENT)
397-
23-12-06 14:21:58 [DEBUG] Pressing VOLUME_DECREMENT (0xEA) on /dev/hidg2
398-
23-12-06 14:21:58 [DEBUG] Received synchronization event at 1701868918.851380, SYN_REPORT from AceRK Keyboard
399-
23-12-06 14:21:58 [DEBUG] Received event at 1701868918.852051, code 04, type 04, val 786666 from AceRK Keyboard
400-
23-12-06 14:21:58 [DEBUG] Received key event at 1701868918.852051, 114 (KEY_VOLUMEDOWN), up from AceRK Keyboard
401-
23-12-06 14:21:58 [DEBUG] Converted evdev scancode 0x72 (KEY_VOLUMEDOWN) to HID UsageID 0xEA (VOLUME_DECREMENT)
402-
23-12-06 14:21:58 [DEBUG] Releasing VOLUME_DECREMENT (0xEA) on /dev/hidg2
403-
23-12-06 14:21:58 [DEBUG] Received synchronization event at 1701868918.852051, SYN_REPORT from AceRK Keyboard
404-
23-12-06 14:22:00 [DEBUG] Received event at 1701868920.411452, code 04, type 04, val 589826 from AceRK Mouse
405-
23-12-06 14:22:00 [DEBUG] Received key event at 1701868920.411452, 273 (BTN_RIGHT), down from AceRK Mouse
406-
23-12-06 14:22:00 [DEBUG] Converted evdev scancode 0x111 (BTN_RIGHT) to HID UsageID 0x02 (RIGHT)
407-
23-12-06 14:22:00 [DEBUG] Pressing RIGHT (0x02) on /dev/hidg0
408-
23-12-06 14:22:00 [DEBUG] Received synchronization event at 1701868920.411452, SYN_REPORT from AceRK Mouse
409-
23-12-06 14:22:00 [DEBUG] Received event at 1701868920.460166, code 04, type 04, val 589826 from AceRK Mouse
410-
23-12-06 14:22:00 [DEBUG] Received key event at 1701868920.460166, 273 (BTN_RIGHT), up from AceRK Mouse
411-
23-12-06 14:22:00 [DEBUG] Converted evdev scancode 0x111 (BTN_RIGHT) to HID UsageID 0x02 (RIGHT)
412-
23-12-06 14:22:00 [DEBUG] Releasing RIGHT (0x02) on /dev/hidg0
413-
23-12-06 14:22:00 [DEBUG] Received synchronization event at 1701868920.460166, SYN_REPORT from AceRK Mouse
414-
23-12-06 14:22:01 [DEBUG] Received relative axis event at 1701868921.288916, REL_X from AceRK Mouse
415-
23-12-06 14:22:01 [DEBUG] Moving mouse /dev/hidg0 (x=125, y=0, mwheel=0)
416-
23-12-06 14:22:01 [DEBUG] Received synchronization event at 1701868921.288916, SYN_REPORT from AceRK Mouse
417-
^C23-12-06 14:22:12 [INFO] Exiting gracefully. Received signal: 2, frame: <frame at 0xb5f40df8, file '/usr/lib/python3.11/selectors.py', line 468, code select>
418-
23-12-06 14:22:12 [CRITICAL] AceRK Keyboard was cancelled
419-
23-12-06 14:22:12 [CRITICAL] AceRK Mouse was cancelled
383+
23-12-12 13:04:32 [INFO] Activated relay for device /dev/input/event2, name "AceRK Mouse", phys "b8:27:eb:be:dc:81"
384+
23-12-12 13:04:32 [INFO] Activated relay for device /dev/input/event1, name "AceRK Keyboard", phys "b8:27:eb:be:dc:81"
385+
23-12-12 13:05:17 [DEBUG] Received event at 1702382717.714878, code 04, type 04, val 458807 from AceRK Keyboard
386+
23-12-12 13:05:17 [DEBUG] Received key event at 1702382717.714878, 52 (KEY_DOT), down from AceRK Keyboard
387+
23-12-12 13:05:17 [DEBUG] Converted evdev scancode 0x34 (KEY_DOT) to HID UsageID 0x37 (PERIOD)
388+
23-12-12 13:05:17 [DEBUG] Pressing PERIOD (0x37) on /dev/hidg1
389+
23-12-12 13:05:17 [DEBUG] Received synchronization event at 1702382717.714878, SYN_REPORT from AceRK Keyboard
390+
.23-12-12 13:05:17 [DEBUG] Received event at 1702382717.763608, code 04, type 04, val 458807 from AceRK Keyboard
391+
23-12-12 13:05:17 [DEBUG] Received key event at 1702382717.763608, 52 (KEY_DOT), up from AceRK Keyboard
392+
23-12-12 13:05:17 [DEBUG] Converted evdev scancode 0x34 (KEY_DOT) to HID UsageID 0x37 (PERIOD)
393+
23-12-12 13:05:17 [DEBUG] Releasing PERIOD (0x37) on /dev/hidg1
394+
23-12-12 13:05:17 [DEBUG] Received synchronization event at 1702382717.763608, SYN_REPORT from AceRK Keyboard
395+
23-12-12 13:05:41 [DEBUG] Received relative axis event at 1702382741.553669, REL_X from AceRK Mouse
396+
23-12-12 13:05:41 [DEBUG] Moving mouse /dev/hidg0 (x=50, y=0, mwheel=0)
397+
23-12-12 13:05:41 [DEBUG] Received synchronization event at 1702382741.553669, SYN_REPORT from AceRK Mouse
398+
23-12-12 13:05:42 [DEBUG] Received event at 1702382742.382372, code 04, type 04, val 589826 from AceRK Mouse
399+
23-12-12 13:05:42 [DEBUG] Received key event at 1702382742.382372, 273 (BTN_RIGHT), down from AceRK Mouse
400+
23-12-12 13:05:42 [DEBUG] Converted evdev scancode 0x111 (BTN_RIGHT) to HID UsageID 0x02 (RIGHT)
401+
23-12-12 13:05:42 [DEBUG] Pressing RIGHT (0x02) on /dev/hidg0
402+
23-12-12 13:05:42 [DEBUG] Received synchronization event at 1702382742.382372, SYN_REPORT from AceRK Mouse
403+
23-12-12 13:05:42 [DEBUG] Received event at 1702382742.383094, code 04, type 04, val 589826 from AceRK Mouse
404+
23-12-12 13:05:42 [DEBUG] Received key event at 1702382742.383094, 273 (BTN_RIGHT), up from AceRK Mouse
405+
23-12-12 13:05:42 [DEBUG] Converted evdev scancode 0x111 (BTN_RIGHT) to HID UsageID 0x02 (RIGHT)
406+
23-12-12 13:05:42 [DEBUG] Releasing RIGHT (0x02) on /dev/hidg0
407+
23-12-12 13:05:42 [DEBUG] Received synchronization event at 1702382742.383094, SYN_REPORT from AceRK Mouse
408+
23-12-12 13:05:43 [DEBUG] Received relative axis event at 1702382743.259934, REL_WHEEL from AceRK Mouse
409+
23-12-12 13:05:43 [DEBUG] Moving mouse /dev/hidg0 (x=0, y=0, mwheel=1)
410+
23-12-12 13:05:43 [DEBUG] Received relative axis event at 1702382743.259934, REL_WHEEL_HI_RES from AceRK Mouse
411+
23-12-12 13:05:43 [DEBUG] Moving mouse /dev/hidg0 (x=0, y=0, mwheel=0)
412+
23-12-12 13:05:43 [DEBUG] Received synchronization event at 1702382743.259934, SYN_REPORT from AceRK Mouse
413+
^C23-12-12 13:06:26 [INFO] Exiting gracefully. Received signal: 2, frame: <frame at 0xb5ed5028, file '/usr/lib/python3.11/selectors.py', line 468, code select>
414+
23-12-12 13:06:26 [CRITICAL] vc4-hdmi was cancelled
415+
23-12-12 13:06:26 [CRITICAL] AceRK Keyboard was cancelled
416+
23-12-12 13:06:26 [CRITICAL] AceRK Mouse was cancelled
420417
```
421418

422419
</p>

bluetooth_2_usb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from src.bluetooth_2_usb.args import parse_args
1111
from src.bluetooth_2_usb.logging import add_file_handler, get_logger
12-
from src.bluetooth_2_usb.relay import RelayController, list_readable_devices
12+
from src.bluetooth_2_usb.relay import RelayController, list_input_devices
1313

1414

1515
_logger = get_logger()
@@ -53,7 +53,7 @@ async def _main() -> NoReturn:
5353

5454

5555
def _list_devices():
56-
for dev in list_readable_devices():
56+
for dev in list_input_devices():
5757
print(f"{dev.name}\t{dev.uniq if dev.uniq else dev.phys}\t{dev.path}")
5858
_exit_safely()
5959

src/bluetooth_2_usb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
DeviceIdentifier,
1717
DeviceRelay,
1818
RelayController,
19-
list_readable_devices,
19+
list_input_devices,
2020
)

src/bluetooth_2_usb/relay.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,30 @@
2929
NAME = "name"
3030

3131

32-
def list_readable_devices() -> list[InputDevice]:
32+
def list_input_devices() -> list[InputDevice]:
3333
return [InputDevice(path) for path in list_devices()]
3434

3535

36-
def init_usb_devices() -> None:
36+
def init_usb_gadgets() -> None:
37+
_logger.debug("Initializing USB gadgets...")
3738
usb_hid.enable(
3839
[
3940
Device.MOUSE,
4041
Device.KEYBOARD,
4142
Device.CONSUMER_CONTROL,
4243
]
4344
)
44-
_logger.debug(f"Available USB devices: {usb_hid.devices}")
4545
global _keyboard_gadget, _mouse_gadget, _consumer_gadget
4646
_keyboard_gadget = Keyboard(usb_hid.devices)
4747
_mouse_gadget = Mouse(usb_hid.devices)
4848
_consumer_gadget = ConsumerControl(usb_hid.devices)
49+
_logger.debug(f"Enabled USB gadgets: {usb_hid.devices}")
50+
51+
52+
def all_gadgets_ready():
53+
return all(
54+
dev is not None for dev in (_keyboard_gadget, _mouse_gadget, _consumer_gadget)
55+
)
4956

5057

5158
class DeviceIdentifier:
@@ -101,6 +108,8 @@ def matches(self, device: InputDevice) -> bool:
101108
class DeviceRelay:
102109
def __init__(self, input_device: InputDevice):
103110
self._input_device = input_device
111+
if not all_gadgets_ready():
112+
init_usb_gadgets()
104113

105114
@property
106115
def input_device(self) -> InputDevice:
@@ -171,7 +180,6 @@ class RelayController:
171180
def __init__(
172181
self, device_identifiers: list[str] = None, auto_discover: bool = False
173182
) -> None:
174-
init_usb_devices()
175183
if not device_identifiers:
176184
device_identifiers = []
177185
self._device_ids = [DeviceIdentifier(id) for id in device_identifiers]
@@ -200,7 +208,7 @@ async def _async_discover_devices_loop(self) -> AsyncGenerator[InputDevice, None
200208
all_device_ids = " or ".join(repr(id) for id in self._device_ids)
201209
_logger.debug(f"Relaying devices with matching {all_device_ids}")
202210
while True:
203-
for device in list_readable_devices():
211+
for device in list_input_devices():
204212
if self._should_relay(device):
205213
yield device
206214
await asyncio.sleep(0.1)

0 commit comments

Comments
 (0)