A Bluetooth connect that fails says which step failed
One "requires a PIN" sentence covered every GATT refusal, and a link that never opened read as "didn't answer as a MeshCore device". A Heltec V4 user whose Windows bond had gone stale was told to pair a device Settings showed as paired. Each failure point now has its own message and remedy: link never opened, stale Windows bond, wrong PIN, device refused to pair (with the WinRT status), paired but still refused, a BLE probe timeout by stage, and link up but no identity reply. The PIN dialog shows the diagnosis instead of calling every refusal a rejected PIN, and each case is logged at warning so a log sent in from another machine names it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
40183e89ca
commit
ed2ad3b7ab
5 files changed
+331
-34
No files matched your search
+226
-24
@@ -22,7 +22,7 @@ import sys
|
||||
from abc import ABC, abstractmethod
|
||||
from collections.abc import AsyncIterator, Callable, Iterable
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import replace
|
||||
from dataclasses import dataclass, replace
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
@@ -226,8 +226,27 @@ class DeviceAuthenticationError(DeviceCommandError):
|
||||
picker opens a PIN dialog and retries, while the scripted CLI (which catches the base
|
||||
class) prints the message and bails, since it can't prompt. The message already names the
|
||||
fix (``--ble-pin`` and OS pairing).
|
||||
|
||||
The one exception covers several different failures — no PIN at all, a stale bond the OS
|
||||
still believes in, a wrong PIN, a pairing the device itself refused — and each has its own
|
||||
remedy, so the message names which one happened (see
|
||||
:meth:`MeshCoreDevice._ble_auth_failure`) rather than one sentence covering all of them.
|
||||
|
||||
Attributes:
|
||||
hint: The same diagnosis cut to one line for the PIN dialog, which re-asks under it;
|
||||
empty when there is nothing to say beyond the dialog's own question.
|
||||
"""
|
||||
|
||||
def __init__(self, message: str, *, hint: str = "") -> None:
|
||||
"""Build the error.
|
||||
|
||||
Args:
|
||||
message: The full, actionable sentence — what failed and what to do.
|
||||
hint: The dialog-sized version of it (see the class attributes).
|
||||
"""
|
||||
super().__init__(message)
|
||||
self.hint = hint
|
||||
|
||||
|
||||
class FloodScopeError(DeviceCommandError):
|
||||
"""The companion could not send under the scope a channel send asked for — so it didn't.
|
||||
@@ -519,6 +538,52 @@ _BLE_AUTH_HINTS = (
|
||||
"insufficient authorization",
|
||||
"insufficient encryption",
|
||||
"not paired",
|
||||
# The pairing step itself failing, rather than the subscribe it guards: bleak's
|
||||
# ``pair()`` ("Could not pair with device: …") and BlueZ's AuthenticationFailed. Without
|
||||
# these a refused pairing fell through as an anonymous failure and was reported as a
|
||||
# device that "didn't answer as a MeshCore device".
|
||||
"could not pair",
|
||||
"authentication failed",
|
||||
"authenticationfailed",
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _BlePairing:
|
||||
"""What the last Windows PIN-pairing attempt found, kept so a refusal can say why.
|
||||
|
||||
The pairing step is best-effort and returns a bool (see
|
||||
:meth:`MeshCoreDevice._pair_ble_windows`), which is all the connect needs to decide
|
||||
whether to retry — but a bool can't tell the reader whether the PIN was wrong, the device
|
||||
refused, or Windows couldn't reach it, and those have different fixes.
|
||||
|
||||
Attributes:
|
||||
outcome: ``"reused"`` (an existing bond was trusted), ``"paired"``, ``"absent"``
|
||||
(Windows couldn't reach the device), ``"failed"`` (the ceremony returned a
|
||||
non-success status), or ``"error"`` (the WinRT call raised).
|
||||
status: The ceremony's status name, lowercase with spaces (``"authentication
|
||||
failure"``), or the exception text for ``"error"``; empty otherwise.
|
||||
"""
|
||||
|
||||
outcome: str
|
||||
status: str = ""
|
||||
|
||||
|
||||
#: Pairing statuses that mean the PIN itself was wrong.
|
||||
_PAIRING_WRONG_PIN = frozenset({"authentication failure", "invalid ceremony data"})
|
||||
|
||||
#: Pairing statuses that mean the device (not the PIN) turned the pairing down — busy with
|
||||
#: another host, holding an old bond, or simply not answering in time.
|
||||
_PAIRING_REFUSED = frozenset(
|
||||
{
|
||||
"connection rejected",
|
||||
"too many connections",
|
||||
"remote device has association",
|
||||
"not ready to pair",
|
||||
"rejected by handler",
|
||||
"authentication timeout",
|
||||
"protection level could not be met",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -1369,6 +1434,9 @@ class MeshCoreDevice(Device):
|
||||
self._transport = transport
|
||||
self._address = address
|
||||
self._pin = pin
|
||||
#: What the last Windows PIN-pairing attempt found (``None`` when none ran), so an
|
||||
#: authentication refusal can name the failure rather than guess at it.
|
||||
self._ble_pairing: _BlePairing | None = None
|
||||
self._ble_device = ble_device
|
||||
self._host = host
|
||||
self._tcp_port = tcp_port
|
||||
@@ -1515,7 +1583,7 @@ class MeshCoreDevice(Device):
|
||||
# putting its Passkey dialog up as we unwind. Wait for the person to answer it.
|
||||
if allow_repair and sys.platform == "darwin":
|
||||
return await self._open_ble_after_macos_pairing(mesh_core, exc)
|
||||
raise DeviceAuthenticationError(self._ble_auth_message()) from exc
|
||||
raise await self._ble_auth_failure() from exc
|
||||
|
||||
async def _open_ble_after_macos_pairing(self, mesh_core, cause: BaseException): # type: ignore[no-untyped-def]
|
||||
"""Re-open the link while macOS is running the Passkey dialog it just raised.
|
||||
@@ -1556,7 +1624,7 @@ class MeshCoreDevice(Device):
|
||||
attempt + 1,
|
||||
_BLE_MACOS_PAIRING_ATTEMPTS,
|
||||
)
|
||||
raise DeviceAuthenticationError(self._ble_auth_message()) from cause
|
||||
raise await self._ble_auth_failure() from cause
|
||||
|
||||
async def _create_ble_with_retry(self, mesh_core): # type: ignore[no-untyped-def]
|
||||
"""Open the owned BLE client, retrying the transport-level failures that are transient.
|
||||
@@ -1585,7 +1653,9 @@ class MeshCoreDevice(Device):
|
||||
the peripheral never answered the identity handshake.
|
||||
|
||||
Raises:
|
||||
ConnectionError: If every attempt failed to open the link.
|
||||
DeviceCommandError: If every attempt failed to open the link — said as such,
|
||||
because this is the one failure where the device was never reached at all,
|
||||
and "didn't answer as a MeshCore device" sent people looking at the firmware.
|
||||
"""
|
||||
last_exc: ConnectionError | None = None
|
||||
for attempt in range(_BLE_CONNECT_ATTEMPTS):
|
||||
@@ -1603,7 +1673,14 @@ class MeshCoreDevice(Device):
|
||||
)
|
||||
last_exc = exc
|
||||
assert last_exc is not None # the loop always runs; only ConnectionError falls through
|
||||
raise last_exc
|
||||
where = self._address or "the selected Bluetooth device"
|
||||
_log.warning("BLE link to %s never opened: %s", where, last_exc)
|
||||
raise DeviceCommandError(
|
||||
f"couldn't open a Bluetooth link to {where} ({_BLE_CONNECT_ATTEMPTS} tries) — it "
|
||||
"wasn't found, or didn't accept the connection. It may be out of range, powered "
|
||||
"off, or connected to a phone or another computer (a companion takes one "
|
||||
"connection at a time)."
|
||||
) from last_exc
|
||||
|
||||
async def _connect_owned_ble(self, mesh_core): # type: ignore[no-untyped-def]
|
||||
"""Build the meshcore BLE client here and connect it, so we own its teardown.
|
||||
@@ -1786,14 +1863,18 @@ class MeshCoreDevice(Device):
|
||||
_log.debug("BLE PIN pairing unavailable (winrt import failed): %s", exc)
|
||||
return False
|
||||
|
||||
# Every exit records what it found in ``_ble_pairing``, so a refusal further on can
|
||||
# say which of these it was — the bool alone is only enough to decide on a retry.
|
||||
device = None
|
||||
try:
|
||||
device = await BluetoothLEDevice.from_bluetooth_address_async(address)
|
||||
if device is None:
|
||||
self._ble_pairing = _BlePairing("absent")
|
||||
return False # out of range / not connectable right now
|
||||
pairing = device.device_information.pairing
|
||||
if pairing.is_paired:
|
||||
if not force:
|
||||
self._ble_pairing = _BlePairing("reused")
|
||||
return True # trust the existing (authenticated) bond — fast path
|
||||
# Tear the stale bond down, then re-fetch: the pairing object is a snapshot and
|
||||
# won't reflect the unpair, so a fresh device_information is needed to re-pair.
|
||||
@@ -1801,6 +1882,7 @@ class MeshCoreDevice(Device):
|
||||
MeshCoreDevice._close_ble_device(device) # release the pre-unpair handle
|
||||
device = await BluetoothLEDevice.from_bluetooth_address_async(address)
|
||||
if device is None:
|
||||
self._ble_pairing = _BlePairing("absent")
|
||||
return False
|
||||
pairing = device.device_information.pairing
|
||||
custom = pairing.custom
|
||||
@@ -1823,10 +1905,19 @@ class MeshCoreDevice(Device):
|
||||
int(DevicePairingResultStatus.PAIRED),
|
||||
int(DevicePairingResultStatus.ALREADY_PAIRED),
|
||||
)
|
||||
_log.debug("BLE ProvidePin pairing for %s: status=%d ok=%s", self._address, status, ok)
|
||||
name = str(getattr(result.status, "name", status)).lower().replace("_", " ")
|
||||
if ok:
|
||||
self._ble_pairing = _BlePairing("paired")
|
||||
_log.debug("BLE ProvidePin pairing for %s: %s", self._address, name)
|
||||
else:
|
||||
# A warning, not debug: this is the line that tells a wrong PIN from a device
|
||||
# that refused to pair, and the default log level would otherwise hide it.
|
||||
self._ble_pairing = _BlePairing("failed", name)
|
||||
_log.warning("BLE PIN pairing with %s failed: %s", self._address, name)
|
||||
return ok
|
||||
except Exception as exc: # noqa: BLE001 - best-effort; caller falls through on False
|
||||
_log.debug("BLE ProvidePin pairing attempt failed for %s: %s", self._address, exc)
|
||||
self._ble_pairing = _BlePairing("error", str(exc))
|
||||
_log.warning("BLE PIN pairing with %s raised: %s", self._address, exc)
|
||||
return False
|
||||
finally:
|
||||
MeshCoreDevice._close_ble_device(device)
|
||||
@@ -1969,14 +2060,54 @@ class MeshCoreDevice(Device):
|
||||
# it the device stays "connected" after the unpair and won't re-pair until rebooted.
|
||||
MeshCoreDevice._close_ble_device(device)
|
||||
|
||||
def _ble_auth_message(self) -> str:
|
||||
"""A clean, actionable error for a Bluetooth companion that requires a PIN/bond.
|
||||
async def _ble_os_bonded(self) -> bool:
|
||||
"""Whether Windows holds a bond for this device — asked only to explain a refusal.
|
||||
|
||||
Distinguishes "you gave the wrong PIN" from "you gave none at all", and points at
|
||||
both fixes: MeshTerm's ``--ble-pin`` and the one-time OS pairing that Windows needs
|
||||
before an authenticated characteristic can be subscribed.
|
||||
An instance hook over :meth:`is_ble_paired` so tests can answer it without the OS.
|
||||
"""
|
||||
return await MeshCoreDevice.is_ble_paired(self._address or "")
|
||||
|
||||
async def _ble_auth_failure(self) -> DeviceAuthenticationError:
|
||||
"""Name *which* authentication failure this was, and what fixes it.
|
||||
|
||||
The GATT refusal looks the same on the wire whatever caused it, but the causes don't
|
||||
share a remedy, and one sentence covering all of them sent people to the wrong one — a
|
||||
stale Windows bond, say, was reported as "requires a PIN" to someone whose device
|
||||
Windows plainly showed as paired. So the error is built from what is actually known:
|
||||
|
||||
* macOS — the OS runs the pairing in its own dialog; say so.
|
||||
* No PIN, and Windows holds a bond — the bond is stale (the device was reflashed,
|
||||
reset, or given a new PIN since), and re-pairing is the fix.
|
||||
* No PIN, no bond — the device needs its PIN.
|
||||
* A PIN, and the Windows pairing ceremony reported a status — a wrong PIN, a device
|
||||
that refused to pair, or some other status, each named.
|
||||
* A PIN, the pairing succeeded, and the device still refused — it is holding an old
|
||||
bond for this computer.
|
||||
* A PIN and nothing more known (Linux, or no WinRT) — the PIN was rejected.
|
||||
|
||||
Every case is also logged, so a log sent in from another machine says which it was.
|
||||
|
||||
Returns:
|
||||
The error to raise, carrying a one-line :attr:`~DeviceAuthenticationError.hint`
|
||||
for the PIN dialog.
|
||||
"""
|
||||
where = self._address or "the selected Bluetooth device"
|
||||
# Windows' bond changes the message in exactly one case, so it is asked only there.
|
||||
bonded = sys.platform == "win32" and not self._pin and await self._ble_os_bonded()
|
||||
message, hint = self._ble_auth_diagnosis(where, bonded)
|
||||
_log.warning("BLE connect to %s refused: %s", where, message)
|
||||
return DeviceAuthenticationError(message, hint=hint)
|
||||
|
||||
def _ble_auth_diagnosis(self, where: str, os_bonded: bool) -> tuple[str, str]:
|
||||
"""The message and dialog hint for :meth:`_ble_auth_failure` (see there for the cases).
|
||||
|
||||
Args:
|
||||
where: The device, as the message names it.
|
||||
os_bonded: Whether Windows holds a bond (asked only when no PIN was given).
|
||||
|
||||
Returns:
|
||||
``(message, hint)``.
|
||||
"""
|
||||
if sys.platform == "darwin":
|
||||
# macOS collects the code itself, in its own dialog, so --ble-pin is not the
|
||||
# remedy here and naming it would send the reader somewhere that cannot help.
|
||||
@@ -1985,29 +2116,76 @@ class MeshCoreDevice(Device):
|
||||
"rather than through MeshTerm — enter the 6-digit code shown on the device "
|
||||
"(or in the MeshCore app) when it appears, and the bond is remembered for "
|
||||
"next time. If no dialog appeared, check that Bluetooth is allowed for this "
|
||||
"terminal in System Settings > Privacy & Security > Bluetooth."
|
||||
"terminal in System Settings > Privacy & Security > Bluetooth.",
|
||||
"",
|
||||
)
|
||||
if self._pin:
|
||||
if not self._pin:
|
||||
if os_bonded:
|
||||
return (
|
||||
f"Windows has {where} paired, but the device refused that pairing — it is "
|
||||
"probably out of date (the device was reflashed, reset, or given a new PIN "
|
||||
"since). Pass --ble-pin <PIN> and MeshTerm will pair it again, or remove it "
|
||||
"in Settings > Bluetooth and reconnect.",
|
||||
"Windows' saved pairing was refused — the PIN pairs it again.",
|
||||
)
|
||||
return (
|
||||
f"{where} rejected the Bluetooth PIN — it needs pairing and the PIN provided "
|
||||
"wasn't accepted. Double-check the 6-digit code shown on the device (or in the "
|
||||
"MeshCore app) and pass it with --ble-pin, then try again. On Windows you may "
|
||||
"also need to remove and re-pair the device in Settings > Bluetooth."
|
||||
f"{where} requires a Bluetooth pairing PIN. Pass it with --ble-pin <PIN> (the "
|
||||
"6-digit code shown on the device or in the MeshCore app). On Windows you may "
|
||||
"also need to pair the device once in Settings > Bluetooth before it will "
|
||||
"connect.",
|
||||
"",
|
||||
)
|
||||
pairing = self._ble_pairing
|
||||
outcome = pairing.outcome if pairing else ""
|
||||
status = pairing.status if pairing else ""
|
||||
if outcome == "failed" and status in _PAIRING_REFUSED:
|
||||
return (
|
||||
f"{where} refused to pair ({status}) — it may be connected to a phone or "
|
||||
"another computer, or still holding an old pairing for this one. Disconnect "
|
||||
"it there or restart it, then try again.",
|
||||
"The device refused to pair — restart it and try again.",
|
||||
)
|
||||
if outcome == "failed" and status not in _PAIRING_WRONG_PIN:
|
||||
return (
|
||||
f"Windows couldn't pair with {where} ({status}). Remove it in Settings > "
|
||||
"Bluetooth, restart the device, and try again.",
|
||||
f"Windows couldn't pair ({status}).",
|
||||
)
|
||||
if outcome == "absent":
|
||||
return (
|
||||
f"Windows couldn't reach {where} to pair it — it may be out of range, asleep, "
|
||||
"or connected to another device.",
|
||||
"Windows couldn't reach the device to pair it.",
|
||||
)
|
||||
if outcome == "error":
|
||||
return (
|
||||
f"Windows' pairing call failed for {where}: {status}",
|
||||
"Windows' pairing call failed — the log has the detail.",
|
||||
)
|
||||
if outcome in ("paired", "reused"):
|
||||
return (
|
||||
f"Windows paired with {where}, but the device still refuses the connection — "
|
||||
"it is probably holding an old pairing for this computer. Restart the device, "
|
||||
"remove it in Settings > Bluetooth, and try again.",
|
||||
"Paired, but still refused — restart the device and retry.",
|
||||
)
|
||||
return (
|
||||
f"{where} requires a Bluetooth pairing PIN. Pass it with --ble-pin <PIN> (the "
|
||||
"6-digit code shown on the device or in the MeshCore app). On Windows you may also "
|
||||
"need to pair the device once in Settings > Bluetooth before it will connect."
|
||||
f"{where} rejected the Bluetooth PIN — it needs pairing and the PIN provided "
|
||||
"wasn't accepted. Double-check the 6-digit code shown on the device (or in the "
|
||||
"MeshCore app) and pass it with --ble-pin, then try again.",
|
||||
"That PIN was rejected — check the code and try again.",
|
||||
)
|
||||
|
||||
def _no_response_message(self) -> str:
|
||||
"""A clean, recoverable error for an endpoint that didn't answer as a companion."""
|
||||
if self._transport == "ble":
|
||||
# The link opened (a link that didn't has its own message), so range and power
|
||||
# are not the question here — what answered is.
|
||||
where = self._address or "the selected Bluetooth device"
|
||||
return (
|
||||
f"no response from a MeshCore companion over Bluetooth ({where}); it may be "
|
||||
"out of range, powered off, already connected to another device, or not a "
|
||||
"MeshCore device."
|
||||
f"connected to {where} over Bluetooth, but it never answered as a MeshCore "
|
||||
"companion — it may be running repeater or room server firmware, or be busy "
|
||||
"with another app."
|
||||
)
|
||||
if self._transport == "tcp":
|
||||
where = self.endpoint or "the selected network device"
|
||||
@@ -4655,9 +4833,33 @@ async def _probe(device: MeshCoreDevice, timeout: float) -> tuple[MeshCoreDevice
|
||||
# ``wait_for`` is only a safety net a few seconds beyond that, so we never cancel the
|
||||
# client mid-handshake (which would leak the open connection). A real companion answers in
|
||||
# well under a second (serial) or a few seconds (BLE), so this never delays a good device.
|
||||
stage = "connect"
|
||||
try:
|
||||
await asyncio.wait_for(device.connect(), timeout + 4.0)
|
||||
stage = "identity"
|
||||
info = await asyncio.wait_for(device.get_self_info(), timeout)
|
||||
except asyncio.TimeoutError as exc:
|
||||
await _safe_disconnect(device)
|
||||
if getattr(device, "transport", None) != "ble":
|
||||
return None
|
||||
# A Bluetooth connect has more stages than a serial one (link, pairing, service
|
||||
# discovery, the identity reply), so saying which one ran out of time is what
|
||||
# tells a device out of range apart from one that connected and then went quiet.
|
||||
where = device.endpoint or "the selected Bluetooth device"
|
||||
if stage == "connect":
|
||||
message = (
|
||||
f"connecting to {where} over Bluetooth took longer than {timeout + 4.0:.0f}s — "
|
||||
"the link, pairing, or service discovery stalled. Move closer, restart the "
|
||||
"device, and try again."
|
||||
)
|
||||
else:
|
||||
message = (
|
||||
f"connected to {where} over Bluetooth, but it didn't answer the identity query "
|
||||
f"within {timeout:.0f}s — it may be busy with another app; restart it and try "
|
||||
"again."
|
||||
)
|
||||
_log.warning("BLE probe of %s timed out at the %s stage", where, stage)
|
||||
raise DeviceCommandError(message) from exc
|
||||
except DeviceCommandError:
|
||||
# A clean, actionable failure (the device needs a PIN, say): close the probe and let it
|
||||
# through so the picker surfaces the remedy rather than hiding it behind "didn't answer".
|
||||
|
||||
@@ -251,7 +251,6 @@ async def _smoke_test(
|
||||
unrecoverable failure, or a cancelled PIN prompt).
|
||||
"""
|
||||
pin: str | None = None
|
||||
pin_error = "" # empty on the first ask; set once a PIN has been rejected
|
||||
while True:
|
||||
# BLE connect and service discovery take a few seconds, so the spinner matters most here.
|
||||
try:
|
||||
@@ -261,19 +260,21 @@ async def _smoke_test(
|
||||
title="Checking companion",
|
||||
banner=load_logo(),
|
||||
)
|
||||
except DeviceAuthenticationError:
|
||||
except DeviceAuthenticationError as exc:
|
||||
# The device answered the scan but won't connect until it's bonded (or the last PIN
|
||||
# was wrong). Collect one in the popup and loop to retry; Esc returns to the list.
|
||||
# The error says *which* refusal it was — a stale bond, a wrong PIN, a device that
|
||||
# turned the pairing down — and the dialog carries that line under its question,
|
||||
# rather than calling every refusal a rejected PIN.
|
||||
entered = await ui.prompt_pin_startup(
|
||||
name,
|
||||
error=pin_error,
|
||||
error=exc.hint,
|
||||
help_text="The 6-digit code shown on the device or in the MeshCore app",
|
||||
banner=load_logo(),
|
||||
)
|
||||
if entered is None:
|
||||
return None # the user gave up → back to the device list
|
||||
pin = entered
|
||||
pin_error = "That PIN was rejected — check the code and try again."
|
||||
continue
|
||||
except DeviceCommandError as exc:
|
||||
# A different actionable failure (not a PIN): show its remedy verbatim, then back to
|
||||
|
||||
@@ -317,15 +317,16 @@ def test_the_retry_loop_never_reuses_a_torn_down_client() -> None:
|
||||
|
||||
|
||||
def test_every_link_open_attempt_failing_raises_the_last_error() -> None:
|
||||
"""Exhausting the retries surfaces the link failure — with nothing left open."""
|
||||
"""Exhausting the retries says the link never opened — with nothing left open."""
|
||||
_FakeMeshCore.reset(
|
||||
ConnectionError("Failed to connect to device"),
|
||||
ConnectionError("Failed to connect to device"),
|
||||
)
|
||||
dev = _device()
|
||||
|
||||
with pytest.raises(ConnectionError):
|
||||
with pytest.raises(conn_mod.DeviceCommandError) as excinfo:
|
||||
asyncio.run(dev._create_ble_with_retry(_FakeMeshCore))
|
||||
|
||||
assert isinstance(excinfo.value.__cause__, ConnectionError)
|
||||
assert len(_FakeMeshCore.built) == conn_mod._BLE_CONNECT_ATTEMPTS
|
||||
assert all(not c.cx.link_open for c in _FakeMeshCore.built)
|
||||
@@ -139,7 +139,11 @@ async def _disable_windows_pairing(dev: connection.MeshCoreDevice) -> None:
|
||||
async def _never_pairs(*, force: bool) -> bool:
|
||||
return False
|
||||
|
||||
async def _no_bond() -> bool:
|
||||
return False
|
||||
|
||||
dev._pair_ble_windows = _never_pairs # type: ignore[method-assign]
|
||||
dev._ble_os_bonded = _no_bond # type: ignore[method-assign]
|
||||
|
||||
|
||||
def _fake_ble_stack(monkeypatch: pytest.MonkeyPatch, *outcomes):
|
||||
@@ -219,6 +223,90 @@ async def test_create_ble_translates_auth_error_to_pin_guidance(monkeypatch) ->
|
||||
assert "rejected" in str(excinfo_pin.value).lower()
|
||||
|
||||
|
||||
async def test_create_ble_names_a_stale_windows_bond(monkeypatch) -> None:
|
||||
"""No PIN, but Windows holds a bond the device refuses: say the bond is stale.
|
||||
|
||||
The case reported from the field — a companion Windows showed as paired, refusing the
|
||||
subscribe after the device's side of the bond was lost. "Requires a PIN" was the wrong
|
||||
story for someone looking at "Paired" in Settings.
|
||||
"""
|
||||
monkeypatch.setattr(sys, "platform", "win32")
|
||||
fake = _fake_ble_stack(monkeypatch, BleakGATTProtocolError("Insufficient Authentication"))
|
||||
dev = connection.MeshCoreDevice(transport="ble", address=_BONDED_ADDR)
|
||||
await _disable_windows_pairing(dev)
|
||||
|
||||
async def _bonded() -> bool:
|
||||
return True
|
||||
|
||||
dev._ble_os_bonded = _bonded # type: ignore[method-assign]
|
||||
with pytest.raises(connection.DeviceAuthenticationError) as excinfo:
|
||||
await dev._create_ble(fake)
|
||||
assert "Windows has" in str(excinfo.value)
|
||||
assert "--ble-pin" in str(excinfo.value) # the PIN is still how MeshTerm re-pairs it
|
||||
assert "saved pairing" in excinfo.value.hint # and the PIN dialog says why it is asking
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("pairing", "expected", "hint"),
|
||||
[
|
||||
(connection._BlePairing("failed", "authentication failure"), "rejected", "PIN"),
|
||||
(connection._BlePairing("failed", "connection rejected"), "refused to pair", "refused"),
|
||||
(connection._BlePairing("failed", "hardware failure"), "couldn't pair", "hardware"),
|
||||
(connection._BlePairing("absent"), "couldn't reach", "reach"),
|
||||
(connection._BlePairing("paired"), "still refuses", "still refused"),
|
||||
(None, "rejected the Bluetooth PIN", "PIN was rejected"),
|
||||
],
|
||||
)
|
||||
async def test_create_ble_names_what_the_pairing_found(
|
||||
monkeypatch, pairing, expected: str, hint: str
|
||||
) -> None:
|
||||
"""With a PIN, the refusal names the pairing's own outcome, each with its own fix."""
|
||||
monkeypatch.setattr(sys, "platform", "win32")
|
||||
fake = _fake_ble_stack(
|
||||
monkeypatch,
|
||||
BleakGATTProtocolError("Insufficient Authentication"),
|
||||
BleakGATTProtocolError("Insufficient Authentication"),
|
||||
)
|
||||
dev = connection.MeshCoreDevice(transport="ble", address=_BONDED_ADDR, pin=_A_PIN)
|
||||
|
||||
async def _pair(*, force: bool) -> bool:
|
||||
dev._ble_pairing = pairing
|
||||
return pairing is not None and pairing.outcome == "paired"
|
||||
|
||||
dev._pair_ble_windows = _pair # type: ignore[method-assign]
|
||||
with pytest.raises(connection.DeviceAuthenticationError) as excinfo:
|
||||
await dev._create_ble(fake)
|
||||
assert expected in str(excinfo.value)
|
||||
assert hint in excinfo.value.hint
|
||||
|
||||
|
||||
def test_a_refused_pairing_step_is_an_auth_error() -> None:
|
||||
"""A failing pair() in bleak is a pairing problem, not a device that "didn't answer"."""
|
||||
assert connection._is_ble_auth_error(Exception("Could not pair with device: 19: FAILED"))
|
||||
assert connection._is_ble_auth_error(Exception("org.bluez.Error.AuthenticationFailed"))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("stage", "expected"), [("connect", "took longer"), ("identity", "identity")]
|
||||
)
|
||||
async def test_ble_probe_timeout_names_its_stage(monkeypatch, stage: str, expected: str) -> None:
|
||||
"""A BLE probe that runs out of time says which stage stalled, instead of returning None."""
|
||||
dev = connection.MeshCoreDevice(transport="ble", address=_OPEN_ADDR)
|
||||
|
||||
async def _connect() -> None:
|
||||
if stage == "connect":
|
||||
raise asyncio.TimeoutError
|
||||
|
||||
async def _self_info() -> dict:
|
||||
raise asyncio.TimeoutError
|
||||
|
||||
dev.connect = _connect # type: ignore[method-assign]
|
||||
dev.get_self_info = _self_info # type: ignore[method-assign]
|
||||
with pytest.raises(DeviceCommandError) as excinfo:
|
||||
await connection._probe(dev, 1.0)
|
||||
assert expected in str(excinfo.value)
|
||||
|
||||
|
||||
async def test_create_ble_repairs_stale_bond_and_retries_once(monkeypatch) -> None:
|
||||
"""A first auth failure triggers one unpair-and-re-pair, then the retried connect succeeds.
|
||||
|
||||
@@ -290,7 +378,7 @@ async def test_create_ble_retries_a_transient_link_failure(monkeypatch) -> None:
|
||||
|
||||
|
||||
async def test_create_ble_gives_up_after_the_retry(monkeypatch) -> None:
|
||||
"""A link that never opens surfaces its ConnectionError after the bounded retries."""
|
||||
"""A link that never opens says so after the bounded retries — not "not a companion"."""
|
||||
fake = _fake_ble_stack(
|
||||
monkeypatch,
|
||||
ConnectionError("Failed to connect to device"),
|
||||
@@ -299,8 +387,11 @@ async def test_create_ble_gives_up_after_the_retry(monkeypatch) -> None:
|
||||
monkeypatch.setattr(connection, "_BLE_CONNECT_RETRY_DELAY_S", 0.0)
|
||||
dev = connection.MeshCoreDevice(transport="ble", address=_OPEN_ADDR)
|
||||
await _disable_windows_pairing(dev)
|
||||
with pytest.raises(ConnectionError):
|
||||
with pytest.raises(DeviceCommandError) as excinfo:
|
||||
await dev._create_ble(fake)
|
||||
assert not isinstance(excinfo.value, connection.DeviceAuthenticationError)
|
||||
assert "couldn't open a Bluetooth link" in str(excinfo.value)
|
||||
assert isinstance(excinfo.value.__cause__, ConnectionError) # the original rides along
|
||||
assert len(fake.built) == connection._BLE_CONNECT_ATTEMPTS
|
||||
assert all(client.disconnect_calls == 1 for client in fake.built)
|
||||
|
||||
|
||||
+4
-2
@@ -1911,12 +1911,14 @@ def test_device_picker_prompts_and_retries_ble_pin(tmp_path) -> None:
|
||||
|
||||
async def verify(_device, pin=None):
|
||||
if pin != "654321": # the first probe (no PIN) and the wrong code both get rejected
|
||||
raise DeviceAuthenticationError("needs a Bluetooth pairing PIN")
|
||||
# The connection names the refusal; the picker carries its hint into the dialog.
|
||||
hint = "That PIN was rejected — check the code and try again." if pin else ""
|
||||
raise DeviceAuthenticationError("needs a Bluetooth pairing PIN", hint=hint)
|
||||
return {"adv_name": "Pinned", "model": "Seeed Tracker T1000-E"}
|
||||
|
||||
chosen = asyncio.run(prompt_device(_Ui(), devices, store, verify))
|
||||
assert chosen is devices[0]
|
||||
# Asked twice: first with no error, then with a "rejected" note after the wrong code.
|
||||
# Asked twice: first with no error, then with the refusal's own hint after the wrong code.
|
||||
assert errors[0] == ""
|
||||
assert "rejected" in errors[1].lower()
|
||||
remembered = store.load()
|
||||
|
||||
Reference in new issue
Block a user