From b9f478c9898a662b1c338e1bb71ad3a71d6191de Mon Sep 17 00:00:00 2001 From: agessaman Date: Sat, 9 May 2026 18:06:18 -0700 Subject: [PATCH] Add MQTT origin handling and string utility function Implemented a new function to strip surrounding quotes from strings in TxtDataHelpers, enhancing string manipulation capabilities. Updated CommonCLI to handle MQTT origin configuration, allowing for both setting and clearing of the mqtt.origin preference. This change improves the flexibility of MQTT origin management in the CLI. --- src/helpers/CommonCLI.cpp | 5 +++++ src/helpers/TxtDataHelpers.cpp | 14 ++++++++++++++ src/helpers/TxtDataHelpers.h | 2 ++ src/helpers/bridges/MQTTBridge.cpp | 24 +++--------------------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 991e19935..39ef6eb8f 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -1275,8 +1275,13 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep strcpy(reply, "OK"); #endif #ifdef WITH_MQTT_BRIDGE + } else if (strcmp(config, "mqtt.origin") == 0) { + _prefs->mqtt_origin[0] = '\0'; + savePrefs(); + strcpy(reply, "OK"); } else if (memcmp(config, "mqtt.origin ", 12) == 0) { StrHelper::strncpy(_prefs->mqtt_origin, &config[12], sizeof(_prefs->mqtt_origin)); + StrHelper::stripSurroundingQuotes(_prefs->mqtt_origin, sizeof(_prefs->mqtt_origin)); savePrefs(); strcpy(reply, "OK"); } else if (memcmp(config, "mqtt.iata ", 10) == 0) { diff --git a/src/helpers/TxtDataHelpers.cpp b/src/helpers/TxtDataHelpers.cpp index d327931fd..f994c9dec 100644 --- a/src/helpers/TxtDataHelpers.cpp +++ b/src/helpers/TxtDataHelpers.cpp @@ -1,4 +1,18 @@ #include "TxtDataHelpers.h" +#include + +void StrHelper::stripSurroundingQuotes(char* str, size_t buf_sz) { + if (!str || buf_sz == 0) return; + size_t len = strlen(str); + if (len == 0) return; + if (str[0] == '"' || str[0] == '\'') { + memmove(str, str + 1, len); + len--; + } + if (len > 0 && (str[len - 1] == '"' || str[len - 1] == '\'')) { + str[len - 1] = '\0'; + } +} void StrHelper::strncpy(char* dest, const char* src, size_t buf_sz) { while (buf_sz > 1 && *src) { diff --git a/src/helpers/TxtDataHelpers.h b/src/helpers/TxtDataHelpers.h index ece494f29..d9e82b23a 100644 --- a/src/helpers/TxtDataHelpers.h +++ b/src/helpers/TxtDataHelpers.h @@ -13,6 +13,8 @@ class StrHelper { public: static void strncpy(char* dest, const char* src, size_t buf_sz); static void strzcpy(char* dest, const char* src, size_t buf_sz); // pads with trailing nulls + /** Remove one leading and one trailing ASCII " or ' if present (in-place). No-op if empty. */ + static void stripSurroundingQuotes(char* str, size_t buf_sz); static const char* ftoa(float f); static const char* ftoa3(float f); //Converts float to string with 3 decimal places static bool isBlank(const char* str); diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index a706f45ad..e43ee162d 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -1,5 +1,6 @@ #include "MQTTBridge.h" #include "../MQTTMessageBuilder.h" +#include "../TxtDataHelpers.h" #include #include #include @@ -18,25 +19,6 @@ #include #endif -// Helper function to strip quotes from strings (both single and double quotes) -static void stripQuotes(char* str, size_t max_len) { - if (!str || max_len == 0) return; - - size_t len = strlen(str); - if (len == 0) return; - - // Remove leading quote (single or double) - if (str[0] == '"' || str[0] == '\'') { - memmove(str, str + 1, len); - len--; - } - - // Remove trailing quote (single or double) - if (len > 0 && (str[len-1] == '"' || str[len-1] == '\'')) { - str[len-1] = '\0'; - } -} - // Effective MQTT origin: empty mqtt_origin follows node_name; otherwise mqtt_origin override (quotes stripped). static void applyEffectiveOrigin(const NodePrefs* prefs, char* dest, size_t dest_size) { if (!prefs || !dest || dest_size == 0) return; @@ -46,7 +28,7 @@ static void applyEffectiveOrigin(const NodePrefs* prefs, char* dest, size_t dest strncpy(dest, prefs->mqtt_origin, dest_size - 1); } dest[dest_size - 1] = '\0'; - stripQuotes(dest, dest_size); + StrHelper::stripSurroundingQuotes(dest, dest_size); } void MQTTBridge::refreshOriginFromPrefs() { @@ -488,7 +470,7 @@ void MQTTBridge::begin() { strncpy(_iata, _prefs->mqtt_iata, sizeof(_iata) - 1); _iata[sizeof(_iata) - 1] = '\0'; - stripQuotes(_iata, sizeof(_iata)); + StrHelper::stripSurroundingQuotes(_iata, sizeof(_iata)); // Convert IATA code to uppercase (IATA codes are conventionally uppercase) for (int i = 0; _iata[i]; i++) {