~ubuntu-branches/ubuntu/quantal/zaptel/quantal

« back to all changes in this revision

Viewing changes to xpp/utils/genzaptelconf

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2006-10-24 22:41:01 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20061024224101-464p4n2jk16n1jrh
Tags: 1:1.2.10.dfsg-2
* bristuff-0.3.0-PRE-1v
* Remove redundant GPL LICENCE text

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
# The script uses a number of bash-specific features
31
31
# TODO: either ditch them or convert to perl
32
 
# Don't override variables here. Override them in /etc/default/zaptel
33
 
#
34
 
# 0.5.3:
35
 
# * Experimental support for Sangoma analog cards (A20x)
36
 
# * Support OpenVox A1200P (A TDM400P clone refactored)
37
 
# * Fixed timeout for Astribank load
38
 
# * Delete unsuccessfully-probe modules
39
 
# * Pass callerid from trunks
40
 
# * A simpler wait_for_xpp using new proc wait_for_xpds
41
 
# * fixed confusion between framing and coding for E1/{il/nl}
42
 
# 0.5.2:
43
 
# * Now it should detect most PRI cards and even wcusb
44
 
# 0.5.1:
45
 
# * Initial support for ztgsm (Junghanns's PCI GSM card)
46
 
# * Wait for the xpp module to register if just loaded
47
 
# 0.5.0:
48
 
# * Not trying to read from zaptel channels: we can run genzaptelconf even 
49
 
#   with asterisk up.
50
 
# * Don't add ztdummy to the list of detected modules
51
 
# 0.4.4:
52
 
# * remove ztdummy when rewriting modules file
53
 
# * Better ISDN PRI behaviour in Israel (il)
54
 
# 0.4.3:
55
 
# * Added -F: to disable writing about FXS ports in zapata.conf
56
 
# * if we have an astribank: start zaptel rather than simple ztcfg (xpd sync)
57
 
# 0.4.2: 
58
 
# * support for digital input/output ports of Astribank
59
 
# * Different ISDN parameters for the Netherlands (nl)
60
 
# * unload zaptel and its dependencies, not a hard-coded list
61
 
# * hence we can reduce the list of modules
 
32
# Don't override variables here. 
 
33
# Override them in /etc/default/zaptel (debian) or /etc/sysconfig/zaptel 
 
34
# (redhat/centos)
62
35
 
63
36
# /etc/default/zaptel may override the following variables
64
 
VERSION=0.5.3
65
 
rcsid='$Id: genzaptelconf 1229 2006-07-14 20:12:54Z tzafrir $'
 
37
VERSION=0.5.6
 
38
rcsid='$Id: genzaptelconf 1495 2006-10-03 04:49:19Z tzafrir $'
66
39
lc_country=us
67
40
base_exten=6000
68
41
# If set: no context changes are made in zapata-channels.conf
82
55
# will set this to "no", so you can safely leave it as "yes".
83
56
set_immediate=yes
84
57
 
85
 
ZAPCONF_FILE=/etc/zaptel.conf
86
 
ZAPATA_FILE=/etc/asterisk/zapata-channels.conf
87
 
ZAPTEL_BOOT=/etc/default/zaptel
 
58
ZAPCONF_FILE=${ZAPCONF_FILE:-/etc/zaptel.conf}
 
59
ZAPCONF_FILE_SYSTEM=$ZAPCONF_FILE
 
60
ZAPATA_FILE=${ZAPATA_FILE:-/etc/asterisk/zapata-channels.conf}
 
61
ZAPTEL_BOOT_DEBIAN=${ZAPTEL_BOOT_DEBIAN:-/etc/default/zaptel}
 
62
ZAPTEL_BOOT_FEDORA=${ZAPTEL_BOOT_FEDORA:-/etc/sysconfig/zaptel}
88
63
MODLIST_FILE=/etc/modules
 
64
MODLIST_FILE_FEDORA=/etc/sysconfig/zaptel
89
65
exten_base_dir=/etc/asterisk/extensions-phones.d
90
66
exten_defs_file=/etc/asterisk/extensions-defs.conf
91
 
ZTCFG=/sbin/ztcfg
 
67
# how long to wait for /dev/zap/ctl to appear? (seconds)
 
68
DEVZAP_TIMEOUT=${DEVZAP_TIMEOUT:-20}
 
69
ZTCFG=${ZTCFG:-/sbin/ztcfg}
92
70
 
93
 
# a temporary directory. Created when the switch -r is parsed on getopts
94
 
# and deleted in the end on update_extensions_defs
 
71
# a temporary directory to store whatever we need to remember.
 
72
#
 
73
# The main loop of genconf is run in a sub-process.
95
74
tmp_dir=
96
75
 
97
76
# A list of all modules:
100
79
ALL_MODULES="zaphfc qozap ztgsm wctdm wctdm24xxp wcfxo wcfxs pciradio tor2 torisa wct1xxp wct4xxp wcte11xp wanpipe wcusb xpp_usb"
101
80
 
102
81
# read default configuration from /etc/default/zaptel
103
 
if [ -r $ZAPTEL_BOOT ]; then . $ZAPTEL_BOOT; fi
 
82
if [ -r $ZAPTEL_BOOT_DEBIAN ]; then . $ZAPTEL_BOOT_DEBIAN; fi
 
83
if [ -r $ZAPTEL_BOOT_FEDORA ]; then . $ZAPTEL_BOOT_FEDORA; fi
 
