summaryrefslogtreecommitdiff
path: root/xpp/utils/xpp_sync
blob: d03c0a441d44c88131cf2669946817f28f2fe520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#! /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.
#
# $Id$
#
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);
}

sub unique_xbus(@) {
	my %seen;

	grep { !$seen{$_->xbus}++; } @_;
}

my $curr_sync = Zaptel::Xpp::sync;
my @sync_xpds = unique_xbus(get_sorted_xpds());

sub show_sync() {
	foreach my $xpd (@sync_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;
		foreach my $x (sort { $a->num <=> $b->num } @xpds) {
			printf "%-3s ", $x->type;
		}
		print "]";
		my $padding = ' ' x (4 * (8 - @xpds));
		printf "%s  (%s)\n", $padding, $xbus->connector;
	}
}

sub check_fxo_host_sync() {
	my @host_synced_xpds = grep { $_->xbus->num() ne $curr_sync } @sync_xpds;
	my @host_synced_fxos = grep($_->type eq 'FXO', @host_synced_xpds);
	if(@host_synced_fxos) {
		my @bad_xbus = map { $_->xbus } unique_xbus(@host_synced_fxos);
		our $lines = join("\n\t", map { $_->name } @bad_xbus);
		print STDERR <<"END";
=========================================
WARNING: FXO with HOST SYNC cause bad PCM
	 Affected Astribanks are:
-----------------------------------------
	$lines
=========================================
END
	}
}

if($sync) {
	if($autoselect) {
		do_select(@sync_xpds);
	} else {
		$sync = uc($sync);
		do_set($sync);
	}
	$curr_sync = Zaptel::Xpp::sync;
	#print "New sync: ", Zaptel::Xpp::sync, "\n";
} else {
	print "Current sync: ", $curr_sync, "\n";
	print "Best Available Syncers:\n";
	show_sync;
	check_fxo_host_sync;
}

__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)