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

« back to all changes in this revision

Viewing changes to kernel/xpp/utils/genzaptelconf

  • Committer: Bazaar Package Importer
  • Author(s): Tzafrir Cohen
  • Date: 2008-08-28 22:58:23 UTC
  • mfrom: (11.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080828225823-r8bdunirm8hmc76m
Tags: 1:1.4.11~dfsg-2
* Patch xpp_fxs_power: Fixed an issue with hook detection of the Astribank
  FXS module.
* Don't fail init.d script if fxotune fails. This may happen if running it
  when Asterisk is already running.
* Bump standards version to 3.8.0.0 .
* Ignore false lintian warning ("m-a a-i" has "a a").
* Patch xpp_fxo_cid_always: do always pass PCM if that's what the user
  asked.
* Patch vzaphfc_proc_root_dir: fix vzaphfc on 2.6.26.
* Patch wcte12xp_flags: Proper time for irq save flags.
* Patch headers_2627: Fix location of semaphore.h for 2.6.27 .
* Patch xpp_fxs_dtmf_leak: Don't play DTMFs to the wrong channel.
* Patch wctdm_fix_alarm: Fix sending channel alarms.
* Patch device_class_2626: Fix building 2.6.26 (Closes: #493397).
* Using dh_lintian for lintian overrides, hence requiring debhelper 6.0.7.
* Lintian: we know we have direct changes. Too bad we're half-upstream :-(
* Fix doc-base section names. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/bash
 
2
 
 
3
# genzaptelconf: generate as smartly as you can:
 
4
#               /etc/zaptel.conf
 
5
#               /etc/asterisk/zapata-channels.conf (to be #include-d into zapata.conf)
 
6
#       update:
 
7
#               With '-M' /etc/modules (list of modules to load)
 
8
#
 
9
# Copyright (C) 2005 by Xorcom <support@xorcom.com>
 
10
#
 
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.
 
15
#
 
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.
 
20
#
 
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
 
24
#
 
25
#
 
26
# If you have any technical questions, contact 
 
27
# Tzafrir Cohen <tzafrir.cohen@xorcom.com>
 
28
#       
 
29
 
 
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 
 
34
# (redhat/centos)
 
35
 
 
36
# /etc/default/zaptel may override the following variables
 
37
VERSION=0.5.10
 
38
rcsid='$Id: genzaptelconf 4266 2008-05-13 21:08:09Z tzafrir $'
 
39
lc_country=us
 
40
# set to: ls, ks or gs for (Loopstart, Kewlstart and GroundStart)
 
41
#         on FXS channels (FXO signalling).
 
42
fxs_default_start=ks
 
43
base_exten=6000
 
44
# If set: no context changes are made in zapata-channels.conf
 
45
#context_manual=yes
 
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
 
53
#group_manual=yes
 
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.
 
57
fxs_immediate=no
 
58
 
 
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
 
69
# perl utilities:
 
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
 
77
SPAN_GROUP_BASE=10
 
78
# set to "yes" to make BRI NT spans set overlapdial (handy for ISDN phones
 
79
# and other devices).
 
80
brint_overlap=no
 
81
 
 
82
# a temporary directory to store whatever we need to remember.
 
83
#
 
84
# The main loop of genconf is run in a sub-process.
 
85
tmp_dir=
 
86
 
 
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"
 
91
 
 
92
# The name of the variable in /etc/sysconfig/zaptel into which to set 
 
93
# the list of detected modules.
 
94
modules_var=MODULES
 
95
# On SuSE with the rpm package:
 
96
#modules_var=ZAPTEL_MODULES
 
97
 
 
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
 
106
 
 
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
 
110
#
 
111
# Should you need to pass extra arguments:
 
112
ZAPCONF_ASTERISK_CMD=$ZAPCONF_ASTERISK_SCRIPT
 
113
 
 
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
 
117
 
 
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
 
123
        else
 
124
                echo >&2 "ztcfg is not on found, do you have zaptel properly installed?"
 
125
                exit_cleanup 1
 
126
        fi
 
127
fi
 
128
 
 
129
XPP_SYNC=auto # sync mode. can be set to '0' or '1' or HOST explicitly.
 
130
 
 
131
# it is safe to use -c twice: the last one will be used.
 
132
ztcfg_cmd="$ZTCFG -c $ZAPCONF_FILE"
 
133
 
 
134
# work around a bug (that was already fixed) in the installer:
 
135
if [ "$lc_country" = '' ]; then lc_country=us; fi
 
136
 
 
137
force_stop_ast=no
 
138
do_detect=no
 
139
do_unload=no
 
140
do_module_list=no
 
141
verbose=no
 
142
do_restart=yes
 
143
fxsdisable=no
 
144
do_gen_zapscan=no
 
145
 
 
146
span_te_timing_counter=1
 
147
 
 
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 ;;
 
152
*) 
 
153
        die "Incorrect value for ZAPBRI_SIGNALLING ($ZAPBRI_SIGNALLING). Abortring"
 
154
        ;;
 
155
esac
 
156
 
 
157
die() {
 
158
        echo "$@" >&2
 
159
        exit_cleanup 1
 
160
}
 
161
 
 
162
say() {
 
163
        if [ "$verbose" = no ]; then
 
164
                return
 
165
        fi
 
166
        echo "$@"   >&2
 
167
}
 
168
 
 
169
# exit (exit value is the optional $1), and clean up after us
 
170
exit_cleanup() {
 
171
        if [ -d "$tmp_dir" ]; then
 
172
                # don't fail but don't hide error if directory is not
 
173
                # empty
 
174
                rmdir "$tmp_dir" || : 
 
175
        fi
 
176
        exit $1
 
177
}
 
178
 
 
179
# Wait for udev to generate /dev/zap/ctl, if needed:
 
180
wait_for_zapctl() {
 
181
        # if device file already exists, or if zaptel has failed to load: 
 
182
        # no point waiting.
 
183
        if [ -c /dev/zap/ctl ] || ! grep -q zaptel /proc/modules ; then
 
184
          return
 
185
        fi
 
186
        say "Waiting for /dev/zap/ctl to be generated"
 
187
        devzap_found=0
 
188
        for i in `seq $DEVZAP_TIMEOUT`; do
 
189
                sleep 1
 
190
                if [ -c /dev/zap/ctl ]; then
 
191
                  devzap_found=1
 
192
                        break
 
193
                fi
 
194
        done
 
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."
 
198
        fi
 
199
}
 
200
 
 
201
run_ztcfg() {
 
202
        # Run ztcfg itself
 
203
        if [ "$verbose" = no ]; then
 
204
                $ztcfg_cmd "$@"
 
205
        else
 
206
                say "Reconfiguring identified channels"
 
207
                $ztcfg_cmd -vv "$@"
 
208
        fi
 
209
}
 
210
 
 
211
update_module_list_debian() {
 
212
        say "Updating Debian modules list $MODLIST_FILE."
 
213
        del_args=`for i in $ALL_MODULES ztdummy
 
214
        do
 
215
                echo "$i" | sed s:.\*:-e\ '/^&/d':
 
216
        done`
 
217
        add_args=`for i in $*
 
218
        do
 
219
                echo "$i" | sed s:.\*:-e\ '\$a&':
 
220
        done`
 
221
        
 
222
        sed -i.bak $del_args "$MODLIST_FILE"
 
223
        for i in $*
 
224
        do
 
225
                echo "$i"
 
226
        done >> "$MODLIST_FILE"
 
227
}
 
228
 
 
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"
 
233
}
 