84
 
 
85
if [ ! -x "$ZTCFG" ]; then
 
86
        # Work around a bug in the rpm package: ztcfg should be in 
 
87
        # /sbin as it may be required for starting a network interface
 
88
        if [ -x /usr/sbin/ztcfg ]; then
 
89
                ZTCFG=/usr/sbin/ztcfg
 
90
        else
 
91
                echo >&2 "ztcfg is not on found, do you have zaptel properly installed?"
 
92
                exit 1;
 
93
        fi
 
94
fi
104
95
 
105
96
# it is safe to use -c twice: the last one will be used.
106
97
ztcfg_cmd="$ZTCFG -c $ZAPCONF_FILE"
113
104
do_unload=no
114
105
do_module_list=no
115
106
verbose=no
 
107
do_restart=yes
116
108
fxsdisable=no
117
 
rapid_extens=''
118
 
# global: current extension number in extensions list. Should only be 
119
 
# changed in print_pattern:
120
 
rapid_cur_exten=1 
121
 
# set the TRUNK in extensidialplan dialplan defs file  rapid_conf_mode=no
 
109
 
 
110
span_te_timing_counter=1
122
111
 
123
112
die() {
124
113
        echo "$@" >&2
132
121
        echo "$@"   >&2
133
122
}
134
123
 
 
124
# Wait for udev to generate /dev/zap/ctl, if needed:
 
125
wait_for_zapctl() {
 
126
        # if device file already exists, or if zaptel has failed to load: 
 
127
        # no point waiting.
 
128
        if [ -c /dev/zap/ctl ] || ! grep -q zaptel /proc/modules ; then
 
129
          return
 
130
  fi
 
131
  say "Waiting for /dev/zap/ctl to be generated"
 
132
  devzap_found=0
 
133
  for i in `seq $DEVZAP_TIMEOUT`; do
 
134
                sleep 1
 
135
                if [ -c /dev/zap/ctl ]; then
 
136
                  devzap_found=1
 
137
                        break
 
138
                fi
 
139
        done
 
140
        if [ "$devzap_found" != 1 ]; then
 
141
          say "Still no /dev/zap/ctl after $devzap_timeout seconds."
 
142
          echo >&2 "No /dev/zap/ctl: cannot run ztcfg. Aborting."
 
143
        fi
 
144
}
 
145
 
135
146
run_ztcfg() {
 
147
        # Run ztcfg itself
136
148
        if [ "$verbose" = no ]; then
137
149
                $ztcfg_cmd "$@"
138
150
        else
141
153
        fi
142
154
}
143
155
 
144
 
