Files
ZephCore/zephcore/helpers/PrefsCodec.h
T
ZephCore T096andClaude Sonnet 5 f04546df56 prefs: encode led_brightness too, not just decode; fix pinned test layout
companionPrefsEncode()/serverPrefsEncode() never wrote the led_brightness
byte that companionPrefsDecode()/serverPrefsDecode() already read,
desyncing every field that follows it by one byte on the companion side
(wifi_enabled/wifi_ssid/wifi_pwd) and silently truncating the server
format by one byte. Caught by CI's ASan/UBSan run on the host regression
suite: an out-of-bounds read/write in the pinned layout tests.

Since led_brightness never existed upstream, nothing already accounted
for it: fixing only the encoder still leaves the pinned byte tables and
the documented 272/311-byte totals (COMPANION_PREFS_SIZE/
SERVER_PREFS_SIZE, used by the real prefs stores too, not just tests)
one byte short of what the encoder now actually produces.

- PrefsCodec.h: bump both size constants (272->273, 311->312).
- PrefsCodec.cpp: add the missing w.put() in both encoders, mirroring
  the position already used by both decoders.
- prefs_codec_tests.cpp: add led_brightness to both layout tables at its
  real file offset (shifts wifi_enabled/wifi_ssid/wifi_pwd by one byte
  on the companion side; server's is appended last, nothing else
  shifts), extend both historical-boundary length lists with the new
  full-format length, and give it a non-default value in sample() so
  the layout/roundtrip tests actually exercise it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-08 18:27:00 +02:00

37 lines
1.5 KiB
C

/*
* SPDX-License-Identifier: MIT
* PrefsCodec - byte codecs for the two binary prefs files.
*
* Companion /lfs/new_prefs and server /lfs/repeater/prefs are serialized field
* by field, not as struct dumps; the offsets are the file layout (byte maps in
* memory prefs-format.md). Zephyr-free so the host tests can pin both layouts.
* The stores only move bytes between these and the filesystem.
*/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include "NodePrefs.h"
#define COMPANION_PREFS_SIZE 273
#define SERVER_PREFS_SIZE 312
/* Companion file layout, including the fields shared with upstream's former
* new_prefs up to offset 92. Returns the byte count (COMPANION_PREFS_SIZE),
* or 0 if cap is too small. */
size_t companionPrefsEncode(const NodePrefs &p, uint8_t *buf, size_t cap);
/* Returns false and leaves p untouched when the data cannot be ours: shorter
* than 90 bytes, or radio parameters outside ZC_RADIO_*. Fields past len keep
* their value in p, which the caller has filled with initNodePrefs(). */
bool companionPrefsDecode(NodePrefs &p, const uint8_t *buf, size_t len);
/* Repeater / room server / observer layout (upstream CommonCLI order to 290,
* then ZephCore's tail). Returns SERVER_PREFS_SIZE, or 0 if cap is too small. */
size_t serverPrefsEncode(const NodePrefs &p, uint8_t *buf, size_t cap);
/* Fields past len keep their value in p. Radio parameters outside ZC_RADIO_*
* are replaced by the LoRaConfig defaults; everything is sanitized. */
void serverPrefsDecode(NodePrefs &p, const uint8_t *buf, size_t len);