234
 
 
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 "$@"
 
240
        else
 
241
          die "Can't find a modules list to update. Tried: $MODLIST_FILE, $MODLIST_FILE_FEDORA. Aborting"
 
242
        fi
 
243
}
 
244
 
 
245
 
 
246
 
 
247
zap_reg_xpp() {
 
248
        if [ ! -d /proc/xpp ]; then return; fi
 
249
 
 
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:"
 
255
        say "$xbusses"
 
256
 
 
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
 
259
        # the module xpp
 
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
 
264
                echo 1 >$file
 
265
        done
 
266
}
 
267
 
 
268
# Initialize the Xorcom Astribank (xpp/)
 
269
xpp_startup() {
 
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
 
274
 
 
275
        echo "Waiting for Astribank devices to initialize:"
 
276
        cat /proc/xpp/XBUS-[0-9]*/waitfor_xpds 2>/dev/null || true
 
277
        
 
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
 
282
 
 
283
        # this one could actually be run after ztcfg:
 
284
        LC_ALL=C $xpp_sync "$XPP_SYNC"
 
285
}
 
286
 
 
287
 
 
288
 
 
289
usage() {
 
290
        program=`basename $0`
 
291
 
 
292
        echo >&2 "$program: generate zaptel.conf and zapata.conf"
 
293
        echo >&2 "(version $VERSION, $rcsid)"
 
294
        echo >&2 "usage:"
 
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)"
 
