summaryrefslogtreecommitdiff
path: root/xpp/xpp_sync
blob: 41cfd619b0eb876ad5193782cf0bc85e362e7f29 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#! /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;
use File::Basename;
use Getopt::Std;
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/perl_modules"); }

use Dahdi::Xpp;
use Dahdi::Xpp::Xbus;

my $sync;
my $autoselect;

sub usage() {
	print 
		"$0: show / set Astribank sync source\n".
		"\n".
		"Usage: $0 [-v]                    Show sync source.\n".
		"       $0 [-v] <auto|NN|dahdi>   Set sync source.\n".
		"";
	exit 1;
}

my %opts;
getopts('hv', \%opts) || usage;

$opts{h} && usage;
if(@ARGV == 1) {
	$sync = shift;
	$autoselect = 1 if $sync =~ /^auto$/i;
}


sub get_sorted_xpds() {
	my @good_xpds;

	foreach my $xbus (Dahdi::Xpp::xbuses) {
		next unless $xbus->status eq 'CONNECTED';
		foreach my $xpd ($xbus->xpds()) {
			my $isreg = $xpd->dahdi_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 @xpd_prio = Dahdi::Xpp::Xpd::xpds_by_rank(@good_xpds);
	Dahdi::Xpp::Xpd::show_xpd_rank(@xpd_prio) if $opts{v};
	return @xpd_prio;
}

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(Dahdi::Xpp::sync($busnum)) {
			#print "SET $busnum\n";
			$found = 1;
			last;
		} else {
			print STDERR "Failed to set $busnum: $!\n";
		}
	}
}

sub do_set($) {
	my $sync = shift;
	die "Failed to set sync to '$sync'" unless Dahdi::Xpp::sync($sync);
}

sub unique_xbus(@) {
	my %seen;

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

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

sub show_sync() {
	foreach my $xpd (@sync_xpds) {
		my $xbus = $xpd->xbus;
		my $xpdstr = '[ ' . $xbus->pretty_xpds . ' ]';
		my $label = '[' . $xbus->label() . ']';
		my $connector = '(' . $xbus->connector . ')';
		my $mark = ($curr_sync =~ /^\d+$/ and $xbus->num == $curr_sync)?"+":"";
		my $padding = ' ' x (40 - length $xpdstr);
		printf " %1s %s %-25s %-14s %s\n", $mark, $xbus->name, $connector, $label, $xpdstr;
	}
}

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 which is not the syncer cause bad PCM
	 Affected Astribanks are:
--------------------------------------------------
	$lines
==================================================
END
	}
}

if(defined $sync) {
	if($autoselect) {
		do_select(@sync_xpds);
	} else {
		$sync = uc($sync);
		do_set($sync);
	}
	$curr_sync = Dahdi::Xpp::sync;
	#print "New sync: ", Dahdi::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 Astribanks.

=head1 SYNOPSIS

xpp_sync <auto|dahdi|nn>

xpp_sync [-v]

=head1 DESCRIPTION

On a normal operation one Astribank device provides timing for all the
other Astribank devices.

When run without parameters, xpp_sync will display a list of Astribanks
(xbuses) that are connected and registered as Dahdi spans. The current
xpp sync master will be marked.

If you this an Astribank is connected and yet it does not appear on the 
output of xpp_sync, it may be unregistered. Try running dahdi_registration .

=head2 Parameters

=over

=item auto

Automatically selects the best Astribank for syncing.

=item dahdi

Gets synchronization from the Dahdi sync master.

=item nn

Sets XBUS-I<nn> as sync source.

=item -v

Also print the numeric xpp sync rank.

=back

(Parameter name is case-insensitive)

=head2 Example output:

	Setting SYNC
	Current sync: 01
	Best Available Syncers:
	 + XBUS-01 (usb-0000:00:10.4-3) [usb:12345678]       [ PRI_TE PRI_NT PRI_TE PRI_NT ]
	   XBUS-00 (usb-0000:00:10.4-2) [usb:QA-01]          [ FXS FXO ]
	==================================================
	WARNING: FXO which is not the syncer cause bad PCM
		 Affected Astribanks are:
	--------------------------------------------------
		XBUS-00
	==================================================

In this example we see that the recommended xpp sync master is XBUS-02 - 
it is the first on the list. It is also the actual syncer, as we can see
from the '+' beside it.

xpp_sync is normally called from the dahdi init.d script.
The parameter it is called with defaults to 
I<auto>, but it is possible to override that parameter (e.g: set it to
I<dahdi>) through the value of XPP_SYNC in /etc/dahdi/init.conf .

=head1 FILES

=over

=item /proc/xpp/sync

(Deprecated: no longer supported)
xpp_sync is essentially a nicer interface to /proc/xpp/sync . That file
shows the current xpp sync master (and in what format you need to write
to it to set the master).

=back

=head1 SEE ALSO

dahdi_registration(1), dahdi_cfg(1), README.Astribank