forked from asterisk/dahdi-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dahdi_span_types
executable file
·397 lines (372 loc) · 9.83 KB
/
dahdi_span_types
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#! /bin/sh
#
# /usr/sbin/dahdi_span_types
#
# This script can be used both from udev and
# from the command line to manage PRI spans
# type (E1/T1/J1).
#
# Span types can be set only *BEFORE* span are assigned.
#
# It uses a configuration file: $DAHDICONFDIR/span-types.conf
# (default DAHDICONFDIR=/etc/dahdi)
# (the format is documented inside that file)
#
# The first argument is an action:
# "set" - actually write the setting to the driver
# "list" - human-readable list of E1/T1/J1 types
# "dumpconfig" - dump current assignments in a /etc/dahdi/span-types.conf
# compatible format
#
# Without further arguments, it operates on all existing spans
# With one or more sysfs dahdi_devices it is limited to those.
#
# We may use alternative "keys" for device matching:
# * Available keys:
# - "hwid" - Hardware id attribute from sysfs
# - "@location" - Location attribute from sysfs (embeded inside '<>')
# - "/devpath" - The sysfs absolute devpath
#
# * Wildcard are allowed in the configuration file:
# - In the device specifiers (keys)
# - In the span numbers
# - Example for "match-all": * *:T1
#
# * During "set":
# - If there are multiple matches, for a span, all are applied
# - They are always applied in their order in the configuration file
# - This means the last match wins
# - Example:
# * *:T1 # All span on all devices are T1
# usb:X1234567 [34]:E1 # Except spans 3,4 on specific device
#
# * During "dumpconfig", for each device we take the first available key:
# - The preference is: "hwid" or else "@location" or else "/devpath"
# - This can be overriden via the SPAN_ASSIGNMENTS_KEY environment variable
# or the '{-k|--key} key' command line option.
#
# Command line options:
# - The '-h|--help' show a usage message.
# - The '-v|--verbose' show debugging messages (on stderr)
# - The '-n|--dry-run' During "set", only show what would be done
# - The '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment
# variable.
#
# Examples:
# dahdi_span_types list
# dahdi_span_types set # all devices
# dahdi_span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
# dahdi_span_types -k location dumpconfig
#
devbase='/sys/bus/dahdi_devices/devices'
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
DAHDISPANTYPESCONF="${DAHDISPANTYPESCONF:-"${DAHDICONFDIR}/span-types.conf"}"
SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid}
usage() {
echo >&2 "Usage: $0 [options] action [devpath ...]"
echo >&2 " action:"
echo >&2 " set - set spans to E1/T1 according to /etc/dahdi/span-types.conf"
echo >&2 " compare - show config values that differ from system"
echo >&2 " list - human-readable list of all spans"
echo >&2 " dumpconfig - dump current state in /etc/dahdi/span-types.conf format"
echo >&2 ""
echo >&2 " options:"
echo >&2 " -h|--help - Show this help"
echo >&2 " -v|--verbose' - Show debugging messages (on stderr)"
echo >&2 " -n|--dry-run' - During 'set', only show what would be done"
echo >&2 " -k|--key <k> - Override prefered key during dumpconfig action"
echo >&2 " --line-mode <m> - Set default line mode to <m> (E1/T1/J1)"
exit 1
}
# Parse command line options
TEMP=`getopt -o hnvk: --long help,dry-run,verbose,key:,line-mode: -n "$0" -- "$@"`
if [ $? != 0 ]; then
echo >&2 "Bad options"
usage
fi
compare=false
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-h|--help)
usage
;;
-n|--dry-run)
shift
dry_run=true
;;
-v|--verbose)
shift
verbose=true
;;
-k|--key)
SPAN_ASSIGNMENTS_KEY="$2"
shift
shift
;;
--line-mode)
DEFAULT_LINE_MODE="$2"
shift
shift
;;
--)
shift
break
;;
*)
echo "Internal error!"
exit 1
;;
esac
done
if [ "$#" -eq 0 ]; then
echo >&2 "Missing action argument"
usage
fi
action="$1"
shift
# Validate SPAN_ASSIGNMENTS_KEY
case "$SPAN_ASSIGNMENTS_KEY" in
hwid|location|devpath)
;;
*)
echo >&2 "Bad --key='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
usage
;;
esac
# Validate DEFAULT_LINE_MODE
case "$DEFAULT_LINE_MODE" in
E1|T1|J1|'')
;;
*)
echo >&2 "Bad --line-mode='$DEFAULT_LINE_MODE' (should be: E1|T1|J1)"
usage
;;
esac
if [ ! -d "$devbase" ]; then
echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
exit 1
fi
# Use given devices or otherwise, all existing devices
if [ "$#" -gt 0 ]; then
DEVICES="$@"
else
DEVICES=`ls -d $devbase/* 2>/dev/null`
fi
# Beware of special characters in attributes
attr_clean() {
cat "$1" 2>/dev/null | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
}
show_spantypes() {
echo "# PRI span types (E1/T1/J1)"
for device in $DEVICES
do
devpath=`cd "$device" && pwd -P`
location='@'`attr_clean "$device/location"`
hardware_id=`attr_clean "$device/hardware_id"`
cat "$device/spantype" | while read st; do
case "$st" in
*:[ETJ]1)
printf "%-10s %-20s %-30s %s\n" \
"$st" "[$hardware_id]" "$location" \
"$devpath"
;;
esac
done
done
}
list_pri_spantypes() {
find $DEVICES -follow -maxdepth 1 -name spantype | \
xargs cat | \
sed -n '/:[ETJ]1$/s/^.*://p' | \
sort -u | \
tr '\n' ' ' | \
sed -e 's/^ *//' -e 's/ *$//'
}
dump_config() {
pri_spantypes=`list_pri_spantypes`
num_spantypes=`echo "$pri_spantypes" | wc -w`
gen_default=''
echo '#'
echo "# Autogenerated by $0 on `date`"
echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
echo "#"
echo "# Summary:"
if [ "$DEFAULT_LINE_MODE" != '' ]; then
gen_default="$DEFAULT_LINE_MODE"
echo "# * Generating wildcard match of $gen_default."
echo "# - Was run with '--line-mode=$DEFAULT_LINE_MODE'"
elif [ "$num_spantypes" -eq 1 ]; then
gen_default="$pri_spantypes"
echo "# * Generating wildcard match of $gen_default."
echo "# - Spans were $pri_spantypes"
else
echo "# * Not generating wildcard match."
echo "# - Was run without '--line-mode' option and span were of mixed types [$pri_spantypes]"
fi
echo "#"
if [ "$num_spantypes" -eq 1 ]; then
echo "# * Generating a list of commented out configurations for spans."
echo "# - Spans were $pri_spantypes"
echo "# - Uncomment for specific overrides"
else
echo "# * Generating a list of specific span configurations."
echo "# - Spans were of mixed types: $pri_spantypes"
fi
echo "#"
echo ''
fmt="%-65s %s"
printf "$fmt\n" '# @location/hardware_id' 'span_type'
if [ "$gen_default" != '' ]; then
printf "$fmt\t\t# Wildcard line-mode" "*" "*:$gen_default"
echo ""
fi
echo ""
for device in $DEVICES
do
devpath=`cd "$device" && pwd -P`
location=`attr_clean "$device/location"`
hardware_id=`attr_clean "$device/hardware_id"`
if [ "$SPAN_ASSIGNMENTS_KEY" = 'hwid' -a "$hardware_id" != '' ]; then
id="$hardware_id"
elif [ "$SPAN_ASSIGNMENTS_KEY" = 'location' -a "$location" != '' ]; then
id="@$location"
else
id="$devpath"
fi
echo "# Device: [$hardware_id] @$location $devpath"
cat "$device/spantype" | while read st; do
case "$st" in
*:[ETJ]1)
if [ "$num_spantypes" -eq 1 ]; then
printf "#$fmt\n" "$id" "$st"
else
printf "$fmt\n" "$id" "$st"
fi
;;
*)
#echo "# Skipped local span `echo $st | sed 's/:/ -- /'`"
;;
esac
done | sort -n
echo ''
done
}
# Allow comments and empty lines in config file
filter_conf() {
sed -e 's/#.*//' -e '/^[ \t]*$/d' "$DAHDISPANTYPESCONF"
}
handle_span() {
device="$1"
spantype="$2"
attr_file="$device/spantype"
devpath=`cd "$device" && pwd -P`
devname=`echo "$device" | sed "s,$devbase/,,"`
location='@'`attr_clean "$device/location"`
hardware_id=`attr_clean "$device/hardware_id"`
spanno=`echo "$spantype" | cut -d: -f1`
#echo >&2 "DEBUG: $device $spanno ($spantype)"
filter_conf | while read id span_spec; do
sn=`echo "$span_spec" | cut -d: -f1`
val=`echo "$span_spec" | cut -d: -f2`
case "$spanno" in
$sn)
;;
*)
#echo >&2 "no-match($device $spanno): $sn"
continue
;;
esac
found=no
# GLOBBING
case "$location" in
$id)
#echo >&2 "match($id): $span_spec"
found=yes
;;
esac
case "$hardware_id" in
$id)
#echo >&2 "match([$id]): $span_spec"
found=yes
;;
esac
case "$devpath" in
$id)
#echo >&2 "match([$id]): $span_spec"
found=yes
;;
esac
if [ "$found" = 'yes' ]; then
if [ "$dry_run" = 'true' -o "$verbose" = 'true' ]; then
echo >&2 "Set $devname span $spanno = $val"
fi
if [ "$dry_run" != 'true' ]; then
if [ "$compare" = 'true' ]; then
config="$spanno:$val"
system=`grep "$spanno:" "$attr_file"`
if [ "$config" != "$system" ]; then
active_val=`echo $system | cut -d: -f2`
echo "$devname $spanno $val $active_val" >>"$compare_results_file"
fi
else
echo "$spanno:$val" > "$attr_file"
fi
fi
fi
done
}
set_all_devices() {
span_differs='false'
SPANS_DIFFER='false'
if [ ! -f "$DAHDISPANTYPESCONF" ]; then
echo >&2 "$0: Missing configuration '$DAHDISPANTYPESCONF'"
exit 1
fi
for device in $DEVICES
do
devname=`echo "$device" | sed "s,$devbase/,,"`
cat "$device/spantype" | while read spantype; do
case "$spantype" in
*:[ETJ]1)
handle_span "$device" "$spantype"
;;
*)
if [ "$dry_run" = 'true' -o "$verbose" = 'true' ]; then
echo >&2 "Skipping non-E1/T1/J1 span ($devname -- $spantype)"
fi
;;
esac
done
done
if [ "$compare" = 'true' ]; then
if [ -s "$compare_results_file" ]; then
echo "# Device Unit Config Active"
cat "$compare_results_file"
rm -f "$compare_results_file"
exit 5
fi
rm -f "$compare_results_file"
exit 0
fi
}
case "$action" in
list)
show_spantypes
;;
dumpconfig)
dump_config
;;
set)
set_all_devices
;;
compare)
compare=true
compare_results_file=`mktemp`
set_all_devices
;;
*)
usage
;;
esac