summaryrefslogtreecommitdiff
path: root/xpp/utils/zapconf
blob: 8bcca1319e578eba25b3d897413544eec649eebf (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
#! /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;
BEGIN { my $dir = $0; $dir =~ s:/[^/]+$::; unshift(@INC, "$dir", "$dir/zconf"); }

use Zaptel;
use Zaptel::Xpp;

my %default_context = (
	FXO	=> 'from-pstn',
	FXS	=> 'from-internal',
	IN	=> 'astbank-input',
	OUT	=> 'astbank-output'
	);

my %default_group = (
	FXO	=> '0',
	FXS	=> '5',
	IN	=> '5',
	OUT	=> '5'
	);

my @zaptel_default_vars = qw(
		base_exten
		lc_country
		context_lines
		context_phones
		context_input
		context_output
		group_lines
		group_phones
	);

my $fxs_immediate = 'no';
my $fxs_default_start;
my $lc_country = 'us';
my $loadzone;
my $defaultzone;

sub map_zaptel_defaults {
	my %defaults = @_;

	$default_group{FXO} = $defaults{group_lines}
			if defined $defaults{group_lines};
	$default_group{FXS} = $default_group{IN} = $default_group{OUT} = $defaults{group_phones}
			if defined $defaults{group_phones};
	$default_context{FXO} ||= $defaults{context_lines};
	$default_context{FXS} ||= $defaults{context_phones};
	$default_context{IN} ||= $defaults{context_input};
	$default_context{OUT} ||= $defaults{context_output};
	$fxs_immediate ||= $defaults{fxs_immediate};
	$fxs_default_start ||= $defaults{fxs_default_start} || 'ls';
	$lc_country ||= $defaults{lc_country};
	$loadzone = $defaultzone = $lc_country;
}


my $zapconf_file;
my $zapata_file;

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;

	if($span->is_bri()) {
		$range = sprintf "%d-%d", $first_num, $first_num + 1;
	}
	return $range;
}

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

	if($type eq 'FXO') {
		printf "fxsks=%d\n", $num;
	} elsif($type eq 'FXS') {
		printf "fxo%s=%d\n", $fxs_default_start, $num;
	} elsif ($type eq 'IN') {
		printf "# astbanktype: input\n";
		printf "fxo%s=%d\n", $fxs_default_start, $num;
	} elsif ($type eq 'OUT') {
		printf "# astbanktype: output\n";
		printf "fxo%s=%d\n", $fxs_default_start, $num;
	} elsif ($type eq 'BRI_TE') {
		printf "# termtype: te\n";
	} elsif ($type eq 'BRI_NT') {
		printf "# termtype: nt\n";
	} elsif($type eq 'EMPTY') {
		printf "# channel %d, %s, no module.\n", $num, $chan->fqn;
		return;
	}
}

my $bri_te_last_timing = 1;

sub gen_zaptel_digital($) {
	my $span = shift || die;
	my $num = $span->num();
	my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n";
	my $timing;
	my $lbo = 1;
	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_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\n",
			$num,
			$timing,
			$lbo,
			$framing,
			"$coding$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 $!{ENOENT}
		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_bri()) {
			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;
}

sub gen_zapata_channel($) {
	my $chan = shift || die;
	my $type = $chan->type;
	my $num = $chan->num;
	my $sig;
	my $immediate;
	my $callerid = sprintf "\"Channel %d\" <4%03d>", $num, $num;
	my $context = $default_context{$type};
	my $group = $default_group{$type};

	if ($type eq 'FXO') {
		$sig = "fxs_ks";
		$callerid = 'asreceived';
	} elsif($type eq 'FXS') {
		$sig = "fxo_" . $fxs_default_start;
		$immediate = 'yes' if $fxs_immediate eq 'yes';
	} elsif($type eq 'IN') {
		$sig = "fxo_" . $fxs_default_start;
		$immediate = 'yes';
	} elsif($type eq 'OUT') {
		$sig = "fxo_" . $fxs_default_start;
	} elsif($type eq 'EMPTY') {
		return;
	}
	die "Unknown signalling for channel $num type $type\n"
		unless defined $sig;
	printf ";;; line=\"%d %s\"\n", $num, $chan->fqn;
	printf "signalling=$sig\n";
	printf "callerid=$callerid\n";
	printf "mailbox=4%03d\n", $num unless $type eq 'FXO';
	if(defined $group) {
		printf "group=%d\n", $group;
	}
	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_zapataconf($) {
	my $file = shift || die;
	rename "$file", "$file.bak"
		or $!{ENOENT}
		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;
		foreach my $chan ($span->chans()) {
			gen_zapata_channel($chan);
		}
		print "\n";
	}
	close F;
	select $old;
}

# Use the shell to source a file and expand a given list
# of variables.
sub do_source($@) {
	my $file = shift;
	my @vars = @_;
	my @output = `env -i sh -e -c '. $file; export @vars; env'`;
	die "$0: Sourcing '$file' exited with $?" if $?;
	my %vars;
	
	foreach my $line (@output) {
		chomp $line;
		my ($k, $v) = split(/=/, $line, 2);
		$vars{$k} = $v if grep /^$k$/, @vars;
	}
	return %vars;
}

sub set_defaults {
	my $zaptel_boot_debian = $ENV{ZAPTEL_BOOT_DEBIAN} || "/etc/default/zaptel";
	my $zaptel_boot_fedora = $ENV{ZAPTEL_BOOT_FEDORA} || "/etc/sysconfig/zaptel";

	# Source default files
	my %source_defaults;
	foreach my $defaults ($zaptel_boot_debian, $zaptel_boot_fedora) {
		%source_defaults = do_source($defaults, @zaptel_default_vars) if -r $defaults;
	}
	map_zaptel_defaults(%source_defaults);
	$zapconf_file = $ENV{ZAPCONF_FILE} || "/etc/zaptel.conf";
	$zapata_file = $ENV{ZAPATA_FILE} || "/etc/asterisk/zapata-channels.conf";
}

set_defaults;
gen_zaptelconf $zapconf_file;
gen_zapataconf $zapata_file;

__END__

=head1 NAME

zapconf - Generate configuration for zaptel channels.

=head1 SYNOPSIS

zapconf

=head1 DESCRIPTION