[dahdi-commits] tzafrir: tools/trunk r4533 - in /tools/trunk/xpp: ./ perl_modules/Dahdi/
SVN commits to the DAHDI project
dahdi-commits at lists.digium.com
Thu Jul 3 03:22:03 CDT 2008
Author: tzafrir
Date: Thu Jul 3 03:22:02 2008
New Revision: 4533
URL: http://svn.digium.com/view/dahdi?view=rev&rev=4533
Log:
Allow a different sort order in dahdi_registration without editing the
script.
Modified:
tools/trunk/xpp/dahdi_registration
tools/trunk/xpp/perl_modules/Dahdi/Xpp.pm
Modified: tools/trunk/xpp/dahdi_registration
URL: http://svn.digium.com/view/dahdi/tools/trunk/xpp/dahdi_registration?view=diff&rev=4533&r1=4532&r2=4533
==============================================================================
--- tools/trunk/xpp/dahdi_registration (original)
+++ tools/trunk/xpp/dahdi_registration Thu Jul 3 03:22:02 2008
@@ -15,9 +15,24 @@
use Dahdi::Span;
use Dahdi::Xpp;
use Dahdi::Xpp::Xbus;
+use Getopt::Std;
sub usage {
die "Usage: $0 [on|off|1|0]\n";
+}
+
+my %opts;
+getopts('s:', \%opts);
+
+my $sort_order = $opts{s} || $ENV{XBUS_SORT} || 'SORT_CONNECTOR';
+my $sorter = Dahdi::Xpp::sorters($sort_order);
+
+if(!defined $sorter) {
+ my @sorter_names = Dahdi::Xpp::sorters;
+ print STDERR "Unknown sort order $sort_order. Select from:\n\t";
+ print STDERR join("\n\t", @sorter_names);
+ print STDERR "\n";
+ exit 1;
}
@ARGV == 0 or @ARGV == 1 or usage;
@@ -42,7 +57,7 @@
my @spans = Dahdi::spans;
-foreach my $xbus (Dahdi::Xpp::xbuses('SORT_CONNECTOR')) {
+foreach my $xbus (Dahdi::Xpp::xbuses($sorter)) {
myprintf "%-10s\t%s\t%s\n", $xbus->name, $xbus->label, $xbus->connector;
next unless $xbus->status eq 'CONNECTED';
foreach my $xpd ($xbus->xpds()) {
@@ -61,6 +76,7 @@
myprintf "%3s ==> %3s\n", state2str($prev), state2str($on);
}
}
+myprintf "# Sorted: $sort_order\n";
__END__
@@ -70,7 +86,7 @@
=head1 SYNOPSIS
-dahdi_registration [on|off]
+dahdi_registration [-s sortorder] [on|off]
=head1 DESCRIPTION
@@ -90,25 +106,46 @@
on -- registers all XPD's to dahdi.
+=head2 Options
+
+=over
+
+=item -s I<sort_order>
+
+The sort order to use.
+
+=back
+
+If the option is not used, the sort order is taken from the environment
+variable XBUS_SORT and failing that: the hard-coded default of
+SORT_CONNECTOR.
+
+The available sorting orders are documented in Dahdi::Xpp manual.
+
+
+
=head2 Sample Output
An example of the output of dahdi_registration for some registered
Astribanks:
- $ dahdi_registration
- XBUS-02 [] usb-0000:00:1d.7-4
- XBUS-00/XPD-00: on Span 1
- XBUS-00/XPD-10: on Span 2
- XBUS-00 [usb:00000126] usb-0000:00:1d.7-2
- XBUS-02/XPD-00: on Span 3
- XBUS-02/XPD-10: on Span 4
- XBUS-02/XPD-20: on Span 5
- XBUS-02/XPD-30: on Span 6
- XBUS-01 [usb:00000128] usb-0000:00:1d.7-1
- XBUS-01/XPD-00: on Span 7
- XBUS-01/XPD-10: on Span 8
- XBUS-01/XPD-20: on Span 9
- XBUS-01/XPD-30: on Span 10
+ $ dahdi_registration -s type
+ XBUS-01 usb:0000153 usb-0000:00:10.4-2
+ XBUS-01/XPD-00: on Span 1
+ XBUS-01/XPD-01: on Span 2
+ XBUS-00 usb:0000157 usb-0000:00:10.4-4
+ XBUS-00/XPD-00: on Span 3
+ XBUS-00/XPD-01: on Span 4
+ XBUS-00/XPD-02: on Span 5
+ XBUS-00/XPD-03: on Span 6
+ XBUS-00/XPD-04: on Span 7
+ XBUS-00/XPD-05: on Span 8
+ XBUS-00/XPD-06: on Span 9
+ XBUS-00/XPD-07: on Span 10
+ XBUS-02 usb-0000:00:10.4-1
+ XBUS-02/XPD-00: on Span 11
+ XBUS-02/XPD-10: on Span 12
+ # Sorted: type
=head1 FILES
Modified: tools/trunk/xpp/perl_modules/Dahdi/Xpp.pm
URL: http://svn.digium.com/view/dahdi/tools/trunk/xpp/perl_modules/Dahdi/Xpp.pm?view=diff&rev=4533&r1=4532&r2=4533
==============================================================================
--- tools/trunk/xpp/perl_modules/Dahdi/Xpp.pm (original)
+++ tools/trunk/xpp/perl_modules/Dahdi/Xpp.pm Thu Jul 3 03:22:02 2008
@@ -26,6 +26,7 @@
print " - ".$xpd->fqn,"\n";
}
}
+
=cut
@@ -46,12 +47,46 @@
return $a->connector cmp $b->connector;
}
+sub score_type {
+ my $score;
+
+ return 1 if grep(/\b[ETJ]1/, @_);
+ return 2 if grep(/\bBRI/, @_);
+ return 3 if grep(/\bFXO/, @_);
+ return 4; # FXS
+}
+
+sub by_type {
+ my @a_types = map { $_->type } $a->xpds();
+ my @b_types = map { $_->type } $b->xpds();
+ my $res;
+
+ my $a_score = score_type(@a_types);
+ my $b_score = score_type(@b_types);
+ #printf STDERR "DEBUG-a: %s %s %s\n", $a->name, $a_score, join(',', at a_types);
+ #printf STDERR "DEBUG-b: %s %s %s\n", $b->name, $b_score, join(',', at b_types);
+ $res = $a_score <=> $b_score;
+ $res = $a->connector cmp $b->connector if $res == 0;
+ return $res;
+}
+
+
=head1 xbuses([sort_order])
Scans system (/proc and /sys) and returns a list of Astribank (Xbus)
objects. The optional parameter sort_order is the order in which
the Astribanks will be returns:
+
+=head1 sorters([sort_order])
+
+With no parameters, returns the names of built in sorters.
+With a single parameter, returns a reference to the requested built in sorter.
+Also, for convenience, a reference to a custom sorter function may be passed
+and returned as is.
+
+The built in sorters are:
+
=over
=item SORT_CONNECTOR
@@ -63,13 +98,20 @@
Sorts by the label of the Astribank. The label field is unique to the
Astribank. It can also be viewed through 'lsusb -v' without the drivers
-loaded (the iSerial field in the Device Descriptor).
+loaded (the iSerial field in the Device Descriptor). This is normally
+relieble, but some older Astribanks have an empty label.
=item SORT_NAME
Sort by the "name". e.g: "XBUS-00". The order of Astribank names depends
on the load order, and hence may change between different runs.
+=item SORT_TYPE
+
+Sort by XPD types. First Astribanks with E1/T1/J1 XPDs, then with BRI,
+then with FXO, then ones with only FXS ports. Within each type they
+are sorted by the connector field (as in SORT_CONNECTOR above).
+
=item custom function
Instead of using a predefined sorter, you can pass your own sorting
@@ -78,6 +120,23 @@
=back
=cut
+
+sub sorters {
+ my %sorter_table = (
+ SORT_CONNECTOR => \&by_connector,
+ SORT_NAME => \&by_name,
+ SORT_LABEL => \&by_label,
+ SORT_TYPE => \&by_type,
+ # Aliases
+ connector => \&by_connector,
+ name => \&by_name,
+ label => \&by_label,
+ type => \&by_type,
+ );
+ my $which_sorter = shift || return sort keys %sorter_table;
+ return $which_sorter if ref($which_sorter) eq 'CODE';
+ return $sorter_table{$which_sorter};
+}
sub xbuses {
my $optsort = shift || 'SORT_CONNECTOR';
@@ -100,18 +159,8 @@
my $xbus = Dahdi::Xpp::Xbus->new(NAME => $name, NUM => $num, @attr);
push(@xbuses, $xbus);
}
- my $sorter;
- if($optsort eq "SORT_CONNECTOR") {
- $sorter = \&by_connector;
- } elsif($optsort eq "SORT_NAME") {
- $sorter = \&by_name;
- } elsif($optsort eq "SORT_LABEL") {
- $sorter = \&by_label;
- } elsif(ref($optsort) eq 'CODE') {
- $sorter = $optsort;
- } else {
- die "Unknown optional sorter '$optsort'";
- }
+ my $sorter = sorters($optsort);
+ die "Unknown optional sorter '$optsort'" unless defined $sorter;
@xbuses = sort $sorter @xbuses;
return @xbuses;
}
More information about the dahdi-commits
mailing list