159 lines
6.1 KiB
C
159 lines
6.1 KiB
C
// Line-based scriptable serial console on USB-Serial-JTAG.
|
|
// Commands (one per line):
|
|
// status | stats | clear_stats | radio_status | radio_reset | test_tx
|
|
// 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
|
|
// reboot
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_system.h"
|
|
#include "driver/usb_serial_jtag.h"
|
|
#include "driver/usb_serial_jtag_vfs.h"
|
|
|
|
#include "repeater.h"
|
|
#include "sx126x.h"
|
|
|
|
static void ack(const char *cmd, bool ok, const char *detail)
|
|
{
|
|
printf("{\"event\":\"cmd_ack\",\"cmd\":\"%s\",\"ok\":%s%s%s%s}\n",
|
|
cmd, ok ? "true" : "false",
|
|
detail ? ",\"detail\":\"" : "", detail ? detail : "", detail ? "\"" : "");
|
|
fflush(stdout);
|
|
}
|
|
|
|
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, "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 {
|
|
ack("set", false, "unknown key");
|
|
}
|
|
}
|
|
|
|
static void console_task(void *arg)
|
|
{
|
|
(void)arg;
|
|
char line[256];
|
|
for (;;) {
|
|
if (!fgets(line, sizeof(line), stdin)) {
|
|
vTaskDelay(pdMS_TO_TICKS(50));
|
|
continue;
|
|
}
|
|
line[strcspn(line, "\r\n")] = 0;
|
|
if (line[0] == 0) continue;
|
|
|
|
if (strcmp(line, "status") == 0) repeater_print_status();
|
|
else if (strcmp(line, "stats") == 0) repeater_print_stats();
|
|
else if (strcmp(line, "clear_stats") == 0) {
|
|
memset(&g_stats, 0, sizeof(g_stats));
|
|
ack("clear_stats", true, NULL);
|
|
}
|
|
else if (strcmp(line, "radio_status") == 0) repeater_print_radio_status();
|
|
else if (strcmp(line, "radio_reset") == 0) {
|
|
repeater_request_radio_reset();
|
|
ack("radio_reset", true, "queued");
|
|
}
|
|
else if (strcmp(line, "probe") == 0) {
|
|
repeater_request_probe();
|
|
ack("probe", true, "queued");
|
|
}
|
|
else if (strncmp(line, "inject ", 7) == 0) {
|
|
const char *w = line + 7;
|
|
if (strcmp(w, "standby") == 0) { repeater_inject_fault(1); ack("inject standby", true, "queued"); }
|
|
else if (strcmp(w, "sleep") == 0) { repeater_inject_fault(2); ack("inject sleep", true, "queued"); }
|
|
else ack("inject", false, "use standby|sleep");
|
|
}
|
|
else if (strcmp(line, "test_burst") == 0) {
|
|
repeater_request_test_burst();
|
|
ack("test_burst", true, "queued");
|
|
}
|
|
else if (strcmp(line, "test_tx") == 0) {
|
|
repeater_request_test_tx();
|
|
ack("test_tx", 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");
|
|
}
|
|
}
|
|
|
|
void console_start(void)
|
|
{
|
|
// Blocking stdin via USB-Serial-JTAG driver
|
|
usb_serial_jtag_driver_config_t cfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
|
|
usb_serial_jtag_driver_install(&cfg);
|
|
usb_serial_jtag_vfs_use_driver();
|
|
xTaskCreate(console_task, "console", 4096, NULL, 5, NULL);
|
|
}
|