Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions examples/simple_repeater/MyMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,6 @@ void MyMesh::begin(FILESYSTEM *fs) {
_fs = fs;
// load persisted prefs
_cli.loadPrefs(_fs);

// Set MQTT origin to actual device name (not build-time ADVERT_NAME)
StrHelper::strncpy(_prefs.mqtt_origin, _prefs.node_name, sizeof(_prefs.mqtt_origin));
MESH_DEBUG_PRINTLN("MQTT origin set to device name: %s", _prefs.mqtt_origin);

acl.load(_fs, self_id);
// TODO: key_store.begin();
region_map.load(_fs);
Expand Down
4 changes: 0 additions & 4 deletions examples/simple_room_server/MyMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,6 @@ void MyMesh::begin(FILESYSTEM *fs) {
applyGpsPrefs();
#endif
#ifdef WITH_MQTT_BRIDGE
// Set MQTT origin to actual device name (not build-time ADVERT_NAME) - same as repeater
StrHelper::strncpy(_prefs.mqtt_origin, _prefs.node_name, sizeof(_prefs.mqtt_origin));
MESH_DEBUG_PRINTLN("MQTT origin set to device name: %s", _prefs.mqtt_origin);

if (_prefs.bridge_enabled) {
// Set device public key for MQTT topics (same as repeater)
char device_id[65];
Expand Down
31 changes: 25 additions & 6 deletions src/helpers/CommonCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ void CommonCLI::loadPrefs(FILESYSTEM* fs) {
loadMQTTPrefs(fs);
// Sync MQTT prefs to NodePrefs so existing code (like MQTTBridge) can access them
syncMQTTPrefsToNodePrefs();
// Fresh-install fallback: default mqtt.origin to node_name when mqtt_prefs has no origin yet.
// This preserves explicit user-configured mqtt.origin values.
if (_prefs->mqtt_origin[0] == '\0' && _prefs->node_name[0] != '\0') {
StrHelper::strncpy(_prefs->mqtt_origin, _prefs->node_name, sizeof(_prefs->mqtt_origin));
syncNodePrefsToMQTTPrefs();
saveMQTTPrefs(fs);
}

// For MQTT bridge, migrate bridge.source to RX (logRx) only on fresh installs or upgrades
// This ensures new users get the correct default, but respects existing user choices
Expand Down Expand Up @@ -392,7 +399,11 @@ void CommonCLI::saveMQTTPrefs(FILESYSTEM* fs) {
File file = fs->open("/mqtt_prefs", "w", true);
#endif
if (file) {
file.write((uint8_t *)&_mqtt_prefs, sizeof(_mqtt_prefs));
size_t bytes_written = file.write((uint8_t *)&_mqtt_prefs, sizeof(_mqtt_prefs));
if (bytes_written != sizeof(_mqtt_prefs)) {
MESH_DEBUG_PRINTLN("Failed to write /mqtt_prefs completely (wrote %u/%u bytes)",
(unsigned)bytes_written, (unsigned)sizeof(_mqtt_prefs));
}
file.close();
}
}
Expand Down Expand Up @@ -503,9 +514,13 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
strcpy(reply, "ERR: clock cannot go backwards");
}
} else if (memcmp(command, "memory", 6) == 0) {
#ifdef ESP_PLATFORM
sprintf(reply, "Free: %d, Min: %d, Max: %d, Queue: %d",
ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap(),
_callbacks->getQueueSize());
#else
sprintf(reply, "Queue: %d", _callbacks->getQueueSize());
#endif
} else if (memcmp(command, "start ota", 9) == 0) {
if (!_board->startOTAUpdate(_prefs->node_name, reply)) {
strcpy(reply, "Error");
Expand Down Expand Up @@ -741,10 +756,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
uint8_t ps = _prefs->wifi_power_save;
const char* ps_name = (ps == 1) ? "none" : (ps == 2) ? "max" : "min";
sprintf(reply, "> %s", ps_name);
} else if (memcmp(config, "timezone", 8) == 0) {
sprintf(reply, "> %s", _prefs->timezone_string);
} else if (memcmp(config, "timezone.offset", 15) == 0) {
sprintf(reply, "> %d", _prefs->timezone_offset);
} else if (memcmp(config, "timezone", 8) == 0) {
sprintf(reply, "> %s", _prefs->timezone_string);
} else if (memcmp(config, "mqtt.analyzer.us", 17) == 0) {
sprintf(reply, "> %s", _prefs->mqtt_analyzer_us_enabled ? "on" : "off");
} else if (memcmp(config, "mqtt.analyzer.eu", 17) == 0) {
Expand Down Expand Up @@ -1149,9 +1164,13 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
savePrefs();
strcpy(reply, "OK");
} else if (memcmp(config, "timezone.offset ", 16) == 0) {
int8_t offset = _atoi(&config[16]);
if (offset >= -12 && offset <= 14) {
_prefs->timezone_offset = offset;
const char* offset_str = &config[16];
char* endptr = nullptr;
long parsed = strtol(offset_str, &endptr, 10);
if (endptr == offset_str || (endptr != nullptr && *endptr != '\0')) {
strcpy(reply, "Error: timezone offset must be an integer");
} else if (parsed >= -12 && parsed <= 14) {
_prefs->timezone_offset = (int8_t)parsed;
savePrefs();
strcpy(reply, "OK");
} else {
Expand Down
11 changes: 8 additions & 3 deletions src/helpers/JWTHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ size_t JWTHelper::base64UrlEncode(const uint8_t* input, size_t inputLen, char* o

size_t JWTHelper::createHeader(char* output, size_t outputSize) {
// Create JWT header: {"alg":"Ed25519","typ":"JWT"}
DynamicJsonDocument doc(256);
StaticJsonDocument<128> doc;
doc["alg"] = "Ed25519";
doc["typ"] = "JWT";
if (doc.overflowed()) {
return 0;
}

char jsonBuffer[256];
size_t len = serializeJson(doc, jsonBuffer, sizeof(jsonBuffer));
Expand All @@ -163,7 +166,7 @@ size_t JWTHelper::createPayload(
const char* email
) {
// Create JWT payload
DynamicJsonDocument doc(512);
StaticJsonDocument<512> doc;
doc["publicKey"] = publicKey;
doc["aud"] = audience;
doc["iat"] = issuedAt;
Expand All @@ -186,6 +189,9 @@ size_t JWTHelper::createPayload(
if (email && strlen(email) > 0) {
doc["email"] = email;
}
if (doc.overflowed()) {
return 0;
}

char jsonBuffer[512];
size_t len = serializeJson(doc, jsonBuffer, sizeof(jsonBuffer));
Expand All @@ -195,4 +201,3 @@ size_t JWTHelper::createPayload(

return base64UrlEncode((uint8_t*)jsonBuffer, len, output, outputSize);
}

Loading