summaryrefslogtreecommitdiff
path: root/xpp/utils/xpp_sync
diff options
context:
space:
mode:
Diffstat (limited to 'xpp/utils/xpp_sync')
-rwxr-xr-xxpp/utils/xpp_sync152
1 files changed, 152 insertions, 0 deletions
diff --git a/xpp/utils/xpp_sync b/xpp/utils/xpp_sync
new file mode 100755
index 0000000..018d268
--- /dev/null
+++ b/xpp/utils/xpp_sync
@@ -0,0 +1,152 @@
+#! /usr/bin/perl -w
+#
+# Written by Oron Peled <oron@actcom.co.il>
+# Copyright (C) 2007, Xorcom
+# This program is free software; you can redistribute and/or
+# modify it under the same terms as Perl itself.
+#
+#use strict;
+BEGIN { my $dir = $0; $dir =~ s:/[^/]+$::; unshift(@INC, "$dir", "$dir/zconf"); }
+
+use Zaptel::Xpp;
+use Zaptel::Xpp::Xbus;
+
+my $sync;
+my $autoselect;
+
+if(@ARGV == 1) {
+ $sync = shift;
+ $autoselect = 1 if $sync =~ /^auto$/i;
+}
+
+sub get_sorted_xpds() {
+ my @good_xpds;
+
+ foreach my $xbus (Zaptel::Xpp::xbuses('SORT_CONNECTOR')) {
+ next unless $xbus->status eq 'CONNECTED';
+ foreach my $xpd ($xbus->xpds()) {
+ my $isreg = $xpd->zt_registration();
+ if(!defined($isreg)) { # Failure
+ printf STDERR "%s: Failed %s\n", $xpd->fqn, $!;
+ next;
+ }
+ next unless $isreg; # Skip unregistered XPDs
+ push(@good_xpds, $xpd);
+ }
+ }
+
+ my @bri_xpds = grep { $_->type =~ /BRI/; } @good_xpds;
+ my @fxo_xpds = grep { $_->type eq 'FXO'; } @good_xpds;
+ my @fxs_xpds = grep { $_->type eq 'FXS'; } @good_xpds;
+ return (@bri_xpds, @fxo_xpds, @fxs_xpds);
+}
+
+sub do_select(@) {
+ my $found;
+
+ foreach my $xpd (@_) {
+ my $xbus = $xpd->xbus;
+ my $busnum = $xbus->name;
+ die "Uknown bus name" unless $busnum;
+ $busnum =~ s/XBUS-//;
+ die "bad bus name" unless $busnum =~ /^\d+$/;
+ #printf "Setting sync: %-10s (%s)\n", $xpd->fqn, $xpd->type;
+ if(Zaptel::Xpp::sync($busnum)) {
+ #print "SET $busnum\n";
+ $found = 1;
+ last;
+ } else {
+ print STDERR "Failed to set $busnum: $!\n";
+ }
+ }
+ if(!$found) {
+ print STDERR "Fall back to HOST sync\n";
+ die "Failed to set HOST sync\n" unless Zaptel::Xpp::sync('HOST');
+ }
+}
+
+sub do_set($) {
+ my $sync = shift;
+ die "Failed to set sync to '$sync'" unless Zaptel::Xpp::sync($sync);
+}
+
+my $curr_sync = Zaptel::Xpp::sync;
+my %xbus_seen;
+my @sorted_xpds = grep { !$xbus_seen{$_->xbus}++; } get_sorted_xpds;
+if($sync) {
+ if($autoselect) {
+ do_select(@sorted_xpds);
+ } else {
+ $sync = uc($sync);
+ do_set($sync);
+ }
+ #print "New sync: ", Zaptel::Xpp::sync, "\n";
+} else {
+ print "Current sync: ", $curr_sync, "\n";
+ print "Best Available Syncers:\n";
+ foreach my $xpd (@sorted_xpds) {
+ my $xbus = $xpd->xbus;
+ my @xpds = $xbus->xpds;
+ my @types = map { $_->type } @xpds;
+ my $mark = ($curr_sync =~ /\d+/ and $xbus->num == $curr_sync)?"+":"";
+ printf "\t%1s %s [ ", $mark, $xbus->name;
+ my $next = 0;
+ foreach my $x (sort { $a->num <=> $b->num } @xpds) {
+ my $n = $x->num;
+ # Fill spaces
+ for(my $i = $next; $i < $n; $i++) {
+ printf "%-3s ", "";
+ }
+ printf "%-3s ", $x->type;
+ $next = $n + 1;
+ }
+ # Fill spaces to end
+ $n = 4;
+ for(my $i = $next; $i < $n; $i++) {
+ printf "%-3s ", "";
+ }
+ printf "] (%s)\n", $xbus->connector;
+ }
+}
+
+__END__
+
+=head1 NAME
+
+xpp_sync - Handle sync selection of Xorcom XPD's.
+
+=head1 SYNOPSIS
+
+xpp_sync [auto|host|nn]
+
+=head1 DESCRIPTION
+
+Without parameters, the current syncer. Either HOST or the XBUS number.
+Then a list of the 3 best XPD's for syncing.
+
+=head2 Parameters
+
+=over
+
+=item auto
+
+Automatically selects the best XPD for syncing (with HOST fallback).
+
+=item host
+
+Set HOST synchronization (XPP timers).
+
+=item nn
+
+Set XBUS number nn as sync source.
+
+=back
+
+=head2 Example output:
+
+ Current sync: 03
+ Best Available Syncers:
+ XBUS-00: FXS FXO (USB-0000:00:10.4-4)
+ + XBUS-03: FXS FXS FXS FXS (USB-0000:00:10.4-1)
+ XBUS-02: FXS FXS FXS FXS (USB-0000:00:10.4-2)
+ XBUS-01: FXS FXS FXS FXS (USB-0000:00:10.4-3)