update_module_list() {
 
156
update_module_list_debian() {
 
157
        say "Updating Debian modules list $MODLIST_FILE."
145
158
        del_args=`for i in $ALL_MODULES ztdummy
146
159
        do
147
160
                echo "$i" | sed s:.\*:-e\ '/^&/d':
158
171
        done >> "$MODLIST_FILE"
159
172
}
160
173
 
161
 
do_update() {
162
 
        if [ ! -d `dirname ${ZAPTEL_BOOT}` ]
163
 
        then
164
 
                return
165
 
        fi
166
 
        sed -i.bak "s/^$1=.*\$/$1=\"$2\"/" ${ZAPTEL_BOOT}
167
 
        if ! grep -q "^$1=" ${ZAPTEL_BOOT}; then
168
 
                echo "$1=\"$2\"" >> ${ZAPTEL_BOOT}
169
 
        fi
170
 
}
171
 
 
172
 
update_extensions_defs() {
173
 
        if [ "$rapid_conf_mode" = 'yes' ]
174
 
        then
175
 
                say "DEBUG: Updating dialplan defs file $exten_defs_file"
176
 
                if [ "`echo $tmp_dir/fxo_* | grep -v '*'`" != '' ]
177
 
                then
178
 
                        trunk_nums=`cat $tmp_dir/fxo_* | sort -n | xargs`
179
 
                        say "Configuring TRUNK to be [first of] zaptel channels: $trunk_nums"
180
 
                        trunk_dev=`echo $trunk_nums| sed -e 's/ /\\\\\\&/g' -e 's/[0-9a-zA-Z]\+/Zap\\\\\\/&/g'`
181
 
                        echo >&2 sed -i "s/^TRUNK.*=>.*/TRUNK => $trunk_dev/" $exten_defs_file
182
 
                        sed -i "s/^TRUNK.*=>.*/TRUNK => $trunk_dev/" $exten_defs_file
183
 
                        if ! grep -q "^TRUNK =>" $exten_defs_file; then
184
 
                                trunk_dev=`echo $trunk_nums| sed -e 's/ /&/g' -e 's/[0-9a-zA-Z]*/Zap\\/&/g'`
185
 
                                echo "TRUNK => $trunk_dev" >> $exten_defs_file
186
 
                        fi
187
 
                else
188
 
                        say "Warning: No FXO channel for trunk. Moving on."
189
 
                fi
190
 
                if [ "`echo $tmp_dir/fxs_* | grep -v '*'`" != '' ]
191
 
                then
192
 
                        fxs_nums=`cat $tmp_dir/fxs_* | sort -n | xargs`
193
 
                        zap_nums=`grep '^[^;].*Zap/\${CHAN_ZAP_' $exten_base_dir/*.conf | \
194
 
                                sed -e 's/.*Zap\/\${CHAN_ZAP_\([0-9]*\)}.*/\1/' | sort -u | xargs`
195
 
                        say "Configuring channels: $fxs_nums as channel placeholders: $zap_nums"
196
 
                        j=1
197
 
                        for i in $zap_nums
198
 
                        do
199
 
                                chan=`echo $fxs_nums | awk "{print \\$$i}"`
200
 
                                if [ "$chan" = '' ]
201
 
                                then
202
 
                                        # if the result is empty, we probably got past the last one.
203
 
                                        # bail out.
204
 
                                        say "Warning: No FXS channel for CHAN_ZAP_$i. Moving on"
205
 
                                        break
206
 
                                fi
207
 
                                say "DEBUG: setting channel $chan to placeholder $i"
208
 
                                if grep -q "^CHAN_ZAP_$i " $exten_defs_file
209
 
                                then
210
 
                                        sed -i -e "s/^CHAN_ZAP_$i .*/CHAN_ZAP_$i => Zap\/$chan/" $exten_defs_file
211
 
                                else
212
 
                                        echo "CHAN_ZAP_$i => Zap/$chan" >> $exten_defs_file
213
 
                                fi
214
 
                        done
215
 
                fi      
216
 
                # cleaning up the temp dir
217
 
        fi
218
 
        if [ -d "$tmp_dir" ]; then rm -rf "$tmp_dir"; fi
219
 
}
 
174
update_module_list_fedora() {
 
175
        say "Updating modules list in zaptel init config $MODLIST_FILE_FEDORA."
 
176
        sed -i.bak -e '/^MODULES=/d' "$MODLIST_FILE_FEDORA"
 
177
        echo "MODULES=\"$*\"" >> "$MODLIST_FILE_FEDORA"
 
178
}
 
179
 
 
180
update_module_list() {
 
181
  if   [ -f "$MODLIST_FILE" ]; then
 
182
          update_module_list_debian "$@"
 
183
        elif [ -f "$MODLIST_FILE_FEDORA" ]; then
 
184
          update_module_list_fedora "$@"
 
185
        else
 
186
          die "Can't find a modules list to update. Tried: $MODLIST_FILE, $MODLIST_FILE_FEDORA. Aborting"
 
187
        fi
 
188
}
 
189
 
 
190
 
 
191
 
 
192
zap_reg_xpp() {
 
193
        if [ ! -d /proc/xpp ]; then return; fi
 
194
 
 
195
        # Get a list of connected Astribank devices, sorted by the name of
 
196
        # the USB connector. That order is rather arbitrary, but will not
 
197
        # change without changes to the cabling.
 
198
        xbusses=`sort -k 2 /proc/xpp/xbuses | awk -F: '/STATUS=connected/ {print $1}'`
 
199
 
 
200
        # get a list of XPDs that were not yet registered as zaptel spans.
 
201
        # this will be the case if you set the parameter zap_autoreg=0 to
 
202
        # the module xpp
 
203
        # Append /dev/null to provide a valid file name in case of an empty pattern.
 
204
        xbusses_pattern=`echo $xbusses| sed -e 's|XBUS-[0-9]*|/proc/xpp/&/XPD-*/zt_registration|g'`' /dev/null'
 
205
        xpds_to_register=`grep -l 0 $xbusses_pattern`
 
206
        for file in $xpds_to_register; do
 
207
                echo 1 >$file
 
208
        done
 
209
}
 
210
 
220
211
 
221
212
check_for_astribank(){
222
213
        if ! grep -q XPP_IN/ /proc/zaptel/* 2>/dev/null
247
238
        echo >&2 "  -u: Unload zaptel modules"
248
239
        echo >&2 "  -v: verbose"
249
240
        echo >&2 "  -s: Don't fail if asterisk is running. Stop it"
250
 
        echo >&2 "  -r: rapid configuration mode: configure Zaptel FXS channels from "
251
 
        echo >&2 "      existing Rapid extension files. FXOs will all be TRUNK "
 
241
        echo >&2 "  -R: Don't restart asterisk in the end."
252
242
}
253
243
 
254
244
# $1: channel number
272
262
        then method=ls
273
263
        fi
274
264
        case "$mode" in
275
 
        zaptel)
 
265
        list) echo $chan $sig $astbanktype;;
 
266
        files)
276
267
                # sadly, both input ports and output ports go into the same span as 
277
268
                # the FXS ports. Thus we need to separate between them. See also 
278
269
                # the zapata.conf section:
279
 
                if [ "$astbank_type" != '' ]; then echo "# astbanktype: $astbank_type"; fi
280
 
                echo "${sig}$method=$chan" 
281
 
                ;;
282
 
        list) echo $chan $sig $astbanktype;;
283
 
        zapata)
 
270
                if [ "$astbank_type" != '' ]; 
 
271
                        then echo "# astbanktype: $astbank_type" >>$zaptel_file; 
 
272
                fi
 
273
                echo "${sig}$method=$chan" >>$zaptel_file
284
274
                # zap2amp will rewrite those from zaptel.conf and hints there
285
 
                if [ "$fxsdisable" = 'yes' ] && [ $sig = 'fxo' ]; then return; fi
 
275
                if [ "$fxsdisable" = 'yes' ] && [ "$sig" = 'fxo' ]; then return; fi
286
276
                        
287
 
                echo "signalling=${sig}_$method"
 
277
                echo "signalling=${sig}_$method" >>$zapata_file
288
278
                if [ "$sig" = 'fxo' ]
289
279
                then
290
280
                        # to preconfigure channel 1's extension to 550, set
297
287
                  var_name=`echo chan_${chan}_cntxt`
298
288
                        cfg_cntxt=`echo ${!var_name} | tr -d -c 0-9`
299
289
                        
300
 
                        # if option -E was given, get configuration from current extension
301
 
                        if [ "$rapid_conf_mode" = 'yes' ]
302
 
                        then
303
 
                                rap_exten=`echo $rapid_extens |awk "{print \\$$rapid_cur_exten}"`
304
 
                                if [ "$rap_exten" != '' ]
305
 
                                then
306
 
                                        rap_cfgfile="$exten_base_dir/$rap_exten.conf"
307
 
                                        if [ -r "$rap_exten" ]
308
 
                                        then
309
 
                                                cfg_exten=$rap_exten
310
 
                                                # the vmbox is the third parameter to stdexten
311
 
                                                rap_vmbox=`grep '^[^;].*Macro(stdexten' $rap_exten | cut -d, -f3 \
312
 
                                                        | cut -d')' -f1 | tr -d -c '0-9@a-zA-Z'`
313
 
                                                if [ "$rap_vmbox" ]!= '' ; then cfg_vmbox=$rap_vmbox; fi
314
 
                                        fi
315
 
                                fi
316
 
                                rapid_cur_exten=$(($rapid_cur_exten + 1))
317
 
                        fi
318
 
                        
319
290
                        if [ "$cfg_exten" = '' ]
320
291
                        then # No extension number set for this channel
321
292
                                exten=$(($chan+$base_exten))
329
300
                        else # use the pre-configured extension number
330
301
                                vmbox=$cfg_vmbox
331
302
                        fi
332
 
                        echo "callerid=\"Channel $chan\" <$exten>"
333
 
                        echo "mailbox=$exten"
 
303
                        echo "callerid=\"Channel $chan\" <$exten>"     >> $zapata_file
 
304
                        echo "mailbox=$exten"                          >> $zapata_file
334
305
                        if [ "$group_manual" != "yes" ]
335
306
                        then 
336
 
                                echo "group=$group_phones"
 
307
                                echo "group=$group_phones"                   >> $zapata_file
337
308
                        fi
338
309
                        if [ "$context_manual" != "yes" ]
339
310
                        then
340
311
                                if [ "$astbank_type" != '' ];
341
312
                                then 
342
313
                                        context_var_name=context_$astbank_type
343
 
                                        echo context=${!context_var_name}
 
314
                                        echo context=${!context_var_name}          >> $zapata_file
344
315
                                else
345
 
                                        echo "context=$context_phones"
 
316
                                        echo "context=$context_phones"             >> $zapata_file
346
317
                                fi
347
318
                        fi
348
319
                else # this is an FXO (trunk/phone: FXO signalling)
349
320
                  # we have may have set it. So reset it:
350
 
                        echo "callerid=asrecieved"
351
 
                        echo "mailbox="
 
321
                        echo "callerid=asreceived"                     >> $zapata_file
 
322
                        echo "mailbox="                                >> $zapata_file
352
323
                        if [ "$group_manual" != "yes" ]
353
324
                        then 
354
 
                                echo "group=$group_lines"
 
325
                                echo "group=$group_lines"                    >> $zapata_file
355
326
                        fi
356
327
                        if [ "$context_manual" != "yes" ]
357
328
                        then 
358
 
                                echo "context=$context_lines"
 
329
                                echo "context=$context_lines"                >> $zapata_file
359
330
                        fi
360
331
                        if [ "$lc_country" = 'uk' ]
361
332
                        then
362
 
                          echo "cidsignalling=v23"
 
333
                          echo "cidsignalling=v23"                     >> $zapata_file
363
334
                          case $line in 
364
 
                          *WCFXO*) echo "cidstart=history";;
365
 
                          *)       echo "cidstart=polarity";; #a TDM400
 
335
                          *WCFXO*) echo "cidstart=history"             >> $zapata_file;;
 
336
                          *)       echo "cidstart=polarity"            >> $zapata_file;;
366
337
                          esac
367
338
                        fi
368
 
                        echo ";;; line=\"$line\""
 
339
                        echo ";;; line=\"$line\""                      >> $zapata_file
369
340
                        # if kewlstart is not used, busydetect has to be employed:
370
341
                        if [ "$method" = 'ls' ]
371
 
                        then echo 'busydetect=yes'
372
 
                        else echo 'busydetect=no'
 
342
                        then echo 'busydetect=yes'                     >> $zapata_file
 
343
                        else echo 'busydetect=no'                      >> $zapata_file
373
344
                        fi
374
345
                fi
375
346
 
376
347
                if [ "$set_immediate" = 'yes' ]
377
348
                then
378
349
                        if [ "$astbank_type" = 'input' ]
379
 
                        then echo 'immediate=yes'
380
 
                        else echo 'immediate=no'
 
350
                        then echo 'immediate=yes'                      >> $zapata_file
 
351
                        else echo 'immediate=no'                       >> $zapata_file
381
352
                        fi
382
353
                fi
383
 
                echo "channel => $chan"
384
 
                echo ""
 
354
                echo "channel => $chan"                          >> $zapata_file
 
355
                echo ""                                          >> $zapata_file
385
356
 
386
357
                # Keep a note of what channels we have identified
387
358
                say "DEBUG: adding to channels list: channel: $chan, sig: $sig"
483
454
 
484
455
# sleep a while until the xpp modules fully register
485
456
wait_for_xpp() {
 
457
        case "`uname -r`" in
 
458
        2.6.8*)
 
459
                autoreg="/sys/module/xpp/zap_autoreg"
 
460
                ;;
 
461
        *)
 
462
                autoreg="/sys/module/xpp/parameters/zap_autoreg"
 
463
                ;;
 
464
        esac
486
465
        if [ -d /proc/xpp ] && \
487
 
           [ "`cat /sys/module/xpp/parameters/zap_autoreg`" = 'Y' ]
 
466
           [ "`cat $autoreg`" = 'Y' ]
488
467
        then
489
468
                # wait for the XPDs to register:
490
469
                # TODO: improve error reporting and produce a messagee here
491
 
                cat /proc/xpp/XBUS-*/wait_for_xpds 2>/dev/null >/dev/null  || true
 
470
                cat /proc/xpp/XBUS-*/waitfor_xpds 2>/dev/null >/dev/null  || true
492
471
        fi
493
472
}
494
473
 
495
474
detect() {
496
475
        unload_modules
 
476
        temporary_zapconf save
497
477
        load_modules
 
478
        temporary_zapconf restore
498
479
        modlist="$probed_modules"
499
 
        #for i in $ALL_MODULES
500
 
        #do
501
 
        #       if lsmod | grep "^$i  *" > /dev/null; then
502
 
        #               modlist="$modlist $i"
503
 
        #       fi
504
 
        #done
505
480
        modlist="$(echo $modlist)"              # clean spaces
506
481
        if [ "$do_module_list" = yes ]
507
482
        then
508
 
                say "Updating '${MODLIST_FILE}'"
509
483
                update_module_list "$modlist"
510
484
        fi
511
485
        if echo $modlist | grep -q xpp_usb; then wait_for_xpp; fi
512
486
}
513
487
 
 
488
# The module configuration generated by zaptel includes a totally useless 
 
489
# automatic run of ztcfg after modules loading. As a workaround for that, 
 
490
# we provide an empty zaptel.conf temporarily.
 
491
 
492
# At hardware detection time we shouldn't really touch zaptel.conf . So we 
 
493
# must keep a copy of it somewhere.
 
494
#
 
495
# I use ZAPCONF_FILE_SYSTEM rather than ZAPCONF_FILE, as the bogus modprobe 
 
496
# entries will not be initiated by us.
 
497
#
 
498
# ZAPCONF_FILE_TMP is a "global", as it needs to be preserved between the
 
499
# call to 'save' and to 'restore'
 
500
ZAPCONF_FILE_TMP=
 
501
temporary_zapconf() {
 
502
  case "$1" in
 
503
        save)
 
504
          say "Temporarily moving zaptel.conf aside to work around broken modprobe.conf"
 
505
                ZAPCONF_FILE_TMP=`mktemp -t zaptel.conf-XXXXXX` || die "Error creating temporary zaptel.conf"
 
506
                cp -a $ZAPCONF_FILE_SYSTEM $ZAPCONF_FILE_TMP
 
507
                echo -n >$ZAPCONF_FILE_SYSTEM
 
508
                ;;
 
509
        restore)
 
