fxstest: Use DAHDI_SPECIFY when opening by integer channel number.
[dahdi/tools.git] / span_assignments
1 #! /bin/sh
2 #
3 # /usr/share/dahdi/span_assignments:
4 #
5 # this script can be used both from udev and
6 # from the command line to assign/unassign and list
7 # current assignments.
8 #
9 # The first argument is an action:
10 #   "add" to assign (spans which are not already assigned)
11 #   "remove" to unassign (spans which are not already unassigned)
12 #   "list" to show all spans (with/without assignments)
13 #
14 # Without further arguments, it operates on all existing spans
15 # With one or more sysfs dahdi_devices it is limited to those.
16 #
17 # Examples:
18 #    span_assignments list
19 #    span_assignments add       # all
20 #    span_assignments add /sys/bus/dahdi_devices/devices/astribanks:xbus-00
21 #    span_assignments remove    # all
22 #
23
24 devbase='/sys/bus/dahdi_devices/devices'
25 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
26 pinned_spans_conf="$DAHDICONFDIR/pinned-spans.conf"
27
28 usage() {
29         echo >&2 "Usage: $0 {add|remove|list} [devpath ...]"
30         exit 1
31 }
32
33 if [ "$#" -eq 0 ]; then
34         usage
35 fi
36 action="$1"
37 shift
38
39 if [ ! -d "$devbase" ]; then
40         echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
41         exit 1
42 fi
43
44 # Use given devices or otherwise, all existing devices
45 if [ "$#" -gt 0 ]; then
46         DEVICES="$@"
47 else
48         DEVICES=`echo $devbase/*`
49 fi
50
51 show_devices() {
52
53         for device in $DEVICES
54         do
55                 hw_id=`cat "$device/hardware_id"`
56                 location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
57                 for local_spanno in `cut -d: -f1 "$device/spantype"`
58                 do
59                         span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
60                                 sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'`
61                         if [ "$span" != '' ]; then
62                                 spanno=`echo $span | sed 's/^.*-//'`
63                                 name=`cat 2>/dev/null "$device/$span/name"`
64                                 basechan=`cat 2>/dev/null "$device/$span/basechan"`
65                         else
66                                 spanno='-'
67                                 basechan='-'
68                         fi
69                         printf "%-4s %-12s %s\n" "$local_spanno:$spanno:$basechan" "[$hw_id]" "@$location"
70                 done | sort -n
71         done
72 }
73
74 dump_config() {
75
76         for device in $DEVICES
77         do
78                 hw_id=`cat "$device/hardware_id"`
79                 location=`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
80                 if [ "$hw_id" != '' ]; then
81                         id="$hw_id"
82                 else
83                         id="@$location"
84                 fi
85                 for local_spanno in `cut -d: -f1 "$device/spantype"`
86                 do
87                         span=`grep 2>/dev/null -Hw "$local_spanno" "$device/span-"*"/local_spanno" | \
88                                 sed -e 's,/local_spanno:.*,,' -e 's,.*/,,'`
89                         if [ "$span" != '' ]; then
90                                 spanno=`echo $span | sed 's/^.*-//'`
91                                 name=`cat 2>/dev/null "$device/$span/name"`
92                                 basechan=`cat 2>/dev/null "$device/$span/basechan"`
93                         else
94                                 spanno='-'
95                         fi
96                         printf "%-30s %s\n" "$id" "$local_spanno:$spanno:$basechan"
97                 done | sort -n
98         done
99 }
100
101 unassign_all_spans() {
102         for device in $DEVICES
103         do
104                 find "$device" -follow -maxdepth 1 -name 'span-*' -type d | \
105                         sort | while read spandir; do
106                         local_spanno=`cat "$spandir/local_spanno"`
107                         echo "unassign $device $local_spanno"
108                         if ! echo "$local_spanno" > "$device/unassign_span"; then
109                                 echo >&2 "$0: failed unassigning '$local_spanno' in '$device'"
110                         fi
111                 done
112         done
113 }
114
115 # Allow comments and empty lines in config file
116 filter_conf() {
117         sed -e 's/#.*//' -e '/^[ \t]*$/d' "$pinned_spans_conf"
118 }
119
120 # Beware of special characters in attributes
121 attr_clean() {
122         cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
123 }
124
125 assign_device_spans() {
126         device="$1"
127         for s in $spanspecs
128         do
129                 local_spanno=`echo "$s" | cut -d: -f1`
130                 spanno=`echo "$s" | cut -d: -f2`
131                 span="$device/span-$spanno"
132                 if [ -d "$span" ]; then
133                         span_local_spanno=`cat "$span/local_spanno"`
134                         if [ "$span_local_spanno" != "$local_spanno" ]; then
135                                 echo "WARNING: $span_local_spanno != $local_spanno"
136                         fi
137                         echo "$device [$local_spanno] already assigned to $spanno. Skipping..."
138                         continue
139                 fi
140                 echo "assign $device: $s"
141                 if ! echo "$s" > "$device/assign_span"; then
142                         echo >&2 "$0: failed assigning '$s' to '$device'"
143                 fi
144         done
145 }
146
147 match_device() {
148         device="$1"
149         location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
150         hardware_id=`attr_clean "$device/hardware_id"`
151         filter_conf | while read id spanspecs
152         do
153                 # We use case to enable shell-style globbing in configuration
154                 case "$location" in
155                 $id)
156                         #echo "match location($id ~ $location): $spanspecs"
157                         assign_device_spans "$device"
158                         ;;
159                 esac
160                 # We use case to enable shell-style globbing in configuration
161                 case "$hardware_id" in
162                 $id)
163                         #echo "match hardware_id([$id] ~ $hardware_id): $spanspecs"
164                         assign_device_spans "$device"
165                         ;;
166                 esac
167         done
168 }
169
170 assign_devices() {
171         for device in $DEVICES
172         do
173                 match_device "$device"
174         done
175 }
176
177 case "$action" in
178 list)
179         show_devices
180         ;;
181 dump)
182         dump_config
183         ;;
184 add)
185         assign_devices
186         ;;
187 remove)
188         unassign_all_spans
189         ;;
190 *)
191         usage
192         ;;
193 esac