summaryrefslogtreecommitdiff
path: root/kernel/xpp/utils/zapconf
blob: 78a20b961cbfd47d6c404735bf347bf6b48e5e18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#! /usr/bin/perl -w
#
# Written by Oron Peled <oron@actcom.co.il>
# Copyright (C) 2007, Xorcom
# This program is free software; you can redistribute and/or
# modify it under the same terms as Perl itself.
#
# $Id$
#
use strict;
use File::Basename;
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/zconf"); }

use Zaptel;
use Zaptel::Xpp;
use Zaptel::Config::Defaults;

my %default_context = (
	FXO	=> 'from-pstn',
	FXS	=> 'from-internal',
	IN	=> 'astbank-input',
	OUT	=> 'astbank-output',
	BRI_TE	=> 'from-pstn',
	BRI_NT	=> 'from-internal',
	E1_TE	=> 'from-pstn',
	T1_TE	=> 'from-pstn',
	J1_TE	=> 'from-pstn',
	E1_NT	=> 'from-internal',
	T1_NT	=> 'from-internal',
	J1_NT	=> 'from-internal',
	);

my %default_group = (
	FXO	=> 0,
	FXS	=> "5",
	IN	=> '',
	OUT	=> '',
	BRI_TE	=> 0,
	BRI_NT	=> 6,
	E1_TE	=> 0,
	T1_TE	=> 0,
	J1_TE	=> 0,
	E1_NT	=> 6,
	T1_NT	=> 6,
	J1_NT	=> 6,
	);

my $fxs_default_start = 'ls';

my %default_zaptel_signalling = (
	FXO	=> 'fxsks',
	FXS	=> "fxo{fxs_default_start}",
	IN	=> "fxo{fxs_default_start}",
	OUT	=> "fxo{fxs_default_start}",
	);

my %default_zapata_signalling = (
	FXO	=> 'fxs_ks',
	FXS	=> "fxo_{fxs_default_start}",
	IN	=> "fxo_{fxs_default_start}",
	OUT	=> "fxo_{fxs_default_start}",
	);

my $base_exten = 4000;
my $fxs_immediate = 'no';
my $lc_country = 'us';
my $loadzone = $lc_country;
my $defaultzone = $lc_country;
my $bri_sig_style = 'bri_ptmp';
my $brint_overlap = 'no';

my %zaptel_default_vars = (
		base_exten		=> \$base_exten,
		fxs_immediate		=> \$fxs_immediate,
		fxs_default_start	=> \$fxs_default_start,
		lc_country		=> [
						\$loadzone,
						\$defaultzone,
					],
		context_lines		=> \$default_context{FXO},
		context_phones		=> \$default_context{FXS},
		context_input		=> \$default_context{IN},
		context_output		=> \$default_context{OUT},
		group_phones		=> [
						\$default_group{FXS},
						\$default_group{IN},
						\$default_group{OUT},
					],
		group_lines		=> \$default_group{FXO},
		ZAPBRI_SIGNALLING	=> \$bri_sig_style,
		brint_overlap		=> \$brint_overlap,
		);

sub map_zaptel_defaults {
	my %defaults = @_;
	foreach my $name (keys %defaults) {
		my $val = $defaults{$name};
		my $ref = $zaptel_default_vars{$name};
		my $type = ref $ref;
		my @vars = ();
		# Some broken shells (msh) export even variables
		# That where not defined. Work around that.
		next unless defined $val && $val ne '';
		if($type eq 'SCALAR') {
			@vars = ($ref);
		} elsif($type eq 'ARRAY') {
			@vars = @$ref;
		} else {
			die "$0: Don't know how to map '$name' (type=$type)\n";
		}
		foreach my $v (@vars) {
			$$v = $val;
		}
	}
}


my $zapconf_file;
my $zapatachannels_file;
my $users_file;
my $zapataconf_file;

my %files = (
	zaptel		=> { file => \$zapconf_file, func => \&gen_zaptelconf },
	zapata		=> { file => \$zapatachannels_file, func => \&gen_zapatachannelsconf },
	users		=> { file => \$users_file, func => \&gen_usersconf },
	zapataconf	=> { file => \$zapataconf_file, func => \&gen_zapataconf },
);

my @default_files = ("zaptel", "zapata");

my @spans = Zaptel::spans();

sub bchan_range($) {
	my $span = shift || die;
	my $first_chan = ($span->chans())[0];
	my $first_num = $first_chan->num();
	my $range_start = $first_num;
	my @range;
	my $prev = undef;

	die unless $span->is_digital();
	foreach my $c (@{$span->bchan_list()}) {
		my $curr = $c + $first_num;
		if(!defined($prev)) {
			$prev = $curr;
		} elsif($curr != $prev + 1) {
			push(@range, sprintf("%d-%d", $range_start, $prev));
			$range_start = $curr;
		}
		$prev = $curr;
	}
	if($prev >= $first_num) {
		push(@range, sprintf("%d-%d", $range_start, $prev));
	}
	return join(',', @range);
}

