Debian Codegraph @

xpp/waitfor_xpds

raw
1 #! /bin/sh
2
3 # waitfor_xpds: wait until all Astribanks were initialized
4 # $Id$
5
6 # Written by Oron Peled <[email protected]>
7 # Copyright (C) 2008-2009, Xorcom
8 #
9 # All rights reserved.
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #
25
26
27 set -e
28
29 # For lab testing
30 mydir=`dirname $0`
31 PATH="${mydir}:${PATH}"
32 XPP_WAIT_AB_TIMEOUT=100
33
34 [ -r /etc/dahdi/init.conf ] && . /etc/dahdi/init.conf
35
36 ab_list() {
37 find /sys/devices -name idVendor 2>/dev/null | \
38 xargs grep -H 'e4e4' 2>/dev/null | \
39 sed -e 's/idVendor.*/idProduct/' | xargs grep -H '11[3456]' | \
40 sed 's,/[^/]*$,,' || :
41 }
42
43 ab_serial_nums() {
44 for i in `ab_list`; do
45 s=`cat "$i/serial" 2>/dev/null` || :
46 if [ "$s" = '' ]; then
47 echo "NO-SERIAL"
48 else
49 echo "$s"
50 fi
51 done | sort -u || :
52 }
53
54 detected_serial_nums() {
55 for i in `ls -1d /sys/bus/astribanks/devices/*/transport 2>/dev/null`; do
56 s=`cat "$i/serial" 2>/dev/null` || :
57 if [ "$s" = '' ]; then
58 echo "NO-SERIAL"
59 else
60 echo "$s"
61 fi
62 done | sort -u || :
63 }
64
65 calc_union() {
66 echo "$@" | tr -s ' ' '\n' | sort -u
67 }
68
69 detected_ab_list() {
70 # Only check /sys info (don't use /proc anymore).
71 find /sys/bus/astribanks/devices/*/ -name waitfor_xpds 2> /dev/null || :
72 }
73
74 waitfor_ab_initialization() {
75 oldab=''
76 while
77 if ! ab=`detected_ab_list`; then
78 exit 1
79 fi
80 test "$oldab" != "$ab"
81 do
82 if [ "$ab" = '' ]; then
83 echo >&2 "Astribanks disappeared"
84 break
85 fi
86 oldab="$ab"
87 cat $ab
88 #echo -n 1>&2 "_"
89 done
90 }
91
92 clean_lines() {
93 sed -e 's/#.*//' -e 'y/\t/ /' -e 's/^ *//' -e 's/ *$//' -e '$s/$/\n/' "$1"
94 }
95
96 # Any hardware?
97 if ! dahdi_hardware="`which dahdi_hardware 2>/dev/null`"; then
98 echo >&2 "$0: Missing dahdi_hardware"
99 exit 0
100 fi
101
102 # Just make sure
103 if [ "`$dahdi_hardware | grep xpp_usb`" != "" ]; then
104 astribank_is_starting -v -a
105 fi
106 if ! astribank_is_starting; then
107 # No Astribanks ever seen -- nothing to do
108 exit 0
109 fi
110
111 # Sanity check
112 for i in `ab_list`; do
113 s=`cat "$i/serial" 2>/dev/null` || :
114 if [ "$s" = '' ]; then
115 echo >&2 "WARNING! Astribank without serial number: $i"
116 fi
117 done
118
119 serial_nums=`ab_serial_nums`
120
121 # Loop until detected (hopefully) all astribanks and they are initialized
122 echo -n 1>&2 "Astribanks detection "
123 tries="$XPP_WAIT_AB_TIMEOUT"
124 last_detected=0
125 while
126 new_serial_nums=`ab_serial_nums`
127 detected_serial_nums=`detected_serial_nums`
128 curr_union=`calc_union $curr_union $serial_nums $new_serial_nums`
129 num_detected=`detected_ab_list | wc -l`
130 if [ "$num_detected" != "$last_detected" ]; then
131 # Visual feedback (number of detected AB so far)
132 echo -n 1>&2 "[$num_detected]"
133 last_detected="$num_detected"
134 waitfor_ab_initialization > /dev/null
135 fi
136 # Break only when we have something and it's stable
137 test "$curr_union" != "$detected_serial_nums" -o "$detected_serial_nums" = ''
138 do
139 if [ "$tries" -le 0 ]; then
140 echo 1>&2 "TIMEOUT"
141 exit 1
142 fi
143 echo -n 1>&2 "."
144 sleep 1
145 : $((tries-=1))
146 serial_nums="$new_serial_nums"
147 done
148
149 # Finished: Show a nice output
150 echo ""
151 cat /sys/bus/astribanks/devices/*/waitfor_xpds 2> /dev/null || :
152
153 # Wait for device to stabilize and XPD's to finish initalizations
154 echo 1>&2 "Astribanks initializing spans"
155 if [ "$XPP_HOTPLUG_DAHDI" = yes -a "$CALLED_FROM_ATRIBANK_HOOK" = '' -a \
156 "$ASTERISK_SUPPORTS_DAHDI_HOTPLUG" != 'yes' ]; then
157 if [ ! -f /etc/dahdi/xpp_order ]; then
158 echo 1>&2 "WARNING: No ASTERISK_SUPPORTS_DAHDI_HOTPLUG" \
159 " and no /etc/dahdi/xpp_order"
160 else
161 count=`clean_lines /etc/dahdi/xpp_order | wc -l`
162 if [ "$count" -le 0 ]; then
163 echo 1>&2 "WARNING: No ASTERISK_SUPPORTS_DAHDI_HOTPLUG" \
164 " and empty /etc/dahdi/xpp_order"
165 else
166 # Now we can wait until the hotplug run would remove the semaphore
167 echo -n 1>&2 "Other DAHDI initializations... "
168 astribank_is_starting -v -w 1>&2
169 fi
170 fi
171 fi
172 # All Astribanks initialized -- remove semaphore
173 astribank_is_starting -v -r 1>&2