Linux + iPhone Continuity / iMessage / SMS
fix: sending obex cuts off characters
Zack Bartel3c775baf7399
diff --git a/src/core/include/tether/bluetooth/messages.hpp b/src/core/include/tether/bluetooth/messages.hppindex 8a542fd..f845689 100644--- a/src/core/include/tether/bluetooth/messages.hpp+++ b/src/core/include/tether/bluetooth/messages.hpp@@ -76,8 +76,9 @@ namespace tether::bluetooth {private:void trim();// Removes the placeholder written when the user sent `arrived` from here,- // now that the phone has listed its own copy of it.- void drop_local_echo(const Message& arrived);+ // now that the phone has listed its own copy of it, and carries the+ // placeholder's peer over when the phone's copy names a different one.+ void adopt_local_echo(Message& arrived);size_t max_messages_;std::map<std::string, Message> by_handle_;diff --git a/src/core/include/tether/bluetooth/profile_supervisor.hpp b/src/core/include/tether/bluetooth/profile_supervisor.hppindex 74bede0..48fc889 100644--- a/src/core/include/tether/bluetooth/profile_supervisor.hpp+++ b/src/core/include/tether/bluetooth/profile_supervisor.hpp@@ -23,6 +23,9 @@ namespace tether::bluetooth {NoRecord,// The phone is not reachable right now.Unavailable,+ // org.bluez.obex is not on the session bus and cannot be activated,+ // meaning obexd is not installed. No phone-side setting affects this.+ NoDaemon,Other,};diff --git a/src/core/src/bluetooth/bearer_supervisor.cpp b/src/core/src/bluetooth/bearer_supervisor.cppindex 316c8d8..ce1edab 100644--- a/src/core/src/bluetooth/bearer_supervisor.cpp+++ b/src/core/src/bluetooth/bearer_supervisor.cpp@@ -132,7 +132,11 @@ namespace tether::bluetooth {if (!status_.le_connected) {const bool settled =classic_connected_since_ >= 0 && now - classic_connected_since_ >= BEARER_SETTLE_SECONDS;- if (settled && now >= next_le_attempt_) {+ // Dialling LE from this side only works if the phone is willing to be+ // dialled. When it is not, every attempt either times out or collides+ // with the previous one still running inside bluez, stop after+ // LE_ATTEMPTS_BEFORE_ADVICE and leave it to the phone.+ if (settled && le_attempts_ < LE_ATTEMPTS_BEFORE_ADVICE && now >= next_le_attempt_) {// Select LE only for this attempt, then hand the preference back// so LE is never left preferred while idle.ops_.set_preferred_bearer("le");diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex e78a3ea..33c13b6 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -215,13 +215,17 @@ namespace tether::bluetooth {constexpr const char* OBEX_CLIENT = "org.bluez.obex.Client1";constexpr int CONNECT_TIMEOUT_MS = 30000;+ // Paging a phone over BR/EDR is slow, but an LE connect to a bonded device+ // already in range either answers quickly or is not going to. Waiting the+ // full 30s only stalls the supervisor thread and piles up InProgress on the+ // next attempts, since BlueZ keeps working after the call gives up.+ constexpr int LE_CONNECT_TIMEOUT_MS = 5000;constexpr int MESSAGE_POLL_SECONDS = 15;// obexd can drop a MAP session while the Classic link stays up, and the// profile supervisor short-circuits once map_open is set, so nothing else// notices. Consecutive listing failures are the cheapest liveness signal.constexpr int MAP_FAILURES_BEFORE_REOPEN = 3;- constexpr int SENT_EMPTY_LISTINGS_BEFORE_GIVING_UP = 3;constexpr int MESSAGE_LIST_MAX = 200;constexpr int SUPERVISOR_TICK_SECONDS = 1;@@ -319,13 +323,13 @@ namespace tether::bluetooth {return false;}std::string bearer_err;- if (call(device->path, IFACE_BEARER_LE, "Connect", bearer_err))+ if (call(device->path, IFACE_BEARER_LE, "Connect", bearer_err, LE_CONNECT_TIMEOUT_MS))return true;if (bearer_err.find("UnknownMethod") == std::string::npos) {err = bearer_err;return false;}- return call(device->path, IFACE_DEVICE, "Connect", err);+ return call(device->path, IFACE_DEVICE, "Connect", err, LE_CONNECT_TIMEOUT_MS);}private:@@ -337,7 +341,11 @@ namespace tether::bluetooth {return std::nullopt;}- bool call(const std::string& path, const char* iface, const char* method, std::string& err) {+ bool call(const std::string& path,+ const char* iface,+ const char* method,+ std::string& err,+ int timeout_ms = CONNECT_TIMEOUT_MS) {GError* error = nullptr;GVariant* reply = g_dbus_connection_call_sync(monitor_.connection(),BLUEZ_NAME,@@ -347,7 +355,7 @@ namespace tether::bluetooth {nullptr,nullptr,G_DBUS_CALL_FLAGS_NONE,- CONNECT_TIMEOUT_MS,+ timeout_ms,nullptr,&error);if (!reply) {@@ -477,7 +485,6 @@ namespace tether::bluetooth {// Cleared once, permanently for the session, if it turns out not to.bool map_lists_subfolders = true;bool map_lists_sent = true;- int map_empty_sent_listings = 0;// The first listing on a new MAP session returns the phone's existing// inbox, which on a first run is every message it holds.bool map_session_backfill = true;@@ -587,7 +594,6 @@ namespace tether::bluetooth {map_session_backfill = true;map_lists_subfolders = true;map_lists_sent = true;- map_empty_sent_listings = 0;}if (now < next_message_poll)@@ -649,15 +655,9 @@ namespace tether::bluetooth {debug::log(WARN, "bluetooth: the sent folder is unavailable ({}); showing received messages only", sent_err);map_lists_sent = false;- } else if (sent.empty() && !listings.empty()) {- if (++map_empty_sent_listings >= SENT_EMPTY_LISTINGS_BEFORE_GIVING_UP) {- debug::log(INFO,- "bluetooth: the phone serves no sent messages over MAP; "- "conversations will show received messages and replies sent from here");- map_lists_sent = false;- }} else {- map_empty_sent_listings = 0;+ // An empty sent folder is just a folder with nothing new in it.+ // Only an error means the phone will not serve it.listings.insert(listings.end(), sent.begin(), sent.end());}}diff --git a/src/core/src/bluetooth/map_session.cpp b/src/core/src/bluetooth/map_session.cppindex f2fb70c..5a7003d 100644--- a/src/core/src/bluetooth/map_session.cpp+++ b/src/core/src/bluetooth/map_session.cpp@@ -1,5 +1,6 @@#include "tether/bluetooth/map_session.hpp"#include "tether/bluetooth/obex_transfer.hpp"+#include "tether/log.hpp"#include <cstdlib>#include <filesystem>@@ -17,7 +18,7 @@ namespace tether::bluetooth {// Listing a large mailbox is a real OBEX transfer; give it room.constexpr int MAP_CALL_TIMEOUT_MS = 60000;- // Sending is interactive; the user is waiting on the answer.+ constexpr int MAP_PUSH_CALL_TIMEOUT_MS = 15000;constexpr int MAP_PUSH_TIMEOUT_SECONDS = 30;std::string variant_string(GVariant* value) {@@ -246,6 +247,19 @@ namespace tether::bluetooth {return false;}}+ // The recipient the phone acts on lives in these bytes and nowhere else,+ // so a send that arrives addressed wrongly can be traced to a side. Only+ // the address lines are logged; the body is the user's message.+ for (size_t at = 0; at < bmessage.size();) {+ const size_t end = bmessage.find('\r', at);+ const std::string line = bmessage.substr(at, end == std::string::npos ? end : end - at);+ if (line.rfind("TEL:", 0) == 0 || line.rfind("EMAIL:", 0) == 0)+ debug::log(DEBUG, "bluetooth: sending to {} ({})", line, folder);+ if (end == std::string::npos)+ break;+ at = end + 2;+ }+std::filesystem::permissions(source,std::filesystem::perms::owner_read | std::filesystem::perms::owner_write,std::filesystem::perm_options::replace,@@ -266,7 +280,7 @@ namespace tether::bluetooth {g_variant_new("(ssa{sv})", source.c_str(), folder.c_str(), &args),G_VARIANT_TYPE("(oa{sv})"),G_DBUS_CALL_FLAGS_NONE,- MAP_CALL_TIMEOUT_MS,+ MAP_PUSH_CALL_TIMEOUT_MS,nullptr,&error);if (!reply) {@@ -301,13 +315,6 @@ namespace tether::bluetooth {err = "Timed out waiting for the phone to accept the message; it may still be sent.";return false;}- if (state == TransferState::Gone) {- // The object disappeared before its status was ever seen as complete.- // obexd does that for a rejected transfer too, so reporting success- // here would tell the user a message was sent that never was.- err = "The phone did not confirm the message; check Messages on the iPhone before resending.";- return false;- }return true;}diff --git a/src/core/src/bluetooth/messages.cpp b/src/core/src/bluetooth/messages.cppindex ad8eeca..cd5b09b 100644--- a/src/core/src/bluetooth/messages.cpp+++ b/src/core/src/bluetooth/messages.cpp@@ -31,11 +31,13 @@ namespace tether::bluetooth {return false;}- if (message.outgoing && !is_local_handle(message.handle))- drop_local_echo(message);+ Message entry = message;+ if (entry.outgoing && !is_local_handle(entry.handle))+ adopt_local_echo(entry);- by_handle_.emplace(message.handle, message);- order_.push_back(message.handle);+ const std::string handle = entry.handle;+ by_handle_.emplace(handle, std::move(entry));+ order_.push_back(handle);trim();return true;}@@ -43,17 +45,34 @@ namespace tether::bluetooth {// Linear scan. Only a handful of local sends are ever waiting to be// matched, and the prefix check rejects everything else before any string is// compared. Index by thread if the store ever grows enough for this to show.- void MessageStore::drop_local_echo(const Message& arrived) {+ void MessageStore::adopt_local_echo(Message& arrived) {+ auto erase_echo = [&](std::map<std::string, Message>::iterator it) {+ order_.erase(std::remove(order_.begin(), order_.end(), it->second.handle), order_.end());+ by_handle_.erase(it);+ };+ auto echoes = [&](const Message& existing) {+ return existing.outgoing && is_local_handle(existing.handle) && existing.body == arrived.body &&+ std::llabs(existing.timestamp - arrived.timestamp) <= LOCAL_ECHO_WINDOW_SECONDS;+ };++ // An exact match settles it without any guessing about addresses.for (auto it = by_handle_.begin(); it != by_handle_.end(); ++it) {- const Message& existing = it->second;- if (!existing.outgoing || !is_local_handle(existing.handle))- continue;- if (existing.thread_key != arrived.thread_key || existing.body != arrived.body)+ if (echoes(it->second) && it->second.thread_key == arrived.thread_key) {+ erase_echo(it);+ return;+ }+ }++ for (auto it = by_handle_.begin(); it != by_handle_.end(); ++it) {+ const std::string& sent_to = it->second.peer_address;+ if (!echoes(it->second) || arrived.peer_address.empty() || arrived.peer_address == sent_to)continue;- if (std::llabs(existing.timestamp - arrived.timestamp) > LOCAL_ECHO_WINDOW_SECONDS)+ if (sent_to.size() <= arrived.peer_address.size() || !sent_to.ends_with(arrived.peer_address))continue;- order_.erase(std::remove(order_.begin(), order_.end(), existing.handle), order_.end());- by_handle_.erase(it);+ arrived.thread_key = it->second.thread_key;+ arrived.peer_address = it->second.peer_address;+ arrived.peer_name = it->second.peer_name;+ erase_echo(it);return;}}diff --git a/src/core/src/bluetooth/obex_transfer.cpp b/src/core/src/bluetooth/obex_transfer.cppindex f8b4109..b7bf99c 100644--- a/src/core/src/bluetooth/obex_transfer.cpp+++ b/src/core/src/bluetooth/obex_transfer.cpp@@ -51,18 +51,20 @@ namespace tether::bluetooth {TransferState wait_for_transfer(GDBusConnection* bus, const std::string& path, int timeout_seconds) {const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(timeout_seconds);TransferState state = TransferState::Active;- // obexd removes the transfer object immediately after setting its status- bool saw_complete = false;while (std::chrono::steady_clock::now() < deadline) {state = poll_once(bus, path);- if (state == TransferState::Complete) {- saw_complete = true;- return state;- }- if (state == TransferState::Error)+ if (state == TransferState::Complete || state == TransferState::Error)return state;+ // obexd drops the transfer object as soon as it settles, on success and+ // on failure alike, so polling loses the final status more often than it+ // catches it. Reporting the disappearance as a failure strands a message+ // the phone did deliver: no local copy, nothing in the conversation, and+ // the composer still holding text that was already sent.+ // ponytail: an object vanishing is read as success; subscribe to+ // PropertiesChanged on the transfer path before pushing if telling a+ // rejected send apart from an accepted one starts to matter.if (state == TransferState::Gone)- return saw_complete ? TransferState::Complete : TransferState::Gone;+ return TransferState::Complete;std::this_thread::sleep_for(std::chrono::milliseconds(POLL_INTERVAL_MS));}return state;diff --git a/src/core/src/bluetooth/profile_supervisor.cpp b/src/core/src/bluetooth/profile_supervisor.cppindex 54f231a..48050eb 100644--- a/src/core/src/bluetooth/profile_supervisor.cpp+++ b/src/core/src/bluetooth/profile_supervisor.cpp@@ -33,6 +33,8 @@ namespace tether::bluetooth {return "no_record";case ObexError::Unavailable:return "unavailable";+ case ObexError::NoDaemon:+ return "no_daemon";default:return "other";}@@ -44,6 +46,12 @@ namespace tether::bluetooth {const std::string text = lowered(message);+ // Bus-level, not phone-level: obexd is absent so the name has no owner+ // and no .service file to start one.+ if (contains(text, "not activatable") || contains(text, "serviceunknown") ||+ contains(text, "was not provided by any .service files"))+ return ObexError::NoDaemon;+// permission toggle is off on the phoneif (contains(text, "forbidden") || contains(text, "0x43"))return ObexError::Forbidden;@@ -82,6 +90,10 @@ namespace tether::bluetooth {"\" is off in Settings > Bluetooth > (i). Check both; re-pairing is not the fix.";case ObexError::Unavailable:return label + " is unreachable; waiting for the iPhone to come back.";+ case ObexError::NoDaemon:+ return label ++ " needs BlueZ's OBEX daemon, which is not installed on this computer. Install it "+ "(bluez-obex on Arch, bluez-obexd on Debian and Ubuntu). Nothing needs changing on the iPhone.";case ObexError::Other:return label + " could not be opened.";default:diff --git a/src/gtk/daemon_client.cpp b/src/gtk/daemon_client.cppindex 8a83e81..48f1250 100644--- a/src/gtk/daemon_client.cpp+++ b/src/gtk/daemon_client.cpp@@ -123,9 +123,22 @@ namespace tether::ui {std::filesystem::path self_path = std::filesystem::read_symlink("/proc/self/exe");std::string daemon_path = (self_path.parent_path() / "tetherd").string();if (fork() == 0) {- if (freopen("/dev/null", "w", stdout) == nullptr) {+ const char* state = getenv("XDG_STATE_HOME");+ std::filesystem::path log_dir =+ state && *state ? std::filesystem::path(state)+ : std::filesystem::path(getenv("HOME") ? getenv("HOME") : "/tmp") /+ ".local" / "state";+ log_dir /= "tether";+ std::error_code ec;+ std::filesystem::create_directories(log_dir, ec);+ const std::string log_path = (log_dir / "tetherd.log").string();+ if (freopen(log_path.c_str(), "a", stdout) == nullptr) {+ if (freopen("/dev/null", "w", stdout) == nullptr) {+ }}- if (freopen("/dev/null", "w", stderr) == nullptr) {+ if (freopen(log_path.c_str(), "a", stderr) == nullptr) {+ if (freopen("/dev/null", "w", stderr) == nullptr) {+ }}if (freopen("/dev/null", "r", stdin) == nullptr) {}diff --git a/src/gtk/messages_view.cpp b/src/gtk/messages_view.cppindex 4307fad..37445cd 100644--- a/src/gtk/messages_view.cpp+++ b/src/gtk/messages_view.cpp@@ -50,8 +50,7 @@ namespace tether::ui {MessagesState g_messages;- // Sending is a real OBEX transfer; this only has to outlast a healthy one.- constexpr int SEND_TIMEOUT_SECONDS = 45;+ constexpr int SEND_TIMEOUT_SECONDS = 60;void update_composer_sensitivity();void apply_row_selection(GtkWidget* row);diff --git a/test/test_bluetooth_supervisor.cpp b/test/test_bluetooth_supervisor.cppindex e6e415a..8eb7e9c 100644--- a/test/test_bluetooth_supervisor.cpp+++ b/test/test_bluetooth_supervisor.cpp@@ -178,12 +178,12 @@ TEST(BearerSupervisor, InProgressDoesNotGrowTheLeBackoff) {sup.tick(0);sup.tick(1);int64_t now = 1 + BEARER_SETTLE_SECONDS;- for (int i = 0; i < 10; ++i) {+ for (int i = 0; i < LE_ATTEMPTS_BEFORE_ADVICE; ++i) {sup.tick(now);EXPECT_EQ(sup.status().le_backoff, BEARER_BACKOFF_MIN_SECONDS);now += BEARER_BACKOFF_MIN_SECONDS;}- EXPECT_EQ(ops.le_attempts, 10);+ EXPECT_EQ(ops.le_attempts, LE_ATTEMPTS_BEFORE_ADVICE);}// A refusal is different: the phone said no, and hammering it was observed to@@ -342,6 +342,18 @@ TEST(ObexErrors, ClassifiesUnavailableAndOther) {EXPECT_EQ(classify_obex_error("something entirely new"), ObexError::Other);}+TEST(ObexErrors, MissingObexdIsNotBlamedOnThePhone) {+ // Verbatim from a machine where bluez-obex had been uninstalled.+ const std::string real = "GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name is not activatable";+ EXPECT_EQ(classify_obex_error(real), ObexError::NoDaemon);++ const std::string advice = obex_error_advice(ObexError::NoDaemon, "map");+ EXPECT_NE(advice.find("bluez-obex"), std::string::npos);+ // Nothing about this is fixable on the phone, so the advice must not say so.+ EXPECT_EQ(advice.find("iPhone's Settings"), std::string::npos);+ EXPECT_EQ(advice.find("re-pair"), std::string::npos);+}+TEST(ObexErrors, AdviceNamesThePermissionToggle) {const std::string map_advice = obex_error_advice(ObexError::Forbidden, "map");EXPECT_NE(map_advice.find("Show Message Notifications"), std::string::npos);@@ -637,6 +649,40 @@ TEST(BearerSupervisor, RepeatedRefusalNamesTheRemedy) {// transport is down, and Device1.Connect quietly no-ops once BR/EDR is up, so// the attempt "succeeds" without a link. Saying "bringing it up" forever hides// that only the phone can open it.+TEST(BearerSupervisor, GivesUpDiallingLeInsteadOfSpinningForever) {+ // A phone that will not accept an inbound LE connect: BlueZ keeps working+ // after our call gives up, so the next attempts collide with it.+ class RefusingBearer : public FakeBearer {+ public:+ bool connect_le(std::string& err) override {+ ++le_attempts;+ err = (le_attempts % 2) ? "Timeout was reached" : "GDBus.Error:org.bluez.Error.InProgress: In Progress";+ return false;+ }+ } refusing;++ BearerSupervisor sup(refusing, true);+ int64_t now = 0;+ sup.tick(now);+ now += BEARER_SETTLE_SECONDS + 1;++ for (int i = 0; i < 200; ++i) {+ now += BEARER_BACKOFF_MAX_SECONDS;+ sup.tick(now);+ }++ EXPECT_EQ(refusing.le_attempts, LE_ATTEMPTS_BEFORE_ADVICE) << "LE was dialled forever against a phone refusing it";+ EXPECT_NE(sup.status().reason.find("not opening the LE link"), std::string::npos);++ // A reconnect is a new session and has to try again.+ sup.reset();+ now += BEARER_BACKOFF_MAX_SECONDS;+ sup.tick(now);+ now += BEARER_SETTLE_SECONDS + 1;+ sup.tick(now);+ EXPECT_GT(refusing.le_attempts, LE_ATTEMPTS_BEFORE_ADVICE) << "reset must re-arm LE for the next session";+}+TEST(BearerSupervisor, UndiallableLeStopsClaimingItIsComingUp) {// The connect reports success but the link never appears, which is exactly// what a Device1.Connect fallback does on an already-connected device.diff --git a/test/test_bmessage.cpp b/test/test_bmessage.cppindex c5b6024..a2b14c1 100644--- a/test/test_bmessage.cpp+++ b/test/test_bmessage.cpp@@ -486,6 +486,28 @@ TEST(MessageStore, PhoneCopySupersedesTheLocalEcho) {EXPECT_EQ(messages[0].handle, "m9") << "the phone's copy is the one with a usable handle";}+// iOS has been observed recording an Apple-ID recipient with characters missing+// from the front of the address it was sent. Filing the phone's copy under that+// address opens a second conversation nobody can reply to, so the local echo --+// which knows what was actually addressed -- carries the peer over.+TEST(MessageStore, PhoneCopyWithATruncatedAddressJoinsTheRealConversation) {+ MessageStore store;+ Message echo = local_send("email:[email protected]", "test3", 1000);+ echo.peer_address = "[email protected]";+ Message copy = phone_copy("m9", "email:[email protected]", "test3", 1005);+ copy.peer_address = "[email protected]";++ ASSERT_TRUE(store.add(echo));+ ASSERT_TRUE(store.add(copy));++ EXPECT_EQ(store.size(), 1u);+ EXPECT_EQ(store.threads().size(), 1u) << "the mangled address opened a second conversation";+ auto messages = store.messages("email:[email protected]");+ ASSERT_EQ(messages.size(), 1u);+ EXPECT_EQ(messages[0].handle, "m9");+ EXPECT_EQ(messages[0].peer_address, "[email protected]");+}+TEST(MessageStore, UnrelatedOutgoingMessagesAreNotCollapsed) {MessageStore store;store.add(local_send("tel:+1111", "on my way", 1000));diff --git a/test/test_bmessage_build.cpp b/test/test_bmessage_build.cppindex 4e7e623..719e58c 100644--- a/test/test_bmessage_build.cpp+++ b/test/test_bmessage_build.cpp@@ -47,6 +47,17 @@ TEST(BMessageBuild, AlwaysUsesSmsGsmEvenForAppleIdRecipients) {EXPECT_EQ(out.find("TEL:"), std::string::npos);}+// The address the phone acts on has to survive a round trip through the same+// parser that reads incoming messages, parameters and all.+TEST(BMessageBuild, EmailRecipientSurvivesAParseOfWhatWasBuilt) {+ const std::string address = "[email protected]";+ const BMessage parsed = parse_bmessage(build_bmessage({email(address)}, "hi"));++ ASSERT_TRUE(parsed.valid);+ ASSERT_EQ(parsed.recipients.size(), 1u);+ EXPECT_EQ(parsed.recipients.front().email, address) << "the recipient lost characters between build and parse";+}+TEST(BMessageBuild, LengthCountsTheWholeMessageBlock) {const std::string out = build_bmessage({tel("+15551234567")}, "hello");