Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:base-system:hotplug [2024/01/15 17:01] – [Table] systemcrashdocs:guide-user:base-system:hotplug [2024/08/23 10:25] – Avoid duplication/duplicate execution oliv3r
Line 22: Line 22:
 | **ntp**       | Time sync events: time step, time server stratum change                                                   | | **ntp**       | Time sync events: time step, time server stratum change                                                   |
 | **tftp**      | TFTP-related events                                                                                       | | **tftp**      | TFTP-related events                                                                                       |
 +| **tty**       | TTY-related events, including but not limited to WWAN modems, but pointing directly to TTY interface      |
 | **usb**       | USB devices like 3g-modem and tty*                                                                        | | **usb**       | USB devices like 3g-modem and tty*                                                                        |
 | **usbmisc**   | Special USB peripherals like printers (scripts in this subsystem have fewer variables available)          | | **usbmisc**   | Special USB peripherals like printers (scripts in this subsystem have fewer variables available)          |
Line 146: Line 147:
  
 Even without NTP sync, you will receive a "periodic" hotplug event, with ''stratum=16'', about every 11 minutes out of the box. Even without NTP sync, you will receive a "periodic" hotplug event, with ''stratum=16'', about every 11 minutes out of the box.
 +
 +==== tty ====
 +^ Variable name ^ description ^
 +| ACTION | add, remove, bind, unbind as above |
 +| DEVICENAME | eg "ttyUSB2" |
 +| DEVNAME | only for bind/unbind, eg, "ttyUSB2" |
 +| DEVPATH | eg, "/devices/platform/ahb/1b400000.usb/usb2/2-1/2-1:1.3/ttyUSB2" |
 +| SEQNUM | (action number since boot for this subsystem) eg 335 |
 +| SUBSYSTEM | Specific type of device, eg, "usb-serial" |
 +| MAJOR | eg 189 |
 +| MINOR | eg 1 |
  
 ==== usb ==== ==== usb ====
Line 186: Line 198:
 ==== Symlink instead of rename a device ==== ==== Symlink instead of rename a device ====
 An other script to create a symlink instead of renaming the device. An other script to create a symlink instead of renaming the device.
-I test if DEVICE_NAME is empty because when I plug usb device I retrieve two add event, and the first come before created device, so symlink fails. 
  
 <code bash> <code bash>
 cat << "EOF" > /etc/hotplug.d/usb/20-cp210x cat << "EOF" > /etc/hotplug.d/usb/20-cp210x
-CP210_PRODID="10c4/ea60/100"+CP210_PRODID='10c4/ea60/100'
 SYMLINK="my_link" SYMLINK="my_link"
  
-if [ "${PRODUCT}" = "${CP210_PRODID}" ]; then +set -eu
-    if [ "${ACTION}" = "add" ]; then +
-          DEVICE_NAME=$(ls /sys/${DEVPATH} | grep tty) +
-          if [ -z ${DEVICE_NAME} ]; then +
-              logger -t hotplug "Warning DEVICE_NAME is empty" +
-              exit 0 +
-          fi +
-          logger -t hotplug "Device name of cp210 is ${DEVICE_NAME}" +
-          ln -s /dev/${DEVICE_NAME} /dev/${SYMLINK} +
-          logger -t hotplug "Symlink from /dev/${DEVICE_NAME} to /dev/${SYMLINK} created" +
-    fi +
-fi+
  