299
        echo >&2 ""
 
300
        echo >&2 "Options:"
 
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."
 
311
}
 
312
 
 
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
 
319
 
 
320
        echo "
 
321
[$2]
 
322
port=$1
 
323
"       >>$zapscan_file
 
324
}
 
325
 
 
326
# $1: channel number
 
327
print_pattern() {
 
328
        local astbank_type=''
 
329
        local reset_values=""
 
330
        OPTIND=1
 
331
        while getopts 'a:' arg
 
332
        do
 
333
                case "$arg" in
 
334
                        a) case "$OPTARG" in input|output) astbank_type=$OPTARG;;esac ;;
 
335
                esac
 
336
        done
 
337
        shift $(( $OPTIND-1 ))
 
338
 
 
339
 
 
340
        local chan=$1
 
341
        local sig=$2 #fxs/fxo
 
342
        local mode=$3
 
343
        local method
 
344
        
 
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
 
349
                ae|il|si) 
 
350
                        method=ls
 
351
                        ;;
 
352
                *)
 
353
                        method=ks
 
354
                        ;;
 
355
                esac
 
356
        else
 
357
                method="$fxs_default_start"
 
358
        fi
 
359
        case "$sig" in
 
360
        fxs) sig_name=FXO;;
 
361
        fxo) sig_name=FXS;;
 
362
        esac
 
363
        case "$mode" in
 
364
        list)
 
365
                echo $chan $sig_name $astbank_type;;
 
366
        files)
 
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:
 
370
                
 
371
                echo ";;; line=\"$line\""                      >> $zapata_file
 
372
                
 
373
                if [ "$astbank_type" != '' ]; 
 
374
                        then echo "# astbanktype: $astbank_type" >>$zaptel_file; 
 
375
                fi
 
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
 
379
                        
 
380
                echo "signalling=${sig}_$method" >>$zapata_file
 
381
                if [ "$sig" = 'fxo' ]
 
382
                then
 
383
                        # to preconfigure channel 1's extension to 550, set
 
