|
19 | 19 | import os |
20 | 20 | from collections import defaultdict |
21 | 21 | from collections.abc import Callable, Coroutine, MutableMapping |
| 22 | +from functools import partial |
22 | 23 | from typing import Any, NamedTuple, Optional, cast |
23 | 24 | from weakref import WeakKeyDictionary |
24 | 25 |
|
@@ -154,6 +155,12 @@ class DeviceWatcher(NamedTuple): |
154 | 155 | } |
155 | 156 |
|
156 | 157 |
|
| 158 | +def get_max_write_without_response_size(char_props: GattCharacteristic1) -> int: |
| 159 | + # "MTU" property was added in BlueZ 5.62, otherwise fall |
| 160 | + # back to minimum MTU according to Bluetooth spec. |
| 161 | + return char_props.get("MTU", 23) - 3 |
| 162 | + |
| 163 | + |
157 | 164 | class BlueZManager: |
158 | 165 | """ |
159 | 166 | BlueZ D-Bus object manager. |
@@ -244,9 +251,12 @@ async def async_init(self) -> None: |
244 | 251 | # dbus-next will destroy the underlying file descriptors |
245 | 252 | # when the previous one is closed in its finalizer. |
246 | 253 | bus = MessageBus(bus_type=BusType.SYSTEM, auth=get_dbus_authenticator()) |
247 | | - await bus.connect() |
248 | 254 |
|
249 | 255 | try: |
| 256 | + # We need to call bus.disconnect() even when bus.connect() fails in |
| 257 | + # order to release the file handles created in the constructor. |
| 258 | + await bus.connect() |
| 259 | + |
250 | 260 | # Add signal listeners |
251 | 261 |
|
252 | 262 | bus.add_message_handler(self._parse_msg) |
@@ -709,9 +719,11 @@ async def get_services( |
709 | 719 | extract_service_handle_from_path(char_path), |
710 | 720 | char_props["UUID"], |
711 | 721 | char_props["Flags"], |
712 | | - # "MTU" property was added in BlueZ 5.62, otherwise fall |
713 | | - # back to minimum MTU according to Bluetooth spec. |
714 | | - lambda: char_props.get("MTU", 23) - 3, |
| 722 | + # Because `char_props` is a loop varialbe, we cannot |
| 723 | + # directly bind a closure (i.e. lambda) to it; |
| 724 | + # instead, we let `functools.partial` create a new |
| 725 | + # function frame to close over at each iteration. |
| 726 | + partial(get_max_write_without_response_size, char_props), |
715 | 727 | service, |
716 | 728 | ) |
717 | 729 |
|
|
0 commit comments