| 1 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Device::RFXCOM::Base; | ||||||||||
| 4 | 5 | $Device::RFXCOM::Base::VERSION = '1.142010'; | |||||||||
| 5 | # ABSTRACT: module for RFXCOM device base class | ||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | 5 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | use constant { | ||||||||||
| 10 | DEBUG => $ENV{DEVICE_RFXCOM_BASE_DEBUG}, | ||||||||||
| 11 | TESTING => $ENV{DEVICE_RFXCOM_TESTING}, | ||||||||||
| 12 | 5 | ▶ | }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 5 | ▶ | use Carp qw/croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | ▶ | use IO::Select; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 5 | ▶ | use Time::HiRes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | use Symbol qw/gensym/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | ▶ | use Device::SerialPort qw( :PARAM :STAT 0.07 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 5 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | sub _new { | ||||||||||
| 22 | 12 | ▶ | my ($pkg, %p) = @_; | ||||||||
|
Subroutine
_new: called CC=2 SCAR=6.9
| |||||||||||
| 23 | 12 | my $self = bless | |||||||||
| 24 | { | ||||||||||
| 25 | baud => 4800, | ||||||||||
| 26 | port => 10001, | ||||||||||
| 27 | discard_timeout => 0.03, | ||||||||||
| 28 | ack_timeout => 2, | ||||||||||
| 29 | dup_timeout => 0.5, | ||||||||||
| 30 | _q => [], | ||||||||||
| 31 | _buf => '', | ||||||||||
| 32 | _last_read => 0, | ||||||||||
| 33 | init_callback => undef, | ||||||||||
| 34 | %p, | ||||||||||
| 35 | }, $pkg; | ||||||||||
| 36 | 12 | ▶ | $self->{plugins} = [$self->plugins()] unless ($self->{plugins}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 12 | $self->_open(); | |||||||||
| 38 | 6 | $self->_init(); | |||||||||
| 39 | 6 | $self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | sub DESTROY { | ||||||||||
| 43 | 12 | ▶ | my $self = shift; | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 12 | delete $self->{init}; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | sub queue { | ||||||||||
| 49 | 1 | ▶ | scalar @{$_[0]->{_q}}; | ||||||||
|
Subroutine
queue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | sub _write { | ||||||||||
| 54 | 22 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_write: called CC=4 SCAR=13.9
| |||||||||||
| 55 | 22 | my %p = @_; | |||||||||
| 56 | 22 | ▶ | $p{raw} = pack 'H*', $p{hex} unless (exists $p{raw}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 22 | ▶ | $p{hex} = unpack 'H*', $p{raw} unless (exists $p{hex}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 22 | print STDERR "Queued: ", $p{hex}, ' ', ($p{desc}||''), "\n" if DEBUG; | |||||||||
| 59 | 22 | push @{$self->{_q}}, \%p; | |||||||||
| 60 | 22 | ▶ | $self->_write_now unless ($self->{_waiting}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 22 | 1; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub _write_now { | ||||||||||
| 65 | 28 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_write_now: called CC=4 SCAR=13.9
| |||||||||||
| 66 | 28 | my $rec = shift @{$self->{_q}}; | |||||||||
| 67 | 28 | my $wait_record = $self->{_waiting}; | |||||||||
| 68 | 28 | ▶ | if ($wait_record) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 19 | delete $self->{_waiting}; | |||||||||
| 70 | 19 | my $cb = $wait_record->[1]->{callback}; | |||||||||
| 71 | 19 | ▶ | $cb->() if ($cb); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | } | ||||||||||
| 73 | 28 | ▶ | return unless (defined $rec); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 19 | $self->_real_write($rec); | |||||||||
| 75 | 19 | $self->{_waiting} = [ $self->_time_now, $rec ]; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub _real_write { | ||||||||||
| 79 | 19 | ▶ | my ($self, $rec) = @_; | ||||||||
|
Subroutine
_real_write: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 19 | print STDERR "Sending: ", $rec->{hex}, ' ', ($rec->{desc}||''), "\n" if DEBUG; | |||||||||
| 81 | 19 | syswrite $self->{fh}, $rec->{raw}, length $rec->{raw}; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | |||||||||||
| 85 | sub filehandle { | ||||||||||
| 86 | shift->{fh} | ||||||||||
| 87 | 26 | ▶ | } | ||||||||
|
Subroutine
filehandle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 88 | |||||||||||
| 89 | sub _open { | ||||||||||
| 90 | 11 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_open: called CC=2 SCAR=6.9
| |||||||||||
| 91 | 11 | ▶ | $self->{device} =~ m![/\\]! ? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | $self->_open_serial_port(@_) : $self->_open_tcp_port(@_) | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | sub _open_tcp_port { | ||||||||||
| 96 | 9 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_open_tcp_port: called CC=3 SCAR=11.0
| |||||||||||
| 97 | 9 | my $dev = $self->{device}; | |||||||||
| 98 | 9 | print STDERR "Opening $dev as tcp socket\n" if DEBUG; | |||||||||
| 99 | 9 | require IO::Socket::INET; import IO::Socket::INET; | |||||||||
| 100 | 9 | ▶ | $dev .= ':'.$self->{port} unless ($dev =~ /:/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 9 | ▶ | my $fh = IO::Socket::INET->new($dev) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | croak "TCP connect to '$dev' failed: $!"; | ||||||||||
| 103 | 4 | return $self->{fh} = $fh; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | sub _open_serial_port { | ||||||||||
| 107 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_open_serial_port: called CC=3 SCAR=11.0
| |||||||||||
| 108 | 2 | my $dev = $self->{device}; | |||||||||
| 109 | 2 | print STDERR "Opening $dev as serial port\n" if DEBUG; | |||||||||
| 110 | 2 | my $fh = gensym(); | |||||||||
| 111 | 2 | ▶ | my $sport = tie (*$fh, 'Device::SerialPort', $dev) or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | croak "Could not tie serial port, $dev, to file handle: $!"; | ||||||||||
| 113 | 2 | $sport->baudrate($self->baud); | |||||||||
| 114 | 2 | $sport->databits(8); | |||||||||
| 115 | 2 | $sport->parity("none"); | |||||||||
| 116 | 2 | $sport->stopbits(1); | |||||||||
| 117 | 2 | $sport->datatype("raw"); | |||||||||
| 118 | 2 | $sport->write_settings(); | |||||||||
| 119 | |||||||||||
| 120 | 2 | ▶ | sysopen $fh, $dev, O_RDWR|O_NOCTTY|O_NDELAY or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | croak "sysopen of '$dev' failed: $!"; | ||||||||||
| 122 | 1 | $fh->autoflush(1); | |||||||||
| 123 | 1 | binmode($fh); | |||||||||
| 124 | 1 | return $self->{fh} = $fh; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | sub baud { | ||||||||||
| 129 | shift->{baud} | ||||||||||
| 130 | 3 | ▶ | } | ||||||||
|
Subroutine
baud: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 131 | |||||||||||
| 132 | sub _time_now { | ||||||||||
| 133 | 132 | ▶ | Time::HiRes::time | ||||||||
|
Subroutine
_time_now: called CC=1 SCAR=0.0
| |||||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 5 | 1; | |||||||||
| 137 | |||||||||||
| 138 | __END__ | ||||||||||