From 66ca50e198ed2989b97293fac59de30f1be48bd3 Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Fri, 13 Feb 2009 21:09:51 +0000 Subject: Rework dahdi_genconf to have separate configuration generation modules. 'dahdi_genconf foo bar' will use the modules Dahdi::Config::Gen::Foo and Dahdi::Config::Gen::Bar to generate configuraion files. Extra formats can thus be added without modifying dahdi_genconf and independently of DAHDI. git-svn-id: http://svn.asterisk.org/svn/dahdi/tools/trunk@6013 a0bf4364-ded3-4de4-8d8a-66a801d63aff --- xpp/perl_modules/Dahdi/Config/Gen/System.pm | 158 ++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 xpp/perl_modules/Dahdi/Config/Gen/System.pm (limited to 'xpp/perl_modules/Dahdi/Config/Gen/System.pm') diff --git a/xpp/perl_modules/Dahdi/Config/Gen/System.pm b/xpp/perl_modules/Dahdi/Config/Gen/System.pm new file mode 100644 index 0000000..17fb802 --- /dev/null +++ b/xpp/perl_modules/Dahdi/Config/Gen/System.pm @@ -0,0 +1,158 @@ +package Dahdi::Config::Gen::System; +use strict; + +use Dahdi::Config::Gen qw(is_true); + +sub new($$$) { + my $pack = shift || die; + my $gconfig = shift || die; + my $genopts = shift || die; + my $file = $ENV{DAHDI_CONF_FILE} || "/etc/dahdi/system.conf"; + my $self = { + FILE => $file, + GCONFIG => $gconfig, + GENOPTS => $genopts, + }; + bless $self, $pack; + return $self; +} + +my $bri_te_last_timing = 1; + +sub gen_digital($$) { + my $gconfig = shift || die; + 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" : ''; + # "MFC/R2 does not normally use CRC4" + # FIXME: a finer way to override: + if ($gconfig->{'pri_connection_type'} eq 'R2') { + $span_crc4 = ''; + $framing = 'cas'; + } + my $dchan_type = 'dchan'; + if ($span->is_bri() && is_true($gconfig->{'bri_hardhdlc'})) { + $dchan_type = 'hardhdlc'; + } + + $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); + if ($gconfig->{'pri_connection_type'} eq 'PRI') { + printf "bchan=%s\n", Dahdi::Config::Gen::bchan_range($span); + my $dchan = $span->dchan(); + printf "$dchan_type=%d\n", $dchan->num(); + } elsif ($gconfig->{'pri_connection_type'} eq 'R2' ) { + my $idle_bits = $gconfig->{'r2_idle_bits'}; + printf "cas=%s:$idle_bits\n", Dahdi::Config::Gen::bchan_range($span); + printf "dchan=%d\n", $span->dchan()->num(); + } +} + +sub gen_signalling($$) { + my $gconfig = shift || die; + 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 $signalling = $gconfig->{'dahdi_signalling'}; + my $sig = $signalling->{$type} || die "unknown default dahdi 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"; +} + +sub generate($$$) { + my $self = shift || die; + my $file = $self->{FILE}; + my $gconfig = $self->{GCONFIG}; + my $genopts = $self->{GENOPTS}; + my @spans = @_; + warn "Empty configuration -- no spans\n" unless @spans; + rename "$file", "$file.bak" + or $! == 2 # ENOENT (No dependency on Errno.pm) + or die "Failed to backup old config: $!\n"; + #Dahdi::Config::Gen::show_gconfig($gconfig); + print "Generating $file\n" if $genopts->{verbose}; + 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"; +# Dahdi Configuration File +# +# This file is parsed by the Dahdi Configurator, dahdi_cfg +# +HEAD + foreach my $span (@spans) { + printf "# Span %d: %s %s\n", $span->num, $span->name, $span->description; + if($span->is_digital()) { + gen_digital($gconfig, $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_signalling($gconfig, $chan); + } + } + print "\n"; + } + print <<"TAIL"; +# Global data + +loadzone = $gconfig->{'loadzone'} +defaultzone = $gconfig->{'defaultzone'} +TAIL + close F; + select $old; +} + +1; + +__END__ + +=head1 NAME + +dahdi - Generate configuration for dahdi drivers. + +=head1 SYNOPSIS + + use Dahdi::Config::Gen::Dahdi; + + my $cfg = new Dahdi::Config::Gen::Dahdi(\%global_config, \%genopts); + $cfg->generate(@span_list); + +=head1 DESCRIPTION + +Generate the F. +This is the configuration for dahdi_cfg(1). + +Its location may be overriden via the environment variable F. -- cgit v1.2.3