From d49443f66ca976cd47e8a404271d2540c7c915a1 Mon Sep 17 00:00:00 2001 From: UA1ZBE Date: Wed, 22 Jul 2026 13:55:05 +0300 Subject: [PATCH] auto beacon every hour + on startup via AdvertDataBuilder --- src/main.cpp | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index adb791f..8393f35 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include "target.h" @@ -65,6 +66,7 @@ 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; @@ -295,9 +297,8 @@ public: saveDeviceName(); defer_response(g_device_name); } else if (strcmp(cmd, "beacon") == 0) { - 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"); + sendBeacon(); + defer_response("beacon ok"); } else if (strcmp(cmd, "reboot") == 0) { send_text("rebooting"); delay(100); @@ -308,17 +309,23 @@ 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]; - 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; + AdvertDataParser parser(app_data, app_data_len); + if (parser.isValid() && parser.hasName()) { + snprintf(buf, sizeof(buf), "beacon:%s", parser.getName()); } 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); @@ -399,6 +406,10 @@ 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");