update zephyr

This commit is contained in:
liquidraver
2026-03-23 08:56:21 +01:00
parent 249a446877
commit c9013d85d4
12 changed files with 266 additions and 171 deletions
+109 -62
View File
@@ -111,6 +111,8 @@ void CommonCLI::loadPrefs(const char* path) {
ok = ok && prefs_read(&file, _prefs->owner_info, sizeof(_prefs->owner_info)); // 170
ok = ok && prefs_read(&file, &_prefs->rx_boost, sizeof(_prefs->rx_boost)); // 290
ok = ok && prefs_read(&file, &_prefs->rx_duty_cycle, sizeof(_prefs->rx_duty_cycle)); // 291
ok = ok && prefs_read(&file, &_prefs->apc_enabled, sizeof(_prefs->apc_enabled)); // 292
ok = ok && prefs_read(&file, &_prefs->apc_margin, sizeof(_prefs->apc_margin)); // 293
if (!ok) {
LOG_WRN("Prefs file %s truncated, some fields use defaults", path);
@@ -153,6 +155,8 @@ void CommonCLI::loadPrefs(const char* path) {
_prefs->advert_loc_policy = constrain(_prefs->advert_loc_policy, (uint8_t)0, (uint8_t)2);
_prefs->rx_boost = constrain(_prefs->rx_boost, (uint8_t)0, (uint8_t)1);
_prefs->rx_duty_cycle = constrain(_prefs->rx_duty_cycle, (uint8_t)0, (uint8_t)1);
_prefs->apc_enabled = constrain(_prefs->apc_enabled, (uint8_t)0, (uint8_t)1);
_prefs->apc_margin = constrain(_prefs->apc_margin, (uint8_t)6, (uint8_t)30);
LOG_INF("Loaded prefs from %s", path);
}
@@ -216,6 +220,8 @@ void CommonCLI::savePrefs(const char* path) {
fs_write(&file, _prefs->owner_info, sizeof(_prefs->owner_info));
fs_write(&file, &_prefs->rx_boost, sizeof(_prefs->rx_boost));
fs_write(&file, &_prefs->rx_duty_cycle, sizeof(_prefs->rx_duty_cycle));
fs_write(&file, &_prefs->apc_enabled, sizeof(_prefs->apc_enabled));
fs_write(&file, &_prefs->apc_margin, sizeof(_prefs->apc_margin));
fs_close(&file);
LOG_INF("Saved prefs to %s", path);
@@ -376,7 +382,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
_callbacks->applyTempRadioParams(freq, bw, sf, cr, temp_timeout_mins);
snprintf(reply, CLI_REPLY_SIZE, "OK - temp params for %d mins", temp_timeout_mins);
} else {
strcpy(reply, "Error, invalid params");
strcpy(reply, "Error: freq 300-2500, bw 7-500, sf 5-12, cr 5-8, timeout>0");
}
} else if (memcmp(command, "password ", 9) == 0) {
StrHelper::strncpy(_prefs->password, &command[9], sizeof(_prefs->password));
@@ -433,18 +439,6 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
(double)est, (double)ff);
} else if (memcmp(config, "apc.margin", 10) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "> %d dB", (int)_callbacks->getAPCTargetMargin());
} else if (memcmp(config, "txpower", 7) == 0) {
if (_callbacks->isAPCEnabled()) {
int8_t apc = _callbacks->getAPCReduction();
float margin = _callbacks->getAPCMargin();
int effective = (int)_prefs->tx_power_dbm - (int)apc;
snprintf(reply, CLI_REPLY_SIZE, "> %ddBm (max=%d apc=-%d margin=%.1f target=%d)",
effective, (int)_prefs->tx_power_dbm, (int)apc, (double)margin,
(int)_callbacks->getAPCTargetMargin());
} else {
snprintf(reply, CLI_REPLY_SIZE, "> %ddBm (apc=off)",
(int)_prefs->tx_power_dbm);
}
} else if (memcmp(config, "flood.max", 9) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "> %u", (uint32_t)_prefs->flood_max);
} else if (memcmp(config, "direct.txdelay", 14) == 0) {
@@ -473,7 +467,17 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
strcpy(reply, "> strict");
}
} else if (memcmp(config, "tx", 2) == 0 && (config[2] == 0 || config[2] == ' ')) {
snprintf(reply, CLI_REPLY_SIZE, "> %d", (int)_prefs->tx_power_dbm);
if (_callbacks->isAPCEnabled()) {
int8_t apc = _callbacks->getAPCReduction();
float margin = _callbacks->getAPCMargin();
int effective = (int)_prefs->tx_power_dbm - (int)apc;
snprintf(reply, CLI_REPLY_SIZE, "> %ddBm (max=%d apc=-%d margin=%.1f target=%d)",
effective, (int)_prefs->tx_power_dbm, (int)apc, (double)margin,
(int)_callbacks->getAPCTargetMargin());
} else {
snprintf(reply, CLI_REPLY_SIZE, "> %ddBm (apc=off)",
(int)_prefs->tx_power_dbm);
}
} else if (memcmp(config, "freq", 4) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "> %.3f", (double)_prefs->freq);
} else if (memcmp(config, "public.key", 10) == 0) {
@@ -495,6 +499,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
} else {
snprintf(reply, CLI_REPLY_SIZE, "> %.3f", (double)adc_mult);
}
} else if (memcmp(config, "rxboost", 7) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "> %d", (int)_prefs->rx_boost);
} else if (memcmp(config, "rxduty", 6) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "> %d", (int)_prefs->rx_duty_cycle);
} else {
snprintf(reply, CLI_REPLY_SIZE, "??: %s", config);
}
@@ -505,14 +513,17 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
const char* config = &command[4];
if (memcmp(config, "af ", 3) == 0) {
int val = atoi(&config[3]);
if (val <= 0 || val >= 100) {
if (val < 0 || val > 99) {
strcpy(reply, "Error: range 0-99 (0=unlimited)");
} else if (val == 0) {
_prefs->airtime_factor = 0.0f;
savePrefs();
strcpy(reply, "Duty Cycle set: 100% (unlimited)");
} else {
_prefs->airtime_factor = (float)val;
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "Duty Cycle set: %d%%", val);
}
savePrefs();
int dc = (_prefs->airtime_factor == 0.0f) ? 100 : (int)_prefs->airtime_factor;
snprintf(reply, CLI_REPLY_SIZE, "Duty Cycle set: %d%%", dc);
} else if (memcmp(config, "int.thresh ", 11) == 0) {
_prefs->interference_threshold = atoi(&config[11]);
savePrefs();
@@ -522,13 +533,26 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - interval rounded to %u", ((uint32_t)_prefs->agc_reset_interval) * 4);
} else if (memcmp(config, "multi.acks ", 11) == 0) {
_prefs->multi_acks = atoi(&config[11]);
savePrefs();
strcpy(reply, "OK");
int val = atoi(&config[11]);
if (val == 0 || val == 1) {
_prefs->multi_acks = (uint8_t)val;
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Error: must be 0 or 1");
}
} else if (memcmp(config, "allow.read.only ", 16) == 0) {
_prefs->allow_read_only = memcmp(&config[16], "on", 2) == 0;
savePrefs();
strcpy(reply, "OK");
if (memcmp(&config[16], "on", 2) == 0) {
_prefs->allow_read_only = 1;
savePrefs();
strcpy(reply, "OK");
} else if (memcmp(&config[16], "off", 3) == 0) {
_prefs->allow_read_only = 0;
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Error: must be on or off");
}
} else if (memcmp(config, "flood.advert.interval ", 22) == 0) {
int hours = _atoi(&config[22]);
if ((hours > 0 && hours < 3) || (hours > 168)) {
@@ -571,12 +595,20 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, bad chars");
strcpy(reply, "Error: name cannot contain [ ] \\ : , ? *");
}
} else if (memcmp(config, "repeat ", 7) == 0) {
_prefs->disable_fwd = memcmp(&config[7], "off", 3) == 0;
savePrefs();
strcpy(reply, _prefs->disable_fwd ? "OK - repeat is now OFF" : "OK - repeat is now ON");
if (memcmp(&config[7], "on", 2) == 0) {
_prefs->disable_fwd = 0;
savePrefs();
strcpy(reply, "OK - repeat is now ON");
} else if (memcmp(&config[7], "off", 3) == 0) {
_prefs->disable_fwd = 1;
savePrefs();
strcpy(reply, "OK - repeat is now OFF");
} else {
strcpy(reply, "Error: must be on or off");
}
} else if (memcmp(config, "radio ", 6) == 0) {
strcpy(tmp, &config[6]);
const char* parts[4];
@@ -594,7 +626,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
_callbacks->savePrefs();
strcpy(reply, "OK - reboot to apply");
} else {
strcpy(reply, "Error, invalid radio params");
strcpy(reply, "Error: freq 300-2500, bw 7-500, sf 5-12, cr 5-8");
}
} else if (memcmp(config, "lat ", 4) == 0) {
_prefs->node_lat = atof(&config[4]);
@@ -613,13 +645,13 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
savePrefs();
strcpy(reply, "OK (ignored: txdelay is now adaptive)");
} else if (memcmp(config, "flood.max ", 10) == 0) {
uint8_t m = atoi(&config[10]);
if (m <= 64) {
_prefs->flood_max = m;
int m = atoi(&config[10]);
if (m >= 0 && m <= 64) {
_prefs->flood_max = (uint8_t)m;
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Error, max 64");
strcpy(reply, "Error: range 0-64");
}
} else if (memcmp(config, "direct.txdelay ", 15) == 0) {
_prefs->direct_tx_delay_factor = atof(&config[15]);
@@ -678,34 +710,47 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
} else if (memcmp(config, "apc.margin ", 11) == 0) {
int val = atoi(&config[11]);
if (val >= 6 && val <= 30) {
_prefs->apc_margin = (uint8_t)val;
_callbacks->setAPCTargetMargin((uint8_t)val);
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - APC target margin=%d dB", val);
} else {
strcpy(reply, "Error, range 6-30 dB");
strcpy(reply, "Error: range 6-30 dB");
}
} else if (memcmp(config, "tx ", 3) == 0) {
if (memcmp(&config[3], "apc", 3) == 0) {
_prefs->apc_enabled = 1;
_callbacks->setAPCEnabled(true);
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - tx power=%d dBm (apc=on)",
(int)_prefs->tx_power_dbm);
} else {
int val = atoi(&config[3]);
int max_tx = 30;
#ifdef CONFIG_ZEPHCORE_MAX_TX_POWER_DBM
if (val > CONFIG_ZEPHCORE_MAX_TX_POWER_DBM) {
val = CONFIG_ZEPHCORE_MAX_TX_POWER_DBM;
}
max_tx = CONFIG_ZEPHCORE_MAX_TX_POWER_DBM;
#endif
_prefs->tx_power_dbm = (int8_t)val;
savePrefs();
_callbacks->setAPCEnabled(false);
_callbacks->setTxPower(_prefs->tx_power_dbm);
snprintf(reply, CLI_REPLY_SIZE, "OK - tx power=%d dBm (apc=off)",
(int)_prefs->tx_power_dbm);
if (val < -9 || val > max_tx) {
snprintf(reply, CLI_REPLY_SIZE, "Error: range -9 to %d dBm, or 'apc'", max_tx);
} else {
_prefs->apc_enabled = 0;
_prefs->tx_power_dbm = (int8_t)val;
savePrefs();
_callbacks->setAPCEnabled(false);
_callbacks->setTxPower(_prefs->tx_power_dbm);
snprintf(reply, CLI_REPLY_SIZE, "OK - tx power=%d dBm (apc=off)",
(int)_prefs->tx_power_dbm);
}
}
} else if (sender_timestamp == 0 && memcmp(config, "freq ", 5) == 0) {
_prefs->freq = atof(&config[5]);
savePrefs();
strcpy(reply, "OK - reboot to apply");
float f = atof(&config[5]);
if (f >= 400.0f && f <= 2500.0f) {
_prefs->freq = f;
savePrefs();
strcpy(reply, "OK - reboot to apply");
} else {
strcpy(reply, "Error: range 400-2500 MHz");
}
} else if (memcmp(config, "adc.multiplier ", 15) == 0) {
_prefs->adc_multiplier = atof(&config[15]);
if (_board->setAdcMultiplier(_prefs->adc_multiplier)) {
@@ -720,13 +765,23 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
strcpy(reply, "Error: unsupported by this board");
}
} else if (memcmp(config, "rxboost ", 8) == 0) {
_prefs->rx_boost = atoi(&config[8]) ? 1 : 0;
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - rxboost=%d (reboot to apply)", _prefs->rx_boost);
} else if (memcmp(config, "rxboost", 7) == 0 && (config[7] == 0 || config[7] == ' ')) {
snprintf(reply, CLI_REPLY_SIZE, "> %d", _prefs->rx_boost);
} else if (memcmp(config, "rxduty", 6) == 0) {
snprintf(reply, CLI_REPLY_SIZE, "RX duty cycle disabled (continuous RX)");
int val = atoi(&config[8]);
if (val == 0 || val == 1) {
_prefs->rx_boost = (uint8_t)val;
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - rxboost=%d (reboot to apply)", _prefs->rx_boost);
} else {
strcpy(reply, "Error: must be 0 or 1");
}
} else if (memcmp(config, "rxduty ", 7) == 0) {
int val = atoi(&config[7]);
if (val == 0 || val == 1) {
_prefs->rx_duty_cycle = (uint8_t)val;
savePrefs();
snprintf(reply, CLI_REPLY_SIZE, "OK - rxduty=%d (reboot to apply)", _prefs->rx_duty_cycle);
} else {
strcpy(reply, "Error: must be 0 or 1");
}
} else {
snprintf(reply, CLI_REPLY_SIZE, "unknown config: %s", config);
}
@@ -831,16 +886,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
} else {
strcpy(reply, "off");
}
} else if (memcmp(command, "powersaving on", 14) == 0) {
_prefs->powersaving_enabled = 1;
savePrefs();
strcpy(reply, "ok");
} else if (memcmp(command, "powersaving off", 15) == 0) {
_prefs->powersaving_enabled = 0;
savePrefs();
strcpy(reply, "ok");
} else if (memcmp(command, "powersaving", 11) == 0) {
strcpy(reply, _prefs->powersaving_enabled ? "on" : "off");
strcpy(reply, "Not implemented");
} else if (memcmp(command, "log start", 9) == 0) {
_callbacks->setLoggingOn(true);
strcpy(reply, " logging on");
+4
View File
@@ -64,6 +64,8 @@ struct NodePrefs {
char owner_info[120];
uint8_t rx_boost; // 1 = boosted RX gain (+3dB, +2mA), 0 = power save
uint8_t rx_duty_cycle; // 1 = RX duty cycle (power save), 0 = continuous RX
uint8_t apc_enabled; // 1 = adaptive power control on, 0 = fixed TX power
uint8_t apc_margin; // APC target link margin in dB (6-30, default 16)
/* ---- Companion-only fields (Zephyr additions, not in Arduino) ---- */
uint8_t manual_add_contacts;
@@ -124,4 +126,6 @@ static inline void initNodePrefs(NodePrefs* prefs) {
prefs->adc_multiplier = 0.0f;
prefs->rx_boost = 1; // Default to boosted RX for better sensitivity
prefs->rx_duty_cycle = 0; // Default OFF — continuous RX for best reliability
prefs->apc_enabled = 0; // Default OFF — fixed TX power
prefs->apc_margin = 16; // Default 16 dB target link margin
}
+26 -12
View File
@@ -5,11 +5,10 @@
#include "TransportKeyStore.h"
#include <string.h>
#include <mbedtls/sha256.h>
#include <mbedtls/md.h>
#include <psa/crypto.h>
uint16_t TransportKey::calcTransportCode(const mesh::Packet* packet) const {
/* HMAC-SHA256 using mbedTLS */
/* HMAC-SHA256 using PSA Crypto */
uint8_t hmac[32];
uint8_t type = packet->getPayloadType();
@@ -19,14 +18,27 @@ uint16_t TransportKey::calcTransportCode(const mesh::Packet* packet) const {
memcpy(&msg[1], packet->payload, packet->payload_len);
size_t msg_len = 1 + packet->payload_len;
/* Calculate HMAC-SHA256 */
mbedtls_md_context_t ctx;
mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
mbedtls_md_hmac_starts(&ctx, key, sizeof(key));
mbedtls_md_hmac_update(&ctx, msg, msg_len);
mbedtls_md_hmac_finish(&ctx, hmac);
mbedtls_md_free(&ctx);
/* Import HMAC key */
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
psa_set_key_type(&attr, PSA_KEY_TYPE_HMAC);
psa_set_key_bits(&attr, sizeof(key) * 8);
psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_SIGN_MESSAGE);
psa_set_key_algorithm(&attr, PSA_ALG_HMAC(PSA_ALG_SHA_256));
psa_key_id_t key_id;
psa_status_t status = psa_import_key(&attr, key, sizeof(key), &key_id);
if (status != PSA_SUCCESS) {
return 0;
}
size_t out_len;
status = psa_mac_compute(key_id, PSA_ALG_HMAC(PSA_ALG_SHA_256),
msg, msg_len, hmac, sizeof(hmac), &out_len);
psa_destroy_key(key_id);
if (status != PSA_SUCCESS) {
return 0;
}
/* Extract first 2 bytes as transport code (little-endian, matching Arduino SHA256 finalizeHMAC behavior) */
uint16_t code = (uint16_t)hmac[0] | ((uint16_t)hmac[1] << 8);
@@ -66,7 +78,9 @@ void TransportKeyStore::getAutoKeyFor(uint16_t id, const char* name, TransportKe
// calc key for publicly-known hashtag region name (SHA256 hash, first 16 bytes)
uint8_t hash[32];
mbedtls_sha256((const unsigned char*)name, strlen(name), hash, 0);
size_t out_len;
psa_hash_compute(PSA_ALG_SHA_256, (const uint8_t*)name, strlen(name),
hash, sizeof(hash), &out_len);
memcpy(dest.key, hash, sizeof(dest.key));
putCache(id, dest);