Refactor Packet handling in MQTTMessageBuilder to support multibyte paths. Update path length handling in Packet::writeTo and Packet::readFrom methods, ensuring correct byte length calculations. Modify path string formatting in MQTTMessageBuilder for improved clarity on path details.

This commit is contained in:
agessaman
2026-02-25 22:01:44 -08:00
parent 51e9907fa6
commit c8e220b1ba
2 changed files with 16 additions and 12 deletions
+9 -5
View File
@@ -56,8 +56,9 @@ uint8_t Packet::writeTo(uint8_t dest[]) const {
memcpy(&dest[i], &transport_codes[0], 2); i += 2;
memcpy(&dest[i], &transport_codes[1], 2); i += 2;
}
dest[i++] = path_len;
memcpy(&dest[i], path, path_len); i += path_len;
dest[i++] = (uint8_t)path_len;
size_t path_byte_len = getPathByteLen();
memcpy(&dest[i], path, path_byte_len); i += path_byte_len;
memcpy(&dest[i], payload, payload_len); i += payload_len;
return i;
}
@@ -71,9 +72,12 @@ bool Packet::readFrom(const uint8_t src[], uint8_t len) {
} else {
transport_codes[0] = transport_codes[1] = 0;
}
path_len = src[i++];
if (path_len > sizeof(path)) return false; // bad encoding
memcpy(path, &src[i], path_len); i += path_len;
uint8_t pl = src[i++];
path_len = pl;
if ((pl >> 6) == 3) return false; // reserved path mode
uint8_t path_byte_len = (pl & 63) * ((pl >> 6) + 1);
if (path_byte_len > sizeof(path) || i + path_byte_len > len) return false; // bad encoding
memcpy(path, &src[i], path_byte_len); i += path_byte_len;
if (i >= len) return false; // bad encoding
payload_len = len - i;
if (payload_len > sizeof(payload)) return false; // bad encoding
+7 -7
View File
@@ -211,18 +211,18 @@ int MQTTMessageBuilder::buildPacketJSON(
packet->calculatePacketHash(packet_hash);
bytesToHex(packet_hash, MAX_HASH_SIZE, hash_str, sizeof(hash_str));
// Build path string for direct packets
// Build path string for direct packets (multibyte-path: show hash count, hash size, byte length)
char path_str[128] = "";
if (packet->isRouteDirect() && packet->path_len > 0) {
// Simplified path representation
snprintf(path_str, sizeof(path_str), "path_len_%d", packet->path_len);
snprintf(path_str, sizeof(path_str), "path_%dx%d_%db",
(int)packet->getPathHashCount(), (int)packet->getPathHashSize(), (int)packet->getPathByteLen());
}
return buildPacketMessage(
origin, origin_id, timestamp,
is_tx ? "tx" : "rx",
time_str, date_str,
packet->path_len + packet->payload_len + 2,
packet->getRawLength(),
packet_type, route_str,
packet->payload_len,
raw_hex,
@@ -293,11 +293,11 @@ int MQTTMessageBuilder::buildPacketJSONFromRaw(
packet->calculatePacketHash(packet_hash);
bytesToHex(packet_hash, MAX_HASH_SIZE, hash_str, sizeof(hash_str));
// Build path string for direct packets
// Build path string for direct packets (multibyte-path: show hash count, hash size, byte length)
char path_str[128] = "";
if (packet->isRouteDirect() && packet->path_len > 0) {
// Simplified path representation
snprintf(path_str, sizeof(path_str), "path_len_%d", packet->path_len);
snprintf(path_str, sizeof(path_str), "path_%dx%d_%db",
(int)packet->getPathHashCount(), (int)packet->getPathHashSize(), (int)packet->getPathByteLen());
}
return buildPacketMessage(