384
                        # chan_1_exten=550
 
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`
 
392
                        
 
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
 
397
                                exten=$cfg_exten
 
398
                        fi
 
399
                        # is there any real need to set 'mailbox=' ?
 
400
                        if [ "x$cfg_vmbox" = x ]
 
401
                        then # No extension number set for this channel
 
402
                                vmbox=$exten
 
403
                        else # use the pre-configured extension number
 
404
                                vmbox=$cfg_vmbox
 
405
                        fi
 
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" ]
 
411
                        then 
 
412
                                echo "group=$group_phones"                   >> $zapata_file
 
413
                                reset_values="$reset_values group"
 
414
                        fi
 
415
                        if [ "$context_manual" != "yes" ]
 
416
                        then
 
417
                                if [ "$astbank_type" != '' ];
 
418
                                then 
 
419
                                        context_var_name=context_$astbank_type
 
420
                                        echo context=${!context_var_name}          >> $zapata_file
 
421
                                else
 
422
                                        echo "context=$context_phones"             >> $zapata_file
 
423
                                fi
 
424
                                reset_values="$reset_values context"
 
425
                        fi
 
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" ]
 
430
                        then 
 
431
                                echo "group=$group_lines"                    >> $zapata_file
 
432
                        fi
 
433
                        if [ "$context_manual" != "yes" ]
 
434
                        then 
 
435
                                echo "context=$context_lines"                >> $zapata_file
 
436
                                reset_values="$reset_values context"
 
437
                        fi
 
438
                        if [ "$lc_country" = 'uk' ]
 
439
                        then
 
440
                          echo "cidsignalling=v23"                     >> $zapata_file
 
441
                          case $line in 
 
442
                          *WCFXO*) echo "cidstart=history"             >> $zapata_file;;
 
443
                          *)       echo "cidstart=polarity"            >> $zapata_file;;
 
444
                          esac
 
445
                                reset_values="$reset_values cidsignalling cidstart"
 
446
                        fi
 
447
                        case "$line" in
 
448
                        *XPP_FXO*) 
 
449
                                if [ "$xpp_fxo_rxgain" != '' ]; then
 
450
                                        echo "rxgain=$xpp_fxo_rxgain"             >> $zapata_file
 
451
                                        reset_values="$reset_values rxgain"
 
452
                                fi
 
453
                                ;;
 
454
                        esac
 
455
                        # if kewlstart is not used, busydetect has to be employed:
 
456
                        if [ "$method" = 'ls' ]
 
457
                        then
 
458
                          echo 'busydetect=yes'                     >> $zapata_file
 
459
                                reset_values="$reset_values busydetect"
 
460
                        fi
 
461
                fi
 
462
 
 
463
                if [ "$astbank_type" = 'input' ] || \
 
464
                        ( [ "$fxs_immediate" = 'yes' ] && [ "$sig" = "fxo" ] )
 
465
                then 
 
466
                  echo 'immediate=yes'                      >> $zapata_file
 
467
                        reset_values="$reset_values immediate"
 
468
                fi
 
469
                echo "channel => $chan"                          >> $zapata_file
 
470
                reset_zapata_entry $zapata_file $reset_values
 
471
                echo ""                                          >> $zapata_file
 
472
 
 
473
                print_zapscan_port "$sig" "$chan"
 
474
                ;;
 
475
        esac
 
476
        
 
477
}
 
478
 
 
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
 
484
        
 
485
        (
 
486
                for file in `echo /proc/zaptel/* |grep -v '\*'`
 
487
                do sed -e 1,2d $file # remove the two header lines
 
488
                done
 
489
        ) | wc -l # the total number of lines
 
490
}
 
491
 
 
492
load_modules() {
 
493
        say "Test Loading modules:"
 
494
        for i in $ALL_MODULES
 
495
        do
 
496
                lines_before=`count_proc_zap_lines`
 
497
                args="${i}_args"
 
498
                eval "args=\$$args"
 
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 
 
503
                then
 
504
                  check=0
 
505
                  case "$i" in
 
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 ;;
 
508
                        esac
 
509
                        if [ "$check" != 0 ]
 
510
                        then
 
511
                          probed_modules="$probed_modules $i"
 
512
                          say " ok      $i      $args"
 
513
                  else
 
514
                          say " -       $i      $args"
 
515
                                rmmod $i
 
516
                  fi
 
517
                else
 
518
                        say "   -       $i      $args"
 
519
                fi
 
520
        done
 
521
}
 
522
 
 
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 
 
527
unload_module() {
 
528
        module="$1"
 
529
        line=`lsmod 2>/dev/null | grep "^$1 "`
 
530
        if [ "$line" = '' ]; then return; fi # module was not loaded
 
531
 
 
532
        set -- $line
 
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
 
536
        # before them:
 
537
        case "$module" in xpd_*) mods="xpp_usb $mods";; esac
 
538
        for mod in $mods; do
 
539
                # run in a subshell, so it won't step over our vars:
 
540
                (unload_module $mod) 
 
541
                # TODO: the following is probably the error handling we want:
 
542
                # if [ $? != 0 ]; then return 1; fi
 
543
        done
 
544
        rmmod $module
 
545
}
 
546
 
 
547
unload_modules() {
 
548
        say "Unloading zaptel modules:"
 
549
        unload_module zaptel
 
550
        say ''
 
551
}
 
552
 
 
553
# sleep a while until the xpp modules fully register
 
554
wait_for_xpp() {
 
555
        if [ -d /proc/xpp ]
 
556
        then
 
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
 
560
        fi
 
561
}
 
562
 
 
563
detect() {
 
564
        unload_modules
 
565
        temporary_zapconf save
 
566
        load_modules
 
567
        temporary_zapconf restore
 
568
        modlist="$probed_modules"
 
569
        modlist="$(echo $modlist)"              # clean spaces
 
570
        if [ "$do_module_list" = yes ]
 
571
        then
 
572
                update_module_list "$modlist"
 
573
        fi
 
574
        if echo $modlist | grep -q xpp_usb; then wait_for_xpp; fi
 
575
}
 
576
 
 
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.
 
580
 
581
# At hardware detection time we shouldn't really touch zaptel.conf . So we 
 
582
# must keep a copy of it somewhere.
 
583
#
 
584
# I use ZAPCONF_FILE_SYSTEM rather than ZAPCONF_FILE, as the bogus modprobe 
 
585
# entries will not be initiated by us.
 
586
#
 
587
# ZAPCONF_FILE_TMP is a "global", as it needs to be preserved between the
 
588
# call to 'save' and to 'restore'
 
589
ZAPCONF_FILE_TMP=
 
590
temporary_zapconf() {
 
591
  case "$1" in
 
592
        save)
 
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
 
597
                fi
 
598
                echo -n >$ZAPCONF_FILE_SYSTEM
 
599
                ;;
 
600
        restore)
 
601
                # restore original zaptel.conf:
 
602
                if [ "$ZAPCONF_FILE_TMP" = '' ]; then return; fi
 
603
                mv $ZAPCONF_FILE_TMP $ZAPCONF_FILE_SYSTEM
 
604
                ZAPCONF_FILE_TMP=''
 
605
                ;;
 
606
        esac
 
607
}
 
608
 
 
609
# run after the channel's entry. Used to reset all the values 
 
610
reset_zapata_entry() {
 
611
        conf_file="$1"; shift
 
612
        
 
613
        for arg in "$@"; do
 
614
                case "$arg" in
 
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;;
 
622
                esac
 
623
        done
 
624
}
 
625
 
 
626
 
 
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.
 
631
#
 
632
# This function gives the temporary file the permissions of the original,
 
633
# or some sane defaults.
 
634
#
 
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).
 
637
#
 
638
# $1: orig_cfg_file
 
639
# $2: new_var
 
640
gen_tmp_conf() {
 
641
        orig_cfg_file=$1
 
642
        new_var=$2
 
643
 
 
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  
 
650
        else
 
651
                chmod 644 ${!new_var}
 
652
        fi
 
653
}
 
654
 
 
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() {
 
660
        line="$1"
 
661
        chan_num="$2"
 
662
        span_num="$3"
 
663
 
 
664
        # PRI/BRI 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.
 
669
 
 
670
        # span_lastd is always the one before last 
 
671
        # channel. span_bchan is the last:
 
672
        echo $chan_num      >$tmp_dir/span_end
 
673
 
 
674
        if [ "`cat $tmp_dir/span_begin`" != "-1" ]
 
675
        then
 
676
                return #we already configured this span.
 
677
        fi
 
678
 
 
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
 
682
 
 
683
        case "$line" in
 
684
        *ZTHFC*/* | *ztqoz*/* | *XPP_BRI_*/*)
 
