-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.c
388 lines (354 loc) · 11.7 KB
/
main.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* -----------------------------------------------------------------------
Test AIS routines
Copyright 2006-2019 by Brian C. Lane
All Rights Reserved
----------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "portable.h"
#include "nmea.h"
#include "sixbit.h"
#include "vdm_parse.h"
#include "test_nmea.h"
#include "test_sixbit.h"
#include "test_vdm_parse.h"
#include "test_seaway.h"
#include "test_access.h"
int main( int argc, char *argv[] )
{
ais_state ais;
unsigned int i;
/* AIS message structures */
aismsg_1 msg_1;
aismsg_2 msg_2;
aismsg_3 msg_3;
aismsg_4 msg_4;
aismsg_5 msg_5;
aismsg_12 msg_12;
aismsg_18 msg_18;
aismsg_24 msg_24;
aismsg_27 msg_27;
/* Test messages for the raw 6-bit parsing code */
#define NUM_TEST_MSGS 13
char *test_msgs[] = { "19NS7Sp02wo?HETKA2K6mUM20<L=",
"35Mk33gOkSG?bLtK?;B2dRO`00`",
"403OwpiuIKl:Ro=sbvK=CG700<3b",
"55Mf@6P00001MUS;7GQL4hh61L4hh6222222220t41H==40HtI4i@E531H1QDTVH51DSCS0",
"703Owpi9lmaQ",
"<03Owph00002QG51D85BP1<5BDQP",
"=03Owpi;Eo7`",
"?03OwpiGPmD0000",
"D03OwphiIN>4",
"B52IRsP005=abWRnlQP03w`UkP06",
"H52IRsP518Tj0l59D0000000000",
"H52IRsTU000000000000000@5120",
"K3M@PpqK>Qkv=PEp"
};
/* Test messages for the vdm_parse function */
#define NUM_DEMO_MSGS 11
char *demo_msgs[] = { "!AIVDM,1,1,,B,15MqvC0Oh9G?qinK?VlPhA480@2n,0*1F,123,14",
"!AIVDM,1,1,,B,15Mf@6P001G?v68K??4SejL<00Sl,0*71",
"!AIVDM,1,1,,B,15Mn4kPP01G?qNvK>:grkOv<0<11,0*55",
"!AIVDM,1,1,,B,15O1Pv0022o?GeNKB3f7QV2>00SP,0*26",
"!AIVDM,1,1,,B,15MqvC0Oh:G?qj0K?Vp@di4B0@5>,0*44",
"!AIVDM,1,1,,B,15Mts3?P@;G8RB@JLbgamrBF0H6B,0*16",
"$BSVDM,1,1,,B,15MqvC0Oh:G?qj0K?Vp@di4B0@5>,0*5D",
"!AIVDM,1,1,,B,15Mts3?P@;G8RB@JLbgamrBF0H6B,0*16,142,aass,12311",
"!AIVDM,1,1,,A,34a=CB1001JwAEhHra0qk4wF2000,0*5B",
"\\s:ASM//Port=63//MMSI=2573225,c:1301961602*7A\\!BSVDM,1,1,,A,13P<JR50h00IkkJQi<Dt29ef0`PL,0*57",
"!AIVDM,1,1,,A,18UG;P0012G?Uq4EdHa=c;7@051@,0*53"
};
if (test_ahextobin() != 1)
{
exit(-1);
}
if (test_nmea_next_field() != 1)
{
exit(-1);
}
if (test_nmea_uint() != 1)
{
exit(-1);
}
if (test_nmea_copy_field() != 1)
{
exit(-1);
}
if (test_find_nmea_start() != 1)
{
exit(-1);
}
if (test_nmea_checksum() != 1)
{
exit(-1);
}
if (test_check_nmea_checksum() != 1)
{
exit(-1);
}
if (test_binfrom6bit() != 1)
{
exit(-1);
}
if (test_init_6bit() != 1)
{
exit(-1);
}
if (test_get_6bit() != 1)
{
exit(-1);
}
if (test_ais2ascii() != 1)
{
exit(-1);
}
if (test_conv_pos() != 1)
{
exit(-1);
}
if (test_conv_pos27() != 1)
{
exit(-1);
}
if (test_pos2ddd() != 1)
{
exit(-1);
}
if (test_pos2dmm() != 1)
{
exit(-1);
}
if (test_assemble_vdm() != 1)
{
exit(-1);
}
if( test_ais_1() != 1 )
{
exit(-1);
}
if( test_ais_3() != 1 )
{
exit(-1);
}
if( test_ais_4() != 1 )
{
exit(-1);
}
if( test_ais_5() != 1 )
{
exit(-1);
}
if( test_ais_7() != 1 )
{
exit(-1);
}
if( test_ais_9() != 1 )
{
exit(-1);
}
if( test_ais_12() != 1 )
{
exit(-1);
}
if( test_ais_13() != 1 )
{
exit(-1);
}
if( test_ais_15() != 1 )
{
exit(-1);
}
if( test_ais_18() != 1 )
{
exit(-1);
}
if( test_ais_20() != 1 )
{
exit(-1);
}
if( test_ais_24A() != 1 )
{
exit(-1);
}
if( test_ais_24B() != 1 )
{
exit(-1);
}
if( test_ais_27() != 1 )
{
exit(-1);
}
printf("Testing test_msgs\n");
/* Clear out the structures */
memset( &ais, 0, sizeof( ais_state ) );
for (i=0; i<NUM_TEST_MSGS; i++)
{
init_6bit( &ais.six_state );
strcpy( ais.six_state.bits, test_msgs[i] );
ais.msgid = (unsigned char) get_6bit( &ais.six_state, 6 );
/* Process the AIS message */
printf( "Message %d\n", ais.msgid );
/* process message with appropriate parser */
switch( ais.msgid ) {
case 1:
if( parse_ais_1( &ais, &msg_1 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_1.userid );
printf( "Lat : %ld\n", msg_1.latitude );
printf( "Long : %ld\n", msg_1.longitude );
}
break;
case 2:
if( parse_ais_2( &ais, &msg_2 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_2.userid );
printf( "Lat : %ld\n", msg_1.latitude );
printf( "Long : %ld\n", msg_1.longitude );
}
break;
case 3:
if( parse_ais_3( &ais, &msg_3 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_3.userid );
printf( "Lat : %ld\n", msg_1.latitude );
printf( "Long : %ld\n", msg_1.longitude );
}
break;
case 4:
if( parse_ais_4( &ais, &msg_4 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_4.userid );
printf( "Lat : %ld\n", msg_1.latitude );
printf( "Long : %ld\n", msg_1.longitude );
}
break;
case 5:
if( parse_ais_5( &ais, &msg_5 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_5.userid );
printf( "Callsign : %s\n", msg_5.callsign );
printf( "Name : %s\n", msg_5.name );
printf( "Destination : %s\n", msg_5.dest );
}
break;
case 12:
if (parse_ais_12(&ais, &msg_12) == 0)
{
/* Do something with the resu;ts */
printf( "MMSI : %09ld\n", msg_12.userid );
printf( "Message : %s\n", msg_12.message );
}
break;
case 18:
if( parse_ais_18( &ais, &msg_18 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_18.userid );
printf( "Lat : %ld\n", msg_18.latitude );
printf( "Long : %ld\n", msg_18.longitude );
}
break;
case 24:
if( parse_ais_24( &ais, &msg_24 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_24.userid );
if (msg_24.flags & 1)
{
printf("Name : %s\n", msg_24.name );
}
if (msg_24.flags & 2)
{
printf("Callsign : %s\n", msg_24.callsign );
}
}
break;
case 27:
if( parse_ais_27( &ais, &msg_27 ) == 0 )
{
/* Do something with results */
printf( "MMSI : %09ld\n", msg_27.userid );
printf( "Lat : %ld\n", msg_27.latitude );
printf( "Long : %ld\n", msg_27.longitude );
}
break;
}
}
/* St. Lawrence msg8 testing */
test_seaway();
printf("Testing demo_msgs\n");
/* Clear out the structures */
memset( &ais, 0, sizeof( ais_state ) );
for (i=0; i<NUM_DEMO_MSGS; i++)
{
int err;
printf( "%d of %d: %s\n", i+1, NUM_DEMO_MSGS, demo_msgs[i]);
if( (err = assemble_vdm( &ais, demo_msgs[i] )) != 0 ) {
printf("ERROR %d\n", err);
continue;
}
ais.msgid = (unsigned char) get_6bit( &ais.six_state, 6 );
/* Process the AIS message */
printf( "msgid : %d\n", ais.msgid );
/* process message with appropriate parser */
switch( ais.msgid ) {
case 1:
if( parse_ais_1( &ais, &msg_1 ) == 0 )
{
printf("repeat : %d\n", msg_1.repeat );
printf("userid : %ld\n", msg_1.userid );
printf("nav_status : %d\n", msg_1.nav_status );
printf("rot : %d\n", msg_1.rot );
printf("sog : %d\n", msg_1.sog );
printf("pos_acc : %d\n", msg_1.pos_acc );
printf("longitude : %ld\n", msg_1.longitude );
printf("latitude : %ld\n", msg_1.latitude );
printf("cog : %d\n", msg_1.cog );
printf("true : %d\n", msg_1.true );
printf("utc_sec : %d\n", msg_1.utc_sec );
printf("regional : %d\n", msg_1.regional );
printf("spare : %d\n", msg_1.spare );
printf("raim : %d\n", msg_1.raim );
printf("sync_state : %d\n", msg_1.sync_state );
printf("slot_timeout: %d\n", msg_1.slot_timeout );
printf("sub_message : %d\n", msg_1.sub_message );
}
break;
case 3:
if( parse_ais_3( &ais, &msg_3 ) == 0 )
{
printf("repeat : %d\n", msg_3.repeat );
printf("userid : %ld\n", msg_3.userid );
printf("nav_status : %d\n", msg_3.nav_status );
printf("rot : %d\n", msg_3.rot );
printf("sog : %d\n", msg_3.sog );
printf("pos_acc : %d\n", msg_3.pos_acc );
printf("longitude : %ld\n", msg_3.longitude );
printf("latitude : %ld\n", msg_3.latitude );
printf("cog : %d\n", msg_3.cog );
printf("true : %d\n", msg_3.true );
printf("utc_sec : %d\n", msg_3.utc_sec );
printf("regional : %d\n", msg_3.regional );
printf("spare : %d\n", msg_3.spare );
printf("raim : %d\n", msg_3.raim );
printf("sync_state : %d\n", msg_3.sync_state );
printf("slot_inc : %d\n", msg_3.slot_increment );
printf("num_slots : %d\n", msg_3.num_slots );
printf("keep : %d\n", msg_3.keep);
}
break;
}
printf("\n\n");
}
/* Test some possible seaway messages */
test_seaway_msgs();
/* Test the seaway and imo messages */
test_access();
return 0;
}