Debian Codegraph @

xpp/twinstar

raw
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;
16 use Dahdi::Hardware;
17 use Dahdi::Span;
18 use Dahdi::Xpp;
19 use Dahdi::Xpp::Xbus;
20 use Dahdi::Xpp::Mpp;
21
22 $Getopt::Std::STANDARD_HELP_VERSION = 1;
23 $main::VERSION = '$Id$';
24
25 sub HELP_MESSAGE() {
26 eval(usage());
27 return 0;
28 }
29
30 sub usage {
31 die "Usage: $0 {status|jump|enable-wd|disable-wd|ports}\n";
32 }
33
34 our ($opt_v, $opt_x);
35 getopts('vx') || usage;
36 @ARGV == 1 or usage;
37
38
39 # Find USB bus toplevel
40 my $usb_top;
41 $usb_top = '/dev/bus/usb';
42 $usb_top = '/proc/bus/usb' unless -d $usb_top;
43 die "No USB toplevel found\n" unless -d $usb_top;
44
45 sub tws_devs() {
46 my @devs;
47 foreach my $dev (Dahdi::Hardware->device_list) {
48 next unless $dev->is_astribank;
49 next unless $dev->product =~ /116./;
50 push(@devs, $dev->hardware_name);
51 }
52 return @devs;
53 }
54
55 sub tws_usb_devfile($) {
56 my $name = shift || die;
57 # Remove prefix
58 if($name !~ s/usb://) {
59 die "$name is not a USB name\n";
60 }
61 return "$usb_top/$name";
62 }
63
64 sub tws_show(@) {
65 my @usb_devs = @_;
66 my $format = "%-15s %-10s %-15s %-10s %-10s\n";
67
68 printf $format, 'DEVICE', 'PORT', 'WATCHDOG', 'POWER0', 'POWER1';
69 foreach my $dev (@usb_devs) {
70 my $mppinfo = $dev->mppinfo;
71 if(!defined $mppinfo) {
72 printf STDERR "%s: no MPP information\n", $dev->hardware_name;
73 next;
74 }
75 if(!defined $mppinfo->{TWINSTAR_PORT}) {
76 printf STDERR "%s: no TWINSTAR_PORT information\n", $dev->hardware_name;
77 next;
78 }
79 my $power = $mppinfo->twinstar_power;
80 printf $format,
81 $dev->hardware_name,
82 $mppinfo->twinstar_port,
83 ($mppinfo->twinstar_watchdog) ? "on" : "off",
84 ($power->[0]) ? "yes" : "no",
85 ($power->[1]) ? "yes" : "no";
86 }
87 }
88
89 sub tws_portnum($) {
90 my $dev = shift || die "Missing dev";
91 my $mppinfo = $dev->mppinfo;
92 if(!defined $mppinfo) {
93 printf STDERR "%s: no MPP information\n", $dev->hardware_name;
94 return undef;
95 }
96 return $mppinfo->twinstar_port;
97 }
98
99 sub tws_showports(@) {
100 my @usb_devs = @_;
101 foreach my $dev (@usb_devs) {
102 my $mppinfo = $dev->mppinfo;
103 if(!defined $mppinfo) {
104 printf STDERR "%s: no MPP information\n", $dev->hardware_name;
105 next;
106 }
107 if(!defined $mppinfo->{TWINSTAR_PORT}) {
108 printf STDERR "%s: no TWINSTAR_PORT information\n", $dev->hardware_name;
109 next;
110 }
111 printf "%s\n", $mppinfo->{TWINSTAR_PORT};
112 }
113 }
114
115 sub tws_watchdog($@) {
116 my $on = shift;
117 die "tws_watchdog() on/off?" unless defined $on;
118 my @usb_devs = @_;
119
120 foreach my $dev (@usb_devs) {
121 my $mppinfo = $dev->mppinfo;
122 if(!defined $mppinfo) {
123 printf STDERR "%s: no MPP information\n", $dev->hardware_name;
124 next;
125 }
126 $mppinfo->mpp_setwatchdog($on);
127 }
128 }
129
130 sub tws_jump(@) {
131 my @usb_devs = @_;
132
133 foreach my $dev (@usb_devs) {
134 my $mppinfo = $dev->mppinfo;
135 if(!defined $mppinfo) {
136 printf STDERR "%s: no MPP information\n", $dev->hardware_name;
137 next;
138 }
139 eval {
140 $mppinfo->mpp_jump;
141 };
142 warn $@ if $@;
143 }
144 }
145
146 sub dev_list() {
147 my @devs;
148 foreach my $dev (Dahdi::Hardware->device_list) {
149 next unless $dev->is_astribank;
150 next unless $dev->product =~ /116./;
151 Dahdi::Xpp::Mpp->mpp_addinfo($dev);
152 push(@devs, $dev);
153 }
154 return @devs;
155 }
156
157 my @usb_devices = dev_list();
158
159 if($ARGV[0] eq 'status') {
160 tws_show(@usb_devices);
161 } elsif($ARGV[0] eq 'jump') {
162 tws_jump(@usb_devices);
163 } elsif($ARGV[0] eq 'disable-wd') {
164 tws_watchdog(0, @usb_devices);
165 } elsif($ARGV[0] eq 'enable-wd') {
166 tws_watchdog(1, @usb_devices);
167 } elsif($ARGV[0] eq 'ports') {
168 tws_showports(@usb_devices);
169 } else {
170 usage;
171 }
172
173 __END__
174
175 =head1 NAME
176
177 twinstar - Control the Twinstar feature of a Xorcom Astribank
178
179 =head1 SYNOPSIS
180
181 twinstar {status|jump|enable-wd|disable-wd|ports}
182
183 =head1 DESCRIPTION
184
185 B<twinstar> is a tool to control the Twinstar (dual USB port) of a
186 Xorcom Astribank. There is a single and mandatory argument which is the
187 command to run. That command operates on all the Astribanks connected to
188 the system.
189
190 Technically all the commands are implemented using Dahdi::Xpp::Mpp which
191 in turn uses astribank_tool. Thus using thus tool will require root
192 permissions or otherwise read/write permissions to the USB device.
193
194 The twinstar may be in I<watchdog mode>, which means that it will jump
195 to the remote host if it loses contact with the local host. This can
196 happen if the machine is powered down or hangs or even if the xpp
197 drivers are unloaded. Which is why the standard twinstar scripts put the
198 Astribanks in twinstar mode on startup and remove it on normal shutdown.
199
200 An Astribank will only jump to the other host (either if asked
201 explicitly or by the watchdog) only if there is a different Astribank
202 connected to the other port and running. Which is why all of this has no
203 effect on systems that don't need this functionality.
204
205 The command are:
206
207 =head2 status
208
209 Shows the current status of all Astribanks. Note that it only shows
210 Astribanks whose current active USB port is the one connected to this
211 computer.
212
213 Example output:
214
215 DEVICE PORT WATCHDOG POWER0 POWER1
216 usb:001/010 0 on yes yes
217 usb:001/011 0 on yes yes
218
219 For each Astribank on the system that has Twinstar support we get:
220
221 =over 4
222
223 =item Device
224
225 The address of the device. This is the bus address, e.g. the address you
226 see in lsusb / dahdi_hardware.
227
228 =item Port
229
230 The active USB port on the Astribank. This should be always '0' on the
231 master and always 1 on the slave.
232
233 =item Watchdog
234
235 I<on> if the watchdog is triggered in the Atribank or I<off> otherwise.
236
237 =item Power0, Power1
238
239 Shows which ports of this Astribank are connected to a USB port of a
240 running computer. This only shows whether or not the USB host provides
241 power.
242
243 =back
244
245 =head2 ports
246
247 Shows the same 'Port' column of the B<status> command.
248
249 =head2 jump
250
251 Command all the Astribanks to jump to the other port. This works
252 regardless the watchdog mode is enabled or not. But requires that there
253 is power on the other port.
254
255 =head2 enable-wd
256
257 Enables watchdog mode.
258
259 =head2 disable-wd
260
261 Disables watchdog mode.
262
263 =head1 FILES
264
265 B<twinstar> mostly uses astribank_tool which in turn mostly uses USB
266 files under /dev/bus/usb .
267