mirror of
https://github.com/liquidraver/ZephCore.git
synced 2026-10-15 21:20:03 +00:00
Server roles (slice 7) - CommonCLI split into upstream's shape; region CLI moved into CommonCLI; ClientACL, RegionMap, TransportKeyStore and both server meshes in upstream shape over an Arduino compat shim (helpers/compat: Arduino.h, Stream/Print, Serial, SHA256, FILESYSTEM/File over Zephyr fs) - room server processAck port; `get acl` prints as upstream - room: default name Room-XXXX, display/input rotation, live radio UI refresh Event loops (slice 7b) - repeater and room server share server_main_common with a ServerRole descriptor; shared event bits in src/mesh_events.h - room server deadline-driven (idle wakes 2.2/s -> 0.08/s); companion loop() runs on housekeeping (closes the lazy contact-flush gap) - `get loop.wakes` in debug builds (CONFIG_ZEPHCORE_LOOP_WAKE_STATS) Companion transports and WiFi (slice 8) - upstream MultiSerialInterface: BLE, USB/UART and TCP are each a BaseSerialInterface, every connected transport gets every frame; CompanionSerial holds a lossless reply; shared frame_txq for BLE/TCP - SerialCompanionTransport and LinuxTCPTransport replaced by TcpCompanionTransport; ZephyrBLE split (gatt layout, DFU) - WiFi companion (upstream wifi.ssid/pwd/enabled CLI, TCP port 5000) on boards declaring `capabilities: wifi` in zephcore.yml: the PSRAM ESP32-S3 boards, plus Heltec V3 (140 contacts), Wireless Tracker (130) and both C6 boards (280); ESP32-S3 without PSRAM gets bounded WiFi buffers and a measured 72 KB heap; PSRAM boards' internal heap 72 KB - TCP queues: own depth (ZEPHCORE_TCP_QUEUE_SIZE=8), RX waits instead of dropping - RAM: companion ClientACL dropped; main/BT-TX/TCP stacks sized from measured peaks (-7 KB nRF companions, -9 KB ESP32 companions) - ESP32 WiFi stopped working under ~200 frames/s unicast. Fixed by: patches/zephyr/0017 never block the WiFi task in the RX callback; patches/zephyr/0018 lock shared_multi_heap (heap corruption with the WiFi heap in PSRAM); patches/modules/hal_espressif/0001 backport of hal_espressif af97fb02df (PSRAM realloc) Storage and prefs (slice 9) - prefs are now prefs.json through upstream's ConfigSerializer (ported verbatim), with upstream's key names for shared fields and ZephCore's own under "zc"; on first boot the binary new_prefs / repeater prefs is migrated and kept, so a downgrade still boots with the old settings - helpers/PrefsCodec: both legacy binary layouts (companion 272 B, server 311 B) as Zephyr-free codecs; sanitizeNodePrefs is the only validator - fix: server prefs could be replaced by a truncated file when a write failed; identity, prefs and channel writes go through one checked atomic replace (ZephyrFsUtil) - fix: a corrupt cad_auto byte turned adaptive CAD off instead of the default on - fix: a deliberate 300 s GPS duty on a repeater was rewritten to 48 h on every boot (that migration now runs on legacy files only) - one identity loader for all roles (IdentityFile); an unusable key file is always parked as .bad before regenerating - server store no longer stamps repeater defaults on room server and observer; companion first-boot volume policy moved into the store (adoptVolume) - ZephyrDataStore 1435 -> ~850 lines
382 lines
12 KiB
C++
382 lines
12 KiB
C++
#include "RegionMap.h"
|
|
#include <helpers/TxtDataHelpers.h>
|
|
#include <SHA256.h>
|
|
|
|
// helper class for region map exporter, we emulate Stream with a safe buffer writer.
|
|
|
|
class BufStream : public Stream {
|
|
public:
|
|
BufStream(char *buf, size_t max_len)
|
|
: _buf(buf), _max_len(max_len), _pos(0) {
|
|
if (_max_len > 0) _buf[0] = 0;
|
|
}
|
|
|
|
size_t write(uint8_t c) override {
|
|
if (_pos + 1 >= _max_len) return 0;
|
|
_buf[_pos++] = c;
|
|
_buf[_pos] = 0;
|
|
return 1;
|
|
}
|
|
|
|
size_t write(const uint8_t *buffer, size_t size) override {
|
|
size_t written = 0;
|
|
while (written < size) {
|
|
if (!write(buffer[written])) break;
|
|
written++;
|
|
}
|
|
return written;
|
|
}
|
|
|
|
int available() override { return 0; }
|
|
int read() override { return -1; }
|
|
int peek() override { return -1; }
|
|
void flush() override {}
|
|
|
|
size_t length() const { return _pos; }
|
|
|
|
private:
|
|
char *_buf;
|
|
size_t _max_len;
|
|
size_t _pos;
|
|
};
|
|
|
|
|
|
RegionMap::RegionMap(TransportKeyStore& store) : _store(&store) {
|
|
next_id = 1; num_regions = 0;
|
|
default_id = home_id = 0;
|
|
wildcard.id = wildcard.parent = 0;
|
|
wildcard.flags = 0; // default behaviour, allow flood and direct
|
|
strcpy(wildcard.name, "*");
|
|
}
|
|
|
|
bool RegionMap::is_name_char(uint8_t c) {
|
|
// accept all alpha-num or accented characters, but exclude most punctuation chars
|
|
return c == '-' || c == '$' || c == '#' || (c >= '0' && c <= '9') || c >= 'A';
|
|
}
|
|
|
|
static const char* skip_hash(const char* name) {
|
|
return *name == '#' ? name + 1 : name;
|
|
}
|
|
|
|
static File openWrite(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
|
|
return _fs->open(filename, "w", true);
|
|
#endif
|
|
}
|
|
|
|
bool RegionMap::load(FILESYSTEM* _fs, const char* path) {
|
|
if (_fs->exists(path ? path : "/regions2")) {
|
|
#if defined(RP2040_PLATFORM)
|
|
File file = _fs->open(path ? path : "/regions2", "r");
|
|
#else
|
|
File file = _fs->open(path ? path : "/regions2");
|
|
#endif
|
|
|
|
if (file) {
|
|
uint8_t pad[128];
|
|
|
|
num_regions = 0; next_id = 1;
|
|
default_id = home_id = 0;
|
|
|
|
bool success = file.read(pad, 3) == 3; // reserved header
|
|
success = success && file.read((uint8_t *) &default_id, sizeof(default_id)) == sizeof(default_id);
|
|
success = success && file.read((uint8_t *) &home_id, sizeof(home_id)) == sizeof(home_id);
|
|
success = success && file.read((uint8_t *) &wildcard.flags, sizeof(wildcard.flags)) == sizeof(wildcard.flags);
|
|
success = success && file.read((uint8_t *) &next_id, sizeof(next_id)) == sizeof(next_id);
|
|
|
|
if (success) {
|
|
while (num_regions < MAX_REGION_ENTRIES) {
|
|
auto r = ®ions[num_regions];
|
|
|
|
int n = file.read((uint8_t *) &r->id, sizeof(r->id));
|
|
if (n == 0) break; // clean EOF
|
|
success = (n == sizeof(r->id));
|
|
success = success && file.read((uint8_t *) &r->parent, sizeof(r->parent)) == sizeof(r->parent);
|
|
success = success && file.read((uint8_t *) r->name, sizeof(r->name)) == sizeof(r->name);
|
|
success = success && file.read((uint8_t *) &r->flags, sizeof(r->flags)) == sizeof(r->flags);
|
|
success = success && file.read(pad, sizeof(pad)) == sizeof(pad);
|
|
|
|
if (!success) break; // partial read or corruption
|
|
|
|
if (r->id >= next_id) { // make sure next_id is valid
|
|
next_id = r->id + 1;
|
|
}
|
|
num_regions++;
|
|
}
|
|
}
|
|
file.close();
|
|
return success;
|
|
}
|
|
}
|
|
return false; // failed
|
|
}
|
|
|
|
bool RegionMap::save(FILESYSTEM* _fs, const char* path) {
|
|
File file = openWrite(_fs, path ? path : "/regions2");
|
|
if (file) {
|
|
uint8_t pad[128];
|
|
memset(pad, 0, sizeof(pad));
|
|
|
|
bool success = file.write(pad, 3) == 3; // reserved header
|
|
success = success && file.write((uint8_t *) &default_id, sizeof(default_id)) == sizeof(default_id);
|
|
success = success && file.write((uint8_t *) &home_id, sizeof(home_id)) == sizeof(home_id);
|
|
success = success && file.write((uint8_t *) &wildcard.flags, sizeof(wildcard.flags)) == sizeof(wildcard.flags);
|
|
success = success && file.write((uint8_t *) &next_id, sizeof(next_id)) == sizeof(next_id);
|
|
|
|
if (success) {
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto r = ®ions[i];
|
|
|
|
success = file.write((uint8_t *) &r->id, sizeof(r->id)) == sizeof(r->id);
|
|
success = success && file.write((uint8_t *) &r->parent, sizeof(r->parent)) == sizeof(r->parent);
|
|
success = success && file.write((uint8_t *) r->name, sizeof(r->name)) == sizeof(r->name);
|
|
success = success && file.write((uint8_t *) &r->flags, sizeof(r->flags)) == sizeof(r->flags);
|
|
success = success && file.write(pad, sizeof(pad)) == sizeof(pad);
|
|
if (!success) break; // write failed
|
|
}
|
|
}
|
|
file.close();
|
|
return success;
|
|
}
|
|
return false; // failed
|
|
}
|
|
|
|
RegionEntry* RegionMap::putRegion(const char* name, uint16_t parent_id, uint16_t id) {
|
|
const char* sp = name; // check for illegal name chars
|
|
while (*sp) {
|
|
if (!is_name_char(*sp)) return NULL; // error
|
|
sp++;
|
|
}
|
|
|
|
auto region = findByName(name);
|
|
if (region) {
|
|
if (region->id == parent_id) return NULL; // ERROR: invalid parent!
|
|
|
|
region->parent = parent_id; // re-parent / move this region in the hierarchy
|
|
} else {
|
|
if (id == 0 && num_regions >= MAX_REGION_ENTRIES) return NULL; // full!
|
|
|
|
region = ®ions[num_regions++]; // alloc new RegionEntry
|
|
region->flags = REGION_DENY_FLOOD; // DENY by default
|
|
region->id = id == 0 ? next_id++ : id;
|
|
StrHelper::strncpy(region->name, name, sizeof(region->name));
|
|
region->parent = parent_id;
|
|
}
|
|
return region;
|
|
}
|
|
|
|
int RegionMap::getTransportKeysFor(const RegionEntry& src, TransportKey dest[], int max_num) {
|
|
int num;
|
|
if (src.name[0] == '$') { // private region
|
|
num = _store->loadKeysFor(src.id, dest, max_num);
|
|
} else if (src.name[0] == '#') { // auto hashtag region
|
|
_store->getAutoKeyFor(src.id, src.name, dest[0]);
|
|
num = 1;
|
|
} else { // new: implicit auto hashtag region
|
|
char tmp[sizeof(src.name)+1];
|
|
tmp[0] = '#';
|
|
memcpy(&tmp[1], src.name, sizeof(src.name) - 1); // ZEPHCORE: bounded, the name
|
|
tmp[sizeof(src.name)] = 0; // may fill all 31 bytes
|
|
_store->getAutoKeyFor(src.id, tmp, dest[0]);
|
|
num = 1;
|
|
}
|
|
return num;
|
|
}
|
|
|
|
RegionEntry* RegionMap::findMatch(mesh::Packet* packet, uint8_t mask) {
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto region = ®ions[i];
|
|
if ((region->flags & mask) == 0) { // does region allow this? (per 'mask' param)
|
|
TransportKey keys[4];
|
|
int num = getTransportKeysFor(*region, keys, 4);
|
|
for (int j = 0; j < num; j++) {
|
|
uint16_t code = keys[j].calcTransportCode(packet);
|
|
if (packet->transport_codes[0] == code) { // a match!!
|
|
return region;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return NULL; // no matches
|
|
}
|
|
|
|
RegionEntry* RegionMap::findByName(const char* name) {
|
|
if (strcmp(name, "*") == 0) return &wildcard;
|
|
|
|
if (*name == '#') { name++; } // ignore the '#' when matching by name
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto region = ®ions[i];
|
|
if (strcmp(name, skip_hash(region->name)) == 0) return region;
|
|
}
|
|
return NULL; // not found
|
|
}
|
|
|
|
RegionEntry* RegionMap::findByNamePrefix(const char* prefix) {
|
|
if (strcmp(prefix, "*") == 0) return &wildcard;
|
|
|
|
if (*prefix == '#') { prefix++; } // ignore the '#' when matching by name
|
|
RegionEntry* partial = NULL;
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto region = ®ions[i];
|
|
if (strcmp(prefix, skip_hash(region->name)) == 0) return region; // is a complete match, preference this one
|
|
if (memcmp(prefix, skip_hash(region->name), strlen(prefix)) == 0) {
|
|
partial = region;
|
|
}
|
|
}
|
|
return partial;
|
|
}
|
|
|
|
RegionEntry* RegionMap::findById(uint16_t id) {
|
|
if (id == 0) return &wildcard; // special root Region
|
|
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto region = ®ions[i];
|
|
if (region->id == id) return region;
|
|
}
|
|
return NULL; // not found
|
|
}
|
|
|
|
RegionEntry* RegionMap::getHomeRegion() {
|
|
return findById(home_id);
|
|
}
|
|
|
|
void RegionMap::setHomeRegion(const RegionEntry* home) {
|
|
home_id = home ? home->id : 0;
|
|
}
|
|
|
|
RegionEntry* RegionMap::getDefaultRegion() {
|
|
return default_id == 0 ? NULL : findById(default_id);
|
|
}
|
|
|
|
void RegionMap::setDefaultRegion(const RegionEntry* def) {
|
|
default_id = def ? def->id : 0;
|
|
}
|
|
|
|
bool RegionMap::removeRegion(const RegionEntry& region) {
|
|
if (region.id == 0) return false; // failed (cannot remove the wildcard Region)
|
|
|
|
int i; // first check region has no child regions
|
|
for (i = 0; i < num_regions; i++) {
|
|
if (regions[i].parent == region.id) return false; // failed (must remove child Regions first)
|
|
}
|
|
|
|
i = 0;
|
|
while (i < num_regions) {
|
|
if (region.id == regions[i].id) break;
|
|
i++;
|
|
}
|
|
if (i >= num_regions) return false; // failed (not found)
|
|
|
|
num_regions--; // remove from regions array
|
|
while (i < num_regions) {
|
|
regions[i] = regions[i + 1];
|
|
i++;
|
|
}
|
|
return true; // success
|
|
}
|
|
|
|
bool RegionMap::clear() {
|
|
num_regions = 0;
|
|
return true; // success
|
|
}
|
|
|
|
void RegionMap::printChildRegions(int indent, const RegionEntry* parent, Stream& out) const {
|
|
for (int i = 0; i < indent; i++) {
|
|
out.print(' ');
|
|
}
|
|
|
|
if (parent->flags & REGION_DENY_FLOOD) {
|
|
out.printf("%s%s\n", skip_hash(parent->name), parent->id == home_id ? "^" : "");
|
|
} else {
|
|
out.printf("%s%s F\n", skip_hash(parent->name), parent->id == home_id ? "^" : "");
|
|
}
|
|
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto r = ®ions[i];
|
|
if (r->parent == parent->id) {
|
|
printChildRegions(indent + 1, r, out);
|
|
}
|
|
}
|
|
}
|
|
|
|
void RegionMap::exportTo(Stream& out) const {
|
|
printChildRegions(0, &wildcard, out); // recursive
|
|
}
|
|
|
|
// ZEPHCORE: whole lines only. Upstream's BufStream cuts the last line mid-name
|
|
// when the map overflows the buffer; a cut name pasted back into `region load`
|
|
// would define a wrong region. A line is written only if it fits entirely.
|
|
class LineBufStream : public Stream {
|
|
public:
|
|
LineBufStream(char *buf, size_t max_len) : _buf(buf), _max_len(max_len), _pos(0), _line_len(0) {
|
|
if (_max_len > 0) _buf[0] = 0;
|
|
}
|
|
size_t write(uint8_t c) override {
|
|
if (_line_len < sizeof(_line)) _line[_line_len++] = c;
|
|
if (c == '\n') {
|
|
if (_pos + _line_len < _max_len) {
|
|
memcpy(&_buf[_pos], _line, _line_len);
|
|
_pos += _line_len;
|
|
_buf[_pos] = 0;
|
|
}
|
|
_line_len = 0;
|
|
}
|
|
return 1;
|
|
}
|
|
int available() override { return 0; }
|
|
int read() override { return -1; }
|
|
int peek() override { return -1; }
|
|
size_t length() const { return _pos; }
|
|
private:
|
|
char *_buf;
|
|
size_t _max_len;
|
|
size_t _pos;
|
|
char _line[48];
|
|
size_t _line_len;
|
|
};
|
|
|
|
size_t RegionMap::exportTo(char *dest, size_t max_len) const {
|
|
if (!dest || max_len == 0) return 0;
|
|
|
|
LineBufStream bs(dest, max_len); // ZEPHCORE: was BufStream
|
|
exportTo(bs); // ← reuse existing logic
|
|
return bs.length();
|
|
}
|
|
|
|
int RegionMap::exportNamesTo(char *dest, int max_len, uint8_t mask, bool invert) {
|
|
char *dp = dest;
|
|
|
|
// Check wildcard region
|
|
bool wildcard_matches = invert ? (wildcard.flags & mask) : !(wildcard.flags & mask);
|
|
if (wildcard_matches) {
|
|
*dp++ = '*';
|
|
*dp++ = ',';
|
|
}
|
|
|
|
for (int i = 0; i < num_regions; i++) {
|
|
auto region = ®ions[i];
|
|
|
|
// Check if region matches the filter criteria
|
|
bool region_matches = invert ? (region->flags & mask) : !(region->flags & mask);
|
|
|
|
if (region_matches) {
|
|
int len = strlen(skip_hash(region->name));
|
|
if ((dp - dest) + len + 2 < max_len) { // only append if name will fit
|
|
memcpy(dp, skip_hash(region->name), len);
|
|
dp += len;
|
|
*dp++ = ',';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dp > dest) { dp--; } // don't include trailing comma
|
|
|
|
*dp = 0; // set null terminator
|
|
return dp - dest; // return length
|
|
}
|