diff --git a/src/main.cpp b/src/main.cpp index 8393f35..adb791f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include "target.h" @@ -66,7 +65,6 @@ static unsigned long g_boot_time = 0; static bool g_startup_msg_sent = false; static unsigned long g_auto_status_interval = 0; static unsigned long g_last_auto_status = 0; -static unsigned long g_last_beacon_ms = 0; struct SecurityConfig { uint32_t magic; uint8_t armed; uint8_t relay2; }; static const uint32_t CFG_MAGIC = 0x53454355; @@ -297,8 +295,9 @@ public: saveDeviceName(); defer_response(g_device_name); } else if (strcmp(cmd, "beacon") == 0) { - sendBeacon(); - defer_response("beacon ok"); + auto pkt = createAdvert(self_id, (const uint8_t*)g_device_name, strlen(g_device_name)); + if (pkt) { sendFlood(pkt); defer_response("beacon ok"); } + else defer_response("beacon fail"); } else if (strcmp(cmd, "reboot") == 0) { send_text("rebooting"); delay(100); @@ -309,23 +308,17 @@ public: void onAdvertRecv(mesh::Packet* pkt, const mesh::Identity& id, uint32_t ts, const uint8_t* app_data, size_t app_data_len) override { char buf[128]; - AdvertDataParser parser(app_data, app_data_len); - if (parser.isValid() && parser.hasName()) { - snprintf(buf, sizeof(buf), "beacon:%s", parser.getName()); + int n = 0; + n += snprintf(buf + n, sizeof(buf) - n, "beacon:"); + if (app_data_len > 0 && app_data[0]) { + int l = app_data_len; if (l > 20) l = 20; + memcpy(buf + n, app_data, l); n += l; } else { - int n = snprintf(buf, sizeof(buf), "beacon:"); for (int i = 0; i < 4 && i < (int)PUB_KEY_SIZE; i++) { n += snprintf(buf + n, sizeof(buf) - n, "%02X", id.pub_key[i]); } } + buf[n] = 0; send_text(buf); } - - void sendBeacon() { - uint8_t app_data[MAX_ADVERT_DATA_SIZE]; - AdvertDataBuilder builder(ADV_TYPE_SENSOR, g_device_name[0] ? g_device_name : "NONAME"); - uint8_t app_data_len = builder.encodeTo(app_data); - auto pkt = createAdvert(self_id, app_data, app_data_len); - if (pkt) sendFlood(pkt, (uint32_t)0, (uint8_t)1); - } }; SecurityMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables); @@ -406,10 +399,6 @@ void loop() { } unsigned long now = millis(); - if (now - g_last_beacon_ms >= 3600000 || g_last_beacon_ms == 0) { - the_mesh.sendBeacon(); - g_last_beacon_ms = now; - } int reed = digitalRead(PIN_REED); if (g_armed && reed == HIGH && !g_hall_alert && now - g_last_hall_alert > ALERT_COOLDOWN_MS) { g_hall_alert = true; g_last_hall_alert = now; the_mesh.send_text("alert:hall");