auto beacon every hour + on startup via AdvertDataBuilder

This commit is contained in:
UA1ZBE
2026-07-22 13:55:05 +03:00
parent 8b21bccac2
commit d49443f66c

View File

@@ -9,6 +9,7 @@
#include <helpers/StaticPoolPacketManager.h> #include <helpers/StaticPoolPacketManager.h>
#include <helpers/ChannelDetails.h> #include <helpers/ChannelDetails.h>
#include <helpers/ArduinoSerialInterface.h> #include <helpers/ArduinoSerialInterface.h>
#include <helpers/AdvertDataHelpers.h>
#include <Crypto.h> #include <Crypto.h>
#include <SHA256.h> #include <SHA256.h>
#include "target.h" #include "target.h"
@@ -65,6 +66,7 @@ static unsigned long g_boot_time = 0;
static bool g_startup_msg_sent = false; static bool g_startup_msg_sent = false;
static unsigned long g_auto_status_interval = 0; static unsigned long g_auto_status_interval = 0;
static unsigned long g_last_auto_status = 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; }; struct SecurityConfig { uint32_t magic; uint8_t armed; uint8_t relay2; };
static const uint32_t CFG_MAGIC = 0x53454355; static const uint32_t CFG_MAGIC = 0x53454355;
@@ -295,9 +297,8 @@ public:
saveDeviceName(); saveDeviceName();
defer_response(g_device_name); defer_response(g_device_name);
} else if (strcmp(cmd, "beacon") == 0) { } else if (strcmp(cmd, "beacon") == 0) {
auto pkt = createAdvert(self_id, (const uint8_t*)g_device_name, strlen(g_device_name)); sendBeacon();
if (pkt) { sendFlood(pkt); defer_response("beacon ok"); } defer_response("beacon ok");
else defer_response("beacon fail");
} else if (strcmp(cmd, "reboot") == 0) { } else if (strcmp(cmd, "reboot") == 0) {
send_text("rebooting"); send_text("rebooting");
delay(100); 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 { 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]; char buf[128];
int n = 0; AdvertDataParser parser(app_data, app_data_len);
n += snprintf(buf + n, sizeof(buf) - n, "beacon:"); if (parser.isValid() && parser.hasName()) {
if (app_data_len > 0 && app_data[0]) { snprintf(buf, sizeof(buf), "beacon:%s", parser.getName());
int l = app_data_len; if (l > 20) l = 20;
memcpy(buf + n, app_data, l); n += l;
} else { } 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]); } 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); 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); SecurityMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables);
@@ -399,6 +406,10 @@ void loop() {
} }
unsigned long now = millis(); 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); int reed = digitalRead(PIN_REED);
if (g_armed && reed == HIGH && !g_hall_alert && now - g_last_hall_alert > ALERT_COOLDOWN_MS) { 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"); g_hall_alert = true; g_last_hall_alert = now; the_mesh.send_text("alert:hall");