feat: prepare v1.16.07 firmware release
This commit is contained in:
1 parent
129e687c02
commit
ccce0c8d75
95 files changed
+5550
-278
No files matched your search
@@ -7,6 +7,12 @@
|
||||
#include <helpers/MQTTDefaults.h>
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
#include <helpers/WiFiSetupPortal.h>
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
#endif
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
if (failure_count < 6) failure_count++;
|
||||
@@ -1180,6 +1186,10 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
|
||||
_mqtt_configured = false;
|
||||
_mqtt_started = false;
|
||||
#endif
|
||||
#ifdef WITH_WEBCONFIG
|
||||
_webconfig = nullptr;
|
||||
_wc_mqtt_dirty = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::begin(bool has_display) {
|
||||
@@ -1297,6 +1307,16 @@ void MyMesh::begin(bool has_display) {
|
||||
_mqtt_bridge->setStatsSources(this, _radio, &board, _ms);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
void* web_mqtt_prefs = nullptr;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
web_mqtt_prefs = &_mqtt_prefs;
|
||||
#endif
|
||||
_webconfig = new WebConfigServer(this, web_mqtt_prefs, false,
|
||||
self_id.pub_key, FIRMWARE_VERSION,
|
||||
"companion", board.getManufacturerName());
|
||||
#endif
|
||||
}
|
||||
|
||||
mesh::RadioParamApplyResult MyMesh::tryApplyRadioParams(float freq, float bw, uint8_t sf, uint8_t cr) {
|
||||
@@ -1389,9 +1409,8 @@ static void copyMqttString(char* dest, size_t dest_size, const char* src) {
|
||||
dest[dest_size - 1] = 0;
|
||||
}
|
||||
|
||||
void MyMesh::serviceMQTT(const char* wifi_ssid, const char* wifi_password,
|
||||
bool allow_setup_page) {
|
||||
if (!_mqtt_started && !_mqtt_setup.isActive()) {
|
||||
void MyMesh::serviceMQTT(const char* wifi_ssid, const char* wifi_password) {
|
||||
if (!_mqtt_started) {
|
||||
if (strcmp(_mqtt_prefs.wifi_ssid, wifi_ssid ? wifi_ssid : "") != 0) {
|
||||
copyMqttString(_mqtt_prefs.wifi_ssid, sizeof(_mqtt_prefs.wifi_ssid), wifi_ssid);
|
||||
}
|
||||
@@ -1400,25 +1419,11 @@ void MyMesh::serviceMQTT(const char* wifi_ssid, const char* wifi_password,
|
||||
}
|
||||
}
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED || !allow_setup_page) {
|
||||
_mqtt_setup.stop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mqtt_setup.loop()) {
|
||||
if (_mqtt_started && _mqtt_bridge) {
|
||||
_mqtt_bridge->end();
|
||||
}
|
||||
_mqtt_started = false;
|
||||
_mqtt_configured = true;
|
||||
Serial.println("MQTT companion: applying settings saved from the web page");
|
||||
}
|
||||
|
||||
if (!_mqtt_setup.isActive()) {
|
||||
_mqtt_setup.begin(&_mqtt_prefs);
|
||||
}
|
||||
|
||||
if (_mqtt_configured && !_mqtt_started && _mqtt_bridge) {
|
||||
if (WiFi.status() == WL_CONNECTED && _mqtt_configured && !_mqtt_started && _mqtt_bridge
|
||||
#ifdef WITH_WEBCONFIG
|
||||
&& !isWebConfigSetupActive()
|
||||
#endif
|
||||
) {
|
||||
_mqtt_started = true; // begin is one-shot; avoid retrying partial allocations
|
||||
_mqtt_bridge->begin();
|
||||
if (_mqtt_bridge->isRunning()) {
|
||||
@@ -1428,9 +1433,429 @@ void MyMesh::serviceMQTT(const char* wifi_ssid, const char* wifi_password,
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void MyMesh::stopMQTTSetupPage() {
|
||||
_mqtt_setup.stop();
|
||||
#ifdef WITH_WEBCONFIG
|
||||
static bool wcParseBool(const char* value, bool& out) {
|
||||
if (strcmp(value, "on") == 0) { out = true; return true; }
|
||||
if (strcmp(value, "off") == 0) { out = false; return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool wcParseLong(const char* value, long min_value, long max_value, long& out) {
|
||||
if (!value || !value[0]) return false;
|
||||
char* end = nullptr;
|
||||
long parsed = strtol(value, &end, 10);
|
||||
if (!end || *end != 0 || parsed < min_value || parsed > max_value) return false;
|
||||
out = parsed;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wcParseDouble(const char* value, double min_value, double max_value, double& out) {
|
||||
if (!value || !value[0]) return false;
|
||||
char* end = nullptr;
|
||||
double parsed = strtod(value, &end);
|
||||
if (!end || *end != 0 || !isfinite(parsed) || parsed < min_value || parsed > max_value) return false;
|
||||
out = parsed;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wcValidNtpHost(const char* value) {
|
||||
if (!value || !value[0] || strlen(value) > 63 || value[0] == '.') return false;
|
||||
size_t len = strlen(value);
|
||||
if (value[len - 1] == '.') return false;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
char c = value[i];
|
||||
if (!isalnum(static_cast<unsigned char>(c)) && c != '.' && c != '-') return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wcValidHexKey(const char* value) {
|
||||
if (!value || strlen(value) != 64) return false;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (!isxdigit(static_cast<unsigned char>(value[i]))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool wcCopyValue(char* dest, size_t dest_size, const char* value) {
|
||||
if (!dest || dest_size == 0 || !value || strlen(value) >= dest_size) return false;
|
||||
strncpy(dest, value, dest_size - 1);
|
||||
dest[dest_size - 1] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
|
||||
memset(&s, 0, sizeof(s));
|
||||
wcCopyValue(s.name, sizeof(s.name), _prefs.node_name);
|
||||
s.lat = sensors.node_lat;
|
||||
s.lon = sensors.node_lon;
|
||||
s.freq = _prefs.freq;
|
||||
s.bw = _prefs.bw;
|
||||
s.sf = _prefs.sf;
|
||||
s.cr = _prefs.cr;
|
||||
s.tx_power = _prefs.tx_power_dbm;
|
||||
s.airtime_factor = _prefs.airtime_factor;
|
||||
s.rx_delay = _prefs.rx_delay_base;
|
||||
s.rx_gain = _prefs.rx_boosted_gain;
|
||||
s.repeat = _prefs.client_repeat != 0;
|
||||
s.capabilities = WebConfigServer::CAP_LOCATION | WebConfigServer::CAP_AIRTIME
|
||||
| WebConfigServer::CAP_RX_DELAY | WebConfigServer::CAP_RX_GAIN
|
||||
| WebConfigServer::CAP_REPEAT;
|
||||
}
|
||||
|
||||
bool MyMesh::startWebConfig(bool force_ap, char* reply) {
|
||||
if (!_webconfig) {
|
||||
strcpy(reply, "Err: WebUI unavailable (not enough memory)");
|
||||
return false;
|
||||
}
|
||||
if (_webconfig->isRunning() || _webconfig->isStopping()) {
|
||||
if (force_ap && !_webconfig->isStopping()
|
||||
&& _webconfig->mode() == WebConfigServer::MODE_LAN) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_mqtt_started && _mqtt_bridge) {
|
||||
_mqtt_bridge->end();
|
||||
_mqtt_started = false;
|
||||
}
|
||||
#endif
|
||||
return _webconfig->startSetupMode(reply);
|
||||
}
|
||||
strcpy(reply, _webconfig->isStopping() ? "Err: WebUI still stopping"
|
||||
: "Err: WebUI already running");
|
||||
return false;
|
||||
}
|
||||
if (force_ap) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_mqtt_started && _mqtt_bridge) {
|
||||
_mqtt_bridge->end();
|
||||
_mqtt_started = false;
|
||||
}
|
||||
#endif
|
||||
return _webconfig->startSetupMode(reply);
|
||||
}
|
||||
return _webconfig->startAutoMode(reply);
|
||||
}
|
||||
|
||||
void MyMesh::stopWebConfig() {
|
||||
if (_webconfig && _webconfig->isRunning()) _webconfig->requestStop();
|
||||
}
|
||||
|
||||
void MyMesh::serviceWebConfig() {
|
||||
if (_webconfig) _webconfig->tick(millis());
|
||||
}
|
||||
|
||||
bool MyMesh::isWebConfigSetupActive() const {
|
||||
return _webconfig && _webconfig->mode() == WebConfigServer::MODE_SETUP
|
||||
&& !_webconfig->isStopping();
|
||||
}
|
||||
|
||||
void MyMesh::rebootNow() {
|
||||
board.reboot();
|
||||
}
|
||||
|
||||
void MyMesh::onConfigBatchStart() {
|
||||
_wc_mqtt_dirty = false;
|
||||
}
|
||||
|
||||
void MyMesh::onConfigBatchEnd() {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_wc_mqtt_dirty) {
|
||||
CompanionMqttSetupPortal::saveStoredConfig(_mqtt_prefs);
|
||||
if (_mqtt_started && _mqtt_bridge) _mqtt_bridge->end();
|
||||
_mqtt_started = false;
|
||||
MQTTPrefs verified;
|
||||
_mqtt_configured = CompanionMqttSetupPortal::loadStoredConfig(verified);
|
||||
if (_mqtt_configured) _mqtt_prefs = verified;
|
||||
}
|
||||
#endif
|
||||
_wc_mqtt_dirty = false;
|
||||
}
|
||||
|
||||
void MyMesh::execCommand(char* cmd, char* reply) {
|
||||
reply[0] = 0;
|
||||
if (!cmd || strncmp(cmd, "set ", 4) != 0) {
|
||||
strcpy(reply, "Error: unsupported command");
|
||||
return;
|
||||
}
|
||||
char* key = cmd + 4;
|
||||
char* split = strchr(key, ' ');
|
||||
if (!split) {
|
||||
strcpy(reply, "Error: missing value");
|
||||
return;
|
||||
}
|
||||
*split = 0;
|
||||
const char* value = split + 1;
|
||||
|
||||
if (strcmp(key, "name") == 0) {
|
||||
if (!value[0] || !wcCopyValue(_prefs.node_name, sizeof(_prefs.node_name), value)) {
|
||||
strcpy(reply, "Error: name must be 1-31 characters");
|
||||
} else {
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(key, "lat") == 0 || strcmp(key, "lon") == 0) {
|
||||
double parsed;
|
||||
const bool latitude = key[1] == 'a';
|
||||
if (!wcParseDouble(value, latitude ? -90.0 : -180.0,
|
||||
latitude ? 90.0 : 180.0, parsed)) {
|
||||
strcpy(reply, latitude ? "Error: latitude must be -90 to 90"
|
||||
: "Error: longitude must be -180 to 180");
|
||||
} else {
|
||||
if (latitude) sensors.node_lat = parsed; else sensors.node_lon = parsed;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(key, "radio") == 0) {
|
||||
float freq, bw;
|
||||
int sf, cr;
|
||||
char extra;
|
||||
if (sscanf(value, "%f,%f,%d,%d%c", &freq, &bw, &sf, &cr, &extra) != 4
|
||||
|| !isfinite(freq) || !isfinite(bw) || freq < 150.0f || freq > 2500.0f
|
||||
|| bw < 7.0f || bw > 500.0f || sf < 5 || sf > 12 || cr < 5 || cr > 8) {
|
||||
strcpy(reply, "Error: radio must be freq,bw,sf,cr");
|
||||
} else {
|
||||
_prefs.freq = freq;
|
||||
_prefs.bw = bw;
|
||||
_prefs.sf = static_cast<uint8_t>(sf);
|
||||
_prefs.cr = static_cast<uint8_t>(cr);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK - reboot required");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(key, "tx") == 0) {
|
||||
long parsed;
|
||||
if (!wcParseLong(value, -9, MAX_LORA_TX_POWER, parsed)) {
|
||||
snprintf(reply, 160, "Error: TX power must be -9 to %d", MAX_LORA_TX_POWER);
|
||||
} else {
|
||||
_prefs.tx_power_dbm = static_cast<int8_t>(parsed);
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(key, "af") == 0 || strcmp(key, "rxdelay") == 0) {
|
||||
double parsed;
|
||||
const bool is_af = strcmp(key, "af") == 0;
|
||||
if (!wcParseDouble(value, 0.0, is_af ? 9.0 : 20.0, parsed)) {
|
||||
strcpy(reply, is_af ? "Error: airtime factor must be 0-9"
|
||||
: "Error: RX delay must be 0-20");
|
||||
} else {
|
||||
if (is_af) _prefs.airtime_factor = parsed; else _prefs.rx_delay_base = parsed;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (strcmp(key, "radio.rxgain") == 0 || strcmp(key, "repeat") == 0) {
|
||||
bool enabled;
|
||||
if (!wcParseBool(value, enabled)) {
|
||||
strcpy(reply, "Error: must be on or off");
|
||||
} else {
|
||||
if (strcmp(key, "radio.rxgain") == 0) {
|
||||
_prefs.rx_boosted_gain = enabled;
|
||||
radio_driver.setRxBoostedGainMode(enabled);
|
||||
} else {
|
||||
_prefs.client_repeat = enabled;
|
||||
}
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bool changed = false;
|
||||
if (strcmp(key, "mqtt.origin") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_origin, sizeof(_mqtt_prefs.mqtt_origin), value);
|
||||
} else if (strcmp(key, "mqtt.iata") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_iata, sizeof(_mqtt_prefs.mqtt_iata), value);
|
||||
if (changed) {
|
||||
for (char* p = _mqtt_prefs.mqtt_iata; *p; p++) *p = toupper(static_cast<unsigned char>(*p));
|
||||
}
|
||||
} else if (strcmp(key, "mqtt.status") == 0 || strcmp(key, "mqtt.packets") == 0
|
||||
|| strcmp(key, "mqtt.raw") == 0 || strcmp(key, "mqtt.rx") == 0
|
||||
|| strcmp(key, "snmp") == 0) {
|
||||
bool enabled;
|
||||
if (!wcParseBool(value, enabled)) {
|
||||
strcpy(reply, "Error: must be on or off");
|
||||
return;
|
||||
}
|
||||
if (strcmp(key, "mqtt.status") == 0) _mqtt_prefs.mqtt_status_enabled = enabled;
|
||||
else if (strcmp(key, "mqtt.packets") == 0) _mqtt_prefs.mqtt_packets_enabled = enabled;
|
||||
else if (strcmp(key, "mqtt.raw") == 0) _mqtt_prefs.mqtt_raw_enabled = enabled;
|
||||
else if (strcmp(key, "mqtt.rx") == 0) _mqtt_prefs.mqtt_rx_enabled = enabled;
|
||||
else _mqtt_prefs.snmp_enabled = enabled;
|
||||
changed = true;
|
||||
} else if (strcmp(key, "mqtt.tx") == 0) {
|
||||
if (strcmp(value, "off") == 0) _mqtt_prefs.mqtt_tx_enabled = 0;
|
||||
else if (strcmp(value, "on") == 0) _mqtt_prefs.mqtt_tx_enabled = 1;
|
||||
else if (strcmp(value, "advert") == 0) _mqtt_prefs.mqtt_tx_enabled = 2;
|
||||
else {
|
||||
strcpy(reply, "Error: MQTT TX must be off, on, or advert");
|
||||
return;
|
||||
}
|
||||
changed = true;
|
||||
} else if (strcmp(key, "mqtt.interval") == 0) {
|
||||
long minutes;
|
||||
if (!wcParseLong(value, 1, 60, minutes)) {
|
||||
strcpy(reply, "Error: interval must be 1-60 minutes");
|
||||
return;
|
||||
}
|
||||
_mqtt_prefs.mqtt_status_interval = static_cast<uint32_t>(minutes) * 60000UL;
|
||||
changed = true;
|
||||
} else if (strcmp(key, "mqtt.ntp") == 0) {
|
||||
if (strcmp(value, "none") == 0) {
|
||||
_mqtt_prefs.mqtt_ntp_server[0] = 0;
|
||||
changed = true;
|
||||
} else if (wcValidNtpHost(value)) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_ntp_server,
|
||||
sizeof(_mqtt_prefs.mqtt_ntp_server), value);
|
||||
}
|
||||
} else if (strcmp(key, "mqtt.owner") == 0) {
|
||||
if (!value[0]) {
|
||||
_mqtt_prefs.mqtt_owner_public_key[0] = 0;
|
||||
changed = true;
|
||||
} else if (wcValidHexKey(value)) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_owner_public_key,
|
||||
sizeof(_mqtt_prefs.mqtt_owner_public_key), value);
|
||||
}
|
||||
} else if (strcmp(key, "mqtt.email") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_email, sizeof(_mqtt_prefs.mqtt_email), value);
|
||||
} else if (strcmp(key, "timezone") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.timezone_string, sizeof(_mqtt_prefs.timezone_string), value);
|
||||
} else if (strcmp(key, "timezone.offset") == 0) {
|
||||
long offset;
|
||||
if (!wcParseLong(value, -12, 14, offset)) {
|
||||
strcpy(reply, "Error: timezone offset must be -12 to 14");
|
||||
return;
|
||||
}
|
||||
_mqtt_prefs.timezone_offset = static_cast<int8_t>(offset);
|
||||
changed = true;
|
||||
} else if (strcmp(key, "snmp.community") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.snmp_community,
|
||||
sizeof(_mqtt_prefs.snmp_community), value);
|
||||
} else if (strncmp(key, "mqtt", 4) == 0 && key[4] >= '1'
|
||||
&& key[4] <= ('0' + MAX_MQTT_SLOTS) && key[5] == '.') {
|
||||
int slot = key[4] - '1';
|
||||
const char* field = key + 6;
|
||||
if (strcmp(field, "preset") == 0) {
|
||||
const bool valid = findMQTTPreset(value) != nullptr
|
||||
|| strcmp(value, MQTT_PRESET_CUSTOM) == 0
|
||||
|| strcmp(value, MQTT_PRESET_NONE) == 0;
|
||||
if (!valid) {
|
||||
strcpy(reply, "Error: unknown MQTT preset");
|
||||
return;
|
||||
}
|
||||
if (findMQTTPreset(value)) {
|
||||
for (int i = 0; i < MAX_MQTT_SLOTS; i++) {
|
||||
if (i != slot && strcmp(_mqtt_prefs.mqtt_slot_preset[i], value) == 0) {
|
||||
snprintf(reply, 160, "Error: preset already assigned to slot %d", i + 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_preset[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_preset[slot]), value);
|
||||
} else if (strcmp(field, "server") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_host[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_host[slot]), value);
|
||||
} else if (strcmp(field, "port") == 0) {
|
||||
long port;
|
||||
if (!wcParseLong(value, 1, 65535, port)) {
|
||||
strcpy(reply, "Error: port must be 1-65535");
|
||||
return;
|
||||
}
|
||||
_mqtt_prefs.mqtt_slot_port[slot] = static_cast<uint16_t>(port);
|
||||
changed = true;
|
||||
} else if (strcmp(field, "username") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_username[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_username[slot]), value);
|
||||
} else if (strcmp(field, "password") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_password[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_password[slot]), value);
|
||||
} else if (strcmp(field, "token") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_token[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_token[slot]), value);
|
||||
} else if (strcmp(field, "topic") == 0) {
|
||||
if (strcmp(_mqtt_prefs.mqtt_slot_preset[slot], MQTT_PRESET_CUSTOM) != 0) {
|
||||
strcpy(reply, "Error: topic only applies to custom slots");
|
||||
return;
|
||||
}
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_topic[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_topic[slot]), value);
|
||||
} else if (strcmp(field, "audience") == 0) {
|
||||
changed = wcCopyValue(_mqtt_prefs.mqtt_slot_audience[slot],
|
||||
sizeof(_mqtt_prefs.mqtt_slot_audience[slot]), value);
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
_wc_mqtt_dirty = true;
|
||||
strcpy(reply, "OK");
|
||||
} else if (reply[0] == 0) {
|
||||
strcpy(reply, "Error: invalid or unsupported value");
|
||||
}
|
||||
#else
|
||||
strcpy(reply, "Error: unsupported setting for this companion");
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::buildStatsJson(char* buf, size_t buf_size) {
|
||||
char ip[20] = "";
|
||||
int wifi_rssi = 0;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
strncpy(ip, WiFi.localIP().toString().c_str(), sizeof(ip) - 1);
|
||||
wifi_rssi = WiFi.RSSI();
|
||||
} else if (_webconfig && _webconfig->mode() == WebConfigServer::MODE_SETUP) {
|
||||
strncpy(ip, WiFi.softAPIP().toString().c_str(), sizeof(ip) - 1);
|
||||
}
|
||||
int pos = snprintf(buf, buf_size,
|
||||
"{\"uptime_s\":%lu,\"batt_mv\":%u,"
|
||||
"\"heap_free\":%lu,\"heap_min\":%lu,\"heap_max_alloc\":%lu,"
|
||||
"\"noise\":%d,\"rssi\":%d,\"snr\":%.1f,"
|
||||
"\"airtime_s\":%lu,\"rx_airtime_s\":%lu,"
|
||||
"\"recv\":%lu,\"sent\":%lu,\"rx_err\":%lu,"
|
||||
"\"sent_flood\":%lu,\"sent_direct\":%lu,\"recv_flood\":%lu,\"recv_direct\":%lu,"
|
||||
"\"tx_queue\":%d,\"wifi_rssi\":%d,\"ip\":\"%s\",\"mqtt_queue\":%d,\"slots\":[",
|
||||
(unsigned long)(millis() / 1000), (unsigned)board.getBattMilliVolts(),
|
||||
(unsigned long)ESP.getFreeHeap(), (unsigned long)ESP.getMinFreeHeap(),
|
||||
(unsigned long)ESP.getMaxAllocHeap(),
|
||||
(int)_radio->getNoiseFloor(), (int)radio_driver.getLastRSSI(),
|
||||
radio_driver.getLastSNR(),
|
||||
(unsigned long)(getTotalAirTime() / 1000), (unsigned long)(getReceiveAirTime() / 1000),
|
||||
(unsigned long)radio_driver.getPacketsRecv(), (unsigned long)radio_driver.getPacketsSent(),
|
||||
(unsigned long)radio_driver.getPacketsRecvErrors(),
|
||||
(unsigned long)getNumSentFlood(), (unsigned long)getNumSentDirect(),
|
||||
(unsigned long)getNumRecvFlood(), (unsigned long)getNumRecvDirect(),
|
||||
(int)_mgr->getOutboundCount(0xFFFFFFFF), wifi_rssi, ip,
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
_mqtt_bridge ? _mqtt_bridge->getQueueSize() : 0);
|
||||
#else
|
||||
0);
|
||||
#endif
|
||||
if (pos < 0 || pos >= static_cast<int>(buf_size) - 3) return;
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bool first = true;
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
MQTTBridge::SlotStatusSnapshot status;
|
||||
if (!MQTTBridge::getSlotStatusSnapshot(i, &status)) continue;
|
||||
int written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\"}",
|
||||
first ? "" : ",", i + 1, status.name, status.state);
|
||||
if (written < 0 || written >= static_cast<int>(buf_size - pos)) break;
|
||||
pos += written;
|
||||
first = false;
|
||||
}
|
||||
#endif
|
||||
snprintf(buf + pos, buf_size - pos, "]}");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@
|
||||
#include "DataStore.h"
|
||||
#include "NodePrefs.h"
|
||||
|
||||
#if defined(ESP32_PLATFORM) && defined(WIFI_SSID) && !defined(WEBCONFIG_DISABLED)
|
||||
#include <helpers/esp32/WebConfigServer.h>
|
||||
#endif
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
#include <helpers/CompanionMqttSetupPortal.h>
|
||||
#include <helpers/MQTTPrefs.h>
|
||||
@@ -90,7 +94,11 @@ struct AdvertPath {
|
||||
uint8_t path[MAX_PATH_SIZE];
|
||||
};
|
||||
|
||||
class MyMesh : public BaseChatMesh, public DataStoreHost {
|
||||
class MyMesh : public BaseChatMesh, public DataStoreHost
|
||||
#ifdef WITH_WEBCONFIG
|
||||
, public WebConfigServer::Callbacks
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMeshTables &tables, DataStore& store, AbstractUITask* ui=NULL);
|
||||
|
||||
@@ -102,12 +110,24 @@ public:
|
||||
uint32_t getBLEPin();
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
void serviceMQTT(const char* wifi_ssid, const char* wifi_password,
|
||||
bool allow_setup_page);
|
||||
void stopMQTTSetupPage();
|
||||
void serviceMQTT(const char* wifi_ssid, const char* wifi_password);
|
||||
bool isMQTTConfigured() const { return _mqtt_configured; }
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
bool startWebConfig(bool force_ap, char* reply);
|
||||
void stopWebConfig();
|
||||
void serviceWebConfig();
|
||||
bool isWebConfigSetupActive() const;
|
||||
|
||||
void getNodeSnapshot(WebConfigServer::NodeSnapshot& snapshot) override;
|
||||
void execCommand(char* cmd, char* reply) override;
|
||||
void rebootNow() override;
|
||||
void onConfigBatchStart() override;
|
||||
void onConfigBatchEnd() override;
|
||||
void buildStatsJson(char* buf, size_t buf_size) override;
|
||||
#endif
|
||||
|
||||
void loop();
|
||||
void handleCmdFrame(size_t len);
|
||||
bool advert();
|
||||
@@ -237,9 +257,12 @@ private:
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
|
||||
MQTTPrefs _mqtt_prefs;
|
||||
MQTTBridge* _mqtt_bridge;
|
||||
CompanionMqttSetupPortal _mqtt_setup;
|
||||
bool _mqtt_configured;
|
||||
bool _mqtt_started;
|
||||
#endif
|
||||
#ifdef WITH_WEBCONFIG
|
||||
WebConfigServer* _webconfig;
|
||||
bool _wc_mqtt_dirty;
|
||||
#endif
|
||||
uint32_t pending_login;
|
||||
uint32_t pending_status;
|
||||
|
||||
@@ -128,7 +128,7 @@ void halt() {
|
||||
static char configured_wifi_ssid[32];
|
||||
static char configured_wifi_password[64];
|
||||
|
||||
#if defined(WITH_MQTT_BRIDGE) && defined(DISPLAY_CLASS)
|
||||
#if defined(WITH_WEBCONFIG) && defined(DISPLAY_CLASS)
|
||||
static DisplayDriver* companion_setup_display = nullptr;
|
||||
static unsigned long companion_setup_display_refresh = 0;
|
||||
|
||||
@@ -142,9 +142,23 @@ void halt() {
|
||||
companion_setup_display->setTextSize(1);
|
||||
companion_setup_display->setColor(DisplayDriver::LIGHT);
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
char setup_ssid[33] = {0};
|
||||
char setup_ip[16] = {0};
|
||||
if (WebConfigServer::getSetupInfo(setup_ssid, sizeof(setup_ssid),
|
||||
setup_ip, sizeof(setup_ip))) {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 0, "MQTT setup");
|
||||
companion_setup_display->width() / 2, 0, "WebUI setup");
|
||||
companion_setup_display->setCursor(0, 14);
|
||||
companion_setup_display->print("Join open WiFi:");
|
||||
companion_setup_display->drawTextEllipsized(
|
||||
0, 25, companion_setup_display->width(), setup_ssid);
|
||||
companion_setup_display->setCursor(0, 39);
|
||||
companion_setup_display->print("Open in browser:");
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 51, setup_ip);
|
||||
} else if (WiFi.status() == WL_CONNECTED) {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 0, "WebUI");
|
||||
companion_setup_display->setCursor(0, 14);
|
||||
companion_setup_display->print("Join WiFi:");
|
||||
companion_setup_display->drawTextEllipsized(
|
||||
@@ -154,18 +168,6 @@ void halt() {
|
||||
const String ip = WiFi.localIP().toString();
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 51, ip.c_str());
|
||||
} else if (wifiSetupPortal().isActive()) {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 0, "WiFi setup");
|
||||
companion_setup_display->setCursor(0, 14);
|
||||
companion_setup_display->print("Join open WiFi:");
|
||||
companion_setup_display->drawTextEllipsized(
|
||||
0, 25, companion_setup_display->width(), COMPANION_WIFI_SETUP_AP);
|
||||
companion_setup_display->setCursor(0, 39);
|
||||
companion_setup_display->print("Open in browser:");
|
||||
const String ip = wifiSetupPortal().apIP().toString();
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 51, ip.c_str());
|
||||
} else {
|
||||
companion_setup_display->drawTextCentered(
|
||||
companion_setup_display->width() / 2, 8, "WiFi connecting");
|
||||
@@ -286,7 +288,7 @@ void setup() {
|
||||
DisplayDriver* disp = NULL;
|
||||
if (display.begin()) {
|
||||
disp = &display;
|
||||
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_MQTT_BRIDGE)
|
||||
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_WEBCONFIG)
|
||||
companion_setup_display = disp;
|
||||
#endif
|
||||
disp->startFrame();
|
||||
@@ -415,12 +417,25 @@ void setup() {
|
||||
WiFi.mode(WIFI_STA);
|
||||
wifi_disconnected_since = millis() ? millis() : 1;
|
||||
WiFi.begin(configured_wifi_ssid, configured_wifi_password);
|
||||
} else if (!wifiSetupPortal().begin(COMPANION_WIFI_SETUP_AP, saveCompanionWiFi, nullptr)) {
|
||||
}
|
||||
#ifndef WITH_WEBCONFIG
|
||||
else if (!wifiSetupPortal().begin(COMPANION_WIFI_SETUP_AP, saveCompanionWiFi, nullptr)) {
|
||||
WIFI_DEBUG_PRINTLN("WiFi setup: could not start setup portal");
|
||||
}
|
||||
// Disable WiFi modem power-save: its periodic modem/light-sleep stalls the SX1262 SPI+DIO servicing,
|
||||
// which makes the LoRa radio go deaf (no TX/RX) while WiFi is associated. Required for LoRa+WiFi to
|
||||
// coexist on this ESP32 — the small extra idle current is well worth a working radio.
|
||||
#endif
|
||||
#ifdef WITH_WEBCONFIG
|
||||
// WiFi companions expose the shared WebUI by default. With no stored
|
||||
// credentials it opens the captive setup AP; otherwise it waits for the
|
||||
// station connection and serves the same page on the LAN.
|
||||
if (WebConfigServer::loadEnabled(true)) {
|
||||
char web_reply[160];
|
||||
the_mesh.startWebConfig(!have_wifi, web_reply);
|
||||
WIFI_DEBUG_PRINTLN("%s", web_reply);
|
||||
}
|
||||
#endif
|
||||
// Disable WiFi modem power-save after starting WebConfig: that startup may
|
||||
// restore a saved ESP-IDF WiFi power policy. Companion radios must keep
|
||||
// modem sleep disabled because its pauses can stall SX1262 SPI/DIO service.
|
||||
WiFi.setSleep(false);
|
||||
serial_interface.begin(TCP_PORT);
|
||||
#ifdef ENABLE_OTA
|
||||
@@ -491,11 +506,15 @@ void loop() {
|
||||
the_mesh.loop();
|
||||
sensors.loop();
|
||||
#ifdef DISPLAY_CLASS
|
||||
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_MQTT_BRIDGE)
|
||||
if (the_mesh.isMQTTConfigured()) {
|
||||
ui_task.loop();
|
||||
} else {
|
||||
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_WEBCONFIG)
|
||||
if (the_mesh.isWebConfigSetupActive()
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|| !the_mesh.isMQTTConfigured()
|
||||
#endif
|
||||
) {
|
||||
renderCompanionSetupDisplay();
|
||||
} else {
|
||||
ui_task.loop();
|
||||
}
|
||||
#else
|
||||
ui_task.loop();
|
||||
@@ -521,23 +540,51 @@ void loop() {
|
||||
}
|
||||
|
||||
#if defined(ESP32) && defined(WIFI_SSID)
|
||||
#ifdef WITH_WEBCONFIG
|
||||
the_mesh.serviceWebConfig();
|
||||
#endif
|
||||
#ifdef ENABLE_OTA
|
||||
ota_seeder_loop(); // accept/drop a motatool `serve --tcp` connection on the dedicated seeder port
|
||||
ota_console_loop(); // service the OTA text console (port 5002)
|
||||
#endif
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
wifi_disconnected_since = 0;
|
||||
if (wifi_setup_recovery_mode && !wifiSetupPortal().isActive()) {
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (wifi_setup_recovery_mode && the_mesh.isWebConfigSetupActive()) {
|
||||
the_mesh.stopWebConfig();
|
||||
wifi_setup_recovery_mode = false;
|
||||
}
|
||||
#endif
|
||||
if (wifi_setup_recovery_mode
|
||||
#ifdef WITH_WEBCONFIG
|
||||
&& !the_mesh.isWebConfigSetupActive()
|
||||
#else
|
||||
&& !wifiSetupPortal().isActive()
|
||||
#endif
|
||||
) {
|
||||
wifi_setup_recovery_mode = false;
|
||||
}
|
||||
} else if (configured_wifi_ssid[0]) {
|
||||
unsigned long now = millis();
|
||||
if (wifi_disconnected_since == 0) wifi_disconnected_since = now ? now : 1;
|
||||
if (!wifi_setup_recovery_mode && !wifiSetupPortal().isActive()
|
||||
&& now - wifi_disconnected_since >= WIFI_SETUP_FALLBACK_MS) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
the_mesh.stopMQTTSetupPage();
|
||||
if (!wifi_setup_recovery_mode
|
||||
#ifdef WITH_WEBCONFIG
|
||||
&& !the_mesh.isWebConfigSetupActive()
|
||||
#else
|
||||
&& !wifiSetupPortal().isActive()
|
||||
#endif
|
||||
&& now - wifi_disconnected_since >= WIFI_SETUP_FALLBACK_MS) {
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (WebConfigServer::loadEnabled(true)) {
|
||||
char web_reply[160];
|
||||
if (the_mesh.startWebConfig(true, web_reply)) {
|
||||
wifi_setup_recovery_mode = true;
|
||||
WIFI_DEBUG_PRINTLN("WiFi unavailable for two minutes; %s", web_reply);
|
||||
} else {
|
||||
wifi_disconnected_since = now;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (wifiSetupPortal().begin(COMPANION_WIFI_SETUP_AP, saveCompanionWiFi, nullptr)) {
|
||||
wifiSetupPortal().configureRecovery(
|
||||
configured_wifi_ssid, configured_wifi_password, WIFI_SETUP_FALLBACK_MS);
|
||||
@@ -547,10 +594,17 @@ void loop() {
|
||||
// Avoid retrying portal creation on every pass through loop().
|
||||
wifi_disconnected_since = now;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
// Safely attempt to reconnect every 10 seconds if flagged
|
||||
if (!wifiSetupPortal().isActive() && wifi_needs_reconnect
|
||||
if (
|
||||
#ifdef WITH_WEBCONFIG
|
||||
!the_mesh.isWebConfigSetupActive()
|
||||
#else
|
||||
!wifiSetupPortal().isActive()
|
||||
#endif
|
||||
&& wifi_needs_reconnect
|
||||
&& (millis() - last_wifi_reconnect_attempt > 10000)) {
|
||||
WIFI_DEBUG_PRINTLN("Attempting manual WiFi reconnect...");
|
||||
WiFi.disconnect();
|
||||
@@ -558,8 +612,7 @@ void loop() {
|
||||
last_wifi_reconnect_attempt = millis();
|
||||
}
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
the_mesh.serviceMQTT(configured_wifi_ssid, configured_wifi_password,
|
||||
!wifiSetupPortal().isActive());
|
||||
the_mesh.serviceMQTT(configured_wifi_ssid, configured_wifi_password);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
#include "MyMesh.h"
|
||||
#include <algorithm>
|
||||
#include <stdlib.h> // for qsort()
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/ClockSyncUtils.h>
|
||||
#include <helpers/RxReservePacketManager.h>
|
||||
#ifdef WITH_WEBCONFIG
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
/* ------------------------------ Config -------------------------------- */
|
||||
|
||||
@@ -2644,6 +2648,23 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
_alerter.setBridge(mqtt_bridge);
|
||||
#endif
|
||||
|
||||
#if defined(WITH_WEBCONFIG) && !defined(WEBCONFIG_NO_AUTO_AP)
|
||||
bool start_webui = WebConfigServer::loadEnabled(false);
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Preserve the MQTT observer's first-boot setup experience even though the
|
||||
// persistent WebUI master switch defaults off on infrastructure roles.
|
||||
start_webui = start_webui || _cli.getObserverPrefs()->wifi_ssid[0] == 0;
|
||||
if (start_webui && _cli.getObserverPrefs()->wifi_ssid[0] == 0) {
|
||||
if (mqtt_bridge && mqtt_bridge->isRunning()) mqtt_bridge->end();
|
||||
}
|
||||
#endif
|
||||
if (start_webui) {
|
||||
char wc_reply[160];
|
||||
startWebConfig(false, wc_reply);
|
||||
Serial.println(wc_reply);
|
||||
}
|
||||
#endif
|
||||
|
||||
saved_radio_apply_pending = !applySavedRadioParams();
|
||||
if (!saved_radio_apply_pending) {
|
||||
radio_driver.setTxPower(_prefs.tx_power_dbm);
|
||||
@@ -3990,9 +4011,27 @@ static uint8_t floodPacketFilterProtectedHops(const mesh::Packet* packet) {
|
||||
}
|
||||
}
|
||||
|
||||
void MyMesh::seedDefaultFloodPacketFilters() {
|
||||
auto& entry = flood_packet_filters[0];
|
||||
memset(&entry, 0, sizeof(entry));
|
||||
entry.active = true;
|
||||
entry.payload_type = PAYLOAD_TYPE_OTA;
|
||||
entry.min_hops = 0;
|
||||
entry.max_hops = FLOOD_PACKET_FILTER_MAX_HOPS;
|
||||
entry.suspend_on_temp_radio = true;
|
||||
}
|
||||
|
||||
void MyMesh::loadFloodPacketFilters() {
|
||||
memset(flood_packet_filters, 0, sizeof(flood_packet_filters));
|
||||
if (_fs == NULL || !_fs->exists(FLOOD_PACKET_FILTER_FILE)) return;
|
||||
if (_fs == NULL) {
|
||||
seedDefaultFloodPacketFilters();
|
||||
return;
|
||||
}
|
||||
if (!_fs->exists(FLOOD_PACKET_FILTER_FILE)) {
|
||||
seedDefaultFloodPacketFilters();
|
||||
saveFloodPacketFilters();
|
||||
return;
|
||||
}
|
||||
|
||||
File file = openFloodChannelBlockRead(_fs, FLOOD_PACKET_FILTER_FILE);
|
||||
if (!file) return;
|
||||
@@ -5995,6 +6034,210 @@ void MyMesh::clearStats() {
|
||||
((SimpleMeshTables *)getTables())->resetStats();
|
||||
}
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
|
||||
memset(&s, 0, sizeof(s));
|
||||
StrHelper::strncpy(s.name, _prefs.node_name, sizeof(s.name));
|
||||
StrHelper::strncpy(s.admin_password, _prefs.password, sizeof(s.admin_password));
|
||||
s.lat = _prefs.node_lat;
|
||||
s.lon = _prefs.node_lon;
|
||||
s.freq = _prefs.freq;
|
||||
s.bw = _prefs.bw;
|
||||
s.sf = _prefs.sf;
|
||||
s.cr = _prefs.cr;
|
||||
s.tx_power = _prefs.tx_power_dbm;
|
||||
s.airtime_factor = _prefs.airtime_factor;
|
||||
s.rx_delay = _prefs.rx_delay_base;
|
||||
s.tx_delay = _prefs.tx_delay_factor;
|
||||
s.cad = _prefs.cad_enabled;
|
||||
s.rx_gain = _prefs.rx_boosted_gain;
|
||||
s.repeat = !_prefs.disable_fwd;
|
||||
s.advert_interval = _prefs.advert_interval * 2;
|
||||
s.flood_advert_interval = _prefs.flood_advert_interval;
|
||||
s.flood_max = _prefs.flood_max;
|
||||
s.flood_max_advert = _prefs.flood_max_advert;
|
||||
s.flood_max_unscoped = _prefs.flood_max_unscoped;
|
||||
s.loop_detect = _prefs.loop_detect;
|
||||
s.capabilities = WebConfigServer::CAP_LOCATION | WebConfigServer::CAP_AIRTIME
|
||||
| WebConfigServer::CAP_DELAYS | WebConfigServer::CAP_CAD
|
||||
| WebConfigServer::CAP_RX_GAIN | WebConfigServer::CAP_REPEAT
|
||||
| WebConfigServer::CAP_ADVERT | WebConfigServer::CAP_FLOOD
|
||||
| WebConfigServer::CAP_LOOP | WebConfigServer::CAP_WIFI_POWER_SAVE;
|
||||
}
|
||||
|
||||
bool MyMesh::startWebConfig(bool force_ap, char* reply) {
|
||||
if (_cli.getBoard()->isOTAUpdateRunning()) {
|
||||
strcpy(reply, "Err: OTA server is running - 'stop ota' first");
|
||||
return true;
|
||||
}
|
||||
if (_webconfig && (_webconfig->isRunning() || _webconfig->isStopping())) {
|
||||
strcpy(reply, _webconfig->isStopping() ? "Err: webconfig still stopping, retry shortly"
|
||||
: "Err: webconfig already running");
|
||||
return true;
|
||||
}
|
||||
if (!_webconfig) {
|
||||
void* mqtt_prefs = nullptr;
|
||||
bool owns_wifi = true;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
mqtt_prefs = _cli.getObserverPrefs();
|
||||
owns_wifi = false;
|
||||
#endif
|
||||
_webconfig = new WebConfigServer(this, mqtt_prefs, owns_wifi,
|
||||
self_id.pub_key, getFirmwareVer(), getRole(),
|
||||
_cli.getBoard()->getManufacturerName());
|
||||
if (!_webconfig) {
|
||||
strcpy(reply, "Err: not enough memory for webconfig");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (force_ap) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (mqtt_bridge && mqtt_bridge->isRunning()) {
|
||||
strcpy(reply, "Err: MQTT bridge is running - 'set bridge off' first");
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
_webconfig->startSetupMode(reply);
|
||||
} else {
|
||||
_webconfig->startAutoMode(reply);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::stopWebConfig(char* reply) {
|
||||
if (!_webconfig || !_webconfig->isRunning()) {
|
||||
strcpy(reply, "Err: webconfig not running");
|
||||
return true;
|
||||
}
|
||||
_webconfig->requestStop();
|
||||
strcpy(reply, "OK - webconfig stopping");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::setWebUIEnabled(bool enabled, char* reply) {
|
||||
if (!WebConfigServer::saveEnabled(enabled)) {
|
||||
strcpy(reply, "Error: failed to save webui setting");
|
||||
return true;
|
||||
}
|
||||
if (enabled) {
|
||||
if (_webconfig && (_webconfig->isRunning() || _webconfig->isStopping())) {
|
||||
strcpy(reply, "OK - webui on (already active)");
|
||||
} else {
|
||||
startWebConfig(false, reply);
|
||||
if (strncmp(reply, "WebConfig", 9) == 0) {
|
||||
char tmp[160];
|
||||
StrHelper::strncpy(tmp, reply, sizeof(tmp));
|
||||
snprintf(reply, 160, "OK - webui on; %s", tmp);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (_webconfig && _webconfig->isRunning()) _webconfig->requestStop();
|
||||
strcpy(reply, "OK - webui off");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::getWebUIStatus(char* reply) const {
|
||||
const bool enabled = WebConfigServer::loadEnabled(false);
|
||||
if (!_webconfig || (!_webconfig->isRunning() && !_webconfig->isStopping())) {
|
||||
snprintf(reply, 160, "> %s, inactive", enabled ? "on" : "off");
|
||||
} else if (_webconfig->mode() == WebConfigServer::MODE_SETUP) {
|
||||
char ssid[33], ip[16];
|
||||
WebConfigServer::getSetupInfo(ssid, sizeof(ssid), ip, sizeof(ip));
|
||||
snprintf(reply, 160, "> %s, setup AP %s http://%s/", enabled ? "on" : "off", ssid, ip);
|
||||
} else if (_webconfig->mode() == WebConfigServer::MODE_CONNECTING) {
|
||||
snprintf(reply, 160, "> %s, connecting to WiFi", enabled ? "on" : "off");
|
||||
} else {
|
||||
snprintf(reply, 160, "> %s, http://%s/", enabled ? "on" : "off",
|
||||
WiFi.localIP().toString().c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyMesh::onConfigBatchEnd() {
|
||||
_wc_batch_active = false;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_wc_restart_pending) {
|
||||
_wc_restart_pending = false;
|
||||
_wc_slot_restart_mask = 0;
|
||||
restartBridge();
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t mask = _wc_slot_restart_mask;
|
||||
_wc_slot_restart_mask = 0;
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
if (mask & (1U << i)) restartBridgeSlot(i);
|
||||
}
|
||||
#else
|
||||
_wc_restart_pending = false;
|
||||
_wc_slot_restart_mask = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::buildStatsJson(char* buf, size_t buf_size) {
|
||||
char ip[20] = "";
|
||||
int wifi_rssi = 0;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
strncpy(ip, WiFi.localIP().toString().c_str(), sizeof(ip) - 1);
|
||||
wifi_rssi = WiFi.RSSI();
|
||||
} else if (_webconfig && _webconfig->mode() == WebConfigServer::MODE_SETUP) {
|
||||
strncpy(ip, WiFi.softAPIP().toString().c_str(), sizeof(ip) - 1);
|
||||
}
|
||||
int pos = snprintf(buf, buf_size,
|
||||
"{\"uptime_s\":%lu,\"batt_mv\":%u,"
|
||||
"\"heap_free\":%lu,\"heap_min\":%lu,\"heap_max_alloc\":%lu,"
|
||||
"\"noise\":%d,\"rssi\":%d,\"snr\":%.1f,"
|
||||
"\"airtime_s\":%lu,\"rx_airtime_s\":%lu,"
|
||||
"\"recv\":%lu,\"sent\":%lu,\"rx_err\":%lu,"
|
||||
"\"sent_flood\":%lu,\"sent_direct\":%lu,\"recv_flood\":%lu,\"recv_direct\":%lu,"
|
||||
"\"tx_queue\":%d,\"wifi_rssi\":%d,\"ip\":\"%s\",\"mqtt_queue\":%d,\"slots\":[",
|
||||
(unsigned long)(uptime_millis / 1000), (unsigned)board.getBattMilliVolts(),
|
||||
(unsigned long)ESP.getFreeHeap(), (unsigned long)ESP.getMinFreeHeap(),
|
||||
(unsigned long)ESP.getMaxAllocHeap(),
|
||||
(int)_radio->getNoiseFloor(), (int)radio_driver.getLastRSSI(),
|
||||
radio_driver.getLastSNR(),
|
||||
(unsigned long)(getTotalAirTime() / 1000), (unsigned long)(getReceiveAirTime() / 1000),
|
||||
(unsigned long)radio_driver.getPacketsRecv(), (unsigned long)radio_driver.getPacketsSent(),
|
||||
(unsigned long)radio_driver.getPacketsRecvErrors(),
|
||||
(unsigned long)getNumSentFlood(), (unsigned long)getNumSentDirect(),
|
||||
(unsigned long)getNumRecvFlood(), (unsigned long)getNumRecvDirect(),
|
||||
(int)_mgr->getOutboundCount(0xFFFFFFFF), wifi_rssi, ip,
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
mqtt_bridge ? mqtt_bridge->getQueueSize() : 0);
|
||||
#else
|
||||
0);
|
||||
#endif
|
||||
if (pos < 0 || pos >= static_cast<int>(buf_size) - 3) return;
|
||||
|
||||
bool first = true;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
MQTTBridge::SlotStatusSnapshot status;
|
||||
if (!MQTTBridge::getSlotStatusSnapshot(i, &status)) continue;
|
||||
int written;
|
||||
if (status.has_publish_counts) {
|
||||
written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu}",
|
||||
first ? "" : ",", i + 1, status.name, status.state,
|
||||
status.publish_ok, status.publish_err);
|
||||
} else {
|
||||
written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\"}",
|
||||
first ? "" : ",", i + 1, status.name, status.state);
|
||||
}
|
||||
if (written < 0 || written >= static_cast<int>(buf_size - pos)) break;
|
||||
pos += written;
|
||||
first = false;
|
||||
}
|
||||
#else
|
||||
(void)first;
|
||||
#endif
|
||||
snprintf(buf + pos, buf_size - pos, "]}");
|
||||
}
|
||||
#endif
|
||||
|
||||
static char* trimSpaces(char* s) {
|
||||
while (*s == ' ') s++;
|
||||
char* end = s + strlen(s);
|
||||
@@ -6090,10 +6333,27 @@ static void formatPathReply(const uint8_t* path, uint8_t path_len, char* out, si
|
||||
|
||||
uint8_t hash_size = (path_len >> 6) + 1;
|
||||
uint8_t hop_count = path_len & 63;
|
||||
uint8_t byte_len = hop_count * hash_size;
|
||||
char hex[(MAX_PATH_SIZE * 2) + 1];
|
||||
mesh::Utils::toHex(hex, path, byte_len);
|
||||
snprintf(out, out_len, "> hs=%u hops=%u hex=%s", (uint32_t)hash_size, (uint32_t)hop_count, hex);
|
||||
size_t path_text_len = (size_t)hop_count * hash_size * 2 + (hop_count - 1);
|
||||
|
||||
// Every path that fits in the 160-byte CLI command buffer also fits this
|
||||
// shorter reply form. Retain a compact, complete fallback for a path learned
|
||||
// from an unusually long over-the-air route rather than truncating it.
|
||||
if (path_text_len + 3 > out_len) {
|
||||
out[0] = '>';
|
||||
out[1] = ' ';
|
||||
mesh::Utils::toHex(out + 2, path, (size_t)hop_count * hash_size);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
out[pos++] = '>';
|
||||
out[pos++] = ' ';
|
||||
for (uint8_t hop = 0; hop < hop_count; hop++) {
|
||||
mesh::Utils::toHex(out + pos, path + ((size_t)hop * hash_size), hash_size);
|
||||
pos += hash_size * 2;
|
||||
if (hop + 1 < hop_count) out[pos++] = ',';
|
||||
}
|
||||
out[pos] = 0;
|
||||
}
|
||||
|
||||
static bool commandFamilyMatches(const char* command, const char* family) {
|
||||
@@ -6227,6 +6487,8 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
|
||||
command += 3;
|
||||
}
|
||||
|
||||
mesh::cli::normalizeCommandVerb(command);
|
||||
|
||||
if (sender && !sender->isAdmin()) {
|
||||
bool allowed = (sender->isRegionMgr() && isRegionMgrAllowed(command))
|
||||
|| (sender->isFilterMgr() && isFilterMgrAllowed(command));
|
||||
@@ -6600,6 +6862,21 @@ void MyMesh::loop() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (WebConfigServer::takeButtonToggleRequest()) {
|
||||
char wc_reply[160];
|
||||
setWebUIEnabled(!WebConfigServer::loadEnabled(false), wc_reply);
|
||||
Serial.println(wc_reply);
|
||||
}
|
||||
if (_webconfig) {
|
||||
_webconfig->tick(millis());
|
||||
if (!_webconfig->isRunning() && !_webconfig->isStopping()) {
|
||||
delete _webconfig;
|
||||
_webconfig = nullptr;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// is pending dirty contacts write needed?
|
||||
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
|
||||
acl.save(_fs);
|
||||
|
||||
@@ -48,6 +48,10 @@
|
||||
#define WITH_BRIDGE
|
||||
#endif
|
||||
|
||||
#if defined(ESP_PLATFORM) && defined(ADMIN_PASSWORD) && !defined(WEBCONFIG_DISABLED)
|
||||
#include "helpers/esp32/WebConfigServer.h"
|
||||
#endif
|
||||
|
||||
#ifdef WITH_SNMP
|
||||
#include "helpers/SNMPAgent.h"
|
||||
#endif
|
||||
@@ -168,7 +172,11 @@ struct NeighbourInfo {
|
||||
#define RADIO_APPLY_RETRY_INTERVAL_MILLIS 1000UL
|
||||
#define SCHEDULED_RADIO_CLOCK_CHECKPOINT_SECS 60UL
|
||||
|
||||
class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
class MyMesh : public mesh::Mesh, public CommonCLICallbacks
|
||||
#ifdef WITH_WEBCONFIG
|
||||
, public WebConfigServer::Callbacks
|
||||
#endif
|
||||
{
|
||||
struct ScheduledRadioSetting {
|
||||
bool active;
|
||||
bool temporary;
|
||||
@@ -341,6 +349,12 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
AlertReporter _alerter;
|
||||
#endif
|
||||
#ifdef WITH_WEBCONFIG
|
||||
WebConfigServer* _webconfig = nullptr;
|
||||
bool _wc_batch_active = false;
|
||||
bool _wc_restart_pending = false;
|
||||
uint8_t _wc_slot_restart_mask = 0;
|
||||
#endif
|
||||
|
||||
bool extractDirectRetryPrefix(const mesh::Packet* packet, uint8_t* prefix, uint8_t& prefix_len) const;
|
||||
int8_t getDirectRetryMinSNRX4() const;
|
||||
@@ -406,6 +420,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
bool shouldBlockFloodChannelForward(const mesh::Packet* packet) const;
|
||||
void loadFloodPacketFilters();
|
||||
bool saveFloodPacketFilters();
|
||||
void seedDefaultFloodPacketFilters();
|
||||
bool shouldBlockFloodPacketForward(const mesh::Packet* packet) const;
|
||||
void formatFloodPacketFilters(const char* args, char* reply) const;
|
||||
void formatFloodPacketFilterDetail(int index, char* reply, size_t reply_len) const;
|
||||
@@ -669,6 +684,12 @@ public:
|
||||
void restartBridge() override {
|
||||
AbstractBridge* active_bridge = activeBridge();
|
||||
if (!active_bridge || !active_bridge->isRunning()) return;
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (_wc_batch_active) {
|
||||
_wc_restart_pending = true;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
active_bridge->end();
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Set device metadata before restarting bridge (same as in begin())
|
||||
@@ -687,6 +708,12 @@ public:
|
||||
void restartBridgeSlot(int slot) override {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (!mqtt_bridge || !mqtt_bridge->isRunning()) return;
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (_wc_batch_active && slot >= 0 && slot < MAX_MQTT_SLOTS) {
|
||||
_wc_slot_restart_mask |= static_cast<uint8_t>(1U << slot);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
mqtt_bridge->setSlotPreset(slot, _cli.getObserverPrefs()->mqtt_slot_preset[slot]);
|
||||
#else
|
||||
(void)slot;
|
||||
@@ -741,6 +768,26 @@ public:
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
bool startWebConfig(bool force_ap, char* reply) override;
|
||||
bool stopWebConfig(char* reply) override;
|
||||
bool setWebUIEnabled(bool enabled, char* reply) override;
|
||||
bool getWebUIStatus(char* reply) const override;
|
||||
bool isWebConfigActive() const override {
|
||||
return _webconfig && (_webconfig->isRunning() || _webconfig->isStopping());
|
||||
}
|
||||
void getNodeSnapshot(WebConfigServer::NodeSnapshot& snapshot) override;
|
||||
void execCommand(char* cmd, char* reply) override { handleCommand(0, cmd, reply); }
|
||||
void rebootNow() override { _cli.getBoard()->reboot(); }
|
||||
void onConfigBatchStart() override {
|
||||
_wc_batch_active = true;
|
||||
_wc_restart_pending = false;
|
||||
_wc_slot_restart_mask = 0;
|
||||
}
|
||||
void onConfigBatchEnd() override;
|
||||
void buildStatsJson(char* buf, size_t buf_size) override;
|
||||
#endif
|
||||
|
||||
// To check if there is pending work
|
||||
bool hasPendingWork() const;
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
#define USER_BTN_PRESSED LOW
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <WiFi.h>
|
||||
#include <helpers/esp32/WebConfigServer.h>
|
||||
#endif
|
||||
|
||||
#define AUTO_OFF_MILLIS 20000 // 20 seconds
|
||||
@@ -97,6 +98,38 @@ void UITask::renderCurrScreen() {
|
||||
_display->setCursor((_display->width() - poffWidth) / 2, 48);
|
||||
_display->drawTextCentered(_display->width()/2, 48, poweroff_string);
|
||||
} else {
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (WebConfigServer::isRebootPending()) {
|
||||
_display->setTextSize(1);
|
||||
_display->setColor(DisplayDriver::GREEN);
|
||||
_display->setCursor(0, 14);
|
||||
_display->print("Config saved!");
|
||||
_display->setColor(DisplayDriver::LIGHT);
|
||||
_display->setCursor(0, 30);
|
||||
_display->print("Rebooting...");
|
||||
return;
|
||||
}
|
||||
char wc_ssid[33], wc_ip[16];
|
||||
if (WebConfigServer::getSetupInfo(wc_ssid, sizeof(wc_ssid), wc_ip, sizeof(wc_ip))) {
|
||||
_display->setTextSize(1);
|
||||
_display->setColor(DisplayDriver::GREEN);
|
||||
_display->setCursor(0, 0);
|
||||
_display->print("WebUI WiFi Setup");
|
||||
_display->setColor(DisplayDriver::LIGHT);
|
||||
_display->setCursor(0, 14);
|
||||
_display->print("Join WiFi:");
|
||||
_display->setColor(DisplayDriver::YELLOW);
|
||||
_display->setCursor(6, 24);
|
||||
_display->print(wc_ssid);
|
||||
_display->setColor(DisplayDriver::LIGHT);
|
||||
_display->setCursor(0, 40);
|
||||
_display->print("Then browse to:");
|
||||
_display->setColor(DisplayDriver::YELLOW);
|
||||
_display->setCursor(6, 50);
|
||||
_display->print(wc_ip);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
_display->setCursor(0, 0);
|
||||
_display->setTextSize(1);
|
||||
_display->setColor(DisplayDriver::GREEN);
|
||||
@@ -140,6 +173,21 @@ void UITask::loop() {
|
||||
_display->turnOn();
|
||||
Serial.println("Powering Off");
|
||||
_powering_off_at = millis() + POWEROFF_DELAY;
|
||||
#ifdef WITH_WEBCONFIG
|
||||
} else if (ev == BUTTON_EVENT_TRIPLE_CLICK) {
|
||||
// Single click wakes the display and long press powers off. Triple-click
|
||||
// was unused in the repeater UI, so use it for the persistent WebUI toggle.
|
||||
WebConfigServer::requestToggleFromButton();
|
||||
_display->turnOn();
|
||||
_auto_off = millis() + AUTO_OFF_MILLIS;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (WebConfigServer::getSetupInfo(nullptr, 0, nullptr, 0)) {
|
||||
if (!_display->isOn()) _display->turnOn();
|
||||
_auto_off = millis() + AUTO_OFF_MILLIS;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <Mesh.h>
|
||||
|
||||
#include "MyMesh.h"
|
||||
#if defined(ESP32) && MAX_RECENT_REPEATERS > 0
|
||||
#include <new>
|
||||
#endif
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
#include "UITask.h"
|
||||
@@ -15,8 +18,20 @@
|
||||
|
||||
StdRNG fast_rng;
|
||||
#if MAX_RECENT_REPEATERS > 0
|
||||
#if defined(ESP32)
|
||||
// Classic ESP32 has a much smaller link-time DRAM window than its runtime heap
|
||||
// map. Allocate this large, fixed-capacity history at startup so enabling WiFi
|
||||
// features (including WebConfig) does not overflow that static DRAM window.
|
||||
// Global constructors run before setup(), while the heap is still
|
||||
// unfragmented; a failed allocation safely falls back to no history table.
|
||||
SimpleMeshTables::RecentRepeaterInfo* recent_repeater_storage =
|
||||
new (std::nothrow) SimpleMeshTables::RecentRepeaterInfo[MAX_RECENT_REPEATERS];
|
||||
SimpleMeshTables tables(recent_repeater_storage,
|
||||
recent_repeater_storage ? MAX_RECENT_REPEATERS : 0);
|
||||
#else
|
||||
SimpleMeshTables::RecentRepeaterInfo recent_repeater_storage[MAX_RECENT_REPEATERS];
|
||||
SimpleMeshTables tables(recent_repeater_storage, MAX_RECENT_REPEATERS);
|
||||
#endif
|
||||
#else
|
||||
SimpleMeshTables tables;
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include "MyMesh.h"
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/RxReservePacketManager.h>
|
||||
#ifdef WITH_WEBCONFIG
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
@@ -883,6 +887,21 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
_alerter.begin(&_prefs, _cli.getObserverPrefs(), this, this);
|
||||
_alerter.setBridge(bridge);
|
||||
#endif
|
||||
|
||||
#if defined(WITH_WEBCONFIG) && !defined(WEBCONFIG_NO_AUTO_AP)
|
||||
bool start_webui = WebConfigServer::loadEnabled(false);
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
start_webui = start_webui || _cli.getObserverPrefs()->wifi_ssid[0] == 0;
|
||||
if (start_webui && _cli.getObserverPrefs()->wifi_ssid[0] == 0) {
|
||||
if (bridge && bridge->isRunning()) bridge->end();
|
||||
}
|
||||
#endif
|
||||
if (start_webui) {
|
||||
char wc_reply[160];
|
||||
startWebConfig(false, wc_reply);
|
||||
Serial.println(wc_reply);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool MyMesh::applySavedRadioParams() {
|
||||
@@ -1059,6 +1078,209 @@ void MyMesh::clearStats() {
|
||||
((SimpleMeshTables *)getTables())->resetStats();
|
||||
}
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
|
||||
memset(&s, 0, sizeof(s));
|
||||
StrHelper::strncpy(s.name, _prefs.node_name, sizeof(s.name));
|
||||
StrHelper::strncpy(s.admin_password, _prefs.password, sizeof(s.admin_password));
|
||||
s.lat = _prefs.node_lat;
|
||||
s.lon = _prefs.node_lon;
|
||||
s.freq = _prefs.freq;
|
||||
s.bw = _prefs.bw;
|
||||
s.sf = _prefs.sf;
|
||||
s.cr = _prefs.cr;
|
||||
s.tx_power = _prefs.tx_power_dbm;
|
||||
s.airtime_factor = _prefs.airtime_factor;
|
||||
s.rx_delay = _prefs.rx_delay_base;
|
||||
s.tx_delay = _prefs.tx_delay_factor;
|
||||
s.cad = _prefs.cad_enabled;
|
||||
s.rx_gain = _prefs.rx_boosted_gain;
|
||||
s.repeat = !_prefs.disable_fwd;
|
||||
s.advert_interval = _prefs.advert_interval * 2;
|
||||
s.flood_advert_interval = _prefs.flood_advert_interval;
|
||||
s.flood_max = _prefs.flood_max;
|
||||
s.flood_max_advert = _prefs.flood_max_advert;
|
||||
s.flood_max_unscoped = _prefs.flood_max_unscoped;
|
||||
s.loop_detect = _prefs.loop_detect;
|
||||
s.capabilities = WebConfigServer::CAP_LOCATION | WebConfigServer::CAP_AIRTIME
|
||||
| WebConfigServer::CAP_DELAYS | WebConfigServer::CAP_CAD
|
||||
| WebConfigServer::CAP_RX_GAIN | WebConfigServer::CAP_REPEAT
|
||||
| WebConfigServer::CAP_ADVERT | WebConfigServer::CAP_FLOOD
|
||||
| WebConfigServer::CAP_LOOP | WebConfigServer::CAP_WIFI_POWER_SAVE;
|
||||
}
|
||||
|
||||
bool MyMesh::startWebConfig(bool force_ap, char* reply) {
|
||||
if (_cli.getBoard()->isOTAUpdateRunning()) {
|
||||
strcpy(reply, "Err: OTA server is running - 'stop ota' first");
|
||||
return true;
|
||||
}
|
||||
if (_webconfig && (_webconfig->isRunning() || _webconfig->isStopping())) {
|
||||
strcpy(reply, _webconfig->isStopping() ? "Err: webconfig still stopping, retry shortly"
|
||||
: "Err: webconfig already running");
|
||||
return true;
|
||||
}
|
||||
if (!_webconfig) {
|
||||
void* mqtt_prefs = nullptr;
|
||||
bool owns_wifi = true;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
mqtt_prefs = _cli.getObserverPrefs();
|
||||
owns_wifi = false;
|
||||
#endif
|
||||
_webconfig = new WebConfigServer(this, mqtt_prefs, owns_wifi,
|
||||
self_id.pub_key, getFirmwareVer(), getRole(),
|
||||
_cli.getBoard()->getManufacturerName());
|
||||
if (!_webconfig) {
|
||||
strcpy(reply, "Err: not enough memory for webconfig");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (force_ap) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (bridge && bridge->isRunning()) {
|
||||
strcpy(reply, "Err: MQTT bridge is running - 'set bridge off' first");
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
_webconfig->startSetupMode(reply);
|
||||
} else {
|
||||
_webconfig->startAutoMode(reply);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::setWebUIEnabled(bool enabled, char* reply) {
|
||||
if (!WebConfigServer::saveEnabled(enabled)) {
|
||||
strcpy(reply, "Error: failed to save webui setting");
|
||||
return true;
|
||||
}
|
||||
if (enabled) {
|
||||
if (_webconfig && (_webconfig->isRunning() || _webconfig->isStopping())) {
|
||||
strcpy(reply, "OK - webui on (already active)");
|
||||
} else {
|
||||
startWebConfig(false, reply);
|
||||
if (strncmp(reply, "WebConfig", 9) == 0) {
|
||||
char tmp[160];
|
||||
StrHelper::strncpy(tmp, reply, sizeof(tmp));
|
||||
snprintf(reply, 160, "OK - webui on; %s", tmp);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (_webconfig && _webconfig->isRunning()) _webconfig->requestStop();
|
||||
strcpy(reply, "OK - webui off");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::getWebUIStatus(char* reply) const {
|
||||
const bool enabled = WebConfigServer::loadEnabled(false);
|
||||
if (!_webconfig || (!_webconfig->isRunning() && !_webconfig->isStopping())) {
|
||||
snprintf(reply, 160, "> %s, inactive", enabled ? "on" : "off");
|
||||
} else if (_webconfig->mode() == WebConfigServer::MODE_SETUP) {
|
||||
char ssid[33], ip[16];
|
||||
WebConfigServer::getSetupInfo(ssid, sizeof(ssid), ip, sizeof(ip));
|
||||
snprintf(reply, 160, "> %s, setup AP %s http://%s/", enabled ? "on" : "off", ssid, ip);
|
||||
} else if (_webconfig->mode() == WebConfigServer::MODE_CONNECTING) {
|
||||
snprintf(reply, 160, "> %s, connecting to WiFi", enabled ? "on" : "off");
|
||||
} else {
|
||||
snprintf(reply, 160, "> %s, http://%s/", enabled ? "on" : "off",
|
||||
WiFi.localIP().toString().c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MyMesh::stopWebConfig(char* reply) {
|
||||
if (!_webconfig || !_webconfig->isRunning()) {
|
||||
strcpy(reply, "Err: webconfig not running");
|
||||
return true;
|
||||
}
|
||||
_webconfig->requestStop();
|
||||
strcpy(reply, "OK - webconfig stopping");
|
||||
return true;
|
||||
}
|
||||
|
||||
void MyMesh::onConfigBatchEnd() {
|
||||
_wc_batch_active = false;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_wc_restart_pending) {
|
||||
_wc_restart_pending = false;
|
||||
_wc_slot_restart_mask = 0;
|
||||
restartBridge();
|
||||
return;
|
||||
}
|
||||
const uint8_t mask = _wc_slot_restart_mask;
|
||||
_wc_slot_restart_mask = 0;
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
if (mask & (1U << i)) restartBridgeSlot(i);
|
||||
}
|
||||
#else
|
||||
_wc_restart_pending = false;
|
||||
_wc_slot_restart_mask = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::buildStatsJson(char* buf, size_t buf_size) {
|
||||
char ip[20] = "";
|
||||
int wifi_rssi = 0;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
strncpy(ip, WiFi.localIP().toString().c_str(), sizeof(ip) - 1);
|
||||
wifi_rssi = WiFi.RSSI();
|
||||
} else if (_webconfig && _webconfig->mode() == WebConfigServer::MODE_SETUP) {
|
||||
strncpy(ip, WiFi.softAPIP().toString().c_str(), sizeof(ip) - 1);
|
||||
}
|
||||
int pos = snprintf(buf, buf_size,
|
||||
"{\"uptime_s\":%lu,\"batt_mv\":%u,"
|
||||
"\"heap_free\":%lu,\"heap_min\":%lu,\"heap_max_alloc\":%lu,"
|
||||
"\"noise\":%d,\"rssi\":%d,\"snr\":%.1f,"
|
||||
"\"airtime_s\":%lu,\"rx_airtime_s\":%lu,"
|
||||
"\"recv\":%lu,\"sent\":%lu,\"rx_err\":%lu,"
|
||||
"\"sent_flood\":%lu,\"sent_direct\":%lu,\"recv_flood\":%lu,\"recv_direct\":%lu,"
|
||||
"\"tx_queue\":%d,\"wifi_rssi\":%d,\"ip\":\"%s\",\"mqtt_queue\":%d,\"slots\":[",
|
||||
(unsigned long)(uptime_millis / 1000), (unsigned)board.getBattMilliVolts(),
|
||||
(unsigned long)ESP.getFreeHeap(), (unsigned long)ESP.getMinFreeHeap(),
|
||||
(unsigned long)ESP.getMaxAllocHeap(),
|
||||
(int)_radio->getNoiseFloor(), (int)radio_driver.getLastRSSI(),
|
||||
radio_driver.getLastSNR(),
|
||||
(unsigned long)(getTotalAirTime() / 1000), (unsigned long)(getReceiveAirTime() / 1000),
|
||||
(unsigned long)radio_driver.getPacketsRecv(), (unsigned long)radio_driver.getPacketsSent(),
|
||||
(unsigned long)radio_driver.getPacketsRecvErrors(),
|
||||
(unsigned long)getNumSentFlood(), (unsigned long)getNumSentDirect(),
|
||||
(unsigned long)getNumRecvFlood(), (unsigned long)getNumRecvDirect(),
|
||||
(int)_mgr->getOutboundCount(0xFFFFFFFF), wifi_rssi, ip,
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bridge ? bridge->getQueueSize() : 0);
|
||||
#else
|
||||
0);
|
||||
#endif
|
||||
if (pos < 0 || pos >= static_cast<int>(buf_size) - 3) return;
|
||||
|
||||
bool first = true;
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
MQTTBridge::SlotStatusSnapshot status;
|
||||
if (!MQTTBridge::getSlotStatusSnapshot(i, &status)) continue;
|
||||
int written;
|
||||
if (status.has_publish_counts) {
|
||||
written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu}",
|
||||
first ? "" : ",", i + 1, status.name, status.state,
|
||||
status.publish_ok, status.publish_err);
|
||||
} else {
|
||||
written = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\"}",
|
||||
first ? "" : ",", i + 1, status.name, status.state);
|
||||
}
|
||||
if (written < 0 || written >= static_cast<int>(buf_size - pos)) break;
|
||||
pos += written;
|
||||
first = false;
|
||||
}
|
||||
#else
|
||||
(void)first;
|
||||
#endif
|
||||
snprintf(buf + pos, buf_size - pos, "]}");
|
||||
}
|
||||
#endif
|
||||
|
||||
void MyMesh::formatStatsReply(char *reply) {
|
||||
StatsFormatHelper::formatCoreStats(reply, board, *_ms, _err_flags, _mgr);
|
||||
}
|
||||
@@ -1125,6 +1347,8 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
|
||||
command += 3;
|
||||
}
|
||||
|
||||
mesh::cli::normalizeCommandVerb(command);
|
||||
|
||||
// handle ACL related commands
|
||||
if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int8}
|
||||
char* hex = &command[8];
|
||||
@@ -1303,6 +1527,16 @@ void MyMesh::loop() {
|
||||
radio_apply_retry_at = futureMillis(nextRadioApplyRetryDelay(radio_apply_failures));
|
||||
}
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (_webconfig) {
|
||||
_webconfig->tick(millis());
|
||||
if (!_webconfig->isRunning() && !_webconfig->isStopping()) {
|
||||
delete _webconfig;
|
||||
_webconfig = nullptr;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// is pending dirty contacts write needed?
|
||||
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
|
||||
acl.save(_fs, MyMesh::saveFilter);
|
||||
|
||||
@@ -30,6 +30,11 @@
|
||||
#define WITH_BRIDGE
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(ESP_PLATFORM) && defined(ADMIN_PASSWORD) && !defined(WEBCONFIG_DISABLED)
|
||||
#include "helpers/esp32/WebConfigServer.h"
|
||||
#endif
|
||||
|
||||
/* ------------------------------ Config -------------------------------- */
|
||||
|
||||
#ifndef FIRMWARE_BUILD_DATE
|
||||
@@ -94,7 +99,11 @@ struct PostInfo {
|
||||
char text[MAX_POST_TEXT_LEN+1];
|
||||
};
|
||||
|
||||
class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
class MyMesh : public mesh::Mesh, public CommonCLICallbacks
|
||||
#ifdef WITH_WEBCONFIG
|
||||
, public WebConfigServer::Callbacks
|
||||
#endif
|
||||
{
|
||||
FILESYSTEM* _fs;
|
||||
uint32_t last_millis;
|
||||
uint64_t uptime_millis;
|
||||
@@ -134,6 +143,12 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
AlertReporter _alerter;
|
||||
#endif
|
||||
#ifdef WITH_WEBCONFIG
|
||||
WebConfigServer* _webconfig = nullptr;
|
||||
bool _wc_batch_active = false;
|
||||
bool _wc_restart_pending = false;
|
||||
uint8_t _wc_slot_restart_mask = 0;
|
||||
#endif
|
||||
|
||||
void addPost(ClientInfo* client, const char* postData);
|
||||
bool applySavedRadioParams();
|
||||
@@ -317,6 +332,12 @@ public:
|
||||
|
||||
void restartBridge() override {
|
||||
if (!bridge || !bridge->isRunning()) return;
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (_wc_batch_active) {
|
||||
_wc_restart_pending = true;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
bridge->end();
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
@@ -334,6 +355,12 @@ public:
|
||||
void restartBridgeSlot(int slot) override {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (!bridge || !bridge->isRunning()) return;
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (_wc_batch_active && slot >= 0 && slot < MAX_MQTT_SLOTS) {
|
||||
_wc_slot_restart_mask |= static_cast<uint8_t>(1U << slot);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
bridge->setSlotPreset(slot, _cli.getObserverPrefs()->mqtt_slot_preset[slot]);
|
||||
#else
|
||||
(void)slot;
|
||||
@@ -359,6 +386,26 @@ public:
|
||||
return bridge->ntpDiag(reply, reply_size, verbose);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
bool startWebConfig(bool force_ap, char* reply) override;
|
||||
bool stopWebConfig(char* reply) override;
|
||||
bool setWebUIEnabled(bool enabled, char* reply) override;
|
||||
bool getWebUIStatus(char* reply) const override;
|
||||
bool isWebConfigActive() const override {
|
||||
return _webconfig && (_webconfig->isRunning() || _webconfig->isStopping());
|
||||
}
|
||||
void getNodeSnapshot(WebConfigServer::NodeSnapshot& snapshot) override;
|
||||
void execCommand(char* cmd, char* reply) override { handleCommand(0, cmd, reply); }
|
||||
void rebootNow() override { _cli.getBoard()->reboot(); }
|
||||
void onConfigBatchStart() override {
|
||||
_wc_batch_active = true;
|
||||
_wc_restart_pending = false;
|
||||
_wc_slot_restart_mask = 0;
|
||||
}
|
||||
void onConfigBatchEnd() override;
|
||||
void buildStatsJson(char* buf, size_t buf_size) override;
|
||||
#endif
|
||||
uint32_t getPowerSaveSleepSeconds(uint32_t max_secs) const;
|
||||
|
||||
// To check if there is pending work
|
||||
|
||||
@@ -6,8 +6,9 @@
|
||||
#define USER_BTN_PRESSED LOW
|
||||
#endif
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <WiFi.h>
|
||||
#include <helpers/esp32/WebConfigServer.h>
|
||||
#endif
|
||||
|
||||
#define AUTO_OFF_MILLIS 20000 // 20 seconds
|
||||
@@ -78,6 +79,38 @@ void UITask::renderCurrScreen() {
|
||||
_display->setCursor((_display->width() - typeWidth) / 2, 48);
|
||||
_display->print(node_type);
|
||||
} else { // home screen
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (WebConfigServer::isRebootPending()) {
|
||||
_display->setTextSize(1);
|
||||
_display->setColor(DisplayDriver::GREEN);
|
||||
_display->setCursor(0, 14);
|
||||
_display->print("Config saved!");
|
||||
_display->setColor(DisplayDriver::LIGHT);
|
||||
_display->setCursor(0, 30);
|
||||
_display->print("Rebooting...");
|
||||
return;
|
||||
}
|
||||
char wc_ssid[33], wc_ip[16];
|
||||
if (WebConfigServer::getSetupInfo(wc_ssid, sizeof(wc_ssid), wc_ip, sizeof(wc_ip))) {
|
||||
_display->setTextSize(1);
|
||||
_display->setColor(DisplayDriver::GREEN);
|
||||
_display->setCursor(0, 0);
|
||||
_display->print("WebUI WiFi Setup");
|
||||
_display->setColor(DisplayDriver::LIGHT);
|
||||
_display->setCursor(0, 14);
|
||||
_display->print("Join WiFi:");
|
||||
_display->setColor(DisplayDriver::YELLOW);
|
||||
_display->setCursor(6, 24);
|
||||
_display->print(wc_ssid);
|
||||
_display->setColor(DisplayDriver::LIGHT);
|
||||
_display->setCursor(0, 40);
|
||||
_display->print("Then browse to:");
|
||||
_display->setColor(DisplayDriver::YELLOW);
|
||||
_display->setCursor(6, 50);
|
||||
_display->print(wc_ip);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
// node name
|
||||
_display->setCursor(0, 0);
|
||||
_display->setTextSize(1);
|
||||
@@ -127,6 +160,13 @@ void UITask::loop() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef WITH_WEBCONFIG
|
||||
if (WebConfigServer::getSetupInfo(nullptr, 0, nullptr, 0)) {
|
||||
if (!_display->isOn()) _display->turnOn();
|
||||
_auto_off = millis() + AUTO_OFF_MILLIS;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (_display->isOn()) {
|
||||
if (millis() >= _next_refresh) {
|
||||
_display->startFrame();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "SensorMesh.h"
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
|
||||
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
|
||||
uint8_t shift = failure_count < 5 ? failure_count : 5;
|
||||
@@ -503,6 +504,8 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r
|
||||
command += 3;
|
||||
}
|
||||
|
||||
mesh::cli::normalizeCommandVerb(command);
|
||||
|
||||
// first, see if this is a custom-handled CLI command (ie. in main.cpp)
|
||||
if (handleCustomCommand(sender_timestamp, command, reply)) {
|
||||
return; // command has been handled
|
||||
|
||||
Reference in new issue
Block a user