-
Notifications
You must be signed in to change notification settings - Fork 22
/
LOC.pm
208 lines (149 loc) · 5.14 KB
/
LOC.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package TRBO::LOC;
=head1 NAME
TRBO::LOC - trbo location packet parser.
=head1 DESCRIPTION
=head1 FUNCTIONS
=cut
use strict;
use warnings;
use Data::Dumper;
use TRBO::Common;
our @ISA = ('TRBO::Common');
=over
=item decode($data)
Decodes packet on location port
=back
=cut
sub decode($$$)
{
my($self, $rh, $data) = @_;
$self->_rx_accounting($data);
my $plen = length($data);
if ($plen < 2) {
return $self->_fail($rh, 'plen_short');
}
$rh->{'class'} = 'loc';
my $cmd = unpack('C', substr($data, 0, 1));
my $dlen = unpack('C', substr($data, 1, 1));
$self->_debug(sprintf("LOC cmd %02x dlen $dlen plen $plen", $cmd));
if ($dlen != $plen - 2) {
return $self->_fail($rh, 'plen_mismatch');
}
if ($cmd == 0x0d) {
my $token = unpack('C', substr($data, 8, 1));
$self->_debug(sprintf("0x0d: location report, token %02x", $token));
if ($token != 0x69 && $token != 0x51) {
$self->_info(sprintf("%s: LOC RX (timed) token 0x%02x indicates no GPS fix", $rh->{'src_id'}, $token));
return 1;
}
return $self->_decode_loc($rh, $data, $plen);
} elsif ($cmd == 0x07) {
my $token = unpack('C', substr($data, 8, 1));
$self->_debug(sprintf("0x07: immediate location answer, token %02x", $token));
if ($token != 0x69 && $token != 0x51) {
$self->_info(sprintf("%s: LOC RX (immediate) token 0x%02x indicates no GPS fix", $rh->{'src_id'}, $token));
return 1;
}
return $self->_decode_loc($rh, $data, $plen);
} elsif ($cmd == 0x0b) {
$self->_debug("0x0b: command reply: " . TRBO::Common::_hex_dump(substr($data, 2)));
if ($dlen == 8) {
my $op = unpack('n', substr($data, 8, 2));
$self->_debug(sprintf("0x0b op %04x", $op));
if ($op == 0x3716) {
$self->_debug("0x0b: command reply: 0x3716, probably OK");
}
$rh->{'msg'} = 'ack';
return 1;
} elsif ($dlen == 7) {
my $op = unpack('C', substr($data, 8, 1));
$self->_debug(sprintf("0x0b op %02x", $op));
if ($op == 0x38) {
$self->_debug("0x0b: command reply: 0x38, probably OK");
}
$rh->{'msg'} = 'ack';
return 1;
}
} elsif ($cmd == 0x11) {
$self->_debug("0x11: command reply: " . TRBO::Common::_hex_dump(substr($data, 2)));
$rh->{'msg'} = 'ack';
return 1;
}
return $self->_fail($rh, 'unknown');
}
sub _decode_loc($$$$)
{
my($self, $rh, $data, $plen) = @_;
# Latitude and longitude are packed as network byte order integers with
# little scaling to make them firmly integers in packet.
# This was pain to get right. GPS simulator helped.
# First bit in integer is sign.
my $x = 45.0 / 1073741824.0;
# Old Code working
my $lat = unpack('N', substr($data, 9, 4)) * $x;
my $lng = -(360-unpack('N', substr($data, 13, 4)) * 2 * $x);
# New code Islas Cainman
# my $lat_i = unpack('N', substr($data, 9, 4));
# my $lat = ($lat_i & 0x7FFFFFFF) * $x;
# $lat *= -1 if ($lat_i & 0x80000000);
# my $lng_i = unpack('N', substr($data, 13, 4));
# my $lng = ($lng_i & 0x7FFFFFFF) * 2 * $x;
# $lng *= -1 if ($lng_i & 0x80000000);
# Altitude and speed probably encoded bit like in BER/ASN.1/SNMP:
# "base-128 in big-endian order where the 8th bit is 1 if more bytes follow and 0 for the last byte"
# (the same encoding is used in ARS config packets too)
my $i = 17; # this is where altitude and speed probably are.
my($alt, $speed) = (0, 0);
$self->_info(sprintf('%s: LOC RX lat %.6f lng %.6f speed %d alt %d', $rh->{'src_id'}, $lat, $lng, $speed, $alt));
$rh->{'msg'} = 'loc';
$rh->{'latitude'} = $lat;
$rh->{'longitude'} = $lng;
#warn "addr: '$addr'\n";
return 1;
}
=over
=item request_locs($id, $interval)
Send request to radio to send location packets
at specified interval.
=back
=cut
sub request_locs($$$)
{
my($self, $id, $interval) = @_;
$self->_info($id . ": Requesting location every $interval s");
# disable loc sends / cancel old requests
# apparently radio not take in new command otherwise!
$self->request_no_locs($id);
sleep(1);
# have no idea what this really contains, except for interval which
# is BER encoded in end.
$self->_send($id,
pack('n*', 0x2204, 0x3727, 0x1707)
. pack('n*', 0x5074, 0x6934)
. pack('C', 0x31)
. $self->_encode_ber_uint($interval)
, pack('C', 0x09)); # command 0x9
}
=over
=item request_no_locs($id)
Send request to radio not to send location packets.
=back
=cut
sub request_no_locs($$$)
{
my($self, $id, $interval) = @_;
# disable loc sends?
$self->_send($id,
pack('n*', 0x2204, 0x3727, 0x1707)
, pack('C', 0x0f)); # command 0xf
}
sub _pack($$;$)
{
my($self, $data, $prefix) = @_;
my $out = pack('C', length($data)) . $data;
if (defined $prefix) {
$out = $prefix . $out;
}
return $out;
}
1;