a2cloud/setup/ttyusbhandler.txt

63 lines
2.3 KiB
Plaintext
Raw Normal View History

2015-10-30 12:34:48 +00:00
#! /bin/bash
2016-10-25 17:42:29 +00:00
# vim: set tabstop=4 shiftwidth=4 noexpandtab filetype=sh:
2015-10-30 11:24:15 +00:00
# called by udev as:
# ttyusbhandler [add|remove] ttyUSBname
# depending on what port ttyUSB adapter was added or removed from,
# automatically launches or kills ADTPro as needed
# restarts getty as needed
# remove:
# kill any ADTPro for this port
# if a getty is on this port, do nothing here, it will be killed and respawn
# iteself
# add:
# stagger adds by port number to prevent problems during simultaneous add at startup
# if lower port, solo adapter on lower port USB hub, or lower port of any USB hub,
# kill any ADTPro, rescan for getty if sleeping, and launch adtPro
# if upper port, solo adapter on upper port USB hub, or higher port of any USB hub,
# rescan for getty
if [[ $1 == "remove" ]]; then
2016-10-25 17:42:29 +00:00
rm /tmp/udev-$2-removed &> /dev/null
touch /tmp/udev-$2-removed
pkill -f "$2.*ADTPro"
2015-10-30 11:24:15 +00:00
elif [[ $1 == "add" ]]; then
2016-10-25 17:42:29 +00:00
[[ $2 == "ttyUSBlower" ]] && sleep 1.5
[[ ${#2} -gt 11 ]] && sleep "${2:15:2}"
if [[ $2 == "ttyUSBlower" || \
2016-10-28 15:10:53 +00:00
$2 == $(ls -1 /dev/ttyUSBlower_hub* 2> /dev/null | head -1 | cut -c 6-) || \
( ${2:0:12} == "ttyUSBlower_" && $2 != $(ls -1 /dev/ttyUSBupper_hub* 2> /dev/null | tail -1 | cut -c 6-) ) \
]]; then
2016-10-25 17:42:29 +00:00
rm /tmp/udev-ttyUSBlower-added &> /dev/null
touch /tmp/udev-ttyUSBlower-added
pkill -f "[A]DTPro"
pkill -f "[u]sbgetty"
exec echo "/usr/local/adtpro/adtpro.sh headless serial" | at -M now
else # ttyUSBupper
rm /tmp/udev-ttyUSBupper-added &> /dev/null
touch /tmp/udev-ttyUSBupper-added
pkill -f "[g]etty.*ttyUSB"
if [[ -f /bin/systemctl ]]; then # if systemd
# if USB-to-serial adapter is directly attached to upper port
if [[ -c /dev/ttyUSBupper ]]; then
ttyUSB=ttyUSBupper
# if hub in upper port, use highest numbered port on hub
elif [[ $(ls -1 /dev/ttyUSBupper_hub* 2> /dev/null | wc -l) -gt 0 ]]; then
ttyUSB=$(ls -1 /dev/ttyUSBupper_hub* 2> /dev/null | tail -1 | cut -c 6-)
# if hub in lower port with multiple adapters, use highest numbered port on hub
elif [[ $(ls -1 /dev/ttyUSBlower_hub* 2> /dev/null | wc -l) -gt 1 ]]; then
ttyUSB=$(ls -1 /dev/ttyUSBlower_hub* 2> /dev/null | tail -1 | cut -c 6-)
else
# by definition, this shouldn't happen
ttyUSB=
fi
exec systemctl restart usbgetty@$ttyUSB
fi
fi
2015-10-30 11:24:15 +00:00
else
2016-10-25 17:42:29 +00:00
exit 2
2015-10-30 11:24:15 +00:00
fi