685
                # BRI channel
 
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 ]
 
689
                then
 
690
                        echo $ZAPBRI_NET >$tmp_dir/span_signalling 
 
691
                else
 
692
                        echo $ZAPBRI_CPE >$tmp_dir/span_signalling
 
693
                fi
 
694
                ;;
 
695
        *ztgsm*/*)
 
696
                # Junghanns's GSM cards. 
 
697
                echo 'ccs'          >$tmp_dir/span_framing
 
698
                #Does this mean anything?
 
699
                echo 'gsm'          >$tmp_dir/span_signalling
 
700
                ;;
 
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.
 
705
 
 
706
                # PRI span (E1/T1)
 
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 ]
 
711
                then
 
712
                        echo pri_net >$tmp_dir/span_signalling 
 
713
                else
 
714
                        echo pri_cpe >$tmp_dir/span_signalling
 
715
                fi
 
716
                # an example of country-specific setup. This is probably not accurate
 
717
                # Contributions are welcome
 
718
                case "$lc_country" in 
 
719
                nl)
 
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
 
726
                        ;;
 
727
                il|de|au)
 
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
 
732
                        ;;
 
733
                cl)
 
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
 
738
                        ;;
 
739
                esac
 
740
        ;;
 
741
        esac
 
742
}
 
743
 
 
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
 
748
        # nothing to do:
 
749
        if [ "`cat $tmp_dir/span_begin`" = -1 ]; then return; fi
 
750
 
 
751
        # read files to variables:
 
752
        for suffix in num begin end timing lbo framing \
 
753
                coding switchtype signalling yellow termtype
 
754
        do
 
755
                eval span_$suffix=`cat $tmp_dir/span_$suffix 2>/dev/null`
 
756
        done
 
757
 
 
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
 
762
                dchan=$span_end
 
763
                bchans="$span_begin-$(($span_end-1))"
 
764
                ;;
 
765
        31) #E1
 
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:
 
770
                        span_framing=ccs
 
771
                        span_coding=hdb3
 
772
                        span_switchtype=euroisdn
 
773
                        span_yellow=crc4
 
774
                fi
 
775
                ;;
 
776
        esac
 
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, 
 
781
        # second: 2, etc.
 
782
        case "$span_signalling" in *_cpe*)
 
783
                span_timing=$span_te_timing_counter
 
784
                span_te_timing_counter=$(($span_te_timing_counter + 1))
 
785
                ;;
 
786
        esac
 
787
        case "$mode" in
 
788
        files)
 
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
 
794
                fi
 
795
 
 
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
 
800
                
 
801
                span_group=$(($SPAN_GROUP_BASE + $span_num))
 
802
                
 
803
                if [ "$span_termtype" != '' ]
 
804
                then
 
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' ]
 
808
                        then
 
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' ]
 
813
                                then
 
814
                                        echo "overlapdial=yes"         >> $zapata_file
 
815
                                        reset_values="$reset_values overlapdial"
 
816
                                fi
 
817
                                if [ "$group_manual" != "yes" ]
 
818
                                then 
 
819
                                        echo "group=$group_phones,$span_group"     >> $zapata_file
 
820
                                        reset_values="$reset_values group"
 
821
                                fi
 
822
                                if [ "$context_manual" != "yes" ]
 
823
                                then 
 
824
                                        echo "context=$context_phones" >> $zapata_file
 
825
                                fi
 
826
                        else 
 
827
                                #echo "mailbox="
 
828
                                if [ "$group_manual" != "yes" ]
 
829
                                then 
 
830
                                        echo "group=$group_lines,$span_group"      >> $zapata_file
 
831
                                        reset_values="$reset_values group"
 
832
                                fi
 
833
                                if [ "$context_manual" != "yes" ]
 
834
                                then 
 
835
                                        echo "context=$context_lines"  >> $zapata_file
 
836
                                        reset_values="$reset_values context"
 
837
                                fi
 
838
                        fi
 
839
                fi
 
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
 
844
                reset_values=
 
845
                ;;
 
846
        list)
 
847
                echo "### BRI/PRI: $span_termtype" 
 
848
                echo "$bchans Data"
 
849
                echo "$dchan Control"
 
850
                #echo BRI/PRI: chans: $bchans, control: $dchan
 
851
                ;;
 
852
        esac
 
853
}
 
854
 
 
855
# This is where the actual detection configuration detection work happens.
 
856
genconf() {
 
857
        local mode=$1
 
858
        local reset_values=""
 
859
 
 
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/|'`
 
864
 
 
865
        #if [ "$spanlist" == "" ]; then
 
866
        #       die "No zapata interfaces in /proc/zaptel"
 
867
        #fi
 
868
 
 
869
 
 
870
        case "$mode" in 
 
871
        files)
 
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)
 
877
;
 
878
; Automatically generated by $0 -- Please do not edit.
 
879
 
 
880
EOF
 
881
                fi
 
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
 
887
#
 
888
# This file is parsed by the Zaptel Configurator, ztcfg
 
889
#
 
890
 
 
891
# It must be in the module loading order
 
892
 
 
893
EOF
 
894
                cat <<EOF >$zapata_file
 
895
; Autogenerated by $0 -- do not hand edit
 
896
; Zaptel Channels Configurations (zapata.conf)
 
897
;
 
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
 
900
;
 
901
EOF
 
902
                ;;
 
903
        esac
 
904
 
 
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.
 
909
        #
 
910
        # The '<(command)' syntax creates a temporary file whose content is is the
 
911
        # output of 'command'.
 
912
        #
 
913
        # Another problem with such an approach is how to include an existing 
 
914
        # configuration file. For instance: how to include some default settings.
 
915
        #
 
916
        # Maybe an 'include' directive should be added to zaptel.conf ?
 
917
        #cat $spanlist | 
 
918
        for procfile in $spanlist
 
919
        do
 
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)
 
925
                case "$mode" in
 
926
                list) echo "### $title";;
 
927
                files)
 
928
                        echo ""         >>$zaptel_file
 
929
                        echo "# $title" >>$zaptel_file
 
930
                        echo ""         >>$zapata_file
 
931
                        echo "; $title" >>$zapata_file
 
932
                        ;;
 
933
                esac
 
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" != '' ]
 
943
                then
 
944
                        echo "$zapconf_def_termtype"    >$tmp_dir/span_termtype
 
945
                fi
 
946
 
 
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)'
 
949
                then
 
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)'
 
952
                then
 
953
                        echo 'te' >$tmp_dir/span_termtype
 
954
                fi
 
955
 
 
956
                # The rest of the lines are per-channel lines
 
957
                sed -e 1,2d $procfile | \
 
958
                while read line
 
959
                do 
 
960
                        # in case this is a real channel. 
 
961
                        chan_num=`echo $line |awk '{print $1}'`
 
962
                        case "$line" in
 
963
                        *WCTDM/* | *\ WRTDM/* | *OPVXA1200/*) 
 
964
                                # TDM400P/2400P and similar cards (Sangoma A200, OpenVox A1200)
 
965
                                # this can be either FXS or FXO
 
966
                                maybe_fxs=0
 
967
                                maybe_fxo=0
 
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 ]
 
971
                                then 
 
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. 
 
976
                                        case "$mode" in 
 
977
                                        list)  echo "# channel $chan_num, WCTDM, no module.";;
 
978
                                        files) echo "# channel $chan_num, WCTDM, no module." >> $zaptel_file;;
 
979
                                        esac
 
980
                                        continue
 
981
                                fi
 
982
                                
 
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
 
985
                                ;;
 
986
                        *WCFXO/*) 
 
987
                                # X100P
 
988
                                print_pattern $chan_num fxs $mode || \
 
989
                                echo "# channel $chan_num, WCFXO, inactive." >>$zaptel_file
 
990
                                ;;
 
991
                        *WCUSB/*)
 
992
                                print_pattern $chan_num fxo $mode
 
993
                                ;;
 
994
                        *XPP_FXO/*)
 
995
                                # Astribank FXO span
 
996
                                print_pattern $chan_num fxs $mode
 
997
                                ;;
 
998
                        *XPP_FXS/*)
 
999
                                # Astribank FXS span (regular port)
 
1000
                                print_pattern $chan_num fxo $mode
 
1001
                                ;;
 
1002
                        *' FXO'/*)
 
1003
                                # FXO module (probably Rhino)
 
1004
                                print_pattern $chan_num fxs $mode
 
1005
                                ;;
 
1006
                        *' FXS'/*)
 
1007
                                # FXS module (probably Rhino)
 
1008
                                print_pattern $chan_num fxo $mode
 
1009
                                ;;
 
1010
                        *' ---'/*)
 
1011
                                # no module (probably Rhino)
 
1012
                                continue
 
1013
                                ;;
 
1014
                        *XPP_OUT/*)
 
1015
                                # Astribank FXS span (output port)
 
1016
                                print_pattern -a output $chan_num fxo $mode
 
1017
                                ;;
 
1018
                        *XPP_IN/*)
 
1019
                                # Astribank FXS span (input port)
 
1020
                                print_pattern -a input $chan_num fxo $mode
 
1021
                                ;;
 
1022
                        *ZTHFC*/* | *ztqoz*/* |*ztgsm/* |*TE[24]/* | \
 
1023
                                *WCT1/*|*Tor2/* | *TorISA/* | \
 
1024
                                *XPP_BRI_*/* | *WP[TE]1/* | *R[124]T1/* | \
 
1025
                                *XPP_[TE]1*/* )
 
