| 1 |
#! /usr/bin/perl -w |
| 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 File::Basename; |
| 12 |
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/perl_modules"); } |
| 13 |
|
| 14 |
use Getopt::Long; |
| 15 |
use Dahdi; |
| 16 |
use Dahdi::Xpp; |
| 17 |
use Dahdi::Config::Gen; |
| 18 |
use Dahdi::Config::Params; |
| 19 |
|
| 20 |
Getopt::Long::Configure ("bundling"); |
| 21 |
|
| 22 |
my $version = '1'; # Functionality version (integer) |
| 23 |
my $revision = '$Revision$'; |
| 24 |
|
| 25 |
my %opts; |
| 26 |
|
| 27 |
sub usage { |
| 28 |
warn "Usage: $0 [options] <generators>\n"; |
| 29 |
warn " Options:\n"; |
| 30 |
warn " --line-mode=<E1|T1|J1> - Also generate span-types.conf with default line mode\n"; |
| 31 |
warn " -F|--freepbx - Modify configuration for FreePBX (skip FXS channels)\n"; |
| 32 |
warn " -v|--verbose - Be verbose, show generated files\n"; |
| 33 |
warn " -V|--version - Show version and exit\n"; |
| 34 |
warn " -h|--help - Show this message\n"; |
| 35 |
exit 1; |
| 36 |
} |
| 37 |
|
| 38 |
sub set_defaults { |
| 39 |
my $default_file = $ENV{GENCONF_PARAMETERS} || "/etc/dahdi/genconf_parameters"; |
| 40 |
my $params = Dahdi::Config::Params->new($default_file); |
| 41 |
#$params->dump; |
| 42 |
if($opts{v}) { |
| 43 |
print "Default parameters from ", $params->{GENCONF_FILE}, "\n"; |
| 44 |
} |
| 45 |
my $gconfig = Dahdi::Config::Gen->new($params); |
| 46 |
#$gconfig->dump; |
| 47 |
return $gconfig; |
| 48 |
} |
| 49 |
|
| 50 |
sub spans_prep($@) { |
| 51 |
my $gconfig = shift || die; |
| 52 |
my @spans = @_; |
| 53 |
foreach my $span (@spans) { |
| 54 |
if($span->is_pri || $span->is_bri) { |
| 55 |
$span->pri_set_fromconfig($gconfig); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
sub munge_spantypes { |
| 61 |
if ($opts{'line-mode'}) { |
| 62 |
print "Will generate span-types.conf with line-mode=$opts{'line-mode'}\n" |
| 63 |
if $opts{'verbose'}; |
| 64 |
return "spantypes=line-mode=$opts{'line-mode'}"; |
| 65 |
} else { |
| 66 |
print "Will generate span-types.conf\n" if $opts{'verbose'}; |
| 67 |
return "spantypes"; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
sub generator_list($) { |
| 72 |
my $gconfig = shift || die; |
| 73 |
my @genlist; |
| 74 |
|
| 75 |
if (@ARGV) { |
| 76 |
for my $gen (@ARGV) { |
| 77 |
$gen = munge_spantypes() if $gen eq 'spantypes'; |
| 78 |
push @genlist, $gen; |
| 79 |
} |
| 80 |
} else { |
| 81 |
# No files given. Use the defaults. |
| 82 |
@genlist = ('assignedspans', 'system', 'chandahdi'); |
| 83 |
if($gconfig->{'pri_connection_type'} eq 'R2') { |
| 84 |
push @genlist, 'unicall'; |
| 85 |
} |
| 86 |
push(@genlist, munge_spantypes()) if $opts{'line-mode'}; |
| 87 |
} |
| 88 |
return @genlist; |
| 89 |
} |
| 90 |
|
| 91 |
sub parse_genopts($) { |
| 92 |
my $optstr = shift; |
| 93 |
my %genopts; |
| 94 |
|
| 95 |
$optstr = '' unless defined $optstr; |
| 96 |
foreach my $o (split(/,/, $optstr)) { |
| 97 |
my ($k, $v) = split(/=/, $o, 2); |
| 98 |
$v = 1 unless defined $v and $v; |
| 99 |
$genopts{$k} = $v; |
| 100 |
} |
| 101 |
return %genopts; |
| 102 |
} |
| 103 |
|
| 104 |
sub generate_files($@) { |
| 105 |
my $gconfig = shift || die; |
| 106 |
my @spans = @_; |
| 107 |
my @generators = generator_list($gconfig); |
| 108 |
|
| 109 |
for my $gen (@generators) { |
| 110 |
my ($name, $optstr) = split(/=/, $gen, 2); |
| 111 |
die "Illegal name '$name'\n" unless $name =~ /^\w+$/; |
| 112 |
$name =~ s/(.)(.*)/\u$1\L$2/; |
| 113 |
my %genopts = parse_genopts($optstr); |
| 114 |
$genopts{'freepbx'} = 'yes' if $opts{'F'}; |
| 115 |
if(defined $opts{'v'}) { |
| 116 |
$genopts{'verbose'} = $opts{v}; |
| 117 |
} |
| 118 |
$gconfig->run_generator($name, \%genopts, @spans); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
GetOptions(\%opts, |
| 123 |
"line-mode=s", |
| 124 |
"h|help", |
| 125 |
"v|verbose", |
| 126 |
"V|version", |
| 127 |
"F|freepbx", |
| 128 |
) or usage; |
| 129 |
|
| 130 |
usage if $opts{h}; |
| 131 |
|
| 132 |
if($opts{'V'}) { |
| 133 |
my $revstr = $revision; |
| 134 |
$revstr =~ s/[^$]*\$[^:]+:\s*//; |
| 135 |
$revstr =~ s/\s*\$.*//; |
| 136 |
print "$0: version=$version revision=$revstr\n"; |
| 137 |
exit 0; |
| 138 |
} |
| 139 |
|
| 140 |
my $gconfig = set_defaults; |
| 141 |
my @spans = Dahdi::spans(); |
| 142 |
spans_prep($gconfig, @spans); |
| 143 |
generate_files($gconfig, @spans); |
| 144 |
|
| 145 |
__END__ |
| 146 |
|
| 147 |
=head1 NAME |
| 148 |
|
| 149 |
dahdi_genconf - Generate configuration for Dahdi channels. |
| 150 |
|
| 151 |
=head1 SYNOPSIS |
| 152 |
|
| 153 |
dahdi_genconf [options] [generator...] |
| 154 |
|
| 155 |
=head1 DESCRIPTION |
| 156 |
|
| 157 |
This script generate configuration files for Dahdi hardware. |
| 158 |
It uses two information sources: |
| 159 |
|
| 160 |
=over 4 |
| 161 |
|
| 162 |
=item Hardware |
| 163 |
|
| 164 |
The actual Dahdi hardware is automatically detected on the host. |
| 165 |
|
| 166 |
=item /etc/dahdi/genconf_parameters |
| 167 |
|
| 168 |
A configuration file that supplements the hardware information. |
| 169 |
Its location may be overridden via the C<GENCONF_PARAMETERS> environment |
| 170 |
variable. |
| 171 |
|
| 172 |
=back |
| 173 |
|
| 174 |
The dahdi_genconf script can generate various kinds of configuration files |
| 175 |
as specified by the generator arguments. Each generator is a perl class |
| 176 |
in Dahdi::Config::Gen namespace. The generator names on the command line |
| 177 |
are the class names in lowercase. |
| 178 |
|
| 179 |
The following generators are currently implemented: system, modules, spantypes, |
| 180 |
assignedspans, chandahdi, unicall, users. |
| 181 |
|
| 182 |
For further documentation on each, please user perldoc on the relevant |
| 183 |
class. E.g: C<perldoc Dahdi::Config::Gen::Chandahdi> |
| 184 |
|
| 185 |
Each generator on the command line may be passed custom options by assigning |
| 186 |
a comma separated list of options to the generator name. E.g: |
| 187 |
|
| 188 |
dahdi_genconf system chandahdi=verbose unicall |
| 189 |
|
| 190 |
=head2 Global options: |
| 191 |
|
| 192 |
=over 4 |
| 193 |
|
| 194 |
=item -V --version |
| 195 |
|
| 196 |
Version -- print version string and exit. |
| 197 |
|
| 198 |
=item -v --verbose |
| 199 |
|
| 200 |
Verbose -- sets the C<'verbose'> option for all generators. |
| 201 |
|
| 202 |
=item -F --freepbx |
| 203 |
|
| 204 |
FreePBX -- sets the C<'freepbx'> option for all generators. |
| 205 |
Currently, chandahdi is affected. |
| 206 |
|
| 207 |
=item --line-mode=I<mode> |
| 208 |
|
| 209 |
I<mode> may be E1, J1 or T1. |
| 210 |
|
| 211 |
Enables the generator B<spantypes> and the option B<line-mode> to it. |
| 212 |
(Equivalent to the option C<--line-mode> to C<dahdi_span_types>). This |
| 213 |
will generate a C<span-types.conf> file with a single wildcard line |
| 214 |
setting the line mode to I<mode>. |
| 215 |
|
| 216 |
=back |
| 217 |
|
| 218 |
=head2 Implementation notes: |
| 219 |
|
| 220 |
=over 4 |
| 221 |
|
| 222 |
=item * |
| 223 |
|
| 224 |
F<genconf_parameters> parsing is done via C<Dahdi::Config::Params>. |
| 225 |
An object representing the parsed data is instantiated by: |
| 226 |
C<Dahdi::Config::Params-E<gt>new()>. |
| 227 |
The C<item()> method of this object contains all the hard coded |
| 228 |
defaults of the configuration directives. |
| 229 |
|
| 230 |
=item * |
| 231 |
|
| 232 |
A configuration object is instantiated by C<Dahdi::Config::Gen-E<gt>new($params)>. |
| 233 |
The mapping of configuration directives into semantic configuration is |
| 234 |
done in the constructor. |
| 235 |
|
| 236 |
=item * |
| 237 |
|
| 238 |
A single generator is run via the the C<run_generator()> method of the |
| 239 |
configuration object. |
| 240 |
|
| 241 |
=back |