510
                # restore original zaptel.conf:
 
511
                if [ "$ZAPCONF_FILE_TMP" = '' ]; then return; fi
 
512
                mv $ZAPCONF_FILE_TMP $ZAPCONF_FILE_SYSTEM
 
513
                ZAPCONF_FILE_TMP=''
 
514
                ;;
 
515
        esac
 
516
}
 
517
 
514
518
# TODO: kill this function. It is now unreferenced from anywhere.
515
519
check_tdm_sigtype() {
516
520
        chan_num=$1
537
541
        fi      
538
542
}
539
543
 
540
 
# output a list of extensions that need a channel
541
 
get_rapid_extens() {
542
 
        if [ "$rapid_conf_mode" = 'yes' ]
543
 
        then
544
 
                rapid_extens=`grep -l '^[^;].*Zap/\${CHAN_ZAP_' $exten_base_dir/*.conf 2>/dev/null | \
545
 
                        rev | cut -d/ -f1 | cut -d. -f2- | rev | xargs`
546
 
                say "Need to configure extensions: $rapid_extens"
547
 
        fi
548
 
}
549
 
 
550
544
genconf() {
551
545
        local mode=$1
552
 
 
553
546
        # reset FXO list (global)
554
547
        #say "DEBUG: resetting channels lists"
555
548
        rm -f $tmp_dir/fx{s,o}_*
569
562
 
570
563
 
571
564
        case "$mode" in 
572
 
                zaptel)
573
 
                        cat <<EOF
 
565
                files)
 
566
                        local zaptel_file=`mktemp -t genzaptelconf-zaptel-XXXXXX`
 
567
                        local zapata_file=`mktemp -t genzaptelconf-zapata-XXXXXX`
 
568
                        cat <<EOF >$zaptel_file
574
569
# Autogenerated by $0 -- do not hand edit
575
570
# Zaptel Configuration File
576
571
#
580
575
# It must be in the module loading order
581
576
 
582
577
EOF
583
 
                        ;;
584
 
                zapata)
585
 
                        cat <<EOF
 
578
                        cat <<EOF >$zapata_file
586
579
; Autogenerated by $0 -- do not hand edit
587
580
; Zaptel Channels Configurations (zapata.conf)
588
581
;
601
594
        # The '<(command)' syntax creates a temporary file whose content is is the
602
595
        # output of 'command'.
603
596
        #
604
 
        # This approach failed with the T1 card we have: the read operation simply 
605
 
        # hung. 
606
 
        #
607
597
        # Another problem with such an approach is how to include an existing 
608
598
        # configuration file. For instance: how to include some default settings.
609
599
        #
615
605
                # the first line is the title line. It states the model name
616
606
                # the second line is empty
617
607
                title=`head -n 1 $procfile`
618
 
                echo ""
619
 
                # stuff that needs to be remembered accross lines (for PRI support)
620
 
                echo "$rem_char $title"
 
608
                # stuff that needs to be remembered accross lines (for PRI/BRI support)
 
609
                echo ""         >>$zaptel_file
 
610
                echo "# $title" >>$zaptel_file
 
611
                echo ""         >>$zapata_file
 
612
                echo "; $title" >>$zapata_file
621
613
                echo '-1'  >$tmp_dir/span_begin
622
614
                echo '-1'  >$tmp_dir/span_end
623
 
                echo '1'   >$tmp_dir/span_timing
 
615
                echo '0'   >$tmp_dir/span_timing
624
616
                echo '1'   >$tmp_dir/span_lbo
625
617
                echo ''    >$tmp_dir/span_framing
626
618
                echo 'ami' >$tmp_dir/span_coding
627
619
                echo ''    >$tmp_dir/span_switchtype
628
620
                echo ''    >$tmp_dir/span_signalling
629
621
 
 
622
                # Check if ZapBRI cards are in TE or NT mode
630
623
                if echo $title | egrep -q '((quad|octo)BRI PCI ISDN Card.* \[NT\]\ |octoBRI \[NT\] |HFC-S PCI A ISDN.* \[NT\] )'
631
624
                then
632
625
                        echo 'nt' >$tmp_dir/span_termtype
644
637
                        chan_num=`echo $line |awk '{print $1}'`
645
638
                        case "$line" in
646
639
                        *WCTDM/*|*/WRTDM/*|*OPVXA1200/*) 
 
640
                                # TDM400P/2400P and similar cards (Sangoma A200, OpenVox A1200)
647
641
                                # this can be either FXS or FXO
648
642
                                maybe_fxs=0
649
643
                                maybe_fxo=0
663
657
                                if [ $maybe_fxo = 1 ]; then print_pattern $chan_num fxs $mode; fi
664
658
                                ;;
665
659
                        *WCFXO/*) 
 
660
                                # X100P
666
661
                                print_pattern $chan_num fxs $mode || \
667
662
                                echo "$rem_char channel $chan_num, WCFXO, inactive."
668
663
                                ;;
670
665
                                print_pattern $chan_num fxo $mode
671
666
                                ;;
672
667
                        *XPP_FXO/*)
 
668
                                # Astribank FXO span
673
669
                                print_pattern $chan_num fxs $mode
674
670
                                ;;
675
671
                        *XPP_FXS/*)
 
672
                                # Astribank FXS span (regular port)
676
673
                                print_pattern $chan_num fxo $mode
677
674
                                ;;
678
675
                        *XPP_OUT/*)
 
676
                                # Astribank FXS span (output port)
679
677
                                print_pattern -a output $chan_num fxo $mode
680
678
                                ;;
681
679
                        *XPP_IN/*)
 
680
                                # Astribank FXS span (input port)
682
681
                                print_pattern -a input $chan_num fxo $mode
683
682
                                ;;
684
 
                        *ZTHFC*/*|*ztqoz*/*|*ztgsm/*|*TE4/*|*TE2/*|*WCT1/*|*Tor2/*|*TorISA/*) # should also be used for other PRI channels
 
683
                        *ZTHFC*/*|*ztqoz*/*|*ztgsm/*|*TE[24]/*|*WCT1/*|*Tor2/*|*TorISA/*|*XPP_BRI_*/*|*WP[TE]1/*)
 
684
                                # PRI/BRI channel
 
685
                                # Rather than identifying cards by the header line, we identify them by the channel names
 
686
                                # This is shorter. This also allows us to count the channel numbers and check if a PRI
 
687
                                # card is E1 or T1.
685
688
                                if [ "`cat $tmp_dir/span_begin`" = "-1" ]
686
689
                                then
687
690
                                        echo $chan_num      >$tmp_dir/span_begin
688
691
                                        echo $span_num      >$tmp_dir/span_num
689
 
                                        case "$line" in
690
 
                                        *ZTHFC*/*|*ztqoz*/*)
 
692
                                        # The Astribank channels provide the information of TE/NT in the channel name. So 
 
693
                                        # why not use it?
 
694
                                        case "$line" in
 
695
                                        *XPP_BRI_TE/*) echo 'te' >$tmp_dir/span_termtype;;
 
696
                                        *XPP_BRI_NT/*) echo 'nt' >$tmp_dir/span_termtype;;
 
697
                                        esac
 
698
                                        case "$line" in
 
699
                                        *ZTHFC*/*|*ztqoz*/*|*XPP_BRI_*/*)
 
700
                                                # BRI channel
691
701
                                                echo 'ccs'          >$tmp_dir/span_framing
692
702
                                                echo 'euroisdn'     >$tmp_dir/span_switchtype
693
703
                                                if [ "`cat $tmp_dir/span_termtype`" = 'nt' 2>/dev/null ]
694
704
                                                then
695
 
                                                        echo 'bri_net' >$tmp_dir/span_signalling
 
705
                                                        echo 'bri_net' >$tmp_dir/span_signalling 
696
706
                                                else
697
707
                                                        echo 'bri_cpe' >$tmp_dir/span_signalling
698
708
                                                fi
699
709
                                                ;;
700
710
                                        *ztgsm*/*)
 
711
                                                # Junghanns's GSM cards. 
701
712
                                                echo 'ccs'          >$tmp_dir/span_framing
702
713
            # what switch type? Any meaning to it?
703
714
                                                echo 'gsm'          >$tmp_dir/span_signalling
704
715
                                                ;;
705
 
                                        *TE4/*|*TE2/*|*WCT1/*|*Tor2/*|*TorISA/*)
 
716
                                        *TE[24]/*|*WCT1/*|*Tor2/*|*TorISA/*|*WP[TE]1/*)
 
717
                                                # PRI span (E1/T1)
706
718
                                                echo 'esf'       >$tmp_dir/span_framing
707
719
                                                echo 'b8zs'      >$tmp_dir/span_coding
708
720
                                                echo 'national'  >$tmp_dir/span_switchtype
723
735
                                                        echo 'hdb3'      >$tmp_dir/span_coding
724
736
                                                        echo 'crc4'      >$tmp_dir/span_yellow
725
737
                                                        echo 'euroisdn'  >$tmp_dir/span_switchtype
 
738
                                                        ;;
 
739
                                                cl)
 
740
                                                        echo 'ccs'       >$tmp_dir/span_framing
 
741
                                                        echo 'hdb3'      >$tmp_dir/span_coding
 
742
                                                        #echo 'crc4'      >$tmp_dir/span_yellow
 
743
                                                        echo 'national'  >$tmp_dir/span_switchtype
 
744
                                                        ;;
726
745
                                                esac
727
746
                                        ;;
728
747
                                        esac
732
751
                                echo $chan_num      >$tmp_dir/span_end
733
752
                                ;;
734
753
                        '') ;;          # Empty line (after span header)
735
 
                        *) echo "$rem_char ??: $line";;
 
754
                        *) 
 
755
                                case "$mode" in
 
756
                                list) echo "# ??: $line";;
 
757
                                files)
 
758
                                        echo "# ??: $line" >>$zaptel_file
 
759
                                        echo "; ??: $line" >>$zaptel_file
 
760
                                esac
 
761
                                ;;
736
762
                        esac
737
763
                done
 
764
                # end of part in sub-process.
 
765
                
738
766
                if [ "`cat $tmp_dir/span_begin`" != -1 ]
739
767
                then # write PRI span ocnfig:
740
768
                        # read files to variables:
752
780
                                bchans="$span_begin-$(($span_end-1))"
753
781
                                ;;
754
782
                        31) #E1
755
 
                          dchan="$(($span_begin+15))"
 
783
                                dchan="$(($span_begin+15))"
756
784
                                bchans="$span_begin-$(($span_begin+14)),$(($span_begin+16))-$span_end"
 
785
                                if [ "$span_switchtype" = 'national' ]; then
 
786
                                        span_framing=ccs
 
787
                                        span_coding=hdb3
 
788
                                        span_switchtype=euroisdn
 
789
                                fi
 
790
                                ;;
 
791
                        esac
 
792
                        # Let's assume that a TE span should get the clock from the remote unit,
 
793
                        # and NT spans should provide timing. Just as a sane default.
 
794
                        # If we have several TE spans, the first will have priority 1, 
 
795
                        # second: 2, etc.
 
796
                        case "$span_signalling" in *_cpe*)
 
797
                                span_timing=$span_te_timing_counter
 
798
                                span_te_timing_counter=$(($span_te_timing_counter + 1))
757
799
                                ;;
758
800
                        esac
759
801
                        case "$mode" in
760
 
                        zaptel)
761
 
                                echo span=$span_num,$span_timing,$span_lbo,$span_framing,$span_coding$span_yellow
 
802
                        files)
 
803
                                echo span=$span_num,$span_timing,$span_lbo,$span_framing,$span_coding$span_yellow >> $zaptel_file
762
804
                                if [ "$span_termtype" != '' ]
763
 
                                then echo "# termtype: $span_termtype"
 
805
                                then echo "# termtype: $span_termtype" >>$zaptel_file
764
806
                                fi
765
 
                                echo bchan=$bchans
766
 
                                echo dchan=$dchan
767
 
                                ;;
768
 
                        zapata)
 
807
                                echo bchan=$bchans >>$zaptel_file
 
808
                                echo dchan=$dchan  >>$zaptel_file
 
809
                                
769
810
                                if [ "$span_termtype" != '' ]
770
811
                                then
771
812
                                        # an ISDN card's span that we know if it is in NT mode or TE mode.
772
813
                                        # NT is the same as FXS for us and TE is the same as FXO
773
814
                                        if [ "$span_termtype" = 'nt' ]
774
815
                                        then
775
 
                                                echo "callerid=\"Channels $span_begin - $span_end\" <$span_begin>"
 
816
                                                echo "callerid=\"Channels $span_begin - $span_end\" <$span_begin>" >> $zapata_file
776
817
                                                #echo "mailbox=$exten"
777
818
                                                if [ "$group_manual" != "yes" ]
778
819
                                                then 
779
 
                                                        echo "group=$group_phones"
 
820
                                                        echo "group=$group_phones"     >> $zapata_file
780
821
                                                fi
781
822
                                                if [ "$context_manual" != "yes" ]
782
823
                                                then 
783
 
                                                        echo "context=$context_phones"
 
824
                                                        echo "context=$context_phones" >> $zapata_file
784
825
                                                fi
785
826
                                        else # we have may have set it. So reset it:
786
 
                                                echo "callerid=asrecieved"
 
827
                                                echo "callerid=asreceived"
787
828
                                                #echo "mailbox="
788
829
                                                if [ "$group_manual" != "yes" ]
789
830
                                                then 
790
 
                                                        echo "group=$group_lines"
 
831
                                                        echo "group=$group_lines"      >> $zapata_file
791
832
                                                fi
792
833
                                                if [ "$context_manual" != "yes" ]
793
834
                                                then 
794
 
                                                        echo "context=$context_lines"
 
835
                                                        echo "context=$context_lines"  >> $zapata_file
795
836
                                                fi
796
837
                                        fi
797
838
                                fi
798
 
                                echo "switchtype = $span_switchtype"
799
 
                                echo "signalling = $span_signalling"
800
 
                                echo "channel => $bchans"
 
839
                                echo "switchtype = $span_switchtype" >> $zapata_file
 
840
                                echo "signalling = $span_signalling" >> $zapata_file
 
841
                                echo "channel => $bchans"            >> $zapata_file
801
842
                                ;;
802
843
                        list)
803
844
                                echo BRI/PRI: chans: $bchans, control: $dchan
806
847
                fi
807
848
        done
808
849
 
809
 
        if [ "$mode" = 'zaptel' ]
 
850
        if [ "$mode" = 'files' ]
810
851
        then
811
 
                cat <<EOF
 
852
                cat <<EOF >> ${zaptel_file}
812
853
 
813
854
# Global data
814
855
 
 
856
loadzone        = $loadzone
 
857
defaultzone     = $defaultzone
815
858
EOF
816
 
                echo "loadzone  = $loadzone"
817
 
                echo "defaultzone       = $defaultzone"
818
859
        fi
819
860
        
820
 
        if [ "$mode" = 'zapata' ] || [ "$mode" = 'list' ]
821
 
        then 
822
 
                update_extensions_defs 
823
 
        fi
 
861
        mv ${ZAPCONF_FILE} ${ZAPCONF_FILE}.bak
 
862
        mv $zaptel_file ${ZAPCONF_FILE}
 
863
        mv ${ZAPATA_FILE} ${ZAPATA_FILE}.bak
 
864
        mv $zapata_file ${ZAPATA_FILE}
 
865
        # cleaning up the temp dir
 
866
        if [ -d "$tmp_dir" ]; then rm -rf "$tmp_dir"; fi
824
867
}
825
868
 
826
 
while getopts 'c:de:Fhlm:Mrsuv' arg
 
869
while getopts 'c:de:Fhlm:MRsuv' arg
827
870
do
828
871
        case "$arg" in
829
872
                e) # guarantee that it is a number:
838
881
                l) mode='list' ;;
839
882
                M) do_module_list=yes; do_detect=yes ;;
840
883
                s) force_stop_ast=yes ;;
841
 
                r) 
842
 
                        rapid_conf_mode=yes 
843
 
                        ;;
 
884
                R) do_restart=no ;;
844
885
                h) usage; exit 0;;
845
886
                *) echo >&2 "unknown parameter -$arg, Aborting"; usage; exit 1;;
846
887
        esac
901
942
if [ "$mode" = list ]; then
902
943
        genconf list
903
944
else
 
945
        zap_reg_xpp
904
946
        check_for_astribank
905
 
        get_rapid_extens
906
 
        say "Generating '${ZAPCONF_FILE}'"
907
 
        mv "${ZAPCONF_FILE}" "${ZAPCONF_FILE}.bak"
908
 
        genconf zaptel > "${ZAPCONF_FILE}"
909
 
        say "Generating '${ZAPATA_FILE}'"
910
 
        mv "${ZAPATA_FILE}" "${ZAPATA_FILE}.bak"
911
 
        genconf zapata > "${ZAPATA_FILE}"
 
947
        wait_for_zapctl
 
948
        say "Generating '${ZAPCONF_FILE} and ${ZAPATA_FILE}'"
 
949
        genconf files
912
950
        if [ "$set_immediate" = 'yes' ] && [ -x /etc/init.d/zaptel ]
913
951
        then /etc/init.d/zaptel start
914
952
        else run_ztcfg
920
958
  rm -rf "$tmp_dir"
921
959
fi
922
960
 
923
 
if [ "$force_stop_ast" = 'yes' ]
924
 
then
925
 
  if [ -x /etc/init.d/asterisk ]
926
 
  then
927
 
    /etc/init.d/asterisk start 1>&2
928
 
  fi
 
961
if [ "$force_stop_ast" != 'yes' ] || [ "$do_restart" != 'yes' ]
 
962
then
 
963
  exit 0
 
964
fi
 
965
 
 
966
if [ -x /etc/init.d/asterisk ]
 
967
then
 
968
  /etc/init.d/asterisk start 1>&2
929
969
fi
930
970
 
931
971
# if in verbose mode: verify that asterisk is running
932
 
if [ "$verbose" != 'no' ] && [ "$force_stop_ast" = 'yes' ]
 
972
if [ "$verbose" != 'no' ] 
933
973
        then
934
974
        say "Checking channels configured in Asterisk:"
935
975
        sleep 1 # give it some time. This is enough on our simple test server