#!/opt/bin/perl # http://www.dia.unisa.it/professori/ads/corso-security/www/CORSO-9900/a5/Netsec/netsec.html use strict; use POSIX; use Event; my %tput_cache; sub tput { $tput_cache{join "\x00", @_} ||= qx } my $tty = $ARGV[0] || "/dev/ttyS0"; open my $aor, "+<:raw", $tty or die "$tty: $!"; { my $tio = new POSIX::Termios; $tio->getattr (fileno $aor); $tio->setiflag (POSIX::IXOFF | POSIX::IXON); $tio->setoflag (0); $tio->setcflag (POSIX::CLOCAL | POSIX::CREAD | POSIX::CSTOPB | POSIX::CS8); $tio->setlflag (0); $tio->setispeed (&POSIX::B9600); $tio->setospeed (&POSIX::B9600); $tio->setattr (fileno $aor); } my @info; my @output; sub refresh { $| = 0; print tput "cup", 0, 0; my $clreol = tput "el"; for (0..15) { print "$info[$_]$clreol\n"; } shift @output while @output > 8; print join "$clreol\n", @output; print "$clreol\n> "; print tput "ed"; $| = 1; } Event->timer (after => 0.5, interval => 1, cb => sub { print $aor "RX\015\012LM\015\012LC\015\012"; }); Event->io (fd => \*STDIN, poll => 'r', cb => sub { my $line = <>; $line =~ s/[\015\012]+$//; $line = "\x1e" if $line eq "u"; $line = "\x1f" if $line eq "d"; print $aor uc "$line\015\012LC\015\012"; }); my %mode = ( 0 => "WFM", 1 => "NFM", 2 => "AM", 3 => "USB", 4 => "LSB", 5 => "CW", ); my %info; my $rbuf; Event->io (fd => $aor, poll => 'r', cb => sub { sysread $aor, $rbuf, 8192, length $rbuf; while ($rbuf =~ s/^(.*)[\012\015]+//s) { local $_ = $1; s/[\012\015]//g; $info{substr $_, 0, 2} = $_; $info{freq} = "00 freq $1" if /\bRF(\d+)\b/; $info{mode} = "01 mode $mode{$1}" if /\bMD(\d+)\b/; @info = sort values %info; push @output, $_ unless /^($|SS|LM|VF|MR|MS|SM)/; } refresh; }); refresh; Event::loop;