dahdi-tools/span_types
Oron Peled 00af777a97 Newer version of DAHDI hotplug scripts
A newer version of the scripts fully adapted to pinned spans:
* handle_device does not run dahdi_cfg.
* A separate UDEV rule script for that: span_config. Should also work
  for the non-pinned case.
* span_assignments, span_types: add actions 'auto' (manually enable all)
  and 'dumpconfig' (dump current status in the format of configuration
  file).
* Fixed name of span_types and span_assignments (no '-').
* spantype.conf renamed span-types.conf: configuration files do have a
  dash.
* Those two are useful programs, insstalled to /usr/sbin.

Signed-off-by: Tzafrir Cohen <tzafrir.cohen@xorcom.com>
2013-10-01 21:51:46 +03:00

186 lines
3.7 KiB
Bash
Executable File

#! /bin/sh
#
# /usr/sbin/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 reads a configuration file /etc/dahdi/span-types.conf
# (the format is documented inside that file)
#
# A mandatory first argument is:
# list - to show existing E1/T1/J1 types
# dumpconfig - the same, but in a format (almost) suitable
# for the configuration file
# set - actually write the setting to the driver
#
# Examples:
# span_types list
# span_types dumpconfig
# span_types set # all devices
# span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
#
devbase='/sys/bus/dahdi_devices/devices'
DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
spantypes_conf="$DAHDICONFDIR/span-types.conf"
usage() {
echo >&2 "Usage: $0 {list|dumpconfig|set} [devpath ...]"
exit 1
}
if [ "$#" -eq 0 ]; then
usage
fi
action="$1"
shift
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=`echo $devbase/*`
fi
show_spantypes() {
echo "# PRI span types (E1/T1/J1)"
for device in $DEVICES
do
hw_id=`cat "$device/hardware_id"`
location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
cat "$device/spantype" | while read st; do
case "$st" in
*:[ETJ]1)
printf "%-10s %-20s %s\n" \
"$st" "[$hw_id]" "$location"
;;
esac
done | sort -n
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
hw_id=`cat "$device/hardware_id"`
location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
if [ -n "$hw_id" ]; then
id="$hw_id"
else
id="$location"
fi
#echo "# Device: [$hw_id] $location"
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' "$spantypes_conf"
}
conf_spans() {
hw_id="$1"
location="$2"
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
case "$hw_id" in
$id)
#echo >&2 "match([$id]): $spans"
SPANS="$SPANS $spans"
;;
esac
done
echo "$SPANS"
)
}
# Beware of special characters in attributes
attr_clean() {
cat "$1" | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
}
device_set_spantype() {
device="$1"
attr_file="$device/spantype"
hw_id=`attr_clean "$device/hardware_id"`
location='@'`cd "$device" && pwd -P | sed 's,/sys/devices/,,'`
spanspecs=`conf_spans "$hw_id" "$location"`
#echo >&2 "MATCHED($device): $spanspecs"
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() {
if [ ! -f "$spantypes_conf" ]; then
echo >&2 "$0: Missing configuration '$spantypes_conf'"
exit 1
fi
for device in $DEVICES
do
device_set_spantype "$device"
done
}
case "$action" in
list)
show_spantypes
;;
dumpconfig)
dump_config
;;
set)
set_spantypes
;;
*)
usage
;;
esac