sub gen_zaptel_signalling($) {
	my $chan = shift || die;
	my $type = $chan->type;
	my $num = $chan->num;

	die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
	if($type eq 'EMPTY') {
		printf "# channel %d, %s, no module.\n", $num, $chan->fqn;
		return;
	}
	my $sig = $default_zaptel_signalling{$type} || die "unknown default zaptel signalling for chan $num type $type";
	if ($type eq 'IN') {
		printf "# astbanktype: input\n";
	} elsif ($type eq 'OUT') {
		printf "# astbanktype: output\n";
	}
	printf "$sig=$num\n";
}

my $bri_te_last_timing = 1;

sub gen_zaptel_digital($) {
	my $span = shift || die;
	my $num = $span->num() || die;
	die "Span #$num is analog" unless $span->is_digital();
	my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n";
	my $timing;
	my $lbo = 0;
	my $framing = $span->framing() || die "$0: No framing information for span #$num\n";
	my $coding =  $span->coding() || die "$0: No coding information for span #$num\n";
	my $span_crc4 = $span->crc4();
	$span_crc4 = (defined $span_crc4) ? ",$span_crc4" : '';
	my $span_yellow = $span->yellow();
	$span_yellow = (defined $span_yellow) ? ",$span_yellow" : '';

	$timing = ($termtype eq 'NT') ? 0 : $bri_te_last_timing++;
	printf "span=%d,%d,%d,%s,%s%s%s\n",
			$num,
			$timing,
			$lbo,
			$framing,
			$coding,
			$span_crc4,
			$span_yellow;
	printf "# termtype: %s\n", lc($termtype);
	printf "bchan=%s\n", bchan_range($span);
	my $dchan = $span->dchan();
	printf "dchan=%d\n", $dchan->num();
}

sub gen_zaptelconf($) {
	my $file = shift || die;
	rename "$file", "$file.bak"
		or $! == 2	# ENOENT (No dependency on Errno.pm)
		or die "Failed to backup old config: $!\n";
	open(F, ">$file") || die "$0: Failed to open $file: $!\n";
	my $old = select F;
	printf "# Autogenerated by %s on %s -- do not hand edit\n", $0, scalar(localtime);
	print <<"HEAD";
# Zaptel Configuration File
#
# This file is parsed by the Zaptel Configurator, ztcfg
#
HEAD
	foreach my $span (@spans) {
		printf "# Span %d: %s %s\n", $span->num, $span->name, $span->description;
		if($span->is_digital()) {
			gen_zaptel_digital($span);
		} else {
			foreach my $chan ($span->chans()) {
				if(1 || !defined $chan->type) {
					my $type = $chan->probe_type;
					my $num = $chan->num;
					die "Failed probing type for channel $num"
						unless defined $type;
					$chan->type($type);
				}
				gen_zaptel_signalling($chan);
			}
		}
		print "\n";
	}
	print <<"TAIL";
# Global data

loadzone	= $loadzone
defaultzone	= $defaultzone
TAIL
	close F;
	select $old;
}

my %DefaultConfigs = (
	context => 'default',
	group => '63', # FIXME: should not be needed. 
	overlapdial => 'no',
	busydetect => 'no',
	rxgain => 0,
	txgain => 0,
);

sub reset_zapata_values {
	foreach my $arg (@_) {
		if (exists $DefaultConfigs{$arg}) {
			print "$arg = $DefaultConfigs{$arg}\n";
		} else {
			print "$arg =\n";
		}
	}
}

sub gen_zapata_digital($) {
	my $span = shift || die;
	my $num = $span->num() || die;
	die "Span #$num is analog" unless $span->is_digital();
	my $type = $span->type() || die "$0: Span #$num -- unkown type\n";
	my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n";
	my $group = $default_group{"$type"};
	my $context = $default_context{"$type"};
	my @to_reset = qw/context group/;

	die "$0: missing default group (termtype=$termtype)\n" unless defined($group);
	die "$0: missing default context\n" unless $context;

	my $sig = $span->signalling || die "missing signalling info for span #$num type $type";
	grep($bri_sig_style eq $_, 'bri', 'bri_ptmp', 'pri') or die "unknown signalling style for BRI";
	if($span->is_bri() and $bri_sig_style eq 'bri_ptmp') {
		$sig .= '_ptmp';
	}
	if ($span->is_bri() && $termtype eq 'NT' && $brint_overlap eq 'yes') {
		print "overlapdial = yes\n";
		push(@to_reset, qw/overlapdial/);
	}
		
	$group .= "," . (10 + $num);	# Invent unique group per span
	printf "group=$group\n";
	printf "context=$context\n";
	printf "switchtype = %s\n", $span->switchtype;
	printf "signalling = %s\n", $sig;
	printf "channel => %s\n", bchan_range($span);
	reset_zapata_values(@to_reset);
}