1026
                                detect_digital_channel "$line" "$chan_num" "$span_num"
 
1027
                                ;;
 
1028
                        '') ;;          # Empty line (after span header)
 
1029
                        *) 
 
1030
                                case "$mode" in
 
1031
                                list) echo "# ??: $line";;
 
1032
                                files)
 
1033
                                        echo "# ??: $line" >>$zaptel_file
 
1034
                                        echo "; ??: $line" >>$zapata_file
 
1035
                                esac
 
1036
                                ;;
 
1037
                        esac
 
1038
                done
 
1039
                # end of part in sub-process.
 
1040
                
 
1041
                write_digital_config
 
1042
        done
 
1043
 
 
1044
        if [ "$mode" = 'files' ]
 
1045
        then
 
1046
                cat <<EOF >> ${zaptel_file}
 
1047
 
 
1048
# Global data
 
1049
 
 
1050
loadzone        = $loadzone
 
1051
defaultzone     = $defaultzone
 
1052
EOF
 
1053
        fi
 
1054
        
 
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
 
1063
                fi
 
1064
 
 
1065
                zapata_file_name=`basename $ZAPATA_FILE`
 
1066
                if ! grep -q "^#include.*$zapata_file_name" \
 
1067
                        /etc/asterisk/zapata.conf
 
