Linux + iPhone Continuity / iMessage / SMS
fix: ancs reconnect resubscribe
Zack Bartela4a82e87b339
diff --git a/docs/BLUETOOTH.md b/docs/BLUETOOTH.mdindex 2f1dc2e..700d322 100644--- a/docs/BLUETOOTH.md+++ b/docs/BLUETOOTH.md@@ -3559,3 +3559,36 @@ Also from orychalk (#85): a B-field threshold does not port (Pro 1 keeps an actipod movements on Pro 3; and both buds out moves the volume target, hence the ear-return claim.Not yet verified on hardware: the whole of the above.++### 2026-09-15 - An LE-only drop left notifications dead until a restart (#190)++Reported in #190, and four times in our own `tetherd.log` on 0.2.31, which carries the 2026-09-12+fix above:++```+09-13 15:02:24 ancs: Notification mirroring is active.+09-13 15:10:16 Bearer.LE1 disconnected (Timeout) BR/EDR stays up+09-13 15:10:17 soliciting ANCS for 180s the supervisor saw LE go down+ no ancs line for 2.5 hours+```++Same at 09-14 10:24, 19:52 and 21:55. Each ended only with a daemon restart, whose fresh client+started a session within seconds, so LE was back and only the old subscription was dead. The+cases that recovered (09-12 walk, 09-14 18:43 suspend) dropped both bearers.++- The supervisor saw LE down: `ServicesResolved` goes false on an LE-only drop, so `le_link_up()`+ short-circuits before the read.+- `verify_subscription()` did not: it reads only the stale `Notifying` and the GAP read, and with+ BR/EDR up that read does not answer `Not connected`. `ready` stayed latched, `hold` kept the+ path, and nothing was ever rebuilt.+- Tether never sent `StopNotify`. BlueZ keeps its notify client for our sender across the drop,+ so a resubscribe would be a no-op on that registration; a restarted daemon is a new sender and+ gets a real CCCD write.++The ANCS client now drops the session on the `Disconnected` signal for `Bearer.LE1` or `Device1`+(`BluezMonitor::le_drop_count()`), and `unsubscribe()` sends `StopNotify` on both sources before+every rebuild.++Not settled: the exact `ReadValue` error on the GAP characteristic with only BR/EDR up, and the+fix itself on hardware (walk out until only `LE1` times out, come back, expect `LE link dropped`+then a new session and a popup).diff --git a/src/core/include/tether/bluetooth/ancs/client.hpp b/src/core/include/tether/bluetooth/ancs/client.hppindex d4c4db8..939659e 100644--- a/src/core/include/tether/bluetooth/ancs/client.hpp+++ b/src/core/include/tether/bluetooth/ancs/client.hpp@@ -46,6 +46,10 @@ namespace tether::bluetooth::ancs {// Whether to ask for notification contents or only which app sent them.void set_content_enabled(bool enabled);+ // Ends a live subscription so the next tick resubscribes on whatever link+ // is up then. No-op when not subscribed.+ void drop_session(const std::string& reason);+void tick(int64_t now);bool ready() const;diff --git a/src/core/include/tether/bluetooth/monitor.hpp b/src/core/include/tether/bluetooth/monitor.hppindex 6423112..50f52be 100644--- a/src/core/include/tether/bluetooth/monitor.hpp+++ b/src/core/include/tether/bluetooth/monitor.hpp@@ -42,6 +42,10 @@ namespace tether::bluetooth {// Clears the last disconnect reason for the device, so that the next disconnect will be reported.void clear_disconnect_reason(const std::string& address);+ // Disconnected signals seen on the device's LE bearer or the whole device.+ // Only its change matters. Each one ends any GATT session on the link.+ uint64_t le_drop_count(const std::string& device_path) const;+// Which controller to use: "hciN" or an address, empty for the first powered one.void set_preferred_adapter(std::string id);std::string preferred_adapter_id() const;diff --git a/src/core/src/bluetooth/ancs/client.cpp b/src/core/src/bluetooth/ancs/client.cppindex f506b74..56379f3 100644--- a/src/core/src/bluetooth/ancs/client.cpp+++ b/src/core/src/bluetooth/ancs/client.cpp@@ -82,6 +82,7 @@ namespace tether::bluetooth::ancs {void unsubscribe();void drop_gatt_paths();void verify_subscription();+ void rebuild(const std::string& reason);bool link_alive() const;void handle_source_event(const SourceEvent& event, int64_t now);void handle_response(const Request& request, const Response& response, int64_t now);@@ -320,6 +321,27 @@ namespace tether::bluetooth::ancs {if (!monitor || !monitor->connection())return;GDBusConnection* conn = monitor->connection();+ if (subscribed) {+ for (const std::string& path : {data_source_path, notification_source_path}) {+ if (path.empty())+ continue;+ GError* error = nullptr;+ GVariant* reply = g_dbus_connection_call_sync(conn,+ BLUEZ_NAME,+ path.c_str(),+ IFACE_CHARACTERISTIC,+ "StopNotify",+ nullptr,+ nullptr,+ G_DBUS_CALL_FLAGS_NONE,+ 2000,+ nullptr,+ &error);+ if (reply)+ g_variant_unref(reply);+ g_clear_error(&error);+ }+ }if (source_signal) {g_dbus_connection_signal_unsubscribe(conn, source_signal);source_signal = 0;@@ -387,20 +409,24 @@ namespace tether::bluetooth::ancs {if (!object_gone && notifying && !link_gone)return;- debug::log(INFO,- "ancs: the notification subscription is no longer live ({}), rebuilding it",- object_gone ? "characteristics gone"- : link_gone ? "LE link gone"- : "BlueZ cleared Notifying");if (object_gone) {+ debug::log(INFO,+ "ancs: the notification subscription is no longer live (characteristics gone), rebuilding it");drop_gatt_paths();set_status(false, "Waiting for the iPhone's notification service.");+ sequencer->reset();+ in_progress.clear();} else {- unsubscribe();- subscribed = false;- next_subscribe = 0;- set_status(false, "Subscribing to the iPhone's notifications...");+ rebuild(link_gone ? "LE link gone" : "BlueZ cleared Notifying");}+ }++ void AncsClientState::rebuild(const std::string& reason) {+ debug::log(INFO, "ancs: the notification subscription is no longer live ({}), rebuilding it", reason);+ unsubscribe();+ subscribed = false;+ next_subscribe = 0;+ set_status(false, "Subscribing to the iPhone's notifications...");sequencer->reset();in_progress.clear();}@@ -599,6 +625,11 @@ namespace tether::bluetooth::ancs {void AncsClient::set_content_enabled(bool enabled) { state_->content_enabled = enabled; }+ void AncsClient::drop_session(const std::string& reason) {+ if (state_->subscribed)+ state_->rebuild(reason);+ }+bool AncsClient::ready() const { return state_->ready; }const std::string& AncsClient::status_reason() const { return state_->reason; }diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex 5931b0f..e225f99 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -666,6 +666,7 @@ namespace tether::bluetooth {// When the LE link was first seen up while the phone offered no ANCS// service. -1 whenever that is not the case.int64_t ancs_absent_since = -1;+ uint64_t le_drops_seen = 0;int map_failures = 0;int64_t next_message_poll = 0;std::string open_map_path;@@ -1111,6 +1112,11 @@ namespace tether::bluetooth {}client->set_device(device_path);+ const uint64_t drops = device_path.empty() ? le_drops_seen : monitor->le_drop_count(device_path);+ if (drops != le_drops_seen) {+ le_drops_seen = drops;+ client->drop_session("LE link dropped");+ }client->tick(now);}diff --git a/src/core/src/bluetooth/monitor.cpp b/src/core/src/bluetooth/monitor.cppindex 633f5bb..9d55eba 100644--- a/src/core/src/bluetooth/monitor.cpp+++ b/src/core/src/bluetooth/monitor.cpp@@ -50,6 +50,7 @@ namespace tether::bluetooth {BluezObjects objects;Capability cap;std::map<std::string, std::string> disconnect_reasons;+ std::map<std::string, uint64_t> le_drops;std::string preferred_adapter_id;std::string secure_adapter;std::optional<bool> secure_connections;@@ -129,6 +130,9 @@ namespace tether::bluetooth {{std::lock_guard<std::mutex> lock(impl->mutex);impl->disconnect_reasons[object_path] = reason;+ if (g_strcmp0(interface_name, "org.bluez.Bearer.LE1") == 0 ||+ g_strcmp0(interface_name, "org.bluez.Device1") == 0)+ ++impl->le_drops[object_path];}debug::log(INFO,"bluetooth: {} disconnected from {}: {} ({})",@@ -590,6 +594,12 @@ namespace tether::bluetooth {return {};}+ uint64_t BluezMonitor::le_drop_count(const std::string& device_path) const {+ std::lock_guard<std::mutex> lock(impl_->mutex);+ auto it = impl_->le_drops.find(device_path);+ return it == impl_->le_drops.end() ? 0 : it->second;+ }+void BluezMonitor::clear_disconnect_reason(const std::string& address) {std::lock_guard<std::mutex> lock(impl_->mutex);for (const auto& device : impl_->objects.devices) {