Merge upstream dev companion CLI updates
This commit is contained in:
commit
2143d3e04b
46 files changed
+1976
-163
No files matched your search
@@ -159,6 +159,13 @@ public:
|
||||
virtual bool isPowerManagementInitialized() const { return false; }
|
||||
virtual bool supportsVoltageWake() const { return false; }
|
||||
|
||||
virtual bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply) {
|
||||
(void)command;
|
||||
(void)sender_timestamp;
|
||||
(void)reply;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline static uint32_t n_cad_busy = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -264,6 +264,15 @@ bool Utils::isHexChar(char c) {
|
||||
return c == '0' || hexVal(c) > 0;
|
||||
}
|
||||
|
||||
bool Utils::isZeroes(const uint8_t* buf, size_t len) {
|
||||
while (len > 0) {
|
||||
if (*buf != 0) return false;
|
||||
buf++;
|
||||
len--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Utils::fromHex(uint8_t* dest, int dest_size, const char *src_hex) {
|
||||
if (dest == NULL || src_hex == NULL || dest_size < 0) return false;
|
||||
int len = strlen(src_hex);
|
||||
|
||||
@@ -82,6 +82,8 @@ public:
|
||||
static int parseTextParts(char* text, const char* parts[], int max_num, char separator=',');
|
||||
|
||||
static bool isHexChar(char c);
|
||||
|
||||
static bool isZeroes(const uint8_t* buf, size_t len);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -28,6 +28,14 @@
|
||||
return i;
|
||||
}
|
||||
|
||||
bool AdvertDataParser::isValidName(const char *n) {
|
||||
while (*n) {
|
||||
if (*n == '[' || *n == ']' || *n == '\\' || *n == ':' || *n == ',' || *n == '?' || *n == '*') return false;
|
||||
n++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
AdvertDataParser::AdvertDataParser(const uint8_t app_data[], uint8_t app_data_len) {
|
||||
_name[0] = 0;
|
||||
_lat = _lon = 0;
|
||||
|
||||
@@ -50,6 +50,8 @@ class AdvertDataParser {
|
||||
public:
|
||||
AdvertDataParser(const uint8_t app_data[], uint8_t app_data_len);
|
||||
|
||||
static bool isValidName(const char* name);
|
||||
|
||||
bool isValid() const { return _valid; }
|
||||
uint8_t getType() const { return _flags & 0x0F; }
|
||||
uint16_t getFeat1() const { return _extra1; }
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#define TXT_ACK_DELAY 200
|
||||
#endif
|
||||
|
||||
#define CLI_REPLY_DELAY_MILLIS 600
|
||||
|
||||
bool BaseChatMesh::sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis) {
|
||||
return sendFlood(pkt, delay_millis);
|
||||
}
|
||||
@@ -274,8 +276,8 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender
|
||||
ContactInfo& from = contacts[i];
|
||||
|
||||
if (type == PAYLOAD_TYPE_TXT_MSG && len > 5) {
|
||||
uint32_t timestamp;
|
||||
memcpy(×tamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
|
||||
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
|
||||
|
||||
// len can be > original length, but 'text' will be padded with zeroes
|
||||
@@ -283,7 +285,7 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender
|
||||
|
||||
if (flags == TXT_TYPE_PLAIN) {
|
||||
from.lastmod = getRTCClock()->getCurrentTime(); // update last heard time
|
||||
onMessageRecv(from, packet, timestamp, (const char *) &data[5]); // let UI know
|
||||
onMessageRecv(from, packet, sender_timestamp, (const char *) &data[5]); // let UI know
|
||||
|
||||
int text_len = strlen((char *)&data[5]);
|
||||
uint8_t ack_hash[6]; // calc truncated hash of the message timestamp + text + sender pub_key, to prove to sender that we got it
|
||||
@@ -301,20 +303,50 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender
|
||||
sendAckTo(from, ack_hash, 6);
|
||||
}
|
||||
} else if (flags == TXT_TYPE_CLI_DATA) {
|
||||
onCommandDataRecv(from, packet, timestamp, (const char *) &data[5]); // let UI know
|
||||
char *text = (char *)&data[5];
|
||||
onCommandDataRecv(from, packet, sender_timestamp, text); // let UI know
|
||||
// NOTE: no ack expected for CLI_DATA replies
|
||||
|
||||
if (packet->isRouteFlood()) {
|
||||
// let this sender know path TO here, so they can use sendDirect() (NOTE: no ACK as extra)
|
||||
mesh::Packet* path = createPathReturn(from.id, secret, packet->path, packet->path_len, 0, NULL, 0);
|
||||
// Let this sender learn a direct path without adding an ACK.
|
||||
mesh::Packet* path = createPathReturn(from.id, secret, packet->path,
|
||||
packet->path_len, 0, NULL, 0);
|
||||
if (path) sendFloodScoped(from, path);
|
||||
}
|
||||
} else if (flags == TXT_TYPE_CLI_COMMAND) {
|
||||
uint8_t temp[166];
|
||||
char *command = (char *)&data[5];
|
||||
char *reply = (char *)&temp[5];
|
||||
*reply = 0;
|
||||
|
||||
onCLICommandRecv(from, packet, sender_timestamp, command, reply); // let UI know
|
||||
// NOTE: no ack expected for CLI_COMMAND replies
|
||||
|
||||
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);
|
||||
temp[4] = (TXT_TYPE_CLI_DATA << 2);
|
||||
|
||||
auto reply_pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, from.id, secret, temp, 5 + text_len);
|
||||
if (reply_pkt) {
|
||||
if (from.out_path_len == OUT_PATH_UNKNOWN) {
|
||||
sendFloodScoped(from, reply_pkt, CLI_REPLY_DELAY_MILLIS);
|
||||
} else {
|
||||
sendDirect(reply_pkt, from.out_path, from.out_path_len, CLI_REPLY_DELAY_MILLIS);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (flags == TXT_TYPE_SIGNED_PLAIN) {
|
||||
if (timestamp > from.sync_since) { // make sure 'sync_since' is up-to-date
|
||||
from.sync_since = timestamp;
|
||||
if (sender_timestamp > from.sync_since) { // make sure 'sync_since' is up-to-date
|
||||
from.sync_since = sender_timestamp;
|
||||
}
|
||||
from.lastmod = getRTCClock()->getCurrentTime(); // update last heard time
|
||||
onSignedMessageRecv(from, packet, timestamp, &data[5], (const char *) &data[9]); // let UI know
|
||||
onSignedMessageRecv(from, packet, sender_timestamp, &data[5], (const char *) &data[9]); // let UI know
|
||||
|
||||
uint32_t ack_hash; // calc truncated hash of the message timestamp + text + OUR pub_key, to prove to sender that we got it
|
||||
mesh::Utils::sha256((uint8_t *) &ack_hash, 4, data, 9 + strlen((char *)&data[9]), self_id.pub_key, PUB_KEY_SIZE);
|
||||
@@ -535,15 +567,18 @@ int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint32_t timestamp,
|
||||
|
||||
int BaseChatMesh::sendCommandData(const ContactInfo& recipient,
|
||||
uint32_t timestamp, uint8_t attempt,
|
||||
const char* text, uint32_t& est_timeout,
|
||||
uint8_t txt_type, const char* text,
|
||||
uint32_t& est_timeout,
|
||||
uint32_t logical_request_id) {
|
||||
est_timeout = 0;
|
||||
int text_len = strlen(text);
|
||||
if (text_len > MAX_TEXT_LEN) return MSG_SEND_FAILED;
|
||||
const size_t max_text_len = logical_request_id == 0
|
||||
? MAX_TEXT_LEN : MAX_CORRELATED_CLI_TEXT_LEN;
|
||||
if ((size_t)text_len > max_text_len) return MSG_SEND_FAILED;
|
||||
|
||||
uint8_t temp[5 + MAX_TEXT_LEN + mesh::RemoteCliRequest::EXTENSION_SIZE];
|
||||
memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique
|
||||
temp[4] = (attempt & 3) | (TXT_TYPE_CLI_DATA << 2);
|
||||
temp[4] = (attempt & 3) | (txt_type << 2);
|
||||
memcpy(&temp[5], text, text_len);
|
||||
|
||||
size_t payload_len = 5 + text_len;
|
||||
|
||||
@@ -3,10 +3,17 @@
|
||||
#include <Arduino.h> // needed for PlatformIO
|
||||
#include <Mesh.h>
|
||||
#include <helpers/AdvertDataHelpers.h>
|
||||
#include <helpers/RemoteCliRequest.h>
|
||||
#include <helpers/TxtDataHelpers.h>
|
||||
|
||||
#define MAX_TEXT_LEN (10*CIPHER_BLOCK_SIZE) // must be LESS than (MAX_PACKET_PAYLOAD - 4 - CIPHER_MAC_SIZE - 1)
|
||||
|
||||
static constexpr size_t MAX_CORRELATED_CLI_TEXT_LEN =
|
||||
MAX_PACKET_PAYLOAD - CIPHER_MAC_SIZE - (CIPHER_BLOCK_SIZE - 1)
|
||||
- 5 - mesh::RemoteCliRequest::EXTENSION_SIZE;
|
||||
static_assert(MAX_CORRELATED_CLI_TEXT_LEN <= MAX_TEXT_LEN,
|
||||
"correlated CLI text must fit the encrypted datagram");
|
||||
|
||||
#include "ContactInfo.h"
|
||||
|
||||
#define MAX_SEARCH_RESULTS 8
|
||||
@@ -139,6 +146,7 @@ protected:
|
||||
virtual bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len);
|
||||
virtual void onMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
|
||||
virtual void onCommandDataRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) = 0;
|
||||
virtual void onCLICommandRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text, char* reply) = 0;
|
||||
virtual void onSignedMessageRecv(const ContactInfo& contact, mesh::Packet* pkt, uint32_t sender_timestamp, const uint8_t *sender_prefix, const char *text) = 0;
|
||||
virtual uint32_t calcFloodTimeoutMillisFor(uint32_t pkt_airtime_millis) const = 0;
|
||||
virtual uint32_t calcDirectTimeoutMillisFor(uint32_t pkt_airtime_millis, uint8_t path_len) const = 0;
|
||||
@@ -185,7 +193,7 @@ public:
|
||||
const uint8_t* replace_retry_key = NULL,
|
||||
const uint8_t* message_retry_key = NULL);
|
||||
int sendCommandData(const ContactInfo& recipient, uint32_t timestamp,
|
||||
uint8_t attempt, const char* text,
|
||||
uint8_t attempt, uint8_t txt_type, const char* text,
|
||||
uint32_t& est_timeout,
|
||||
uint32_t logical_request_id = 0);
|
||||
bool sendGroupMessage(uint32_t timestamp, mesh::GroupChannel& channel, const char* sender_name, const char* text, int text_len);
|
||||
|
||||
@@ -2352,6 +2352,7 @@ void CommonCLI::savePrefs(PrefsSaveRouting::Scope scope) {
|
||||
_callbacks->updateAdvertTimer();
|
||||
}
|
||||
_callbacks->savePrefs(scope);
|
||||
_prefs->clearDirty();
|
||||
}
|
||||
|
||||
void CommonCLI::saveObserverPrefs() {
|
||||
@@ -2854,6 +2855,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
_callbacks->formatRadioStatsReply(reply);
|
||||
} else if (sender_timestamp == 0 && memcmp(command, "stats-core", 10) == 0 && (command[10] == 0 || command[10] == ' ')) {
|
||||
_callbacks->formatStatsReply(reply);
|
||||
} else if (_board->handleCommand(command, sender_timestamp, reply)) {
|
||||
if (_prefs->isDirty()) savePrefs();
|
||||
} else {
|
||||
strcpy(reply, "Unknown command");
|
||||
}
|
||||
@@ -4054,16 +4057,6 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
*dp = 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "path.hash.mode ", 15) == 0) {
|
||||
config += 15;
|
||||
uint8_t mode = atoi(config);
|
||||
if (mode < 3) {
|
||||
_prefs->path_hash_mode = mode;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0,1, or 2");
|
||||
}
|
||||
} else if (memcmp(config, "loop.detect ", 12) == 0) {
|
||||
config += 12;
|
||||
uint8_t mode;
|
||||
|
||||
+48
-2
@@ -10,6 +10,8 @@
|
||||
#include <helpers/RegionMap.h>
|
||||
#include <helpers/ConfigSerializer.h>
|
||||
#include <helpers/bridges/ESPNowBridgeFormat.h>
|
||||
#include <helpers/CommonRadioPrefs.h>
|
||||
#include <helpers/DynamicConfigSerializer.h>
|
||||
|
||||
#ifndef DEFAULT_CAD_ENABLED
|
||||
#define DEFAULT_CAD_ENABLED 0
|
||||
@@ -240,7 +242,7 @@ public:
|
||||
uint8_t system_watchdog_enabled = 0; // nRF52 main-loop hardware watchdog (boolean; default on)
|
||||
|
||||
private:
|
||||
class RadioPrefs : public ConfigSerializer {
|
||||
class RadioPrefs : public CommonRadioPrefs {
|
||||
NodePrefs* _parent;
|
||||
|
||||
protected:
|
||||
@@ -266,6 +268,41 @@ private:
|
||||
|
||||
public:
|
||||
explicit RadioPrefs(NodePrefs* parent) : _parent(parent) { }
|
||||
// CommonRadioPrefs interface
|
||||
float getFreq() const override { return _parent->freq; }
|
||||
void setFreq(float f) override { _parent->freq = f; markDirty(); }
|
||||
float getBandwidth() const override { return _parent->bw; }
|
||||
void setBandwidth(float bw) override { _parent->bw = bw; markDirty(); }
|
||||
uint8_t getSpreadFactor() const override { return _parent->sf; }
|
||||
void setSpreadFactor(uint8_t sf) override { _parent->sf = sf; markDirty(); }
|
||||
uint8_t getCodingRate() const override { return _parent->cr; }
|
||||
void setCodingRate(uint8_t cr) override { _parent->cr = cr; markDirty(); }
|
||||
float getAirtimeFactor() const override { return _parent->airtime_factor; }
|
||||
void setAirtimeFactor(float af) override { _parent->airtime_factor = af; markDirty(); }
|
||||
bool isCadEnabled() const override { return _parent->cad_enabled; }
|
||||
void setCadEnabled(bool en) override { _parent->cad_enabled = en; markDirty(); }
|
||||
uint8_t getIntThresh() const override { return _parent->interference_threshold; }
|
||||
void setIntThresh(uint8_t t) override { _parent->interference_threshold = t; markDirty(); }
|
||||
uint8_t getRxGain() const override { return _parent->rx_boosted_gain; }
|
||||
void setRxGain(uint8_t g) override { _parent->rx_boosted_gain = g; markDirty(); }
|
||||
int8_t getTxPower() const override { return _parent->tx_power_dbm; }
|
||||
void setTxPower(int8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
|
||||
float getRxDelay() const override { return _parent->rx_delay_base; }
|
||||
void setRxDelay(float d) override { _parent->rx_delay_base = d; markDirty(); }
|
||||
uint8_t getAgcResetInt() const override { return _parent->agc_reset_interval * 4; }
|
||||
void setAgcResetInt(uint8_t secs) override { _parent->agc_reset_interval = secs / 4; markDirty(); }
|
||||
uint8_t getHashMode() const override { return _parent->path_hash_mode; }
|
||||
void setHashMode(uint8_t m) override { _parent->path_hash_mode = m; markDirty(); }
|
||||
uint8_t getMultiAcks() const override { return _parent->multi_acks; }
|
||||
void setMultiAcks(uint8_t m) override { _parent->multi_acks = m; markDirty(); }
|
||||
float getFloodTxDelay() const override { return _parent->tx_delay_factor; }
|
||||
void setFloodTxDelay(float d) override { _parent->tx_delay_factor = d; markDirty(); }
|
||||
float getDirectTxDelay() const override { return _parent->direct_tx_delay_factor; }
|
||||
void setDirectTxDelay(float d) override { _parent->direct_tx_delay_factor = d; markDirty(); }
|
||||
uint8_t getFEMRxGain() const override { return _parent->radio_fem_rxgain; }
|
||||
void setFEMRxGain(uint8_t g) override { _parent->radio_fem_rxgain = g; markDirty(); }
|
||||
uint8_t getFEMTxGain() const override { return _parent->radio_fem_txgain; }
|
||||
void setFEMTxGain(uint8_t g) override { _parent->radio_fem_txgain = g; markDirty(); }
|
||||
};
|
||||
RadioPrefs radio;
|
||||
|
||||
@@ -349,6 +386,8 @@ private:
|
||||
};
|
||||
RoomPrefs room;
|
||||
|
||||
DynamicConfigSerializer custom;
|
||||
|
||||
protected:
|
||||
void structure() override {
|
||||
def("name", node_name, sizeof(node_name));
|
||||
@@ -366,12 +405,19 @@ protected:
|
||||
def("repeat", repeat);
|
||||
def("room", room);
|
||||
def("power", power);
|
||||
def("custom", custom);
|
||||
}
|
||||
|
||||
public:
|
||||
NodePrefs()
|
||||
: ConfigSerializer(), radio(this), bridge(this), gps(this), power(this),
|
||||
repeat(this), room(this) { }
|
||||
repeat(this), room(this), custom(&radio) { }
|
||||
|
||||
CommonRadioPrefs* getRadioPrefs() { return &radio; }
|
||||
KeyValueStore* getCustom() { return &custom; }
|
||||
|
||||
bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); }
|
||||
void clearDirty() override { ConfigSerializer::clearDirty(); radio.clearDirty(); custom.clearDirty(); }
|
||||
};
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
@@ -0,0 +1,433 @@
|
||||
#include "CommonRadioPrefs.h"
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "target.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr size_t RADIO_ARGS_CAPACITY = 132;
|
||||
|
||||
size_t boundedLength(const char* text, size_t capacity) {
|
||||
if (text == nullptr) return capacity;
|
||||
size_t length = 0;
|
||||
while (length < capacity && text[length] != 0) length++;
|
||||
return length;
|
||||
}
|
||||
|
||||
bool startsWith(const char* text, const char* prefix) {
|
||||
if (text == nullptr || prefix == nullptr) return false;
|
||||
const size_t prefix_len = strlen(prefix);
|
||||
return strncmp(text, prefix, prefix_len) == 0;
|
||||
}
|
||||
|
||||
bool parseIntStrict(const char* text, int32_t min_value, int32_t max_value,
|
||||
int32_t& result) {
|
||||
if (text == nullptr || *text == 0 || min_value > max_value) return false;
|
||||
|
||||
bool negative = false;
|
||||
if (*text == '-') {
|
||||
negative = true;
|
||||
text++;
|
||||
}
|
||||
if (*text == 0) return false;
|
||||
|
||||
int64_t magnitude = 0;
|
||||
while (*text != 0) {
|
||||
if (*text < '0' || *text > '9') return false;
|
||||
magnitude = magnitude * 10 + (*text++ - '0');
|
||||
if (magnitude > 2147483648LL) return false;
|
||||
}
|
||||
|
||||
const int64_t parsed = negative ? -magnitude : magnitude;
|
||||
if (parsed < min_value || parsed > max_value) return false;
|
||||
result = static_cast<int32_t>(parsed);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Accept only a plain fixed-point decimal. Exponents, NaN/Inf, whitespace,
|
||||
// and trailing characters are rejected so malformed CLI values never become
|
||||
// zero through atoi()/atof() fallback behavior.
|
||||
bool parseDecimalStrict(const char* text, float& result) {
|
||||
if (text == nullptr || *text == 0) return false;
|
||||
|
||||
const char* cursor = text;
|
||||
bool negative = false;
|
||||
if (*cursor == '-') {
|
||||
negative = true;
|
||||
cursor++;
|
||||
}
|
||||
|
||||
bool saw_digit = false;
|
||||
double value = 0.0;
|
||||
while (*cursor >= '0' && *cursor <= '9') {
|
||||
value = value * 10.0 + (*cursor++ - '0');
|
||||
if (!isfinite(value)) return false;
|
||||
saw_digit = true;
|
||||
}
|
||||
|
||||
if (*cursor == '.') {
|
||||
cursor++;
|
||||
double place = 0.1;
|
||||
while (*cursor >= '0' && *cursor <= '9') {
|
||||
value += (*cursor++ - '0') * place;
|
||||
place *= 0.1;
|
||||
saw_digit = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!saw_digit || *cursor != 0) return false;
|
||||
value = negative ? -value : value;
|
||||
if (!isfinite(value) || value > 3.402823466e+38
|
||||
|| value < -3.402823466e+38) {
|
||||
return false;
|
||||
}
|
||||
|
||||
result = static_cast<float>(value);
|
||||
return isfinite(result);
|
||||
}
|
||||
|
||||
bool splitRadioArgs(char* text, const char* parts[4]) {
|
||||
if (text == nullptr || *text == 0) return false;
|
||||
|
||||
size_t count = 0;
|
||||
char* field = text;
|
||||
while (true) {
|
||||
if (*field == 0 || count >= 4) return false;
|
||||
parts[count++] = field;
|
||||
|
||||
char* comma = strchr(field, ',');
|
||||
if (comma == nullptr) break;
|
||||
*comma = 0;
|
||||
field = comma + 1;
|
||||
}
|
||||
return count == 4;
|
||||
}
|
||||
|
||||
bool bwMatches(float bw, float allowed) {
|
||||
return fabsf(bw - allowed) <= 0.001f;
|
||||
}
|
||||
|
||||
bool isValidLoRaBandwidth(float bw) {
|
||||
#if defined(USE_LR1110)
|
||||
return bwMatches(bw, 62.5f)
|
||||
|| bwMatches(bw, 125.0f)
|
||||
|| bwMatches(bw, 250.0f)
|
||||
|| bwMatches(bw, 500.0f);
|
||||
#elif defined(USE_LLCC68) || defined(USE_SX1272)
|
||||
return bwMatches(bw, 125.0f)
|
||||
|| bwMatches(bw, 250.0f)
|
||||
|| bwMatches(bw, 500.0f);
|
||||
#else
|
||||
return bwMatches(bw, 7.8f)
|
||||
|| bwMatches(bw, 10.4f)
|
||||
|| bwMatches(bw, 15.6f)
|
||||
|| bwMatches(bw, 20.8f)
|
||||
|| bwMatches(bw, 31.25f)
|
||||
|| bwMatches(bw, 41.7f)
|
||||
|| bwMatches(bw, 62.5f)
|
||||
|| bwMatches(bw, 125.0f)
|
||||
|| bwMatches(bw, 250.0f)
|
||||
|| bwMatches(bw, 500.0f);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool copyValue(char* destination, size_t capacity, const char* value) {
|
||||
if (destination == nullptr || capacity == 0 || value == nullptr) return false;
|
||||
const size_t value_len = strlen(value);
|
||||
if (value_len >= capacity) {
|
||||
destination[0] = 0;
|
||||
return false;
|
||||
}
|
||||
memcpy(destination, value, value_len + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool CommonRadioPrefs::getByKey(const char* key, char* value, size_t max_len) {
|
||||
if (key == nullptr || value == nullptr || max_len == 0) return false;
|
||||
|
||||
if (strcmp(key, "fem_rxgain") == 0) {
|
||||
return copyValue(value, max_len, getFEMRxGain() == 0 ? "0" : "1");
|
||||
}
|
||||
if (strcmp(key, "fem_txgain") == 0) {
|
||||
return copyValue(value, max_len, getFEMTxGain() == 0 ? "0" : "1");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CommonRadioPrefs::setByKey(const char* key, const char* value) {
|
||||
if (key == nullptr || value == nullptr
|
||||
|| (strcmp(value, "0") != 0 && strcmp(value, "1") != 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint8_t enabled = strcmp(value, "1") == 0 ? 1 : 0;
|
||||
if (strcmp(key, "fem_rxgain") == 0) {
|
||||
setFEMRxGain(enabled);
|
||||
markDirty();
|
||||
return true;
|
||||
}
|
||||
if (strcmp(key, "fem_txgain") == 0) {
|
||||
setFEMTxGain(enabled);
|
||||
markDirty();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timestamp, char* reply) {
|
||||
(void)sender_timestamp;
|
||||
if (command == nullptr || reply == nullptr) return false;
|
||||
|
||||
if (strcmp(command, "get radio") == 0) {
|
||||
char freq[16], bw[16];
|
||||
snprintf(freq, sizeof(freq), "%s", StrHelper::ftoa(getFreq()));
|
||||
snprintf(bw, sizeof(bw), "%s", StrHelper::ftoa3(getBandwidth()));
|
||||
sprintf(reply, "> %s,%s,%d,%d", freq, bw, (uint32_t)getSpreadFactor(), (uint32_t)getCodingRate());
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set radio ")) {
|
||||
const char* args = command + strlen("set radio ");
|
||||
const size_t args_len = boundedLength(args, RADIO_ARGS_CAPACITY);
|
||||
if (args_len >= RADIO_ARGS_CAPACITY) {
|
||||
strcpy(reply, "Error, invalid radio params");
|
||||
return true;
|
||||
}
|
||||
|
||||
char tmp[RADIO_ARGS_CAPACITY];
|
||||
memcpy(tmp, args, args_len + 1);
|
||||
const char *parts[4];
|
||||
float freq = 0.0f;
|
||||
float bw = 0.0f;
|
||||
int32_t sf = 0;
|
||||
int32_t cr = 0;
|
||||
if (splitRadioArgs(tmp, parts)
|
||||
&& parseDecimalStrict(parts[0], freq)
|
||||
&& parseDecimalStrict(parts[1], bw)
|
||||
&& parseIntStrict(parts[2], 5, 12, sf)
|
||||
&& parseIntStrict(parts[3], 5, 8, cr)
|
||||
&& freq >= 150.0f && freq <= 2500.0f
|
||||
&& isValidLoRaBandwidth(bw)) {
|
||||
setSpreadFactor(static_cast<uint8_t>(sf));
|
||||
setCodingRate(static_cast<uint8_t>(cr));
|
||||
setFreq(freq);
|
||||
setBandwidth(bw);
|
||||
strcpy(reply, "OK - reboot to apply");
|
||||
} else {
|
||||
strcpy(reply, "Error, invalid radio params");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get freq") == 0) {
|
||||
sprintf(reply, "> %s", StrHelper::ftoa(getFreq()));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get af") == 0) {
|
||||
sprintf(reply, "> %s", StrHelper::ftoa(getAirtimeFactor()));
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set af ")) {
|
||||
float factor = 0.0f;
|
||||
if (parseDecimalStrict(command + strlen("set af "), factor)
|
||||
&& factor >= 0.0f && factor <= 9.0f) {
|
||||
setAirtimeFactor(factor);
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, invalid airtime factor");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get dutycycle") == 0) {
|
||||
const float factor = getAirtimeFactor();
|
||||
if (!isfinite(factor) || factor < 0.0f) {
|
||||
strcpy(reply, "Error: invalid airtime factor");
|
||||
} else {
|
||||
const int tenths = static_cast<int>(1000.0f / (factor + 1.0f) + 0.5f);
|
||||
sprintf(reply, "> %d.%d%%", tenths / 10, tenths % 10);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set dutycycle ")) {
|
||||
float dc = 0.0f;
|
||||
if (!parseDecimalStrict(command + strlen("set dutycycle "), dc)
|
||||
|| dc < 1.0f || dc > 100.0f) {
|
||||
strcpy(reply, "ERROR: dutycycle must be 1-100");
|
||||
} else {
|
||||
setAirtimeFactor((100.0f / dc) - 1.0f);
|
||||
const float actual = 100.0f / (getAirtimeFactor() + 1.0f);
|
||||
const int tenths = static_cast<int>(actual * 10.0f + 0.5f);
|
||||
sprintf(reply, "OK - %d.%d%%", tenths / 10, tenths % 10);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get int.thresh") == 0) {
|
||||
sprintf(reply, "> %d", (uint32_t) getIntThresh());
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set int.thresh ")) {
|
||||
int32_t threshold = 0;
|
||||
if (parseIntStrict(command + strlen("set int.thresh "), 0, 255, threshold)) {
|
||||
setIntThresh(static_cast<uint8_t>(threshold));
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0-255");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get cad") == 0) {
|
||||
sprintf(reply, "> %s", isCadEnabled() ? "on" : "off");
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set cad ")) {
|
||||
const char* value = command + strlen("set cad ");
|
||||
if (strcmp(value, "on") == 0 || strcmp(value, "off") == 0) {
|
||||
setCadEnabled(strcmp(value, "on") == 0);
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error: use set cad on|off");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get radio.rxgain") == 0) {
|
||||
sprintf(reply, "> %s", getRxGain() != 0 ? "on" : "off");
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set radio.rxgain ")) {
|
||||
const char* value = command + strlen("set radio.rxgain ");
|
||||
if (strcmp(value, "on") != 0 && strcmp(value, "off") != 0) {
|
||||
strcpy(reply, "Error: use set radio.rxgain on|off");
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool enabled = strcmp(value, "on") == 0;
|
||||
if (radio_driver.setRxBoostedGainMode(enabled)) {
|
||||
setRxGain(enabled ? 1 : 0);
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error: unsupported");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get tx") == 0) {
|
||||
sprintf(reply, "> %d", static_cast<int>(getTxPower()));
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set tx ")) {
|
||||
int32_t dbm = 0;
|
||||
if (!parseIntStrict(command + strlen("set tx "), -128, 127, dbm)) {
|
||||
strcpy(reply, "Error: invalid TX power");
|
||||
} else if (!radio_driver.setTxPower(static_cast<int8_t>(dbm))) {
|
||||
strcpy(reply, "Error: TX power rejected");
|
||||
} else {
|
||||
setTxPower(static_cast<int8_t>(dbm));
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get rxdelay") == 0) {
|
||||
sprintf(reply, "> %s", StrHelper::ftoa(getRxDelay()));
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set rxdelay ")) {
|
||||
float db = 0.0f;
|
||||
if (parseDecimalStrict(command + strlen("set rxdelay "), db)
|
||||
&& db >= 0.0f && db <= 20.0f) {
|
||||
setRxDelay(db);
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0-20");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get agc.reset.interval") == 0) {
|
||||
sprintf(reply, "> %d", (uint32_t) getAgcResetInt());
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set agc.reset.interval ")) {
|
||||
int32_t seconds = 0;
|
||||
if (parseIntStrict(command + strlen("set agc.reset.interval "), 0, 255, seconds)) {
|
||||
setAgcResetInt(static_cast<uint8_t>(seconds));
|
||||
sprintf(reply, "OK - interval rounded to %d", (uint32_t) getAgcResetInt());
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0-255");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get path.hash.mode") == 0) {
|
||||
sprintf(reply, "> %d", (uint32_t)getHashMode());
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set path.hash.mode ")) {
|
||||
int32_t mode = 0;
|
||||
if (parseIntStrict(command + strlen("set path.hash.mode "), 0, 2, mode)) {
|
||||
setHashMode(static_cast<uint8_t>(mode));
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0,1, or 2");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get multi.acks") == 0) {
|
||||
sprintf(reply, "> %d", (uint32_t) getMultiAcks());
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set multi.acks ")) {
|
||||
int32_t enabled = 0;
|
||||
if (parseIntStrict(command + strlen("set multi.acks "), 0, 1, enabled)) {
|
||||
setMultiAcks(static_cast<uint8_t>(enabled));
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0 or 1");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get txdelay") == 0) {
|
||||
sprintf(reply, "> %s", StrHelper::ftoa(getFloodTxDelay()));
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set txdelay ")) {
|
||||
float f = 0.0f;
|
||||
if (parseDecimalStrict(command + strlen("set txdelay "), f)
|
||||
&& f >= 0.0f && f <= 2.0f) {
|
||||
setFloodTxDelay(f);
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0-2");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get direct.txdelay") == 0) {
|
||||
sprintf(reply, "> %s", StrHelper::ftoa(getDirectTxDelay()));
|
||||
return true;
|
||||
}
|
||||
if (startsWith(command, "set direct.txdelay ")) {
|
||||
float f = 0.0f;
|
||||
if (parseDecimalStrict(command + strlen("set direct.txdelay "), f)
|
||||
&& f >= 0.0f && f <= 2.0f) {
|
||||
setDirectTxDelay(f);
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, must be 0-2");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // not handled
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
#include "ConfigSerializer.h"
|
||||
#include "KeyValueStore.h"
|
||||
|
||||
class CommonRadioPrefs : public ConfigSerializer, public KeyValueStore {
|
||||
bool _is_dirty = false;
|
||||
protected:
|
||||
CommonRadioPrefs() { }
|
||||
public:
|
||||
void markDirty() { _is_dirty = true; }
|
||||
void clearDirty() { _is_dirty = false; }
|
||||
bool isDirty() const { return _is_dirty; }
|
||||
|
||||
virtual float getFreq() const = 0;
|
||||
virtual void setFreq(float f) = 0;
|
||||
|
||||
virtual float getBandwidth() const = 0;
|
||||
virtual void setBandwidth(float bw) = 0;
|
||||
|
||||
virtual uint8_t getSpreadFactor() const = 0;
|
||||
virtual void setSpreadFactor(uint8_t sf) = 0;
|
||||
|
||||
virtual uint8_t getCodingRate() const = 0;
|
||||
virtual void setCodingRate(uint8_t cr) = 0;
|
||||
|
||||
virtual float getAirtimeFactor() const = 0;
|
||||
virtual void setAirtimeFactor(float af) = 0;
|
||||
|
||||
virtual bool isCadEnabled() const = 0;
|
||||
virtual void setCadEnabled(bool en) = 0;
|
||||
|
||||
virtual uint8_t getIntThresh() const = 0;
|
||||
virtual void setIntThresh(uint8_t t) = 0;
|
||||
|
||||
virtual uint8_t getRxGain() const = 0;
|
||||
virtual void setRxGain(uint8_t g) = 0;
|
||||
|
||||
virtual int8_t getTxPower() const = 0;
|
||||
virtual void setTxPower(int8_t dbm) = 0;
|
||||
|
||||
virtual float getRxDelay() const = 0;
|
||||
virtual void setRxDelay(float d) = 0;
|
||||
|
||||
virtual uint8_t getAgcResetInt() const = 0;
|
||||
virtual void setAgcResetInt(uint8_t secs) = 0;
|
||||
|
||||
virtual uint8_t getHashMode() const = 0;
|
||||
virtual void setHashMode(uint8_t m) = 0;
|
||||
|
||||
virtual uint8_t getMultiAcks() const = 0;
|
||||
virtual void setMultiAcks(uint8_t m) = 0;
|
||||
|
||||
virtual float getFloodTxDelay() const = 0;
|
||||
virtual void setFloodTxDelay(float d) = 0;
|
||||
|
||||
virtual float getDirectTxDelay() const = 0;
|
||||
virtual void setDirectTxDelay(float d) = 0;
|
||||
|
||||
virtual uint8_t getFEMRxGain() const = 0;
|
||||
virtual void setFEMRxGain(uint8_t g) = 0;
|
||||
|
||||
virtual uint8_t getFEMTxGain() const = 0;
|
||||
virtual void setFEMTxGain(uint8_t g) = 0;
|
||||
|
||||
bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply);
|
||||
|
||||
bool setByKey(const char* key, const char* value) override; // for dynamic key/value access
|
||||
bool getByKey(const char* key, char* value, size_t max_len) override;
|
||||
};
|
||||
@@ -90,6 +90,12 @@ int ConfigSerializer::Context::readNext() {
|
||||
case EXPECT_COMMA_OR_KEY:
|
||||
if (c == ',') { rd_mode = EXPECT_KEY; return TOK_WHITESPACE; }
|
||||
case EXPECT_KEY:
|
||||
// Empty objects are valid and are emitted by serializers such as the
|
||||
// dynamic `custom` preferences object before it has any entries.
|
||||
if (rd_len == 0 && c == '}') {
|
||||
rd_mode = EXPECT_COMMA_OR_KEY_OR_CLOSE;
|
||||
return TOK_END_OBJ;
|
||||
}
|
||||
if (rd_len > 0 && c == ':') { rd_buf[rd_len] = 0; rd_len = 0; rd_mode = EXPECT_VAL_OR_OBJ; return TOK_KEY; }
|
||||
if (rd_len == 0 && is_whitespace(c)) return TOK_WHITESPACE;
|
||||
if (rd_len < CONFIG_MAX_KEYLEN-1 && is_key_char(c)) { rd_buf[rd_len++] = c; return TOK_WHITESPACE; }
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
class ConfigSerializer {
|
||||
bool _first;
|
||||
int8_t _depth;
|
||||
bool _dirty = false;
|
||||
|
||||
protected:
|
||||
enum OP { READ, WRITE };
|
||||
|
||||
class Context {
|
||||
@@ -38,13 +40,14 @@ class ConfigSerializer {
|
||||
const char* getToken() const { return rd_buf; }
|
||||
bool keyMatch(int8_t depth, const char* key) { return strcmp(key, _keys[depth]) == 0; }
|
||||
void setKey(uint8_t depth, const char* key) { strcpy(_keys[depth], key); }
|
||||
const char* getKey(uint8_t depth) { return _keys[depth]; }
|
||||
};
|
||||
|
||||
Context* _context = NULL;
|
||||
int8_t getDepth() const { return _depth; }
|
||||
|
||||
void writeComma();
|
||||
|
||||
protected:
|
||||
ConfigSerializer() { }
|
||||
|
||||
void def(const char* key, char* value, size_t max_len); // max_len inclusive of null
|
||||
@@ -62,7 +65,12 @@ protected:
|
||||
|
||||
virtual void structure() = 0;
|
||||
|
||||
void markDirty() { _dirty = true; }
|
||||
|
||||
public:
|
||||
bool loadSerial(Stream& s);
|
||||
bool saveSerial(Stream& s);
|
||||
|
||||
virtual bool isDirty() const { return _dirty; }
|
||||
virtual void clearDirty() { _dirty = false; }
|
||||
};
|
||||
@@ -36,6 +36,12 @@ struct ContactInfo {
|
||||
return shared_secret;
|
||||
}
|
||||
|
||||
bool isFav() const { return flags & 0x01; }
|
||||
bool isTelemBaseAllowed() const { return flags & 0x02; }
|
||||
bool isTelemLocAllowed() const { return flags & 0x04; }
|
||||
bool isTelemEnvAllowed() const { return flags & 0x08; }
|
||||
bool isRemoteCLIAllowed() const { return flags & 0x10; }
|
||||
|
||||
private:
|
||||
mutable uint8_t shared_secret[PUB_KEY_SIZE];
|
||||
};
|
||||
@@ -0,0 +1,228 @@
|
||||
#include "DynamicConfigSerializer.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#define PROP_SEP_CHAR '|'
|
||||
#define KEY_SEP_CHAR ':'
|
||||
|
||||
namespace {
|
||||
|
||||
size_t boundedLength(const char* text, size_t capacity) {
|
||||
if (text == nullptr) return capacity;
|
||||
size_t length = 0;
|
||||
while (length < capacity && text[length] != 0) length++;
|
||||
return length;
|
||||
}
|
||||
|
||||
bool isValidKey(const char* key, size_t& key_len) {
|
||||
key_len = boundedLength(key, CONFIG_MAX_KEYLEN);
|
||||
if (key_len == 0 || key_len >= CONFIG_MAX_KEYLEN) return false;
|
||||
|
||||
for (size_t i = 0; i < key_len; i++) {
|
||||
const char c = key[i];
|
||||
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isValidValue(const char* value, size_t& value_len) {
|
||||
value_len = boundedLength(value, MAX_DYNAMIC_CONFG);
|
||||
if (value_len >= MAX_DYNAMIC_CONFG) return false;
|
||||
|
||||
for (size_t i = 0; i < value_len; i++) {
|
||||
const uint8_t c = static_cast<uint8_t>(value[i]);
|
||||
// ':' and '|' are internal delimiters. Control characters would produce
|
||||
// invalid serialized configuration because ConfigSerializer only escapes
|
||||
// a subset of them.
|
||||
if (c == static_cast<uint8_t>(KEY_SEP_CHAR)
|
||||
|| c == static_cast<uint8_t>(PROP_SEP_CHAR)
|
||||
|| c < 0x20 || c == 0x7F) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool appendEntry(char* destination, size_t capacity, size_t& used,
|
||||
const char* key, size_t key_len,
|
||||
const char* value, size_t value_len) {
|
||||
if (destination == nullptr || key == nullptr || value == nullptr
|
||||
|| used >= capacity) {
|
||||
return false;
|
||||
}
|
||||
const size_t separator_len = used == 0 ? 0 : 1;
|
||||
const size_t available = capacity - used;
|
||||
if (separator_len + key_len + 1 + value_len + 1 > available) return false;
|
||||
|
||||
if (separator_len != 0) destination[used++] = PROP_SEP_CHAR;
|
||||
memcpy(destination + used, key, key_len);
|
||||
used += key_len;
|
||||
destination[used++] = KEY_SEP_CHAR;
|
||||
memcpy(destination + used, value, value_len);
|
||||
used += value_len;
|
||||
destination[used] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool DynamicConfigSerializer::setByKeyPrv(const char* key, const char* value) {
|
||||
size_t key_len = 0;
|
||||
size_t value_len = 0;
|
||||
if (!isValidKey(key, key_len) || !isValidValue(value, value_len)) return false;
|
||||
|
||||
if (_fallback && _fallback->setByKey(key, value)) return true;
|
||||
|
||||
const size_t config_len = boundedLength(_config, sizeof(_config));
|
||||
if (config_len >= sizeof(_config)) return false;
|
||||
|
||||
char tmp[MAX_DYNAMIC_CONFG];
|
||||
memcpy(tmp, _config, config_len + 1);
|
||||
|
||||
char new_config[MAX_DYNAMIC_CONFG];
|
||||
new_config[0] = 0;
|
||||
size_t used = 0;
|
||||
|
||||
char* item = tmp;
|
||||
while (*item != 0) {
|
||||
char* next = strchr(item, PROP_SEP_CHAR);
|
||||
if (next != nullptr) *next = 0;
|
||||
|
||||
char* item_separator = strchr(item, KEY_SEP_CHAR);
|
||||
if (item_separator == nullptr || item_separator == item) return false;
|
||||
*item_separator = 0;
|
||||
|
||||
size_t item_key_len = 0;
|
||||
size_t item_value_len = 0;
|
||||
const char* item_value = item_separator + 1;
|
||||
if (!isValidKey(item, item_key_len)
|
||||
|| !isValidValue(item_value, item_value_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool replacing = item_key_len == key_len
|
||||
&& memcmp(item, key, key_len) == 0;
|
||||
if (!replacing
|
||||
&& !appendEntry(new_config, sizeof(new_config), used,
|
||||
item, item_key_len, item_value, item_value_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (next == nullptr) break;
|
||||
item = next + 1;
|
||||
if (*item == 0) return false; // reject a trailing/consecutive separator
|
||||
}
|
||||
|
||||
if (!appendEntry(new_config, sizeof(new_config), used,
|
||||
key, key_len, value, value_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(_config, new_config, used + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DynamicConfigSerializer::setByKey(const char* key, const char* value) {
|
||||
if (setByKeyPrv(key, value)) {
|
||||
markDirty();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DynamicConfigSerializer::getByKey(const char* key, char* value, size_t max_len) {
|
||||
if (value == nullptr || max_len == 0) return false;
|
||||
|
||||
size_t key_len = 0;
|
||||
if (!isValidKey(key, key_len)) {
|
||||
value[0] = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_fallback && _fallback->getByKey(key, value, max_len)) return true;
|
||||
|
||||
value[0] = 0;
|
||||
const size_t config_len = boundedLength(_config, sizeof(_config));
|
||||
if (config_len >= sizeof(_config)) return false;
|
||||
|
||||
char tmp[MAX_DYNAMIC_CONFG];
|
||||
memcpy(tmp, _config, config_len + 1);
|
||||
|
||||
char* item = tmp;
|
||||
while (*item != 0) {
|
||||
char* next = strchr(item, PROP_SEP_CHAR);
|
||||
if (next != nullptr) *next = 0;
|
||||
|
||||
char* item_separator = strchr(item, KEY_SEP_CHAR);
|
||||
if (item_separator == nullptr || item_separator == item) return false;
|
||||
*item_separator = 0;
|
||||
|
||||
size_t item_key_len = 0;
|
||||
size_t item_value_len = 0;
|
||||
const char* item_value = item_separator + 1;
|
||||
if (!isValidKey(item, item_key_len)
|
||||
|| !isValidValue(item_value, item_value_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (item_key_len == key_len && memcmp(item, key, key_len) == 0) {
|
||||
if (item_value_len >= max_len) return false;
|
||||
memcpy(value, item_value, item_value_len + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (next == nullptr) break;
|
||||
item = next + 1;
|
||||
if (*item == 0) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DynamicConfigSerializer::structure() {
|
||||
if (_context->op() == OP::WRITE) {
|
||||
const size_t config_len = boundedLength(_config, sizeof(_config));
|
||||
if (config_len >= sizeof(_config)) {
|
||||
_context->success = false;
|
||||
return;
|
||||
}
|
||||
|
||||
char tmp[MAX_DYNAMIC_CONFG];
|
||||
memcpy(tmp, _config, config_len + 1);
|
||||
|
||||
char* item = tmp;
|
||||
while (*item != 0) {
|
||||
char* next = strchr(item, PROP_SEP_CHAR);
|
||||
if (next != nullptr) *next = 0;
|
||||
|
||||
char* item_separator = strchr(item, KEY_SEP_CHAR);
|
||||
if (item_separator == nullptr || item_separator == item) {
|
||||
_context->success = false;
|
||||
return;
|
||||
}
|
||||
*item_separator = 0;
|
||||
|
||||
size_t item_key_len = 0;
|
||||
size_t item_value_len = 0;
|
||||
char* item_value = item_separator + 1;
|
||||
if (!isValidKey(item, item_key_len)
|
||||
|| !isValidValue(item_value, item_value_len)) {
|
||||
_context->success = false;
|
||||
return;
|
||||
}
|
||||
def(item, item_value, item_value_len + 1);
|
||||
|
||||
if (next == nullptr) break;
|
||||
item = next + 1;
|
||||
if (*item == 0) {
|
||||
_context->success = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!setByKeyPrv(_context->getKey(getDepth()), _context->getToken())) {
|
||||
_context->success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "ConfigSerializer.h"
|
||||
#include "KeyValueStore.h"
|
||||
|
||||
#ifndef MAX_DYNAMIC_CONFG
|
||||
#define MAX_DYNAMIC_CONFG 128
|
||||
#endif
|
||||
|
||||
class DynamicConfigSerializer : public ConfigSerializer, public KeyValueStore {
|
||||
char _config[MAX_DYNAMIC_CONFG];
|
||||
KeyValueStore* _fallback;
|
||||
|
||||
bool setByKeyPrv(const char* key, const char* value);
|
||||
|
||||
protected:
|
||||
void structure() override;
|
||||
|
||||
public:
|
||||
DynamicConfigSerializer(KeyValueStore* fallback = NULL) : _fallback(fallback) { _config[0] = 0; }
|
||||
|
||||
bool setByKey(const char* key, const char* value) override;
|
||||
bool getByKey(const char* key, char* value, size_t max_len) override;
|
||||
};
|
||||
@@ -25,6 +25,7 @@
|
||||
#if !defined(LIGHTWEIGHT_WIFI_OTA)
|
||||
class AsyncWebServer;
|
||||
#endif
|
||||
#include <helpers/KeyValueStore.h>
|
||||
|
||||
class ESP32Board : public mesh::MainBoard {
|
||||
protected:
|
||||
@@ -72,6 +73,8 @@ public:
|
||||
#endif
|
||||
}
|
||||
|
||||
void attachDynamicPrefs(KeyValueStore* prefs) { (void)prefs; } // no-op
|
||||
|
||||
// Temperature from ESP32 MCU
|
||||
float getMCUTemperature() override {
|
||||
uint32_t raw = 0;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
class KeyValueStore {
|
||||
protected:
|
||||
KeyValueStore() { }
|
||||
public:
|
||||
virtual bool setByKey(const char* key, const char* value) { return false; }
|
||||
virtual bool getByKey(const char* key, char* value, size_t max_len) { return false; }
|
||||
};
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <MeshCore.h>
|
||||
#include <helpers/KeyValueStore.h>
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
|
||||
@@ -73,6 +74,8 @@ public:
|
||||
bool isUsbDataConnected() override;
|
||||
bool isUsbHostConnected() override;
|
||||
|
||||
void attachDynamicPrefs(KeyValueStore* prefs) { (void)prefs; } // no-op
|
||||
|
||||
#ifdef NRF52_POWER_MANAGEMENT
|
||||
uint16_t getBootVoltage() override { return boot_voltage_mv; }
|
||||
virtual uint32_t getResetReason() const override { return reset_reason; }
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#define TXT_TYPE_PLAIN 0 // a plain text message
|
||||
#define TXT_TYPE_CLI_DATA 1 // a CLI command
|
||||
#define TXT_TYPE_CLI_DATA 1 // a CLI command -or- reply
|
||||
#define TXT_TYPE_SIGNED_PLAIN 2 // plain text, signed by sender
|
||||
#define TXT_TYPE_CLI_COMMAND 3 // a CLI command (explictly)
|
||||
|
||||
#define DATA_TYPE_RESERVED 0x0000 // reserved for future use
|
||||
#define DATA_TYPE_DEV 0xFFFF // developer namespace for experimenting with group/channel datagrams and building apps
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <MeshCore.h>
|
||||
#include <Arduino.h>
|
||||
#include <helpers/KeyValueStore.h>
|
||||
|
||||
class STM32Board : public mesh::MainBoard {
|
||||
protected:
|
||||
@@ -12,6 +13,8 @@ public:
|
||||
startup_reason = BD_STARTUP_NORMAL;
|
||||
}
|
||||
|
||||
void attachDynamicPrefs(KeyValueStore* prefs) { (void)prefs; } // no-op
|
||||
|
||||
uint8_t getStartupReason() const override { return startup_reason; }
|
||||
|
||||
uint16_t getBattMilliVolts() override {
|
||||
@@ -42,4 +45,4 @@ public:
|
||||
#endif
|
||||
|
||||
bool startOTAUpdate(const char* id, char reply[], bool force_ap = false) override { return false; };
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user