| 1 |
package Dahdi::Config::Gen::Xpporder; |
| 2 |
use strict; |
| 3 |
|
| 4 |
use Dahdi::Config::Gen qw(is_true); |
| 5 |
use Dahdi::Xpp; |
| 6 |
|
| 7 |
sub new($$$) { |
| 8 |
my $pack = shift || die; |
| 9 |
my $gconfig = shift || die; |
| 10 |
my $genopts = shift || die; |
| 11 |
my $file = $ENV{XPPORDER_CONF} || "/etc/dahdi/xpp_order"; |
| 12 |
my $self = { |
| 13 |
FILE => $file, |
| 14 |
GCONFIG => $gconfig, |
| 15 |
GENOPTS => $genopts, |
| 16 |
}; |
| 17 |
bless $self, $pack; |
| 18 |
return $self; |
| 19 |
} |
| 20 |
|
| 21 |
# |
| 22 |
# Returns list of xbuses sorted by the span numbers assigned |
| 23 |
# to their XPD's. Also checks that each XBUS span numbers are sequential. |
| 24 |
sub get_sorted_xbuses(@) { |
| 25 |
my @spans = @_; # Verify our spans |
| 26 |
my @xbuses = Dahdi::Xpp::xbuses; |
| 27 |
my %xbus_of_span; |
| 28 |
my %xbus_beginning; |
| 29 |
my %seen_spans; |
| 30 |
my @sorted_xbuses; |
| 31 |
foreach my $xbus (@xbuses) { |
| 32 |
my $last_spanno; |
| 33 |
foreach my $xpd (Dahdi::Xpp::Xpd::telephony_devs($xbus->xpds())) { |
| 34 |
my $spanno = $xpd->spanno; |
| 35 |
if(!$spanno) { |
| 36 |
printf STDERR "%s: Is not registered. Skipping.\n", $xpd->fqn; |
| 37 |
next; |
| 38 |
} |
| 39 |
$seen_spans{$spanno}++; |
| 40 |
if($xbus_of_span{$spanno}) { |
| 41 |
printf STDERR "%s: Span %d already seen on %s\n", |
| 42 |
$xpd->fqn, $spanno, $xbus_of_span{$spanno}->name; |
| 43 |
die; |
| 44 |
} |
| 45 |
$xbus_of_span{$spanno} = $xbus; |
| 46 |
# Check XPD's sequential numbering |
| 47 |
if(defined $last_spanno) { |
| 48 |
if($last_spanno + 1 != $spanno) { |
| 49 |
printf STDERR "%s: Bad span numbers (%d, %d)\n", |
| 50 |
$xpd->fqn, $last_spanno, $spanno; |
| 51 |
die; |
| 52 |
} |
| 53 |
} else { |
| 54 |
$xbus_beginning{$xbus} = $spanno; |
| 55 |
} |
| 56 |
$last_spanno = $spanno; |
| 57 |
} |
| 58 |
} |
| 59 |
foreach my $span (@spans) { |
| 60 |
my $spanno = $span->num; |
| 61 |
if(!defined($seen_spans{$spanno})) { |
| 62 |
warn "Span $spanno: Ignored: Does not belong to any XPD\n"; |
| 63 |
} |
| 64 |
} |
| 65 |
@sorted_xbuses = sort { $xbus_beginning{$a} <=> $xbus_beginning{$b} } @xbuses; |
| 66 |
return @sorted_xbuses; |
| 67 |
} |
| 68 |
|
| 69 |
sub generate($$$) { |
| 70 |
my $self = shift || die; |
| 71 |
my $file = $self->{FILE}; |
| 72 |
my $gconfig = $self->{GCONFIG}; |
| 73 |
my $genopts = $self->{GENOPTS}; |
| 74 |
my @spans = @_; # Verify it's all our spans |
| 75 |
my @xbuses = get_sorted_xbuses(@spans); |
| 76 |
warn "Empty configuration -- no xbuses\n" unless @xbuses; |
| 77 |
rename "$file", "$file.bak" |
| 78 |
or $! == 2 # ENOENT (No dependency on Errno.pm) |
| 79 |
or die "Failed to backup old config: $!\n"; |
| 80 |
#$gconfig->dump; |
| 81 |
print "Generating $file\n" if $genopts->{verbose}; |
| 82 |
open(F, ">$file") || die "$0: Failed to open $file: $!\n"; |
| 83 |
my $old = select F; |
| 84 |
printf "# Autogenerated by $0 on %s\n", scalar(localtime); |
| 85 |
print "# If you edit this file and execute $0 again,\n"; |
| 86 |
print "# your manual changes will be LOST.\n"; |
| 87 |
print <<'HEAD'; |
| 88 |
# |
| 89 |
# This is an optional configuration file for ordering |
| 90 |
# Dahdi registration. |
| 91 |
# |
| 92 |
# It is read from /etc/dahdi/xpp_order. This location |
| 93 |
# may be overridden via the environment variable XPPORDER_CONF |
| 94 |
# |
| 95 |
# Lines may contain: |
| 96 |
# - The Astribank label (verbatim) |
| 97 |
# - The Astribank connector string (prefixed with @) |
| 98 |
# Ordering number of each listed Astribank is determined |
| 99 |
# by its position in this file. |
| 100 |
# Astribanks not listed in this file, get an ordering |
| 101 |
# number of 99 (last). |
| 102 |
# |
| 103 |
# Astribanks with same ordering number are sorted by their |
| 104 |
# connectors (to preserve legacy behavior). |
| 105 |
# |
| 106 |
# Examples: |
| 107 |
#usb:1234 |
| 108 |
#@usb-0000:06:02.2-2 |
| 109 |
HEAD |
| 110 |
foreach my $xbus (@xbuses) { |
| 111 |
my $label = $xbus->label; |
| 112 |
my $connector = $xbus->connector; |
| 113 |
my $name = $xbus->name; |
| 114 |
printf "%s\t# %s #(%s)\n", $label, $connector, $name; |
| 115 |
} |
| 116 |
close F; |
| 117 |
select $old; |
| 118 |
} |
| 119 |
|
| 120 |
1; |
| 121 |
|
| 122 |
__END__ |
| 123 |
|
| 124 |
=head1 NAME |
| 125 |
|
| 126 |
Xpporder - Generate Astribank ordering information for dahdi_registration. |
| 127 |
|
| 128 |
=head1 SYNOPSIS |
| 129 |
|
| 130 |
use Dahdi::Config::Gen::Xpporder; |
| 131 |
|
| 132 |
my $cfg = new Dahdi::Config::Gen::Xpporder(\%global_config, \%genopts); |
| 133 |
$cfg->generate; |
| 134 |
|
| 135 |
=head1 DESCRIPTION |
| 136 |
|
| 137 |
Generate the F</etc/dahdi/xpp_order>. |
| 138 |
This is the configuration for dahdi_registration(1). |
| 139 |
The order is determined according to current Dahdi registration |
| 140 |
order. |
| 141 |
|
| 142 |
Its location may be overriden via the environment variable F<XPPORDER_CONF>. |