An allocation-free C11 packet encoder and incremental stream parser with CRC validation.
Small devices often deliver bytes in chunks that do not match application packets. This project explores that boundary: a frame can arrive one byte at a time, contain the sync marker inside its payload, or follow noise and a damaged frame.
This is an original educational application protocol, tested on a host computer. It is not a UART peripheral driver, and it has not been tested on a microcontroller or physical serial link.
Requires CMake 3.20+ and a C11 compiler. Python 3 is optional for the independent reference check. There are no downloaded C dependencies.
git clone https://github.com/AkshathCodes/embedded-telemetry-c.git
cd embedded-telemetry-c
cmake -S . -B build
cmake --build build --config Release
ctest --test-dir build -C Release --output-on-failureRun the demonstration:
# Linux/macOS, single-configuration generator
./build/telemetry_demo# Windows, Visual Studio generator
.\build\Release\telemetry_demo.exeThe example feeds noise, a split frame, a CRC-corrupted frame, and another valid frame. Actual output:
encoded=a55a010212340310a55ac3a9
received type=2 sequence=4660 payload=10a55a
received type=2 sequence=4660 payload=10a55a
frames=2 crc_errors=1 discarded_bytes=15
For an optimized build with a single-configuration generator, configure with -DCMAKE_BUILD_TYPE=Release. Visual Studio chooses configuration at build/test time with --config Release and -C Release.
All multibyte fields are big-endian. Payload length is measured in bytes.
| Offset | Field | Size |
|---|---|---|
| 0 | Sync: A5 5A |
2 bytes |
| 2 | Version: 01 |
1 byte |
| 3 | Application message type | 1 byte |
| 4 | Sequence number | 2 bytes |
| 6 | Payload length: 0..64 | 1 byte |
| 7 | Opaque application payload | 0..64 bytes |
| 7 + length | CRC | 2 bytes |
CRC parameters: polynomial 0x1021, initial value 0xFFFF, no input/output reflection, final XOR 0x0000. The checksum covers version through the final payload byte; it excludes sync and the checksum itself. ASCII 123456789 produces 0x29B1.
#include "telemetry.h"
static void receive(const tlm_frame *frame, void *context) {
/* Consume or copy frame->payload here; it is borrowed memory. */
(void)frame;
(void)context;
}
void process_bytes(const uint8_t *bytes, size_t count) {
tlm_parser parser;
tlm_parser_init(&parser);
tlm_parser_feed(&parser, bytes, count, receive, NULL);
}For a continuing stream, initialize the parser once and keep it between calls. The snippet above processes one independent input buffer. See examples/demo.c for persistent parser use across chunks.
tlm_encodereturns the encoded byte count, or zero if the payload exceeds 64 bytes or output capacity is too small. Rejected encodes leave output unchanged.- The parser buffers at most 73 bytes. It tracks accepted frames, discarded bytes, invalid headers, and CRC failures.
- Callbacks run synchronously. Payload memory is valid only during the callback; copy it if needed afterward. Callbacks must not feed or reset the same parser.
- Use initialized parser objects and valid pointers for nonempty buffers; the callback is required. Input/output buffers must not overlap parser storage, and encoder input/output must not overlap.
- The sequence number is transported as data; ordering, duplicate detection, acknowledgments, retransmission, and wrap handling belong to the application.
The scanner preserves partial candidates across feeds. Invalid versions, oversized lengths, and bad CRCs advance the scan by one byte, preserving the chance of finding an overlapping sync marker.
A truncated frame whose header declares a plausible long payload can delay the next frame until enough bytes arrive to validate or reject it. A byte-only framing layer cannot infer a timeout. On an application-level timeout or transport reconnect, call tlm_parser_reset(&parser) to discard buffered bytes while preserving diagnostic counters. tlm_parser_init clears both buffering and counters.
CRC detects some transmission corruption; it does not authenticate senders or guarantee detection of every corruption pattern. This parser is single-threaded and does not provide flow control or a serial transport.
The C executable uses explicit checks that stay active under NDEBUG. Its cases cover the independent golden frame, every split of that frame, all payload lengths, embedded sync bytes, noise, bad headers/CRC, 200 sequenced frames with deterministic chunking, and truncation/reset behavior.
When Python is found, CTest also compares a complete C-encoded frame against binascii.crc_hqx, not against the C decoder:
python tools/reference_vectors.py
# a55a010212340310a55ac3a9CI runs Debug and Release configurations on Linux and Windows. The workflow badge reports hosted results; local testing alone does not establish MCU timing, memory layout, interrupt behavior, or serial reliability.
Created with substantial AI assistance using OpenAI Codex. These are educational implementations and simulations; the repository's tests document the behavior that was verified.