Add function ToHex to support.ino

Add function ToHex to support.ino
This commit is contained in:
Theo Arends 2019-08-10 13:31:05 +02:00
parent b1b9030c8e
commit 52a2da28ac
4 changed files with 30 additions and 46 deletions

View File

@ -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_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) #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 //#define USE_ZIGBEE // Enable serial communication with Zigbee CC2530 flashed with ZNP
// ------------------------------------------------ // ------------------------------------------------

View File

@ -292,6 +292,31 @@ char* ulltoa(unsigned long long value, char *str, int radix)
return str; 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) 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) if ((isnan(number)) || (isinf(number))) { // Fix for JSON output (https://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript)

View File

@ -66,26 +66,8 @@ bool mqtt_connected = false; // MQTT virtual connection status
bool mqtt_allowed = false; // MQTT enabled and parameters valid bool mqtt_allowed = false; // MQTT enabled and parameters valid
#ifdef USE_MQTT_TLS #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 <base64.hpp> #include <base64.hpp>
const br_ec_private_key *AWS_IoT_Private_Key = nullptr; 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 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 // 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 // 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 #ifndef USE_MQTT_TLS_CA_CERT // don't bother with fingerprints if using CA validation
// create a printable version of the fingerprint received // create a printable version of the fingerprint received
char buf_fingerprint[64]; 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); AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_MQTT "Server fingerprint: %s"), buf_fingerprint);
if (learn_fingerprint1 || learn_fingerprint2) { if (learn_fingerprint1 || learn_fingerprint2) {
@ -716,10 +698,7 @@ void CmndMqttFingerprint(void)
} }
restart_flag = 2; restart_flag = 2;
} }
fingerprint[0] = '\0'; ToHex((unsigned char *)Settings.mqtt_fingerprint[XdrvMailbox.index -1], 20, fingerprint, sizeof(fingerprint));
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]);
}
ResponseCmndIdxChar(fingerprint); ResponseCmndIdxChar(fingerprint);
} }
} }

View File

@ -286,26 +286,6 @@ uint32_t zigbee_frame_len = 256;
bool zigbee_active = true; bool zigbee_active = true;
bool zigbee_raw = false; 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) void ZigbeeInput(void)
{ {
// Receive only valid ZNP frames: // Receive only valid ZNP frames: