2011-10-07 03:03:00 +08:00
|
|
|
#! /bin/sh
|
|
|
|
#
|
2013-11-26 23:15:13 +08:00
|
|
|
# /usr/sbin/dahdi_span_types
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
2013-09-23 19:59:14 +08:00
|
|
|
# This script can be used both from udev and
|
|
|
|
# from the command line to manage PRI spans
|
|
|
|
# type (E1/T1/J1).
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
2013-09-23 19:59:14 +08:00
|
|
|
# Span types can be set only *BEFORE* span are assigned.
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
2013-11-04 23:25:25 +08:00
|
|
|
# It uses a configuration file: $DAHDICONFDIR/span-types.conf
|
|
|
|
# (default DAHDICONFDIR=/etc/dahdi)
|
2013-09-23 19:59:14 +08:00
|
|
|
# (the format is documented inside that file)
|
|
|
|
#
|
2013-11-04 23:25:25 +08:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# * 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 '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment
|
|
|
|
# variable.
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
|
|
|
# Examples:
|
2013-11-26 23:15:13 +08:00
|
|
|
# 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
|
2011-10-07 03:03:00 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
devbase='/sys/bus/dahdi_devices/devices'
|
|
|
|
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
|
2013-11-27 02:55:09 +08:00
|
|
|
DAHDISPANTYPESCONF="${DAHDISPANTYPESCONF:-"${DAHDICONFDIR}/span-types.conf"}"
|
2013-11-04 23:25:25 +08:00
|
|
|
SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid}
|
2011-10-07 03:03:00 +08:00
|
|
|
|
|
|
|
usage() {
|
2013-11-04 23:25:25 +08:00
|
|
|
echo >&2 "Usage: $0 [options] action [devpath ...]"
|
|
|
|
echo >&2 " action:"
|
2014-01-09 08:14:04 +08:00
|
|
|
echo >&2 " set - set spans to E1/T1 according to /etc/dahdi/span-types.conf"
|
2013-11-04 23:25:25 +08:00
|
|
|
echo >&2 " list - human-readable list of all spans"
|
2014-01-09 08:14:04 +08:00
|
|
|
echo >&2 " dumpconfig - dump current state in /etc/dahdi/span-types.conf format"
|
2013-11-04 23:25:25 +08:00
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 " options:"
|
|
|
|
echo >&2 " -h|--help - Show this help"
|
|
|
|
echo >&2 " -k|--key <k> - Override prefered key during dumpconfig action"
|
2011-10-07 03:03:00 +08:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2013-11-04 23:25:25 +08:00
|
|
|
# Parse command line options
|
|
|
|
TEMP=`getopt -o hk: --long help,key: -n "$0" -- "$@"`
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
echo >&2 "Bad options"
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Note the quotes around `$TEMP': they are essential!
|
|
|
|
eval set -- "$TEMP"
|
|
|
|
|
|
|
|
while true ; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
-k|--key)
|
|
|
|
SPAN_ASSIGNMENTS_KEY="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Internal error!"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
if [ "$#" -eq 0 ]; then
|
2013-11-04 23:25:25 +08:00
|
|
|
echo >&2 "Missing action argument"
|
2011-10-07 03:03:00 +08:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
action="$1"
|
|
|
|
shift
|
|
|
|
|
2013-11-04 23:25:25 +08:00
|
|
|
# Validate SPAN_ASSIGNMENTS_KEY
|
|
|
|
case "$SPAN_ASSIGNMENTS_KEY" in
|
|
|
|
hwid|location|devpath)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo >&2 "Bad SPAN_ASSIGNMENTS_KEY='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2013-09-23 19:59:14 +08:00
|
|
|
if [ ! -d "$devbase" ]; then
|
|
|
|
echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
# Use given devices or otherwise, all existing devices
|
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
DEVICES="$@"
|
|
|
|
else
|
2014-01-02 17:14:38 +08:00
|
|
|
DEVICES=`ls -d $devbase/* 2>/dev/null`
|
2011-10-07 03:03:00 +08:00
|
|
|
fi
|
|
|
|
|
2013-11-04 23:25:25 +08:00
|
|
|
# Beware of special characters in attributes
|
|
|
|
attr_clean() {
|
|
|
|
cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
|
|
|
|
}
|
|
|
|
|
2011-10-07 03:03:00 +08:00
|
|
|
show_spantypes() {
|
2013-09-23 19:59:14 +08:00
|
|
|
echo "# PRI span types (E1/T1/J1)"
|
2011-10-07 03:03:00 +08:00
|
|
|
for device in $DEVICES
|
|
|
|
do
|
2013-11-04 23:25:25 +08:00
|
|
|
devpath=`cd "$device" && pwd -P`
|
|
|
|
location='@'`attr_clean "$device/location"`
|
|
|
|
hardware_id=`attr_clean "$device/hardware_id"`
|
2011-10-07 03:03:00 +08:00
|
|
|
cat "$device/spantype" | while read st; do
|
2013-09-23 19:59:14 +08:00
|
|
|
case "$st" in
|
|
|
|
*:[ETJ]1)
|
|
|
|
printf "%-10s %-20s %s\n" \
|
2013-11-04 23:25:25 +08:00
|
|
|
"$st" "[$hardware_id]" "$location"
|
2013-09-23 19:59:14 +08:00
|
|
|
;;
|
|
|
|
esac
|
2011-10-07 03:03:00 +08:00
|
|
|
done | sort -n
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_config() {
|
2013-09-23 19:59:14 +08:00
|
|
|
echo '#'
|
|
|
|
echo "# Autogenerated by $0 on `date`"
|
|
|
|
echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
|
|
|
|
echo ''
|
2011-10-07 03:03:00 +08:00
|
|
|
fmt="%-65s %s\n"
|
|
|
|
printf "$fmt" '# @location/hardware_id' 'span_type'
|
|
|
|
for device in $DEVICES
|
|
|
|
do
|
2013-11-04 23:25:25 +08:00
|
|
|
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"
|
2011-10-07 03:03:00 +08:00
|
|
|
else
|
2013-11-04 23:25:25 +08:00
|
|
|
id="$devpath"
|
2011-10-07 03:03:00 +08:00
|
|
|
fi
|
2013-11-04 23:25:25 +08:00
|
|
|
echo "# Device: [$hardware_id] @$location $devpath"
|
2011-10-07 03:03:00 +08:00
|
|
|
cat "$device/spantype" | while read st; do
|
|
|
|
case "$st" in
|
|
|
|
*:[ETJ]1)
|
|
|
|
printf "$fmt" "$id" "$st"
|
|
|
|
;;
|
2013-09-23 19:59:14 +08:00
|
|
|
*)
|
|
|
|
#echo "# Skipped local span `echo $st | sed 's/:/ -- /'`"
|
|
|
|
;;
|
2011-10-07 03:03:00 +08:00
|
|
|
esac
|
|
|
|
done | sort -n
|
2013-09-23 19:59:14 +08:00
|
|
|
#echo ''
|
2011-10-07 03:03:00 +08:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Allow comments and empty lines in config file
|
|
|
|
filter_conf() {
|
2013-11-27 02:55:09 +08:00
|
|
|
sed -e 's/#.*//' -e '/^[ \t]*$/d' "$DAHDISPANTYPESCONF"
|
2011-10-07 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
conf_spans() {
|
2013-11-04 23:25:25 +08:00
|
|
|
hardware_id="$1"
|
2011-10-07 03:03:00 +08:00
|
|
|
location="$2"
|
2013-11-26 10:21:14 +08:00
|
|
|
devpath="$3"
|
2011-10-07 03:03:00 +08:00
|
|
|
filter_conf | (
|
|
|
|
# Collect device spans
|
|
|
|
# in a subshell, so $SPANS is not lost
|
|
|
|
SPANS=''
|
|
|
|
while read id spans; do
|
|
|
|
# GLOBBING
|
|
|
|
case "$location" in
|
|
|
|
$id)
|
|
|
|
#echo >&2 "match($id): $spans"
|
|
|
|
SPANS="$SPANS $spans"
|
|
|
|
;;
|
|
|
|
esac
|
2013-11-04 23:25:25 +08:00
|
|
|
case "$hardware_id" in
|
2011-10-07 03:03:00 +08:00
|
|
|
$id)
|
|
|
|
#echo >&2 "match([$id]): $spans"
|
|
|
|
SPANS="$SPANS $spans"
|
|
|
|
;;
|
|
|
|
esac
|
2013-11-26 10:21:14 +08:00
|
|
|
case "$devpath" in
|
|
|
|
$id)
|
|
|
|
#echo >&2 "match([$id]): $spans"
|
|
|
|
SPANS="$SPANS $spans"
|
|
|
|
;;
|
|
|
|
esac
|
2011-10-07 03:03:00 +08:00
|
|
|
done
|
|
|
|
echo "$SPANS"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
device_set_spantype() {
|
|
|
|
device="$1"
|
|
|
|
attr_file="$device/spantype"
|
2013-11-04 23:25:25 +08:00
|
|
|
devpath=`cd "$device" && pwd -P`
|
|
|
|
location='@'`attr_clean "$device/location"`
|
|
|
|
hardware_id=`attr_clean "$device/hardware_id"`
|
2013-11-26 10:21:14 +08:00
|
|
|
spanspecs=`conf_spans "$hardware_id" "$location" "$devpath"`
|
2013-09-23 19:59:14 +08:00
|
|
|
#echo >&2 "MATCHED($device): $spanspecs"
|
2011-10-07 03:03:00 +08:00
|
|
|
cut -d: -f1 "$attr_file" | while read spanno; do
|
|
|
|
for sp in $spanspecs
|
|
|
|
do
|
|
|
|
s=`echo "$sp" | cut -d: -f1`
|
|
|
|
v=`echo "$sp" | cut -d: -f2`
|
|
|
|
case "$spanno" in
|
|
|
|
$s)
|
|
|
|
#echo >&2 "conf($attr_file): $spanno:$v"
|
|
|
|
echo "$spanno:$v" > "$attr_file"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
set_spantypes() {
|
2013-11-27 02:55:09 +08:00
|
|
|
if [ ! -f "$DAHDISPANTYPESCONF" ]; then
|
|
|
|
echo >&2 "$0: Missing configuration '$DAHDISPANTYPESCONF'"
|
2013-09-23 19:59:14 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2011-10-07 03:03:00 +08:00
|
|
|
for device in $DEVICES
|
|
|
|
do
|
|
|
|
device_set_spantype "$device"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$action" in
|
|
|
|
list)
|
|
|
|
show_spantypes
|
|
|
|
;;
|
2013-09-23 19:59:14 +08:00
|
|
|
dumpconfig)
|
2011-10-07 03:03:00 +08:00
|
|
|
dump_config
|
|
|
|
;;
|
|
|
|
set)
|
|
|
|
set_spantypes
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|