| 1 |
package Dahdi::Config::Gen::Chandahdi; |
| 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{CHAN_DAHDI_CHANNELS_FILE} || "/etc/asterisk/dahdi-channels.conf"; |
| 11 |
my $self = { |
| 12 |
FILE => $file, |
| 13 |
GCONFIG => $gconfig, |
| 14 |
GENOPTS => $genopts, |
| 15 |
}; |
| 16 |
bless $self, $pack; |
| 17 |
return $self; |
| 18 |
} |
| 19 |
|
| 20 |
# Since chan_dahdi definitions "leak" to the next ones, we try |
| 21 |
# To reset some important definitions to their chan_dahdi defaults. |
| 22 |
my %chan_dahdi_defaults = ( |
| 23 |
context => 'default', |
| 24 |
group => '63', # FIXME: should not be needed. |
| 25 |
overlapdial => 'no', |
| 26 |
busydetect => 'no', |
| 27 |
rxgain => 0, |
| 28 |
txgain => 0, |
| 29 |
); |
| 30 |
|
| 31 |
sub reset_chandahdi_values { |
| 32 |
foreach my $arg (@_) { |
| 33 |
if (exists $chan_dahdi_defaults{$arg}) { |
| 34 |
print "$arg = $chan_dahdi_defaults{$arg}\n"; |
| 35 |
} else { |
| 36 |
print "$arg =\n"; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
sub gen_openr2($$$) { |
| 42 |
my $self = shift || die; |
| 43 |
my $gconfig = shift || die; |
| 44 |
my $span = shift || die; |
| 45 |
my $num = $span->num() || die; |
| 46 |
my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n"; |
| 47 |
my $type = $span->type; |
| 48 |
# Fake type for signalling |
| 49 |
my $faketype = ($termtype eq 'TE') ? 'E1_TE' : 'E1_TE'; |
| 50 |
my $group = $gconfig->{'group'}{"$type"}; |
| 51 |
die "$0: missing default group (termtype=$termtype)\n" unless defined($group); |
| 52 |
my $context = $gconfig->{'context'}{"$faketype"}; |
| 53 |
die "$0: missing default context\n" unless $context; |
| 54 |
my @to_reset = qw/context group/; |
| 55 |
my $chans = Dahdi::Config::Gen::bchan_range($span); |
| 56 |
$group .= "," . (10 + $num); # Invent unique group per span |
| 57 |
my $country = $gconfig->{'loadzone'}; |
| 58 |
my @valid_countries = qw( ar br cn cz co ec itu mx ph ve ); |
| 59 |
die "Country '$country' is invalid for R2. Use one of: @valid_countries\n" |
| 60 |
unless grep { $_ eq $country } @valid_countries; |
| 61 |
printf "group=$group\n"; |
| 62 |
printf "context=$context\n"; |
| 63 |
printf "switchtype = %s\n", $span->switchtype; |
| 64 |
printf "signalling = %s\n", 'mfcr2'; |
| 65 |
printf "caller = %s\n", ($termtype eq 'TE') ? 'no' : 'yes'; |
| 66 |
printf "mfcr2_logdir = span%d\n", $span->num; |
| 67 |
print <<"EOF"; |
| 68 |
mfcr2_variant=$country |
| 69 |
mfcr2_get_ani_first=no |
| 70 |
mfcr2_max_ani=10 |
| 71 |
mfcr2_max_dnis=4 |
| 72 |
mfcr2_category=national_subscriber |
| 73 |
mfcr2_call_files=yes |
| 74 |
mfcr2_logging=all |
| 75 |
mfcr2_mfback_timeout=-1 |
| 76 |
mfcr2_metering_pulse_timeout=-1 |
| 77 |
EOF |
| 78 |
printf "channel => %s\n", $chans; |
| 79 |
|
| 80 |
reset_chandahdi_values(@to_reset); |
| 81 |
} |
| 82 |
|
| 83 |
sub gen_cas($$$) { |
| 84 |
my $self = shift || die; |
| 85 |
my $gconfig = shift || die; |
| 86 |
my $span = shift || die; |
| 87 |
my $num = $span->num() || die; |
| 88 |
my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n"; |
| 89 |
my $type = $span->type; |
| 90 |
my $group = $gconfig->{'group'}{"$type"}; |
| 91 |
die "$0: missing default group (termtype=$termtype)\n" unless defined($group); |
| 92 |
my $context = $gconfig->{'context'}{"$type"}; |
| 93 |
die "$0: missing default context\n" unless $context; |
| 94 |
# Fake type for signalling |
| 95 |
my $faketype = ($termtype eq 'TE') ? 'FXO' : 'FXS'; |
| 96 |
my $sig = $gconfig->{'chan_dahdi_signalling'}{$faketype}; |
| 97 |
my $em_signalling = $gconfig->{'em_signalling'}; |
| 98 |
if ($em_signalling ne 'none') { |
| 99 |
$sig = $em_signalling; |
| 100 |
# FIXME: but we don't handle E1 yet |
| 101 |
$sig = 'em_e1' if $span->proto eq 'E1'; |
| 102 |
} |
| 103 |
my @to_reset = qw/context group/; |
| 104 |
my $chans = Dahdi::Config::Gen::chan_range($span->chans()); |
| 105 |
$group .= "," . (10 + $num); # Invent unique group per span |
| 106 |
printf "group=$group\n"; |
| 107 |
printf "context=$context\n"; |
| 108 |
printf "switchtype = %s\n", $span->switchtype; |
| 109 |
printf "signalling = %s\n", $sig; |
| 110 |
printf "channel => %s\n", $chans; |
| 111 |
reset_chandahdi_values(@to_reset); |
| 112 |
} |
| 113 |
|
| 114 |
sub gen_digital($$$) { |
| 115 |
my $self = shift || die; |
| 116 |
my $gconfig = shift || die; |
| 117 |
my $span = shift || die; |
| 118 |
my $num = $span->num() || die; |
| 119 |
die "Span #$num is analog" unless $span->is_digital(); |
| 120 |
if($span->is_pri && $gconfig->{'pri_connection_type'} eq 'R2') { |
| 121 |
printf "; Skipped: $gconfig->{'pri_connection_type'}\n\n"; |
| 122 |
return; |
| 123 |
} |
| 124 |
my $type = $span->type() || die "$0: Span #$num -- unkown type\n"; |
| 125 |
my $termtype = $span->termtype() || die "$0: Span #$num -- unkown termtype [NT/TE]\n"; |
| 126 |
my $group = $gconfig->{'group'}{"$type"}; |
| 127 |
my $context = $gconfig->{'context'}{"$type"}; |
| 128 |
my @to_reset = qw/context group/; |
| 129 |
|
| 130 |
die "$0: missing default group (termtype=$termtype)\n" unless defined($group); |
| 131 |
die "$0: missing default context\n" unless $context; |
| 132 |
|
| 133 |
my $sig = $span->signalling || die "missing signalling info for span #$num type $type"; |
| 134 |
grep($gconfig->{'bri_sig_style'} eq $_, 'bri', 'bri_ptmp', 'pri') or die "unknown signalling style for BRI"; |
| 135 |
if($span->is_bri() and $gconfig->{'bri_sig_style'} eq 'bri_ptmp') { |
| 136 |
$sig .= '_ptmp'; |
| 137 |
} |
| 138 |
if ($span->is_bri() && $termtype eq 'NT' && is_true($gconfig->{'brint_overlap'})) { |
| 139 |
print "overlapdial = yes\n"; |
| 140 |
push(@to_reset, qw/overlapdial/); |
| 141 |
} |
| 142 |
|
| 143 |
$group .= "," . (10 + $num); # Invent unique group per span |
| 144 |
printf "group=$group\n"; |
| 145 |
printf "context=$context\n"; |
| 146 |
printf "switchtype = %s\n", $span->switchtype; |
| 147 |
printf "signalling = %s\n", $sig; |
| 148 |
printf "channel => %s\n", Dahdi::Config::Gen::bchan_range($span); |
| 149 |
reset_chandahdi_values(@to_reset); |
| 150 |
} |
| 151 |
|
| 152 |
sub gen_channel($$) { |
| 153 |
my $self = shift || die; |
| 154 |
my $chan = shift || die; |
| 155 |
my $gconfig = $self->{GCONFIG}; |
| 156 |
my $type = $chan->type; |
| 157 |
my $num = $chan->num; |
| 158 |
die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital(); |
| 159 |
my $exten = $gconfig->{'base_exten'} + $num; |
| 160 |
my $sig = $gconfig->{'chan_dahdi_signalling'}{$type}; |
| 161 |
my $context = $gconfig->{'context'}{$type}; |
| 162 |
my $group = $gconfig->{'group'}{$type}; |
| 163 |
my $callerid; |
| 164 |
my $immediate; |
| 165 |
|
| 166 |
return if $type eq 'EMPTY'; |
| 167 |
die "missing default_chan_dahdi_signalling for chan #$num type $type" unless $sig; |
| 168 |
die "missing context for chan #$num type $type" unless $context; |
| 169 |
$callerid = ($type eq 'FXO') |
| 170 |
? 'asreceived' |
| 171 |
: sprintf "\"Channel %d\" <%d>", $num, $exten; |
| 172 |
if($type eq 'IN') { |
| 173 |
$immediate = 'yes'; |
| 174 |
} |
| 175 |
# FIXME: $immediage should not be set for 'OUT' channels, but meanwhile |
| 176 |
# it's better to be compatible with genzaptelconf |
| 177 |
$immediate = 'yes' if $gconfig->{'fxs_immediate'} eq 'yes' and $sig =~ /^fxo_/; |
| 178 |
my $signalling = $chan->signalling; |
| 179 |
$signalling = " " . $signalling if $signalling; |
| 180 |
my $info = $chan->info; |
| 181 |
$info = " " . $info if $info; |
| 182 |
printf ";;; line=\"%d %s%s%s\"\n", $num, $chan->fqn, $signalling, $info; |
| 183 |
printf "signalling=$sig\n"; |
| 184 |
printf "callerid=$callerid\n"; |
| 185 |
printf "mailbox=%d\n", $exten unless $type eq 'FXO'; |
| 186 |
if(defined $group) { |
| 187 |
printf "group=$group\n"; |
| 188 |
} |
| 189 |
printf "context=$context\n"; |
| 190 |
printf "immediate=$immediate\n" if defined $immediate; |
| 191 |
printf "channel => %d\n", $num; |
| 192 |
# Reset following values to default |
| 193 |
printf "callerid=\n"; |
| 194 |
printf "mailbox=\n" unless $type eq 'FXO'; |
| 195 |
if(defined $group) { |
| 196 |
printf "group=\n"; |
| 197 |
} |
| 198 |
printf "context=default\n"; |
| 199 |
printf "immediate=no\n" if defined $immediate; |
| 200 |
print "\n"; |
| 201 |
} |
| 202 |
|
| 203 |
sub generate($) { |
| 204 |
my $self = shift || die; |
| 205 |
my $file = $self->{FILE}; |
| 206 |
my $gconfig = $self->{GCONFIG}; |
| 207 |
my $genopts = $self->{GENOPTS}; |
| 208 |
#$gconfig->dump; |
| 209 |
my @spans = @_; |
| 210 |
warn "Empty configuration -- no spans\n" unless @spans; |
| 211 |
rename "$file", "$file.bak" |
| 212 |
or $! == 2 # ENOENT (No dependency on Errno.pm) |
| 213 |
or die "Failed to backup old config: $!\n"; |
| 214 |
print "Generating $file\n" if $genopts->{verbose}; |
| 215 |
open(F, ">$file") || die "$0: Failed to open $file: $!\n"; |
| 216 |
my $old = select F; |
| 217 |
printf "; Autogenerated by $0 on %s\n", scalar(localtime); |
| 218 |
print "; If you edit this file and execute $0 again,\n"; |
| 219 |
print "; your manual changes will be LOST.\n"; |
| 220 |
print <<"HEAD"; |
| 221 |
; Dahdi Channels Configurations (chan_dahdi.conf) |
| 222 |
; |
| 223 |
; This is not intended to be a complete chan_dahdi.conf. Rather, it is intended |
| 224 |
; to be #include-d by /etc/chan_dahdi.conf that will include the global settings |
| 225 |
; |
| 226 |
|
| 227 |
HEAD |
| 228 |
foreach my $span (@spans) { |
| 229 |
printf "; Span %d: %s %s\n", $span->num, $span->name, $span->description; |
| 230 |
if($span->is_digital) { |
| 231 |
if($span->is_pri) { |
| 232 |
if($gconfig->{'pri_connection_type'} eq 'R2') { |
| 233 |
$self->gen_openr2($gconfig, $span); |
| 234 |
} elsif($gconfig->{'pri_connection_type'} eq 'CAS') { |
| 235 |
$self->gen_cas($gconfig, $span); |
| 236 |
} else { |
| 237 |
$self->gen_digital($gconfig, $span); |
| 238 |
} |
| 239 |
} elsif($span->is_bri) { |
| 240 |
$self->gen_digital($gconfig, $span); |
| 241 |
} |
| 242 |
} else { |
| 243 |
foreach my $chan ($span->chans()) { |
| 244 |
if(is_true($genopts->{'freepbx'}) || is_true($gconfig->{'freepbx'})) { |
| 245 |
# Freepbx has its own idea about channels |
| 246 |
my $type = $chan->type; |
| 247 |
if($type eq 'FXS' || $type eq 'OUT' || $type eq 'IN') { |
| 248 |
printf "; Skip channel=%s($type) -- freepbx option.\n", |
| 249 |
$chan->num; |
| 250 |
next; |
| 251 |
} |
| 252 |
} |
| 253 |
$self->gen_channel($chan); |
| 254 |
} |
| 255 |
} |
| 256 |
print "\n"; |
| 257 |
} |
| 258 |
close F; |
| 259 |
select $old; |
| 260 |
} |
| 261 |
|
| 262 |
1; |
| 263 |
|
| 264 |
__END__ |
| 265 |
|
| 266 |
=head1 NAME |
| 267 |
|
| 268 |
chandahdi - Generate configuration for chan_dahdi channels. |
| 269 |
|
| 270 |
=head1 SYNOPSIS |
| 271 |
|
| 272 |
use Dahdi::Config::Gen::Chandahdi; |
| 273 |
|
| 274 |
my $cfg = new Dahdi::Config::Gen::Chandahdi(\%global_config, \%genopts); |
| 275 |
$cfg->generate(@span_list); |
| 276 |
|
| 277 |
=head1 DESCRIPTION |
| 278 |
|
| 279 |
Generate the F</etc/asterisk/dahdi-channels.conf> |
| 280 |
This is used as a configuration for asterisk(1). |
| 281 |
It should be included in the main F</etc/asterisk/chan_dahdi.conf>. |
| 282 |
|
| 283 |
Its location may be overriden via the environment variable |
| 284 |
C<CHAN_DAHDI_CHANNELS_FILE>. |
| 285 |
|
| 286 |
=head1 OPTIONS |
| 287 |
|
| 288 |
=over 4 |
| 289 |
|
| 290 |
=item freepbx |
| 291 |
|
| 292 |
With this option we do not generate channel definitions for FXS, Input and |
| 293 |
Output ports. This is done because these channel definitions need to be |
| 294 |
generated and inserted into I<freepbx> database anyway. |
| 295 |
|
| 296 |
=back |
| 297 |
|
| 298 |
The I<freepbx> option may be activated also by adding a C<freepbx yes> line |
| 299 |
to the C<genconf_parameters> file. |