Add LoRa telemetry, fault counters, and time sync status

This commit is contained in:
2026-01-30 13:00:16 +01:00
parent 7e3b537e49
commit 8ba7675a1c
13 changed files with 437 additions and 21 deletions

View File

@@ -7,6 +7,14 @@
static bool g_time_synced = false;
static bool g_tz_set = false;
static bool g_rtc_present = false;
static uint32_t g_last_sync_utc = 0;
static void note_last_sync(uint32_t epoch) {
if (epoch == 0) {
return;
}
g_last_sync_utc = epoch;
}
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2) {
const char *server1 = (ntp_server_1 && ntp_server_1[0] != '\0') ? ntp_server_1 : "pool.ntp.org";
@@ -24,6 +32,10 @@ uint32_t time_get_utc() {
if (now < 1672531200) {
return 0;
}
if (!g_time_synced) {
g_time_synced = true;
note_last_sync(static_cast<uint32_t>(now));
}
return static_cast<uint32_t>(now);
}
@@ -42,16 +54,17 @@ void time_set_utc(uint32_t epoch) {
tv.tv_usec = 0;
settimeofday(&tv, nullptr);
g_time_synced = true;
note_last_sync(epoch);
if (g_rtc_present) {
rtc_ds3231_set_epoch(epoch);
}
}
void time_send_timesync(uint16_t device_id_short) {
bool time_send_timesync(uint16_t device_id_short) {
uint32_t epoch = time_get_utc();
if (epoch == 0) {
return;
return false;
}
char payload_str[32];
@@ -60,7 +73,7 @@ void time_send_timesync(uint16_t device_id_short) {
uint8_t compressed[LORA_MAX_PAYLOAD];
size_t compressed_len = 0;
if (!compressBuffer(reinterpret_cast<const uint8_t *>(payload_str), strlen(payload_str), compressed, sizeof(compressed), compressed_len)) {
return;
return false;
}
LoraPacket pkt = {};
@@ -70,7 +83,7 @@ void time_send_timesync(uint16_t device_id_short) {
pkt.payload_type = PayloadType::TimeSync;
pkt.payload_len = compressed_len;
memcpy(pkt.payload, compressed, compressed_len);
lora_send(pkt);
return lora_send(pkt);
}
bool time_handle_timesync_payload(const uint8_t *payload, size_t len) {
@@ -134,3 +147,18 @@ bool time_try_load_from_rtc() {
bool time_rtc_present() {
return g_rtc_present;
}
uint32_t time_get_last_sync_utc() {
return g_last_sync_utc;
}
uint32_t time_get_last_sync_age_sec() {
if (!time_is_synced()) {
return 0;
}
if (g_last_sync_utc == 0) {
return 0;
}
uint32_t now = time_get_utc();
return now > g_last_sync_utc ? now - g_last_sync_utc : 0;
}