auto beacon every hour + on startup via AdvertDataBuilder
This commit is contained in:
29
src/main.cpp
29
src/main.cpp
@@ -9,6 +9,7 @@
|
||||
#include <helpers/StaticPoolPacketManager.h>
|
||||
#include <helpers/ChannelDetails.h>
|
||||
#include <helpers/ArduinoSerialInterface.h>
|
||||
#include <helpers/AdvertDataHelpers.h>
|
||||
#include <Crypto.h>
|
||||
#include <SHA256.h>
|
||||
#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");
|
||||
|
||||
Reference in New Issue
Block a user