| 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 |
use Getopt::Std; |
| 13 |
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/perl_modules"); } |
| 14 |
|
| 15 |
use Dahdi::Xpp; |
| 16 |
use Dahdi::Xpp::Xbus; |
| 17 |
|
| 18 |
my $sync; |
| 19 |
my $autoselect; |
| 20 |
|
| 21 |
sub usage() { |
| 22 |
print |
| 23 |
"$0: show / set Astribank sync source\n". |
| 24 |
"\n". |
| 25 |
"Usage: $0 [-v] Show sync source.\n". |
| 26 |
" $0 [-v] <auto|NN|dahdi> Set sync source.\n". |
| 27 |
""; |
| 28 |
exit 1; |
| 29 |
} |
| 30 |
|
| 31 |
my %opts; |
| 32 |
getopts('hv', \%opts) || usage; |
| 33 |
|
| 34 |
$opts{h} && usage; |
| 35 |
if(@ARGV == 1) { |
| 36 |
$sync = shift; |
| 37 |
$autoselect = 1 if $sync =~ /^auto$/i; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
sub get_sorted_xpds() { |
| 42 |
my @good_xpds; |
| 43 |
|
| 44 |
foreach my $xbus (Dahdi::Xpp::xbuses) { |
| 45 |
next unless $xbus->status eq 'CONNECTED'; |
| 46 |
foreach my $xpd ($xbus->xpds()) { |
| 47 |
my $isreg = $xpd->dahdi_registration(); |
| 48 |
if(!defined($isreg)) { # Failure |
| 49 |
printf STDERR "%s: Failed %s\n", $xpd->fqn, $!; |
| 50 |
next; |
| 51 |
} |
| 52 |
next unless $isreg; # Skip unregistered XPDs |
| 53 |
push(@good_xpds, $xpd); |
| 54 |
} |
| 55 |
} |
| 56 |
my @xpd_prio = Dahdi::Xpp::Xpd::xpds_by_rank(@good_xpds); |
| 57 |
Dahdi::Xpp::Xpd::show_xpd_rank(@xpd_prio) if $opts{v}; |
| 58 |
return @xpd_prio; |
| 59 |
} |
| 60 |
|
| 61 |
sub do_select(@) { |
| 62 |
my $found; |
| 63 |
|
| 64 |
foreach my $xpd (@_) { |
| 65 |
my $xbus = $xpd->xbus; |
| 66 |
my $busnum = $xbus->name; |
| 67 |
die "Unknown bus name" unless $busnum; |
| 68 |
$busnum =~ s/XBUS-//; |
| 69 |
die "bad bus name" unless $busnum =~ /^\d+$/; |
| 70 |
#printf "Setting sync: %-10s (%s)\n", $xpd->fqn, $xpd->type; |
| 71 |
if(Dahdi::Xpp::sync($busnum)) { |
| 72 |
#print "SET $busnum\n"; |
| 73 |
$found = 1; |
| 74 |
last; |
| 75 |
} else { |
| 76 |
print STDERR "Failed to set $busnum: $!\n"; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
sub do_set($) { |
| 82 |
my $sync = shift; |
| 83 |
die "Failed to set sync to '$sync'" unless Dahdi::Xpp::sync($sync); |
| 84 |
} |
| 85 |
|
| 86 |
sub unique_xbus(@) { |
| 87 |
my %seen; |
| 88 |
|
| 89 |
grep { !$seen{$_->xbus}++; } @_; |
| 90 |
} |
| 91 |
|
| 92 |
my $curr_sync = Dahdi::Xpp::sync; |
| 93 |
my @sync_xpds = unique_xbus(get_sorted_xpds()); |
| 94 |
|
| 95 |
sub show_sync() { |
| 96 |
foreach my $xpd (@sync_xpds) { |
| 97 |
my $xbus = $xpd->xbus; |
| 98 |
my $xpdstr = '[ ' . $xbus->pretty_xpds . ' ]'; |
| 99 |
my $label = '[' . $xbus->label() . ']'; |
| 100 |
my $connector = '(' . $xbus->connector . ')'; |
| 101 |
my $mark = ($curr_sync =~ /^\d+$/ and $xbus->num == $curr_sync)?"+":""; |
| 102 |
my $padding = ' ' x (40 - length $xpdstr); |
| 103 |
printf " %1s %s %-25s %-14s %s\n", $mark, $xbus->name, $connector, $label, $xpdstr; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
sub check_fxo_host_sync() { |
| 108 |
my @host_synced_xpds = grep { $_->xbus->num() ne $curr_sync } @sync_xpds; |
| 109 |
my @host_synced_fxos = grep($_->type eq 'FXO', @host_synced_xpds); |
| 110 |
if(@host_synced_fxos) { |
| 111 |
my @bad_xbus = map { $_->xbus } unique_xbus(@host_synced_fxos); |
| 112 |
our $lines = join("\n\t", map { $_->name } @bad_xbus); |
| 113 |
print STDERR <<"END"; |
| 114 |
================================================== |
| 115 |
WARNING: FXO which is not the syncer cause bad PCM |
| 116 |
Affected Astribanks are: |
| 117 |
-------------------------------------------------- |
| 118 |
$lines |
| 119 |
================================================== |
| 120 |
END |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if(defined $sync) { |
| 125 |
if($autoselect) { |
| 126 |
do_select(@sync_xpds); |
| 127 |
} else { |
| 128 |
$sync = uc($sync); |
| 129 |
do_set($sync); |
| 130 |
} |
| 131 |
$curr_sync = Dahdi::Xpp::sync; |
| 132 |
#print "New sync: ", Dahdi::Xpp::sync, "\n"; |
| 133 |
} else { |
| 134 |
print "Current sync: ", $curr_sync, "\n"; |
| 135 |
print "Best Available Syncers:\n"; |
| 136 |
show_sync; |
| 137 |
check_fxo_host_sync; |
| 138 |
} |
| 139 |
|
| 140 |
__END__ |
| 141 |
|
| 142 |
=head1 NAME |
| 143 |
|
| 144 |
xpp_sync - Handle sync selection of Xorcom Astribanks. |
| 145 |
|
| 146 |
=head1 SYNOPSIS |
| 147 |
|
| 148 |
xpp_sync <auto|dahdi|nn> |
| 149 |
|
| 150 |
xpp_sync [-v] |
| 151 |
|
| 152 |
=head1 DESCRIPTION |
| 153 |
|
| 154 |
On a normal operation one Astribank device provides timing for all the |
| 155 |
other Astribank devices. |
| 156 |
|
| 157 |
When run without parameters, xpp_sync will display a list of Astribanks |
| 158 |
(xbuses) that are connected and registered as Dahdi spans. The current |
| 159 |
xpp sync master will be marked. |
| 160 |
|
| 161 |
If you this an Astribank is connected and yet it does not appear on the |
| 162 |
output of xpp_sync, it may be unregistered. Try running dahdi_registration . |
| 163 |
|
| 164 |
=head2 Parameters |
| 165 |
|
| 166 |
=over |
| 167 |
|
| 168 |
=item auto |
| 169 |
|
| 170 |
Automatically selects the best Astribank for syncing. |
| 171 |
|
| 172 |
=item dahdi |
| 173 |
|
| 174 |
Gets synchronization from the Dahdi sync master. |
| 175 |
|
| 176 |
=item nn |
| 177 |
|
| 178 |
Sets XBUS-I<nn> as sync source. |
| 179 |
|
| 180 |
=item -v |
| 181 |
|
| 182 |
Also print the numeric xpp sync rank. |
| 183 |
|
| 184 |
=back |
| 185 |
|
| 186 |
(Parameter name is case-insensitive) |
| 187 |
|
| 188 |
=head2 Example output: |
| 189 |
|
| 190 |
Setting SYNC |
| 191 |
Current sync: 01 |
| 192 |
Best Available Syncers: |
| 193 |
+ XBUS-01 (usb-0000:00:10.4-3) [usb:12345678] [ PRI_TE PRI_NT PRI_TE PRI_NT ] |
| 194 |
XBUS-00 (usb-0000:00:10.4-2) [usb:QA-01] [ FXS FXO ] |
| 195 |
================================================== |
| 196 |
WARNING: FXO which is not the syncer cause bad PCM |
| 197 |
Affected Astribanks are: |
| 198 |
-------------------------------------------------- |
| 199 |
XBUS-00 |
| 200 |
================================================== |
| 201 |
|
| 202 |
In this example we see that the recommended xpp sync master is XBUS-02 - |
| 203 |
it is the first on the list. It is also the actual syncer, as we can see |
| 204 |
from the '+' beside it. |
| 205 |
|
| 206 |
xpp_sync is normally called from the dahdi init.d script. |
| 207 |
The parameter it is called with defaults to |
| 208 |
I<auto>, but it is possible to override that parameter (e.g: set it to |
| 209 |
I<dahdi>) through the value of XPP_SYNC in /etc/dahdi/init.conf . |
| 210 |
|
| 211 |
=head1 FILES |
| 212 |
|
| 213 |
=over |
| 214 |
|
| 215 |
=item /sys/bus/astribanks/drivers/xppdrv/sync |
| 216 |
|
| 217 |
xpp_sync is essentially a nicer interface to |
| 218 |
C</sys/bus/astribanks/drivers/xppdrv/sync>. That file |
| 219 |
shows the current xpp sync master. |
| 220 |
|
| 221 |
Writing to it, force XPP drivers to use a different sync master |
| 222 |
|
| 223 |
=back |
| 224 |
|
| 225 |
=head1 SEE ALSO |
| 226 |
|
| 227 |
dahdi_registration(1), dahdi_cfg(1), README.Astribank |