-if [ "${PRODUCT}" = "${CP210_PRODID}" ]; then +if [ "${DEVTYPE:-}" = 'usb_interface' ] && \ 
-    if [ "${ACTION}" = "remove" ]; then +   [ "${PRODUCT:-}" = "${CP210_PRODID}" ]; then 
-        rm /dev/${SYMLINK} + if [ "${ACTION:-}" = 'bind' ]; then 
-        logger -t hotplug "Symlink /dev/${SYMLINK} removed" + if [ -L "/dev/${SYMLINK}" ]; then 
-    fi+ logger -t hotplug "Symlink '/dev/${SYMLINK}' already exists" 
 + exit 0 
 + fi 
 + 
 + DEVICE_NAME="$(find /sys${DEVPATH:-} -maxdepth 1 -type d -iname 'ttyUSB*' -exec basename {} \;)" 
 + if [ -z "${DEVICE_NAME}" ]; then 
 + logger -t hotplug 'Warning: DEVICE_NAME is empty' 
 + exit 0 
 + fi 
 + 
 + logger -t hotplug "Device name of cp210 is '${DEVICE_NAME}'" 
 + ln -s "/dev/${DEVICE_NAME}" "/dev/${SYMLINK}" 
 + logger -t hotplug "Symlink from '/dev/${DEVICE_NAME}' to '/dev/${SYMLINK}' created" 
 + fi 
 + 
 + 
 + if [ "${ACTION:-}" = 'unbind' ]; then 
 + rm "/dev/${SYMLINK}" 
 + logger -t hotplug "Symlink '/dev/${SYMLINK}removed" 
 + fi
 fi fi
 EOF EOF
 </code> </code>
- 
 ==== Script that detects whether plugged usb device is bluetooth or not ==== ==== Script that detects whether plugged usb device is bluetooth or not ====
 <code bash> <code bash>
Line 454: Line 472:
 </code> </code>
  
 +==== Create deterministic/persistent links to USB-serial devices ====
 +Resulting links will be placed in /dev/serial/by-id and /dev/serial/by-path,
 +with a name structure closely resembling links created by udev, but not fully exact.
 +
 +<code bash>
 +set -o pipefail
 +[ "${ACTION}" = "bind" -o "${ACTION}" = "unbind" ] || exit 0
 +[ "${SUBSYSTEM}" = "usb-serial" ] || exit 0
 +[ -n "${DEVICENAME}" -a -n "${DEVPATH}" ] || exit 1
 +
 +if [ "${ACTION}" = "bind" ]; then
 +        subsystem="$(basename $(readlink /sys${DEVPATH}/../subsystem))"
 +
 +        [ "$subsystem" = "usb" ] || exit 0
 +
 +        replace_whitespace="s/^[ \t]*|[ \t]*$//g; s/[ \t]+/_/g"
 +        manufacturer="$(cat /sys${DEVPATH}/../../manufacturer | sed -E "${replace_whitespace}")" || manufacturer="$(cat /sys${DEVPATH}/../../idVendor)"
 +        product="$(cat /sys${DEVPATH}/../../product | sed -E "${replace_whitespace}")" || product="$(cat /sys${DEVPATH}/../../idProduct)"
 +        serial="$(cat /sys${DEVPATH}/../../serial | sed -E "${replace_whitespace}")"
 +        interface="$(cat /sys${DEVPATH}/../bInterfaceNumber)"
 +        port="$(cat /sys${DEVPATH}/port_number)"
 +
 +        replace_chars="s/[^0-9A-Za-z#+.:=@-]/_/g"
 +        id_link=$(echo "${subsystem}"-"${manufacturer}"_"${product}${serial:+_}${serial}"-if"${interface}${port:+-port}${port}" | sed "${replace_chars}")
 +        path_link=$(echo "${DEVPATH}${port:+-port}${port}" | sed "s%/devices/%%; s%/${DEVICENAME}%%g; ${replace_chars}")
 +
 +        mkdir -p /dev/serial/by-id /dev/serial/by-path
 +        ln -sf "/dev/${DEVICENAME}" "/dev/serial/by-id/${id_link}"
 +        ln -sf "/dev/${DEVICENAME}" "/dev/serial/by-path/${path_link}"
 +elif [ "${ACTION}" = "unbind" ]; then
 +        for link in $(find /dev/serial -type l); do
 +                [ -L ${link} -a "$(readlink ${link})" = "/dev/$DEVICENAME" ] && rm ${link}
 +        done
 +fi
 +</code>
 +[[https://gist.github.com/Leo-PL/b5ee737e49b34c1551dba6c182707c8e|(Source)]]
  • Last modified: 2024/12/19 04:42
  • by pythonic