3
# genzaptelconf: generate as smartly as you can:
5
# /etc/asterisk/zapata-channels.conf (to be #include-d into zapata.conf)
7
# With '-M' /etc/modules (list of modules to load)
9
# Copyright (C) 2005 by Xorcom <support@xorcom.com>
11
# This program is free software; you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation; either version 2 of the License, or
14
# (at your option) any later version.
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
# GNU General Public License for more details.
21
# You should have received a copy of the GNU General Public License
22
# along with this program; if not, write to the Free Software
23
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
# If you have any technical questions, contact
27
# Tzafrir Cohen <tzafrir.cohen@xorcom.com>
30
# The script uses a number of bash-specific features
31
# TODO: either ditch them or convert to perl
32
# Don't override variables here.
33
# Override them in /etc/default/zaptel (debian) or /etc/sysconfig/zaptel
36
# /etc/default/zaptel may override the following variables
38
rcsid='$Id: genzaptelconf 4266 2008-05-13 21:08:09Z tzafrir $'
40
# set to: ls, ks or gs for (Loopstart, Kewlstart and GroundStart)
41
# on FXS channels (FXO signalling).
44
# If set: no context changes are made in zapata-channels.conf
46
context_lines=from-pstn # context into which PSTN calls go
47
context_phones=from-internal # context for internal phones calls.
48
# The two below apply to input and output ports of the Xorcom Astribank:
49
context_input=astbank-input
50
context_output=astbank-output # useless, but helps marking the channels :-)
51
# TODO: what about PRI/BRI?
52
# If set: no group changes are made in zapata-channels.conf
54
group_phones=5 # group for phones
55
group_lines=0 # group for lines
56
# Set fxs_immediate to 'yes' to make all FXS lines answer immediately.
59
ZAPCONF_FILE=${ZAPCONF_FILE:-/etc/zaptel.conf}
60
ZAPCONF_FILE_SYSTEM=$ZAPCONF_FILE
61
ZAPATA_FILE=${ZAPATA_FILE:-/etc/asterisk/zapata-channels.conf}
62
ZAPSCAN_FILE=${ZAPSCAN_FILE:-/etc/asterisk/zapscan.conf}
63
ZAPTEL_BOOT_DEBIAN=${ZAPTEL_BOOT_DEBIAN:-/etc/default/zaptel}
64
ZAPTEL_BOOT_FEDORA=${ZAPTEL_BOOT_FEDORA:-/etc/sysconfig/zaptel}
65
MODLIST_FILE=/etc/modules
66
MODLIST_FILE_FEDORA=/etc/sysconfig/zaptel
67
exten_base_dir=/etc/asterisk/extensions-phones.d
68
exten_defs_file=/etc/asterisk/extensions-defs.conf
70
xpp_sync=/usr/sbin/xpp_sync
71
zt_registration=/usr/sbin/zt_registration
72
# how long to wait for /dev/zap/ctl to appear? (seconds)
73
DEVZAP_TIMEOUT=${DEVZAP_TIMEOUT:-20}
74
ZTCFG=${ZTCFG:-/sbin/ztcfg}
75
# BRI/PRI spans will be in an additional per-span group whose number
76
# is SPAN_GROUP_BASE + span-number
78
# set to "yes" to make BRI NT spans set overlapdial (handy for ISDN phones
82
# a temporary directory to store whatever we need to remember.
84
# The main loop of genconf is run in a sub-process.
87
# A list of all modules:
88
# - the list of modules which will be probed (in this order) if -d is used
89
# - The module that will be deleted from /etc/modules , if -d -M is used
90
ALL_MODULES="wct4xxp wcte12xp wcte11xp wct1xxp wanpipe tor2 torisa qozap vzaphfc zaphfc ztgsm wctdm24xxp wctdm opvxa1200 wcfxo pciradio wcusb xpp_usb"
92
# The name of the variable in /etc/sysconfig/zaptel into which to set
93
# the list of detected modules.
95
# On SuSE with the rpm package:
96
#modules_var=ZAPTEL_MODULES
98
# What signalling to give to ZapBRI channels?
99
# bri: bri_net; bri_cpe (Bristuffed Asterisk. No multi- support)
100
# bri_ptmpi: bri_net_ptmp; bri_cpe_ptmp (Bristuffed Asterisk, multi- support)
101
# pri: pri_net; pri_cpe (Recent Asterisk. Experimental)
102
#ZAPBRI_SIGNALLING=bri
103
ZAPBRI_SIGNALLING=bri_ptmp
104
#ZAPBRI_SIGNALLING=pri
105
zapconf_def_termtype=te
107
# A command to stop / start asterisk. Must support parameters "start"
108
# and "stop" . This is the executable:
109
ZAPCONF_ASTERISK_SCRIPT=/etc/init.d/asterisk
111
# Should you need to pass extra arguments:
112
ZAPCONF_ASTERISK_CMD=$ZAPCONF_ASTERISK_SCRIPT
114
# read default configuration from /etc/default/zaptel
115
if [ -r $ZAPTEL_BOOT_DEBIAN ]; then . $ZAPTEL_BOOT_DEBIAN; fi
116
if [ -r $ZAPTEL_BOOT_FEDORA ]; then . $ZAPTEL_BOOT_FEDORA; fi
118
if [ ! -x "$ZTCFG" ]; then
119
# Work around a bug in the rpm package: ztcfg should be in
120
# /sbin as it may be required for starting a network interface
121
if [ -x /usr/sbin/ztcfg ]; then
122
ZTCFG=/usr/sbin/ztcfg
124
echo >&2 "ztcfg is not on found, do you have zaptel properly installed?"
129
XPP_SYNC=auto # sync mode. can be set to '0' or '1' or HOST explicitly.
131
# it is safe to use -c twice: the last one will be used.
132
ztcfg_cmd="$ZTCFG -c $ZAPCONF_FILE"
134
# work around a bug (that was already fixed) in the installer:
135
if [ "$lc_country" = '' ]; then lc_country=us; fi
146
span_te_timing_counter=1
148
case "$ZAPBRI_SIGNALLING" in
149
bri) ZAPBRI_NET=bri_net; ZAPBRI_CPE=bri_cpe ;;
150
pri) ZAPBRI_NET=pri_net; ZAPBRI_CPE=pri_cpe ;;
151
bri_ptmp) ZAPBRI_NET=bri_net_ptmp; ZAPBRI_CPE=bri_cpe_ptmp ;;
153
die "Incorrect value for ZAPBRI_SIGNALLING ($ZAPBRI_SIGNALLING). Abortring"
163
if [ "$verbose" = no ]; then
169
# exit (exit value is the optional $1), and clean up after us
171
if [ -d "$tmp_dir" ]; then
172
# don't fail but don't hide error if directory is not
174
rmdir "$tmp_dir" || :
179
# Wait for udev to generate /dev/zap/ctl, if needed:
181
# if device file already exists, or if zaptel has failed to load:
183
if [ -c /dev/zap/ctl ] || ! grep -q zaptel /proc/modules ; then
186
say "Waiting for /dev/zap/ctl to be generated"
188
for i in `seq $DEVZAP_TIMEOUT`; do
190
if [ -c /dev/zap/ctl ]; then
195
if [ "$devzap_found" != 1 ]; then
196
say "Still no /dev/zap/ctl after $devzap_timeout seconds."
197
echo >&2 "No /dev/zap/ctl: cannot run ztcfg. Aborting."
203
if [ "$verbose" = no ]; then
206
say "Reconfiguring identified channels"
211
update_module_list_debian() {
212
say "Updating Debian modules list $MODLIST_FILE."
213
del_args=`for i in $ALL_MODULES ztdummy
215
echo "$i" | sed s:.\*:-e\ '/^&/d':
217
add_args=`for i in $*
219
echo "$i" | sed s:.\*:-e\ '\$a&':
222
sed -i.bak $del_args "$MODLIST_FILE"
226
done >> "$MODLIST_FILE"
229
update_module_list_fedora() {
230
say "Updating modules list in zaptel init config $MODLIST_FILE_FEDORA."
231
sed -i.bak -e "/^$modules_var=/d" "$MODLIST_FILE_FEDORA"
232
echo "$modules_var=\"$*\"" >> "$MODLIST_FILE_FEDORA"
235
update_module_list() {
236
if [ -f "$MODLIST_FILE" ]; then
237
update_module_list_debian "$@"
238
elif [ -f "$MODLIST_FILE_FEDORA" ]; then
239
update_module_list_fedora "$@"
241
die "Can't find a modules list to update. Tried: $MODLIST_FILE, $MODLIST_FILE_FEDORA. Aborting"
248
if [ ! -d /proc/xpp ]; then return; fi
250
# Get a list of connected Astribank devices, sorted by the name of
251
# the USB connector. That order is rather arbitrary, but will not
252
# change without changes to the cabling.
253
xbusses=`sed -e '/STATUS=connected/!d' -e 's/ *STATUS=.*//' -e 's/ *CONNECTOR=//' /proc/xpp/xbuses | sort -t: -k 2 | cut -d: -f1`
254
say "Zaptel registration order:"
257
# get a list of XPDs that were not yet registered as zaptel spans.
258
# this will be the case if you set the parameter zap_autoreg=0 to
260
# Append /dev/null to provide a valid file name in case of an empty pattern.
261
xbusses_pattern=`echo $xbusses| sed -e 's|XBUS-[0-9]*|/proc/xpp/&/XPD-*/zt_registration|g'`' /dev/null'
262
xpds_to_register=`grep -l 0 $xbusses_pattern`
263
for file in $xpds_to_register; do
268
# Initialize the Xorcom Astribank (xpp/)
270
# do nothing if the module xpp was not loaded, or if no
271
# Astribanks connected:
272
if [ ! -d /proc/xpp ]; then return 0; fi
273
if ! grep -q 'STATUS=connected' /proc/xpp/xbuses; then return 0; fi
275
echo "Waiting for Astribank devices to initialize:"
276
cat /proc/xpp/XBUS-[0-9]*/waitfor_xpds 2>/dev/null || true
278
# overriding locales for the above two, as perl can be noisy
279
# when locales are missing.
280
# No register all the devices if they didn't auto-register:
281
LC_ALL=C $zt_registration on
283
# this one could actually be run after ztcfg:
284
LC_ALL=C $xpp_sync "$XPP_SYNC"
290
program=`basename $0`
292
echo >&2 "$program: generate zaptel.conf and zapata.conf"
293
echo >&2 "(version $VERSION, $rcsid)"
295
echo >&2 " $program [-sRdv] [-m k|l|g] [-c <country_code>] [-e <base_exten>] [-F]"
296
echo >&2 " $program [-sRdv] -l"
297
echo >&2 " $program -su"
298
echo >&2 " $program -h (this screen)"
301
echo >&2 " -c CODE: set the country code (default: $lc_country)"
302
echo >&2 " -e NUM: set the base extension number (default: $base_exten)"
303
echo >&2 " -F: Don't print FXSs in zapata.conf"
304
echo >&2 " -l: output a list of detected channels instead of zaptel.conf"
305
echo >&2 " -d: Perform hardware detection"
306
echo >&2 " -u: Unload zaptel modules (will not restart Asterisk)."
307
echo >&2 " -v: verbose"
308
echo >&2 " -s: Stop Asterisk before running, and start it at the end."
309
echo >&2 " -R: Don't restart asterisk in the end."
310
echo >&2 " -z: also generate zapscan.conf for the asterisk GUI."
313
# print /etc/asterisk/zapscan.conf for the Asterisk-GUI
314
# $1: port type. Currently only fxs/fxo . Note that this is the type, and
315
# not the signalling (which would be the fxo fir an fxs port.
316
# $2: port number. Probably a range of ports is also allowed.
317
print_zapscan_port () {
318
if [ "$do_gen_zapscan" != 'yes' ]; then return 0; fi
328
local astbank_type=''
329
local reset_values=""
331
while getopts 'a:' arg
334
a) case "$OPTARG" in input|output) astbank_type=$OPTARG;;esac ;;
337
shift $(( $OPTIND-1 ))
341
local sig=$2 #fxs/fxo
345
if [ "$sig" = 'fxs' ]; then
346
# Coutries in which we need to use busydetect:
347
# United Arab Emirats, Israel, Slovenia
348
case "$lc_country" in
357
method="$fxs_default_start"
365
echo $chan $sig_name $astbank_type;;
367
# sadly, both input ports and output ports go into the same span as
368
# the FXS ports. Thus we need to separate between them. See also
369
# the zapata.conf section:
371
echo ";;; line=\"$line\"" >> $zapata_file
373
if [ "$astbank_type" != '' ];
374
then echo "# astbanktype: $astbank_type" >>$zaptel_file;
376
echo "${sig}$method=$chan" >>$zaptel_file
377
# zap2amp will rewrite those from zaptel.conf and hints there
378
if [ "$fxsdisable" = 'yes' ] && [ "$sig" = 'fxo' ]; then return; fi
380
echo "signalling=${sig}_$method" >>$zapata_file
381
if [ "$sig" = 'fxo' ]
383
# to preconfigure channel 1's extension to 550, set
385
# in, e.g, /etc/default/zaptel
386
var_name=`echo chan_${chan}_exten`
387
cfg_exten=`echo ${!var_name} | tr -d -c 0-9`
388
var_name=`echo chan_${chan}_vmbox`
389
cfg_vmbox=`echo ${!var_name} | tr -d -c 0-9`
390
var_name=`echo chan_${chan}_cntxt`
391
cfg_cntxt=`echo ${!var_name} | tr -d -c 0-9`
393
if [ "$cfg_exten" = '' ]
394
then # No extension number set for this channel
395
exten=$(($chan+$base_exten))
396
else # use the pre-configured extension number
399
# is there any real need to set 'mailbox=' ?
400
if [ "x$cfg_vmbox" = x ]
401
then # No extension number set for this channel
403
else # use the pre-configured extension number
406
echo "callerid=\"Channel $chan\" <$exten>" >> $zapata_file
407
reset_values="$reset_values callerid"
408
echo "mailbox=$exten" >> $zapata_file
409
reset_values="$reset_values mailbox"
410
if [ "$group_manual" != "yes" ]
412
echo "group=$group_phones" >> $zapata_file
413
reset_values="$reset_values group"
415
if [ "$context_manual" != "yes" ]
417
if [ "$astbank_type" != '' ];
419
context_var_name=context_$astbank_type
420
echo context=${!context_var_name} >> $zapata_file
422
echo "context=$context_phones" >> $zapata_file
424
reset_values="$reset_values context"
426
else # this is an FXO (trunk/phone: FXO signalling)
427
# we have may have set it. So reset it:
428
echo "callerid=asreceived" >> $zapata_file
429
if [ "$group_manual" != "yes" ]
431
echo "group=$group_lines" >> $zapata_file
433
if [ "$context_manual" != "yes" ]
435
echo "context=$context_lines" >> $zapata_file
436
reset_values="$reset_values context"
438
if [ "$lc_country" = 'uk' ]
440
echo "cidsignalling=v23" >> $zapata_file
442
*WCFXO*) echo "cidstart=history" >> $zapata_file;;
443
*) echo "cidstart=polarity" >> $zapata_file;;
445
reset_values="$reset_values cidsignalling cidstart"
449
if [ "$xpp_fxo_rxgain" != '' ]; then
450
echo "rxgain=$xpp_fxo_rxgain" >> $zapata_file
451
reset_values="$reset_values rxgain"
455
# if kewlstart is not used, busydetect has to be employed:
456
if [ "$method" = 'ls' ]
458
echo 'busydetect=yes' >> $zapata_file
459
reset_values="$reset_values busydetect"
463
if [ "$astbank_type" = 'input' ] || \
464
( [ "$fxs_immediate" = 'yes' ] && [ "$sig" = "fxo" ] )
466
echo 'immediate=yes' >> $zapata_file
467
reset_values="$reset_values immediate"
469
echo "channel => $chan" >> $zapata_file
470
reset_zapata_entry $zapata_file $reset_values
471
echo "" >> $zapata_file
473
print_zapscan_port "$sig" "$chan"
479
# the number of channels from /proc/zaptel
480
# must always print a number as its output.
481
count_proc_zap_lines() {
482
# if zaptel is not loaded there are 0 channels:
483
if [ ! -d /proc/zaptel ]; then echo '0'; return; fi
486
for file in `echo /proc/zaptel/* |grep -v '\*'`
487
do sed -e 1,2d $file # remove the two header lines
489
) | wc -l # the total number of lines
493
say "Test Loading modules:"
494
for i in $ALL_MODULES
496
lines_before=`count_proc_zap_lines`
499
# a module is worth listing if it:
500
# a. loaded successfully, and
501
# b. added channels lines under /proc/zaptel/*
502
if /sbin/modprobe $i $args 2> /dev/null
506
xpp_usb) check=`grep 'STATUS=connected' 2>/dev/null /proc/xpp/xbuses | wc -l` ;;
507
*) if [ $lines_before -lt `count_proc_zap_lines` ]; then check=1; fi ;;
511
probed_modules="$probed_modules $i"
523
# recursively unload a module and its dependencies, if possible.
524
# where's modprobe -r when you need it?
525
# inputs: module to unload.
526
# returns: the result from
529
line=`lsmod 2>/dev/null | grep "^$1 "`
530
if [ "$line" = '' ]; then return; fi # module was not loaded
533
# $1: the original module, $2: size, $3: refcount, $4: deps list
534
mods=`echo $4 | tr , ' '`
535
# xpp_usb keeps the xpds below busy, and hence must be removed
537
case "$module" in xpd_*) mods="xpp_usb $mods";; esac
539
# run in a subshell, so it won't step over our vars:
541
# TODO: the following is probably the error handling we want:
542
# if [ $? != 0 ]; then return 1; fi
548
say "Unloading zaptel modules:"
553
# sleep a while until the xpp modules fully register
557
# wait for the XPDs to register:
558
# TODO: improve error reporting and produce a messagee here
559
cat /proc/xpp/XBUS-*/waitfor_xpds 2>/dev/null >/dev/null || true
565
temporary_zapconf save
567
temporary_zapconf restore
568
modlist="$probed_modules"
569
modlist="$(echo $modlist)" # clean spaces
570
if [ "$do_module_list" = yes ]
572
update_module_list "$modlist"
574
if echo $modlist | grep -q xpp_usb; then wait_for_xpp; fi
577
# The module configuration generated by zaptel includes a totally useless
578
# automatic run of ztcfg after modules loading. As a workaround for that,
579
# we provide an empty zaptel.conf temporarily.
581
# At hardware detection time we shouldn't really touch zaptel.conf . So we
582
# must keep a copy of it somewhere.
584
# I use ZAPCONF_FILE_SYSTEM rather than ZAPCONF_FILE, as the bogus modprobe
585
# entries will not be initiated by us.
587
# ZAPCONF_FILE_TMP is a "global", as it needs to be preserved between the
588
# call to 'save' and to 'restore'
590
temporary_zapconf() {
593
say "Temporarily moving zaptel.conf aside to work around broken modprobe.conf"
594
ZAPCONF_FILE_TMP=`mktemp /tmp/genzaptelconf-zaptel.conf-XXXXXX` || die "Error creating temporary zaptel.conf"
595
if [ -f $ZAPCONF_FILE_SYSTEM ]; then
596
cp -a $ZAPCONF_FILE_SYSTEM $ZAPCONF_FILE_TMP
598
echo -n >$ZAPCONF_FILE_SYSTEM
601
# restore original zaptel.conf:
602
if [ "$ZAPCONF_FILE_TMP" = '' ]; then return; fi
603
mv $ZAPCONF_FILE_TMP $ZAPCONF_FILE_SYSTEM
609
# run after the channel's entry. Used to reset all the values
610
reset_zapata_entry() {
611
conf_file="$1"; shift
615
busydetect) echo "$arg=no" >>$conf_file;;
616
context) echo "$arg=default" >>$conf_file;;
617
immediate) echo "$arg=no" >>$conf_file;;
618
overlapdial) echo "$arg=no" >>$conf_file;;
619
rxgain) echo "$arg=0" >>$conf_file;;
620
txgain) echo "$arg=0" >>$conf_file;;
621
*) echo "$arg=" >>$conf_file;;
627
# we need to preserve the permissions of existing configurations files.
628
# However we also don't want to be left with incomplete configurations in
629
# case we are stopped in the middle. Thus we create a temporary file and
630
# mv it to the original file only when done.
632
# This function gives the temporary file the permissions of the original,
633
# or some sane defaults.
635
# The temporary file is generated here, and ths its name is passed through
636
# a variable whose name is passed as a parameter (new_var).
644
# assign to new_var by reference:
645
eval $new_var=`mktemp /tmp/genzaptelconf-conf-XXXXXX` \
646
|| die "Unable to create temporary config file for $orig_cfg_file"
647
if [ -r "$orig_cfg_file" ]; then
648
chown --reference="$orig_cfg_file" ${!new_var} 2>/dev/null
649
chmod --reference="$orig_cfg_file" ${!new_var} 2>/dev/null
651
chmod 644 ${!new_var}
655
# Extract information from one digital channel (one line in a /proc/zaptel
656
# file). Information is saved to $tmp_dir/span_foo variables.
657
# FIXME: detection should move to when we know the number of channels
658
# and hence can tell between an E1 and a T1.
659
detect_digital_channel() {
665
# Rather than identifying cards by the header line, we identify
666
# them by the channel names. This is shorter.
667
# This also allows us to count the channel numbers and check if
668
# we have a E1 span, a T1 span or a BRI span.
670
# span_lastd is always the one before last
671
# channel. span_bchan is the last:
672
echo $chan_num >$tmp_dir/span_end
674
if [ "`cat $tmp_dir/span_begin`" != "-1" ]
676
return #we already configured this span.
679
# Now we need to give initial configuration to the span
680
echo $chan_num >$tmp_dir/span_begin
681
echo $span_num >$tmp_dir/span_num
684
*ZTHFC*/* | *ztqoz*/* | *XPP_BRI_*/*)
686
echo 'ccs' >$tmp_dir/span_framing
687
echo 'euroisdn' >$tmp_dir/span_switchtype
688
if [ "`cat $tmp_dir/span_termtype`" = 'nt' 2>/dev/null ]
690
echo $ZAPBRI_NET >$tmp_dir/span_signalling
692
echo $ZAPBRI_CPE >$tmp_dir/span_signalling
696
# Junghanns's GSM cards.
697
echo 'ccs' >$tmp_dir/span_framing
698
#Does this mean anything?
699
echo 'gsm' >$tmp_dir/span_signalling
701
*TE[24]/* | *WCT1/* | *Tor2/* | *TorISA/* | *WP[TE]1/* | \
702
*R[124]T1/* | *XPP_[TEJ]1_*)
703
# FIXME: handle cwain around here.
704
# name: *cwain[12]/* . Always E1.
707
echo 'esf' >$tmp_dir/span_framing
708
echo 'b8zs' >$tmp_dir/span_coding
709
echo 'national' >$tmp_dir/span_switchtype
710
if [ "`cat $tmp_dir/span_termtype`" = 'nt' 2>/dev/null ]
712
echo pri_net >$tmp_dir/span_signalling
714
echo pri_cpe >$tmp_dir/span_signalling
716
# an example of country-specific setup. This is probably not accurate
717
# Contributions are welcome
718
case "$lc_country" in
720
# (Just an example for per-country info)
721
echo 'ccs' >$tmp_dir/span_framing
722
echo 'ami' >$tmp_dir/span_coding
723
#echo 'crc4' >$tmp_dir/span_yellow
724
#echo 'euroisdn' >$tmp_dir/span_switchtype
725
#echo 'pri_cpe' >$tmp_dir/span_signalling
728
echo 'ccs' >$tmp_dir/span_framing
729
echo 'hdb3' >$tmp_dir/span_coding
730
echo 'crc4' >$tmp_dir/span_yellow
731
echo 'euroisdn' >$tmp_dir/span_switchtype
734
echo 'ccs' >$tmp_dir/span_framing
735
echo 'hdb3' >$tmp_dir/span_coding
736
#echo 'crc4' >$tmp_dir/span_yellow
737
echo 'national' >$tmp_dir/span_switchtype
744
# read information from the $tmp_dir/span_foo files and generate
745
# configuration for the span and its channels.
746
write_digital_config() {
747
# if the current file we checked was not of a digital span, we have
749
if [ "`cat $tmp_dir/span_begin`" = -1 ]; then return; fi
751
# read files to variables:
752
for suffix in num begin end timing lbo framing \
753
coding switchtype signalling yellow termtype
755
eval span_$suffix=`cat $tmp_dir/span_$suffix 2>/dev/null`
758
# exactly the same logic is used in asterisk's chan_zap.c.
759
# also not that $(( )) is bash-specific
760
case "$((1+ $span_end - $span_begin))" in
761
2|3|24) #ztgsm, BRI or T1
763
bchans="$span_begin-$(($span_end-1))"
766
dchan="$(($span_begin+15))"
767
bchans="$span_begin-$(($span_begin+14)),$(($span_begin+16))-$span_end"
768
if [ "$span_framing" = 'esf' ]; then
769
# don't leave an E1 span with defective defaults:
772
span_switchtype=euroisdn
777
if [ "$span_yellow" != '' ]; then span_yellow=",$span_yellow"; fi
778
# Let's assume that a TE span should get the clock from the remote unit,
779
# and NT spans should provide timing. Just as a sane default.
780
# If we have several TE spans, the first will have priority 1,
782
case "$span_signalling" in *_cpe*)
783
span_timing=$span_te_timing_counter
784
span_te_timing_counter=$(($span_te_timing_counter + 1))
789
echo span=$span_num,$span_timing,$span_lbo,$span_framing,$span_coding$span_yellow >> $zaptel_file
790
# leave a comment in zaptel.conf that allows to tell if
791
# this span is TE or NT:
792
if [ "$span_termtype" != '' ]
793
then echo "# termtype: $span_termtype" >>$zaptel_file
796
echo bchan=$bchans >>$zaptel_file
797
echo dchan=$dchan >>$zaptel_file
798
if [ "$fxsdisable" = 'yes' ] && [ "$span_termtype" = 'nt' ]; then return; fi
799
# You should not send content to zapata.conf below this line
801
span_group=$(($SPAN_GROUP_BASE + $span_num))
803
if [ "$span_termtype" != '' ]
805
# an ISDN card's span that we know if it is in NT mode or TE mode.
806
# NT is the same as FXS for us and TE is the same as FXO
807
if [ "$span_termtype" = 'nt' ]
809
echo "callerid=\"Channels $span_begin - $span_end\" <$span_begin>" >> $zapata_file
810
reset_values="$reset_values callerid"
811
#echo "mailbox=$exten"
812
if [ "$brint_overlap" = 'yes' ]
814
echo "overlapdial=yes" >> $zapata_file
815
reset_values="$reset_values overlapdial"
817
if [ "$group_manual" != "yes" ]
819
echo "group=$group_phones,$span_group" >> $zapata_file
820
reset_values="$reset_values group"
822
if [ "$context_manual" != "yes" ]
824
echo "context=$context_phones" >> $zapata_file
828
if [ "$group_manual" != "yes" ]
830
echo "group=$group_lines,$span_group" >> $zapata_file
831
reset_values="$reset_values group"
833
if [ "$context_manual" != "yes" ]
835
echo "context=$context_lines" >> $zapata_file
836
reset_values="$reset_values context"
840
echo "switchtype = $span_switchtype" >> $zapata_file
841
echo "signalling = $span_signalling" >> $zapata_file
842
echo "channel => $bchans" >> $zapata_file
843
reset_zapata_entry $zapata_file $reset_values
847
echo "### BRI/PRI: $span_termtype"
849
echo "$dchan Control"
850
#echo BRI/PRI: chans: $bchans, control: $dchan
855
# This is where the actual detection configuration detection work happens.
858
local reset_values=""
860
# spanlist=`echo /proc/zaptel/* | grep -v '\*'`
861
# spanlist=$(for i in `for i in /proc/zaptel/*; do if [ -f $i ]; then echo $i | cut -f 4 -d / ; fi; done | sort -n`; do echo -n "/proc/zaptel/$i "; done)
862
# spanlist=(cd /proc/zaptel; ls | sort -n | sed 's|^|/proc/zaptel/|')
863
spanlist=`ls /proc/zaptel/ 2>/dev/null | sort -n | sed 's|^|/proc/zaptel/|'`
865
#if [ "$spanlist" == "" ]; then
866
# die "No zapata interfaces in /proc/zaptel"
872
if [ "$do_gen_zapscan" = 'yes' ]; then
873
gen_tmp_conf "$ZAPSCAN_FILE" zapscan_file
874
cat <<EOF >$zapscan_file
875
; zapscan.conf: information about detected zaptel channels
876
; (currently: analog only)
878
; Automatically generated by $0 -- Please do not edit.
882
gen_tmp_conf "$ZAPTEL_FILE" zaptel_file
883
gen_tmp_conf "$ZAPATA_FILE" zapata_file
884
cat <<EOF >$zaptel_file
885
# Autogenerated by $0 -- do not hand edit
886
# Zaptel Configuration File
888
# This file is parsed by the Zaptel Configurator, ztcfg
891
# It must be in the module loading order
894
cat <<EOF >$zapata_file
895
; Autogenerated by $0 -- do not hand edit
896
; Zaptel Channels Configurations (zapata.conf)
898
; This is not intended to be a complete zapata.conf. Rather, it is intended
899
; to be #include-d by /etc/zapata.conf that will include the global settings
905
# For each line in the spanlist: see if it represents a channel.
906
# if it does, test that the channel is usable.
907
# we do that by configuring it (using ztcfg with a 1-line config file)
908
# and then trying to read 1 byte from the device file.
910
# The '<(command)' syntax creates a temporary file whose content is is the
911
# output of 'command'.
913
# Another problem with such an approach is how to include an existing
914
# configuration file. For instance: how to include some default settings.
916
# Maybe an 'include' directive should be added to zaptel.conf ?
918
for procfile in $spanlist
920
span_num=`basename $procfile`
921
# the first line is the title line. It states the model name
922
# the second line is empty
923
title=`head -n 1 $procfile`
924
# stuff that needs to be remembered accross lines (for PRI/BRI support)
926
list) echo "### $title";;
928
echo "" >>$zaptel_file
929
echo "# $title" >>$zaptel_file
930
echo "" >>$zapata_file
931
echo "; $title" >>$zapata_file
934
echo '-1' >$tmp_dir/span_begin
935
echo '-1' >$tmp_dir/span_end
936
echo '0' >$tmp_dir/span_timing
937
echo '0' >$tmp_dir/span_lbo
938
echo '' >$tmp_dir/span_framing
939
echo 'ami' >$tmp_dir/span_coding
940
echo '' >$tmp_dir/span_switchtype
941
echo '' >$tmp_dir/span_signalling
942
if [ "$zapconf_def_termtype" != '' ]
944
echo "$zapconf_def_termtype" >$tmp_dir/span_termtype
947
# Check if ZapBRI cards are in TE or NT mode
948
if echo $title | egrep -q '((quad|octo)BRI PCI ISDN Card.* \[NT\]\ |octoBRI \[NT\] |HFC-S PCI A ISDN.* \[NT\] |Xorcom .* (BRI|T1|E1)_NT)'
950
echo 'nt' >$tmp_dir/span_termtype
951
elif echo $title | egrep -q '((quad|octo)BRI PCI ISDN Card.* \[TE\]\ |octoBRI \[TE\] |HFC-S PCI A ISDN.* \[TE\] |Xorcom .* (BRI|T1|E1)_TE)'
953
echo 'te' >$tmp_dir/span_termtype
956
# The rest of the lines are per-channel lines
957
sed -e 1,2d $procfile | \
960
# in case this is a real channel.
961
chan_num=`echo $line |awk '{print $1}'`
963
*WCTDM/* | *\ WRTDM/* | *OPVXA1200/*)
964
# TDM400P/2400P and similar cards (Sangoma A200, OpenVox A1200)
965
# this can be either FXS or FXO
968
$ztcfg_cmd -c <(echo fxoks=$chan_num) &>/dev/null && maybe_fxs=1
969
$ztcfg_cmd -c <(echo fxsks=$chan_num) &>/dev/null && maybe_fxo=1
970
if [ $maybe_fxs = 1 ] && [ $maybe_fxo = 1 ]
972
# An installed module won't accept both FXS and FXO signalling types:
973
# this is an empty slot.
974
# TODO: I believe that the Sangoma A20x will reject both and thus
975
# print nothing here.
977
list) echo "# channel $chan_num, WCTDM, no module.";;
978
files) echo "# channel $chan_num, WCTDM, no module." >> $zaptel_file;;
983
if [ $maybe_fxs = 1 ]; then print_pattern $chan_num fxo $mode; fi
984
if [ $maybe_fxo = 1 ]; then print_pattern $chan_num fxs $mode; fi
988
print_pattern $chan_num fxs $mode || \
989
echo "# channel $chan_num, WCFXO, inactive." >>$zaptel_file
992
print_pattern $chan_num fxo $mode
996
print_pattern $chan_num fxs $mode
999
# Astribank FXS span (regular port)
1000
print_pattern $chan_num fxo $mode
1003
# FXO module (probably Rhino)
1004
print_pattern $chan_num fxs $mode
1007
# FXS module (probably Rhino)
1008
print_pattern $chan_num fxo $mode
1011
# no module (probably Rhino)
1015
# Astribank FXS span (output port)
1016
print_pattern -a output $chan_num fxo $mode
1019
# Astribank FXS span (input port)
1020
print_pattern -a input $chan_num fxo $mode
1022
*ZTHFC*/* | *ztqoz*/* |*ztgsm/* |*TE[24]/* | \
1023
*WCT1/*|*Tor2/* | *TorISA/* | \
1024
*XPP_BRI_*/* | *WP[TE]1/* | *R[124]T1/* | \
1026
detect_digital_channel "$line" "$chan_num" "$span_num"
1028
'') ;; # Empty line (after span header)
1031
list) echo "# ??: $line";;
1033
echo "# ??: $line" >>$zaptel_file
1034
echo "; ??: $line" >>$zapata_file
1039
# end of part in sub-process.
1041
write_digital_config
1044
if [ "$mode" = 'files' ]
1046
cat <<EOF >> ${zaptel_file}
1050
loadzone = $loadzone
1051
defaultzone = $defaultzone
1055
if [ "$mode" = 'files' ]; then
1056
mv ${ZAPCONF_FILE} ${ZAPCONF_FILE}.bak 2>/dev/null
1057
mv $zaptel_file ${ZAPCONF_FILE}
1058
mv ${ZAPATA_FILE} ${ZAPATA_FILE}.bak 2>/dev/nullk
1059
mv $zapata_file ${ZAPATA_FILE}
1060
if [ "$do_gen_zapscan" = 'yes' ]; then
1061
mv $ZAPSCAN_FILE $ZAPSCAN_FILE.bak 2>/dev/null
1062
mv $zapscan_file $ZAPSCAN_FILE
1065
zapata_file_name=`basename $ZAPATA_FILE`
1066
if ! grep -q "^#include.*$zapata_file_name" \
1067
/etc/asterisk/zapata.conf
1069
say "Note: generated $ZAPATA_FILE not included in zapata.conf"
1070
say "To fix: echo '#include $zapata_file_name' >>/etc/asterisk/zapata.conf"
1075
while getopts 'c:de:Fhlm:MRsuvz' arg
1078
e) # guarantee that it is a number:
1079
new_base_exten=`echo $OPTARG | tr -d -c 0-9`
1080
if [ "x$new_base_exten" != x ]; then base_exten=$new_base_exten; fi
1082
c) lc_country=`echo $OPTARG | tr -d -c a-z` ;;
1085
u) do_unload=yes ; force_stop_ast=yes ;;
1088
M) do_module_list=yes; do_detect=yes ;;
1089
s) force_stop_ast=yes ;;
1091
z) do_gen_zapscan=yes ;;
1093
*) echo >&2 "unknown parameter -$arg, Aborting"; usage; exit 1;;
1096
shift $(( $OPTIND-1 ))
1097
if [ $# != 0 ]; then
1098
echo >&2 "$0: too many parameters"
1103
tmp_dir=`mktemp -d /tmp/genzaptelconf-dir-XXXXXX` || \
1104
die "$0: failed to create temporary directory. Aborting"
1107
case "$lc_country" in
1108
# the list was generated from the source of zaptel:
1109
#grep '{.*[0-9]\+,.*"[a-z][a-z]"' zonedata.c | cut -d'"' -f 2 | xargs |tr ' ' '|'
1110
us|au|fr|nl|uk|fi|es|jp|no|at|nz|it|gr|tw|cl|se|be|sg|il|br|hu|lt|pl|za|pt|ee|mx|in|de|ch|dk|cz|cn):;;
1113
echo >&2 "unknown country-code $lc_country, defaulting to \"us\""
1116
# any reason for loadzone and defaultzone to be different? If so, this is
1117
# the place to make that difference
1118
loadzone=$lc_country
1119
defaultzone=$loadzone
1121
# make sure asterisk is not in our way
1122
if [ "$force_stop_ast" = 'yes' ]
1124
$ZAPCONF_ASTERISK_CMD stop 1>&2
1126
# if asterisk is running and we wanted to detect modules
1127
# or simply to unload modules, asterisk needs to go away.
1128
if ( [ "$do_unload" = yes ] || [ "$do_detect" = yes ] ) && \
1129
pidof asterisk >/dev/null
1131
echo >&2 "Asterisk is already running. Configuration left untouched"
1132
echo >&2 "You can use the option -s to shut down Asterisk for the"
1133
echo >&2 "duration of the detection."
1138
if [ "$do_unload" = yes ]
1144
if [ "$do_detect" = yes ]
1149
if [ "$zapconf_use_perl" = yes ]; then
1150
#redefine genconf to use perl utilities:
1153
list) zaptel_hardware ;;
1159
if [ "$mode" = list ]; then
1165
say "Generating '${ZAPCONF_FILE} and ${ZAPATA_FILE}'"
1170
if [ "$tmp_dir" != '' ]
1175
if [ "$force_stop_ast" != 'yes' ] || [ "$do_restart" != 'yes' ]
1180
if [ -x "$ZAPCONF_ASTERISK_SCRIPT" ]
1182
$ZAPCONF_ASTERISK_CMD start 1>&2
1185
# if in verbose mode: verify that asterisk is running
1186
if [ "$verbose" != 'no' ]
1188
say "Checking channels configured in Asterisk:"
1189
sleep 1 # give it some time. This is enough on our simple test server
1192
ast-cmd cmd "zap show channels"
1194
asterisk -rx "zap show channels"