firmware: SX1262 GFSK wM-Bus C1 repeater, dev tooling, discovery scripts
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// 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"
|
||||
|
||||
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, "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, "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);
|
||||
}
|
||||
Reference in New Issue
Block a user