When a message exceeds the maximum payload size of a single packet, it must be fragmented into multiple packets for transmission. The receiver then reassembles these packets to reconstruct the original message. Some protocols — such as LoRaWAN and MQTT-SN — require message fragmentation.
This ANSI-C library for Arduino simplifies the process of message fragmentation and reassembly. The algorithm is illustrated in the following diagram:
Main features of the library:
- Fully written in ANSI-C;
- Only 1 additional byte is added to each chunk to maintain ordering;
- Chunk size is not fixed and can change during fragmentation (useful in LoRaWAN communication with ADR);
- The reassembler requires chunks to arrive in order;
- No checksum added to the payload (e.g., CRC-32);
- No padding added to chunks;
- No dynamic memory allocation (
malloc/free): the buffer must be allocated by the caller.
This library has been successfully tested on the following hardware:
Initialization
fragmenter_t fragmenter;
fragmenter_init(&fragmenter, message, msg_len);Fragment the message
while (fragmenter_has_more(&fragmenter)) {
uint32_t chunk_size = fragmenter_next(&fragmenter, chunk, max_chunk_size);
...
}Initialization
reassembler_t reassembler;
reassembler_ret_code = reassembler_init(&reassembler, buffer, max_buffer_size);Reassemble the message
reassembler_ret_code = 0;
while (reassembler_ret_code != REASSEMBLER_COMPLETE) {
reassembler_ret_code = reassembler_add_chunk(&reassembler, chunk, chunk_size);
...
}