sub gen_zapata_channel($) {
	my $chan = shift || die;
	my $type = $chan->type;
	my $num = $chan->num;
	die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
	my $exten = $base_exten + $num;
	my $sig = $default_zapata_signalling{$type};
	my $context = $default_context{$type};
	my $group = $default_group{$type};
	my $callerid;
	my $immediate;

	return if $type eq 'EMPTY';
	die "missing default_zapata_signalling for chan #$num type $type" unless $sig;
	$callerid = ($type eq 'FXO')
			? 'asreceived'
			: sprintf "\"Channel %d\" <%04d>", $num, $exten;
	if($type eq 'IN') {
		$immediate = 'yes';
	}
	# FIXME: $immediage should not be set for 'OUT' channels, but meanwhile
	#        it's better to be compatible with genzaptelconf
	$immediate = 'yes' if $fxs_immediate eq 'yes' and $sig =~ /^fxo_/;
	my $signalling = $chan->signalling;
	$signalling = " " . $signalling if $signalling;
	my $info = $chan->info;
	$info = " " . $info if $info;
	printf ";;; line=\"%d %s%s%s\"\n", $num, $chan->fqn, $signalling, $info;
	printf "signalling=$sig\n";
	printf "callerid=$callerid\n";
	printf "mailbox=%04d\n", $exten unless $type eq 'FXO';
	if(defined $group) {
		printf "group=$group\n";
	}
	printf "context=$context\n";
	printf "immediate=$immediate\n" if defined $immediate;
	printf "channel => %d\n", $num;
	# Reset following values to default
	printf "callerid=\n";
	printf "mailbox=\n" unless $type eq 'FXO';
	if(defined $group) {
		printf "group=\n";
	}
	printf "context=default\n";
	printf "immediate=no\n" if defined $immediate;
	print "\n";
}

sub gen_zapatachannelsconf($) {
	my $file = shift || die;
	rename "$file", "$file.bak"
		or $! == 2	# ENOENT (No dependency on Errno.pm)
		or die "Failed to backup old config: $!\n";
	open(F, ">$file") || die "$0: Failed to open $file: $!\n";
	my $old = select F;
	printf "; Autogenerated by %s on %s -- do not hand edit\n", $0, scalar(localtime);
	print <<"HEAD";
; Zaptel Channels Configurations (zapata.conf)
;
; This is not intended to be a complete zapata.conf. Rather, it is intended
; to be #include-d by /etc/zapata.conf that will include the global settings
;

HEAD
	foreach my $span (@spans) {
		printf "; Span %d: %s %s\n", $span->num, $span->name, $span->description;
		if($span->is_digital()) {
			gen_zapata_digital($span);
		} else {
			foreach my $chan ($span->chans()) {
				gen_zapata_channel($chan);
			}
		}
		print "\n";
	}
	close F;
	select $old;
}

sub gen_users_channel($) {
	my $chan = shift || die;
	my $type = $chan->type;
	my $num = $chan->num;
	die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
	my $exten = $base_exten + $num;
	my $sig = $default_zapata_signalling{$type};
	my $full_name = "$type $num";

	die "missing default_zapata_signalling for chan #$num type $type" unless $sig;
	print << "EOF";
[$exten]
callwaiting = yes
context = numberplan-custom-1
fullname = $full_name
cid_number = $exten
hasagent = no
hasdirectory = no
hasiax = no
hasmanager = no
hassip = no
hasvoicemail = yes
host = dynamic
mailbox = $exten
threewaycalling = yes
vmsecret = 1234
secret = 1234
signalling = $sig
zapchan = $num
registeriax = no
registersip = no
canreinvite = no
nat = no
dtmfmode = rfc2833
disallow = all
allow = all

EOF
}

