Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: segfault on no anc

Zack Bartel5a3b43433e4d
diff --git a/src/core/src/bluetooth/airpods.cpp b/src/core/src/bluetooth/airpods.cpp
index a0bfd45..84ffb58 100644
--- a/src/core/src/bluetooth/airpods.cpp
+++ b/src/core/src/bluetooth/airpods.cpp
@@ -148,19 +148,21 @@ namespace tether::bluetooth {
}
nlohmann::json to_json(const AirPodsState& s) {
- return {
+ nlohmann::json j = {
{"command", "bt_airpods"},
{"address", s.address},
{"name", s.name},
{"left", s.battery.left},
{"right", s.battery.right},
{"case", s.battery.case_},
- {"anc", s.anc ? nlohmann::json(to_string(*s.anc)) : nlohmann::json()},
{"ear", {{"primary", to_string(s.ear.primary)}, {"secondary", to_string(s.ear.secondary)}}},
{"in_ear", s.ear.in_ear()},
{"status", to_string(s.status)},
{"reason", s.reason},
};
+ if (s.anc)
+ j["anc"] = to_string(*s.anc);
+ return j;
}
const char* to_string(AncMode mode) {
diff --git a/test/test_airpods.cpp b/test/test_airpods.cpp
index 6063c15..7311afc 100644
--- a/test/test_airpods.cpp
+++ b/test/test_airpods.cpp
@@ -250,3 +250,16 @@ TEST(AirPods, DistinguishesAirPodsFromAnIPhone) {
speaker.uuids = {UUID_A2DP_SINK};
EXPECT_FALSE(speaker.looks_like_airpods());
}
+
+TEST(AirPods, JsonOmitsUnknownListeningMode) {
+ AirPodsState state;
+ ASSERT_FALSE(state.anc.has_value());
+
+ const nlohmann::json j = to_json(state);
+ EXPECT_FALSE(j.contains("anc"));
+ // Readers ask for it as a string; a null would throw instead of defaulting.
+ EXPECT_EQ(j.value("anc", ""), "");
+
+ state.anc = AncMode::Transparency;
+ EXPECT_EQ(to_json(state).value("anc", ""), "transparency");
+}