dahdi-tools/dahdi_span_types
Oron Peled 451a8b4d6f dahdi_span_types: allow defaults + overrides
* Allow wildcards for both device and span number (as before).
  Example:
     *        *:T1

* But now we are carefull to follow strict order in the configuration
  file. This means, if there are multiple matches -- last one wins.

* So we can use specialisation:
     *        *:T1	# Everything is T1
     FOO      [34]:T1	# Except spans 3,4 on the FOO device

* Added --dry-run and --verbose options.

* Updated the man-page:
  - Fixed "registered" => "assigned".
  - Use "line mode" for E1/J1/T1.
  - Document current changes.

Signed-off-by: Tzafrir Cohen <tzafrir.cohen@xorcom.com>
Acked-By: Russ Meyerriecks <rmeyerriecks@digium.com>
2014-01-23 21:28:59 +02:00

303 lines
7.4 KiB
Bash
Executable File

#! /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 " 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"
exit 1
}
# Parse command line options
TEMP=`getopt -o hnvk: --long help,dry-run,verbose,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
;;
-n|--dry-run)
shift
dry_run=true
;;
-v|--verbose)
shift
verbose=true
;;
-k|--key)
SPAN_ASSIGNMENTS_KEY="$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 SPAN_ASSIGNMENTS_KEY='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
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" | 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
}
dump_config() {
echo '#'
echo "# Autogenerated by $0 on `date`"
echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
echo ''
fmt="%-65s %s\n"
printf "$fmt" '# @location/hardware_id' 'span_type'
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)
printf "$fmt" "$id" "$st"
;;
*)
#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
echo "$spanno:$val" > "$attr_file"
fi
fi
done
}
set_all_devices() {
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
}
case "$action" in
list)
show_spantypes
;;
dumpconfig)
dump_config
;;
set)
set_all_devices
;;
*)
usage
;;
esac