dahdi_span_types: Show location of configuration file in help message.
[dahdi/tools.git] / dahdi_span_types
1 #! /bin/sh
2 #
3 # /usr/sbin/dahdi_span_types
4 #
5 # This script can be used both from udev and
6 # from the command line to manage PRI spans
7 # type (E1/T1/J1).
8 #
9 # Span types can be set only *BEFORE* span are assigned.
10 #
11 # It uses a configuration file: $DAHDICONFDIR/span-types.conf
12 # (default DAHDICONFDIR=/etc/dahdi)
13 # (the format is documented inside that file)
14 #
15 # The first argument is an action:
16 #   "set"        - actually write the setting to the driver
17 #   "list"       - human-readable list of E1/T1/J1 types
18 #   "dumpconfig" - dump current assignments in a /etc/dahdi/span-types.conf
19 #                  compatible format
20 #
21 # Without further arguments, it operates on all existing spans
22 # With one or more sysfs dahdi_devices it is limited to those.
23 #
24 # We may use alternative "keys" for device matching:
25 # * Available keys:
26 #   - "hwid"       - Hardware id attribute from sysfs
27 #   - "@location"  - Location attribute from sysfs (embeded inside '<>')
28 #   - "/devpath"   - The sysfs absolute devpath
29 #
30 # * During "dumpconfig", for each device we take the first available key:
31 #   - The preference is: "hwid" or else "@location" or else "/devpath"
32 #   - This can be overriden via the SPAN_ASSIGNMENTS_KEY environment variable
33 #     or the '{-k|--key} key' command line option.
34 #
35 # Command line options:
36 #  - The '-h|--help' show a usage message.
37 #  - The '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment
38 #    variable.
39 #
40 # Examples:
41 #    dahdi_span_types list
42 #    dahdi_span_types set            # all devices
43 #    dahdi_span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
44 #    dahdi_span_types -k location dumpconfig
45 #
46
47
48 devbase='/sys/bus/dahdi_devices/devices'
49 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
50 DAHDISPANTYPESCONF="${DAHDISPANTYPESCONF:-"${DAHDICONFDIR}/span-types.conf"}"
51 SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid}
52
53 usage() {
54         echo >&2 "Usage: $0 [options] action [devpath ...]"
55         echo >&2 "       action:"
56         echo >&2 "         set        - set spans to E1/T1 according to /etc/dahdi/span-types.conf"
57         echo >&2 "         list       - human-readable list of all spans"
58         echo >&2 "         dumpconfig - dump current state in /etc/dahdi/span-types.conf format"
59         echo >&2 ""
60         echo >&2 "       options:"
61         echo >&2 "         -h|--help      - Show this help"
62         echo >&2 "         -k|--key <k>   - Override prefered key during dumpconfig action"
63         exit 1
64 }
65
66 # Parse command line options
67 TEMP=`getopt -o hk: --long help,key: -n "$0" -- "$@"`
68 if [ $? != 0 ]; then
69         echo >&2 "Bad options"
70         usage
71 fi
72
73 # Note the quotes around `$TEMP': they are essential!
74 eval set -- "$TEMP"
75
76 while true ; do
77         case "$1" in
78         -h|--help)
79                 usage
80                 ;;
81         -k|--key)
82                 SPAN_ASSIGNMENTS_KEY="$2"
83                 shift
84                 shift
85                 ;;
86         --)
87                 shift
88                 break
89                 ;;
90         *)
91                 echo "Internal error!"
92                 exit 1
93                 ;;
94         esac
95 done
96
97 if [ "$#" -eq 0 ]; then
98         echo >&2 "Missing action argument"
99         usage
100 fi
101 action="$1"
102 shift
103
104 # Validate SPAN_ASSIGNMENTS_KEY
105 case "$SPAN_ASSIGNMENTS_KEY" in
106 hwid|location|devpath)
107         ;;
108 *)
109         echo >&2 "Bad SPAN_ASSIGNMENTS_KEY='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
110         usage
111         ;;
112 esac
113
114 if [ ! -d "$devbase" ]; then
115         echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
116         exit 1
117 fi
118
119 # Use given devices or otherwise, all existing devices
120 if [ "$#" -gt 0 ]; then
121         DEVICES="$@"
122 else
123         DEVICES=`ls -d $devbase/* 2>/dev/null`
124 fi
125
126 # Beware of special characters in attributes
127 attr_clean() {
128         cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
129 }
130
131 show_spantypes() {
132         echo "# PRI span types (E1/T1/J1)"
133         for device in $DEVICES
134         do
135                 devpath=`cd "$device" && pwd -P`
136                 location='@'`attr_clean "$device/location"`
137                 hardware_id=`attr_clean "$device/hardware_id"`
138                 cat "$device/spantype" | while read st; do
139                         case "$st" in
140                         *:[ETJ]1)
141                                 printf "%-10s %-20s %s\n" \
142                                         "$st" "[$hardware_id]" "$location"
143                                 ;;
144                         esac
145                 done | sort -n
146         done
147 }
148
149 dump_config() {
150         echo '#'
151         echo "# Autogenerated by $0 on `date`"
152         echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
153         echo ''
154         fmt="%-65s %s\n"
155         printf "$fmt" '# @location/hardware_id' 'span_type'
156         for device in $DEVICES
157         do
158                 devpath=`cd "$device" && pwd -P`
159                 location=`attr_clean "$device/location"`
160                 hardware_id=`attr_clean "$device/hardware_id"`
161                 if [ "$SPAN_ASSIGNMENTS_KEY" = 'hwid' -a "$hardware_id" != '' ]; then
162                         id="$hardware_id"
163                 elif [ "$SPAN_ASSIGNMENTS_KEY" = 'location' -a "$location" != '' ]; then
164                         id="@$location"
165                 else
166                         id="$devpath"
167                 fi
168                 echo "# Device: [$hardware_id] @$location $devpath"
169                 cat "$device/spantype" | while read st; do
170                         case "$st" in
171                         *:[ETJ]1)
172                                 printf "$fmt" "$id" "$st"
173                                 ;;
174                         *)
175                                 #echo "#    Skipped local span `echo $st | sed 's/:/ -- /'`"
176                                 ;;
177                         esac
178                 done | sort -n
179                 #echo ''
180         done
181 }
182
183 # Allow comments and empty lines in config file
184 filter_conf() {
185         sed -e 's/#.*//' -e '/^[ \t]*$/d' "$DAHDISPANTYPESCONF"
186 }
187
188 conf_spans() {
189         hardware_id="$1"
190         location="$2"
191         devpath="$3"
192         filter_conf | (
193                 # Collect device spans
194                 # in a subshell, so $SPANS is not lost
195                 SPANS=''
196                 while read id spans; do
197                         # GLOBBING
198                         case "$location" in
199                         $id)
200                                 #echo >&2 "match($id): $spans"
201                                 SPANS="$SPANS $spans"
202                                 ;;
203                         esac
204                         case "$hardware_id" in
205                         $id)
206                                 #echo >&2 "match([$id]): $spans"
207                                 SPANS="$SPANS $spans"
208                                 ;;
209                         esac
210                         case "$devpath" in
211                         $id)
212                                 #echo >&2 "match([$id]): $spans"
213                                 SPANS="$SPANS $spans"
214                                 ;;
215                         esac
216                 done
217                 echo "$SPANS"
218         )
219 }
220
221 device_set_spantype() {
222         device="$1"
223         attr_file="$device/spantype"
224         devpath=`cd "$device" && pwd -P`
225         location='@'`attr_clean "$device/location"`
226         hardware_id=`attr_clean "$device/hardware_id"`
227         spanspecs=`conf_spans "$hardware_id" "$location" "$devpath"`
228         #echo >&2 "MATCHED($device): $spanspecs"
229         cut -d: -f1 "$attr_file" | while read spanno; do
230                         for sp in $spanspecs
231                         do
232                                 s=`echo "$sp" | cut -d: -f1`
233                                 v=`echo "$sp" | cut -d: -f2`
234                                 case "$spanno" in
235                                 $s)
236                                         #echo >&2 "conf($attr_file): $spanno:$v"
237                                         echo "$spanno:$v" > "$attr_file"
238                                         ;;
239                                 esac
240                         done
241                 done
242 }
243
244 set_spantypes() {
245         if [ ! -f "$DAHDISPANTYPESCONF" ]; then
246                 echo >&2 "$0: Missing configuration '$DAHDISPANTYPESCONF'"
247                 exit 1
248         fi
249         for device in $DEVICES
250         do
251                 device_set_spantype "$device"
252         done
253 }
254
255 case "$action" in
256 list)
257         show_spantypes
258         ;;
259 dumpconfig)
260         dump_config
261         ;;
262 set)
263         set_spantypes
264         ;;
265 *)
266         usage
267         ;;
268 esac