Skip to content

The Telemetry Decoder Brief

The Course
•
30 min study

Today you assemble everything from the past six days into one library and one small application. Before reading any of its code, read its specification. A C library's real interface is not its function signatures; it is the set of promises about bytes, lifetimes, progress and failure that callers can rely on. Every one of those promises was built in an earlier lesson, and this brief collects them in one place so you can check the code against them, rather than learning what the code does by reading it.

The wire format

A version 1 frame is ten octets: magic 54, version 01, type 01, a big-endian payload length of 00 04, a two-octet sensor ID, a two-octet value, and a checksum. Sensor IDs are 1 to 4095 and values 0 to 10000, both big-endian. The checksum is the sum of the first nine octets modulo 256. The format requires 8-bit bytes, checked at compile time, and no native struct layout is ever written to or read from the wire.

The library contract

  • Handle. decoder_create makes one bounded allocation through a caller-supplied allocator and returns NULL on failure. decoder_destroy accepts NULL and must run exactly once per handle. The allocator's context must outlive the handle.
  • Feed. Each call processes at most one frame and reports a status and a consumed count. NEED_MORE consumes everything supplied. FRAME_READY consumes through one checksum and copies a validated Reading to the caller. Input is borrowed only during the call.
  • Errors. Any malformed byte enters sticky error: later feeds consume zero, and the output never changes on an error. There is no automatic resynchronization.
  • Finish and reset. decoder_finish reports STREAM_END at a clean boundary and turns a partial frame into an error. Reset discards partial bytes and errors. Only creation allocates.

The application

main.c reads whitespace-separated two-digit hex tokens from standard input, feeds each byte to the library, prints each completed reading, and ends with a frame count. It is deliberately not all-or-nothing: readings decoded before a later error have already been printed. These are its acceptance cases, and the course's checker runs every one:

  • Empty input prints Frames: 0 and exits with status 0.
  • The two-frame sample prints both readings and Frames: 2.
  • 54 01 01, a cut-off frame, prints Truncated frame to standard error and exits with status 1.
  • A frame whose checksum is 75 instead of 74 prints Invalid frame; reset required.
  • A frame with version 02 and its checksum adjusted to 75 is rejected the same way, by the header check rather than the checksum.
  • GG prints Invalid hex token; 5 and 5401 print Expected two hex digits per token.
  • A valid frame followed by a stray 00 prints the reading, then the error, and exits with status 1.

Predict and run

Read the program before pressing Run. Predict the frame size, then derive the ten bytes for sensor 2, value 40 by hand.

Predict before running C
#include <stdio.h>

int main(void) {
    size_t header = 5, payload = 4, checksum = 1;
    printf("Frame size: %zu\n", header + payload + checksum);
    return 0;
}
Check your prediction
Text
Frame size: 10

For sensor 2, value 40, the header is unchanged: 54 01 01 00 04. Sensor 2 is 00 02 and 40 is 0x28, so the value is 00 28. The checksum is 84 + 1 + 1 + 4 + 2 + 40 = 132, which is 0x84. That gives 54 01 01 00 04 00 02 00 28 84, the second line of the sample input. You have just checked the sample independently, the same way the tests check the encoder.

Why these choices

Each design decision answers a failure you met earlier. The opaque handle stops callers from editing used directly. Fixed storage means a hostile length cannot make the decoder allocate. One frame per call keeps the output a single value with no lifetime rules. Sticky errors stop the decoder from guessing where a frame starts inside garbage. A separate finish exists because only the caller knows the input ended. When you read the code in the next lesson, look for the line that implements each of these, and treat any promise you cannot find in the code or the tests as a gap.

Practice

Exercise

Your turn

Write int frame_size(size_t payload, size_t *out) that stores 5 + payload + 1 when payload is at most the 16-byte limit, and leaves *out unchanged otherwise. Then build the frame for sensor 2, value 40 from its fields, computing the checksum in code, and print it. Print:

Text
payload 4: frame 10
payload 16: frame 22
payload 17: rejected, out still 22
sensor 2/value 40: 54 01 01 00 04 00 02 00 28 84
Your workspace C
#include <stdio.h>

int main(void) {
    size_t out = 0;
    unsigned char frame[10] = {0x54, 1, 1, 0, 4};
    /* Write frame_size above, then fill the rest of frame and print it. */
    (void)out; (void)frame;
    return 0;
}
Hint 1: the idea

The protocol limit is what makes the addition safe: once payload <= 16 is known, 5 + payload + 1 cannot overflow. The rejection must return before the one line that writes *out.

Hint 2: a first step

Write the length check and the five header octets first, and print the frame. Then fill offsets 5 to 8, two octets per value with the high byte first. Leave the checksum for last: it covers every octet before it.

Show one solution
Run the solution C
#include <stdio.h>

int frame_size(size_t payload, size_t *out) {
    if (payload > 16) { return 0; }
    *out = 5 + payload + 1;
    return 1;
}

int main(void) {
    size_t out = 0;
    const size_t payloads[3] = {4, 16, 17};
    for (size_t i = 0; i < 3; ++i) {
        if (frame_size(payloads[i], &out)) { printf("payload %zu: frame %zu\n", payloads[i], out); }
        else { printf("payload %zu: rejected, out still %zu\n", payloads[i], out); }
    }
    unsigned char frame[10] = {0x54, 1, 1, 0, 4};
    const unsigned sensor = 2, value = 40;
    frame[5] = (unsigned char)(sensor >> 8); frame[6] = (unsigned char)(sensor & 0xFFu);
    frame[7] = (unsigned char)(value >> 8);  frame[8] = (unsigned char)(value & 0xFFu);
    unsigned char sum = 0;
    for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + frame[i]); }
    frame[9] = sum;
    printf("sensor 2/value 40:");
    for (size_t i = 0; i < 10; ++i) { printf(" %02X", (unsigned)frame[i]); }
    printf("\n");
    return 0;
}
Text
payload 4: frame 10
payload 16: frame 22
payload 17: rejected, out still 22
sensor 2/value 40: 54 01 01 00 04 00 02 00 28 84

Payload 16 gives 22, the size of the decoder's internal buffer, so the limit and the storage agree. The computed frame matches your hand derivation and the sample input, which means three independent routes reached the same bytes. Next, you trace those bytes through the real library and application.

Was this article helpful?

Your response is saved on this device.