Skip to content

Checkpoint: A Complete Reading Frame

The Course
•
45 min study

You now have every piece of a frame: big-endian fields, a checked cursor, header validation and a checksum. Before Day 4 makes the decoder streaming, this checkpoint puts the pieces together for one complete, in-memory frame. You already wrote the encoder in Day 2's checkpoint, so it is given here; the new half is the decoder that turns ten octets back into a Reading, and the tests that prove it.

There is a trap in testing such a pair. If you only check that decode(encode(r)) gives back r, an encoder and decoder that share the same mistake will pass together. Both could write the low octet first; both could skip the type byte. The defense is an independent fixture: bytes derived by hand from the specification. For sensor 1, value 25:

  • 54 01 01 are magic, version and type.
  • 00 04 is the payload length 4, big-endian.
  • 00 01 is sensor 1 and 00 19 is value 25, since 25 is 0x19.
  • 74 is the checksum: 0x54 + 1 + 1 + 0 + 4 + 0 + 1 + 0 + 0x19 is 84 + 7 + 25 = 116, which is 0x74.

Predict and run

Read the program before pressing Run. Predict the output, then find the offsets of the sensor, the value and the checksum in the array.

Predict before running C
#include <stdio.h>

int main(void) {
    unsigned char frame[10] = {0x54,1,1,0,4,0,1,0,25,0x74};
    printf("Size: %zu\n", sizeof frame);
    return 0;
}
Check your prediction
Text
Size: 10

sizeof on an array gives its size in bytes, 10 here, because the array was declared in this scope. Inside a function that receives frame as a parameter, sizeof frame would give the size of a pointer instead, which is why the project passes sizes explicitly. The sensor sits at offsets 5 and 6, the value at 7 and 8, and the checksum at 9.

What you already have

  • The encoder from Checkpoint: Encode a Header on Day 2, given in the starter as encode_frame, which takes a Reading.
  • Big-endian reads, header checks and the checksum from Days 2 and 3.
  • Validate into a candidate, publish once from A Checksum Is Evidence, Not Authentication, and sentinel outputs such as 77 and 88, so a test can prove that a failure left the output alone.

Plan before you type

Goals, not statements:

  1. Decide the order of the decoder's checks so that it never reads an octet it has not proved is there, and so each failure reports the first thing wrong.
  2. For each failure code, design the smallest frame that should produce it and nothing else. A frame meant to test the range check must pass the header and checksum checks first.
  3. Predict all seven lines before you run, including the sentinel values on every failure line.
Prove it can fail

Once the output matches, reverse the byte order of every two-octet field in both the given encoder and your decoder at the same time. The round-trip line still says ok, 4095 10000, because the two mistakes cancel, but the first line says no and the fixture decodes as 256 6400. That is the reason the fixture exists. Restore both, then make the decoder assign *out before its range check: the value-10001 line reports out of range, 1 10001, a failure that changed the output.

Mistakes worth checking for

  • A nine-byte input decodes as a reading. The decoder read f[9] without checking size, which is an out-of-bounds read. Check the size first.
  • The checksum is off by the checksum byte. The sum included octet 9. It covers octets 0 to 8 only.
  • The value-10001 line says checksum. The test changed the value without recomputing octet 9, so it never reached the range check. Each bad frame must break exactly one rule.
  • The round trip passes but the fixture line fails. The decoder and a test helper share a byte-order or offset mistake. Compare with the fixture one position at a time.

Practice

Exercise

Your turn

Write decode_frame(bytes, size, out), returning 0 for ok, 1 for short input, 2 for a bad header, 3 for a checksum mismatch and 4 for a field out of range, and writing *out only on 0. Then write the decode checks in main, using the given check helper, so the program prints:

Shell
encode 1/25 matches fixture: yes
decode fixture: ok, 1 25
decode 9 bytes: short, 77 88
decode version 2: bad header, 77 88
decode flipped value bit: checksum, 77 88
decode value 10001: out of range, 77 88
round trip 4095/10000: ok, 4095 10000
Your workspace C
#include <stdio.h>
#include <string.h>

typedef struct { unsigned sensor; unsigned value; } Reading;
static const unsigned char fixture[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 0x19, 0x74};
static const char *names[] = {"ok", "short", "bad header", "checksum", "out of range"};

static unsigned char sum9(const unsigned char *f) {
    unsigned char sum = 0;
    for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
    return sum;
}
static int valid_reading(Reading r) { return r.sensor >= 1 && r.sensor <= 4095 && r.value <= 10000; }

/* Given: Day 2's encoder, taking a Reading. */
int encode_frame(Reading record, unsigned char *out, size_t capacity, size_t *written) {
    if (capacity < 10 || !valid_reading(record)) { return 0; }
    unsigned char frame[10] = {0x54, 1, 1, 0, 4, 0, 0, 0, 0, 0};
    frame[5] = (unsigned char)(record.sensor >> 8); frame[6] = (unsigned char)(record.sensor & 0xFFu);
    frame[7] = (unsigned char)(record.value >> 8);  frame[8] = (unsigned char)(record.value & 0xFFu);
    frame[9] = sum9(frame);
    memcpy(out, frame, sizeof frame);
    *written = sizeof frame;
    return 1;
}

