Add repeater protocol and device firmware features
This commit is contained in:
@@ -14,3 +14,22 @@ trap 'rm -rf "$OUT"' EXIT
|
||||
-o "$OUT/test_wmbus" test_wmbus.c ../main/wmbus.c
|
||||
|
||||
"$OUT/test_wmbus"
|
||||
|
||||
"$CC" -std=c11 -O1 -g -Wall -Wextra -Werror \
|
||||
-fsanitize=address,undefined -fno-omit-frame-pointer \
|
||||
-I../main -o "$OUT/test_duty_ledger" \
|
||||
test_duty_ledger.c ../main/duty_ledger.c
|
||||
|
||||
"$OUT/test_duty_ledger"
|
||||
|
||||
"$CC" -std=c11 -O1 -g -Wall -Wextra -Werror \
|
||||
-fsanitize=address,undefined -fno-omit-frame-pointer \
|
||||
-I../main -o "$OUT/test_t1_long" \
|
||||
test_t1_long.c ../main/wmbus.c
|
||||
|
||||
"$OUT/test_t1_long"
|
||||
|
||||
"$CC" -std=c11 -O1 -g -Wall -Wextra -Werror \
|
||||
-I../main -o "$OUT/test_font_render" test_font_render.c
|
||||
|
||||
"$OUT/test_font_render" | tail -1
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "duty_ledger.h"
|
||||
|
||||
static void test_accumulates_without_event_limit(void)
|
||||
{
|
||||
duty_ledger_t ledger = {0};
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
duty_ledger_record(&ledger, 1000000, 100);
|
||||
}
|
||||
assert(duty_ledger_used(&ledger, 2000000) == 1000000);
|
||||
}
|
||||
|
||||
static void test_retains_every_overlapping_minute(void)
|
||||
{
|
||||
duty_ledger_t ledger = {0};
|
||||
for (int minute = 0; minute <= 60; minute++) {
|
||||
duty_ledger_record(&ledger, (int64_t)minute * DUTY_LEDGER_BUCKET_US, 100);
|
||||
}
|
||||
|
||||
// The oldest partially overlapping minute is deliberately included.
|
||||
assert(duty_ledger_used(&ledger,
|
||||
60 * DUTY_LEDGER_BUCKET_US + DUTY_LEDGER_BUCKET_US / 2) == 6100);
|
||||
|
||||
// Reusing the ring slot at minute 61 cannot discard in-window airtime.
|
||||
duty_ledger_record(&ledger, 61 * DUTY_LEDGER_BUCKET_US, 200);
|
||||
assert(duty_ledger_used(&ledger, 61 * DUTY_LEDGER_BUCKET_US) == 6200);
|
||||
}
|
||||
|
||||
static void test_expires_whole_old_buckets(void)
|
||||
{
|
||||
duty_ledger_t ledger = {0};
|
||||
duty_ledger_record(&ledger, 0, 123);
|
||||
assert(duty_ledger_used(&ledger, DUTY_LEDGER_WINDOW_US) == 123);
|
||||
assert(duty_ledger_used(&ledger,
|
||||
DUTY_LEDGER_WINDOW_US + DUTY_LEDGER_BUCKET_US) == 0);
|
||||
}
|
||||
|
||||
static void test_rejects_invalid_records(void)
|
||||
{
|
||||
duty_ledger_t ledger = {0};
|
||||
duty_ledger_record(&ledger, -1, 100);
|
||||
duty_ledger_record(&ledger, 1, 0);
|
||||
assert(duty_ledger_used(&ledger, 1) == 0);
|
||||
assert(duty_ledger_used(&ledger, -1) == 0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_accumulates_without_event_limit();
|
||||
test_retains_every_overlapping_minute();
|
||||
test_expires_whole_old_buckets();
|
||||
test_rejects_invalid_records();
|
||||
puts("duty ledger tests: PASS");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Host-side visual check of the 5x7 font: renders representative UI text
|
||||
// as ASCII art so the glyph table can be reviewed without hardware, and
|
||||
// asserts basic structural invariants.
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "font5x7.h"
|
||||
|
||||
static void render_line(const char *text)
|
||||
{
|
||||
char rows[FONT5X7_H][128];
|
||||
int cols = 0;
|
||||
memset(rows, 0, sizeof(rows));
|
||||
for (const char *p = text; *p && cols + 6 < 120; p++) {
|
||||
unsigned c = (unsigned char)*p;
|
||||
if (c < FONT5X7_FIRST || c > FONT5X7_LAST) c = '?';
|
||||
const uint8_t *g = font5x7[c - FONT5X7_FIRST];
|
||||
for (int x = 0; x < FONT5X7_W; x++) {
|
||||
for (int y = 0; y < FONT5X7_H; y++) {
|
||||
rows[y][cols + x] = (g[x] >> y) & 1 ? '#' : ' ';
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < FONT5X7_H; y++) rows[y][cols + 5] = ' ';
|
||||
cols += 6;
|
||||
}
|
||||
for (int y = 0; y < FONT5X7_H; y++) {
|
||||
rows[y][cols] = 0;
|
||||
printf("|%s|\n", rows[y]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Every printable glyph except space must have at least one pixel.
|
||||
for (int c = FONT5X7_FIRST + 1; c <= FONT5X7_LAST; c++) {
|
||||
int bits = 0;
|
||||
for (int x = 0; x < FONT5X7_W; x++) {
|
||||
const uint8_t *g = font5x7[c - FONT5X7_FIRST];
|
||||
for (int y = 0; y < FONT5X7_H; y++) bits += (g[x] >> y) & 1;
|
||||
assert((g[x] & 0x80) == 0); // 7 rows only
|
||||
}
|
||||
assert(bits > 0);
|
||||
}
|
||||
|
||||
render_line("WMBUS RPT 868.95 C1");
|
||||
render_line("RX 389 TX 381 OK");
|
||||
render_line("DUTY [####-] 0.09%");
|
||||
render_line("0123456789 +-/:=.#%");
|
||||
render_line("ABCDEFGHIJKLM");
|
||||
render_line("NOPQRSTUVWXYZ");
|
||||
puts("font render test: PASS (inspect art above)");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// T-mode 3-of-6 codec + long-frame CRC path tests (host, sanitizers).
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wmbus.h"
|
||||
|
||||
static void test_codec_roundtrip_all_values(void)
|
||||
{
|
||||
uint8_t data[256], coded[512], back[256];
|
||||
for (int i = 0; i < 256; i++) data[i] = (uint8_t)i;
|
||||
int clen = wmbus_3of6_encode(data, 256, coded, sizeof(coded));
|
||||
assert(clen == wmbus_3of6_coded_len(256));
|
||||
assert(clen == 384); // 256 * 1.5
|
||||
assert(wmbus_3of6_decode(coded, (size_t)clen, back, 256) == 0);
|
||||
assert(memcmp(data, back, 256) == 0);
|
||||
assert(wmbus_3of6_first_byte(coded, (size_t)clen) == 0x00);
|
||||
}
|
||||
|
||||
static void test_codec_detects_any_single_chip_flip(void)
|
||||
{
|
||||
// 3-of-6 codes have constant weight 3: one flipped chip changes the
|
||||
// weight, so every single-bit corruption inside the coded payload
|
||||
// must surface as an invalid code.
|
||||
uint8_t data[32], coded[64], back[32];
|
||||
for (int i = 0; i < 32; i++) data[i] = (uint8_t)(i * 37 + 5);
|
||||
int clen = wmbus_3of6_encode(data, 32, coded, sizeof(coded));
|
||||
assert(clen == 48);
|
||||
for (int bit = 0; bit < 32 * 12; bit++) {
|
||||
coded[bit >> 3] ^= (uint8_t)(0x80u >> (bit & 7));
|
||||
assert(wmbus_3of6_decode(coded, (size_t)clen, back, 32) > 0);
|
||||
coded[bit >> 3] ^= (uint8_t)(0x80u >> (bit & 7));
|
||||
}
|
||||
assert(wmbus_3of6_decode(coded, (size_t)clen, back, 32) == 0);
|
||||
}
|
||||
|
||||
static void test_codec_bounds(void)
|
||||
{
|
||||
uint8_t coded[8] = {0};
|
||||
uint8_t out[8];
|
||||
assert(wmbus_3of6_decode(coded, 2, out, 2) == -1); // 24 bits > 16
|
||||
assert(wmbus_3of6_first_byte(coded, 1) == -1);
|
||||
assert(wmbus_3of6_first_byte(coded, 2) == -1); // 000000 invalid
|
||||
uint8_t d[4] = {0};
|
||||
assert(wmbus_3of6_encode(d, 4, coded, 5) == -1); // needs 6
|
||||
}
|
||||
|
||||
static void test_t1_frame_roundtrip(void)
|
||||
{
|
||||
uint8_t frame[256], coded[512], back[256];
|
||||
int total = wmbus_build_test_frame(frame, sizeof(frame), 42, 100);
|
||||
assert(total > 0);
|
||||
int clen = wmbus_3of6_encode(frame, total, coded, sizeof(coded));
|
||||
assert(clen == wmbus_3of6_coded_len(total));
|
||||
assert(wmbus_3of6_first_byte(coded, (size_t)clen) == frame[0]);
|
||||
assert(wmbus_3of6_decode(coded, (size_t)clen, back, total) == 0);
|
||||
assert(memcmp(frame, back, total) == 0);
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_A, back, total));
|
||||
}
|
||||
|
||||
static void build_b_frame(uint8_t *out, uint8_t lfield)
|
||||
{
|
||||
int total = 1 + lfield;
|
||||
out[0] = lfield;
|
||||
for (int i = 1; i < total; i++) out[i] = (uint8_t)(i * 73 + 11);
|
||||
if (total > 128) {
|
||||
// two chunks: CRC over first 126 at [126..127], rest at the tail
|
||||
int rem = total - 128;
|
||||
assert(rem >= 3);
|
||||
uint16_t c = wmbus_crc16(out, 126);
|
||||
out[126] = c >> 8; out[127] = c & 0xFF;
|
||||
c = wmbus_crc16(out + 128, rem - 2);
|
||||
out[total - 2] = c >> 8; out[total - 1] = c & 0xFF;
|
||||
} else {
|
||||
uint16_t c = wmbus_crc16(out, total - 2);
|
||||
out[total - 2] = c >> 8; out[total - 1] = c & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_format_b_long(void)
|
||||
{
|
||||
uint8_t f[256];
|
||||
// > 128 bytes: two CRC chunks
|
||||
build_b_frame(f, 200);
|
||||
assert(wmbus_frame_total_len(WMBUS_FRAME_B, 200) == 201);
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 201));
|
||||
// corruption in either chunk must fail
|
||||
f[50] ^= 1;
|
||||
assert(!wmbus_check_crc(WMBUS_FRAME_B, f, 201));
|
||||
f[50] ^= 1;
|
||||
f[150] ^= 1;
|
||||
assert(!wmbus_check_crc(WMBUS_FRAME_B, f, 201));
|
||||
f[150] ^= 1;
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 201));
|
||||
// exactly one full chunk
|
||||
build_b_frame(f, 127);
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 128));
|
||||
// short frame
|
||||
build_b_frame(f, 20);
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 21));
|
||||
// maximum that fits the radio payload
|
||||
build_b_frame(f, 252);
|
||||
assert(wmbus_frame_total_len(WMBUS_FRAME_B, 252) == 253);
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_B, f, 253));
|
||||
}
|
||||
|
||||
static void test_format_a_long(void)
|
||||
{
|
||||
uint8_t frame[256];
|
||||
int total = wmbus_build_test_frame(frame, sizeof(frame), 7, 200);
|
||||
assert(total == 227); // 1 + 200 + 2 * (1 + ceil(191/16))
|
||||
assert(wmbus_check_crc(WMBUS_FRAME_A, frame, total));
|
||||
frame[220] ^= 1;
|
||||
assert(!wmbus_check_crc(WMBUS_FRAME_A, frame, total));
|
||||
// beyond the fixed radio capture: must be rejected, not truncated
|
||||
assert(wmbus_frame_total_len(WMBUS_FRAME_A, 255) == -1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_codec_roundtrip_all_values();
|
||||
test_codec_detects_any_single_chip_flip();
|
||||
test_codec_bounds();
|
||||
test_t1_frame_roundtrip();
|
||||
test_format_b_long();
|
||||
test_format_a_long();
|
||||
puts("t1 + long-frame tests: PASS");
|
||||
return 0;
|
||||
}
|
||||
+210
-1
@@ -162,9 +162,213 @@ static void test_builder_roundtrip(void)
|
||||
"builder must refuse a buffer that cannot hold the frame");
|
||||
}
|
||||
|
||||
// Pack cooked bytes independently of the production raw-frame walker. Only
|
||||
// the CRC primitive is shared (anchored by the captured known-answer test).
|
||||
static int pack_hop_frame(wmbus_frame_fmt_t fmt, uint8_t *cooked, int len,
|
||||
uint8_t *raw)
|
||||
{
|
||||
assert(len >= 10 && len <= 200);
|
||||
int total = fmt == WMBUS_FRAME_A
|
||||
? len + 2 * (1 + (len - 10 + 15) / 16)
|
||||
: len + (len > 126 ? 4 : 2);
|
||||
cooked[0] = (uint8_t)(fmt == WMBUS_FRAME_A ? len - 1 : total - 1);
|
||||
int src = 0, dst = 0;
|
||||
while (src < len) {
|
||||
int block = fmt == WMBUS_FRAME_A ? (src == 0 ? 10 : 16) : 126;
|
||||
if (block > len - src) block = len - src;
|
||||
memcpy(raw + dst, cooked + src, (size_t)block);
|
||||
uint16_t crc = wmbus_crc16(cooked + src, (size_t)block);
|
||||
raw[dst + block] = (uint8_t)(crc >> 8);
|
||||
raw[dst + block + 1] = (uint8_t)crc;
|
||||
src += block;
|
||||
dst += block + 2;
|
||||
}
|
||||
assert(dst == total);
|
||||
return total;
|
||||
}
|
||||
|
||||
static void init_hop_frame(uint8_t *cooked, int len, uint8_t ci)
|
||||
{
|
||||
for (int i = 0; i < len; i++) cooked[i] = (uint8_t)(i * 73 + 11);
|
||||
cooked[1] = 0x44;
|
||||
cooked[10] = ci;
|
||||
}
|
||||
|
||||
static void check_hop_mark(wmbus_frame_fmt_t fmt, uint8_t *cooked, int len,
|
||||
int h_offset, uint8_t h_mask)
|
||||
{
|
||||
uint8_t raw[300], expected[300];
|
||||
memset(raw, 0xA5, sizeof(raw));
|
||||
memset(expected, 0xA5, sizeof(expected));
|
||||
int total = pack_hop_frame(fmt, cooked, len, raw);
|
||||
CHECK(wmbus_check_crc(fmt, raw, total), "invalid hop input fixture");
|
||||
cooked[h_offset] |= h_mask;
|
||||
pack_hop_frame(fmt, cooked, len, expected);
|
||||
cooked[h_offset] &= (uint8_t)~h_mask;
|
||||
|
||||
CHECK(wmbus_mark_repeated(fmt, raw, total),
|
||||
"fmt=%d CI=%02X: unmarked frame refused", fmt, cooked[10]);
|
||||
CHECK(memcmp(raw, expected, sizeof(raw)) == 0,
|
||||
"fmt=%d CI=%02X: bytes other than H and its CRC changed",
|
||||
fmt, cooked[10]);
|
||||
CHECK(wmbus_check_crc(fmt, raw, total), "marked frame has invalid CRC");
|
||||
CHECK(!wmbus_mark_repeated(fmt, raw, total), "second hop accepted");
|
||||
CHECK(memcmp(raw, expected, sizeof(raw)) == 0,
|
||||
"refused second hop changed frame");
|
||||
|
||||
if (fmt == WMBUS_FRAME_A) {
|
||||
uint8_t coded[450], decoded[300];
|
||||
int coded_len = wmbus_3of6_encode(raw, total, coded, sizeof(coded));
|
||||
CHECK(coded_len > 0, "marked T1 frame did not encode");
|
||||
if (coded_len > 0) {
|
||||
CHECK(wmbus_3of6_first_byte(coded, (size_t)coded_len) == raw[0],
|
||||
"marked T1 frame lost L-field");
|
||||
CHECK(wmbus_3of6_decode(coded, (size_t)coded_len, decoded, total) == 0,
|
||||
"marked T1 frame did not decode");
|
||||
CHECK(memcmp(decoded, expected, (size_t)total) == 0,
|
||||
"T1 roundtrip lost hop mark or changed payload");
|
||||
CHECK(wmbus_check_crc(fmt, decoded, total),
|
||||
"decoded marked T1 frame has invalid CRC");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void check_hop_refused(wmbus_frame_fmt_t fmt, uint8_t *cooked, int len)
|
||||
{
|
||||
uint8_t raw[300], before[300];
|
||||
memset(raw, 0xA5, sizeof(raw));
|
||||
int total = pack_hop_frame(fmt, cooked, len, raw);
|
||||
memcpy(before, raw, sizeof(raw));
|
||||
CHECK(wmbus_check_crc(fmt, raw, total), "invalid refusal fixture");
|
||||
CHECK(!wmbus_mark_repeated(fmt, raw, total),
|
||||
"fmt=%d CI=%02X: unsupported/repeated frame accepted",
|
||||
fmt, cooked[10]);
|
||||
CHECK(memcmp(raw, before, sizeof(raw)) == 0,
|
||||
"refused frame changed bytes");
|
||||
}
|
||||
|
||||
static void test_hop_tpl(void)
|
||||
{
|
||||
static const uint8_t short_ci[] = {
|
||||
0x67, 0x6E, 0x74, 0x7A, 0x7D, 0x7F, 0x88, 0x8A, 0x9E, 0xC1, 0xC4,
|
||||
};
|
||||
static const uint8_t long_ci[] = {
|
||||
0x68, 0x6F, 0x72, 0x75, 0x7C, 0x7E, 0x87, 0x8B, 0x9F, 0xC2, 0xC5,
|
||||
};
|
||||
for (int f = WMBUS_FRAME_A; f <= WMBUS_FRAME_B; f++) {
|
||||
wmbus_frame_fmt_t fmt = (wmbus_frame_fmt_t)f;
|
||||
for (int long_header = 0; long_header <= 1; long_header++) {
|
||||
const uint8_t *cis = long_header ? long_ci : short_ci;
|
||||
int cf = long_header ? 21 : 13;
|
||||
uint8_t cooked[200];
|
||||
for (size_t i = 0; i < sizeof(short_ci); i++) {
|
||||
// Long bodies cross A's 16-byte blocks and B's 126-byte
|
||||
// first block; exact comparison protects all ciphertext.
|
||||
for (int mode = 0; mode <= 5; mode += 5) {
|
||||
init_hop_frame(cooked, sizeof(cooked), cis[i]);
|
||||
cooked[1] = i % 2 ? 0x46 : 0x44;
|
||||
cooked[cf] = 0xA4;
|
||||
cooked[cf + 1] = (uint8_t)(0xE0 | mode);
|
||||
check_hop_mark(fmt, cooked, sizeof(cooked), cf, 0x01);
|
||||
}
|
||||
}
|
||||
init_hop_frame(cooked, sizeof(cooked), cis[0]);
|
||||
cooked[cf] = 0xA4;
|
||||
cooked[cf + 1] = 7;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[cf + 1] = 5;
|
||||
cooked[cf] |= 0x01;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[cf] = 0xA6;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
|
||||
// A complete transport header needs no application payload,
|
||||
// but every shorter prefix must be refused without mutation.
|
||||
cooked[cf] = 0xA4;
|
||||
cooked[1] = 0x54; // not a masked variant of SND_NR
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[1] = 0x44;
|
||||
check_hop_mark(fmt, cooked, cf + 2, cf, 0x01);
|
||||
for (int len = 11; len < cf + 2; len++)
|
||||
check_hop_refused(fmt, cooked, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_hop_ell(void)
|
||||
{
|
||||
for (int f = WMBUS_FRAME_A; f <= WMBUS_FRAME_B; f++) {
|
||||
wmbus_frame_fmt_t fmt = (wmbus_frame_fmt_t)f;
|
||||
for (int long_header = 0; long_header <= 1; long_header++) {
|
||||
uint8_t cooked[200];
|
||||
int inner = long_header ? 21 : 13;
|
||||
init_hop_frame(cooked, sizeof(cooked), long_header ? 0x8E : 0x8C);
|
||||
cooked[11] = 0xE5; // preserve every non-H/R CC bit
|
||||
cooked[inner] = 0x72;
|
||||
cooked[inner + 11] = 0x03; // inner TPL already H=R=1
|
||||
cooked[inner + 12] = 7; // encrypted/authenticated inner TPL
|
||||
check_hop_mark(fmt, cooked, sizeof(cooked), 11, 0x10);
|
||||
// Exactly full and one-byte-over CRC blocks. For long ELL the
|
||||
// inner TPL crosses A's block boundary; it must remain opaque.
|
||||
int boundary = fmt == WMBUS_FRAME_A ? 26 : 126;
|
||||
check_hop_mark(fmt, cooked, boundary, 11, 0x10);
|
||||
check_hop_mark(fmt, cooked, boundary + 1, 11, 0x10);
|
||||
cooked[inner] = 0x90; // AFL is opaque beneath a usable ELL
|
||||
check_hop_mark(fmt, cooked, sizeof(cooked), 11, 0x10);
|
||||
cooked[inner] = 0x78;
|
||||
cooked[1] = 0x46;
|
||||
check_hop_mark(fmt, cooked, inner + 1, 11, 0x10);
|
||||
for (int len = 11; len <= inner; len++)
|
||||
check_hop_refused(fmt, cooked, len);
|
||||
cooked[11] |= 0x10;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
cooked[11] = 0xE7;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_hop_invalid_headers(void)
|
||||
{
|
||||
static const uint8_t unsupported_ci[] = {0x78, 0x8D, 0x8F, 0x90, 0x00, 0xFF};
|
||||
for (int f = WMBUS_FRAME_A; f <= WMBUS_FRAME_B; f++) {
|
||||
wmbus_frame_fmt_t fmt = (wmbus_frame_fmt_t)f;
|
||||
uint8_t cooked[40], raw[300], before[300];
|
||||
for (size_t i = 0; i < sizeof(unsupported_ci); i++) {
|
||||
init_hop_frame(cooked, sizeof(cooked), unsupported_ci[i]);
|
||||
cooked[11] = 0;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
}
|
||||
init_hop_frame(cooked, sizeof(cooked), 0x8C);
|
||||
cooked[11] = 0;
|
||||
// Only these two exact C-fields are valid, not masked variants.
|
||||
for (int c = 0; c <= 255; c++) {
|
||||
if (c == 0x44 || c == 0x46) continue;
|
||||
cooked[1] = (uint8_t)c;
|
||||
check_hop_refused(fmt, cooked, sizeof(cooked));
|
||||
}
|
||||
cooked[1] = 0x44;
|
||||
check_hop_refused(fmt, cooked, 10); // no CI; A has only block 1
|
||||
memset(raw, 0xA5, sizeof(raw));
|
||||
int total = pack_hop_frame(fmt, cooked, sizeof(cooked), raw);
|
||||
memcpy(before, raw, sizeof(raw));
|
||||
for (int len = -1; len <= total + 1; len++) {
|
||||
if (len == total) continue;
|
||||
CHECK(!wmbus_mark_repeated(fmt, raw, len),
|
||||
"mismatched raw length %d accepted (actual %d)", len, total);
|
||||
CHECK(memcmp(raw, before, sizeof(raw)) == 0,
|
||||
"invalid raw length changed frame");
|
||||
}
|
||||
CHECK(!wmbus_mark_repeated(WMBUS_FRAME_UNKNOWN, raw, total),
|
||||
"unknown format accepted");
|
||||
CHECK(memcmp(raw, before, sizeof(raw)) == 0,
|
||||
"unknown format changed frame");
|
||||
}
|
||||
}
|
||||
|
||||
// Adversarial input: random bytes with a length that disagrees with the
|
||||
// L-field, exactly what a corrupted reception looks like. Under ASan this
|
||||
// proves the CRC walk never indexes outside the buffer.
|
||||
// proves the CRC and hop-header walks never index outside the buffer.
|
||||
static void test_fuzz_bounds(void)
|
||||
{
|
||||
srand(12345);
|
||||
@@ -183,8 +387,10 @@ static void test_fuzz_bounds(void)
|
||||
int total = wmbus_frame_total_len(fmt, buf[0]);
|
||||
if (total > 0 && total <= n) {
|
||||
(void)wmbus_check_crc(fmt, buf, total);
|
||||
(void)wmbus_mark_repeated(fmt, buf, total);
|
||||
}
|
||||
(void)wmbus_check_crc(fmt, buf, n);
|
||||
(void)wmbus_mark_repeated(fmt, buf, n);
|
||||
(void)wmbus_hash(buf, (size_t)n);
|
||||
free(buf);
|
||||
}
|
||||
@@ -211,6 +417,9 @@ int main(void)
|
||||
test_length_formulas();
|
||||
test_frame_format_detection();
|
||||
test_builder_roundtrip();
|
||||
test_hop_tpl();
|
||||
test_hop_ell();
|
||||
test_hop_invalid_headers();
|
||||
test_fuzz_bounds();
|
||||
|
||||
if (failures) {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Host tool: feed captured post-sync payload bytes (hex on argv[1]) through
|
||||
// the firmware's T1 classification/decode path exactly as handle_rx does.
|
||||
// Prints the decode result for cross-checking against a known transmitted
|
||||
// frame. Not part of run_tests.sh; used by ad-hoc RF verification.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wmbus.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) { fprintf(stderr, "usage: %s HEXBYTES\n", argv[0]); return 2; }
|
||||
size_t hexlen = strlen(argv[1]);
|
||||
static uint8_t buf[512];
|
||||
size_t n = hexlen / 2;
|
||||
if (n > sizeof(buf)) n = sizeof(buf);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned v;
|
||||
if (sscanf(argv[1] + 2 * i, "%2x", &v) != 1) return 2;
|
||||
buf[i] = (uint8_t)v;
|
||||
}
|
||||
|
||||
int lfield = wmbus_3of6_first_byte(buf, n);
|
||||
printf("lfield=%d\n", lfield);
|
||||
if (lfield < 9) return 1;
|
||||
int total = wmbus_frame_total_len(WMBUS_FRAME_A, (uint8_t)lfield);
|
||||
printf("total=%d coded_len=%d have=%zu\n",
|
||||
total, total > 0 ? wmbus_3of6_coded_len(total) : -1, n);
|
||||
if (total < 0 || wmbus_3of6_coded_len(total) > (int)n) return 1;
|
||||
static uint8_t frame[512];
|
||||
int bad = wmbus_3of6_decode(buf, n, frame, total);
|
||||
int crc = wmbus_check_crc(WMBUS_FRAME_A, frame, total);
|
||||
printf("bad_codes=%d crc_ok=%d\nframe=", bad, crc);
|
||||
for (int i = 0; i < total; i++) printf("%02X", frame[i]);
|
||||
printf("\n");
|
||||
return crc ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user