Debian Codegraph @

xpp/perl_modules/Dahdi/Config/Gen/System.pm

raw
1 package Dahdi::Config::Gen::System;
2 use strict;
3
4 use Dahdi::Config::Gen qw(is_true);
5
6 sub new($$$) {
7 my $pack = shift || die;
8 my $gconfig = shift || die;
9 my $genopts = shift || die;
10 my $file = $ENV{DAHDI_CONF_FILE} || "/etc/dahdi/system.conf";
11 my $self = {
12 FILE => $file,
13 GCONFIG => $gconfig,
14 GENOPTS => $genopts,
15 };
16 bless $self, $pack;
17 return $self;
18 }
19
20 my $bri_te_last_timing = 1;
21
22 sub print_echo_can($$) {
23 my $gconfig = shift || die;
24 my $chans = shift || die; # channel or range of channels.
25 my $echo_can = $gconfig->{'echo_can'};
26 return if !defined($echo_can) || $echo_can eq 'none';
27
28 print "echocanceller=$echo_can,$chans\n";
29 }
30
31 sub gen_t1_cas($$) {
32 my $self = shift || die;
33 my $gconfig = shift || die;
34 my $parameters = $gconfig->{PARAMETERS} || die;
35 my $genconf_file = $parameters->{GENCONF_FILE} || die;
36 my $span = shift || die;
37 my $num = $span->num() || die;
38 my $proto = $span->proto || die;
39 die "Generate configuration for '$proto' is not possible. Maybe you meant R2?"
40 unless $proto eq 'T1';
41 my $pri_connection_type = $gconfig->{pri_connection_type} || die;
42 die "Span #$num is analog" unless $span->is_digital();
43 die "Span #$num is not CAS" unless $span->is_pri && $pri_connection_type eq 'CAS';
44 my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n";
45 my $timing;
46 my $lbo = 0;
47 my $framing = $gconfig->{tdm_framing};
48 if(!defined $framing) {
49 $framing = 'esf';
50 } elsif($framing ne 'esf' && $framing ne 'd4') {
51 die "T1-CAS valid framing is only 'esf' or 'd4'. Not '$framing'. Check '$genconf_file'\n";
52 }
53 my $coding = $span->coding() || die "$0: No coding information for span #$num\n";
54 my $span_crc4 = $span->crc4();
55 $span_crc4 = (defined $span_crc4) ? ",$span_crc4" : '';
56 my $span_yellow = $span->yellow();
57 $span_yellow = (defined $span_yellow) ? ",$span_yellow" : '';
58 $timing = ($termtype eq 'NT') ? 0 : $bri_te_last_timing++;
59 printf "span=%d,%d,%d,%s,%s%s%s\n",
60 $num,
61 $timing,
62 $lbo,
63 $framing,
64 $coding,
65 $span_crc4,
66 $span_yellow;
67 printf "# termtype: %s\n", lc($termtype);
68 my $dchan_type;
69 my $chan_range;
70 if($span->is_pri()) {
71 if ($pri_connection_type eq 'PRI') {
72 $chan_range = Dahdi::Config::Gen::bchan_range($span);
73 printf "bchan=%s\n", $chan_range;
74 my $dchan = $span->dchan();
75 printf "dchan=%d\n", $dchan->num();
76 } elsif ($pri_connection_type eq 'R2' ) {
77 my $idle_bits = $gconfig->{'r2_idle_bits'};
78 $chan_range = Dahdi::Config::Gen::bchan_range($span);
79 printf "cas=%s:$idle_bits\n", $chan_range;
80 } elsif ($pri_connection_type eq 'CAS' ) {
81 my $type = ($termtype eq 'TE') ? 'FXO' : 'FXS';
82 my $sig = $gconfig->{'dahdi_signalling'}{$type};
83 my $em_signalling = $gconfig->{'em_signalling'};
84 if ($em_signalling ne 'none') {
85 $sig = 'e&m';
86 # FIXME: but we don't handle E1 yet
87 $sig = 'e&me1' if $proto eq 'E1';
88 }
89 die "unknown default dahdi signalling for chan $num type $type" unless defined $sig;
90 $chan_range = Dahdi::Config::Gen::chan_range($span->chans());
91 printf "%s=%s\n", $sig, $chan_range;
92 }
93 } else {
94 die "Digital span $num is not PRI";
95 }
96 print_echo_can($gconfig, $chan_range);
97 }
98
99 sub gen_digital($$$) {
100 my $self = shift || die;
101 my $gconfig = shift || die;
102 my $span = shift || die;
103 my $num = $span->num() || die;
104 die "Span #$num is analog" unless $span->is_digital();
105 my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n";
106 my $timing;
107 my $lbo = 0;
108 my $framing = $span->framing() || die "$0: No framing information for span #$num\n";
109 my $coding = $span->coding() || die "$0: No coding information for span #$num\n";
110 my $span_crc4 = $span->crc4();
111 $span_crc4 = (defined $span_crc4) ? ",$span_crc4" : '';
112 my $span_yellow = $span->yellow();
113 $span_yellow = (defined $span_yellow) ? ",$span_yellow" : '';
114 my $span_termination = $span->termination();
115 $span_termination = (defined $span_termination) ? ",$span_termination" : '';
116 my $span_softntte = $span->softntte();
117 $span_softntte = (defined $span_softntte) ? ",$span_softntte" : '';
118 # "MFC/R2 does not normally use CRC4"
119 # FIXME: a finer way to override:
120 if ($gconfig->{'pri_connection_type'} eq 'R2') {
121 $span_crc4 = '';
122 $framing = 'cas';
123 }
124 $timing = ($termtype eq 'NT') ? 0 : $bri_te_last_timing++;
125 printf "span=%d,%d,%d,%s,%s%s%s%s%s\n",
126 $num,
127 $timing,
128 $lbo,
129 $framing,
130 $coding,
131 $span_crc4,
132 $span_yellow,
133 $span_termination,
134 $span_softntte;
135 printf "# termtype: %s\n", lc($termtype);
136 my $dchan_type;
137 if ($span->is_bri()) {
138 my $use_bristuff = 0;
139 my $cfg_hardhdlc = $gconfig->{'bri_hardhdlc'};
140 my $xpd = Dahdi::Xpp::xpd_of_span($span);
141 if(!defined($cfg_hardhdlc) || $cfg_hardhdlc =~ /AUTO/i) {
142 # Autodetect
143 if(defined($xpd)) {
144 # Bristuff?
145 if(defined($xpd->dchan_hardhdlc) && !is_true($xpd->dchan_hardhdlc)) {
146 $use_bristuff = 1;
147 }
148 }
149 } elsif(!is_true($cfg_hardhdlc)) {
150 $use_bristuff = 1;
151 }
152 if($use_bristuff) {
153 $dchan_type = 'dchan';
154 } else {
155 $dchan_type = 'hardhdlc';
156 }
157 printf "bchan=%s\n", Dahdi::Config::Gen::bchan_range($span);
158 my $dchan = $span->dchan();
159 printf "$dchan_type=%d\n", $dchan->num();
160 } elsif($span->is_pri()) {
161 if ($gconfig->{'pri_connection_type'} eq 'PRI') {
162 printf "bchan=%s\n", Dahdi::Config::Gen::bchan_range($span);
163 my $dchan = $span->dchan();
164 printf "dchan=%d\n", $dchan->num();
165 } elsif ($gconfig->{'pri_connection_type'} eq 'R2' ) {
166 my $idle_bits = $gconfig->{'r2_idle_bits'};
167 printf "cas=%s:$idle_bits\n", Dahdi::Config::Gen::bchan_range($span);
168 printf "dchan=%d\n", $span->dchan()->num();
169 }
170 } else {
171 die "Digital span $num is not BRI, nor PRI?";
172 }
173 print_echo_can($gconfig, Dahdi::Config::Gen::bchan_range($span));
174 }
175
176 sub gen_signalling($$) {
177 my $gconfig = shift || die;
178 my $chan = shift || die;
179 my $type = $chan->type;
180 my $num = $chan->num;
181
182 die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
183 if($type eq 'EMPTY') {
184 printf "# channel %d, %s, no module.\n", $num, $chan->fqn;
185 return;
186 }
187 my $signalling = $gconfig->{'dahdi_signalling'};
188 my $sig = $signalling->{$type} || die "unknown default dahdi signalling for chan $num type $type";
189 if ($type eq 'IN') {
190 printf "# astbanktype: input\n";
191 } elsif ($type eq 'OUT') {
192 printf "# astbanktype: output\n";
193 }
194 printf "$sig=$num\n";
195 print_echo_can($gconfig, $num);
196 }
197
198 sub generate($$$) {
199 my $self = shift || die;
200 my $file = $self->{FILE};
201 my $gconfig = $self->{GCONFIG};
202 my $genopts = $self->{GENOPTS};
203 my @spans = @_;
204 warn "Empty configuration -- no spans\n" unless @spans;
205 rename "$file", "$file.bak"
206 or $! == 2 # ENOENT (No dependency on Errno.pm)
207 or die "Failed to backup old config: $!\n";
208 #$gconfig->dump;
209 print "Generating $file\n" if $genopts->{verbose};
210 open(F, ">$file") || die "$0: Failed to open $file: $!\n";
211 my $old = select F;
212 printf "# Autogenerated by $0 on %s\n", scalar(localtime);
213 print "# If you edit this file and execute $0 again,\n";
214 print "# your manual changes will be LOST.\n";
215 print <<"HEAD";
216 # Dahdi Configuration File
217 #
218 # This file is parsed by the Dahdi Configurator, dahdi_cfg
219 #
220 HEAD
221 foreach my $span (@spans) {
222 printf "# Span %d: %s %s\n", $span->num, $span->name, $span->description;
223 if($span->is_digital) {
224 if($span->is_pri) {
225 if($gconfig->{'pri_connection_type'} eq 'CAS') {
226 $self->gen_t1_cas($gconfig, $span);
227 } else {
228 $self->gen_digital($gconfig, $span);
229 }
230 } elsif($span->is_bri) {
231 $self->gen_digital($gconfig, $span);
232 }
233 } else {
234 foreach my $chan ($span->chans()) {
235 if(1 || !defined $chan->type) {
236 my $type = $chan->probe_type;
237 my $num = $chan->num;
238 die "Failed probing type for channel $num"
239 unless defined $type;
240 $chan->type($type);
241 }
242 gen_signalling($gconfig, $chan);
243 }
244 }
245 print "\n";
246 }
247 print <<"TAIL";
248 # Global data
249
250 loadzone = $gconfig->{'loadzone'}
251 defaultzone = $gconfig->{'defaultzone'}
252 TAIL
253 close F;
254 select $old;
255 }
256
257 1;
258
259 __END__
260
261 =head1 NAME
262
263 dahdi - Generate configuration for dahdi drivers.
264
265 =head1 SYNOPSIS
266
267 use Dahdi::Config::Gen::Dahdi;
268
269 my $cfg = new Dahdi::Config::Gen::Dahdi(\%global_config, \%genopts);
270 $cfg->generate(@span_list);
271
272 =head1 DESCRIPTION
273
274 Generate the F</etc/dahdi/system.conf>.
275 This is the configuration for dahdi_cfg(1).
276
277 Its location may be overriden via the environment variable F<DAHDI_CONF_FILE>.