// Line-based scriptable serial console on USB-Serial-JTAG. // Commands (one per line): // 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 #include #include #include #include #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 "settings.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 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 *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; } } static void console_task(void *arg) { (void)arg; char line[256]; for (;;) { if (!fgets(line, sizeof(line), stdin)) { 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; 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 (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("unknown", 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); }