1068
                then
 
1069
                        say "Note: generated $ZAPATA_FILE not included in zapata.conf"
 
1070
                        say "To fix:  echo '#include $zapata_file_name' >>/etc/asterisk/zapata.conf"
 
1071
                fi
 
1072
        fi
 
1073
}
 
1074
 
 
1075
while getopts 'c:de:Fhlm:MRsuvz' arg
 
1076
do
 
1077
        case "$arg" in
 
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
 
1081
                        ;;
 
1082
                c) lc_country=`echo $OPTARG | tr -d -c a-z` ;;
 
1083
                d) do_detect=yes ;;
 
1084
                F) fxsdisable=yes;;
 
1085
                u) do_unload=yes ; force_stop_ast=yes ;;
 
1086
                v) verbose=yes ;;
 
1087
                l) mode='list' ;;
 
1088
                M) do_module_list=yes; do_detect=yes ;;
 
1089
                s) force_stop_ast=yes ;;
 
1090
                R) do_restart=no ;;
 
1091
                z) do_gen_zapscan=yes ;;
 
1092
                h) usage; exit 0;;
 
1093
                *) echo >&2 "unknown parameter -$arg, Aborting"; usage; exit 1;;
 
1094
        esac
 
1095
done
 
1096
shift $(( $OPTIND-1 ))
 
