Debian Codegraph @

xpp/perl_modules/Dahdi/Span.pm

raw
1 package Dahdi::Span;
2 #
3 # Written by Oron Peled <[email protected]>
4 # Copyright (C) 2007, Xorcom
5 # This program is free software; you can redistribute and/or
6 # modify it under the same terms as Perl itself.
7 #
8 # $Id$
9 #
10 use strict;
11 use Dahdi::Utils;
12 use Dahdi::Chans;
13
14 =head1 NAME
15
16 Dahdi::Spans - Perl interface to a Dahdi span information
17
18 This package allows access from perl to information about a Dahdi
19 channel. It is part of the Dahdi Perl package.
20
21 A span is a logical unit of Dahdi channels. Normally a port in a
22 digital card or a whole analog card.
23
24 See documentation of module L<Dahdi> for usage example. Specifically
25 C<Dahdi::spans()> must be run initially.
26
27 =head1 by_number()
28
29 Get a span by its Dahdi span number.
30
31 =head1 Span Properties
32
33 =head2 num()
34
35 The span number.
36
37 =head2 name()
38
39 The name field of a Dahdi span. E.g.:
40
41 TE2/0/1
42
43 =head2 description()
44
45 The description field of the span. e.g:
46
47 "T2XXP (PCI) Card 0 Span 1" HDB3/CCS/CRC4 RED
48
49 =head2 chans()
50
51 The list of the channels (L<Dahdi::Chan> objects) of this span.
52 In a scalar context returns the number of channels this span has.
53
54 =head2 bchans()
55
56 Likewise a list of bchannels (or a count in a scalar context).
57
58 =head2 is_sync_master()
59
60 Is this span the source of timing for Dahdi?
61
62 =head2 type()
63
64 Type of span, or "UNKNOWN" if could not be detected. Current known
65 types:
66
67 BRI_TE, BRI_NT, E1_TE, E1_NT, J1_TE, J1_NT, T1_TE, T1_NT, FXS, FXO
68
69 =head2 is_pri()
70
71 Is this an E1/J1/T1 span?
72
73 =head2 is_bri()
74
75 Is this a BRI span?
76
77 =head2 is_digital()
78
79 Is this a digital (as opposed to analog) span?
80
81 =head2 termtype()
82
83 Set for digital spans. "TE" or "NT". Will probably be assumed to be "TE"
84 if there's no information pointing either way.
85
86 =head2 coding()
87
88 Suggested sane coding type (e.g.: "hdb3", "b8zs") for this type of span.
89
90 =head2 framing()
91
92 Suggested sane framing type (e.g.: "ccs", "esf") for this type of span.
93
94 =head2 yellow(), crc4()
95
96 Likewise, suggestions ofr the respective fields in the span= line in
97 /etc/dahdi/system.conf for this span.
98
99 =head2 signalling()
100
101 Suggested chan_dahdi.conf signalling for channels of this span.
102
103 =head2 switchtype()
104
105 Suggested chan_dahdi.conf switchtype for channels of this span.
106
107 =head1 Note
108
109 Most of those properties are normally used as lower-case functions, but
110 actually set in the module as capital-letter propeties. To look at e.g.
111 "signalling" is set, look for "SIGNALLING".
112
113 =cut
114
115 sub chans($) {
116 my $span = shift;
117 return @{$span->{CHANS}};
118 }
119
120 sub by_number($) {
121 my $span_number = shift;
122 die "Missing span number" unless defined $span_number;
123 my @spans = Dahdi::spans();
124
125 my ($span) = grep { $_->num == $span_number } @spans;
126 return $span;
127 }
128
129 my @bri_strings = (
130 'BRI_(NT|TE)',
131 '(?:quad|octo)BRI PCI ISDN Card.* \[(NT|TE)\]',
132 'octoBRI \[(NT|TE)\] ',
133 'HFC-S PCI A ISDN.* \[(NT|TE)\] ',
134 '(B4XXP) \(PCI\) Card', # Use dahdi_scan to determine TE/NT mode
135 '(WCBRI)', # has selectable NT/TE modes via dahdi_cfg
136 );
137
138 my @pri_strings = (
139 'Tormenta 2 .*Quad (E1|T1)', # tor2.
140 'Xorcom XPD.*: (E1|T1)', # Astribank PRI
141 'Digium Wildcard .100P (T1|E1)/', # wct1xxp
142 'ISA Tormenta Span 1', # torisa
143 'TE110P T1/E1', # wcte11xp
144 'Wildcard TE120P', # wcte12xp
145 'Wildcard TE121', # wcte12xp
146 'Wildcard TE122', # wcte12xp
147 'Wildcard TE131/TE133', # wcte13xp
148 'Wildcard TE132/TE134', # wcte13xp
149 'T[248]XXP \(PCI\) Card ', # wct4xxp
150 'WCTE43X \(PCI\) Card ', # wcte43xp
151 'WCTE23X \(PCI\) Card ', # wcte43xp
152 'R[24]T1 \(PCI\) Card', # rxt1
153 'Rhino R1T1 (E1)/PRA Card', # r1t1
154 'Rhino R1T1 (T1)/PRI Card', # r1t1
155 'WP(E1|T1)/.* "wanpipe', # Sangoma E1/T1
156 );
157
158 my @soft_term_type_strings = (
159 'Xorcom XPD.*: (E1|T1)', # Astribank PRI
160 '(WCBRI)', # has selectable NT/TE modes via dahdi_cfg
161 );
162
163 our $DAHDI_BRI_NET = 'bri_net';
164 our $DAHDI_BRI_CPE = 'bri_cpe';
165
166 our $DAHDI_PRI_NET = 'pri_net';
167 our $DAHDI_PRI_CPE = 'pri_cpe';
168
169 sub init_proto($$) {
170 my $self = shift;
171 my $proto = shift;
172
173 $self->{PROTO} = $proto;
174 if($proto eq 'E1') {
175 $self->{DCHAN_IDX} = 15;
176 $self->{BCHAN_LIST} = [ 0 .. 14, 16 .. 30 ];
177 } elsif($proto eq 'T1') {
178 $self->{DCHAN_IDX} = 23;
179 $self->{BCHAN_LIST} = [ 0 .. 22 ];
180 }
181 $self->{TYPE} = "${proto}_$self->{TERMTYPE}";
182 }
183
184 sub get_digital_spantype {
185 my $span_no = shift;
186 my @lines = split /\n/, `dahdi_scan`;
187 my $found_span = 0;
188 foreach my $line (@lines) {
189 if (! $found_span) {
190 if ($line =~ m/\[$span_no\]/) {
191 $found_span = 1;
192 }
193 } else {
194 if ($line !~ m/^\[/) {
195 if ($line =~ m/digital-(TE|NT)/ ){
196 return $1;
197 }
198 } else {
199 $found_span = 0;
200 }
201 }
202 }
203 die "Cannot determine digital spantype";
204 }
205
206 sub new($$) {
207 my $pack = shift or die "Wasn't called as a class method\n";
208 my $proc_file = shift or die "Missing a proc file parameter\n";
209 $proc_file =~ m{[^/]*/(\d+)$};
210 my $num = $1 or die " Invalid span file name: $proc_file\n";
211 my $self = { NUM => $num };
212 bless $self, $pack;
213 $self->{TYPE} = "UNKNOWN";
214 open(F, "$proc_file") or die "Failed to open '$proc_file\n";
215 my $head = <F>;
216 chomp $head;
217 $self->{IS_DIGITAL} = 0;
218 $self->{IS_BRI} = 0;
219 $self->{IS_PRI} = 0;
220 $self->{TERMTYPE} = "UNKNOWN";
221 foreach my $cardtype (@bri_strings) {
222 if($head =~ m/$cardtype/) {
223 my $termtype = $1;
224 if ($1 eq 'B4XXP') {
225 $termtype = get_digital_spantype($num);
226 }
227 if ($1 eq 'WCBRI') {
228 $termtype = 'TE';
229 }
230 $self->{IS_DIGITAL} = 1;
231 $self->{IS_BRI} = 1;
232 $self->{TERMTYPE} = $termtype;
233 $self->{TYPE} = "BRI_$termtype";
234 $self->{DCHAN_IDX} = 2;
235 $self->{BCHAN_LIST} = [ 0, 1 ];
236 $self->init_proto('BRI');
237 last;
238 }
239 }
240 foreach my $cardtype (@pri_strings) {
241 if($head =~ m/$cardtype/) {
242 my @info;
243
244 push(@info, $1) if defined $1;
245 push(@info, $2) if defined $2;
246 my ($proto) = grep(/(E1|T1|J1)/, @info);
247 $proto = 'UNKNOWN' unless defined $proto;
248 my ($termtype) = grep(/(NT|TE)/, @info);
249 $termtype = 'UNKNOWN' unless defined $termtype;
250
251 $self->{IS_DIGITAL} = 1;
252 $self->{IS_PRI} = 1;
253 $self->{TERMTYPE} = $termtype;
254 $self->init_proto($proto);
255 last;
256 }
257 }
258 $self->{IS_SOFT_TERM_TYPE} = 0;
259 foreach my $cardtype (@soft_term_type_strings) {
260 if($head =~ m/$cardtype/) {
261 $self->{IS_SOFT_TERM_TYPE} = 1;
262 last;
263 }
264 }
265
266 if (($self->is_soft_term_type == 0) and ($self->termtype eq "UNKNOWN")) {
267 $self->{IS_SOFT_TERM_TYPE} = 1;
268 }
269
270 ($self->{NAME}, $self->{DESCRIPTION}) = (split(/\s+/, $head, 4))[2, 3];
271 $self->{IS_DAHDI_SYNC_MASTER} =
272 ($self->{DESCRIPTION} =~ /\(MASTER\)/) ? 1 : 0;
273 $self->{CHANS} = [];
274 my @channels;
275 my $index = 0;
276 my @channel_lines = <F>;
277 foreach (@channel_lines) {
278 chomp;
279 s/^\s*//;
280 s/\s*$//;
281 next unless /\S/;
282 next unless /^\s*\d+/; # must be a real channel string.
283 my $c = Dahdi::Chans->new($self, $index, $_);
284 push(@channels, $c);
285 $index++;
286 }
287 close F;
288 if($self->is_pri()) {
289 # Check for PRI with unknown type strings
290 if($index == 31) {
291 if($self->{PROTO} eq 'UNKNOWN') {
292 $self->init_proto('E1');
293 } elsif($self->{PROTO} ne 'E1') {
294 die "$index channels in a $self->{PROTO} span";
295 }
296 } elsif($index == 24) {
297 if($self->{PROTO} eq 'UNKNOWN') {
298 $self->init_proto('T1'); # FIXME: J1?
299 } elsif($self->{PROTO} ne 'T1') {
300 die "$index channels in a $self->{PROTO} span";
301 }
302 }
303 }
304 @channels = sort { $a->num <=> $b->num } @channels;
305 $self->{CHANS} = \@channels;
306 $self->{YELLOW} = undef;
307 $self->{CRC4} = undef;
308 $self->{SOFTNTTE} = undef;
309 $self->{TERMINATION} = undef;
310 if($self->is_bri()) {
311 $self->{CODING} = 'ami';
312 $self->{DCHAN} = ($self->chans())[$self->{DCHAN_IDX}];
313 $self->{BCHANS} = [ ($self->chans())[@{$self->{BCHAN_LIST}}] ];
314 # Infer some info from channel name:
315 my $first_chan = ($self->chans())[0] || die "$0: No channels in span #$num\n";
316 my $chan_fqn = $first_chan->fqn();
317 if($chan_fqn =~ m(ZTHFC.*/|ztqoz.*/|XPP_BRI_.*|B4/.*|WCBRI/.*)) { # BRI
318 if($chan_fqn =~ m(WCBRI/.*)) { # make sure to set termination resistors on hybrid cards
319 $self->{TERMINATION} = 'term';
320 $self->{SOFTNTTE} = 'te';
321 }
322 $self->{FRAMING} = 'ccs';
323 $self->{SWITCHTYPE} = 'euroisdn';
324 $self->{SIGNALLING} = ($self->{TERMTYPE} eq 'NT') ? $DAHDI_BRI_NET : $DAHDI_BRI_CPE ;
325 } elsif($chan_fqn =~ m(ztgsm.*/)) { # Junghanns's GSM cards.
326 $self->{FRAMING} = 'ccs';
327 $self->{SIGNALLING} = 'gsm';
328 }
329 }
330 if($self->is_pri()) {
331 $self->{DCHAN} = ($self->chans())[$self->{DCHAN_IDX}];
332 $self->{BCHANS} = [ ($self->chans())[@{$self->{BCHAN_LIST}}] ];
333 if($self->{PROTO} eq 'E1') {
334 $self->{CODING} = 'hdb3';
335 $self->{FRAMING} = 'ccs';
336 $self->{SWITCHTYPE} = 'euroisdn';
337 $self->{CRC4} = 'crc4';
338 } elsif($self->{PROTO} eq 'T1') {
339 $self->{CODING} = 'b8zs';
340 $self->{FRAMING} = 'esf';
341 $self->{SWITCHTYPE} = 'national';
342 } else {
343 die "'$self->{PROTO}' unsupported yet";
344 }
345 }
346 return $self;
347 }
348
349 sub bchans($) {
350 my $self = shift || die;
351
352 return @{$self->{BCHANS}};
353 }
354
355 sub set_termtype($$) {
356 my $span = shift || die;
357 my $termtype = shift || die;
358 $span->{TERMTYPE} = $termtype;
359 if ($span->is_pri) {
360 $span->{SIGNALLING} = ($termtype eq 'NT') ? $DAHDI_PRI_NET : $DAHDI_PRI_CPE ;
361 } elsif ($span->is_bri) {
362 $span->{SIGNALLING} = ($termtype eq 'NT') ? $DAHDI_BRI_NET : $DAHDI_BRI_CPE ;
363 }
364 $span->{TYPE} = $span->proto . "_$termtype";
365 }
366
367 sub pri_set_fromconfig($$) {
368 my $span = shift || die;
369 my $genconf = shift || die;
370 my $name = $span->name;
371 return unless $span->is_soft_term_type;
372 # if(defined $termtype) {
373 # die "Termtype for $name already defined as $termtype\n";
374 # }
375 my $pri_termtype = $genconf->{pri_termtype};
376 my @pri_specs;
377 if(defined $pri_termtype) {
378 @pri_specs = @{$pri_termtype};
379 }
380 push(@pri_specs , 'SPAN/* TE'); # Default
381 my @patlist = ( "SPAN/" . $span->num );
382 my $xpd = Dahdi::Xpp::xpd_of_span($span);
383 if(defined $xpd) {
384 my $xbus = $xpd->xbus;
385 my $xbus_name = $xbus->name;
386 my $xpd_name = "XPD-" . $xpd->id;
387 my $label = $xbus->label;
388 my $connector = $xbus->connector;
389 #print "DEBUG: '$xbus_name/$xpd_name' LABEL='$label' CONNECTOR='$connector'\n";
390 push(@patlist, "NUM/$xbus_name/$xpd_name");
391 push(@patlist, "LABEL/$label/$xpd_name");
392 push(@patlist, "CONNECTOR/$connector/$xpd_name");
393 }
394 #print STDERR "PATLIST=@patlist\n";
395 my $match_termtype;
396 SPEC:
397 for(my $i = 0; $i < @pri_specs; $i++) {
398 my $spec = $pri_specs[$i];
399 #print STDERR "spec: $spec\n";
400 my ($match, $termtype) = split(/\s+/, $spec);
401 next unless defined $match and defined $termtype;
402 # Convert "globs" to regex
403 $match =~ s/\*/.*/g;
404 $match =~ s/\?/./g;
405 #print STDERR "match: $match\n";
406 foreach my $pattern (@patlist) {
407 #print STDERR "testmatch: $pattern =~ $match\n";
408 if($pattern =~ /^$match$/) {
409 #print STDERR "MATCH '$pattern' ~ '$match' termtype=$termtype\n";
410 $match_termtype = $termtype;
411 last SPEC;
412 }
413 }
414 }
415 die "Unknown pri_termtype" unless defined $match_termtype;
416 $span->set_termtype($match_termtype);
417 }
418
419
420 1;