configure.ac: add libusb/libusbx support (for xpp)
[dahdi/tools.git] / xpp / astribank_hook
1 #! /bin/sh
2
3 me=`basename $0`
4 dir=`dirname $0`
5 LOGGER="logger -i -t '$me'"
6
7 # Always redirect stderr somewhere, otherwise the shell script will die
8 # when it tries to do I/O related stuff on closed file descriptor.
9 # Our default is to throw it down the bit-bucket.
10 #exec 2> /dev/console
11 ## If you wish to trace this script:
12 #exec 2> "/tmp/${me}_$XBUS_NAME" 1>&2
13
14 # Our directory in the beginning, so we can use local lab setup
15 PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
16
17 set -e
18
19 LOCK="/var/lock/twinstar_startup"
20
21 [ -r /etc/dahdi/init.conf ] && . /etc/dahdi/init.conf
22
23 # For lab testing
24 liveconf="$dir/liveconf/dahdi"
25
26 if [ -d "$liveconf" ]; then
27         dahdi_conf="$liveconf"
28 else
29         dahdi_conf="/etc/dahdi"
30 fi
31
32 if [ "$XPP_HOTPLUG_DAHDI" != yes ]; then
33         exit 0
34 fi
35
36 export XPPORDER_CONF="$dahdi_conf/xpp_order"
37 export DAHDI_CFG_CMD="dahdi_cfg -c $dahdi_conf/system.conf"
38 export CALLED_FROM_ATRIBANK_HOOK=yes
39
40 can_full_async() {
41         # Can we work aynchronously:
42         #    - Need modern Asterisk that accept hotplug DAHDI devices.
43         #    - Need DAHDI with "auto_assign_spans" == 0
44         if [ "$ASTERISK_SUPPORTS_DAHDI_HOTPLUG" = yes ]; then
45                 aas_param='/sys/module/dahdi/parameters/auto_assign_spans'
46                 aas=`cat "$aas_param" 2>/dev/null`
47                 if [ "$aas" = 0 ]; then
48                         return 0
49                 else
50                         $LOGGER "No async operation ($aas_param != 0)"
51                 fi
52         else
53                 $LOGGER "No async operation (ASTERISK_SUPPORTS_DAHDI_HOTPLUG!=yes)"
54         fi
55         return 1
56 }
57
58 check_xpporder_conf() {
59         if [ ! -r "$XPPORDER_CONF" ]; then
60                 (
61                         echo "Skip($ACTION): No '$XPPORDER_CONF'"
62                         echo "Removing uneeded startup semaphore"
63                         astribank_is_starting -v -r 2>&1
64                 ) 2>&1 | $LOGGER
65                 exit 0
66         fi
67 }
68
69 clean_lines() {
70         sed -e 's/#.*//' -e 'y/\t/ /' -e 's/^ *//' -e 's/ *$//' -e '$s/$/\n/' "$XPPORDER_CONF"
71 }
72
73 matched_devices() {
74         ready=`grep -H READY /sys/bus/astribanks/devices/*/xbus_state | sed 's,/xbus_state.*,,'`
75         for dev in $ready
76         do
77                 label=`cat "$dev/label"`
78                 connector=`cat "$dev/connector"`
79                 xbus=`echo "$dev" | sed 's,.*/,,'`
80                 lineno=`clean_lines | egrep -n "^${label}$|^@${connector}$" | cut -d: -f1`
81                 if [ "$lineno" != "" ]; then
82                         #echo "$xbus: $XPPORDER_CONF:$lineno -- Match ${label} @${connector}" | $LOGGER
83                         printf "${xbus}\t${label}\n"
84                 else
85                         echo "${xbus}: ${label} @${connector} not found in $XPPORDER_CONF: Ignore($ACTION)" | $LOGGER
86                 fi
87         done
88 }
89
90 # Wait until udev finished processing our requests
91 # so we know the device files were actually created
92 # before trying dahdi_cfg et-al.
93 wait_for_udev() {
94         UDEV_SETTLE_MAX_TIME=10
95
96         echo "Waiting for udev to settle down..."
97         if [ -x /sbin/udevsettle ]; then
98                 # Old system, stand-alone udevsettle command
99                 /sbin/udevsettle --timeout="$UDEV_SETTLE_MAX_TIME"
100         elif [ -x /sbin/udevadm ]; then
101                 # Assume modern system, udevadm has settle parameter
102                 if ! /sbin/udevadm settle --timeout="$UDEV_SETTLE_MAX_TIME"
103                 then
104                         echo "udevadm failed ($?)."
105                         echo "Fallback to sleep $UDEV_SETTLE_MAX_TIME seconds."
106                         sleep "$UDEV_SETTLE_MAX_TIME"
107                 fi
108         else
109                 echo "No udevsettle/udevadm."
110                 echo "Fallback to sleep $UDEV_SETTLE_MAX_TIME seconds."
111                 sleep "$UDEV_SETTLE_MAX_TIME"
112         fi
113         sleep 1 # Wait a bit more (races)
114 }
115
116 start_dahdi() {
117         wait_for_udev
118         script=/etc/init.d/dahdi
119         echo "Starting $script."
120         "$script" start | logger -i -t "$script"
121         status=$?
122         echo "Status($script): $status"
123         if [ -x "$dir/twinstar_hook" ]; then
124                 "$dir/twinstar_hook"
125         fi
126         # Finished astribanks
127         echo "Removing semaphore"
128         astribank_is_starting -v -r
129         rm -f "$LOCK"
130 }
131
132 old_synchronous_start() {
133         NUM_GOOD=`matched_devices | wc -l`
134         NUM_WANTED=`clean_lines | sed '/^$/d' | wc -l`
135         echo "$ACTION($XBUS_NAME): $NUM_GOOD/$NUM_WANTED from $XPPORDER_CONF" | $LOGGER
136         if [ "$NUM_GOOD" -eq "$NUM_WANTED" ]; then
137                 (
138                         # Delay the initialization of the Astribank until the filesystem
139                         # is mounted read-write:
140                         test_file="/var/lock/astribank_test_file"
141                         for i in `seq 1 20`; do
142                                 if touch $test_file 2> /dev/null; then
143                                         rm -f $test_file
144                                         break
145                                 else
146                                         echo "$0: [$i] - Failed writing '$test_file'...waiting" | $LOGGER
147                                         sleep 1;
148                                 fi
149                         done
150
151                         if ln -s "$XBUS_NAME" "$LOCK"; then
152                                 echo "START-DAHDI: Total $NUM_GOOD online." | $LOGGER
153                                 # Fork services
154                                 start_dahdi < /dev/null 2>&1 | $LOGGER
155                         else
156                                 echo "$0: Was started: $(ls -l $LOCK)" | $LOGGER
157                         fi
158                 ) < /dev/null 2>&1 | $LOGGER &
159         fi
160 }
161
162 old_synchronous_stop() {
163         NUM_GOOD=`matched_devices | wc -l`
164         NUM_WANTED=`clean_lines | sed '/^$/d' | wc -l`
165         echo "$ACTION($XBUS_NAME): $NUM_GOOD/$NUM_WANTED from $XPPORDER_CONF" | $LOGGER
166         if [ "$NUM_GOOD" -eq 0 ]; then
167                 echo "All Astribanks offline" | $LOGGER
168                 if [ -x "$dir/twinstar_hook" ]; then
169                         "$dir/twinstar_hook" || :
170                 fi
171                 rm -f "$LOCK"
172         fi
173 }
174
175 ab_list() {
176         find /sys/devices -name idVendor 2>/dev/null | \
177                 xargs grep -H  'e4e4' 2>/dev/null | \
178                 sed -e 's/idVendor.*/idProduct/' | xargs grep -H '11[3456]' | \
179                 sed 's,/[^/]*$,,' || :
180 }
181
182 tws_watchdog_enable() {
183         devdir="/sys$DEVPATH"
184         label=`cat "$devdir/label"`
185         connector=`cat "$devdir/connector"`
186         xbus=`echo "$devdir" | sed 's,.*/,,'`
187         prefix="${xbus}: [${label}] @${connector}"
188         TWS_NOAUTOJUMPFILE="$TWS_DIR/twinstar_no_autojump"
189         if [ -e "$TWS_NOAUTOJUMPFILE" ]; then
190                 $LOGGER "$prefix: ignore wd (found $TWS_NOAUTOJUMPFILE)"
191         else
192                 # Re-arm Astribank watchdog
193                 transportdir="$devdir/transport"
194                 busnum=`cat "$transportdir/busnum" 2>/dev/null || :`
195                 devnum=`cat "$transportdir/devnum" 2>/dev/null || :`
196                 devaddr=`printf "%03d/%03d" "$busnum" "$devnum"`
197                 $LOGGER "$prefix: enabling twinstar watchdog"
198                 astribank_tool -D "$devaddr" -w 1 2>&1 | $LOGGER
199         fi
200 }
201
202 #echo "$0: $ACTION($XBUS_NAME)" | $LOGGER
203 case "$ACTION" in
204 add)
205         ;;
206 remove)
207         ab=`ab_list | wc -l`
208         if [ "$ab" -eq 0 ]; then
209                 $LOGGER "$prefix: No more Astribanks -- remove astribank_is_starting semaphore"
210                 astribank_is_starting -v -r 2>&1 | $LOGGER
211         fi
212         ;;
213 online)
214         if can_full_async; then
215                 tws_watchdog_enable
216         else
217                 old_synchronous_start
218         fi
219         ;;
220 offline)
221         if can_full_async; then
222                 :       # Nothing to do
223         else
224                 old_synchronous_stop
225         fi
226         ;;
227 *)
228         echo "$0: Unknown ACTION='$ACTION'" | $LOGGER
229         echo "$0: ARGS='$*'" | $LOGGER
230         echo "$0: ENV:" | $LOGGER
231         env | $LOGGER
232         exit 1
233 esac
234