Skip to content

Commit 6dfcc35

Browse files
author
Hakan Kaya
committed
feat(examples): add embedded GPIO tool, auto-load kernel modules, and fix I2C CUSE adapter
- Add generic-gpio-embedded tool using libgpiod to inspect, read, set, and monitor GPIO lines - Add examples/run_all.sh to build and validate all example applications - Add Makefile for generic-uart-embedded tool - Fix I2C CUSE adapter lifecycle parent PID check when executed via sudo/pkexec - Auto-load cuse and gpio-sim kernel modules during sudo session in run.sh - Include gpio-sim adapter in dev.sh and gpio_tool in build.sh and install
1 parent 7ad1424 commit 6dfcc35

10 files changed

Lines changed: 650 additions & 4 deletions

File tree

‎adapters/i2c-cuse/main.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,23 @@ static void *make_device_accessible(void *argument) {
5757
}
5858

5959
static int configure_lifecycle(vds4e_i2c_config_t *config) {
60+
if (prctl(PR_SET_PDEATHSIG, SIGTERM) != 0) {
61+
perror("failed to configure parent-death cleanup");
62+
return -1;
63+
}
64+
6065
if (config->parent_pid > 0) {
61-
if (prctl(PR_SET_PDEATHSIG, SIGTERM) != 0 ||
62-
getppid() != config->parent_pid) {
63-
return -1;
64-
}
6566
pthread_t watcher;
6667
if (pthread_create(&watcher, NULL, watch_parent, &config->parent_pid) != 0) {
68+
perror("failed to start parent watcher");
6769
return -1;
6870
}
6971
(void)pthread_detach(watcher);
7072
}
7173

7274
pthread_t permissions;
7375
if (pthread_create(&permissions, NULL, make_device_accessible, config) != 0) {
76+
perror("failed to start permissions worker");
7477
return -1;
7578
}
7679
(void)pthread_detach(permissions);

‎build.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ echo "Building generic UART Embedded Linux tool..."
9090
"$ROOT_DIR/examples/generic-uart-embedded/uart_ping.c" \
9191
-o "$EXAMPLE_BUILD_DIR/uart_ping"
9292

93+
echo "Building generic GPIO Embedded Linux tool..."
94+
"${CC:-cc}" \
95+
-O2 -g -Wall -Wextra -Wpedantic -Werror -std=c11 -D_POSIX_C_SOURCE=200809L \
96+
"$ROOT_DIR/examples/generic-gpio-embedded/gpio_tool.c" \
97+
-o "$EXAMPLE_BUILD_DIR/gpio_tool" \
98+
-lgpiod
99+
93100
touch "$STATE_DIR/.built"
94101

95102
echo

‎dev.sh‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ I2C_CUSE_BUILD_DIR="$ROOT_DIR/build/adapters/i2c-cuse"
1313
I2C_CUSE_EXECUTABLE="$I2C_CUSE_BUILD_DIR/vds4e-i2c-cuse"
1414
UART_PTY_BUILD_DIR="$ROOT_DIR/build/adapters/uart-pty"
1515
UART_PTY_EXECUTABLE="$UART_PTY_BUILD_DIR/vds4e-uart-pty"
16+
GPIO_SIM_BUILD_DIR="$ROOT_DIR/build/adapters/gpio-sim"
17+
GPIO_SIM_EXECUTABLE="$GPIO_SIM_BUILD_DIR/vds4e-gpio-sim"
1618
LOCK_FILE="${TMPDIR:-/tmp}/vds4e-dev-${UID}.lock"
1719
SERVER_PID=""
1820
WEB_PID=""
@@ -140,6 +142,14 @@ cmake \
140142
-DCMAKE_BUILD_TYPE=Debug
141143
cmake --build "$UART_PTY_BUILD_DIR" --parallel
142144

145+
echo "Configuring and building the GPIO simulator adapter..."
146+
prepare_cmake_build_dir "$ROOT_DIR/adapters/gpio-sim" "$GPIO_SIM_BUILD_DIR"
147+
cmake \
148+
-S "$ROOT_DIR/adapters/gpio-sim" \
149+
-B "$GPIO_SIM_BUILD_DIR" \
150+
-DCMAKE_BUILD_TYPE=Debug
151+
cmake --build "$GPIO_SIM_BUILD_DIR" --parallel
152+
143153
if [[ "${VDS4E_ADAPTER_AUTH:-pkexec}" == "sudo" ]]; then
144154
# sudo's default credential cache is terminal-scoped. Keep the server in
145155
# this terminal session so adapter helpers can use the one authorization
@@ -149,13 +159,15 @@ if [[ "${VDS4E_ADAPTER_AUTH:-pkexec}" == "sudo" ]]; then
149159
VDS4E_SPI_CUSE_EXECUTABLE="$SPI_CUSE_EXECUTABLE" \
150160
VDS4E_I2C_CUSE_EXECUTABLE="$I2C_CUSE_EXECUTABLE" \
151161
VDS4E_UART_PTY_EXECUTABLE="$UART_PTY_EXECUTABLE" \
162+
VDS4E_GPIO_SIM_EXECUTABLE="$GPIO_SIM_EXECUTABLE" \
152163
"$ROOT_DIR/build/rust/debug/vds-server" --config config/vds-server.yaml &
153164
SERVER_PID=$!
154165
else
155166
setsid env \
156167
VDS4E_SPI_CUSE_EXECUTABLE="$SPI_CUSE_EXECUTABLE" \
157168
VDS4E_I2C_CUSE_EXECUTABLE="$I2C_CUSE_EXECUTABLE" \
158169
VDS4E_UART_PTY_EXECUTABLE="$UART_PTY_EXECUTABLE" \
170+
VDS4E_GPIO_SIM_EXECUTABLE="$GPIO_SIM_EXECUTABLE" \
159171
cargo run -p vds-server -- --config config/vds-server.yaml &
160172
SERVER_PID=$!
161173
SERVER_OWN_PROCESS_GROUP=true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CC ?= cc
2+
CFLAGS ?= -O2 -g -Wall -Wextra -Wpedantic -Werror -std=c11 -D_POSIX_C_SOURCE=200809L
3+
LDFLAGS ?=
4+
LDLIBS ?= -lgpiod
5+
6+
.PHONY: all clean
7+
8+
all: gpio_tool
9+
10+
gpio_tool: gpio_tool.c
11+
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ $(LDLIBS)
12+
13+
clean:
14+
rm -f gpio_tool
15+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Generic GPIO Embedded Linux Tool (`gpio_tool`)
2+
3+
This example application uses the standard Linux `libgpiod` API to inspect, read, drive, and monitor virtual GPIO lines over a `/dev/gpiochipN` character device.
4+
5+
It is designed to work with the VDS4E [`generic-gpio-bank-32`](../../device-models/examples/generic-gpio-bank/) model:
6+
- **Lines 0–15 (`GPIO0`–`GPIO15`):** Device inputs from the virtual peripheral's perspective. The host drives these lines with `set`; the corresponding `GPIOx_STATE` register updates in VDS4E.
7+
- **Lines 16–31 (`GPIO16`–`GPIO31`):** Device outputs from the virtual peripheral's perspective. The host reads these lines with `get` or monitors edge transitions with `monitor`.
8+
9+
---
10+
11+
## Build
12+
13+
```bash
14+
make
15+
```
16+
17+
To clean build artifacts:
18+
```bash
19+
make clean
20+
```
21+
22+
---
23+
24+
## Usage
25+
26+
### 1. Show Controller Info & Line Table
27+
```bash
28+
./gpio_tool -c /dev/gpiochip0 info
29+
```
30+
31+
### 2. Read Device Output Line
32+
```bash
33+
./gpio_tool -c /dev/gpiochip0 get 16
34+
```
35+
36+
### 3. Drive Device Input Line
37+
```bash
38+
./gpio_tool -c /dev/gpiochip0 set 0 1
39+
```
40+
41+
To keep the line held in state:
42+
```bash
43+
./gpio_tool -c /dev/gpiochip0 set 0 1 --hold
44+
```
45+
46+
### 4. Monitor Edge Transitions (Interrupts / Events)
47+
```bash
48+
./gpio_tool -c /dev/gpiochip0 monitor 16
49+
```
50+
*(When `GPIO16_STATE` changes via Web UI or API, edge events appear in real time)*

0 commit comments

Comments
 (0)