|
7 | 7 | import os
|
8 | 8 | import struct
|
9 | 9 | from typing import Awaitable, Callable, List, Optional, TypeVar
|
| 10 | +from uuid import UUID |
10 | 11 |
|
11 | 12 | import reactivex.operators as op
|
12 | 13 | import semver
|
|
17 | 18 | from reactivex.subject import BehaviorSubject, Subject
|
18 | 19 | from tqdm.auto import tqdm
|
19 | 20 | from tqdm.contrib.logging import logging_redirect_tqdm
|
| 21 | +from usb.control import get_descriptor |
| 22 | +from usb.core import Device as USBDevice |
| 23 | +from usb.core import Endpoint |
| 24 | +from usb.util import ENDPOINT_IN, ENDPOINT_OUT, endpoint_direction, find_descriptor |
20 | 25 |
|
21 | 26 | from ..ble.lwp3.bytecodes import HubKind
|
22 | 27 | from ..ble.nus import NUS_RX_UUID, NUS_TX_UUID
|
@@ -705,3 +710,91 @@ async def write_gatt_char(self, uuid: str, data, response: bool) -> None:
|
705 | 710 |
|
706 | 711 | async def start_notify(self, uuid: str, callback: Callable) -> None:
|
707 | 712 | return await self._client.start_notify(uuid, callback)
|
| 713 | + |
| 714 | + |
| 715 | +class PybricksHubUSB(PybricksHub): |
| 716 | + _device: USBDevice |
| 717 | + _ep_in: Endpoint |
| 718 | + _ep_out: Endpoint |
| 719 | + _notify_callbacks = {} |
| 720 | + |
| 721 | + def __init__(self, device: USBDevice): |
| 722 | + super().__init__() |
| 723 | + self._device = device |
| 724 | + |
| 725 | + async def _client_connect(self) -> bool: |
| 726 | + self._device.set_configuration() |
| 727 | + |
| 728 | + # Save input and output endpoints |
| 729 | + cfg = self._device.get_active_configuration() |
| 730 | + intf = cfg[(0, 0)] |
| 731 | + self._ep_in = find_descriptor( |
| 732 | + intf, |
| 733 | + custom_match=lambda e: endpoint_direction(e.bEndpointAddress) |
| 734 | + == ENDPOINT_IN, |
| 735 | + ) |
| 736 | + self._ep_out = find_descriptor( |
| 737 | + intf, |
| 738 | + custom_match=lambda e: endpoint_direction(e.bEndpointAddress) |
| 739 | + == ENDPOINT_OUT, |
| 740 | + ) |
| 741 | + |
| 742 | + # Set write size to endpoint packet size minus length of UUID |
| 743 | + self._max_write_size = self._ep_out.wMaxPacketSize - 16 |
| 744 | + |
| 745 | + # Get length of BOS descriptor |
| 746 | + bos_descriptor = get_descriptor(self._device, 5, 0x0F, 0) |
| 747 | + (ofst, _, bos_len, _) = struct.unpack("<BBHB", bos_descriptor) |
| 748 | + |
| 749 | + # Get full BOS descriptor |
| 750 | + bos_descriptor = get_descriptor(self._device, bos_len, 0x0F, 0) |
| 751 | + |
| 752 | + while ofst < bos_len: |
| 753 | + (len, desc_type, cap_type) = struct.unpack_from( |
| 754 | + "<BBB", bos_descriptor, offset=ofst |
| 755 | + ) |
| 756 | + |
| 757 | + if desc_type != 0x10: |
| 758 | + logger.error("Expected Device Capability descriptor") |
| 759 | + exit(1) |
| 760 | + |
| 761 | + # Look for platform descriptors |
| 762 | + if cap_type == 0x05: |
| 763 | + uuid_bytes = bos_descriptor[ofst + 4 : ofst + 4 + 16] |
| 764 | + uuid_str = str(UUID(bytes_le=bytes(uuid_bytes))) |
| 765 | + |
| 766 | + if uuid_str == FW_REV_UUID: |
| 767 | + fw_version = bytearray( |
| 768 | + bos_descriptor[ofst + 20 : ofst + len - 1] |
| 769 | + ) # Remove null-terminator |
| 770 | + self.fw_version = Version(fw_version.decode()) |
| 771 | + |
| 772 | + elif uuid_str == SW_REV_UUID: |
| 773 | + self._protocol_version = bytearray( |
| 774 | + bos_descriptor[ofst + 20 : ofst + len - 1] |
| 775 | + ) # Remove null-terminator |
| 776 | + |
| 777 | + elif uuid_str == PYBRICKS_HUB_CAPABILITIES_UUID: |
| 778 | + caps = bytearray(bos_descriptor[ofst + 20 : ofst + len]) |
| 779 | + ( |
| 780 | + _, |
| 781 | + self._capability_flags, |
| 782 | + self._max_user_program_size, |
| 783 | + ) = unpack_hub_capabilities(caps) |
| 784 | + |
| 785 | + ofst += len |
| 786 | + |
| 787 | + return True |
| 788 | + |
| 789 | + async def _client_disconnect(self) -> bool: |
| 790 | + self._handle_disconnect() |
| 791 | + |
| 792 | + async def read_gatt_char(self, uuid: str) -> bytearray: |
| 793 | + return None |
| 794 | + |
| 795 | + async def write_gatt_char(self, uuid: str, data, response: bool) -> None: |
| 796 | + self._ep_out.write(UUID(uuid).bytes_le + data) |
| 797 | + # TODO: Handle response |
| 798 | + |
| 799 | + async def start_notify(self, uuid: str, callback: Callable) -> None: |
| 800 | + self._notify_callbacks[uuid] = callback |
0 commit comments