fxstest: Use DAHDI_SPECIFY when opening by integer channel number.
[dahdi/tools.git] / span_types
1 #! /bin/sh
2 #
3 # /usr/share/dahdi/span_types:
4 #
5 # this script can be used both from udev and
6 # from the command line for spantype management.
7 #
8 # It use a configuration file /etc/dahdi/spantype.conf
9 # (the format is documented inside this file)
10 #
11 # The first argument is an action:
12 #   "list" to show existing E1/T1/J1 types
13 #   "dump" the same, but in a format (almost) suitable for
14 #          the configuration file
15 #          FIXME: we currently don't have the base channo in sysfs.
16 #   "set"  actually write the setting to the driver
17 #
18 # Examples:
19 #    span_types list
20 #    span_types dump
21 #    span_types set     # all
22 #    span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
23 #
24
25
26 devbase='/sys/bus/dahdi_devices/devices'
27 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
28 spantype_conf="$DAHDICONFDIR/spantype.conf"
29
30 usage() {
31         echo >&2 "Usage: $0 {list|dump|set} [devpath ...]"
32         exit 1
33 }
34
35 if [ "$#" -eq 0 ]; then
36         usage
37 fi
38 action="$1"
39 shift
40
41 # Use given devices or otherwise, all existing devices
42 if [ "$#" -gt 0 ]; then
43         DEVICES="$@"
44 else
45         DEVICES=`echo $devbase/*`
46 fi
47
48 show_spantypes() {
49         for device in $DEVICES
50         do
51                 hw_id=`cat "$device/hardware_id"`
52                 location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
53                 cat "$device/spantype" | while read st; do
54                         printf "%-10s %-20s %s\n" "$st" "[$hw_id]" "$location"
55                 done | sort -n
56         done
57 }
58
59 dump_config() {
60         fmt="%-65s %s\n"
61         echo "# Map of dahdi_devices to span types for E1/T1/J1"
62         printf "$fmt" '# @location/hardware_id' 'span_type'
63         for device in $DEVICES
64         do
65                 hw_id=`cat "$device/hardware_id"`
66                 location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
67                 if [ -n "$hw_id" ]; then
68                         id="$hw_id"
69                 else
70                         id="$location"
71                 fi
72                 cat "$device/spantype" | while read st; do
73                         case "$st" in
74                         *:[ETJ]1)
75                                 printf "$fmt" "$id" "$st"
76                                 ;;
77                         esac
78                 done | sort -n
79         done
80 }
81
82 # Allow comments and empty lines in config file
83 filter_conf() {
84         sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantype_conf"
85 }
86
87 conf_spans() {
88         hw_id="$1"
89         location="$2"
90         filter_conf | (
91                 # Collect device spans
92                 # in a subshell, so $SPANS is not lost
93                 SPANS=''
94                 while read id spans; do
95                         # GLOBBING
96                         case "$location" in
97                         $id)
98                                 #echo >&2 "match($id): $spans"
99                                 SPANS="$SPANS $spans"
100                                 ;;
101                         esac
102                         case "$hw_id" in
103                         $id)
104                                 #echo >&2 "match([$id]): $spans"
105                                 SPANS="$SPANS $spans"
106                                 ;;
107                         esac
108                 done
109                 echo "$SPANS"
110         )
111 }
112
113 # Beware of special characters in attributes
114 attr_clean() {
115         cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
116 }
117
118 device_set_spantype() {
119         device="$1"
120         attr_file="$device/spantype"
121         hw_id=`attr_clean "$device/hardware_id"`
122         location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
123         spanspecs=`conf_spans "$hw_id" "$location"`
124         echo >&2 "MATCHED($device): $spanspecs"
125         cut -d: -f1 "$attr_file" | while read spanno; do
126                         for sp in $spanspecs
127                         do
128                                 s=`echo "$sp" | cut -d: -f1`
129                                 v=`echo "$sp" | cut -d: -f2`
130                                 case "$spanno" in
131                                 $s)
132                                         #echo >&2 "conf($attr_file): $spanno:$v"
133                                         echo "$spanno:$v" > "$attr_file"
134                                         ;;
135                                 esac
136                         done
137                 done
138 }
139
140 set_spantypes() {
141         for device in $DEVICES
142         do
143                 device_set_spantype "$device"
144         done
145 }
146
147 case "$action" in
148 list)
149         show_spantypes
150         ;;
151 dump)
152         dump_config
153         ;;
154 set)
155         set_spantypes
156         ;;
157 *)
158         usage
159         ;;
160 esac