-
Notifications
You must be signed in to change notification settings - Fork 53
/
unpacker.pl
227 lines (164 loc) · 5.41 KB
/
unpacker.pl
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/perl -w
# ============= FROM UNPACKER.C ====================
# This source code is part of the AIS BlackToolkit.
# Unpacker.c allows you to build a NMEA sentece out of its payload. Normally used in combination with AIVDM_Encoder.
# Copyright 2013-2014 -- Embyte & Pastus
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# ==================================================
# This is a Perl version of the C code in the AIS BlackToolkit distribution
# (c) Gary C. Kessler, 2018
# Version -- 09/06/2018 (v0.1)
# Usage example:
# $ ./AIVDM_Encoder.py --type=1 --vsize=30x10 | xargs -IX ./unpacker.pl X 1 A
# The command above:
# 1) Runs AIVDM_encoder with whatever parameters and creates a binary string output
# 2) xargs takes the binary string and assigns it to "X"
# 3) Run unpacker with three arguments:
# X is the binary string output from AIVDM_Encoder
# 1 is the enable_nmea value (1 = ON, else OFF)
# A is the AIS channel (should be either A or B)
# Parse command line
if (parsecommandline ())
{ exit 1; };
$strlen = length ($in_string);
# Error checking. Ensure that string is a multiple of 6 bits and composed of 0s and 1s; NMEA value is 0 or 1; and
# Channel is A or B
if ($strlen % 6 != 0)
{
print "Input length not an even multiple of 6...\n";
exit;
}
for ($i=0; $i<$strlen; $i++)
{
$tyui = substr ($in_string, $i, 1);
if ($tyui ne "0" && $tyui ne "1")
{
print "Input contains non-binary value... $tyui\n";
exit;
}
}
if ($enable_nmea ne "0" && $enable_nmea ne "1")
{
print "Enable NMEA value not 0 or 1... $enable_nmea\n";
exit;
}
if ($channel ne "A" && $channel ne "B")
{
print "Channel must be A or B... $channel\n";
exit;
}
print "NMEA value: $enable_nmea Channel: $channel\n";
print "Input: $in_string\n";
print "Armored ASCII: ";
# Manipulate $in_string from binary to the NMEA 6-bit "armored" ASCII
for ($i=0; $i<$strlen/6; $i++)
{
# One character = 6 bits, so get the 6-bit block
$in_buffer [$i] = substr ($in_string, $i*6, 6);
# Convert the 6 binary bits to an integer value
$in_buffer [$i] = convert ($in_buffer [$i], 6);
# Now convert the value to an ASCII character
if ($in_buffer [$i] > 39)
{ $in_buffer [$i] += 8; }
$in_buffer [$i] += 48;
$in_buffer [$i] = chr ($in_buffer [$i]);
print $in_buffer [$i];
}
print "\n\n";
# Prepare AIS Message
# If enable_nmea = 1, then prepare NMEA header
# Next part is the ASCII string
# If enable_nmea=1, add compute and add checksum
$AIS_message = "";
if ($enable_nmea == 1)
{ $AIS_message = "!AIVDM,1,1,," . $channel . ","; }
for ($i=0; $i<$strlen/6; $i++)
{ $AIS_message = $AIS_message . $in_buffer [$i]; }
if ($enable_nmea == 1)
{
$AIS_message = $AIS_message . ",0";
$checksum = nmea_checksum ($AIS_message);
$AIS_message = $AIS_message . "*" . $checksum;
}
print "AIS Message: $AIS_message\n\n";
# ********************************************************
# ***** SUBROUTINE BLOCKS *****
# ********************************************************
# ********************************************************
# nmea_checksum
# ********************************************************
sub nmea_checksum
# The NMEA checksum is computed on the entire sentence including the AIVDM/AIVDO tag but excluding the leading "!"
# The checksum is merely a bybe-by-byte XOR of the sentence
{
my ($sentence, $char, $i, $n, $len, $sum);
$sentence = $_[0];
$len = length ($sentence);
$sum = 0;
if (substr ($sentence,0,1) eq "!")
{ $n = 1; }
else
{ $n = 0; }
for ($i=$n; $i<$len; $i++)
{ $sum ^= ord (substr ($sentence,$i,1)); }
# Ensure that $sum is two upper-case hex digits
$sum = sprintf ("%02X", $sum);
return $sum;
}
# ********************************************************
# convert
# ********************************************************
sub convert
{
my ($j, $sum);
my ($str, $n);
$str = $_[0]; $n = $_[1];
$sum=0;
for ($j=0; $j<$n; $j++)
{
if (substr ($str,$j,1) == 1) { $sum += 2 ** ($n-$j-1); }
}
return $sum;
}
# ********************************************************
# help_text
# ********************************************************
# Display the help file
sub help_text
{
print<< "EOT";
Program usage: unpacker S N C
where: S is the input string (an even multiple of 6 bits in length
composed of 0s and 1s)
N is 1 to output an NMEA sentence or 0
C is the broadcast channel (A or B)
EOT
return;
}
# ********************************************************
# parsecommandline
# ********************************************************
# Parse command line for file name, if present. Query
# user for any missing information
# Return $state = 1 to indicate that the program should stop
# immediately (switch -h)
sub parsecommandline
{
my $state = 0;
# Parse command line switches ($ARGV array of length $#ARGV)
if ($#ARGV == 2)
{
$in_string = $ARGV[0];
$enable_nmea = $ARGV[1];
$channel = $ARGV[2];
}
else
{
help_text();
$state = 1;
}
return $state;
}