# generate users.conf . The specific users.conf is strictly oriented
# towards using with the asterisk-gui .
#
# This code could have generated a much simpler and smaller
# configuration file, had there been minimal level of support for
# configuration templates in the asterisk configuration rewriting. Right
# now Asterisk's configuration rewriting simply freaks out in the face
# of templates: http://bugs.digium.com/11442 .
sub gen_usersconf($) {
	my $file = shift || die;
	rename "$file", "$file.bak"
		or $! == 2	# ENOENT (No dependency on Errno.pm)
		or die "Failed to backup old config: $!\n";
	open(F, ">$file") || die "$0: Failed to open $file: $!\n";
	my $old = select F;
	print <<"HEAD";
;!
;! Automatically generated configuration file
;! Filename: @{[basename($file)]} ($file)
;! Generator: $0
;! Creation Date: @{[scalar(localtime)]}
;!
[general]
;
; Full name of a user
;
fullname = New User
;
; Starting point of allocation of extensions
;
userbase = @{[$base_exten+1]}
;
; Create voicemail mailbox and use use macro-stdexten
;
hasvoicemail = yes
;
; Set voicemail mailbox @{[$base_exten+1]} password to 1234
;
vmsecret = 1234
;
; Create SIP Peer
;
hassip = no
;
; Create IAX friend
;
hasiax = no
;
; Create Agent friend
;
hasagent = no
;
; Create H.323 friend
;
;hash323 = yes
;
; Create manager entry
;
hasmanager = no
;
; Remaining options are not specific to users.conf entries but are general.
;
callwaiting = yes
threewaycalling = yes
callwaitingcallerid = yes
transfer = yes
canpark = yes
cancallforward = yes
callreturn = yes
callgroup = 1
pickupgroup = 1
localextenlength = @{[length($base_exten)]}


HEAD
	foreach my $span (@spans) {
		next unless grep { $_ eq $span->type} ( 'FXS', 'IN', 'OUT' );
		printf "; Span %d: %s %s\n", $span->num, $span->name, $span->description;
		foreach my $chan ($span->chans()) {
			gen_users_channel($chan);
		}
		print "\n";
	}
	close F;
	select $old;
}

sub gen_zapataconf($) {
	my $file = shift || die;
	open(F, ">>$file") || die "$0: Failed to open $file: $!\n";
	my $old = select F;
	foreach my $span (@spans) {
		next unless $span->type eq 'FXO';
		my $current_sig = "";
		for my $chan ($span->chans()) {
			my $chan_num = $chan->num;
			if ($default_zapata_signalling{$chan->type} ne $current_sig) {
				$current_sig = $default_zapata_signalling{$chan->type};
				print "\nsignalling = $current_sig";
				print "\nchannel => $chan_num";
			} else {
				print ",$chan_num";
			}
		}
		print "\n";
	}
	close F;
	select $old;
}

sub set_defaults {
	# Source default files
	my ($default_file, %source_defaults) =
		Zaptel::Config::Defaults::source_vars(keys(%zaptel_default_vars));
	map_zaptel_defaults(%source_defaults);
	# Fixups
	foreach my $val (values %default_zaptel_signalling, values %default_zapata_signalling) {
		$val =~ s/{fxs_default_start}/$fxs_default_start/g;
	}
	$zapconf_file = $ENV{ZAPCONF_FILE} || "/etc/zaptel.conf";
	$zapatachannels_file = $ENV{ZAPATA_FILE} || "/etc/asterisk/zapata-channels.conf";
	$users_file = $ENV{USERS_FILE} || "/etc/asterisk/users.conf";
	$zapataconf_file = $ENV{ZAPATACONF_FILE} || "/etc/asterisk/zapata.conf";
}

sub parse_args {
	return if @ARGV == 0;
	@default_files = ();
	for my $file (@ARGV) {
		die "$0: Unknown file '$file'" unless defined $files{$file};
		push @default_files, $file;
	}
}

sub generate_files {
	for my $file (@default_files) {
		&{$files{$file}->{func}}(${$files{$file}->{file}});
	}
}
set_defaults;
parse_args;
generate_files;

__END__

=head1 NAME

zapconf - Generate configuration for zaptel channels.

=head1 SYNOPSIS

zapconf [FILES...]

=head1 DESCRIPTION

This script generate configuration files for Zaptel hardware.
Currently it can generate three files: zaptel, zapata, users and zapataconf (see below).
Without arguments, it generates only zaptel and zapata.

=over 4

=item zaptel - /etc/zaptel.conf

Configuration for ztcfg(1). It's location may be overriden by the
environment variable ZAPCONF_FILE.

=item zapata - /etc/asterisk/zapata-channels.conf

Configuration for asterisk(1). It should be included in the main /etc/asterisk/zapata.conf.
It's location may be overriden by the environment variable ZAPATA_FILE.

=item users - /etc/asterisk/users.conf

Configuration for asterisk(1) and AsteriskGUI.
It's location may be overriden by the environment variable USERS_FILE.

=item zapataconf - /etc/asterisk/zapata.conf

Configuration for asterisk(1) and AsteriskGUI.
It's location may be overriden by the environment variable ZAPATACONF_FILE.


=back