IS.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 237 SCAR 68.4
CC
30
Cov
0%
connect
110.0
getline
90.0
sendline
30.0
CRAP 930.0
1  
2 package Ham::APRS::IS;
3  
4 =head1 NAME
5  
6 Ham::APRS::IS - An APRS-IS client module
7  
8 =head1 SYNOPSIS
9  
10 use Ham::APRS::IS;
11 use Ham::APRS::FAP qw(parseaprs);
12
13 my $is = new Ham::APRS::IS('aprs.example.com:12345', 'N0CALL', 'appid' => 'IS-pm-test 1.0');
14 $is->connect('retryuntil' => 3) || die "Failed to connect: $is->{error}";
15
16 for (my $i = 0; $i < 10; $i += 1) {
17 my $l = $is->getline_noncomment();
18 next if (!defined $l);
19 print "\n--- new packet ---\n$l\n";
20
21 my %packetdata;
22 my $retval = parseaprs($l, \%packetdata);
23
24 if ($retval == 1) {
25 while (my ($key, $value) = each(%packetdata)) {
26 print "$key: $value\n";
27 }
28 } else {
29 warn "Parsing failed: $packetdata{resultmsg} ($packetdata{resultcode})\n";
30 }
31 }
32
33 $is->disconnect() || die "Failed to disconnect: $is->{error}";
34  
35 =head1 ABSTRACT
36  
37 This module is a client library for the APRS-IS. It has an object-oriented
38 interface which contains methods to connect and disconnect from a server,
39 and to read and write lines on the connection.
40  
41 =head1 DESCRIPTION
42  
43 Unless a debugging mode is enabled, all errors and warnings are reported
44 through the API (as opposed to printing on STDERR or STDOUT), so that
45 they can be reported nicely on the user interface of an application.
46  
47 =head1 EXPORT
48  
49 None by default.
50  
51 =head1 FUNCTION REFERENCE
52  
53  
54 =cut
55  
56  
57 use 5.006;
58 use strict;
59 use warnings;
60  
61 use Time::HiRes qw( time sleep );
62 use IO::Handle '_IOFBF';
63 use IO::Socket::INET;
64 use IO::Select;
65  
66 our $VERSION = '1.18';
67  
68 our $default_appid = "IS.pm $VERSION";
69  
70 =head1 new(hostport, mycall, option)
71  
72 Initializes a new Ham::APRS::IS socket. Takes two mandatory arguments,
73 the host:port pair to connect to and your client's callsign, and one or more
74 optional named options:
75  
76 =over
77  
78 =item * filter: an APRS-IS filter string sent to the server
79  
80 =item * passcode: an APRS-IS passcode
81  
82 =item * appid: your application's name and version number direction finding
83  
84 =back
85  
86 my $is = new Ham::APRS::IS('aprs.server.com:12345', 'N0CALL', 'appid' => 'myapp 3.4b');
87 my $is = new Ham::APRS::IS('aprs.server.com:12345', 'N0CALL', 'appid' => 'foobar 42', 'filter' => 'f/*');
88 my $is = new Ham::APRS::IS('aprs.server.com:12345', 'N0CALL', 'passcode' => 1234, 'appid' => 'myapp 1.2');
89  
90 =cut
91  
92 sub new($$$;%)
93 {
94 my $that = shift;
95 my $class = ref($that) || $that;
96 my $self = { };
97 bless ($self, $class);
98
99 my($host_port, $mycall, %options) = @_;
100
101 $self->{'host_port'} = $host_port;
102 $self->{'mycall'} = $mycall;
103 $self->{'filter'} = $options{'filter'} if (defined $options{'filter'});
104
105 if ($options{'passcode'}) {
106 $self->{'passcode'} = $options{'passcode'};
107 } else {
108 $self->{'passcode'} = -1;
109 }
110
111 if ($options{'appid'}) {
112 $self->{'appid'} = $options{'appid'};
113 } else {
114 $self->{'appid'} = $default_appid;
115 }
116
117 $self->{'state'} = 'init';
118 $self->{'error'} = "No errors yet.";
119
120 return $self;
121 }
122  
123 =head1 disconnect( )
124  
125 Disconnects from the server. Returns 1 on success, 0 on failure.
126  
127 $is->disconnect() || die "Failed to disconnect: $is->{error}";
128  
129 =cut
130  
131 sub disconnect($)
132 {
133 my($self) = @_;
134
135 if (defined $self->{'sock'}) {
136 $self->{'sock'}->close;
137 undef $self->{'sock'};
138 }
139
140 $self->{'state'} = 'disconnected';
141
142 return 1;
143 }
144  
145 =head1 connect(options)
146  
147 Connects to the server. Returns 1 on success, 0 on failure.
148 Takes an optional options hash as a parameter. Currently knows only one parameter,
149 retryuntil, which specifies the number of seconds to retry the connection. After
150 each failed attempt the code sleeps for 0.5 seconds before trying again. Defaults
151 to 0 (no retries).
152  
153 $is->connect('retryuntil' => 10) || die "Failed to connect: $is->{error}";
154  
155 =cut
156  
157 sub connect($;%)
158 {
159 my($self) = shift;
160
161 my %options = @_;
162
163 if ($self->{'state'} eq 'connected') {
164 $self->{'error'} = 'Already connected';
165 return 0;
166 }
167
168 $self->{'ibuf'} = '';
169
170 my $retryuntil = defined $options{'retryuntil'} ? $options{'retryuntil'} : 0;
171 my $starttime = time();
172
173 while (!defined $self->{'sock'}) {
174 $self->{'sock'} = IO::Socket::INET->new($self->{'host_port'});
175
176 if (!defined($self->{'sock'})) {
177 $self->{'error'} = "Failed to connect to $self->{host_port}: $!";
178
179 if (time() - $starttime >= $retryuntil) {
180 return 0;
181 }
182
183 select(undef, undef, undef, 0.5);
184 }
185 }
186
187 $self->{'error'} = 'Connected successfully';
188
189 # printf ( "APRS::IS->new() mycall='%s' passcode=%d filterre='%s'\n",
190 # $self->{aprsmycall}, $self->{passcode}, $self->{filterre} );
191
192 ##
193 ## * Need to send on initial connect the following logon line:
194 ## user callsign pass passcode vers appname versionnum rest_of_line
195 ##
196 ## callsign = login callsign-SSID
197 ## passcode = login passcode per APRS-IS algorithm, -1 = read-only
198 ## appname = application name (1 word)
199 ## versionnum = application version number (no spaces)
200 ## rest_of_line = server command if connecting to a port that supports commands (see Server Commands)
201 ##
202 ## (appname and versionnum should not exceed 15 characters)
203 ##
204 ##
205 ## * Need to recognize both TCPIP and TCPXX as TCP/IP stations
206 ## * Need to provide a means to perform the user validation. This can either be a user entered password,
207 ## or a client program can automatically figure out the password given the callsign.
208 ## If the later is used, it is the client programmer's responsibility to be certain that non-amateurs
209 ## are not given registrations that can validate themselves in APRS-IS.
210 ## * Probably a good idea to perform some feedback about the limitations of TCPIP without a registration number.
211 ##
212
213 $self->{'sock'}->blocking(1);
214 $self->{'state'} = 'connected';
215
216 my $s;
217 if (defined($self->{'filter'})) {
218 $s = sprintf("user %s pass %s vers %s filter %s\r\n",
219 $self->{'mycall'},
220 $self->{'passcode'}, # -- but we are read-only !
221 $self->{'appid'}, $self->{'filter'} );
222 } else {
223 $s = sprintf("user %s pass %s vers %s\r\n",
224 $self->{'mycall'},
225 $self->{'passcode'}, # -- but we are read-only !
226 $self->{'appid'} );
227 }
228
229 #warn "login: $s\n";
230 if (!$self->{'sock'}->print($s)) {
231 $self->{'error'} = "Failed to write login command to $self->{host_port}: $!";
232 return 0;
233 }
234
235 if (!$self->{'sock'}->flush) {
236 $self->{'error'} = "Failed to flush login command to $self->{host_port}: $!";
237 return 0;
238 }
239
240 $self->{'sock'}->blocking(0);
241
242 my $t = time();
243 while (my $l = $self->getline()) {
244 return 1 if ($l =~ /^#\s+logresp\s+/);
245 if (time() - $t > 5) {
246 $self->{'error'} = "Login command timed out";
247 return 0;
248 }
249 }
250
251 return 1;
252 }
253  
254 =head1 connected( )
255  
256 Checks whether we're connected currently. Returns 1 for connected, 0 for not connected.
257  
258 =cut
259  
260 sub connected($)
261 {
262 my($self) = @_;
263
264 return 1 if $self->{'state'} eq 'connected';
265 return 0;
266 }
267  
268  
269  
270 # -------------------------------------------------------------------------
271 # Get a line (blocking)
272  
273 =head1 getline(timeout)
274  
275 Reads a line from the server (blocking). Returns the line read,
276 or undefined if the reading fails. Takes an optional timeout argument,
277 which tells getline to stop reading after the specified amount of
278 secounds. The timeout defaults to 5 seconds.
279  
280 $l = $is->getline();
281 die "Failed to read: $is->{error}" if (!defined $l);
282  
283 The returned line does not contain the <CR><LF> line feed used as a
284 separator between packets on the APRS-IS.
285  
286 =cut
287  
288  
289 sub getline($;$)
290 {
291 my($self, $timeout) = @_;
292
293 if ($self->{'state'} ne 'connected') {
294 $self->{'error'} = "getline: not connected";
295 return undef;
296 }
297
298 $timeout = 5 if (!defined $timeout);
299
300 my $end_t = time() + $timeout;
301 my $sock = $self->{'sock'};
302
303 while (1) {
304 if ($self->{'ibuf'} =~ s/^(.*?)[\r\n]+//s) {
305 #warn "got: $1\n";
306 return $1;
307 }
308
309 if (time() > $end_t) {
310 $self->{'error'} = "getline: timeout";
311 return undef;
312 }
313
314 my($rin, $rout, $ein, $eout) = ('', '', '', '');
315 vec($rin, fileno($sock), 1) = 1;
316 $ein = $rin;
317 my $nfound = select($rout = $rin, undef, $eout = $ein, 1);
318
319 if (($nfound) && ($rout)) {
320 my $rbuf;
321 my $nread = sysread($sock, $rbuf, 1024);
322 if ($nread > 0) {
323 $self->{'ibuf'} .= $rbuf;
324 } elsif ($nread < 1) {
325 $self->{'error'} = "Failed to read from server: $!";
326
327 $self->disconnect();
328 return undef;
329 }
330 } elsif (($nfound) && ($eout)) {
331 $self->{'error'} = "Failed to read from server (select returned errors): $!";
332 #warn "getline: read error (on select)\n";
333 $self->disconnect();
334 return undef;
335 }
336 }
337 }
338  
339 =head1 getline_noncomment(timeout)
340  
341 Like getline, but only returns noncomment lines (ones which do not
342 begin with a '#' character). The server normally transmits keep-alive timestamp
343 messages, error and status messages as comments.
344  
345 =cut
346  
347 sub getline_noncomment($;$)
348 {
349 my($self, $timeout) = @_;
350
351 return undef if ($self->{'state'} ne 'connected');
352 while (my $l = $self->getline($timeout)) {
353 return $l if !defined $l;
354 return $l if ($l !~ /^#/);
355 }
356 }
357  
358  
359 =head1 sendline(packet)
360  
361 Transmits a line (typically an APRS packet) to the APRS-IS. The line
362 should be a complete packet but WITHOUT the <CR><LF> separator
363 used on the APRS-IS.
364  
365 =cut
366  
367 sub sendline($$)
368 {
369 my($self, $line) = @_;
370 return undef if ($self->{'state'} ne 'connected');
371
372 if (!defined $self->{'sock'}->blocking(1)) {
373 #warn "sendline: blocking(1) failed: $!\n";
374 $self->{'error'} = "sendline: blocking(1) failed: $!";
375 return undef;
376 }
377 my $ret = $self->{'sock'}->printf( "%s\r\n", $line);
378 if (!$self->{'sock'}->flush) {
379 #warn "sendline: flush() failed: $!\n";
380 $self->{'error'} = "sendline: flush() failed: $!";
381 return undef;
382 }
383
384 if (!defined $self->{'sock'}->blocking(0)) {
385 #warn "sendline: blocking(1) failed: $!\n";
386 $self->{'error'} = "sendline: blocking(1) failed: $!";
387 return undef;
388 }
389
390 #warn "sent ($ret): $line\n";
391 return $ret;
392 }
393  
394 # aprspass($callsign)
395 #
396 # Calculates the APRS passcode for a given callsign. Ignores SSID
397 # and converts the callsign to uppercase as required. Returns an integer.
398 #
399 # my $passcode = Ham::APRS::IS::aprspass($callsign);
400  
401 sub aprspass($)
402 {
403 my($call) = @_;
404
405 $call =~ s/-([^\-]+)$//;
406 $call = uc($call);
407
408 my ($a, $h) = (0, 0);
409 map($h ^= ord(uc) << ($a^=8), $call =~ m/./g);
410 return (($h ^ 29666) & 65535);
411 }
412  
413 =head1 sock()
414  
415 Returns the perl socket used on the connection.
416  
417 =cut
418  
419 sub sock {
420 my $self = shift;
421 return $self->{sock};
422 }
423  
424 1;
425 __END__