#! /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: zt_registration 3143 2007-10-16 19:17:46Z tzafrir $
#
use strict;
use File::Basename;
BEGIN { my $dir = dirname($0); unshift(@INC, "$dir", "$dir/zconf"); }

use Zaptel;
use Zaptel::Span;
use Zaptel::Xpp;
use Zaptel::Xpp::Xbus;

sub usage {
	die "Usage: $0 [on|off|1|0]\n";
}

@ARGV == 0 or @ARGV == 1 or usage;
my $on = shift;
my $verbose = 0;
my $should_output = 1;

if(defined($on)) {	# Translate to booleans
	$on = uc($on);
	$on =~ /^(ON|OFF|1|0)$/ or usage;
	$on = ($on eq 'ON') ? 1 : 0;
	$should_output = 0 unless $verbose;
}

sub state2str($) {
	return (shift)?"on":"off";
}

sub myprintf {
	printf @_ if $should_output;
}

my @spans = Zaptel::spans;

foreach my $xbus (Zaptel::Xpp::xbuses('SORT_SERIAL')) {
	myprintf "%-10s\t%s\t%s\n", $xbus->name, $xbus->serial, $xbus->connector;
	next unless $xbus->status eq 'CONNECTED';
	foreach my $xpd ($xbus->xpds()) {
		my $prev = $xpd->zt_registration($on);
		myprintf "\t%-10s: ", $xpd->fqn;
		if(!defined($on)) {			# Query only
			my ($span) = grep { $_->name eq $xpd->fqn } @spans;
			my $spanstr = ($span) ? ("Span " . $span->num) : "";
			myprintf "%s %s\n", state2str($prev), $spanstr ;
			next;
		}
		if(!defined($prev)) {			# Failure
			printf "Failed %s\n", $!;
			next;
		}
		myprintf "%3s ==> %3s\n", state2str($prev), state2str($on);
	}
}

__END__

=head1 NAME

zt_registration - Handle registration of Xorcom XPD modules in zaptel.

=head1 SYNOPSIS

zt_registration [on|off]

=head1 DESCRIPTION

Without parameters, show all connected XPDs sorted by serial number and
physical connector order. Each one is show to be unregistered (off), or 
registered to a specific  zaptel span (the span number is shown).

All registerations/deregisterations are sorted by serial number and
(lacking that) physical connector string.

Span registration should generally always succeed. Span unregistration may 
fail if channels from the span are in use by e.g. asterisk. In such a case
you'll also see those channels as '(In use)' in the output of lszaptel(8).

=head2 Parameters

off -- deregisters all XPD's from zaptel.

on -- registers all XPD's to zaptel.

=head2 Sample Output

An example of the output of zt_registration for some registered
Astribanks:

  $ zt_registration
  XBUS-02         []      usb-0000:00:1d.7-4
          XBUS-00/XPD-00: on Span 1
          XBUS-00/XPD-10: on Span 2
  XBUS-00         [usb:00000126]  usb-0000:00:1d.7-2
          XBUS-02/XPD-00: on Span 3
          XBUS-02/XPD-10: on Span 4
          XBUS-02/XPD-20: on Span 5
          XBUS-02/XPD-30: on Span 6
  XBUS-01         [usb:00000128]  usb-0000:00:1d.7-1
          XBUS-01/XPD-00: on Span 7
          XBUS-01/XPD-10: on Span 8
          XBUS-01/XPD-20: on Span 9
          XBUS-01/XPD-30: on Span 10

In this example the XBUS-02 has an empty serial number and thus 
becomes the first Astribank. The other two are sorted according to their
serial numbers.

=head1 FILES

=over

=item /proc/xpp/XBUS-nn/XPD-mm/zt_registration

Reading from this file shows if if the if the specific XPD is
registered. Writing to it 0 or 1 registers / unregisters the device.

This should allow you to register / unregister a specific XPD rather
than all of them. 

=back
