Allow local CLI maintenance across direct connections

This commit is contained in:
mikecarper committed 2026-09-08 21:44:57 -07:00
1 parent 001b4985f7
commit 06c51fdef5
39 files changed
+3338 -2312

No files matched your search

+120 -4
View File
@@ -8,6 +8,7 @@
#include <helpers/IdentityGeneration.h>
#include <helpers/LazyPersistence.h>
#include <helpers/StorageLayout.h>
#include <helpers/StatsFormatHelper.h>
#include <helpers/UsbAsciiBinarySwitch.h>
#include <helpers/UsbLogging.h>
#include "helpers/radiolib/RXPowerSaving.h"
@@ -54,8 +55,10 @@
#include <helpers/MQTTDefaults.h>
#endif
#ifdef WITH_WEBCONFIG
#if defined(ESP32) && defined(WIFI_SSID)
#include <helpers/WiFiSetupPortal.h>
#endif
#ifdef WITH_WEBCONFIG
#include <WiFi.h>
#include <esp_wifi.h>
#endif
@@ -1071,7 +1074,8 @@ void MyMesh::onCLICommandRecv(const ContactInfo &from, mesh::Packet *pkt, uint32
const char *text, char* reply) {
markConnectionActive(from); // in case this is from a server, and we have a connection
if (from.isRemoteCLIAllowed()) {
if (!handleCommand(text, sender_timestamp, reply)) {
// A timestamp received over the radio must never grant local-console access.
if (!handleCommand(text, sender_timestamp ? sender_timestamp : 1, reply)) {
strcat(reply, "Unknown command"); // reply may have cmd prefix from 'text'
}
} else {
@@ -3378,7 +3382,7 @@ void MyMesh::execAdminCommand(char* cmd, char* reply) {
strcpy(reply, "Error: enter a plain CLI command");
return;
}
if (handleCommand(cmd, 1, reply)) return;
if (handleCommand(cmd, 0, reply)) return;
if (strcmp(cmd, "get radio.rxps") == 0 || strcmp(cmd, "get radio.rxps.config") == 0) {
if (!radio_driver.supportsRxPowerSaving()) {
strcpy(reply, "Error: RX power saving is unsupported on this radio");
@@ -7410,6 +7414,10 @@ void MyMesh::handleTerminalCommand(char* command) {
#endif
char local_reply[160];
if (handleDirectCommand(command, local_reply, sizeof(local_reply))) {
terminalOutput().printf(" %s\r\n", local_reply);
return;
}
#if COMPANION_FEATURE_USB_MOTA_SOURCE
const bool usb_mota_owner_transition =
mesh::isUsbMotaOwnerTransitionCommand(command);
@@ -7880,6 +7888,10 @@ void MyMesh::handleTerminalCommand(char* command) {
FIRMWARE_VERSION, (unsigned)FIRMWARE_VER_CODE, FIRMWARE_BUILD_DATE);
} else if (strcmp(command, "help") == 0) {
terminalOutput().print("Commands:\r\n");
terminalOutput().print(" stats-core / stats-radio / stats-radio-diag / stats-packets\r\n");
terminalOutput().print(" get prv.key (when private key export is enabled)\r\n");
terminalOutput().print(" get password (this role has no admin password)\r\n");
terminalOutput().print(" erase (erase stored settings and identity)\r\n");
terminalOutput().print(" board\r\n");
terminalOutput().print(" version\r\n");
terminalOutput().print(" get storage.layout\r\n");
@@ -7920,7 +7932,7 @@ void MyMesh::handleTerminalCommand(char* command) {
terminalOutput().print(" get wifi.powersave\r\n");
terminalOutput().print(" set wifi.powersave <none|min|max>\r\n");
#ifdef WITH_WEBCONFIG
terminalOutput().print(" get wifi.{ssid|status}\r\n");
terminalOutput().print(" get wifi.{ssid|pwd|status}\r\n");
terminalOutput().print(" set wifi.ssid <network name>\r\n");
terminalOutput().print(" set wifi.pwd <password>\r\n");
terminalOutput().print(" get webui\r\n");
@@ -8023,6 +8035,108 @@ static bool isCompanionRadioPrefsCommand(const char* command) {
return false;
}
// Called only for directly attached USB/BLE/TCP/browser clients. Commands
// received over LoRa use handleCommand() with a nonzero sender timestamp.
bool MyMesh::handleDirectCommand(const char* command, char* reply, size_t reply_size) {
if (strcmp(command, "get password") == 0) {
snprintf(reply, reply_size, "> (no admin password on Companion)");
return true;
}
if (strcmp(command, "get prv.key") == 0) {
#if ENABLE_PRIVATE_KEY_EXPORT
uint8_t key[PRV_KEY_SIZE];
char hex[PRV_KEY_SIZE * 2 + 1];
const int length = self_id.writeTo(key, sizeof(key));
mesh::Utils::toHex(hex, key, length);
snprintf(reply, reply_size, "> %s", hex);
memset(key, 0, sizeof(key));
memset(hex, 0, sizeof(hex));
#else
snprintf(reply, reply_size, "Error: private key export disabled in this build");
#endif
return true;
}
if (strcmp(command, "erase") == 0) {
if (_store->formatFileSystem()) {
stopContactsIterator();
resetContacts();
mesh::resetLazyPersistenceAfterSuccess(dirty_contacts_expiry, dirty_contacts_failures);
snprintf(reply, reply_size, "File system erase: OK");
} else {
snprintf(reply, reply_size, "File system erase: Err");
}
return true;
}
if (strncmp(command, "set freq ", 9) == 0) {
float frequency;
if (!mesh::cli::parseDecimalStrict(command + 9, frequency)
|| frequency < 150.0f || frequency > 2500.0f) {
snprintf(reply, reply_size, "Error: frequency must be 150-2500 MHz");
} else {
const float previous = _prefs.freq;
_prefs.freq = frequency;
if (savePrefs()) {
snprintf(reply, reply_size, "OK - reboot to apply");
} else {
_prefs.freq = previous;
snprintf(reply, reply_size, "Error: frequency could not be saved");
}
}
return true;
}
if (strcmp(command, "stats-core") == 0) {
StatsFormatHelper::formatCoreStats(reply, board, *_ms, _err_flags, _mgr);
return true;
}
if (strcmp(command, "stats-radio") == 0) {
StatsFormatHelper::formatRadioStats(reply, _radio, radio_driver,
getTotalAirTime(), getReceiveAirTime());
return true;
}
if (strcmp(command, "stats-radio-diag") == 0) {
StatsFormatHelper::formatRadioDiag(reply, _radio, radio_driver, *_ms,
_err_flags, hasOutbound());
return true;
}
if (strcmp(command, "stats-packets") == 0) {
StatsFormatHelper::formatPacketStats(reply, radio_driver, getNumSentFlood(),
getNumSentDirect(), getNumRecvFlood(), getNumRecvDirect());
return true;
}
#if defined(ESP32) && defined(WIFI_SSID)
if (strcmp(command, "get wifi.pwd") == 0) {
#ifdef WITH_WEBCONFIG
WebConfigServer::formatWiFiPassword(reply, reply_size);
#else
char ssid[32] = {}, password[65] = {};
if (!WiFiSetupPortal::loadStoredCredentials(ssid, sizeof(ssid), password, sizeof(password))) {
#ifdef WIFI_PWD
snprintf(password, sizeof(password), "%s", WIFI_PWD);
#endif
}
snprintf(reply, reply_size, "> %s", password);
memset(password, 0, sizeof(password));
#endif
return true;
}
#ifdef WITH_MQTT_BRIDGE
if (strncmp(command, "get mqtt", 8) == 0 && command[8] >= '1'
&& command[8] <= '0' + MAX_MQTT_SLOTS && command[9] == '.') {
const int slot = command[8] - '1';
const char* field = command + 10;
const char* value = nullptr;
if (strcmp(field, "password") == 0) value = _mqtt_prefs.mqtt_slot_password[slot];
if (strcmp(field, "token") == 0) value = _mqtt_prefs.mqtt_slot_token[slot];
if (value) {
snprintf(reply, reply_size, "> %s", value);
return true;
}
}
#endif
#endif
return false;
}
bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp,
char* reply) {
if (command == NULL || reply == NULL) return false;
@@ -8039,6 +8153,8 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp,
while (*command == ' ' || *command == '\t') command++;
}
if (sender_timestamp == 0 && handleDirectCommand(command, reply, reply_capacity)) return true;
#if COMPANION_FEATURE_USB_MOTA_SOURCE
if (mesh::isUsbMotaOwnerTransitionCommand(command)) {
snprintf(reply, reply_capacity,
+5
View File
@@ -132,6 +132,10 @@ class MyMesh : public BaseChatMesh, public DataStoreHost, public UIShutdownGuard
public:
MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMeshTables &tables, DataStore& store, AbstractUITask* ui=NULL);
bool handleLocalCommand(const char* command, char* reply) {
return handleCommand(command, 0, reply);
}
void begin(bool has_display, bool radio_available = true);
void activateRadio();
bool isRadioReady() const { return _radio_available; }
@@ -370,6 +374,7 @@ private:
void checkCLIRescueCmd();
bool handleCommand(const char* text, uint32_t sender_timestamp, char* reply);
bool handleDirectCommand(const char* command, char* reply, size_t reply_size);
void checkSerialInterface();
bool applyAndSaveFemRxGain(bool enabled);
bool applyAndSaveFemTxGain(bool enabled);
+7 -6
View File
@@ -1674,7 +1674,7 @@ void halt() {
/* WIFI TEXT CONSOLE - one client at a time on a dedicated port, separate from
Binary Companion (5000) and the mOTA seeder (5001). A Full Companion exposes
the same role CLI here as its USB terminal. Other OTA-enabled Companion
builds retain the bounded `ota ...` management console. */
builds expose local maintenance commands and `ota ...` commands. */
#if defined(ESP32) && defined(WIFI_SSID) && defined(ENABLE_OTA)
#include <helpers/ota/OtaCli.h> // mesh::ota::handle_ota_command(line, reply, board)
#include <helpers/esp32/WiFiOtaSeeder.h>
@@ -1701,7 +1701,7 @@ void halt() {
WIFI_DEBUG_PRINTLN("Full Companion terminal listening on :%d (nc <ip> %d)",
OTA_CONSOLE_TCP_PORT, OTA_CONSOLE_TCP_PORT);
#else
WIFI_DEBUG_PRINTLN("OTA console listening on :%d (nc <ip> %d, type `ota ...`)",
WIFI_DEBUG_PRINTLN("Local CLI and OTA console listening on :%d (nc <ip> %d)",
OTA_CONSOLE_TCP_PORT, OTA_CONSOLE_TCP_PORT);
#endif
}
@@ -1767,7 +1767,7 @@ void halt() {
ota_console_client.stop();
}
#else
ota_console_client.print("OTA console - type `ota ...`\r\n> ");
ota_console_client.print("Local CLI and OTA console - type `version` or `ota status`\r\n> ");
#endif
}
return;
@@ -1810,8 +1810,9 @@ void halt() {
ota_console_client.print("> ");
#else
char reply[160]; reply[0] = 0;
if (!mesh::ota::handle_ota_command(ota_console_line, reply, board))
strcpy(reply, "only `ota ...` commands are supported on this console");
if (!the_mesh.handleLocalCommand(ota_console_line, reply)
&& !mesh::ota::handle_ota_command(ota_console_line, reply, board))
strcpy(reply, "Unknown local or OTA command");
ota_console_client.print(" -> "); ota_console_client.print(reply); ota_console_client.print("\r\n> ");
#endif
ota_console_clear_line();
@@ -2830,7 +2831,7 @@ void loop() {
serviceCompanionWiFiState();
if (companion_wifi_requested && companion_wifi_active) {
#ifdef ENABLE_OTA
ota_console_loop(); // service the OTA text console (port 5002)
ota_console_loop(); // service the local text console (port 5002)
#endif
const unsigned long wifi_now = millis();
const bool station_channel_ok = mesh::wifi::enforceStationChannel();
+58 -1
View File
@@ -2396,6 +2396,22 @@ void MyMesh::formatRecentRepeatersReply(char *reply, int page,
}
void MyMesh::printRecentRepeatersSerial() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output) {
_local_cli_output.startRows(*_command_output,
[](void* context, size_t& row, char* out, size_t capacity) -> size_t {
auto& owner = *static_cast<MyMesh*>(context);
const auto* tables = static_cast<const SimpleMeshTables*>(owner.getTables());
const auto* info = tables ? tables->getRecentRepeaterBySortedIdx(row++) : nullptr;
if (!info) return 0;
char prefix[MAX_ROUTE_HASH_BYTES * 2 + 1], snr[12];
mesh::Utils::toHex(prefix, info->prefix, info->prefix_len);
formatLocalSnrX4(snr, sizeof(snr), info->snr_x4);
return snprintf(out, capacity, "%s,%s%s\r\n", prefix, snr[0] == '-' ? "" : " ", snr);
}, this, "Recent repeaters:\r\n");
return;
}
#endif
const SimpleMeshTables* tables = static_cast<const SimpleMeshTables*>(getTables());
if (tables == NULL) {
mesh::usbConsolePort().printf("Error: unsupported\r\n");
@@ -4795,6 +4811,9 @@ bool MyMesh::scheduleNormalRadio() {
}
bool MyMesh::formatFileSystem() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
_local_cli_output.cancel();
#endif
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
return InternalFS.format();
#elif defined(RP2040_PLATFORM)
@@ -4845,6 +4864,13 @@ void MyMesh::updateFloodAdvertTimer() {
}
void MyMesh::dumpLogFile() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output) {
_local_cli_output.startFile(*_command_output,
mesh::openFileRead(_fs, PACKET_LOG_FILE));
return;
}
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (hasPendingSerialOutput()) {
mesh::usbConsolePort().printf("Err - USB output busy\r\n");
@@ -11100,6 +11126,7 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
_gpio_reply_tracker.beginCommand(gpio_client_index, gpio_path_hash_size,
sender == NULL ? NULL : sender->id.pub_key);
#endif
if (sender != nullptr && sender_timestamp == 0) sender_timestamp = 1;
char* reply_start = reply;
// Parse the original wire text exactly once, also before region-load mode.
// Parsing again after stripping a prefix would let nested prefixes bypass
@@ -11537,6 +11564,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
// Compatibility path for manually defined legacy portable builds. Current
// release builds use FULL and do not enter this branch.
_cli.handleCommand(sender_timestamp, command, reply);
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output && _local_cli_output.owns(*_command_output)) reply[0] = 0;
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (sender_timestamp == 0 && serial_log_eof_pending
&& strcmp(reply, " EOF") == 0) reply[0] = 0;
@@ -11780,6 +11810,24 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
printRecentRepeatersSerial();
reply_start[0] = 0;
} else if (sender_timestamp == 0 && strcmp(command, "get acl") == 0) {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output) {
_local_cli_output.startRows(*_command_output,
[](void* context, size_t& row, char* out, size_t capacity) -> size_t {
auto& owner = *static_cast<MyMesh*>(context);
while (row < static_cast<size_t>(owner.acl.getNumClients())) {
auto* client = owner.acl.getClientByIdx(row++);
if (!client->permissions) continue;
char key[PUB_KEY_SIZE * 2 + 1];
mesh::Utils::toHex(key, client->id.pub_key, PUB_KEY_SIZE);
return snprintf(out, capacity, "%02X %s\r\n", client->permissions, key);
}
return 0;
}, this, "ACL:\r\n");
reply[0] = 0;
return;
}
#endif
mesh::usbConsolePort().printf("ACL:\r\n");
for (int i = 0; i < acl.getNumClients(); i++) {
auto c = acl.getClientByIdx(i);
@@ -11942,7 +11990,10 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
strcpy(reply, "Err - neighbors not enabled in this build");
#endif
} else{
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
_cli.handleCommand(sender_timestamp, command, reply);
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output && _local_cli_output.owns(*_command_output)) reply[0] = 0;
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (sender_timestamp == 0 && serial_log_eof_pending
&& strcmp(reply, " EOF") == 0) reply[0] = 0;
@@ -11951,6 +12002,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
}
void MyMesh::loop() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
_local_cli_output.service();
#endif
#ifdef WITH_MQTT_BRIDGE
if (mqtt_bridge) mqtt_bridge->servicePendingClockCorrection();
#endif
@@ -12937,6 +12991,9 @@ bool MyMesh::startNeighborDiscover(char* reply) {
// To check if there is pending work
bool MyMesh::hasPendingWork() const {
if (hasPendingOtaApply()) return true;
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_local_cli_output.busy()) return true;
#endif
if (deferred_cli_command.pending || pending_self_advert || _cli.hasActiveUserGpioTimer()) return true;
#if defined(WITH_BRIDGE)
const AbstractBridge* active_bridge = activeBridge();
+59 -1
View File
@@ -110,6 +110,7 @@
#include <helpers/SimpleMeshTables.h>
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/StatsFormatHelper.h>
#include <helpers/LocalCliOutput.h>
#if MESH_ENABLE_TELEMETRY_HISTORY
#include <helpers/ExternalVoltageHistory.h>
#include <helpers/TelemetryHistory.h>
@@ -298,6 +299,16 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
};
FILESYSTEM* _fs;
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
#ifdef NRF52_PLATFORM
mesh::LocalCliOutput<File> _local_cli_output{File(InternalFS)};
#else
mesh::LocalCliOutput<File> _local_cli_output;
#endif
Stream* _command_output = nullptr;
Stream* _web_terminal = nullptr;
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
File serial_log_dump;
size_t serial_log_remaining = 0;
@@ -1023,6 +1034,22 @@ public:
void saveIdentity(const mesh::LocalIdentity& new_id) override;
void clearStats() override;
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
void handleLocalCommand(char* command, char* reply, Stream& output) {
if (_local_cli_output.busy()) {
strcpy(reply, "Err - local listing in progress");
return;
}
_command_output = &output;
handleCommand(0, command, reply);
_command_output = nullptr;
}
bool hasPendingLocalOutput() const { return _local_cli_output.busy(); }
void cancelLocalOutput(Stream& output) {
if (_local_cli_output.owns(output)) _local_cli_output.cancel();
}
#endif
void handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char* command,
char* reply, int gpio_client_index = -1,
uint8_t gpio_path_hash_size = 1, bool usb_origin = false);
@@ -1351,6 +1378,9 @@ public:
bool setWebUIEnabled(bool enabled, char* reply) override;
bool getWebUIStatus(char* reply) const override;
bool getWiFiSSID(char* reply) const override;
bool getWiFiPassword(char* reply) const override {
return WebConfigServer::formatWiFiPassword(reply, 160);
}
bool getWiFiStatus(char* reply) const override;
bool getWiFiPowerSave(char* reply) const override;
bool getWiFiCLI(char* reply) const override;
@@ -1365,7 +1395,35 @@ public:
void execCommand(char* cmd, char* reply) override { handleCommand(0, cmd, reply); }
bool supportsCliTerminal() const override { return true; }
void execAdminCommand(char* cmd, char* reply) override {
handleCommand(1, cmd, reply);
if (strcmp(cmd, "get acl") == 0 || strcmp(cmd, "log") == 0
|| strcmp(cmd, "get recent.repeater") == 0
|| strcmp(cmd, "get recent.repeaters") == 0) {
strcpy(reply, "Error: use /api/terminal for streamed listings");
return;
}
handleCommand(0, cmd, reply);
}
bool supportsStreamTerminal() const override { return true; }
bool beginStreamTerminal(Stream& output) override {
if (_web_terminal || _local_cli_output.busy()) return false;
_web_terminal = &output;
return true;
}
bool ownsStreamTerminal(const Stream& output) const override {
return _web_terminal == &output;
}
void runStreamTerminal(char* command) override {
if (!_web_terminal) return;
char reply[160] = {};
handleLocalCommand(command, reply, *_web_terminal);
if (reply[0]) _web_terminal->println(reply);
}
bool streamTerminalBusy() const override {
return _web_terminal && _local_cli_output.owns(*_web_terminal);
}
void endStreamTerminal(Stream& output) override {
cancelLocalOutput(output);
if (_web_terminal == &output) _web_terminal = nullptr;
}
void rebootNow() override { _cli.getBoard()->reboot(); }
void onConfigBatchStart() override {
+6 -2
View File
@@ -313,11 +313,15 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
#ifdef ETHERNET_ENABLED
ethernet_loop_maintain();
if (ethernet_read_line(ethernet_command, sizeof(ethernet_command))) {
if (ethernet_take_session_reset() || !ethernet_client.connected()) {
the_mesh.cancelLocalOutput(ethernet_client);
ethernet_command[0] = 0;
}
if (!the_mesh.hasPendingLocalOutput() && ethernet_read_line(ethernet_command, sizeof(ethernet_command))) {
char reply[160];
reply[0] = 0;
if (!ethernet_handle_command(ethernet_command, reply)) {
the_mesh.handleCommand(0, ethernet_command, reply);
the_mesh.handleLocalCommand(ethernet_command, reply, ethernet_client);
}
ethernet_send_reply(reply);
ethernet_command[0] = 0;
+39 -1
View File
@@ -1536,6 +1536,9 @@ bool MyMesh::scheduleNormalRadio() {
}
bool MyMesh::formatFileSystem() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
_local_cli_output.cancel();
#endif
#if defined(NRF52_PLATFORM)
return InternalFS.format();
#elif defined(RP2040_PLATFORM)
@@ -1577,6 +1580,13 @@ void MyMesh::updateFloodAdvertTimer() {
}
void MyMesh::dumpLogFile() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output) {
_local_cli_output.startFile(*_command_output,
mesh::openFileRead(_fs, PACKET_LOG_FILE));
return;
}
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (hasPendingSerialOutput()) {
mesh::usbConsolePort().printf("Err - USB output busy\r\n");
@@ -2254,6 +2264,7 @@ void MyMesh::sampleTelemetryHistory() {
void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply,
int gpio_client_index,
uint8_t gpio_path_hash_size) {
if (gpio_client_index >= 0 && sender_timestamp == 0) sender_timestamp = 1;
#if defined(ESP32_PLATFORM) || defined(USER_GPIO_CONTROL)
const uint8_t* gpio_client_key = gpio_client_index >= 0 &&
gpio_client_index < acl.getNumClients()
@@ -2346,6 +2357,24 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
}
}
} else if (sender_timestamp == 0 && strcmp(command, "get acl") == 0) {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output) {
_local_cli_output.startRows(*_command_output,
[](void* context, size_t& row, char* out, size_t capacity) -> size_t {
auto& owner = *static_cast<MyMesh*>(context);
while (row < static_cast<size_t>(owner.acl.getNumClients())) {
auto* client = owner.acl.getClientByIdx(row++);
if (!client->permissions) continue;
char key[PUB_KEY_SIZE * 2 + 1];
mesh::Utils::toHex(key, client->id.pub_key, PUB_KEY_SIZE);
return snprintf(out, capacity, "%02X %s\r\n", client->permissions, key);
}
return 0;
}, this, "ACL:\r\n");
reply[0] = 0;
return;
}
#endif
mesh::usbConsolePort().printf("ACL:\r\n");
for (int i = 0; i < acl.getNumClients(); i++) {
auto c = acl.getClientByIdx(i);
@@ -2415,7 +2444,10 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
else if (_clock_sync.handleCommand(command, reply)) {
// handled by the role-independent mesh clock synchronizer
} else {
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
_cli.handleCommand(sender_timestamp, command, reply);
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_command_output && _local_cli_output.owns(*_command_output)) reply[0] = 0;
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
if (sender_timestamp == 0 && serial_log_eof_pending
&& strcmp(reply, " EOF") == 0) reply[0] = 0;
@@ -2428,6 +2460,9 @@ bool MyMesh::saveFilter(ClientInfo* client) {
}
void MyMesh::loop() {
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
_local_cli_output.service();
#endif
#ifdef WITH_MQTT_BRIDGE
if (bridge) bridge->servicePendingClockCorrection();
#endif
@@ -2793,6 +2828,9 @@ uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
// To check if there is pending work
bool MyMesh::hasPendingWork() const {
if (hasPendingOtaApply()) return true;
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
if (_local_cli_output.busy()) return true;
#endif
#if defined(WITH_BRIDGE)
if (bridge && bridge->isRunning()) return true; // bridge needs WiFi radio, can't sleep
#endif
+59 -1
View File
@@ -28,6 +28,7 @@
#include <helpers/UserGpioReplyTracker.h>
#endif
#include <helpers/StatsFormatHelper.h>
#include <helpers/LocalCliOutput.h>
#include <helpers/ClientACL.h>
#include <helpers/LogicalMessageCache.h>
#include <helpers/RemoteCliReplyCache.h>
@@ -158,6 +159,16 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
#endif
{
FILESYSTEM* _fs;
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
#ifdef NRF52_PLATFORM
mesh::LocalCliOutput<File> _local_cli_output{File(InternalFS)};
#else
mesh::LocalCliOutput<File> _local_cli_output;
#endif
Stream* _command_output = nullptr;
Stream* _web_terminal = nullptr;
#endif
#if MESH_ESP32_USB_CONSOLE_COOPERATIVE
File serial_log_dump;
size_t serial_log_remaining = 0;
@@ -491,6 +502,22 @@ public:
void saveIdentity(const mesh::LocalIdentity& new_id) override;
void clearStats() override;
#if defined(WITH_WEBCONFIG) || defined(ETHERNET_ENABLED)
void handleLocalCommand(char* command, char* reply, Stream& output) {
if (_local_cli_output.busy()) {
strcpy(reply, "Err - local listing in progress");
return;
}
_command_output = &output;
handleCommand(0, command, reply);
_command_output = nullptr;
}
bool hasPendingLocalOutput() const { return _local_cli_output.busy(); }
void cancelLocalOutput(Stream& output) {
if (_local_cli_output.owns(output)) _local_cli_output.cancel();
}
#endif
void handleCommand(uint32_t sender_timestamp, char* command, char* reply,
int gpio_client_index = -1,
uint8_t gpio_path_hash_size = 1);
@@ -645,6 +672,9 @@ public:
bool setWebUIEnabled(bool enabled, char* reply) override;
bool getWebUIStatus(char* reply) const override;
bool getWiFiSSID(char* reply) const override;
bool getWiFiPassword(char* reply) const override {
return WebConfigServer::formatWiFiPassword(reply, 160);
}
bool getWiFiStatus(char* reply) const override;
bool getWiFiPowerSave(char* reply) const override;
bool getWiFiCLI(char* reply) const override;
@@ -659,7 +689,35 @@ public:
void execCommand(char* cmd, char* reply) override { handleCommand(0, cmd, reply); }
bool supportsCliTerminal() const override { return true; }
void execAdminCommand(char* cmd, char* reply) override {
handleCommand(1, cmd, reply);
if (strcmp(cmd, "get acl") == 0 || strcmp(cmd, "log") == 0
|| strcmp(cmd, "get recent.repeater") == 0
|| strcmp(cmd, "get recent.repeaters") == 0) {
strcpy(reply, "Error: use /api/terminal for streamed listings");
return;
}
handleCommand(0, cmd, reply);
}
bool supportsStreamTerminal() const override { return true; }
bool beginStreamTerminal(Stream& output) override {
if (_web_terminal || _local_cli_output.busy()) return false;
_web_terminal = &output;
return true;
}
bool ownsStreamTerminal(const Stream& output) const override {
return _web_terminal == &output;
}
void runStreamTerminal(char* command) override {
if (!_web_terminal) return;
char reply[160] = {};
handleLocalCommand(command, reply, *_web_terminal);
if (reply[0]) _web_terminal->println(reply);
}
bool streamTerminalBusy() const override {
return _web_terminal && _local_cli_output.owns(*_web_terminal);
}
void endStreamTerminal(Stream& output) override {
cancelLocalOutput(output);
if (_web_terminal == &output) _web_terminal = nullptr;
}
void rebootNow() override { _cli.getBoard()->reboot(); }
void onConfigBatchStart() override {
+6 -2
View File
@@ -203,11 +203,15 @@ void loop() {
#ifdef ETHERNET_ENABLED
ethernet_loop_maintain();
if (ethernet_read_line(ethernet_command, sizeof(ethernet_command))) {
if (ethernet_take_session_reset() || !ethernet_client.connected()) {
the_mesh.cancelLocalOutput(ethernet_client);
ethernet_command[0] = 0;
}
if (!the_mesh.hasPendingLocalOutput() && ethernet_read_line(ethernet_command, sizeof(ethernet_command))) {
char reply[160];
reply[0] = 0;
if (!ethernet_handle_command(ethernet_command, reply)) {
the_mesh.handleCommand(0, ethernet_command, reply);
the_mesh.handleLocalCommand(ethernet_command, reply, ethernet_client);
}
ethernet_send_reply(reply);
ethernet_command[0] = 0;
+1
View File
@@ -574,6 +574,7 @@ void SensorMesh::onUserGpioTimerCompleted(uint8_t pin, uint8_t state,
void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* reply,
int gpio_client_index,
uint8_t gpio_path_hash_size) {
if (gpio_client_index >= 0 && sender_timestamp == 0) sender_timestamp = 1;
#if defined(ESP32_PLATFORM) || defined(USER_GPIO_CONTROL)
const uint8_t* gpio_client_key = gpio_client_index >= 0 &&
gpio_client_index < acl.getNumClients()