From 52a2da28ac7a37c8623e09f7dd5d27b6ff3e8323 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 10 Aug 2019 13:31:05 +0200 Subject: [PATCH] Add function ToHex to support.ino Add function ToHex to support.ino --- sonoff/my_user_config.h | 2 +- sonoff/support.ino | 25 +++++++++++++++++++++++++ sonoff/xdrv_02_mqtt.ino | 29 ++++------------------------- sonoff/xdrv_23_zigbee.ino | 20 -------------------- 4 files changed, 30 insertions(+), 46 deletions(-) diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index 6468f8091..9a90b9282 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -469,7 +469,7 @@ #define IR_RCV_TIMEOUT 15 // Number of milli-Seconds of no-more-data before we consider a message ended (default 15) #define IR_RCV_MIN_UNKNOWN_SIZE 6 // Set the smallest sized "UNKNOWN" message packets we actually care about (default 6, max 255) -// -- Zigbee interface ------------------------------ +// -- Zigbee interface ---------------------------- //#define USE_ZIGBEE // Enable serial communication with Zigbee CC2530 flashed with ZNP // ------------------------------------------------ diff --git a/sonoff/support.ino b/sonoff/support.ino index 9d7a5ef3b..9458672ed 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -292,6 +292,31 @@ char* ulltoa(unsigned long long value, char *str, int radix) return str; } +// see https://stackoverflow.com/questions/6357031/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-in-c +void ToHex(unsigned char * in, size_t insz, char * out, size_t outsz, char inbetween = ' '); +void ToHex(unsigned char * in, size_t insz, char * out, size_t outsz, char inbetween) +{ + // ToHex(in, insz, out, outz) -> "12 34 56 67" + // ToHex(in, insz, out, outz, '\0') -> "12345667" + // ToHex(in, insz, out, outz, ':') -> "12:34:56:67" + static const char * hex = "0123456789ABCDEF"; + int between = (inbetween) ? 3 : 2; + unsigned char * pin = in; + char * pout = out; + for (; pin < in+insz; pout += between, pin++) { + pout[0] = hex[(*pin>>4) & 0xF]; + pout[1] = hex[ *pin & 0xF]; + if (inbetween) { pout[2] = inbetween; } + if (pout + 3 - out > outsz) { + // Better to truncate output string than overflow buffer + // it would be still better to either return a status + // or ensure the target buffer is large enough and it never happen + break; + } + } + pout[(inbetween) ? -1 : 0] = 0; // Discard last inbetween +} + char* dtostrfd(double number, unsigned char prec, char *s) { if ((isnan(number)) || (isinf(number))) { // Fix for JSON output (https://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript) diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index ff012233a..a685b336c 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -66,26 +66,8 @@ bool mqtt_connected = false; // MQTT virtual connection status bool mqtt_allowed = false; // MQTT enabled and parameters valid #ifdef USE_MQTT_TLS -// see https://stackoverflow.com/questions/6357031/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-in-c -void to_hex(unsigned char * in, size_t insz, char * out, size_t outsz) { - unsigned char * pin = in; - static const char * hex = "0123456789ABCDEF"; - char * pout = out; - for (; pin < in+insz; pout +=3, pin++) { - pout[0] = hex[(*pin>>4) & 0xF]; - pout[1] = hex[ *pin & 0xF]; - pout[2] = ' '; - if (pout + 3 - out > outsz){ - /* Better to truncate output string than overflow buffer */ - /* it would be still better to either return a status */ - /* or ensure the target buffer is large enough and it never happen */ - break; - } - } - pout[-1] = 0; -} -#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT) +#ifdef USE_MQTT_AWS_IOT #include const br_ec_private_key *AWS_IoT_Private_Key = nullptr; @@ -108,7 +90,7 @@ public: tls_dir_t tls_dir; // memory copy of tls_dir from flash -#endif +#endif // USE_MQTT_AWS_IOT // A typical AWS IoT endpoint is 50 characters long, it does not fit // in MqttHost field (32 chars). We need to concatenate both MqttUser and MqttHost @@ -638,7 +620,7 @@ void MqttReconnect(void) #ifndef USE_MQTT_TLS_CA_CERT // don't bother with fingerprints if using CA validation // create a printable version of the fingerprint received char buf_fingerprint[64]; - to_hex((unsigned char *)tlsClient->getRecvPubKeyFingerprint(), 20, buf_fingerprint, 64); + ToHex((unsigned char *)tlsClient->getRecvPubKeyFingerprint(), 20, buf_fingerprint, sizeof(buf_fingerprint)); AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_MQTT "Server fingerprint: %s"), buf_fingerprint); if (learn_fingerprint1 || learn_fingerprint2) { @@ -716,10 +698,7 @@ void CmndMqttFingerprint(void) } restart_flag = 2; } - fingerprint[0] = '\0'; - for (uint32_t i = 0; i < sizeof(Settings.mqtt_fingerprint[XdrvMailbox.index -1]); i++) { - snprintf_P(fingerprint, sizeof(fingerprint), PSTR("%s%s%02X"), fingerprint, (i) ? " " : "", Settings.mqtt_fingerprint[XdrvMailbox.index -1][i]); - } + ToHex((unsigned char *)Settings.mqtt_fingerprint[XdrvMailbox.index -1], 20, fingerprint, sizeof(fingerprint)); ResponseCmndIdxChar(fingerprint); } } diff --git a/sonoff/xdrv_23_zigbee.ino b/sonoff/xdrv_23_zigbee.ino index 23a9038b3..4c2962a4c 100644 --- a/sonoff/xdrv_23_zigbee.ino +++ b/sonoff/xdrv_23_zigbee.ino @@ -286,26 +286,6 @@ uint32_t zigbee_frame_len = 256; bool zigbee_active = true; bool zigbee_raw = false; -// see https://stackoverflow.com/questions/6357031/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-in-c -static void tohex(unsigned char * in, size_t insz, char * out, size_t outsz) { - unsigned char * pin = in; - static const char * hex = "0123456789ABCDEF"; - char * pout = out; - for(; pin < in+insz; pout +=3, pin++){ - pout[0] = hex[(*pin>>4) & 0xF]; - pout[1] = hex[ *pin & 0xF]; - pout[2] = ':'; - if (pout + 3 - out > outsz){ - /* Better to truncate output string than overflow buffer */ - /* it would be still better to either return a status */ - /* or ensure the target buffer is large enough and it never happen */ - break; - } - } - pout[-1] = 0; -} - - void ZigbeeInput(void) { // Receive only valid ZNP frames: