Design
Scope
GPIO over /sys/class/gpio: export, direction, value, unexport, and the mapping from a chip's line offset to the sysfs global number. Left out: edge events (edge plus poll on value, which fs cannot wait on), active_low, and pin sharing between processes beyond what sysfs itself does. For those, the gpio library on /dev/gpiochipN is the interface.
Decisions
- The bundled module's interface, unchanged. The deck starts from the
gpiomodule of Jennifer 0.28.0, which 0.29.0 removed. Names, signatures, constants, the error kind and the environment variables stay as they were, so a program changes only its import line. A deck-specific error kind or variable names would break that for no gain. - Stateless, keyed by pin. A user module cannot hold mutable state, so the chip selection lives in an environment variable (
JENNIFER_GPIO_CHIP_LABEL) and every call reads the chip list again. The cost is a few small file reads per call, far below a GPIO round trip through sysfs. - No guessing the chip. The header chip is matched by label; none or several matches throw with the chips present, rather than taking chip 0.
- Export once. A pin already exported is not exported again (
EBUSYon real sysfs), so a secondsetupafter a crash or to change direction works. - A bounded wait for udev. About 1 s of retries (50 x 20 ms) shared by the direction and value writes of one
setup.
Errors
Error{kind: "gpio"} for everything, with the messages listed in the introduction. A failed fs read or write is rewrapped into that kind, so a caller catching "gpio" sees hardware failures too. No generic kind ("limit", "timeout") applies: the deck has no cap and no deadline other than the udev wait, which reports as "gpio" like the module did.
Security and limits
The deck writes only under the sysfs root and only the files export, unexport, gpioN/direction and gpioN/value. Pin numbers are checked to be non-negative and below the chip's ngpio; with GLOBAL they are written as given and the kernel validates them. Values read back are checked to be 0 or 1.
Testing and validation
src/gpiosysfs_test.j drives every call against a mock tree under the temp directory: Pi 4 numbering from base 512, the base-0 layout of older kernels, the Pi 5 chip list, ambiguous and missing chips, malformed chip attributes, the udev wait and its timeout, re-setup without re-export. The suites run on jennifer and jennifer-tiny. The deck carries the module's tests over unchanged; it has not been run on hardware since it moved.
Known limitations and open questions
- Kernels built without
CONFIG_GPIO_SYSFShave no/sys/class/gpio; every call then throws "base directory not found". - Edge detection is out of reach until
fscan wait forPOLLPRIon a file.
Dependencies on the interpreter
Floor 0.28.0, the last release that bundled the module, for both binaries. Only fs, os, convert, strings and time are used.