use AGWConnect;
use DXDebug;
-use vars qw(@ISA $sock @outqueue $send_offset $inmsg $rproc);
+use vars qw(@ISA $sock @outqueue $send_offset $inmsg $rproc $noports $lastytime
+ $lasthtime $ypolltime $hpolltime %circuit);
@ISA = qw(Msg ExtMsg);
$sock = undef;
$send_offset = 0;
$inmsg = '';
$rproc = undef;
+$noports = 0;
+$lastytime = $lasthtime = time;
+$ypolltime = 10 unless defined $ypolltime;
+$hpolltime = 300 unless defined $hpolltime;
+%circuit = ();
sub init
{
finish();
dbg('err', "AGW initialising and connecting to $addr/$port ...");
- $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp');
+ $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port, Proto=>'tcp', Timeout => 30);
unless ($sock) {
dbg('err', "Cannot connect to AGW Engine at $addr/$port $!");
return;
# R frame for the release number
# G frame to ask for ports
# X frame to say who we are
+ # optional m frame to enable monitoring
_sendf('R');
_sendf('G');
_sendf('X', $main::mycall);
- if ($monitor) {
- _sendf('m')
- }
+ _sendf('m') if $monitor;
}
+my $finishing = 0;
+
sub finish
{
+ return if $finishing;
if ($sock) {
+ $finishing = 1;
dbg('err', "AGW ending...");
- for (values %Msg::conns) {
- next unless $_->isa('AGWMsg');
+ for (values %circuit) {
+ &{$_->{eproc}}() if $_->{eproc};
$_->disconnect;
}
# say we are going
+ _sendf('m') if $monitor;
_sendf('x', $main::mycall);
Msg->sleep(2);
Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
my $len = 0;
$len = length $data;
- dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
+ if ($sort eq 'y' || $sort eq 'H') {
+ dbg('agwpoll', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
+ } elsif ($sort eq 'D') {
+ if (isdbg('agw')) {
+ my $d = $data;
+ $d =~ s/\cM$//;
+ dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$d\"");
+ }
+ } else {
+ dbg('agw', "AGW sendf: $sort '${from}'->'${to}' port: $port pid: $pid \"$data\"");
+ }
push @outqueue, pack('C x3 a1 x1 C x1 a10 a10 V x4 a*', $port, $sort, $pid, $from, $to, $len, $data);
Msg::set_event_handler($sock, write=>\&_send);
}
dbg('agw', "error on AGW connection $addr/$port $!");
Msg::set_event_handler($sock, read=>undef, write=>undef, error=>undef);
$sock = undef;
- for (values %Msg::conns) {
+ for (%circuit) {
next unless $_->isa('AGWMsg');
$_->disconnect;
}
return unless $sock;
# we have at least 36 bytes of data (ugh!)
- my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 a10 a10 V x4', $inmsg);
+ my ($port, $sort, $pid, $from, $to, $len) = unpack('C x3 a1 x1 C x1 Z10 Z10 V x4', $inmsg);
my $data;
# do a sanity check on the length
$data = '' unless defined $data;
if ($sort eq 'D') {
- $data =~ s/\cR//g;
- dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$data\"");
- my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
+ my $d = unpack "Z*", $data;
+ $d =~ s/\cM$//;
+ dbg('agw', "AGW Data In port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
+ my $conn = _find($from eq $main::mycall ? $to : $from);
if ($conn) {
if ($conn->{state} eq 'WC') {
if (exists $conn->{cmd}) {
if (@{$conn->{cmd}}) {
- dbg('connect', $conn->{msg});
- $conn->_docmd($conn->{msg});
+ dbg('connect', $d);
+ $conn->_docmd($d);
}
}
if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
$conn->to_connected($conn->{call}, 'O', $conn->{csort});
}
} else {
- &{$conn->{rproc}}($conn, "I$conn->{call}|$data");
+ my @lines = split /\cM/, $data;
+ if (@lines) {
+ for (@lines) {
+ &{$conn->{rproc}}($conn, "I$conn->{call}|$_");
+ }
+ } else {
+ &{$conn->{rproc}}($conn, "I$conn->{call}|");
+ }
}
} else {
dbg('err', "AGW error Unsolicited Data!");
}
- } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M') {
- dbg('agw', "AGW Monitor \"$data\"");
+ } elsif ($sort eq 'I' || $sort eq 'S' || $sort eq 'U' || $sort eq 'M' || $sort eq 'T') {
+ my $d = unpack "Z*", $data;
+ $d =~ s/\cM$//;
+ my @lines = split /\cM/, $d;
+
+ for (@lines) {
+ s/([\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg;
+ dbg('agw', "AGW Monitor port: $port \"$_\"");
+ }
} elsif ($sort eq 'C') {
- dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to'");
+ my $d = unpack "Z*", $data;
+ $d =~ s/\cM$//;
+ dbg('agw', "AGW Connect port: $port pid: $pid '$from'->'$to' \"$d\"");
my $call = $from eq $main::mycall ? $to : $from;
- my $conn = Msg->conns($call);
+ my $conn = _find($call);
if ($conn) {
if ($conn->{state} eq 'WC') {
if (exists $conn->{cmd} && @{$conn->{cmd}}) {
- $conn->_docmd($data);
+ $conn->_docmd($d);
if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
$conn->to_connected($conn->{call}, 'O', $conn->{csort});
}
}
} else {
$conn = AGWMsg->new($rproc);
+ $conn->{agwpid} = $pid;
+ $conn->{agwport} = $port;
+ $conn->{lineend} = "\cM";
+ $conn->{incoming} = 1;
+ $conn->{agwcall} = $call;
+ $circuit{$call} = $conn;
+ if ($call =~ /^(\w+)-(\d\d?)$/) {
+ my $c = $1;
+ my $s = $2;
+ $s = 15 - $s;
+ if ($s <= 8 && $s > 0) {
+ $call = "${c}-${s}";
+ } else {
+ $call = $c;
+ }
+ }
$conn->to_connected($call, 'A', $conn->{csort} = 'ax25');
}
} elsif ($sort eq 'd') {
- dbg('agw', "AGW '$from'->'$to' Disconnected");
- my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
- $conn->in_disconnect if $conn;
+ dbg('agw', "AGW '$from'->'$to' port: $port Disconnected");
+ my $conn = _find($from eq $main::mycall ? $to : $from);
+ if ($conn) {
+ &{$conn->{eproc}}() if $conn->{eproc};
+ $conn->in_disconnect;
+ }
} elsif ($sort eq 'y') {
my ($frames) = unpack "V", $data;
- dbg('agw', "AGW Frames Outstanding on port $port = $frames");
- my $conn = Msg->conns($from);
+ dbg('agwpollans', "AGW Frames Outstanding on port $port = $frames");
+ my $conn = _find($from);
$conn->{oframes} = $frames if $conn;
} elsif ($sort eq 'Y') {
my ($frames) = unpack "V", $data;
dbg('agw', "AGW Frames Outstanding on circuit '$from'->'$to' = $frames");
- my $conn = Msg->conns($from eq $main::mycall ? $to : $from);
+ my $conn = _find($from eq $main::mycall ? $to : $from);
$conn->{oframes} = $frames if $conn;
+ } elsif ($sort eq 'H') {
+ unless ($from =~ /^\s+$/) {
+ my $d = unpack "Z*", $data;
+ $d =~ s/\cM$//;
+ dbg('agw', "AGW Heard port: $port \"$d\"");
+ }
} elsif ($sort eq 'X') {
my ($r) = unpack "C", $data;
$r = $r ? "Successful" : "Failed";
dbg('agw', "AGW Version $major.$minor");
} elsif ($sort eq 'G') {
my @ports = split /;/, $data;
- dbg('agw', "AGW $ports[0] Ports available");
- my $n = shift @ports;
- pop @ports while @ports > $n;
+ $noports = shift @ports || '0';
+ dbg('agw', "AGW $noports Ports available");
+ pop @ports while @ports > $noports;
for (@ports) {
next unless $_;
dbg('agw', "AGW Port: $_");
}
+ for (my $i = 0; $i < $noports; $i++) {
+ _sendf('y', undef, undef, $i );
+ }
} else {
- dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$data\"");
+ my $d = unpack "Z*", $data;
+ dbg('agw', "AGW decode $sort port: $port pid: $pid '$from'->'$to' length: $len \"$d\"");
}
}
+sub _find
+{
+ my $call = shift;
+ return $circuit{$call};
+}
+
+sub connect
+{
+ my ($conn, $line) = @_;
+
+ my ($port, $call) = split /\s+/, $line;
+ $conn->{agwpid} = ord "\xF0";
+ $conn->{agwport} = $port - 1;
+ $conn->{lineend} = "\cM";
+ $conn->{incoming} = 0;
+ $conn->{csort} = 'ax25';
+ $conn->{agwcall} = uc $call;
+ $circuit{$conn->{agwcall}} = $conn;
+
+ _sendf('C', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
+ $conn->{state} = 'WC';
+
+ return 1;
+}
+
sub in_disconnect
{
my $conn = shift;
- $conn->SUPER->disconnect;
+ delete $circuit{$conn->{agwcall}};
+ $conn->SUPER::disconnect;
}
sub disconnect
{
my $conn = shift;
- _sendf('d', $main::mycall, $conn->{call});
- $conn->SUPER->disconnect;
+ delete $circuit{$conn->{agwcall}};
+ if ($conn->{incoming}) {
+ _sendf('d', $conn->{agwcall}, $main::mycall, $conn->{agwport}, $conn->{agwpid});
+ } else {
+ _sendf('d', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid});
+ }
+ $conn->SUPER::disconnect;
+}
+
+sub enqueue
+{
+ my ($conn, $msg) = @_;
+ if ($msg =~ /^D/) {
+ $msg =~ s/^[-\w]+\|//;
+# _sendf('Y', $main::mycall, $conn->{call}, $conn->{agwport}, $conn->{agwpid});
+ _sendf('D', $main::mycall, $conn->{agwcall}, $conn->{agwport}, $conn->{agwpid}, $msg . $conn->{lineend});
+ my $len = length($msg) + 1;
+ dbg('agw', "AGW Data Out port: $conn->{agwport} pid: $conn->{agwpid} '$main::mycall'->'$conn->{agwcall}' length: $len \"$msg\"");
+ }
+}
+
+sub process
+{
+ return unless $sock;
+ if ($ypolltime && $main::systime - $lastytime >= $ypolltime) {
+ for (my $i = 0; $i < $noports; $i++) {
+ _sendf('y', undef, undef, $i );
+ }
+ $lastytime = $main::systime;
+ }
+ if ($hpolltime && $main::systime - $lasthtime >= $hpolltime) {
+ for (my $i = 0; $i < $noports; $i++) {
+ _sendf('H', undef, undef, $i );
+ }
+ $lasthtime = $main::systime;
+ }
}
1;