Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compression (#2806) #2819

Merged
merged 3 commits into from
Sep 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ bool perhapsDecode(meshtastic_MeshPacket *p)
// LOG_DEBUG("\n\n**\n\nDecompressed length - %d \n", decompressed_len);

memcpy(p->decoded.payload.bytes, decompressed_out, decompressed_len);
p->decoded.payload.size = decompressed_len;

// Switch the port from PortNum_TEXT_MESSAGE_COMPRESSED_APP to PortNum_TEXT_MESSAGE_APP
p->decoded.portnum = meshtastic_PortNum_TEXT_MESSAGE_APP;
Expand All @@ -382,9 +383,7 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p)

// If the packet is not yet encrypted, do so now
if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded);

// Only allow encryption on the text message app.
// Only allow compression on the text message app.
// TODO: Allow modules to opt into compression.
if (p->decoded.portnum == meshtastic_PortNum_TEXT_MESSAGE_APP) {

Expand All @@ -396,17 +395,12 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p)
int compressed_len;
compressed_len = unishox2_compress_simple(original_payload, p->decoded.payload.size, compressed_out);

LOG_DEBUG("Original length - %d \n", p->decoded.payload.size);
LOG_DEBUG("Compressed length - %d \n", compressed_len);
LOG_DEBUG("Length - %d, compressed length - %d \n", p->decoded.payload.size, compressed_len);
LOG_DEBUG("Original message - %s \n", p->decoded.payload.bytes);

// If the compressed length is greater than or equal to the original size, don't use the compressed form
if (compressed_len >= p->decoded.payload.size) {

LOG_DEBUG("Not using compressing message.\n");
// Set the uncompressed payload variant anyway. Shouldn't hurt?
// p->decoded.which_payloadVariant = Data_payload_tag;

// Otherwise we use the compressor
} else {
LOG_DEBUG("Using compressed message.\n");
Expand All @@ -419,6 +413,8 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p)
}
}

size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded);

if (numbytes > MAX_RHPACKETLEN)
return meshtastic_Routing_Error_TOO_LARGE;

Expand Down