Created
July 28, 2021 22:48
-
-
Save Leo-PL/557a7de9f9e4771e744d01a9bf15d9e2 to your computer and use it in GitHub Desktop.
OpenWrt persistent serial port symlinks
This file contains hidden or 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
# Creates persistent serial port symlinks for USB devices, | |
# like mobile broadband modems, USB-UART bridges, etc, | |
# with layout very similar to udev symlinks | |
# Put under /etc/hotplug.d/tty | |
[ "${ACTION}" = "add" -o "${ACTION}" = "remove" ] || exit 0 | |
[ "${SUBSYSTEM}" = "tty" ] || exit 0 | |
[ -n "${DEVNAME}" -a -n "${DEVPATH}" ] || exit 1 | |
if [ "${ACTION}" = "add" ]; then | |
subsystem="$(basename $(readlink /sys${DEVPATH}/../../../subsystem))" | |
[ "$subsystem" = "usb" ] || exit 0 | |
manufacturer="$(cat /sys${DEVPATH}/../../../../manufacturer)" | |
product="$(cat /sys${DEVPATH}/../../../../product)" | |
serial="$(cat /sys${DEVPATH}/../../../../serial)" | |
interface="$(cat /sys${DEVPATH}/../../../bInterfaceNumber)" | |
port="$(cat /sys${DEVPATH}/device/port_number)" | |
id_link=$(echo "${subsystem}"-"${manufacturer}"_"${product}"-if"${interface}"-port"${port}" | sed s/[^0-9A-Za-z-]/_/g) | |
path_link=$(echo "${DEVPATH}" | sed s%/devices/%% | sed s%/${DEVNAME}/tty/${DEVNAME}%%g | sed s/[^0-9A-Za-z-]/_/g) | |
mkdir -p /dev/serial/by-id /dev/serial/by-path | |
ln -sf "/dev/${DEVNAME}" "/dev/serial/by-id/${id_link}" | |
ln -sf "/dev/${DEVNAME}" "/dev/serial/by-path/${path_link}" | |
elif [ "${ACTION}" = "remove" ]; then | |
for link in $(find /dev/serial -type l); do | |
[ -L ${link} -a "$(readlink ${link})" = "/dev/$DEVNAME" ] && rm ${link} | |
done | |
fi |
Since I posted it on pastebin-like site, just assume it's public domain.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please add information about license of this repository — is it allowed to copy, change and use the code in any purpose?
It would be great to see any popular open source license.