| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435 |
- #include "MyMesh.h"
- #include <algorithm>
- #if defined(ESP32) && WITH_WEB_PANEL
- #include <WiFi.h>
- #endif
- #ifndef ARCHIVE_DEBUG
- #if defined(MQTT_DEBUG) && MQTT_DEBUG
- #define ARCHIVE_DEBUG 1
- #else
- #define ARCHIVE_DEBUG 0
- #endif
- #endif
- #if ARCHIVE_DEBUG
- #define ARCHIVE_LOG(fmt, ...) Serial.printf("[ARCHIVE] " fmt "\n", ##__VA_ARGS__)
- #else
- #define ARCHIVE_LOG(...) do { } while (0)
- #endif
- namespace {
- constexpr unsigned long kArchiveNeighboursFlushIntervalMs = 60UL * 1000UL;
- constexpr const char* kArchiveNeighboursSnapshotPath = "/stats/neighbours.snapshot";
- File openArchiveWrite(FILESYSTEM* fs, const char* filename) {
- #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
- fs->remove(filename);
- return fs->open(filename, FILE_O_WRITE);
- #elif defined(RP2040_PLATFORM)
- return fs->open(filename, "w");
- #else
- fs->remove(filename);
- return fs->open(filename, FILE_WRITE);
- #endif
- }
- File openArchiveRead(FILESYSTEM* fs, const char* filename) {
- #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) || defined(RP2040_PLATFORM)
- return fs->open(filename, "r");
- #else
- return fs->open(filename, FILE_READ);
- #endif
- }
- File openArchiveWriteWithRecovery(ArchiveStorage* archive, const char* filename) {
- if (archive == nullptr) {
- return File();
- }
- FILESYSTEM* fs = archive->getFS();
- if (fs == nullptr) {
- return File();
- }
- File file = openArchiveWrite(fs, filename);
- if (file) {
- return file;
- }
- if (!archive->recover()) {
- return File();
- }
- fs = archive->getFS();
- return fs != nullptr ? openArchiveWrite(fs, filename) : File();
- }
- File openArchiveReadWithRecovery(ArchiveStorage* archive, const char* filename) {
- if (archive == nullptr) {
- return File();
- }
- FILESYSTEM* fs = archive->getFS();
- if (fs == nullptr) {
- return File();
- }
- File file = openArchiveRead(fs, filename);
- if (file) {
- return file;
- }
- if (!archive->recover()) {
- return File();
- }
- fs = archive->getFS();
- return fs != nullptr ? openArchiveRead(fs, filename) : File();
- }
- void escapeJsonString(const char* input, char* output, size_t output_size) {
- if (output == nullptr || output_size == 0) {
- return;
- }
- size_t oi = 0;
- for (size_t i = 0; input != nullptr && input[i] != 0 && oi + 1 < output_size; ++i) {
- const char c = input[i];
- const char* escape = nullptr;
- switch (c) {
- case '\\':
- escape = "\\\\";
- break;
- case '"':
- escape = "\\\"";
- break;
- case '\n':
- escape = "\\n";
- break;
- case '\r':
- escape = "\\r";
- break;
- case '\t':
- escape = "\\t";
- break;
- default:
- break;
- }
- if (escape != nullptr) {
- while (*escape != 0 && oi + 1 < output_size) {
- output[oi++] = *escape++;
- }
- } else {
- output[oi++] = c;
- }
- }
- output[oi] = 0;
- }
- } // namespace
- /* ------------------------------ Config -------------------------------- */
- #ifndef LORA_FREQ
- #define LORA_FREQ 915.0
- #endif
- #ifndef LORA_BW
- #define LORA_BW 250
- #endif
- #ifndef LORA_SF
- #define LORA_SF 10
- #endif
- #ifndef LORA_CR
- #define LORA_CR 5
- #endif
- #ifndef LORA_TX_POWER
- #define LORA_TX_POWER 20
- #endif
- #ifndef ADVERT_NAME
- #define ADVERT_NAME "repeater"
- #endif
- #ifndef ADVERT_LAT
- #define ADVERT_LAT 0.0
- #endif
- #ifndef ADVERT_LON
- #define ADVERT_LON 0.0
- #endif
- #ifndef ADMIN_PASSWORD
- #define ADMIN_PASSWORD "password"
- #endif
- #ifndef SERVER_RESPONSE_DELAY
- #define SERVER_RESPONSE_DELAY 300
- #endif
- #ifndef TXT_ACK_DELAY
- #define TXT_ACK_DELAY 200
- #endif
- #define FIRMWARE_VER_LEVEL 2
- #define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
- #define REQ_TYPE_KEEP_ALIVE 0x02
- #define REQ_TYPE_GET_TELEMETRY_DATA 0x03
- #define REQ_TYPE_GET_ACCESS_LIST 0x05
- #define REQ_TYPE_GET_NEIGHBOURS 0x06
- #define REQ_TYPE_GET_OWNER_INFO 0x07 // FIRMWARE_VER_LEVEL >= 2
- #define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
- #define ANON_REQ_TYPE_REGIONS 0x01
- #define ANON_REQ_TYPE_OWNER 0x02
- #define ANON_REQ_TYPE_BASIC 0x03 // just remote clock
- #define CLI_REPLY_DELAY_MILLIS 600
- #define LAZY_CONTACTS_WRITE_DELAY 5000
- void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float snr) {
- #if MAX_NEIGHBOURS // check if neighbours enabled
- // find existing neighbour, else use least recently updated
- uint32_t oldest_timestamp = 0xFFFFFFFF;
- NeighbourInfo *neighbour = &neighbours[0];
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- // if neighbour already known, we should update it
- if (id.matches(neighbours[i].id)) {
- neighbour = &neighbours[i];
- break;
- }
- // otherwise we should update the least recently updated neighbour
- if (neighbours[i].heard_timestamp < oldest_timestamp) {
- neighbour = &neighbours[i];
- oldest_timestamp = neighbour->heard_timestamp;
- }
- }
- // update neighbour info
- neighbour->id = id;
- neighbour->advert_timestamp = timestamp;
- neighbour->heard_timestamp = getRTCClock()->getCurrentTime();
- neighbour->snr = (int8_t)(snr * 4);
- _archive_neighbours_dirty = true;
- #endif
- }
- uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood) {
- ClientInfo* client = NULL;
- if (data[0] == 0) { // blank password, just check if sender is in ACL
- client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
- if (client == NULL) {
- #if MESH_DEBUG
- MESH_DEBUG_PRINTLN("Login, sender not in ACL");
- #endif
- }
- }
- if (client == NULL) {
- uint8_t perms;
- if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password
- perms = PERM_ACL_ADMIN;
- } else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password
- perms = PERM_ACL_GUEST;
- } else {
- #if MESH_DEBUG
- MESH_DEBUG_PRINTLN("Invalid password: %s", data);
- #endif
- return 0;
- }
- client = acl.putClient(sender, 0); // add to contacts (if not already known)
- if (sender_timestamp <= client->last_timestamp) {
- MESH_DEBUG_PRINTLN("Possible login replay attack!");
- return 0; // FATAL: client table is full -OR- replay attack
- }
- MESH_DEBUG_PRINTLN("Login success!");
- client->last_timestamp = sender_timestamp;
- client->last_activity = getRTCClock()->getCurrentTime();
- client->permissions &= ~0x03;
- client->permissions |= perms;
- memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
- if (perms != PERM_ACL_GUEST) { // keep number of FS writes to a minimum
- dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
- }
- }
- if (is_flood) {
- client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path
- }
- uint32_t now = getRTCClock()->getCurrentTimeUnique();
- memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
- reply_data[4] = RESP_SERVER_LOGIN_OK;
- reply_data[5] = 0; // Legacy: was recommended keep-alive interval (secs / 16)
- reply_data[6] = client->isAdmin() ? 1 : 0;
- reply_data[7] = client->permissions;
- getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
- reply_data[12] = FIRMWARE_VER_LEVEL; // New field
- return 13; // reply length
- }
- uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
- if (anon_limiter.allow(rtc_clock.getCurrentTime())) {
- // request data has: {reply-path-len}{reply-path}
- reply_path_len = *data & 63;
- reply_path_hash_size = (*data >> 6) + 1;
- data++;
- memcpy(reply_path, data, ((uint8_t)reply_path_len) * reply_path_hash_size);
- // data += (uint8_t)reply_path_len * reply_path_hash_size;
- memcpy(reply_data, &sender_timestamp, 4); // prefix with sender_timestamp, like a tag
- uint32_t now = getRTCClock()->getCurrentTime();
- memcpy(&reply_data[4], &now, 4); // include our clock (for easy clock sync, and packet hash uniqueness)
- return 8 + region_map.exportNamesTo((char *) &reply_data[8], sizeof(reply_data) - 12, REGION_DENY_FLOOD); // reply length
- }
- return 0;
- }
- uint8_t MyMesh::handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
- if (anon_limiter.allow(rtc_clock.getCurrentTime())) {
- // request data has: {reply-path-len}{reply-path}
- reply_path_len = *data & 63;
- reply_path_hash_size = (*data >> 6) + 1;
- data++;
- memcpy(reply_path, data, ((uint8_t)reply_path_len) * reply_path_hash_size);
- // data += (uint8_t)reply_path_len * reply_path_hash_size;
- memcpy(reply_data, &sender_timestamp, 4); // prefix with sender_timestamp, like a tag
- uint32_t now = getRTCClock()->getCurrentTime();
- memcpy(&reply_data[4], &now, 4); // include our clock (for easy clock sync, and packet hash uniqueness)
- sprintf((char *) &reply_data[8], "%s\n%s", _prefs.node_name, _prefs.owner_info);
- return 8 + strlen((char *) &reply_data[8]); // reply length
- }
- return 0;
- }
- uint8_t MyMesh::handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data) {
- if (anon_limiter.allow(rtc_clock.getCurrentTime())) {
- // request data has: {reply-path-len}{reply-path}
- reply_path_len = *data & 63;
- reply_path_hash_size = (*data >> 6) + 1;
- data++;
- memcpy(reply_path, data, ((uint8_t)reply_path_len) * reply_path_hash_size);
- // data += (uint8_t)reply_path_len * reply_path_hash_size;
- memcpy(reply_data, &sender_timestamp, 4); // prefix with sender_timestamp, like a tag
- uint32_t now = getRTCClock()->getCurrentTime();
- memcpy(&reply_data[4], &now, 4); // include our clock (for easy clock sync, and packet hash uniqueness)
- reply_data[8] = 0; // features
- #ifdef WITH_RS232_BRIDGE
- reply_data[8] |= 0x01; // is bridge, type UART
- #elif WITH_ESPNOW_BRIDGE
- reply_data[8] |= 0x03; // is bridge, type ESP-NOW
- #endif
- if (_prefs.disable_fwd) { // is this repeater currently disabled
- reply_data[8] |= 0x80; // is disabled
- }
- // TODO: add some kind of moving-window utilisation metric, so can query 'how busy' is this repeater
- return 9; // reply length
- }
- return 0;
- }
- int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t *payload, size_t payload_len) {
- // uint32_t now = getRTCClock()->getCurrentTimeUnique();
- // memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
- memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
- if (payload[0] == REQ_TYPE_GET_STATUS) { // guests can also access this now
- RepeaterStats stats;
- stats.batt_milli_volts = board.getBattMilliVolts();
- stats.curr_tx_queue_len = _mgr->getOutboundTotal();
- stats.noise_floor = (int16_t)_radio->getNoiseFloor();
- stats.last_rssi = (int16_t)radio_driver.getLastRSSI();
- stats.n_packets_recv = radio_driver.getPacketsRecv();
- stats.n_packets_sent = radio_driver.getPacketsSent();
- stats.total_air_time_secs = getTotalAirTime() / 1000;
- stats.total_up_time_secs = uptime_millis / 1000;
- stats.n_sent_flood = getNumSentFlood();
- stats.n_sent_direct = getNumSentDirect();
- stats.n_recv_flood = getNumRecvFlood();
- stats.n_recv_direct = getNumRecvDirect();
- stats.err_events = _err_flags;
- stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4);
- stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
- stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
- stats.total_rx_air_time_secs = getReceiveAirTime() / 1000;
- stats.n_recv_errors = radio_driver.getPacketsRecvErrors();
- memcpy(&reply_data[4], &stats, sizeof(stats));
- return 4 + sizeof(stats); // reply_len
- }
- if (payload[0] == REQ_TYPE_GET_TELEMETRY_DATA) {
- uint8_t perm_mask = ~(payload[1]); // NEW: first reserved byte (of 4), is now inverse mask to apply to permissions
- telemetry.reset();
- telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
- // query other sensors -- target specific
- if ((sender->permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) {
- perm_mask = 0x00; // just base telemetry allowed
- }
- sensors.querySensors(perm_mask, telemetry);
- // This default temperature will be overridden by external sensors (if any)
- float temperature = board.getMCUTemperature();
- if(!isnan(temperature)) { // Supported boards with built-in temperature sensor. ESP32-C3 may return NAN
- telemetry.addTemperature(TELEM_CHANNEL_SELF, temperature); // Built-in MCU Temperature
- }
- uint8_t tlen = telemetry.getSize();
- memcpy(&reply_data[4], telemetry.getBuffer(), tlen);
- return 4 + tlen; // reply_len
- }
- if (payload[0] == REQ_TYPE_GET_ACCESS_LIST && sender->isAdmin()) {
- uint8_t res1 = payload[1]; // reserved for future (extra query params)
- uint8_t res2 = payload[2];
- if (res1 == 0 && res2 == 0) {
- uint8_t ofs = 4;
- for (int i = 0; i < acl.getNumClients() && ofs + 7 <= sizeof(reply_data) - 4; i++) {
- auto c = acl.getClientByIdx(i);
- if (c->permissions == 0) continue; // skip deleted entries
- memcpy(&reply_data[ofs], c->id.pub_key, 6); ofs += 6; // just 6-byte pub_key prefix
- reply_data[ofs++] = c->permissions;
- }
- return ofs;
- }
- }
- if (payload[0] == REQ_TYPE_GET_NEIGHBOURS) {
- uint8_t request_version = payload[1];
- if (request_version == 0) {
- // reply data offset (after response sender_timestamp/tag)
- int reply_offset = 4;
- // get request params
- uint8_t count = payload[2]; // how many neighbours to fetch (0-255)
- uint16_t offset;
- memcpy(&offset, &payload[3], 2); // offset from start of neighbours list (0-65535)
- uint8_t order_by = payload[5]; // how to order neighbours. 0=newest_to_oldest, 1=oldest_to_newest, 2=strongest_to_weakest, 3=weakest_to_strongest
- uint8_t pubkey_prefix_length = payload[6]; // how many bytes of neighbour pub key we want
- // we also send a 4 byte random blob in payload[7...10] to help packet uniqueness
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS count=%d, offset=%d, order_by=%d, pubkey_prefix_length=%d", count, offset, order_by, pubkey_prefix_length);
- // clamp pub key prefix length to max pub key length
- if(pubkey_prefix_length > PUB_KEY_SIZE){
- pubkey_prefix_length = PUB_KEY_SIZE;
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS invalid pubkey_prefix_length=%d clamping to %d", pubkey_prefix_length, PUB_KEY_SIZE);
- }
- // create copy of neighbours list, skipping empty entries so we can sort it separately from main list
- int16_t neighbours_count = 0;
- #if MAX_NEIGHBOURS
- NeighbourInfo* sorted_neighbours[MAX_NEIGHBOURS];
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- auto neighbour = &neighbours[i];
- if (neighbour->heard_timestamp > 0) {
- sorted_neighbours[neighbours_count] = neighbour;
- neighbours_count++;
- }
- }
- // sort neighbours based on order
- if (order_by == 0) {
- // sort by newest to oldest
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS sorting newest to oldest");
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->heard_timestamp > b->heard_timestamp; // desc
- });
- } else if (order_by == 1) {
- // sort by oldest to newest
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS sorting oldest to newest");
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->heard_timestamp < b->heard_timestamp; // asc
- });
- } else if (order_by == 2) {
- // sort by strongest to weakest
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS sorting strongest to weakest");
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->snr > b->snr; // desc
- });
- } else if (order_by == 3) {
- // sort by weakest to strongest
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS sorting weakest to strongest");
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->snr < b->snr; // asc
- });
- }
- #endif
- // build results buffer
- int results_count = 0;
- int results_offset = 0;
- uint8_t results_buffer[130];
- for(int index = 0; index < count && index + offset < neighbours_count; index++){
-
- // stop if we can't fit another entry in results
- int entry_size = pubkey_prefix_length + 4 + 1;
- if(results_offset + entry_size > sizeof(results_buffer)){
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS no more entries can fit in results buffer");
- break;
- }
- #if MAX_NEIGHBOURS
- // add next neighbour to results
- auto neighbour = sorted_neighbours[index + offset];
- uint32_t heard_seconds_ago = getRTCClock()->getCurrentTime() - neighbour->heard_timestamp;
- memcpy(&results_buffer[results_offset], neighbour->id.pub_key, pubkey_prefix_length); results_offset += pubkey_prefix_length;
- memcpy(&results_buffer[results_offset], &heard_seconds_ago, 4); results_offset += 4;
- memcpy(&results_buffer[results_offset], &neighbour->snr, 1); results_offset += 1;
- results_count++;
- #endif
- }
- // build reply
- MESH_DEBUG_PRINTLN("REQ_TYPE_GET_NEIGHBOURS neighbours_count=%d results_count=%d", neighbours_count, results_count);
- memcpy(&reply_data[reply_offset], &neighbours_count, 2); reply_offset += 2;
- memcpy(&reply_data[reply_offset], &results_count, 2); reply_offset += 2;
- memcpy(&reply_data[reply_offset], &results_buffer, results_offset); reply_offset += results_offset;
- return reply_offset;
- }
- } else if (payload[0] == REQ_TYPE_GET_OWNER_INFO) {
- sprintf((char *) &reply_data[4], "%s\n%s\n%s", FIRMWARE_VERSION, _prefs.node_name, _prefs.owner_info);
- return 4 + strlen((char *) &reply_data[4]);
- }
- return 0; // unknown command
- }
- mesh::Packet *MyMesh::createSelfAdvert() {
- uint8_t app_data[MAX_ADVERT_DATA_SIZE];
- uint8_t app_data_len = _cli.buildAdvertData(ADV_TYPE_REPEATER, app_data);
- return createAdvert(self_id, app_data, app_data_len);
- }
- File MyMesh::openAppend(const char *fname) {
- #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
- return _fs->open(fname, FILE_O_WRITE);
- #elif defined(RP2040_PLATFORM)
- return _fs->open(fname, "a");
- #else
- return _fs->open(fname, "a", true);
- #endif
- }
- static uint8_t max_loop_minimal[] = { 0, /* 1-byte */ 4, /* 2-byte */ 2, /* 3-byte */ 1 };
- static uint8_t max_loop_moderate[] = { 0, /* 1-byte */ 2, /* 2-byte */ 1, /* 3-byte */ 1 };
- static uint8_t max_loop_strict[] = { 0, /* 1-byte */ 1, /* 2-byte */ 1, /* 3-byte */ 1 };
- bool MyMesh::isLooped(const mesh::Packet* packet, const uint8_t max_counters[]) {
- uint8_t hash_size = packet->getPathHashSize();
- uint8_t hash_count = packet->getPathHashCount();
- uint8_t n = 0;
- const uint8_t* path = packet->path;
- while (hash_count > 0) { // count how many times this node is already in the path
- if (self_id.isHashMatch(path, hash_size)) n++;
- hash_count--;
- path += hash_size;
- }
- return n >= max_counters[hash_size];
- }
- void MyMesh::sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, uint8_t path_hash_size) {
- if (recv_pkt_region && !recv_pkt_region->isWildcard()) { // if _request_ packet scope is known, send reply with same scope
- TransportKey scope;
- if (region_map.getTransportKeysFor(*recv_pkt_region, &scope, 1) > 0) {
- sendFloodScoped(scope, packet, delay_millis, path_hash_size);
- } else {
- sendFlood(packet, delay_millis, path_hash_size); // send un-scoped
- }
- } else {
- sendFlood(packet, delay_millis, path_hash_size); // send un-scoped
- }
- }
- bool MyMesh::allowPacketForward(const mesh::Packet *packet) {
- if (_prefs.disable_fwd) return false;
- if (packet->isRouteFlood() && packet->getPathHashCount() >= _prefs.flood_max) return false;
- if (packet->isRouteFlood() && recv_pkt_region == NULL) {
- MESH_DEBUG_PRINTLN("allowPacketForward: unknown transport code, or wildcard not allowed for FLOOD packet");
- return false;
- }
- if (packet->isRouteFlood() && _prefs.loop_detect != LOOP_DETECT_OFF) {
- const uint8_t* maximums;
- if (_prefs.loop_detect == LOOP_DETECT_MINIMAL) {
- maximums = max_loop_minimal;
- } else if (_prefs.loop_detect == LOOP_DETECT_MODERATE) {
- maximums = max_loop_moderate;
- } else {
- maximums = max_loop_strict;
- }
- if (isLooped(packet, maximums)) {
- MESH_DEBUG_PRINTLN("allowPacketForward: FLOOD packet loop detected!");
- return false;
- }
- }
- return true;
- }
- const char *MyMesh::getLogDateTime() {
- static char tmp[32];
- uint32_t now = getRTCClock()->getCurrentTime();
- DateTime dt = DateTime(now);
- sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(),
- dt.year());
- return tmp;
- }
- void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
- #if MESH_PACKET_LOGGING
- Serial.print(getLogDateTime());
- Serial.print(" RAW: ");
- mesh::Utils::printHex(Serial, raw, len);
- Serial.println();
- #endif
- }
- void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
- #ifdef WITH_BRIDGE
- if (_prefs.bridge_pkt_src == 1) {
- bridge.sendPacket(pkt);
- }
- #endif
- #ifdef WITH_MQTT_UPLINK
- mqtt.publishPacket(*pkt, false, (int)_radio->getLastRSSI(), _radio->getLastSNR(), (int)(score * 1000),
- (int)_radio->getEstAirtimeFor(len));
- #endif
- if (_logging) {
- File f = openAppend(PACKET_LOG_FILE);
- if (f) {
- f.print(getLogDateTime());
- f.printf(": RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d", len,
- pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len,
- (int)_radio->getLastSNR(), (int)_radio->getLastRSSI(), (int)(score * 1000));
- if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ ||
- pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
- f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
- } else {
- f.printf("\n");
- }
- f.close();
- }
- }
- }
- void MyMesh::logTx(mesh::Packet *pkt, int len) {
- #ifdef WITH_BRIDGE
- if (_prefs.bridge_pkt_src == 0) {
- bridge.sendPacket(pkt);
- }
- #endif
- #ifdef WITH_MQTT_UPLINK
- mqtt.publishPacket(*pkt, true, (int)_radio->getLastRSSI(), _radio->getLastSNR());
- #endif
- if (_logging) {
- File f = openAppend(PACKET_LOG_FILE);
- if (f) {
- f.print(getLogDateTime());
- f.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d)", len, pkt->getPayloadType(),
- pkt->isRouteDirect() ? "D" : "F", pkt->payload_len);
- if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ ||
- pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
- f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
- } else {
- f.printf("\n");
- }
- f.close();
- }
- }
- }
- void MyMesh::logTxFail(mesh::Packet *pkt, int len) {
- if (_logging) {
- File f = openAppend(PACKET_LOG_FILE);
- if (f) {
- f.print(getLogDateTime());
- f.printf(": TX FAIL!, len=%d (type=%d, route=%s, payload_len=%d)\n", len, pkt->getPayloadType(),
- pkt->isRouteDirect() ? "D" : "F", pkt->payload_len);
- f.close();
- }
- }
- }
- int MyMesh::calcRxDelay(float score, uint32_t air_time) const {
- if (_prefs.rx_delay_base <= 0.0f) return 0;
- return (int)((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time);
- }
- uint32_t MyMesh::getRetransmitDelay(const mesh::Packet *packet) {
- uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.tx_delay_factor);
- return getRNG()->nextInt(0, 5*t + 1);
- }
- uint32_t MyMesh::getDirectRetransmitDelay(const mesh::Packet *packet) {
- uint32_t t = (_radio->getEstAirtimeFor(packet->getPathByteLen() + packet->payload_len + 2) * _prefs.direct_tx_delay_factor);
- return getRNG()->nextInt(0, 5*t + 1);
- }
- bool MyMesh::filterRecvFloodPacket(mesh::Packet* pkt) {
- // just try to determine region for packet (apply later in allowPacketForward())
- if (pkt->getRouteType() == ROUTE_TYPE_TRANSPORT_FLOOD) {
- recv_pkt_region = region_map.findMatch(pkt, REGION_DENY_FLOOD);
- } else if (pkt->getRouteType() == ROUTE_TYPE_FLOOD) {
- if (region_map.getWildcard().flags & REGION_DENY_FLOOD) {
- recv_pkt_region = NULL;
- } else {
- recv_pkt_region = ®ion_map.getWildcard();
- }
- } else {
- recv_pkt_region = NULL;
- }
- // do normal processing
- return false;
- }
- void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const mesh::Identity &sender,
- uint8_t *data, size_t len) {
- if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
- // client (unknown at this stage)
- uint32_t timestamp;
- memcpy(×tamp, data, 4);
- data[len] = 0; // ensure null terminator
- uint8_t reply_len;
- reply_path_len = -1;
- if (data[4] == 0 || data[4] >= ' ') { // is password, ie. a login request
- reply_len = handleLoginReq(sender, secret, timestamp, &data[4], packet->isRouteFlood());
- } else if (data[4] == ANON_REQ_TYPE_REGIONS && packet->isRouteDirect()) {
- reply_len = handleAnonRegionsReq(sender, timestamp, &data[5]);
- } else if (data[4] == ANON_REQ_TYPE_OWNER && packet->isRouteDirect()) {
- reply_len = handleAnonOwnerReq(sender, timestamp, &data[5]);
- } else if (data[4] == ANON_REQ_TYPE_BASIC && packet->isRouteDirect()) {
- reply_len = handleAnonClockReq(sender, timestamp, &data[5]);
- } else {
- reply_len = 0; // unknown/invalid request type
- }
- if (reply_len == 0) return; // invalid request
- if (packet->isRouteFlood()) {
- // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
- mesh::Packet* path = createPathReturn(sender, secret, packet->path, packet->path_len,
- PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
- if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
- } else if (reply_path_len < 0) {
- mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
- if (reply) sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
- } else {
- mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len);
- uint8_t path_len = ((reply_path_hash_size - 1) << 6) | (reply_path_len & 63);
- if (reply) sendDirect(reply, reply_path, path_len, SERVER_RESPONSE_DELAY);
- }
- }
- }
- int MyMesh::searchPeersByHash(const uint8_t *hash) {
- int n = 0;
- for (int i = 0; i < acl.getNumClients(); i++) {
- if (acl.getClientByIdx(i)->id.isHashMatch(hash)) {
- matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
- }
- }
- return n;
- }
- void MyMesh::getPeerSharedSecret(uint8_t *dest_secret, int peer_idx) {
- int i = matching_peer_indexes[peer_idx];
- if (i >= 0 && i < acl.getNumClients()) {
- // lookup pre-calculated shared_secret
- memcpy(dest_secret, acl.getClientByIdx(i)->shared_secret, PUB_KEY_SIZE);
- } else {
- MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i);
- }
- }
- static bool isShare(const mesh::Packet *packet) {
- if (packet->hasTransportCodes()) {
- return packet->transport_codes[0] == 0 && packet->transport_codes[1] == 0; // codes { 0, 0 } means 'send to nowhere'
- }
- return false;
- }
- void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32_t timestamp,
- const uint8_t *app_data, size_t app_data_len) {
- mesh::Mesh::onAdvertRecv(packet, id, timestamp, app_data, app_data_len); // chain to super impl
- // if this a zero hop advert (and not via 'Share'), add it to neighbours
- if (packet->path_len == 0 && !isShare(packet)) {
- AdvertDataParser parser(app_data, app_data_len);
- if (parser.isValid() && parser.getType() == ADV_TYPE_REPEATER) { // just keep neigbouring Repeaters
- putNeighbour(id, timestamp, packet->getSNR());
- }
- }
- }
- void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret,
- uint8_t *data, size_t len) {
- int i = matching_peer_indexes[sender_idx];
- if (i < 0 || i >= acl.getNumClients()) { // get from our known_clients table (sender SHOULD already be known in this context)
- MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid peer idx: %d", i);
- return;
- }
- ClientInfo* client = acl.getClientByIdx(i);
- if (type == PAYLOAD_TYPE_REQ) { // request (from a Known admin client!)
- uint32_t timestamp;
- memcpy(×tamp, data, 4);
- if (timestamp > client->last_timestamp) { // prevent replay attacks
- int reply_len = handleRequest(client, timestamp, &data[4], len - 4);
- if (reply_len == 0) return; // invalid command
- client->last_timestamp = timestamp;
- client->last_activity = getRTCClock()->getCurrentTime();
- if (packet->isRouteFlood()) {
- // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
- mesh::Packet *path = createPathReturn(client->id, secret, packet->path, packet->path_len,
- PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
- if (path) sendFloodReply(path, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
- } else {
- mesh::Packet *reply =
- createDatagram(PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len);
- if (reply) {
- if (client->out_path_len != OUT_PATH_UNKNOWN) { // we have an out_path, so send DIRECT
- sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
- } else {
- sendFloodReply(reply, SERVER_RESPONSE_DELAY, packet->getPathHashSize());
- }
- }
- }
- } else {
- MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
- }
- } else if (type == PAYLOAD_TYPE_TXT_MSG && len > 5 && client->isAdmin()) { // a CLI command
- uint32_t sender_timestamp;
- memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
- uint8_t flags = (data[4] >> 2); // message attempt number, and other flags
- if (!(flags == TXT_TYPE_PLAIN || flags == TXT_TYPE_CLI_DATA)) {
- MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags);
- } else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks
- bool is_retry = (sender_timestamp == client->last_timestamp);
- client->last_timestamp = sender_timestamp;
- client->last_activity = getRTCClock()->getCurrentTime();
- // len can be > original length, but 'text' will be padded with zeroes
- data[len] = 0; // need to make a C string again, with null terminator
- if (flags == TXT_TYPE_PLAIN) { // for legacy CLI, send Acks
- uint32_t ack_hash; // calc truncated hash of the message timestamp + text + sender pub_key, to prove
- // to sender that we got it
- mesh::Utils::sha256((uint8_t *)&ack_hash, 4, data, 5 + strlen((char *)&data[5]), client->id.pub_key,
- PUB_KEY_SIZE);
- mesh::Packet *ack = createAck(ack_hash);
- if (ack) {
- if (client->out_path_len == OUT_PATH_UNKNOWN) {
- sendFloodReply(ack, TXT_ACK_DELAY, packet->getPathHashSize());
- } else {
- sendDirect(ack, client->out_path, client->out_path_len, TXT_ACK_DELAY);
- }
- }
- }
- uint8_t temp[166];
- char *command = (char *)&data[5];
- char *reply = (char *)&temp[5];
- if (is_retry) {
- *reply = 0;
- } else {
- handleCommand(sender_timestamp, command, reply);
- }
- int text_len = strlen(reply);
- if (text_len > 0) {
- uint32_t timestamp = getRTCClock()->getCurrentTimeUnique();
- if (timestamp == sender_timestamp) {
- // WORKAROUND: the two timestamps need to be different, in the CLI view
- timestamp++;
- }
- memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique
- temp[4] = (TXT_TYPE_CLI_DATA << 2); // NOTE: legacy was: TXT_TYPE_PLAIN
- auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len);
- if (reply) {
- if (client->out_path_len == OUT_PATH_UNKNOWN) {
- sendFloodReply(reply, CLI_REPLY_DELAY_MILLIS, packet->getPathHashSize());
- } else {
- sendDirect(reply, client->out_path, client->out_path_len, CLI_REPLY_DELAY_MILLIS);
- }
- }
- }
- } else {
- MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
- }
- }
- }
- bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t *secret, uint8_t *path,
- uint8_t path_len, uint8_t extra_type, uint8_t *extra, uint8_t extra_len) {
- // TODO: prevent replay attacks
- int i = matching_peer_indexes[sender_idx];
- if (i >= 0 && i < acl.getNumClients()) { // get from our known_clients table (sender SHOULD already be known in this context)
- MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len);
- auto client = acl.getClientByIdx(i);
- // store a copy of path, for sendDirect()
- client->out_path_len = mesh::Packet::copyPath(client->out_path, path, path_len);
- client->last_activity = getRTCClock()->getCurrentTime();
- } else {
- MESH_DEBUG_PRINTLN("onPeerPathRecv: invalid peer idx: %d", i);
- }
- // NOTE: no reciprocal path send!!
- return false;
- }
- #define CTL_TYPE_NODE_DISCOVER_REQ 0x80
- #define CTL_TYPE_NODE_DISCOVER_RESP 0x90
- void MyMesh::onControlDataRecv(mesh::Packet* packet) {
- uint8_t type = packet->payload[0] & 0xF0; // just test upper 4 bits
- if (type == CTL_TYPE_NODE_DISCOVER_REQ && packet->payload_len >= 6
- && !_prefs.disable_fwd && discover_limiter.allow(rtc_clock.getCurrentTime())
- ) {
- int i = 1;
- uint8_t filter = packet->payload[i++];
- uint32_t tag;
- memcpy(&tag, &packet->payload[i], 4); i += 4;
- uint32_t since;
- if (packet->payload_len >= i+4) { // optional since field
- memcpy(&since, &packet->payload[i], 4); i += 4;
- } else {
- since = 0;
- }
- if ((filter & (1 << ADV_TYPE_REPEATER)) != 0 && _prefs.discovery_mod_timestamp >= since) {
- bool prefix_only = packet->payload[0] & 1;
- uint8_t data[6 + PUB_KEY_SIZE];
- data[0] = CTL_TYPE_NODE_DISCOVER_RESP | ADV_TYPE_REPEATER; // low 4-bits for node type
- data[1] = packet->_snr; // let sender know the inbound SNR ( x 4)
- memcpy(&data[2], &tag, 4); // include tag from request, for client to match to
- memcpy(&data[6], self_id.pub_key, PUB_KEY_SIZE);
- auto resp = createControlData(data, prefix_only ? 6 + 8 : 6 + PUB_KEY_SIZE);
- if (resp) {
- sendZeroHop(resp, getRetransmitDelay(resp)*4); // apply random delay (widened x4), as multiple nodes can respond to this
- }
- }
- } else if (type == CTL_TYPE_NODE_DISCOVER_RESP && packet->payload_len >= 6) {
- uint8_t node_type = packet->payload[0] & 0x0F;
- if (node_type != ADV_TYPE_REPEATER) {
- return;
- }
- if (packet->payload_len < 6 + PUB_KEY_SIZE) {
- MESH_DEBUG_PRINTLN("onControlDataRecv: DISCOVER_RESP pubkey too short: %d", (uint32_t)packet->payload_len);
- return;
- }
- if (pending_discover_tag == 0 || millisHasNowPassed(pending_discover_until)) {
- pending_discover_tag = 0;
- return;
- }
- uint32_t tag;
- memcpy(&tag, &packet->payload[2], 4);
- if (tag != pending_discover_tag) {
- return;
- }
- mesh::Identity id(&packet->payload[6]);
- if (id.matches(self_id)) {
- return;
- }
- putNeighbour(id, rtc_clock.getCurrentTime(), packet->getSNR());
- }
- }
- void MyMesh::sendNodeDiscoverReq() {
- uint8_t data[10];
- data[0] = CTL_TYPE_NODE_DISCOVER_REQ; // prefix_only=0
- data[1] = (1 << ADV_TYPE_REPEATER);
- getRNG()->random(&data[2], 4); // tag
- memcpy(&pending_discover_tag, &data[2], 4);
- pending_discover_until = futureMillis(60000);
- uint32_t since = 0;
- memcpy(&data[6], &since, 4);
- auto pkt = createControlData(data, sizeof(data));
- if (pkt) {
- sendZeroHop(pkt);
- }
- }
- MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondClock &ms, mesh::RNG &rng,
- mesh::RTCClock &rtc, mesh::MeshTables &tables)
- : mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
- region_map(key_store), temp_map(key_store),
- _cli(board, rtc, sensors, region_map, acl, &_prefs, this),
- telemetry(MAX_PACKET_PAYLOAD - 4),
- discover_limiter(4, 120), // max 4 every 2 minutes
- anon_limiter(4, 180) // max 4 every 3 minutes
- #if defined(WITH_RS232_BRIDGE)
- , bridge(&_prefs, WITH_RS232_BRIDGE, _mgr, &rtc)
- #endif
- #if defined(WITH_ESPNOW_BRIDGE)
- , bridge(&_prefs, _mgr, &rtc)
- #endif
- #if defined(WITH_MQTT_UPLINK)
- , mqtt(rtc, self_id)
- #endif
- {
- last_millis = 0;
- _archive = nullptr;
- uptime_millis = 0;
- next_archive_neighbours_flush_ms = 0;
- next_history_sample_ms = 0;
- next_local_advert = next_flood_advert = 0;
- dirty_contacts_expiry = 0;
- set_radio_at = revert_radio_at = 0;
- _logging = false;
- _archive_neighbours_dirty = false;
- region_load_active = false;
- memset(&_stats_state, 0, sizeof(_stats_state));
- #if MAX_NEIGHBOURS
- memset(neighbours, 0, sizeof(neighbours));
- #endif
- // defaults
- memset(&_prefs, 0, sizeof(_prefs));
- _prefs.airtime_factor = 1.0;
- _prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
- _prefs.tx_delay_factor = 0.5f; // was 0.25f
- _prefs.direct_tx_delay_factor = 0.3f; // was 0.2
- StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name));
- _prefs.node_lat = ADVERT_LAT;
- _prefs.node_lon = ADVERT_LON;
- StrHelper::strncpy(_prefs.password, ADMIN_PASSWORD, sizeof(_prefs.password));
- _prefs.freq = LORA_FREQ;
- _prefs.sf = LORA_SF;
- _prefs.bw = LORA_BW;
- _prefs.cr = LORA_CR;
- _prefs.tx_power_dbm = LORA_TX_POWER;
- _prefs.advert_interval = 1; // default to 2 minutes for NEW installs
- _prefs.flood_advert_interval = 12; // 12 hours
- _prefs.flood_max = 64;
- _prefs.interference_threshold = 0; // disabled
- // bridge defaults
- _prefs.bridge_enabled = 1; // enabled
- _prefs.bridge_delay = 500; // milliseconds
- _prefs.bridge_pkt_src = 0; // logTx
- _prefs.bridge_baud = 115200; // baud rate
- _prefs.bridge_channel = 1; // channel 1
- StrHelper::strncpy(_prefs.bridge_secret, "LVSITANOS", sizeof(_prefs.bridge_secret));
- // GPS defaults
- _prefs.gps_enabled = 0;
- _prefs.gps_interval = 0;
- _prefs.advert_loc_policy = ADVERT_LOC_PREFS;
- _prefs.adc_multiplier = 0.0f; // 0.0f means use default board multiplier
- _prefs.battery_reporting_enabled = 1;
- #if defined(USE_SX1262) || defined(USE_SX1268)
- #ifdef SX126X_RX_BOOSTED_GAIN
- _prefs.rx_boosted_gain = SX126X_RX_BOOSTED_GAIN;
- #else
- _prefs.rx_boosted_gain = 1; // enabled by default;
- #endif
- #endif
- pending_discover_tag = 0;
- pending_discover_until = 0;
- memset(default_scope.key, 0, sizeof(default_scope.key));
- }
- void MyMesh::begin(FILESYSTEM *fs, ArchiveStorage* archive) {
- mesh::Mesh::begin();
- _fs = fs;
- _archive = archive;
- last_millis = millis();
- // load persisted prefs
- _cli.loadPrefs(_fs);
- acl.load(_fs, self_id);
- // TODO: key_store.begin();
- region_map.load(_fs);
- // establish default-scope
- {
- RegionEntry* r = region_map.getDefaultRegion();
- if (r) {
- region_map.getTransportKeysFor(*r, &default_scope, 1);
- } else {
- #ifdef DEFAULT_FLOOD_SCOPE_NAME
- r = region_map.findByName(DEFAULT_FLOOD_SCOPE_NAME);
- if (r == NULL) {
- r = region_map.putRegion(DEFAULT_FLOOD_SCOPE_NAME, 0); // auto-create the default scope region
- if (r) { r->flags = 0; } // Allow-flood
- }
- if (r) {
- region_map.setDefaultRegion(r);
- region_map.getTransportKeysFor(*r, &default_scope, 1);
- }
- #endif
- }
- }
- #if defined(WITH_BRIDGE)
- if (_prefs.bridge_enabled) {
- bridge.begin();
- }
- #endif
- #if defined(ESP_PLATFORM)
- uint8_t legacy_wifi_powersave = 0;
- const char* legacy_wifi_ssid = nullptr;
- const char* legacy_wifi_pwd = nullptr;
- #ifdef WITH_MQTT_UPLINK
- MQTTPrefs legacy_mqtt_prefs{};
- MQTTPrefsStore::setDefaults(legacy_mqtt_prefs);
- MQTTPrefsStore::load(_fs, legacy_mqtt_prefs);
- legacy_wifi_powersave = legacy_mqtt_prefs.legacy_wifi_powersave;
- legacy_wifi_ssid = legacy_mqtt_prefs.legacy_wifi_ssid;
- legacy_wifi_pwd = legacy_mqtt_prefs.legacy_wifi_pwd;
- #endif
- network.begin(_fs, legacy_wifi_powersave, legacy_wifi_ssid, legacy_wifi_pwd);
- #endif
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- board.setInhibitSleep(true);
- web.setCommandRunner(this);
- web.setNetworkStateProvider(&network);
- web.begin(_fs);
- _stats_history.begin(web.isWebStatsEnabled(), _archive);
- if (web.isWebStatsEnabled() && !_stats_history.isLiveOnly() && _archive != nullptr && _archive->isMounted()) {
- restoreArchiveNeighbours();
- next_archive_neighbours_flush_ms = millis() + kArchiveNeighboursFlushIntervalMs;
- }
- if (web.isWebStatsEnabled()) {
- recordStatsEvent(HISTORY_EVENT_BOOT);
- if (_archive != nullptr) {
- recordStatsEvent(_archive->isMounted() ? HISTORY_EVENT_ARCHIVE_MOUNTED : HISTORY_EVENT_ARCHIVE_UNAVAILABLE);
- }
- }
- #endif
- #if defined(WITH_MQTT_UPLINK) && !(defined(ESP_PLATFORM) && WITH_WEB_PANEL)
- board.setInhibitSleep(true);
- #endif
- #ifdef WITH_MQTT_UPLINK
- mqtt.setNodeNameSource(_prefs.node_name);
- #if defined(ESP_PLATFORM)
- mqtt.setNetworkStateProvider(&network);
- #endif
- mqtt.begin(_fs);
- #endif
- radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
- radio_set_tx_power(_prefs.tx_power_dbm);
- radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain);
- MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s",
- radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
- updateAdvertTimer();
- updateFloodAdvertTimer();
- board.setAdcMultiplier(_prefs.adc_multiplier);
- board.setBatteryReporting(_prefs.battery_reporting_enabled);
- #if ENV_INCLUDE_GPS == 1
- applyGpsPrefs();
- #endif
- next_history_sample_ms = futureMillis(1000);
- }
- void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
- if (scope.isNull()) {
- sendFlood(pkt, delay_millis, path_hash_size);
- } else {
- uint16_t codes[2];
- codes[0] = scope.calcTransportCode(pkt);
- codes[1] = 0; // REVISIT: set to 'home' Region, for sender/return region?
- sendFlood(pkt, codes, delay_millis, path_hash_size);
- }
- }
- void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) {
- set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
- pending_freq = freq;
- pending_bw = bw;
- pending_sf = sf;
- pending_cr = cr;
- revert_radio_at = futureMillis(2000 + timeout_mins * 60 * 1000); // schedule when to revert radio params
- }
- bool MyMesh::formatFileSystem() {
- #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
- return InternalFS.format();
- #elif defined(RP2040_PLATFORM)
- return LittleFS.format();
- #elif defined(ESP32)
- return SPIFFS.format();
- #else
- #error "need to implement file system erase"
- return false;
- #endif
- }
- void MyMesh::sendSelfAdvertisement(int delay_millis, bool flood) {
- mesh::Packet *pkt = createSelfAdvert();
- if (pkt) {
- if (flood) {
- sendFloodScoped(default_scope, pkt, delay_millis, _prefs.path_hash_mode + 1);
- } else {
- sendZeroHop(pkt, delay_millis);
- }
- } else {
- MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!");
- }
- }
- void MyMesh::updateAdvertTimer() {
- if (_prefs.advert_interval > 0) { // schedule local advert timer
- next_local_advert = futureMillis(((uint32_t)_prefs.advert_interval) * 2 * 60 * 1000);
- } else {
- next_local_advert = 0; // stop the timer
- }
- }
- void MyMesh::updateFloodAdvertTimer() {
- if (_prefs.flood_advert_interval > 0) { // schedule flood advert timer
- next_flood_advert = futureMillis(((uint32_t)_prefs.flood_advert_interval) * 60 * 60 * 1000);
- } else {
- next_flood_advert = 0; // stop the timer
- }
- }
- void MyMesh::dumpLogFile() {
- #if defined(RP2040_PLATFORM)
- File f = _fs->open(PACKET_LOG_FILE, "r");
- #else
- File f = _fs->open(PACKET_LOG_FILE);
- #endif
- if (f) {
- while (f.available()) {
- int c = f.read();
- if (c < 0) break;
- Serial.print((char)c);
- }
- f.close();
- }
- }
- void MyMesh::setTxPower(int8_t power_dbm) {
- radio_set_tx_power(power_dbm);
- }
- #if defined(USE_SX1262) || defined(USE_SX1268)
- void MyMesh::setRxBoostedGain(bool enable) {
- radio_driver.setRxBoostedGainMode(enable);
- }
- #endif
- void MyMesh::formatNeighborsReply(char *reply) {
- char *dp = reply;
- #if MAX_NEIGHBOURS
- // create copy of neighbours list, skipping empty entries so we can sort it separately from main list
- int16_t neighbours_count = 0;
- NeighbourInfo* sorted_neighbours[MAX_NEIGHBOURS];
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- auto neighbour = &neighbours[i];
- if (neighbour->heard_timestamp > 0) {
- sorted_neighbours[neighbours_count] = neighbour;
- neighbours_count++;
- }
- }
- // sort neighbours newest to oldest
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->heard_timestamp > b->heard_timestamp; // desc
- });
- for (int i = 0; i < neighbours_count && dp - reply < 134; i++) {
- NeighbourInfo *neighbour = sorted_neighbours[i];
- // add new line if not first item
- if (i > 0) *dp++ = '\n';
- char hex[10];
- // get 4 bytes of neighbour id as hex
- mesh::Utils::toHex(hex, neighbour->id.pub_key, 4);
- // add next neighbour
- uint32_t secs_ago = getRTCClock()->getCurrentTime() - neighbour->heard_timestamp;
- sprintf(dp, "%s:%d:%d", hex, secs_ago, neighbour->snr);
- while (*dp)
- dp++; // find end of string
- }
- #endif
- if (dp == reply) { // no neighbours, need empty response
- strcpy(dp, "-none-");
- dp += 6;
- }
- *dp = 0; // null terminator
- }
- void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) {
- #if MAX_NEIGHBOURS
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- NeighbourInfo *neighbour = &neighbours[i];
- if (memcmp(neighbour->id.pub_key, pubkey, key_len) == 0) {
- neighbours[i] = NeighbourInfo(); // clear neighbour entry
- _archive_neighbours_dirty = true;
- }
- }
- #endif
- }
- void MyMesh::formatStatsReply(char *reply, size_t reply_size) {
- StatsFormatHelper::formatCoreStats(reply, reply_size, board, *_ms, _err_flags, _mgr);
- }
- void MyMesh::startRegionsLoad() {
- temp_map.resetFrom(region_map); // rebuild regions in a temp instance
- memset(load_stack, 0, sizeof(load_stack));
- load_stack[0] = &temp_map.getWildcard();
- region_load_active = true;
- }
- bool MyMesh::saveRegions() {
- return region_map.save(_fs);
- }
- void MyMesh::onDefaultRegionChanged(const RegionEntry* r) {
- if (r) {
- region_map.getTransportKeysFor(*r, &default_scope, 1);
- } else {
- memset(default_scope.key, 0, sizeof(default_scope.key));
- }
- }
- void MyMesh::formatRadioStatsReply(char *reply, size_t reply_size) {
- StatsFormatHelper::formatRadioStats(reply, reply_size, _radio, radio_driver, getTotalAirTime(), getReceiveAirTime());
- }
- void MyMesh::formatPacketStatsReply(char *reply, size_t reply_size) {
- StatsFormatHelper::formatPacketStats(reply, reply_size, radio_driver, getNumSentFlood(), getNumSentDirect(),
- getNumRecvFlood(), getNumRecvDirect());
- }
- void MyMesh::formatMemoryReply(char *reply, size_t reply_size) {
- StatsFormatHelper::formatMemoryStats(reply, reply_size);
- }
- size_t MyMesh::getNeighbourCount() const {
- #if MAX_NEIGHBOURS
- size_t count = 0;
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- if (neighbours[i].heard_timestamp > 0) {
- count++;
- }
- }
- return count;
- #else
- return 0;
- #endif
- }
- bool MyMesh::restoreArchiveNeighbours() {
- #if MAX_NEIGHBOURS
- if (_archive == nullptr || !_archive->isMounted()) {
- return false;
- }
- FILESYSTEM* fs = _archive->getFS();
- if (fs == nullptr || !fs->exists(kArchiveNeighboursSnapshotPath)) {
- return false;
- }
- File file = openArchiveReadWithRecovery(_archive, kArchiveNeighboursSnapshotPath);
- if (!file) {
- ARCHIVE_LOG("neighbours restore open failed path=%s", kArchiveNeighboursSnapshotPath);
- return false;
- }
- memset(neighbours, 0, sizeof(neighbours));
- char line[128];
- size_t line_len = 0;
- size_t restored = 0;
- while (file.available()) {
- const int raw = file.read();
- if (raw < 0) {
- break;
- }
- const char ch = static_cast<char>(raw);
- if (ch == '\r') {
- continue;
- }
- if (ch == '\n') {
- line[line_len] = 0;
- if (line_len > 0 && restored < MAX_NEIGHBOURS) {
- char full_hex[65];
- unsigned long advert_timestamp = 0;
- unsigned long heard_timestamp = 0;
- int snr = 0;
- memset(full_hex, 0, sizeof(full_hex));
- if (sscanf(line, "%64[^,],%lu,%lu,%d", full_hex, &advert_timestamp, &heard_timestamp, &snr) == 4) {
- uint8_t pub_key[PUB_KEY_SIZE];
- if (mesh::Utils::fromHex(pub_key, PUB_KEY_SIZE, full_hex)) {
- neighbours[restored].id = mesh::Identity(pub_key);
- neighbours[restored].advert_timestamp = static_cast<uint32_t>(advert_timestamp);
- neighbours[restored].heard_timestamp = static_cast<uint32_t>(heard_timestamp);
- neighbours[restored].snr = static_cast<int8_t>(constrain(snr, -128, 127));
- restored++;
- }
- }
- }
- line_len = 0;
- continue;
- }
- if (line_len + 1 < sizeof(line)) {
- line[line_len++] = ch;
- }
- }
- if (line_len > 0 && restored < MAX_NEIGHBOURS) {
- line[line_len] = 0;
- char full_hex[65];
- unsigned long advert_timestamp = 0;
- unsigned long heard_timestamp = 0;
- int snr = 0;
- memset(full_hex, 0, sizeof(full_hex));
- if (sscanf(line, "%64[^,],%lu,%lu,%d", full_hex, &advert_timestamp, &heard_timestamp, &snr) == 4) {
- uint8_t pub_key[PUB_KEY_SIZE];
- if (mesh::Utils::fromHex(pub_key, PUB_KEY_SIZE, full_hex)) {
- neighbours[restored].id = mesh::Identity(pub_key);
- neighbours[restored].advert_timestamp = static_cast<uint32_t>(advert_timestamp);
- neighbours[restored].heard_timestamp = static_cast<uint32_t>(heard_timestamp);
- neighbours[restored].snr = static_cast<int8_t>(constrain(snr, -128, 127));
- restored++;
- }
- }
- }
- file.close();
- _archive_neighbours_dirty = false;
- return restored > 0;
- #else
- return false;
- #endif
- }
- void MyMesh::flushArchiveNeighbours() {
- #if MAX_NEIGHBOURS
- if (_archive == nullptr || !_archive->isMounted()) {
- return;
- }
- FILESYSTEM* fs = _archive->getFS();
- if (fs == nullptr) {
- return;
- }
- int16_t neighbours_count = 0;
- NeighbourInfo* sorted_neighbours[MAX_NEIGHBOURS];
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- if (neighbours[i].heard_timestamp > 0) {
- sorted_neighbours[neighbours_count++] = &neighbours[i];
- }
- }
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->heard_timestamp > b->heard_timestamp;
- });
- File file = openArchiveWriteWithRecovery(_archive, kArchiveNeighboursSnapshotPath);
- if (!file) {
- ARCHIVE_LOG("neighbours open failed path=%s", kArchiveNeighboursSnapshotPath);
- return;
- }
- size_t total_written = 0;
- for (int i = 0; i < neighbours_count; ++i) {
- char full_hex[65];
- mesh::Utils::toHex(full_hex, sorted_neighbours[i]->id.pub_key, PUB_KEY_SIZE);
- total_written += file.printf("%s,%lu,%lu,%d\n",
- full_hex,
- static_cast<unsigned long>(sorted_neighbours[i]->advert_timestamp),
- static_cast<unsigned long>(sorted_neighbours[i]->heard_timestamp),
- static_cast<int>(sorted_neighbours[i]->snr));
- }
- file.flush();
- file.close();
- ARCHIVE_LOG("neighbours flushed path=%s bytes=%u count=%d",
- kArchiveNeighboursSnapshotPath,
- static_cast<unsigned>(total_written),
- static_cast<int>(neighbours_count));
- _archive_neighbours_dirty = false;
- #endif
- }
- void MyMesh::maybeFlushArchiveNeighbours(unsigned long now_ms) {
- #if MAX_NEIGHBOURS
- if (!_archive_neighbours_dirty || _archive == nullptr || !_archive->isMounted()) {
- return;
- }
- if (next_archive_neighbours_flush_ms == 0 || millisHasNowPassed(next_archive_neighbours_flush_ms)) {
- flushArchiveNeighbours();
- next_archive_neighbours_flush_ms = now_ms + kArchiveNeighboursFlushIntervalMs;
- }
- #else
- (void)now_ms;
- #endif
- }
- void MyMesh::recordStatsEvent(uint8_t type, int16_t value) {
- _stats_history.recordEvent(type, getRTCClock()->getCurrentTime(), static_cast<uint32_t>(uptime_millis / 1000), value);
- }
- void MyMesh::updateStatsHistory(unsigned long now_ms) {
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- constexpr uint32_t kLowMemoryEnterBytes = 32UL * 1024UL;
- constexpr uint32_t kLowMemoryClearBytes = 48UL * 1024UL;
- constexpr uint32_t kLowMemoryEventCooldownSecs = 5UL * 60UL;
- _stats_history.setArchive(_archive);
- _stats_history.setEnabled(web.isWebStatsEnabled());
- if (!_stats_history.isEnabled()) {
- _stats_state.initialized = false;
- _archive_neighbours_dirty = false;
- return;
- }
- _stats_history.maybeReleaseIdleBuffers(now_ms);
- const bool wifi_connected = network.isWifiConnected();
- #ifdef WITH_MQTT_UPLINK
- const bool mqtt_connected = mqtt.isAnyBrokerConnected();
- #else
- const bool mqtt_connected = false;
- #endif
- const bool web_panel_up = web.isPanelRunning();
- const bool archive_mounted = (_archive != nullptr) && _archive->isMounted();
- #if defined(ESP32)
- const uint32_t free_heap = ESP.getFreeHeap();
- const uint32_t max_alloc_heap = ESP.getMaxAllocHeap();
- const uint32_t uptime_secs = static_cast<uint32_t>(uptime_millis / 1000);
- bool low_memory = _stats_state.low_memory;
- if (!_stats_state.initialized) {
- low_memory = free_heap <= kLowMemoryEnterBytes;
- } else if (low_memory) {
- low_memory = free_heap <= kLowMemoryClearBytes;
- } else {
- low_memory = free_heap <= kLowMemoryEnterBytes;
- }
- #else
- const bool low_memory = false;
- #endif
- if (!_stats_state.initialized) {
- _stats_state.initialized = true;
- _stats_state.wifi_connected = wifi_connected;
- _stats_state.mqtt_connected = mqtt_connected;
- _stats_state.web_panel_up = web_panel_up;
- _stats_state.archive_mounted = archive_mounted;
- _stats_state.low_memory = low_memory;
- _stats_state.last_low_memory_event_uptime_secs = 0;
- } else {
- if (_stats_state.mqtt_connected != mqtt_connected) {
- recordStatsEvent(mqtt_connected ? HISTORY_EVENT_MQTT_CONNECTED : HISTORY_EVENT_MQTT_DISCONNECTED);
- _stats_state.mqtt_connected = mqtt_connected;
- }
- if (_stats_state.web_panel_up != web_panel_up) {
- recordStatsEvent(web_panel_up ? HISTORY_EVENT_WEB_STARTED : HISTORY_EVENT_WEB_STOPPED);
- _stats_state.web_panel_up = web_panel_up;
- }
- if (_stats_state.archive_mounted != archive_mounted) {
- recordStatsEvent(archive_mounted ? HISTORY_EVENT_ARCHIVE_MOUNTED : HISTORY_EVENT_ARCHIVE_UNAVAILABLE);
- _stats_state.archive_mounted = archive_mounted;
- if (!_stats_history.isLiveOnly() && archive_mounted) {
- if (getNeighbourCount() == 0) {
- restoreArchiveNeighbours();
- }
- next_archive_neighbours_flush_ms = now_ms + kArchiveNeighboursFlushIntervalMs;
- }
- }
- if (!_stats_history.isLiveOnly() && !_stats_state.low_memory && low_memory) {
- #if defined(ESP32)
- if (_stats_state.last_low_memory_event_uptime_secs == 0 ||
- (uptime_secs - _stats_state.last_low_memory_event_uptime_secs) >= kLowMemoryEventCooldownSecs) {
- recordStatsEvent(HISTORY_EVENT_LOW_MEMORY, static_cast<int16_t>(min<uint32_t>(free_heap / 1024, 32767)));
- _stats_state.last_low_memory_event_uptime_secs = uptime_secs;
- }
- #endif
- }
- _stats_state.wifi_connected = wifi_connected;
- _stats_state.low_memory = low_memory;
- }
- #if defined(ESP32)
- const bool live_stats_headroom_low =
- _stats_history.isLiveOnly() && (free_heap <= kLowMemoryClearBytes || max_alloc_heap <= (24UL * 1024UL));
- #else
- const bool live_stats_headroom_low = false;
- #endif
- if (next_history_sample_ms == 0 || millisHasNowPassed(next_history_sample_ms)) {
- if (!live_stats_headroom_low) {
- HistorySample sample{};
- sample.epoch_secs = getRTCClock()->getCurrentTime();
- sample.uptime_secs = static_cast<uint32_t>(uptime_millis / 1000);
- sample.packets_sent = radio_driver.getPacketsSent();
- sample.packets_recv = radio_driver.getPacketsRecv();
- sample.battery_mv = board.getBattMilliVolts();
- sample.queue_len = static_cast<uint16_t>(_mgr->getOutboundTotal());
- sample.error_flags = _err_flags;
- sample.recv_errors = static_cast<uint16_t>(min<uint32_t>(radio_driver.getPacketsRecvErrors(), 0xFFFF));
- sample.neighbour_count = static_cast<uint16_t>(min<size_t>(getNeighbourCount(), 0xFFFF));
- sample.direct_dups =
- static_cast<uint16_t>(min<uint32_t>(((SimpleMeshTables *)getTables())->getNumDirectDups(), 0xFFFF));
- sample.flood_dups =
- static_cast<uint16_t>(min<uint32_t>(((SimpleMeshTables *)getTables())->getNumFloodDups(), 0xFFFF));
- sample.last_rssi_x4 = static_cast<int16_t>(radio_driver.getLastRSSI() * 4.0f);
- sample.last_snr_x4 = static_cast<int16_t>(radio_driver.getLastSNR() * 4.0f);
- sample.noise_floor = static_cast<int16_t>(_radio->getNoiseFloor());
- sample.battery_pct = static_cast<int8_t>(board.getBatteryPercent());
- #if defined(ESP32)
- sample.heap_free = free_heap;
- sample.heap_min = ESP.getMinFreeHeap();
- sample.psram_free = ESP.getFreePsram();
- sample.psram_min = ESP.getMinFreePsram();
- #endif
- if (board.isExternalPowered()) sample.flags |= HISTORY_FLAG_EXTERNAL_POWER;
- if (board.isCharging()) sample.flags |= HISTORY_FLAG_CHARGING;
- if (board.isVbusPresent()) sample.flags |= HISTORY_FLAG_VBUS;
- if (wifi_connected) sample.flags |= HISTORY_FLAG_WIFI_CONNECTED;
- if (mqtt_connected) sample.flags |= HISTORY_FLAG_MQTT_CONNECTED;
- if (web.isWebEnabled()) sample.flags |= HISTORY_FLAG_WEB_ENABLED;
- if (web_panel_up) sample.flags |= HISTORY_FLAG_WEB_PANEL_UP;
- if (archive_mounted) sample.flags |= HISTORY_FLAG_ARCHIVE_MOUNTED;
- _stats_history.pushSample(sample);
- }
- next_history_sample_ms = now_ms + 60000UL;
- }
- _stats_history.maybeFlush(now_ms);
- if (!_stats_history.isLiveOnly()) {
- maybeFlushArchiveNeighbours(now_ms);
- }
- #else
- (void)now_ms;
- #endif
- }
- bool MyMesh::appendJsonEvents(char* reply, size_t reply_size, size_t& offset) const {
- offset += snprintf(&reply[offset], reply_size - offset, "\"events\":[");
- const size_t max_events = min<size_t>(_stats_history.getEventCount(), 6);
- const uint32_t now_epoch_secs = getRTCClock()->getCurrentTime();
- const uint32_t now_uptime_secs = static_cast<uint32_t>(uptime_millis / 1000);
- for (size_t i = 0; i < max_events; ++i) {
- HistoryEvent event{};
- if (!_stats_history.getRecentEvent(i, event)) {
- break;
- }
- const uint32_t age_secs = (now_epoch_secs >= event.epoch_secs && event.epoch_secs > 0)
- ? (now_epoch_secs - event.epoch_secs)
- : ((now_uptime_secs >= event.uptime_secs) ? (now_uptime_secs - event.uptime_secs) : event.uptime_secs);
- offset += snprintf(&reply[offset], reply_size - offset,
- "%s{\"t\":%lu,\"type\":\"%s\",\"value\":%d}",
- i == 0 ? "" : ",",
- static_cast<unsigned long>(age_secs),
- StatsHistory::getEventTypeName(event.type),
- static_cast<int>(event.value));
- if (offset >= reply_size) {
- return false;
- }
- }
- offset += snprintf(&reply[offset], reply_size - offset, "]");
- return offset < reply_size;
- }
- bool MyMesh::appendJsonNeighbours(char* reply, size_t reply_size, size_t& offset) const {
- offset += snprintf(&reply[offset], reply_size - offset, "\"neighbors_detail\":[");
- if (offset >= reply_size) {
- return false;
- }
- #if MAX_NEIGHBOURS
- constexpr size_t kMaxNeighboursJson = 10;
- int16_t neighbours_count = 0;
- NeighbourInfo* sorted_neighbours[MAX_NEIGHBOURS];
- for (int i = 0; i < MAX_NEIGHBOURS; i++) {
- auto neighbour = const_cast<NeighbourInfo*>(&neighbours[i]);
- if (neighbour->heard_timestamp > 0) {
- sorted_neighbours[neighbours_count++] = neighbour;
- }
- }
- std::sort(sorted_neighbours, sorted_neighbours + neighbours_count, [](const NeighbourInfo* a, const NeighbourInfo* b) {
- return a->heard_timestamp > b->heard_timestamp;
- });
- const size_t emit_count = min<size_t>(neighbours_count, kMaxNeighboursJson);
- const uint32_t now_secs = getRTCClock()->getCurrentTime();
- for (size_t i = 0; i < emit_count; ++i) {
- const NeighbourInfo* neighbour = sorted_neighbours[i];
- char hex[7];
- char full_hex[65];
- mesh::Utils::toHex(hex, neighbour->id.pub_key, 3);
- mesh::Utils::toHex(full_hex, neighbour->id.pub_key, PUB_KEY_SIZE);
- const uint32_t heard_secs_ago = now_secs - neighbour->heard_timestamp;
- const uint32_t advert_secs_ago = now_secs - neighbour->advert_timestamp;
- offset += snprintf(&reply[offset], reply_size - offset,
- "%s{\"id\":\"%s\",\"full_id\":\"%s\",\"heard_secs_ago\":%lu,\"advert_secs_ago\":%lu,\"snr_db\":%.2f}",
- i == 0 ? "" : ",",
- hex,
- full_hex,
- static_cast<unsigned long>(heard_secs_ago),
- static_cast<unsigned long>(advert_secs_ago),
- static_cast<double>(neighbour->snr) / 4.0);
- if (offset >= reply_size) {
- return false;
- }
- }
- #endif
- offset += snprintf(&reply[offset], reply_size - offset, "]");
- return offset < reply_size;
- }
- void MyMesh::saveIdentity(const mesh::LocalIdentity &new_id) {
- #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
- IdentityStore store(*_fs, "");
- #elif defined(ESP32)
- IdentityStore store(*_fs, "/identity");
- #elif defined(RP2040_PLATFORM)
- IdentityStore store(*_fs, "/identity");
- #else
- #error "need to define saveIdentity()"
- #endif
- store.save("_main", new_id);
- }
- void MyMesh::clearStats() {
- radio_driver.resetStats();
- resetStats();
- ((SimpleMeshTables *)getTables())->resetStats();
- }
- void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) {
- if (region_load_active) {
- if (StrHelper::isBlank(command)) { // empty/blank line, signal to terminate 'load' operation
- region_map = temp_map; // copy over the temp instance as new current map
- region_load_active = false;
- sprintf(reply, "OK - loaded %d regions", region_map.getCount());
- } else {
- char *np = command;
- while (*np == ' ') np++; // skip indent
- int indent = np - command;
- char *ep = np;
- while (RegionMap::is_name_char(*ep)) ep++;
- if (*ep) { *ep++ = 0; } // set null terminator for end of name
- while (*ep && *ep != 'F') ep++; // look for (optional) flags
- if (indent > 0 && indent < 8 && strlen(np) > 0) {
- auto parent = load_stack[indent - 1];
- if (parent) {
- auto old = region_map.findByName(np);
- auto nw = temp_map.putRegion(np, parent->id, old ? old->id : 0); // carry-over the current ID (if name already exists)
- if (nw) {
- nw->flags = old ? old->flags : (*ep == 'F' ? 0 : REGION_DENY_FLOOD); // carry-over flags from curr
- load_stack[indent] = nw; // keep pointers to parent regions, to resolve parent_id's
- }
- }
- }
- reply[0] = 0;
- }
- return;
- }
- while (*command == ' ') command++; // skip leading spaces
- if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
- memcpy(reply, command, 3); // reflect the prefix back
- reply += 3;
- command += 3;
- }
- // handle ACL related commands
- if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int8}
- char* hex = &command[8];
- char* sp = strchr(hex, ' '); // look for separator char
- if (sp == NULL) {
- strcpy(reply, "Err - bad params");
- } else {
- *sp++ = 0; // replace space with null terminator
- uint8_t pubkey[PUB_KEY_SIZE];
- int hex_len = min(sp - hex, PUB_KEY_SIZE*2);
- if (mesh::Utils::fromHex(pubkey, hex_len / 2, hex)) {
- uint8_t perms = atoi(sp);
- if (acl.applyPermissions(self_id, pubkey, hex_len / 2, perms)) {
- dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - invalid params");
- }
- } else {
- strcpy(reply, "Err - bad pubkey");
- }
- }
- } else if (sender_timestamp == 0 && strcmp(command, "get acl") == 0) {
- Serial.println("ACL:");
- for (int i = 0; i < acl.getNumClients(); i++) {
- auto c = acl.getClientByIdx(i);
- if (c->permissions == 0) continue; // skip deleted (or guest) entries
- Serial.printf("%02X ", c->permissions);
- mesh::Utils::printHex(Serial, c->id.pub_key, PUB_KEY_SIZE);
- Serial.printf("\n");
- }
- reply[0] = 0;
- } else if (memcmp(command, "discover.neighbors", 18) == 0) {
- const char* sub = command + 18;
- while (*sub == ' ') sub++;
- if (*sub != 0) {
- strcpy(reply, "Err - discover.neighbors has no options");
- } else {
- sendNodeDiscoverReq();
- strcpy(reply, "OK - Discover sent");
- }
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- } else if (strcmp(command, "get web.status") == 0 || strcmp(command, "get web") == 0) {
- web.formatWebStatusReply(reply, 160);
- } else if (strcmp(command, "get web.stats.status") == 0) {
- snprintf(reply, 160,
- "> enabled:%s history:%s mode:%s psram:%s psram_bytes:%lu boot_auto:%s samples:%u/%u events:%u/%u archive:%s",
- web.isWebStatsEnabled() ? "on" : "off",
- (_stats_history.isEnabled() && _stats_history.isRecentHistoryAvailable()) ? "active" : "inactive",
- _stats_history.isLiveOnly() ? "live" : "full",
- _stats_history.isPsramBacked() ? "yes" : "no",
- static_cast<unsigned long>(_stats_history.getDetectedPsramSizeBytes()),
- _stats_history.isBootAutoCaptureExpected() ? "yes" : "no",
- static_cast<unsigned>(_stats_history.getSampleCount()),
- static_cast<unsigned>(_stats_history.getSampleCapacity()),
- static_cast<unsigned>(_stats_history.getEventCount()),
- static_cast<unsigned>(_stats_history.getEventCapacity()),
- (_archive != nullptr && _archive->isMounted()) ? "mounted" : "unavailable");
- #endif
- #if defined(ESP_PLATFORM)
- } else if (memcmp(command, "get wifi.status", 15) == 0) {
- network.formatWifiStatusReply(reply, 160);
- } else if (memcmp(command, "get wifi.ssid", 13) == 0) {
- sprintf(reply, "> %s", network.getWifiSSID()[0] ? network.getWifiSSID() : "-");
- } else if (memcmp(command, "get wifi.powersaving", 20) == 0) {
- sprintf(reply, "> %s", network.getWifiPowerSave());
- #endif
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- } else if (memcmp(command, "set web ", 8) == 0) {
- web.setWebEnabled(memcmp(&command[8], "on", 2) == 0);
- strcpy(reply, "OK");
- } else if (memcmp(command, "set.web ", 8) == 0) {
- web.setWebEnabled(memcmp(&command[8], "on", 2) == 0);
- strcpy(reply, "OK");
- } else if (memcmp(command, "set web.stats ", 14) == 0 || memcmp(command, "set.web.stats ", 15) == 0) {
- const char* value = (memcmp(command, "set web.stats ", 14) == 0) ? &command[14] : &command[15];
- const bool enabled = memcmp(value, "on", 2) == 0;
- if (web.setWebStatsEnabled(enabled)) {
- _stats_history.setEnabled(enabled);
- recordStatsEvent(enabled ? HISTORY_EVENT_STATS_ENABLED : HISTORY_EVENT_STATS_DISABLED);
- if (enabled) {
- next_history_sample_ms = millis();
- } else {
- _stats_state.initialized = false;
- }
- strcpy(reply, enabled ? "OK - web.stats on" : "OK - web.stats off");
- } else {
- strcpy(reply, "Err - unable to update web.stats");
- }
- #endif
- #if defined(ESP_PLATFORM)
- } else if (memcmp(command, "set wifi.ssid ", 14) == 0) {
- if (network.setWifiSSID(&command[14])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - bad wifi.ssid");
- }
- } else if (memcmp(command, "set wifi.pwd ", 13) == 0) {
- if (network.setWifiPassword(&command[13])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - bad wifi.pwd");
- }
- } else if (memcmp(command, "set wifi.powersaving ", 21) == 0) {
- if (network.setWifiPowerSave(&command[21])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - use none|min|max");
- }
- #endif
- #ifdef WITH_MQTT_UPLINK
- } else if (memcmp(command, "mqtt.owner ", 11) == 0) {
- if (mqtt.setOwnerPublicKey(&command[11])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - owner must be 64 hex chars");
- }
- } else if (memcmp(command, "mqtt.email ", 11) == 0) {
- if (mqtt.setOwnerEmail(&command[11])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - bad mqtt.email");
- }
- } else if (strcmp(command, "send mqtt.status") == 0) {
- if (mqtt.sendStatusNow()) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - mqtt status unavailable");
- }
- } else if (strcmp(command, "get mqtt.statuscfg") == 0) {
- sprintf(reply, "> %s", mqtt.isStatusEnabled() ? "on" : "off");
- } else if (strcmp(command, "get mqtt.status") == 0) {
- mqtt.formatStatusReply(reply, 160);
- } else if (memcmp(command, "get mqtt.iata", 13) == 0) {
- sprintf(reply, "> %s", mqtt.getIata());
- } else if (memcmp(command, "get mqtt.owner", 14) == 0) {
- sprintf(reply, "> %s", mqtt.getOwnerPublicKey()[0] ? mqtt.getOwnerPublicKey() : "-");
- } else if (memcmp(command, "get mqtt.email", 14) == 0) {
- sprintf(reply, "> %s", mqtt.getOwnerEmail()[0] ? mqtt.getOwnerEmail() : "-");
- } else if (memcmp(command, "get mqtt.packets", 16) == 0) {
- sprintf(reply, "> %s", mqtt.isPacketsEnabled() ? "on" : "off");
- } else if (memcmp(command, "get mqtt.raw", 12) == 0) {
- sprintf(reply, "> %s", mqtt.isRawEnabled() ? "on" : "off");
- } else if (memcmp(command, "get mqtt.tx", 11) == 0) {
- sprintf(reply, "> %s", mqtt.isTxEnabled() ? "on" : "off");
- } else if (memcmp(command, "get mqtt.eastmesh-au", 20) == 0 || memcmp(command, "get mqtt.eastmesh.au", 20) == 0) {
- sprintf(reply, "> %s", mqtt.isEndpointEnabled(0x01) ? "on" : "off");
- } else if (memcmp(command, "get mqtt.letsmesh-eu", 21) == 0 || memcmp(command, "get mqtt.letsmesh.eu", 21) == 0) {
- sprintf(reply, "> %s", mqtt.isEndpointEnabled(0x02) ? "on" : "off");
- } else if (memcmp(command, "get mqtt.letsmesh-us", 21) == 0 || memcmp(command, "get mqtt.letsmesh.us", 21) == 0) {
- sprintf(reply, "> %s", mqtt.isEndpointEnabled(0x04) ? "on" : "off");
- } else if (memcmp(command, "set mqtt.tx ", 12) == 0) {
- mqtt.setTxEnabled(memcmp(&command[12], "on", 2) == 0);
- strcpy(reply, "OK");
- } else if (memcmp(command, "set mqtt.iata ", 14) == 0) {
- if (mqtt.setIata(&command[14])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - bad mqtt.iata");
- }
- } else if (memcmp(command, "set mqtt.owner ", 15) == 0) {
- if (mqtt.setOwnerPublicKey(&command[15])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - owner must be 64 hex chars");
- }
- } else if (memcmp(command, "set mqtt.email ", 15) == 0) {
- if (mqtt.setOwnerEmail(&command[15])) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - bad mqtt.email");
- }
- } else if (memcmp(command, "set mqtt.packets ", 17) == 0) {
- mqtt.setPacketsEnabled(memcmp(&command[17], "on", 2) == 0);
- strcpy(reply, "OK");
- } else if (memcmp(command, "set mqtt.raw ", 13) == 0) {
- mqtt.setRawEnabled(memcmp(&command[13], "on", 2) == 0);
- strcpy(reply, "OK");
- } else if (memcmp(command, "set mqtt.status ", 16) == 0) {
- mqtt.setStatusEnabled(memcmp(&command[16], "on", 2) == 0);
- strcpy(reply, "OK");
- } else if (memcmp(command, "set mqtt.eastmesh-au ", 21) == 0 || memcmp(command, "set mqtt.eastmesh.au ", 21) == 0) {
- if (mqtt.setEndpointEnabled(0x01, memcmp(&command[21], "on", 2) == 0)) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - max 2 mqtt brokers");
- }
- } else if (memcmp(command, "set mqtt.letsmesh-eu ", 21) == 0 || memcmp(command, "set mqtt.letsmesh.eu ", 21) == 0) {
- if (mqtt.setEndpointEnabled(0x02, memcmp(&command[21], "on", 2) == 0)) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - max 2 mqtt brokers");
- }
- } else if (memcmp(command, "set mqtt.letsmesh-us ", 21) == 0 || memcmp(command, "set mqtt.letsmesh.us ", 21) == 0) {
- if (mqtt.setEndpointEnabled(0x04, memcmp(&command[21], "on", 2) == 0)) {
- strcpy(reply, "OK");
- } else {
- strcpy(reply, "Err - max 2 mqtt brokers");
- }
- #endif
- } else{
- _cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
- }
- }
- void MyMesh::runWebCommand(const char* command, char* reply, size_t reply_size) {
- if (reply_size == 0) {
- return;
- }
- reply[0] = 0;
- if (command == nullptr) {
- strncpy(reply, "Err - empty command", reply_size - 1);
- reply[reply_size - 1] = 0;
- return;
- }
- auto matches_exact = [command](const char* candidate) -> bool {
- return strcmp(command, candidate) == 0;
- };
- auto matches_prefix = [command](const char* candidate) -> bool {
- size_t len = strlen(candidate);
- return strncmp(command, candidate, len) == 0;
- };
- bool allowed =
- matches_exact("clock") ||
- matches_exact("get mqtt.status") ||
- matches_exact("get web.status") ||
- matches_exact("get web.stats.status") ||
- matches_exact("get web") ||
- matches_exact("advert") ||
- matches_exact("reboot") ||
- matches_exact("start ota") ||
- matches_exact("get wifi.status") ||
- matches_exact("get wifi.powersaving") ||
- matches_exact("stats-core") ||
- matches_exact("stats-radio") ||
- matches_exact("stats-packets") ||
- matches_exact("memory") ||
- matches_exact("get mqtt.iata") ||
- matches_exact("get mqtt.owner") ||
- matches_exact("get mqtt.email") ||
- matches_exact("get mqtt.packets") ||
- matches_exact("get mqtt.raw") ||
- matches_exact("get mqtt.statuscfg") ||
- matches_exact("get mqtt.tx") ||
- matches_exact("get mqtt.eastmesh-au") ||
- matches_exact("get mqtt.eastmesh.au") ||
- matches_exact("get mqtt.letsmesh-eu") ||
- matches_exact("get mqtt.letsmesh.eu") ||
- matches_exact("get mqtt.letsmesh-us") ||
- matches_exact("get mqtt.letsmesh.us") ||
- matches_exact("get name") ||
- matches_exact("get lat") ||
- matches_exact("get lon") ||
- matches_exact("get radio") ||
- matches_exact("get prv.key") ||
- matches_exact("get role") ||
- matches_exact("get public.key") ||
- matches_exact("get advert.interval") ||
- matches_exact("get flood.advert.interval") ||
- matches_exact("get repeat") ||
- matches_exact("get flood.max") ||
- matches_exact("get path.hash.mode") ||
- matches_exact("get owner.info") ||
- matches_exact("get guest.password") ||
- matches_prefix("set wifi.ssid ") ||
- matches_prefix("set wifi.pwd ") ||
- matches_prefix("set wifi.powersaving ") ||
- matches_prefix("set mqtt.iata ") ||
- matches_prefix("set mqtt.owner ") ||
- matches_prefix("set mqtt.email ") ||
- matches_prefix("set mqtt.packets ") ||
- matches_prefix("set mqtt.raw ") ||
- matches_prefix("set mqtt.status ") ||
- matches_prefix("set mqtt.tx ") ||
- matches_prefix("set web ") ||
- matches_prefix("set.web ") ||
- matches_prefix("set web.stats ") ||
- matches_prefix("set.web.stats ") ||
- matches_prefix("set mqtt.eastmesh-au ") ||
- matches_prefix("set mqtt.eastmesh.au ") ||
- matches_prefix("set mqtt.letsmesh-eu ") ||
- matches_prefix("set mqtt.letsmesh.eu ") ||
- matches_prefix("set mqtt.letsmesh-us ") ||
- matches_prefix("set mqtt.letsmesh.us ") ||
- matches_prefix("set name ") ||
- matches_prefix("set lat ") ||
- matches_prefix("set lon ") ||
- matches_prefix("set radio ") ||
- matches_prefix("password ") ||
- matches_prefix("set guest.password ") ||
- matches_prefix("set prv.key ") ||
- matches_prefix("set advert.interval ") ||
- matches_prefix("set flood.advert.interval ") ||
- matches_prefix("set repeat ") ||
- matches_prefix("set flood.max ") ||
- matches_prefix("set path.hash.mode ") ||
- matches_prefix("time ") ||
- matches_prefix("time.force ") ||
- matches_prefix("set owner.info ");
- if (!allowed) {
- strncpy(reply, "Err - command not allowlisted for web access", reply_size - 1);
- reply[reply_size - 1] = 0;
- return;
- }
- char command_buf[192];
- StrHelper::strncpy(command_buf, command, sizeof(command_buf));
- handleCommand(0, command_buf, reply);
- reply[reply_size - 1] = 0;
- }
- bool MyMesh::isWebStatsEnabled() const {
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- return web.isWebStatsEnabled();
- #else
- return false;
- #endif
- }
- bool MyMesh::formatWebStatsSummaryJson(char* reply, size_t reply_size) {
- if (reply == nullptr || reply_size == 0) {
- return false;
- }
- reply[0] = 0;
- #if !defined(ESP_PLATFORM) || !WITH_WEB_PANEL
- return false;
- #else
- char wifi_ssid[48];
- char wifi_status[20];
- char wifi_state[24];
- char wifi_ip[20];
- char wifi_signal[16];
- char wifi_powersave[12];
- escapeJsonString(network.getWifiSSID()[0] ? network.getWifiSSID() : "-", wifi_ssid, sizeof(wifi_ssid));
- escapeJsonString(network.getWifiPowerSave(), wifi_powersave, sizeof(wifi_powersave));
- int wifi_rssi = 0;
- int wifi_quality = 0;
- int wifi_code = 0;
- #if defined(ESP32)
- if (network.getWifiSSID()[0] == 0) {
- strncpy(wifi_status, "unconfigured", sizeof(wifi_status) - 1);
- wifi_status[sizeof(wifi_status) - 1] = 0;
- strncpy(wifi_state, "unconfigured", sizeof(wifi_state) - 1);
- wifi_state[sizeof(wifi_state) - 1] = 0;
- strncpy(wifi_ip, "--", sizeof(wifi_ip) - 1);
- wifi_ip[sizeof(wifi_ip) - 1] = 0;
- strncpy(wifi_signal, "--", sizeof(wifi_signal) - 1);
- wifi_signal[sizeof(wifi_signal) - 1] = 0;
- } else if (network.isWifiConnected()) {
- strncpy(wifi_status, "connected", sizeof(wifi_status) - 1);
- wifi_status[sizeof(wifi_status) - 1] = 0;
- strncpy(wifi_state, "connected", sizeof(wifi_state) - 1);
- wifi_state[sizeof(wifi_state) - 1] = 0;
- String ip = WiFi.localIP().toString();
- escapeJsonString(ip.c_str(), wifi_ip, sizeof(wifi_ip));
- wifi_rssi = WiFi.RSSI();
- wifi_code = static_cast<int>(WiFi.status());
- if (wifi_rssi <= -100) {
- wifi_quality = 0;
- strncpy(wifi_signal, "poor", sizeof(wifi_signal) - 1);
- } else if (wifi_rssi >= -50) {
- wifi_quality = 100;
- strncpy(wifi_signal, "excellent", sizeof(wifi_signal) - 1);
- } else {
- wifi_quality = 2 * (wifi_rssi + 100);
- if (wifi_rssi >= -60) {
- strncpy(wifi_signal, "excellent", sizeof(wifi_signal) - 1);
- } else if (wifi_rssi >= -67) {
- strncpy(wifi_signal, "good", sizeof(wifi_signal) - 1);
- } else if (wifi_rssi >= -75) {
- strncpy(wifi_signal, "fair", sizeof(wifi_signal) - 1);
- } else {
- strncpy(wifi_signal, "poor", sizeof(wifi_signal) - 1);
- }
- }
- wifi_signal[sizeof(wifi_signal) - 1] = 0;
- } else {
- strncpy(wifi_status, "connecting", sizeof(wifi_status) - 1);
- wifi_status[sizeof(wifi_status) - 1] = 0;
- wifi_code = static_cast<int>(WiFi.status());
- switch (WiFi.status()) {
- case WL_IDLE_STATUS:
- strncpy(wifi_state, "idle", sizeof(wifi_state) - 1);
- break;
- case WL_NO_SSID_AVAIL:
- strncpy(wifi_state, "no_ssid", sizeof(wifi_state) - 1);
- break;
- case WL_SCAN_COMPLETED:
- strncpy(wifi_state, "scan_completed", sizeof(wifi_state) - 1);
- break;
- case WL_CONNECT_FAILED:
- strncpy(wifi_state, "connect_failed", sizeof(wifi_state) - 1);
- break;
- case WL_CONNECTION_LOST:
- strncpy(wifi_state, "connection_lost", sizeof(wifi_state) - 1);
- break;
- case WL_DISCONNECTED:
- strncpy(wifi_state, "disconnected", sizeof(wifi_state) - 1);
- break;
- default:
- strncpy(wifi_state, "unknown", sizeof(wifi_state) - 1);
- break;
- }
- wifi_state[sizeof(wifi_state) - 1] = 0;
- strncpy(wifi_ip, "--", sizeof(wifi_ip) - 1);
- wifi_ip[sizeof(wifi_ip) - 1] = 0;
- strncpy(wifi_signal, "--", sizeof(wifi_signal) - 1);
- wifi_signal[sizeof(wifi_signal) - 1] = 0;
- }
- #else
- strncpy(wifi_status, "unsupported", sizeof(wifi_status) - 1);
- wifi_status[sizeof(wifi_status) - 1] = 0;
- strncpy(wifi_state, "unsupported", sizeof(wifi_state) - 1);
- wifi_state[sizeof(wifi_state) - 1] = 0;
- strncpy(wifi_ip, "--", sizeof(wifi_ip) - 1);
- wifi_ip[sizeof(wifi_ip) - 1] = 0;
- strncpy(wifi_signal, "--", sizeof(wifi_signal) - 1);
- wifi_signal[sizeof(wifi_signal) - 1] = 0;
- #endif
- const int battery_pct = board.getBatteryPercent();
- const bool archive_available = (_archive != nullptr) && _archive->isMounted();
- #ifdef WITH_MQTT_UPLINK
- const bool mqtt_connected = mqtt.isAnyBrokerConnected();
- const char* mqtt_state = mqtt.getAggregateBrokerState();
- #else
- const bool mqtt_connected = false;
- const char* mqtt_state = "down";
- #endif
- const bool web_panel_up = web.isPanelRunning();
- const char* archive_name = (_archive != nullptr) ? _archive->getLogicalName() : "archive";
- const char* archive_path = (_archive != nullptr) ? _archive->getLogicalStatsPath() : "archive:/stats";
- const char* archive_type = (_archive != nullptr) ? _archive->getCardTypeName() : "unavailable";
- _stats_history.noteAccess(millis());
- const uint32_t heap_free = ESP.getFreeHeap();
- const uint32_t heap_min = ESP.getMinFreeHeap();
- const uint32_t heap_max = ESP.getMaxAllocHeap();
- const uint32_t psram_free = ESP.getFreePsram();
- const uint32_t psram_min = ESP.getMinFreePsram();
- const uint32_t psram_max = ESP.getMaxAllocPsram();
- size_t offset = 0;
- offset += snprintf(&reply[offset], reply_size - offset,
- "{\"enabled\":true,"
- "\"history\":{\"active\":%s,\"psram\":%s,\"degraded\":%s,\"live_only\":%s,\"samples\":%u,\"sample_capacity\":%u,\"sample_interval_secs\":%lu,"
- "\"archive_restored\":%s,\"archive_restored_samples\":%u,\"archive_summary_interval_secs\":%lu,"
- "\"events\":%u,\"event_capacity\":%u},"
- "\"archive\":{\"logical\":\"%s\",\"available\":%s,\"path\":\"%s\",\"type\":\"%s\","
- "\"total_bytes\":%llu,\"used_bytes\":%llu},"
- "\"core\":{\"battery_mv\":%u,\"battery_pct\":%d,\"uptime_secs\":%lu,\"errors\":%u,\"queue_len\":%u,"
- "\"external_power\":%s,\"charging\":%s,\"vbus\":%s},"
- "\"radio\":{\"noise_floor\":%d,\"last_rssi\":%.2f,\"last_snr\":%.2f,\"tx_air_secs\":%lu,\"rx_air_secs\":%lu},"
- "\"packets\":{\"recv\":%u,\"sent\":%u,\"flood_tx\":%u,\"direct_tx\":%u,\"flood_rx\":%u,\"direct_rx\":%u,"
- "\"recv_errors\":%u,\"direct_dups\":%u,\"flood_dups\":%u,\"neighbors\":%u},"
- "\"memory\":{\"heap_free\":%u,\"heap_min\":%u,\"heap_max\":%u,\"psram_free\":%u,\"psram_min\":%u,\"psram_max\":%u},"
- "\"wifi\":{\"ssid\":\"%s\",\"status\":\"%s\",\"connected\":%s,\"state\":\"%s\",\"code\":%d,\"ip\":\"%s\",\"rssi\":%d,\"quality\":%d,\"signal\":\"%s\",\"powersave\":\"%s\"},"
- "\"services\":{\"mqtt_connected\":%s,\"mqtt_state\":\"%s\",\"web_enabled\":%s,\"web_panel_up\":%s,\"web_auth\":\"%s\","
- "\"archive_available\":%s}",
- (_stats_history.isEnabled() && _stats_history.isRecentHistoryAvailable()) ? "true" : "false",
- _stats_history.isPsramBacked() ? "true" : "false",
- _stats_history.isDegraded() ? "true" : "false",
- _stats_history.isLiveOnly() ? "true" : "false",
- static_cast<unsigned>(_stats_history.getSampleCount()),
- static_cast<unsigned>(_stats_history.getSampleCapacity()),
- static_cast<unsigned long>(StatsHistory::getSampleIntervalSecs()),
- _stats_history.hasArchiveRestore() ? "true" : "false",
- static_cast<unsigned>(_stats_history.getRestoredSampleCount()),
- static_cast<unsigned long>(StatsHistory::getArchiveSummaryIntervalSecs()),
- static_cast<unsigned>(_stats_history.getEventCount()),
- static_cast<unsigned>(_stats_history.getEventCapacity()),
- archive_name,
- archive_available ? "true" : "false",
- archive_path,
- archive_type,
- static_cast<unsigned long long>(_archive != nullptr ? _archive->getTotalBytes() : 0),
- static_cast<unsigned long long>(_archive != nullptr ? _archive->getUsedBytes() : 0),
- board.getBattMilliVolts(),
- battery_pct,
- static_cast<unsigned long>(uptime_millis / 1000),
- _err_flags,
- static_cast<unsigned>(_mgr->getOutboundTotal()),
- board.isExternalPowered() ? "true" : "false",
- board.isCharging() ? "true" : "false",
- board.isVbusPresent() ? "true" : "false",
- static_cast<int>(_radio->getNoiseFloor()),
- radio_driver.getLastRSSI(),
- radio_driver.getLastSNR(),
- static_cast<unsigned long>(getTotalAirTime() / 1000),
- static_cast<unsigned long>(getReceiveAirTime() / 1000),
- static_cast<unsigned>(radio_driver.getPacketsRecv()),
- static_cast<unsigned>(radio_driver.getPacketsSent()),
- static_cast<unsigned>(getNumSentFlood()),
- static_cast<unsigned>(getNumSentDirect()),
- static_cast<unsigned>(getNumRecvFlood()),
- static_cast<unsigned>(getNumRecvDirect()),
- static_cast<unsigned>(radio_driver.getPacketsRecvErrors()),
- static_cast<unsigned>(((SimpleMeshTables *)getTables())->getNumDirectDups()),
- static_cast<unsigned>(((SimpleMeshTables *)getTables())->getNumFloodDups()),
- static_cast<unsigned>(getNeighbourCount()),
- heap_free,
- heap_min,
- heap_max,
- psram_free,
- psram_min,
- psram_max,
- wifi_ssid,
- wifi_status,
- network.isWifiConnected() ? "true" : "false",
- wifi_state,
- wifi_code,
- wifi_ip,
- wifi_rssi,
- wifi_quality,
- wifi_signal,
- wifi_powersave,
- mqtt_connected ? "true" : "false",
- mqtt_state,
- web.isWebEnabled() ? "true" : "false",
- web_panel_up ? "true" : "false",
- web.isPanelUnlocked() ? "unlocked" : "locked",
- archive_available ? "true" : "false");
- if (offset >= reply_size) {
- return false;
- }
- offset += snprintf(&reply[offset], reply_size - offset, ",");
- if (!appendJsonEvents(reply, reply_size, offset)) {
- return false;
- }
- offset += snprintf(&reply[offset], reply_size - offset, ",");
- if (!appendJsonNeighbours(reply, reply_size, offset)) {
- return false;
- }
- offset += snprintf(&reply[offset], reply_size - offset, "}");
- return offset < reply_size;
- #endif
- }
- bool MyMesh::formatWebStatsSeriesJson(const char* series, char* reply, size_t reply_size) {
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- if (!web.isWebStatsEnabled()) {
- if (reply != nullptr && reply_size > 0) {
- reply[0] = 0;
- }
- return false;
- }
- _stats_history.noteAccess(millis());
- return _stats_history.buildSeriesJson(
- series,
- reply,
- reply_size,
- getRTCClock()->getCurrentTime(),
- static_cast<uint32_t>(uptime_millis / 1000));
- #else
- (void)series;
- if (reply != nullptr && reply_size > 0) {
- reply[0] = 0;
- }
- return false;
- #endif
- }
- void MyMesh::loop() {
- #ifdef WITH_BRIDGE
- bridge.loop();
- #endif
- const uint32_t now = millis();
- uptime_millis += now - last_millis;
- last_millis = now;
- mesh::Mesh::loop();
- if (next_flood_advert && millisHasNowPassed(next_flood_advert)) {
- mesh::Packet *pkt = createSelfAdvert();
- uint32_t delay_millis = 0;
- if (pkt) sendFloodScoped(default_scope, pkt, delay_millis, _prefs.path_hash_mode + 1);
- updateFloodAdvertTimer(); // schedule next flood advert
- updateAdvertTimer(); // also schedule local advert (so they don't overlap)
- } else if (next_local_advert && millisHasNowPassed(next_local_advert)) {
- mesh::Packet *pkt = createSelfAdvert();
- if (pkt) sendZeroHop(pkt);
- updateAdvertTimer(); // schedule next local advert
- }
- if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params
- set_radio_at = 0; // clear timer
- radio_set_params(pending_freq, pending_bw, pending_sf, pending_cr);
- MESH_DEBUG_PRINTLN("Temp radio params");
- }
- if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
- revert_radio_at = 0; // clear timer
- radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
- MESH_DEBUG_PRINTLN("Radio params restored");
- }
- // is pending dirty contacts write needed?
- if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
- acl.save(_fs);
- dirty_contacts_expiry = 0;
- }
- #if defined(ESP_PLATFORM)
- bool network_required = false;
- #if WITH_WEB_PANEL
- network_required = web.isWebEnabled();
- #endif
- #ifdef WITH_MQTT_UPLINK
- network_required = network_required || mqtt.isActive();
- #endif
- network.loop(network_required);
- #if WITH_WEB_PANEL
- web.loop();
- #endif
- #endif
- #ifdef WITH_MQTT_UPLINK
- MQTTStatusSnapshot mqtt_status{};
- mqtt_status.battery_mv = static_cast<int>(board.getBattMilliVolts());
- mqtt_status.uptime_secs = static_cast<uint32_t>(uptime_millis / 1000);
- mqtt_status.error_flags = _err_flags;
- mqtt_status.queue_len = static_cast<uint16_t>(_mgr->getOutboundTotal());
- mqtt_status.noise_floor = static_cast<int>(_radio->getNoiseFloor());
- mqtt_status.tx_air_secs = static_cast<uint32_t>(getTotalAirTime() / 1000);
- mqtt_status.rx_air_secs = static_cast<uint32_t>(getReceiveAirTime() / 1000);
- mqtt_status.recv_errors = radio_driver.getPacketsRecvErrors();
- mqtt_status.radio_freq = _prefs.freq;
- mqtt_status.radio_bw = _prefs.bw;
- mqtt_status.radio_sf = _prefs.sf;
- mqtt_status.radio_cr = _prefs.cr;
- mqtt.loop(mqtt_status);
- #endif
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- updateStatsHistory(now);
- #endif
- }
- // To check if there is pending work
- bool MyMesh::hasPendingWork() const {
- #if defined(WITH_BRIDGE)
- if (bridge.isRunning()) return true; // bridge needs WiFi radio, can't sleep
- #endif
- #if defined(WITH_MQTT_UPLINK)
- if (mqtt.isActive()) return true;
- #endif
- #if defined(ESP_PLATFORM) && WITH_WEB_PANEL
- if (web.isWebEnabled()) return true;
- #endif
- return _mgr->getOutboundTotal() > 0;
- }
|