1097
if [ $# != 0 ]; then
 
1098
        echo >&2 "$0: too many parameters"
 
1099
        usage
 
1100
        exit 1
 
1101
fi
 
1102
 
 
1103
tmp_dir=`mktemp -d /tmp/genzaptelconf-dir-XXXXXX` || \
 
1104
        die "$0: failed to create temporary directory. Aborting"
 
1105
 
 
1106
 
 
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):;;
 
1111
        *) 
 
1112
                lc_country=us
 
1113
                echo >&2 "unknown country-code $lc_country, defaulting to \"us\""
 
1114
                ;;
 
1115
esac
 
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
 
1120
 
 
1121
# make sure asterisk is not in our way
 
1122
if [ "$force_stop_ast" = 'yes' ]
 
1123
then
 
1124
        $ZAPCONF_ASTERISK_CMD stop 1>&2
 
1125
else
 
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 
 
1130
        then
 
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."
 
1134
                exit_cleanup 1
 
1135
        fi
 
1136
fi
 
1137
 
 
1138
if [ "$do_unload" = yes ]
 
1139
then
 
1140
        unload_modules
 
1141
        exit_cleanup $?
 
1142
fi
 
1143
 
 
1144
if [ "$do_detect" = yes ]
 
1145
then
 
1146
        detect
 
1147
fi
 
1148
 
 
1149
if [ "$zapconf_use_perl" = yes ]; then
 
1150
        #redefine genconf to use perl utilities:
 
1151
        genconf() {
 
1152
                case "$1" in 
 
1153
                list) zaptel_hardware ;;
 
1154
                files) zapconf ;;
 
1155
                esac
 
1156
        }
 
1157
fi
 
1158
 
 
1159
if [ "$mode" = list ]; then
 
1160
        genconf list
 
1161
else
 
1162
        #zap_reg_xpp
 
1163
        xpp_startup
 
1164
        wait_for_zapctl
 
1165
        say "Generating '${ZAPCONF_FILE} and ${ZAPATA_FILE}'"
 
1166
        genconf files
 
1167
        run_ztcfg
 
1168
fi
 
1169
 
 
1170
if [ "$tmp_dir" != '' ]
 
1171
then
 
1172
        rm -rf "$tmp_dir"
 
1173
fi
 
1174
 
 
1175
if [ "$force_stop_ast" != 'yes' ] || [ "$do_restart" != 'yes' ]
 
1176
then
 
1177
        exit_cleanup 0
 
1178
fi
 
1179
 
 
1180
if [ -x "$ZAPCONF_ASTERISK_SCRIPT" ]
 
1181
then
 
1182
        $ZAPCONF_ASTERISK_CMD start 1>&2
 
1183
fi
 
1184
 
 
1185
# if in verbose mode: verify that asterisk is running
 
1186
if [ "$verbose" != 'no' ] 
 
1187
        then
 
1188
        say "Checking channels configured in Asterisk:"
 
1189
        sleep 1 # give it some time. This is enough on our simple test server
 
1190
        if [ -x ast-cmd ]
 
1191
        then
 
1192
                ast-cmd cmd "zap show channels"
 
1193
        else
 
1194
                asterisk -rx "zap show channels"
 
1195
        fi
 
1196
fi
 
1197
 
 
1198
# vim:ts=8: