dahdi_handle_device: Auto assign only the device being added.
[dahdi/tools.git] / dahdi_span_config
1 #! /bin/sh
2 #
3 # /usr/share/dahdi/dahdi_span_config
4 #
5 # Called by UDEV when a dahdi span is added/removed
6 #
7
8 me=`basename $0`
9 dir=`dirname $0`
10 LOGGER="logger -i -t '$me'"
11 NAME=`basename "$DEVPATH" | tr -c 'A-Za-z0-9-' '_'`
12
13 exec 2> /dev/null
14 # Always redirect stderr somewhere, otherwise the shell script will die
15 # when it tries to do I/O related stuff on closed file descriptor.
16 # Our default is to throw it down the bit-bucket.
17 #exec 2> /dev/console
18 ## If you wish to trace this script:
19 #exec 2> "/tmp/${me}.$NAME" 1>&2
20
21 # Our directory in the beginning, so we can use local lab setup
22 PATH="$dir:/usr/sbin:/sbin:/usr/bin:/bin"
23
24 set -e
25
26 #echo >&2 "$0($ACTION): DEBUG($# args): '$*'"
27
28 # Do we have a configuration?
29 if [ -f /etc/dahdi/init.conf ]; then
30         . /etc/dahdi/init.conf
31 fi
32
33 if [ "$DAHDI_UDEV_DISABLE_SPANS" = 'yes' ]; then
34         echo "DAHDI_UDEV_DISABLE_SPANS=yes. Skip $DEVPATH" | $LOGGER
35         exit 0
36 fi
37
38 # Can we pass a different value so we can use
39 # alternate (testing) configuration?
40 # Meanwhile, make it hard-coded.
41 DAHDICONFDIR='/etc/dahdi'
42 export DAHDICONFDIR
43
44 run_dahdi_cfg() {
45         echo "dahdi_cfg: span $spanno <$basechan-$endchan> ($DEVPATH)"
46         dahdi_cfg -c "$cfg_file" -S "$spanno" -C "$basechan-$endchan"
47 }
48
49 configure_span() {
50         span_devpath="$1"
51         # Sanity check
52         checkit=`"dahdi_cfg" --help 2>&1 | grep -- '-S' | wc -l`
53         if [ "$checkit" != 1 ]; then
54                 echo "Bad dahdi_cfg (no -S support). Skipping"
55                 exit 0
56         fi
57
58         # Set variables
59         spanno=`echo "$span_devpath" | sed 's,.*/span-,,'`
60         basechan=`cat "$span_devpath/basechan"`
61         channels=`cat "$span_devpath/channels"`
62         endchan=`expr "$basechan" + "$channels" - 1`
63
64         # Configure DAHDI
65         cfg_file="$DAHDICONFDIR/system.conf"
66         if [ -r "$cfg_file" ]; then
67                 run_dahdi_cfg
68         else
69                 echo "Using auto-generated config for dahdi_cfg"
70                 cfg_file='-'
71                 DAHDI_CONF_FILE="$cfg_file" dahdi_genconf system | run_dahdi_cfg
72         fi
73         fxotune_cfg='/etc/fxotune.conf'
74         if [ -r "$fxotune_cfg" ]; then
75                 echo "fxotune: span $spanno <$basechan-$endchan> ($DEVPATH)"
76                 fxotune -s -b "$basechan" -e "$endchan"
77         fi
78
79         # Add to asterisk
80         asterisk -rx "dahdi create channels $basechan $endchan"
81 }
82
83 case "$ACTION" in
84 add)
85         echo "$ACTION: $DEVPATH" | $LOGGER
86
87         # Old driver. These scripts probably won't work anyway.
88         if [ ! -f /sys/module/dahdi/parameters/auto_assign_spans ]; then
89                 if [ -f /sys/module/dahdi ]; then
90                         $LOGGER "Old driver (no auto_assign_spans parameter). Skip $DEVPATH"
91                         exit 0
92                 fi
93         fi
94
95         if [ $(cat /sys/module/dahdi/parameters/auto_assign_spans) -eq 1 ]; then
96                 $LOGGER "auto_assign_spans=1. Skip $DEVPATH"
97                 exit 0
98         fi
99
100         # Can have alternate dahdi configuration directory for debugging
101         # export DAHDICONFDIR="/tmp/xortel/dahdi"
102
103         configure_span "/sys$DEVPATH" 2>&1 | $LOGGER
104         ;;
105 remove|online|offline)
106         # Nothing to do yet...
107         echo "$ACTION: $DEVPATH" | $LOGGER
108         ;;
109 *)
110         echo "UNHANDLED: $ACTION: $DEVPATH" | $LOGGER
111         ;;
112 esac
113