pinned-spans: remove '--enable-pinned' from configure
[dahdi/tools.git] / span_types
1 #! /bin/sh
2 #
3 # /usr/sbin/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 reads a configuration file /etc/dahdi/span-types.conf
12 # (the format is documented inside that file)
13 #
14 # A mandatory first argument is:
15 #   list       - to show existing E1/T1/J1 types
16 #   dumpconfig - the same, but in a format (almost) suitable
17 #                for the configuration file
18 #   set        - actually write the setting to the driver
19 #
20 # Examples:
21 #    span_types list
22 #    span_types dumpconfig
23 #    span_types set          # all devices
24 #    span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
25 #
26
27
28 devbase='/sys/bus/dahdi_devices/devices'
29 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
30 spantypes_conf="$DAHDICONFDIR/span-types.conf"
31
32 usage() {
33         echo >&2 "Usage: $0 {list|dumpconfig|set} [devpath ...]"
34         exit 1
35 }
36
37 if [ "$#" -eq 0 ]; then
38         usage
39 fi
40 action="$1"
41 shift
42
43 if [ ! -d "$devbase" ]; then
44         echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
45         exit 1
46 fi
47
48 # Use given devices or otherwise, all existing devices
49 if [ "$#" -gt 0 ]; then
50         DEVICES="$@"
51 else
52         DEVICES=`echo $devbase/*`
53 fi
54
55 show_spantypes() {
56         echo "# PRI span types (E1/T1/J1)"
57         for device in $DEVICES
58         do
59                 hw_id=`cat "$device/hardware_id"`
60                 location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
61                 cat "$device/spantype" | while read st; do
62                         case "$st" in
63                         *:[ETJ]1)
64                                 printf "%-10s %-20s %s\n" \
65                                         "$st" "[$hw_id]" "$location"
66                                 ;;
67                         esac
68                 done | sort -n
69         done
70 }
71
72 dump_config() {
73         echo '#'
74         echo "# Autogenerated by $0 on `date`"
75         echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
76         echo ''
77         fmt="%-65s %s\n"
78         printf "$fmt" '# @location/hardware_id' 'span_type'
79         for device in $DEVICES
80         do
81                 hw_id=`cat "$device/hardware_id"`
82                 location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
83                 if [ -n "$hw_id" ]; then
84                         id="$hw_id"
85                 else
86                         id="$location"
87                 fi
88                 #echo "# Device: [$hw_id] $location"
89                 cat "$device/spantype" | while read st; do
90                         case "$st" in
91                         *:[ETJ]1)
92                                 printf "$fmt" "$id" "$st"
93                                 ;;
94                         *)
95                                 #echo "#    Skipped local span `echo $st | sed 's/:/ -- /'`"
96                                 ;;
97                         esac
98                 done | sort -n
99                 #echo ''
100         done
101 }
102
103 # Allow comments and empty lines in config file
104 filter_conf() {
105         sed -e 's/#.*//' -e '/^[ \t]*$/d' "$spantypes_conf"
106 }
107
108 conf_spans() {
109         hw_id="$1"
110         location="$2"
111         filter_conf | (
112                 # Collect device spans
113                 # in a subshell, so $SPANS is not lost
114                 SPANS=''
115                 while read id spans; do
116                         # GLOBBING
117                         case "$location" in
118                         $id)
119                                 #echo >&2 "match($id): $spans"
120                                 SPANS="$SPANS $spans"
121                                 ;;
122                         esac
123                         case "$hw_id" in
124                         $id)
125                                 #echo >&2 "match([$id]): $spans"
126                                 SPANS="$SPANS $spans"
127                                 ;;
128                         esac
129                 done
130                 echo "$SPANS"
131         )
132 }
133
134 # Beware of special characters in attributes
135 attr_clean() {
136         cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
137 }
138
139 device_set_spantype() {
140         device="$1"
141         attr_file="$device/spantype"
142         hw_id=`attr_clean "$device/hardware_id"`
143         location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
144         spanspecs=`conf_spans "$hw_id" "$location"`
145         #echo >&2 "MATCHED($device): $spanspecs"
146         cut -d: -f1 "$attr_file" | while read spanno; do
147                         for sp in $spanspecs
148                         do
149                                 s=`echo "$sp" | cut -d: -f1`
150                                 v=`echo "$sp" | cut -d: -f2`
151                                 case "$spanno" in
152                                 $s)
153                                         #echo >&2 "conf($attr_file): $spanno:$v"
154                                         echo "$spanno:$v" > "$attr_file"
155                                         ;;
156                                 esac
157                         done
158                 done
159 }
160
161 set_spantypes() {
162         if [ ! -f "$spantypes_conf" ]; then
163                 echo >&2 "$0: Missing configuration '$spantypes_conf'"
164                 exit 1
165         fi
166         for device in $DEVICES
167         do
168                 device_set_spantype "$device"
169         done
170 }
171
172 case "$action" in
173 list)
174         show_spantypes
175         ;;
176 dumpconfig)
177         dump_config
178         ;;
179 set)
180         set_spantypes
181         ;;
182 *)
183         usage
184         ;;
185 esac