forked from KD8EYF/TRBO-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARS.pm
151 lines (95 loc) · 2.41 KB
/
ARS.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
package TRBO::ARS;
=head1 NAME
TRBO::ARS - ARS parser.
=head1 ABSTRACT
=head1 DESCRIPTION
=head1 FUNCTIONS
=cut
# Packets obtained using wireshark:
#
# query radio: "are you there?"
# 00 01 74
# answer "yes i'm here"
# 00 01 3f
# radio says "i'm going away."
# 00 01 31
use strict;
use warnings;
use Data::Dumper;
use TRBO::Common;
our @ISA = ('TRBO::Common');
=over
=item decode($data)
Decode Automatic Registration Service packet
=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');
}
my $dlen = unpack('n', substr($data, 0, 2));
$self->_debug("dlen $dlen plen $plen");
if ($dlen != $plen - 2) {
return $self->_fail($rh, 'plen_mismatch');
}
$rh->{'class'} = 'ars';
my($op, $n) = $self->_decode_ber_uint($data, 2);
$self->_debug("ars op " . sprintf('%04x', $op));
if ($op == 0x3820) {
# first HELLO after powerup is always 0x3820
return $self->_decode_hello($rh, substr($data, 4));
} elsif ($op == 0x3840) {
# following HELLOs, maybe 30 mins from initial, are
# 0x3840. Go figure.
return $self->_decode_hello($rh, substr($data, 4));
} elsif ($op == 0x3f) {
$self->_debug("ARS pong from " . $rh->{'src_id'});
$rh->{'msg'} = 'pong';
return 1;
} elsif ($op == 0x31) {
$self->_info($rh->{'src_id'} . ": LEAVE NET");
$rh->{'msg'} = 'leave';
return 1;
}
return $self->_fail($rh, 'unknown');
}
sub _decode_hello($$$)
{
my($self, $rh, $data) = @_;
my $addr_len = unpack('C', substr($data, 0, 1));
#warn "addr_len $addr_len\n";
my $addr = substr($data, 1, $addr_len);
$rh->{'msg'} = 'hello';
$rh->{'id'} = $addr;
#warn "addr: '$addr'\n";
$self->_info("$addr: HELLO received ($addr)");
# should be answered with 00 02 BF 01
return 1;
}
=over
=item ack_hello($id)
Sends ACK to HELLO packet
=back
=cut
sub ack_hello($$)
{
my($self, $id) = @_;
$self->_debug("sending hello ack to $id");
$self->_send($id, pack('n', 0xBF01));
}
=over
=item ping($id)
Ping radio
=back
=cut
sub ping($$)
{
my($self, $id) = @_;
$self->_debug("sending ping to $id");
$self->_send($id, pack('C', 0x74));
}
1;