-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(esp_usb): Run esp_tinyusb test apps in CI
- New CI target runner esp32s2 usb_device - Runner setup to allow docker container an access to USB devices
- Loading branch information
1 parent
dd9fca9
commit fa22798
Showing
5 changed files
with
160 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# CI target runner setup | ||
|
||
In order to allow a docker container, running on a CI target runner, an access to USB devices connected to the CI target runner some modifications shall be made on a CI target runner. In our case, it's a `RPI` target runner. | ||
|
||
The main idea, comes from this reply on [stackoverflow](https://stackoverflow.com/a/66427245/19840830). The same approach is also recommended in the official docker [documentation](https://docs.docker.com/reference/cli/docker/container/run/#device-cgroup-rule) | ||
|
||
|
||
### Following changes shall be made on a CI target runner | ||
|
||
- [`UDEV rules`](#udev-rules) | ||
- [`Docker tty script`](#docker-tty-script) | ||
- [`Logging`](#logging) | ||
- [`Running a docker container`](#running-a-docker-container) | ||
|
||
## UDEV rules | ||
|
||
- This UDEV rule, will trigger a `docker_tty.sh` script, every time a USB device is connected, or disconnected from a host machine (CI target runner). Also, when a new USB device is enumerated by the host machine. | ||
- Location: `/etc/udev/rules.d/99-docker-tty.rules` | ||
- `99-docker-tty.rules` file content: | ||
|
||
``` | ||
ACTION=="add", SUBSYSTEM=="tty", RUN+="/usr/local/bin/docker_tty.sh 'added' '%E{DEVNAME}' '%M' '%m'" | ||
ACTION=="remove", SUBSYSTEM=="tty", RUN+="/usr/local/bin/docker_tty.sh 'removed' '%E{DEVNAME}' '%M' '%m'" | ||
``` | ||
|
||
## Docker tty script | ||
|
||
- This `.sh` script (triggered by the UDEV rule above) will propagate a USB devices to a running docker container | ||
- Location: `/usr/local/bin/docker_tty.sh` | ||
- `docker_tty.sh` file content: | ||
|
||
``` | ||
#!/usr/bin/env bash | ||
# Log the USB event with parameters | ||
echo "USB event: $1 $2 $3 $4" >> /tmp/docker_tty.log | ||
# Find a running Docker container (using the first one found) | ||
docker_name=$(docker ps --format "{{.Names}}" | head -n 1) | ||
# Check if a container was found | ||
if [ ! -z "$docker_name" ]; then | ||
if [ "$1" == "added" ]; then | ||
docker exec -u 0 "$docker_name" mknod $2 c $3 $4 | ||
docker exec -u 0 "$docker_name" chmod -R 777 $2 | ||
echo "Adding $2 to Docker container $docker_name" >> /tmp/docker_tty.log | ||
else | ||
docker exec -u 0 "$docker_name" rm $2 | ||
echo "Removing $2 from Docker container $docker_name" >> /tmp/docker_tty.log | ||
fi | ||
else | ||
echo "No running Docker containers found." >> /tmp/docker_tty.log | ||
fi | ||
``` | ||
|
||
### Making the script executable | ||
|
||
Don't forget to make the created script executable | ||
``` | ||
root@~$ chmod +x /usr/local/bin/docker_tty.sh | ||
``` | ||
|
||
## Logging | ||
|
||
- The `docker_tty.sh` script logs information about the USB devices, it processes. | ||
- Location: `/tmp/docker_tty.log` | ||
- An example of log from the `docker_tty.log` file showing a flow of `pytest_usb_device.py` test | ||
``` | ||
USB event: added /dev/ttyACM0 166 0 | ||
USB event: added /dev/ttyACM1 166 1 | ||
Adding /dev/ttyACM0 to Docker container d5e5c774174b435b8befea864f8fcb7f_python311bookworm_6a975d | ||
Adding /dev/ttyACM1 to Docker container d5e5c774174b435b8befea864f8fcb7f_python311bookworm_6a975d | ||
USB event: removed /dev/ttyACM0 166 0 | ||
USB event: removed /dev/ttyACM1 166 1 | ||
``` | ||
|
||
## Running a docker container | ||
|
||
### Check major and minor numbers of connected devices | ||
|
||
Check which minor and major numbers Linux kernel assigns to devices, we want a docker container to have an access to. | ||
In our case we want to access `/dev/ttyUSB0`, `/dev/ttyACM0` and `/dev/ttyACM1` | ||
|
||
`/dev/ttyUSB0`: Major 188, Minor 0 | ||
``` | ||
peter@BrnoRPIG007:~ $ ls -l /dev/ttyUSB0 | ||
crw-rw-rw- 1 root dialout 188, 0 Nov 12 11:08 /dev/ttyUSB0 | ||
``` | ||
|
||
`/dev/ttyACM0` and `/dev/ttyACM1`: Major 166, Minor 0 (1) | ||
``` | ||
peter@BrnoRPIG007:~ $ ls -l /dev/ttyACM0 | ||
crw-rw---- 1 root dialout 166, 0 Nov 13 10:26 /dev/ttyACM0 | ||
peter@BrnoRPIG007:~ $ ls -l /dev/ttyACM1 | ||
crw-rw---- 1 root dialout 166, 1 Nov 13 10:26 /dev/ttyACM1 | ||
``` | ||
|
||
### Run a docker container | ||
|
||
Run a docker container with following extra options: | ||
``` | ||
docker run --device-cgroup-rule='c 188:* rmw' --device-cgroup-rule='c 166:* rmw' --group-add plugdev --privileged .. | ||
``` | ||
- `--device-cgroup-rule='c 188:* rmw'`: allow access to `ttyUSBx` (Major 188, all Minor) | ||
- `--device-cgroup-rule='c 166:* rmw'`: allow access to `ttyACMx` (Major 166, all Minor) | ||
- `--group-add plugdev`: add plugdev group | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters