Linux + iPhone Continuity / iMessage / SMS
feat(protocol): correlate Bluetooth message send results
Nick Pisani86a95ec5ba95
diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.mdindex d1a69c3..be94954 100644--- a/docs/PROTOCOL.md+++ b/docs/PROTOCOL.md@@ -525,9 +525,12 @@ Writes through to the phone over OBEX, so it answers asynchronously with a`bt_message_read` event carrying `success` and, on failure, `message`.#### `bt_send_message` (Client -> Daemon, broadcast)-**Payload**: `{"command": "bt_send_message", "thread": "tel:+15035550101", "body": "on my way"}`+**Payload**: `{"command": "bt_send_message", "thread": "tel:+15035550101", "body": "on my way", "operation_id": "send-1"}`Builds a bMessage and pushes it to the phone's outbox. Answers asynchronously with-`bt_send_result` (`success`, and `message` when it failed).+`bt_send_result` (`thread`, `success`, and `message` when it failed). The optional+`operation_id` is echoed in that result, including on failure; legacy clients+may omit it. It correlates a local client's send with the result and does not+authorize the request.Recipients are validated before they are interpolated into the bMessage: an addresscontaining CR, LF, or a vCard delimiter is rejected rather than escaped, because such andiff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex ac0ed89..32d8c2b 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -1327,14 +1327,17 @@ namespace tether {j.contains("body")) {std::string thread = j["thread"];std::string body = j["body"];+ const std::string operation_id = j.value("operation_id", std::string{});// PushMessage is a blocking OBEX transfer, so it cannot// run on the loop that has to keep serving the UI.- std::thread([thread, body]() {+ std::thread([thread, body, operation_id]() {std::string err;bluetooth::Message sent;nlohmann::json event;event["command"] = "bt_send_result";event["thread"] = thread;+ if (!operation_id.empty())+ event["operation_id"] = operation_id;event["success"] = bluetooth::send_message(thread, body, sent, err);if (event["success"]) {nlohmann::json message = bluetooth::to_json(sent);diff --git a/test/test_net.cpp b/test/test_net.cppindex 04819bd..2e00d73 100644--- a/test/test_net.cpp+++ b/test/test_net.cpp@@ -469,6 +469,65 @@ namespace {close(client);}+ TEST(UnixServerTest, MessageSendResultEchoesOptionalOperationId) {+ const std::string runtime_dir = unique_test_dir("tether_unix_message_send_test");+ CleanupGuard cleanup_guard(runtime_dir);+ std::filesystem::remove_all(runtime_dir);+ std::filesystem::create_directories(runtime_dir);+ ScopedEnvVar xdg_runtime_dir("XDG_RUNTIME_DIR", runtime_dir);++ tether::EpollEventLoop loop;+ tether::TcpServer tcp_server(loop, 0);+ tether::UnixServer unix_server(loop, tcp_server);+ ASSERT_TRUE(unix_server.start());+ EventLoopGuard loop_guard(loop);++ const int client = socket(AF_UNIX, SOCK_STREAM, 0);+ ASSERT_GE(client, 0);+ timeval timeout{2, 0};+ ASSERT_EQ(setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)), 0);+ sockaddr_un address{};+ address.sun_family = AF_UNIX;+ std::snprintf(+ address.sun_path, sizeof(address.sun_path), "%s", (tether::get_runtime_dir() + "/tetherd.sock").c_str());+ ASSERT_EQ(connect(client, reinterpret_cast<sockaddr*>(&address), sizeof(address)), 0);++ const std::string subscribe = "{\"command\":\"subscribe\"}\n";+ ASSERT_EQ(write(client, subscribe.data(), subscribe.size()), static_cast<ssize_t>(subscribe.size()));+ ASSERT_FALSE(read_socket_line(client).empty());++ for (const std::string operation_id : {"web-message-1", ""}) {+ nlohmann::json send = {{"command", "bt_send_message"}, {"thread", "invalid"}, {"body", "test"}};+ if (!operation_id.empty())+ send["operation_id"] = operation_id;+ const std::string payload = send.dump() + "\n";+ ASSERT_EQ(write(client, payload.data(), payload.size()), static_cast<ssize_t>(payload.size()));++ nlohmann::json result;+ for (int attempt = 0; attempt < 8; ++attempt) {+ const std::string line = read_socket_line(client);+ if (line.empty())+ break;+ try {+ auto candidate = nlohmann::json::parse(line);+ if (candidate.value("command", "") == "bt_send_result") {+ result = std::move(candidate);+ break;+ }+ } catch (...) {+ }+ }+ ASSERT_FALSE(result.is_null());+ EXPECT_FALSE(result.value("success", true));+ EXPECT_EQ(result.value("thread", ""), "invalid");+ if (operation_id.empty())+ EXPECT_FALSE(result.contains("operation_id"));+ else+ EXPECT_EQ(result.value("operation_id", ""), operation_id);+ }+ close(client);+ }+TEST(ControlProtocolTest, AdvertisesVersionedCapabilities) {const auto info = tether::build_protocol_info();EXPECT_EQ(info.value("command", ""), "protocol_info");