diff --git a/idf.sh b/idf.sh index 5e91478..1688bfa 100755 --- a/idf.sh +++ b/idf.sh @@ -1,9 +1,38 @@ -# This variabe should contain the GUID of the group responsible for the serial -# devices (e.g., 'dialout' for Ubuntu or 'uucp' for Arch) -USB_GUID=986 +#!/bin/bash -# This variable should contain the path to the serial device corresponding to -# the ESP32 board (e.g., '/dev/ttyUSB0' or '/dev/ttyACM0'). -USB_DEV=/dev/ttyUSB0 -docker run --device=${USB_DEV} --rm --user $(id -u):${USB_GUID} -v $PWD:/project -w /project -it hyperlink idf.py $@ +# +# Find the GID of the group responsible for the serial devices +# + + +USB_GID=$(stat -c %g /dev/ttyS0) + + +# +# Find available serial devices and create appropriate flags +# + + +DEVICE_FLAGS= + +# Taken from https://unix.stackexchange.com/a/144735 and modified slightly +for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do + DEVICE_FLAGS=${DEVICE_FLAGS}\ $( + syspath="${sysdevpath%/dev}" + devname="$(udevadm info -q name -p $syspath)" + [[ "$devname" == "bus/"* ]] && exit + eval "$(udevadm info -q property --export -p $syspath)" + [[ -z "$ID_SERIAL" ]] && exit + [[ "$devname" != "ttyUSB"* && "$devname" != "ttyACM"* ]] && exit + echo --device=/dev/$devname + ) +done + + +# +# Run command in docker container +# + + +docker run ${DEVICE_FLAGS} --rm --user $(id -u):${USB_GID} -v $PWD:/project -w /project -it hyperlink idf.py $@