Files
bfe-core1106-sdk/drivers/relays/relays.h
T
BFE EngineeringandClaude Opus 5.5 b18ed3888b
ci / test (push) Successful in 16s
ci / prune-artifacts (pull_request) Skipped
ci / kernel-build (pull_request) Skipped
ci / bench (push) Successful in 11s
ci / patches-apply (push) Successful in 22s
ci / prune-artifacts (push) Skipped
ci / kernel-build (push) Skipped
ci / quality (push) Successful in 37s
ci / badges (push) Skipped
ci / qemu-tools (pull_request) Successful in 45s
ci / qemu-tools (push) Successful in 49s
ci / test (pull_request) Successful in 17s
ci / mcdc (pull_request) Successful in 19s
ci / bench (pull_request) Successful in 8s
ci / mcdc (push) Successful in 23s
ci / patches-apply (pull_request) Successful in 22s
ci / quality (pull_request) Successful in 34s
ci / badges (pull_request) Skipped
relays: refuse a WARDEN_GPIO_ROOT too long for PATH_MAX
Sysfs paths were built in fixed 80/96-byte stack buffers with unchecked
snprintf, so a root longer than about 70 bytes was truncated and both relays
silently read as unavailable.

Each operation now formats all of its paths up front into PATH_MAX buffers
and checks every snprintf return. A root whose longest path does not fit is
refused with a stderr line and the call reports unavailable, off or no-op;
nothing is ever truncated. The export argument comes from a constant table
instead of runtime formatting.

Regression tests: a 200-byte root through the fake io (full path reaches
the backend), the exact PATH_MAX boundary, one byte over (refused and logged
on available, get and set, io untouched), and a real scratch tree under a
root over 200 bytes. MC/DC stays at 100% (50/50 conditions).

Fixes #10

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg
2026-10-02 11:01:40 -06:00

60 lines
2.5 KiB
C

/**
* The two onboard relays (86-Panel bottom board).
*
* RELAY1 <- GPIO1_A0 (sysfs gpio 32)
* RELAY2 <- GPIO1_A1 (sysfs gpio 33)
*
* gpiochip1 base is 32; A0..A7 are offsets 0..7. Nothing in the DT claims these
* (there is no relay driver), so we drive them through /sys/class/gpio directly.
* An off-by-one here would toggle the neighbouring RS485 pair (GPIO1_B0/B1)
* under a running Modbus master, so the numbers come from the schematic, not
* inference. State is never assumed at startup: a relay may be holding a
* contactor closed, and deciding it should be off because we just booted is not
* this module's call.
*
* Hardened for warden-sdk: the sysfs plumbing sits behind a `relay_io` seam so
* the decision logic runs and is measured to 100% MC/DC on the host, and the
* gpio root is `$WARDEN_GPIO_ROOT`-overridable so the real backend can run
* against a scratch tree in an integration test.
*
* Paths are built in PATH_MAX buffers. A root too long for its longest path
* ("<root>/gpioNN/direction" within PATH_MAX) is refused with a stderr line, and
* every call then reports unavailable / off / no-op; it is never truncated.
*/
#ifndef WARDEN_RELAYS_H
#define WARDEN_RELAYS_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define WARDEN_RELAY_COUNT 2
/** Display name for @p idx, e.g. "Relay 1"; "-" if out of range. */
const char * warden_relay_name(uint32_t idx);
/** Current output state, false if it cannot be read. */
bool warden_relay_get(uint32_t idx);
/** Drive the output. Exports and sets the direction on first use. */
void warden_relay_set(uint32_t idx, bool on);
/** True if the GPIO is exported and usable: the page says so if it is not. */
bool warden_relay_available(uint32_t idx);
/* --- Hardware-abstraction seam ---------------------------------------------
* The four filesystem primitives the relay logic needs. Production binds the
* sysfs backend (the default); a unit test binds an in-memory fake that can
* model export -> node-appears, which a passive scratch tree cannot. */
struct relay_io {
bool (*exists)(const char *path); /* stat(path)==0 */
void (*write)(const char *path, const char *value); /* best-effort */
bool (*read_line)(const char *path, char *buf, size_t n); /* a line was read */
bool (*read_int)(const char *path, int *out); /* one int parsed */
};
/** Swap the io backend (test hook). Pass NULL to restore the sysfs default. */
void warden_relay__set_io(const struct relay_io *io);
#endif /* WARDEN_RELAYS_H */