Add repeater protocol and device firmware features

This commit is contained in:
Lars Nolden
2026-09-21 12:30:50 +02:00
parent 73d3ddf00c
commit a11a4b3b89
41 changed files with 7791 additions and 228 deletions
+48
View File
@@ -0,0 +1,48 @@
#include "duty_ledger.h"
#include <limits.h>
#include <stdbool.h>
uint64_t duty_ledger_used(const duty_ledger_t *ledger, int64_t now_us)
{
if (now_us < 0) return 0;
const uint64_t now = (uint64_t)now_us;
const bool has_full_window = now >= DUTY_LEDGER_WINDOW_US;
const uint64_t cutoff = has_full_window ? now - DUTY_LEDGER_WINDOW_US : 0;
uint64_t sum = 0;
for (int i = 0; i < DUTY_LEDGER_BUCKETS; i++) {
const duty_ledger_bucket_t *bucket = &ledger->buckets[i];
if (bucket->minute_key == 0) continue;
const uint64_t minute = bucket->minute_key - 1;
const uint64_t start = minute * DUTY_LEDGER_BUCKET_US;
if (start > now) continue;
if (has_full_window && start + DUTY_LEDGER_BUCKET_US <= cutoff) continue;
if (UINT64_MAX - sum < bucket->airtime_us) return UINT64_MAX;
sum += bucket->airtime_us;
}
return sum;
}
void duty_ledger_record(duty_ledger_t *ledger, int64_t now_us,
uint32_t airtime_us)
{
if (now_us < 0 || airtime_us == 0) return;
const uint64_t minute = (uint64_t)now_us / DUTY_LEDGER_BUCKET_US;
duty_ledger_bucket_t *bucket = &ledger->buckets[minute % DUTY_LEDGER_BUCKETS];
const uint64_t key = minute + 1;
if (bucket->minute_key != key) {
bucket->minute_key = key;
bucket->airtime_us = 0;
}
if (UINT64_MAX - bucket->airtime_us < airtime_us) {
bucket->airtime_us = UINT64_MAX;
} else {
bucket->airtime_us += airtime_us;
}
}