Add repeater protocol and device firmware features
This commit is contained in:
+108
-75
@@ -1,13 +1,16 @@
|
||||
// Line-based scriptable serial console on USB-Serial-JTAG.
|
||||
// Commands (one per line):
|
||||
// status | stats | clear_stats | radio_status | radio_reset | test_tx
|
||||
// status | stats | clear_stats | radio_status | radio_reset | test_tx | test_tx_t1
|
||||
// set repeat_delay_ms N | set tx_power N | set repeat_enabled 0|1
|
||||
// set verbose 0|1 | set whitelist ID[,ID...] | set whitelist clear
|
||||
// set beacon_period_ms N (0=off, uses test_lfield) | set duty_budget_ppm N
|
||||
// set fem_rx_csd 0|1 | set preamble_bits 0|8|16|24|32 | set test_lfield N
|
||||
// reboot
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
@@ -15,7 +18,7 @@
|
||||
#include "driver/usb_serial_jtag_vfs.h"
|
||||
|
||||
#include "repeater.h"
|
||||
#include "sx126x.h"
|
||||
#include "settings.h"
|
||||
|
||||
static void ack(const char *cmd, bool ok, const char *detail)
|
||||
{
|
||||
@@ -25,81 +28,100 @@ static void ack(const char *cmd, bool ok, const char *detail)
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
static bool parse_integer(const char *text, int32_t *out)
|
||||
{
|
||||
const char *digits = text;
|
||||
if (*digits == '-' || *digits == '+') digits++;
|
||||
if (!*digits) return false;
|
||||
for (const char *p = digits; *p; p++) {
|
||||
if (*p < '0' || *p > '9') return false;
|
||||
}
|
||||
char *end;
|
||||
errno = 0;
|
||||
long value = strtol(text, &end, 10);
|
||||
if (errno == ERANGE || *end || value < INT32_MIN || value > INT32_MAX) return false;
|
||||
*out = (int32_t)value;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool parse_whitelist(const char *text, settings_change_t *change)
|
||||
{
|
||||
if (strcmp(text, "clear") == 0) return true;
|
||||
for (;;) {
|
||||
if (change->whitelist_len == 8) return false;
|
||||
uint32_t id = 0;
|
||||
for (unsigned i = 0; i < 8; i++) {
|
||||
unsigned char c = (unsigned char)*text++;
|
||||
unsigned digit;
|
||||
if (c >= '0' && c <= '9') digit = c - '0';
|
||||
else if (c >= 'a' && c <= 'f') digit = c - 'a' + 10;
|
||||
else if (c >= 'A' && c <= 'F') digit = c - 'A' + 10;
|
||||
else return false;
|
||||
id = (id << 4) | digit;
|
||||
}
|
||||
change->whitelist[change->whitelist_len++] = id;
|
||||
if (!*text) return true;
|
||||
if (*text++ != ',' || !*text) return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_set(char *args)
|
||||
{
|
||||
char *key = strtok(args, " ");
|
||||
char *val = strtok(NULL, " ");
|
||||
if (!key || !val) { ack("set", false, "usage"); return; }
|
||||
|
||||
if (strcmp(key, "repeat_delay_ms") == 0) {
|
||||
long v = strtol(val, NULL, 10);
|
||||
if (v < 20 || v > 200) { ack("set", false, "range 20..200"); return; }
|
||||
g_cfg.repeat_delay_ms = (uint32_t)v;
|
||||
ack("set repeat_delay_ms", true, NULL);
|
||||
} else if (strcmp(key, "tx_power") == 0) {
|
||||
long v = strtol(val, NULL, 10);
|
||||
if (v < -9 || v > 14) { ack("set", false, "range -9..14"); return; }
|
||||
g_cfg.tx_power_dbm = (int8_t)v;
|
||||
repeater_request_radio_reset();
|
||||
ack("set tx_power", true, "radio reinit queued");
|
||||
} else if (strcmp(key, "repeat_enabled") == 0) {
|
||||
g_cfg.repeat_enabled = (val[0] == '1');
|
||||
ack("set repeat_enabled", true, NULL);
|
||||
} else if (strcmp(key, "duty_budget_ppm") == 0) {
|
||||
long v = strtol(val, NULL, 10);
|
||||
if (v < 0 || v > 100000) { ack("set", false, "range 0..100000 ppm"); return; }
|
||||
g_cfg.duty_budget_ppm = (uint32_t)v;
|
||||
ack("set duty_budget_ppm", true, v == 0 ? "guard disabled" : NULL);
|
||||
} else if (strcmp(key, "beacon_period_ms") == 0) {
|
||||
long v = strtol(val, NULL, 10);
|
||||
if (v != 0 && (v < 250 || v > 600000)) { ack("set", false, "0 or 250..600000 ms"); return; }
|
||||
g_cfg.beacon_period_ms = (uint32_t)v;
|
||||
ack("set beacon_period_ms", true, v == 0 ? "beacon off" : NULL);
|
||||
} else if (strcmp(key, "preamble_bits") == 0) {
|
||||
long v = strtol(val, NULL, 10);
|
||||
uint8_t code;
|
||||
switch (v) {
|
||||
case 0: code = 0x00; break;
|
||||
case 8: code = 0x04; break;
|
||||
case 16: code = 0x05; break;
|
||||
case 24: code = 0x06; break;
|
||||
case 32: code = 0x07; break;
|
||||
default: ack("set", false, "use 0|8|16|24|32"); return;
|
||||
}
|
||||
sx126x_set_preamble_detect(code);
|
||||
repeater_request_radio_reset();
|
||||
ack("set preamble_bits", true, "radio reinit queued");
|
||||
} else if (strcmp(key, "pa_low") == 0) {
|
||||
sx126x_set_pa_low(val[0] == '1');
|
||||
repeater_request_radio_reset();
|
||||
ack("set pa_low", true, "radio reinit queued");
|
||||
} else if (strcmp(key, "test_lfield") == 0) {
|
||||
long v = strtol(val, NULL, 10);
|
||||
if (v < 19 || v > 200) { ack("set", false, "range 19..200"); return; }
|
||||
repeater_set_test_lfield((uint8_t)v);
|
||||
ack("set test_lfield", true, NULL);
|
||||
} else if (strcmp(key, "fem_rx_csd") == 0) {
|
||||
fem_set_rx_csd(val[0] == '1');
|
||||
ack("set fem_rx_csd", true, "applies at next RX entry");
|
||||
} else if (strcmp(key, "verbose") == 0) {
|
||||
g_cfg.verbose = (val[0] == '1');
|
||||
ack("set verbose", true, NULL);
|
||||
} else if (strcmp(key, "whitelist") == 0) {
|
||||
if (strcmp(val, "clear") == 0) {
|
||||
g_cfg.whitelist_len = 0;
|
||||
ack("set whitelist", true, "cleared");
|
||||
return;
|
||||
}
|
||||
int n = 0;
|
||||
for (char *tok = strtok(val, ","); tok && n < 8; tok = strtok(NULL, ",")) {
|
||||
// meter ID as printed (BCD hex digits) -> stored LE uint32
|
||||
uint32_t id = (uint32_t)strtoul(tok, NULL, 16);
|
||||
g_cfg.whitelist[n++] = id;
|
||||
}
|
||||
g_cfg.whitelist_len = n;
|
||||
ack("set whitelist", true, NULL);
|
||||
} else {
|
||||
char *save;
|
||||
char *key = strtok_r(args, " \t", &save);
|
||||
char *val = strtok_r(NULL, " \t", &save);
|
||||
if (!key || !val || strtok_r(NULL, " \t", &save)) {
|
||||
ack("set", false, "usage: set KEY VALUE");
|
||||
return;
|
||||
}
|
||||
static const struct {
|
||||
const char *name;
|
||||
settings_key_t key;
|
||||
const char *range;
|
||||
} keys[] = {
|
||||
{"repeat_delay_ms", SETTINGS_REPEAT_DELAY_MS, "integer 20..1000"},
|
||||
{"tx_power", SETTINGS_TX_POWER, "integer -9..2"},
|
||||
{"repeat_enabled", SETTINGS_REPEAT_ENABLED, "use 0|1"},
|
||||
{"verbose", SETTINGS_VERBOSE, "use 0|1"},
|
||||
{"fem_rx_csd", SETTINGS_FEM_RX_CSD, "use 0|1"},
|
||||
{"preamble_bits", SETTINGS_PREAMBLE_BITS, "use 0|8|16|24|32"},
|
||||
{"duty_budget_ppm", SETTINGS_DUTY_BUDGET_PPM, "integer 0..100000 ppm"},
|
||||
{"test_lfield", SETTINGS_TEST_LFIELD, "integer 19..200"},
|
||||
{"whitelist", SETTINGS_WHITELIST, "clear or 1..8 unique eight-hex-digit IDs"},
|
||||
{"beacon_period_ms", SETTINGS_BEACON_PERIOD_MS, "integer 0 or 250..600000 ms"},
|
||||
};
|
||||
const size_t count = sizeof(keys) / sizeof(keys[0]);
|
||||
size_t index = 0;
|
||||
while (index < count && strcmp(key, keys[index].name) != 0) index++;
|
||||
if (index == count) {
|
||||
ack("set", false, "unknown key");
|
||||
return;
|
||||
}
|
||||
settings_change_t change = {.key = keys[index].key};
|
||||
bool parsed;
|
||||
if (change.key == SETTINGS_WHITELIST) {
|
||||
parsed = parse_whitelist(val, &change);
|
||||
} else if (change.key == SETTINGS_REPEAT_ENABLED || change.key == SETTINGS_VERBOSE
|
||||
|| change.key == SETTINGS_FEM_RX_CSD) {
|
||||
parsed = strcmp(val, "0") == 0 || strcmp(val, "1") == 0;
|
||||
change.value = val[0] == '1';
|
||||
} else {
|
||||
parsed = parse_integer(val, &change.value);
|
||||
}
|
||||
settings_result_t result = parsed ? settings_update(&change, 0, NULL) : SETTINGS_INVALID;
|
||||
char command[48];
|
||||
snprintf(command, sizeof(command), "set %s", keys[index].name);
|
||||
switch (result) {
|
||||
case SETTINGS_OK:
|
||||
ack(command, true,
|
||||
change.key == SETTINGS_REPEAT_DELAY_MS || change.key == SETTINGS_DUTY_BUDGET_PPM
|
||||
? "persisted; accepted for radio loop boundary"
|
||||
: "accepted for radio loop boundary");
|
||||
break;
|
||||
case SETTINGS_INVALID: ack(command, false, keys[index].range); break;
|
||||
case SETTINGS_CONFLICT: ack(command, false, "settings changed; reload status"); break;
|
||||
case SETTINGS_STORAGE_ERROR: ack(command, false, "storage failed; settings unchanged"); break;
|
||||
case SETTINGS_BUSY: ack(command, false, "settings busy; unchanged"); break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +134,13 @@ static void console_task(void *arg)
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
continue;
|
||||
}
|
||||
if (!strpbrk(line, "\r\n")) {
|
||||
// Never apply the valid-looking prefix of an oversized command.
|
||||
int c;
|
||||
while ((c = getchar()) != '\n' && c != '\r' && c != EOF) {}
|
||||
ack("set", false, "line too long");
|
||||
continue;
|
||||
}
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
if (line[0] == 0) continue;
|
||||
|
||||
@@ -144,13 +173,17 @@ static void console_task(void *arg)
|
||||
repeater_request_test_tx();
|
||||
ack("test_tx", true, "queued");
|
||||
}
|
||||
else if (strcmp(line, "test_tx_t1") == 0) {
|
||||
repeater_request_test_tx_t1();
|
||||
ack("test_tx_t1", true, "queued");
|
||||
}
|
||||
else if (strncmp(line, "set ", 4) == 0) handle_set(line + 4);
|
||||
else if (strcmp(line, "reboot") == 0) {
|
||||
ack("reboot", true, NULL);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
esp_restart();
|
||||
}
|
||||
else ack(line, false, "unknown command");
|
||||
else ack("unknown", false, "unknown command");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user