/* Write decode_frame here: 0 ok, 1 short, 2 bad header, 3 checksum, 4 out of range.
   int decode_frame(const unsigned char *f, size_t size, Reading *out); */

/* Decodes into a fresh sentinel and prints the result code and the output. */
static void check(const char *label, const unsigned char *bytes, size_t size) {
    Reading r = {77, 88};
    int result = decode_frame(bytes, size, &r);
    printf("%s: %s, %u %u\n", label, names[result], r.sensor, r.value);
}

int main(void) {
    unsigned char out[10];
    size_t written = 0;
    Reading first = {1, 25};
    int ok = encode_frame(first, out, sizeof out, &written);
    printf("encode 1/25 matches fixture: %s\n", ok && memcmp(out, fixture, 10) == 0 ? "yes" : "no");

    /* Write the six decode checks here, building each bad frame from a copy of the fixture. */
    return 0;
}
Hint 1: the idea

Each check guards the next one: size before any read, header before trusting the layout, checksum before believing the fields, range before publishing. On the test side, every bad frame starts as a copy of the fixture and breaks one rule, with octet 9 recomputed by sum9 whenever the rule broken is not the checksum.

Hint 2: a first step

Write decode_frame with only the size check and the publish, and write the first two checks in main. Run: both lines should already be right. Then add one check to the decoder and its bad frame to main together, and run after each pair.

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

typedef struct { unsigned sensor; unsigned value; } Reading;
static const unsigned char fixture[10] = {0x54, 1, 1, 0, 4, 0, 1, 0, 0x19, 0x74};
static const char *names[] = {"ok", "short", "bad header", "checksum", "out of range"};

static unsigned char sum9(const unsigned char *f) {
    unsigned char sum = 0;
    for (size_t i = 0; i < 9; ++i) { sum = (unsigned char)(sum + f[i]); }
    return sum;
}
static int valid_reading(Reading r) { return r.sensor >= 1 && r.sensor <= 4095 && r.value <= 10000; }

/* Given: Day 2's encoder, taking a Reading. */
int encode_frame(Reading record, unsigned char *out, size_t capacity, size_t *written) {
    if (capacity < 10 || !valid_reading(record)) { return 0; }
    unsigned char frame[10] = {0x54, 1, 1, 0, 4, 0, 0, 0, 0, 0};
    frame[5] = (unsigned char)(record.sensor >> 8); frame[6] = (unsigned char)(record.sensor & 0xFFu);
    frame[7] = (unsigned char)(record.value >> 8);  frame[8] = (unsigned char)(record.value & 0xFFu);
    frame[9] = sum9(frame);
    memcpy(out, frame, sizeof frame);
    *written = sizeof frame;
    return 1;
}

int decode_frame(const unsigned char *f, size_t size, Reading *out) {
    if (size < 10) { return 1; }
    if (f[0] != 0x54 || f[1] != 1 || f[2] != 1 || f[3] != 0 || f[4] != 4) { return 2; }
    if (sum9(f) != f[9]) { return 3; }
    Reading candidate = {((unsigned)f[5] << 8) | f[6], ((unsigned)f[7] << 8) | f[8]};
    if (!valid_reading(candidate)) { return 4; }
    *out = candidate;
    return 0;
}

/* Decodes into a fresh sentinel and prints the result code and the output. */
static void check(const char *label, const unsigned char *bytes, size_t size) {
    Reading r = {77, 88};
    int result = decode_frame(bytes, size, &r);
    printf("%s: %s, %u %u\n", label, names[result], r.sensor, r.value);
}

int main(void) {
    unsigned char out[10];
    size_t written = 0;
    Reading first = {1, 25};
    int ok = encode_frame(first, out, sizeof out, &written);
    printf("encode 1/25 matches fixture: %s\n", ok && memcmp(out, fixture, 10) == 0 ? "yes" : "no");

    check("decode fixture", fixture, 10);
    check("decode 9 bytes", fixture, 9);
    unsigned char frame[10];
    memcpy(frame, fixture, 10);
    frame[1] = 2; frame[9] = sum9(frame);
    check("decode version 2", frame, 10);
    memcpy(frame, fixture, 10);
    frame[8] ^= 0x01;
    check("decode flipped value bit", frame, 10);
    memcpy(frame, fixture, 10);
    frame[7] = 0x27; frame[8] = 0x11; frame[9] = sum9(frame);
    check("decode value 10001", frame, 10);
    Reading max = {4095, 10000};
    if (encode_frame(max, out, sizeof out, &written)) { check("round trip 4095/10000", out, written); }
    return 0;
}
Text
encode 1/25 matches fixture: yes
decode fixture: ok, 1 25
decode 9 bytes: short, 77 88
decode version 2: bad header, 77 88
decode flipped value bit: checksum, 77 88
decode value 10001: out of range, 77 88
round trip 4095/10000: ok, 4095 10000

The fixture pins the encoder to the specification, and the decoder is checked against the same independent bytes, so neither can drift with the other. Each bad frame breaks exactly one rule, which is why every failure line names a different code and keeps the sentinel 77 88. The maximum reading covers the upper endpoints of both domains. This decoder still needs all ten bytes in one call. Day 4 removes that assumption with a state machine that accepts bytes in any number of pieces.

Was this article helpful?

Your response is saved on this device.