Fixed HexToBytes function. Added Publish3 command. (#21329)

This commit is contained in:
Marius Bezuidenhout 2024-05-04 14:42:35 +02:00 committed by GitHub
parent fffb43fa79
commit 545cc27ca8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 8 deletions

View File

@ -611,16 +611,15 @@ String HexToString(uint8_t* data, uint32_t length) {
// Converts a Hex string (case insensitive) into an array of bytes
// Returns the number of bytes in the array, or -1 if an error occured
// The `out` buffer must be at least half the size of hex string
int32_t HexToBytes(const char* hex, uint8_t* out, size_t* outLen) {
int32_t HexToBytes(const char* hex, uint8_t* out, size_t outLen) {
size_t len = strlen_P(hex);
*outLen = 0;
if (len % 2 != 0) {
return -1;
}
size_t outLength = len / 2;
for(size_t i = 0; i < outLength; i++) {
for (size_t i = 0; i < outLength && i < outLen; i++) {
char byte[3];
byte[0] = hex[i*2];
byte[1] = hex[i*2 + 1];

View File

@ -742,6 +742,25 @@ void MqttPublish(const char* topic, bool retained) {
MqttPublishPayload(topic, ResponseData(), 0, retained);
}
void MqttPublish(const char* topic, bool retained, bool binary) {
int32_t binary_length = 0;
char *response_data = ResponseData();
if (binary) {
// Binary data will be half of the size of a text packet
uint8_t binary_payload[MQTT_MAX_PACKET_SIZE / 2];
binary_length = HexToBytes(response_data, binary_payload, MQTT_MAX_PACKET_SIZE / 2);
if (binary_length == -1) {
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT "Invalid hex: %s"), response_data);
} else {
// Conversion was successful
MqttPublishPayload(topic, (const char *)binary_payload, binary_length, retained);
}
} else {
// Publish <topic> default ResponseData string with optional retained
MqttPublishPayload(topic, response_data, 0, retained);
}
}
void MqttPublish(const char* topic) {
// Publish <topic> default ResponseData string no retained
MqttPublish(topic, false);
@ -1550,7 +1569,7 @@ void CmndPublish(void) {
} else {
ResponseClear();
}
MqttPublish(stemp1, (XdrvMailbox.index == 2));
MqttPublish(stemp1, (XdrvMailbox.index == 2), (XdrvMailbox.index == 3));
ResponseClear();
}
}

View File

@ -654,7 +654,7 @@ uint32_t IrRemoteCmndIrSendJson(void)
}
uint8_t data_bytes[num_bytes]; // allocate on stack since it's small enough
if (HexToBytes(data_hex, data_bytes, &num_bytes) <= 0) { return IE_INVALID_HEXDATA; }
if (HexToBytes(data_hex, data_bytes, num_bytes) <= 0) { return IE_INVALID_HEXDATA; }
if (lsb) {
reverseBits(data_bytes, bits);

View File

@ -93,7 +93,7 @@ bool LoraWanLoadData(void) {
app_key = root.getStr(PSTR(D_JSON_APPKEY), nullptr);
if (strlen(app_key)) {
size_t out_len = TAS_LORAWAN_AES128_KEY_SIZE;
HexToBytes(app_key, Lora->settings.end_node[n].AppKey, &out_len);
HexToBytes(app_key, Lora->settings.end_node[n].AppKey, out_len);
}
Lora->settings.end_node[n].DevEUIh = root.getUInt(PSTR(D_JSON_DEVEUI "h"), Lora->settings.end_node[n].DevEUIh);
Lora->settings.end_node[n].DevEUIl = root.getUInt(PSTR(D_JSON_DEVEUI "l"), Lora->settings.end_node[n].DevEUIl);
@ -581,8 +581,8 @@ void CmndLoraWanAppKey(void) {
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= TAS_LORAWAN_ENDNODES)) {
uint32_t node = XdrvMailbox.index -1;
if (32 == XdrvMailbox.data_len) {
size_t out_len = 16;
HexToBytes(XdrvMailbox.data, Lora->settings.end_node[node].AppKey, &out_len);
size_t out_len = TAS_LORAWAN_AES128_KEY_SIZE;
HexToBytes(XdrvMailbox.data, Lora->settings.end_node[node].AppKey, out_len);
if (0 == Lora->settings.end_node[node].name.length()) {
Lora->settings.end_node[node].name = F("0x0000");
}