forked from g8bpq/linbpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AISCommon.c
3304 lines (2351 loc) · 84.8 KB
/
AISCommon.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "time.h"
#include "CHeaders.h"
//#include "tncinfo.h"
//#include "adif.h"
//#include "telnetserver.h"
VOID __cdecl Debugprintf(const char * format, ...);
#define TRACKPOINTS 50
typedef struct TARGETRECORD
{
UINT ID;
char name[21];
char Callsign[8];
char Dest[21];
UINT IMO;
double ROT;
int LengthA;
int LengthB;
int WidthC;
int WidthD;
int Draft;
double lat;
double Long;
double course;
double speed;
double Heading;
int NavStatus;
UINT TCA;
UINT BCA; // Bearing at closest
UINT TimeAdded;
UINT TimeLastUpdated;
char Type;
char Class;
// Fields for Intellegent TrackLog
double LatTrack[TRACKPOINTS]; // Cyclic Tracklog
double LonTrack[TRACKPOINTS];
time_t TrackTime[TRACKPOINTS];
int Trackptr; // Next record in Tracklog
// info about last track point written
double Lastlat;
double LastLong;
double LastCourse;
double LastSpeed;
time_t LastTime;
int Alt; // For SAR Aircraft
} TargetRecord;
typedef struct NAVAIDRECORD
{
UINT ID;
char name[21];
int NavAidType;
double lat;
double Long;
int LengthA;
int LengthB;
int WidthC;
int WidthD;
int FixType;
time_t TimeAdded;
time_t TimeLastUpdated;
} NavaidRecord;
typedef struct BASESTATIONRECORD
{
UINT ID;
int FixType;
double lat;
double Long;
int NumberofStations;
time_t TimeAdded;
time_t TimeLastUpdated;
} BaseStationRecord;
typedef struct SHIPDATABASERECORD
{
UINT ID;
char name[21];
char Callsign[8];
int LengthA;
int LengthB;
int WidthC;
int WidthD;
int Draft;
time_t TimeAdded;
time_t TimeLastUpdated;
char Type;
char Class;
} ShipDatabaseRecord;
void LookupVessel(UINT UserID);
void LoadVesselDataBase();
void SaveVesselDataBase();
void LoadNavAidDataBase();
void SaveNavAidDataBase();
typedef struct SARRECORD
{
UINT ID;
char name[21];
char Callsign[8];
char Dest[21];
UINT IMO;
double ROT;
int LengthA;
int LengthB;
int WidthC;
int WidthD;
int Draft;
double lat;
double Long;
double course;
double speed;
double Heading;
int NavStatus;
UINT TCA;
UINT BCA; // Bearing at closest
time_t TimeAdded;
time_t TimeLastUpdated;
char Type;
char Class;
// Fields for Intellegent TrackLog
double LatTrack[TRACKPOINTS]; // Cyclic Tracklog
double LonTrack[TRACKPOINTS];
time_t TrackTime[TRACKPOINTS];
int Trackptr; // Next record in Tracklog
// info about last track point written
double Lastlat;
double LastLong;
double LastCourse;
double LastSpeed;
time_t LastTime;
int Alt; // For SAR Aircraft
} SARRecord;
typedef struct ADSBRECORD
{
char hex[8]; //the 24-bit ICAO identifier of the aircraft, as 6 hex digits. The identifier may
//start with '~', this means that the address is a non-ICAO address (e.g. from TIS-B).
char squawk[5]; // the 4-digit squawk (octal representation)
char flight[32]; // the flight name / callsign
double lat;
double lon; // the aircraft position in decimal degrees
// nucp: the NUCp (navigational uncertainty category) reported for the position
int seen_pos; // how long ago (in seconds before "now") the position was last updated
int altitude; // the aircraft altitude in feet, or "ground" if it is reporting it is on the ground
int vert_rate; // vertical rate in feet/minute
int track; // true track over ground in degrees (0-359)
int speed; // reported speed in kt. This is usually speed over ground, but might be IAS - you can't tell the difference here, sorry!
int messages; // total number of Mode S messages received from this aircraft
int seen; // how long ago (in seconds before "now") a message was last received from this aircraft
int rssi; // recent average RSSI (signal power), in dbFS; this will always be negative.
time_t TimeAdded;
time_t TimeLastUpdated;
// Fields for Intellegent TrackLog
double LatTrack[TRACKPOINTS]; // Cyclic Tracklog
double LonTrack[TRACKPOINTS];
time_t TrackTime[TRACKPOINTS];
int Trackptr; // Next record in Tracklog
// info about last track point written
double Lastlat;
double LastLong;
int LastCourse;
int LastSpeed;
time_t LastTime;
} ADSBRecord;
extern BOOL TraceAIS;
extern BOOL NoVDO;
extern BOOL NoAlarms;
UINT PaintColour;
//char Msg[1000];
//UINT Colour;
char CloseString[100];
//extern int ListItem;
SOCKET udpsock;
SOCKADDR_IN sinx;
SOCKADDR_IN rxaddr, txaddr, pitxaddr;
double pi = 3.14159265389754;
double OurSog, OurCog, OurLatitude, OurLongtitude;
double OurLatIncr, OurLongIncr;
unsigned int UserID;
int NavStatus;
double V_Lat, V_Lon, V_COG, V_SOG, V_ROT;
char V_Name[21], V_Callsign[8], V_Dest[21], V_VendorID[8];
int V_IMO, V_Heading, V_Type, B_PartNo;
int Msg_Type, Accuracy, FixType;
int UTCyear, UTCMonth, UTCDay, UTCHour, UTCMinute, UTCSecond;
int NavAidType, V_Draught, ETA;
int Alt;
int V_LenA, V_LenB, V_WidthC, V_WidthD;
int SyncState, SlotTO, SubMsg;
int GpsOK;
int DecayTimer;
static int MaxAge = 7200; // 2 Hours
int VessselDBChanged = 0;
int NavAidDBChanged = 0;
BOOL SoundActive;
extern BOOL KeepTracks;
HANDLE TrackHandle;
HANDLE hTrack;
UINT LastTrackFileTime;
UINT NOW;
char Stack[500];
BOOL Stacked=FALSE;
int NavAidCount=0;
struct NAVAIDRECORD ** NavRecords;
int SARCount=0;
struct SARRECORD ** SARRecords;
int TargetCount=0;
struct TARGETRECORD ** TargetRecords;
int BaseStationCount=0;
struct BASESTATIONRECORD ** BaseStationRecords;
int ADSBCount = 0;
struct ADSBRECORD ** ADSBRecords;
UCHAR conv[256]={99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, // 00
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 30
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, // 40 @
32, 33, 34, 35, 36, 37, 38, 39, 40, 33, 34, 35, 36, 37, 38, 39, // 50 P
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, // 60
56, 57, 58, 59, 60, 61, 62, 63, 0, 1, 2, 3, 4, 5, 6, 7, // 77 =
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99};
char *NavAidString[] = {
"Default, Type of A to N not specified",
"Reference Point",
"RACON",
"Structure Off shore Structure",
"Spare",
"Light, without sectors",
"Light, with sectors",
"Leading Light Front",
"Leading Light Rear",
"Beacon, Cardinal N",
"Beacon, Cardinal E",
"Beacon, Cardinal S",
"Beacon, Cardinal W",
"Beacon, Port hand",
"Beacon, Starboard hand",
"Beacon, Preferred Channel port hand",
"Beacon, Preferred Channel starboard hand",
"Beacon, Isolated danger",
"Beacon, Safe water",
"Beacon, Special mark",
"Cardinal Mark N",
"Cardinal Mark E",
"Cardinal Mark S",
"Cardinal Mark W",
"Port hand Mark",
"Starboard hand Mark",
"Preferred Channel Port hand",
"Preferred Channel Starboard hand",
"Isolated danger",
"Safe Water",
"Special Mark",
"Light Vessel / LANBY / Rigs"};
char * FixTypeString[] = {
"Undefined",
"GPS",
"GLONASS",
"Combined GPS/GLONASS",
"Loran - C",
"Chayka",
"Integrated Navigation System",
"surveyed"};
char * NavStatusString[] = {
"Under way using engine",
"At anchor",
"Not under command",
"Restricted manoeuvrability",
"Constrained by draught",
"Moored",
"Aground",
"Engaged in Fishing",
"Under way sailing",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"Not defined"};
char * VesselType[] = { // First Digit
"Undefined",
"Reserved for future use",
"WIG", // 2
"", // 3
"HSC",
"",
"Passenger ship",
"Cargo ship",
"Tanker",
"Other types of ship"};
char * VesselType3[] = {
"Fishing",
"Towing",
"Towing Len > 200 m or breadth > 25 m",
"Dredging or underwater operations",
"Diving",
"Military",
"Sailing",
"Pleasure Craft",
"Reserved",
"Reserved"};
char * VesselType5[] = {
"Pilot vessel",
"Search and rescue vessel",
"Tug",
"Port tender",
"Vessel with anti-pollution facilities or equipment",
"Law enforcement vessel",
"Spare",
"Spare",
"Medical transport",
"Ships according to Resolution No 18 (Mob-83)"};
extern struct SHIPDATABASERECORD * ShipDataRec;
int ACount = 0, BCount = 0;
char ADSBHost[64] = ""; //"10.8.0.50";
int ADSBPort = 30003;
int ADSBConnected = 0;
extern double Lat;
extern double Lon;
// Forward declarations of functions included in this code module:
double radians(double Degrees);
double degrees(double radians);
BOOL Check0183CheckSum(char * msg,int len);
int ProcessNMEAMessage(char * msg, int len);
int DecodeVDM(char * Data);
int ConvertAsciito6Bit(char * msg, char * buffer, int Len);
void ProcessAISVesselMessage();
void ProcessSARMessage();
void ProcessBaseStationMessage();
void ProcessAISVesselDetails(char Type);
void ProcessAISNavAidMessage();
void MM_Reinit();
void CalculateClosestApproach(struct TARGETRECORD * ptr);
static double Distance(double lah, double loh, double laa, double loa);
static double Bearing(double lat1, double lon1, double lat2, double lon2);
double CalcClosest(struct TARGETRECORD * ptr);
void RefreshTargetList();
void RefreshTargetSub(struct TARGETRECORD * ptr, int j);
void CheckAgeofTargets(UINT MaxAge);
void ActivateTarget();
void Write_Target_Track(struct TARGETRECORD * ptr);
void Write_Our_Track();
void MaintainVesselDatabase(UINT UserID, char Type);
VOID AutoPilotUpdate();
VOID ProcessNMEAMessageCE(char * msg, int len);
void ProcessADSBJSON(char * FN);
void CheckAgeofPlanes(UINT MaxAge);
static VOID ADSBConnect(void * unused);
char ADSBFN[256] = "/dev/shm/adsb/aircraft.json";
char DataBasefn[MAX_PATH];
int ShipDatabaseCount=0;
struct SHIPDATABASERECORD ** ShipDatabaseRecords;
struct SHIPDATABASERECORD * ShipDataRec;
void SaveTrackPoint(struct TARGETRECORD * ptr)
{
// If course and speed have changed, and distance travelled or time is significant, add a track point to log
if ((NOW - ptr->LastTime) < 15) // Not More that once per 15 secs
return;
if (fabs(ptr->LastCourse - ptr->course) < 1.0 && fabs(ptr->LastSpeed - ptr->speed) < 0.2 && ptr->speed > 0.2) // if very slow, COG may be random
{
// Steady Course - update once per 5 minutes if moving
if ((NOW - ptr->LastTime) > 600 && ptr->speed != 0)
goto writeRec;
}
if (fabs(ptr->Lastlat - ptr->lat) < 0.01 && fabs(ptr->LastLong - ptr->Long) < 0.01) // Not moved much
return;
writeRec:
ptr->LastCourse = ptr->course;
ptr->LastSpeed = ptr->speed;
ptr->LastTime = NOW;
ptr->Lastlat = ptr->lat;
ptr->LastLong = ptr->Long;
ptr->LatTrack[ptr->Trackptr] = ptr->lat;
ptr->LonTrack[ptr->Trackptr] = ptr->Long;
ptr->TrackTime[ptr->Trackptr] = NOW;
ptr->Trackptr++;
if (ptr->Trackptr == TRACKPOINTS)
ptr->Trackptr = 0;
}
void SaveSARTrackPoint(struct SARRECORD * ptr)
{
// If course and speed have changed, and distance travelled or time is significant, add a track point to log
if ((NOW - ptr->LastTime) < 15) // Not More that once per 15 secs
return;
if (fabs(ptr->LastCourse - ptr->course) < 1.0 && fabs(ptr->LastSpeed - ptr->speed) < 0.2 && ptr->speed > 0.2) // if very slow, COG may be random
{
// Steady Course - update once per 5 minutes if moving
if ((NOW - ptr->LastTime) > 600 && ptr->speed != 0)
goto writeRec;
}
if (fabs(ptr->Lastlat - ptr->lat) < 0.01 && fabs(ptr->LastLong - ptr->Long) < 0.01) // Not moved much
return;
writeRec:
ptr->LastCourse = ptr->course;
ptr->LastSpeed = ptr->speed;
ptr->LastTime = NOW;
ptr->Lastlat = ptr->lat;
ptr->LastLong = ptr->Long;
ptr->LatTrack[ptr->Trackptr] = ptr->lat;
ptr->LonTrack[ptr->Trackptr] = ptr->Long;
ptr->TrackTime[ptr->Trackptr] = NOW;
ptr->Trackptr++;
if (ptr->Trackptr == TRACKPOINTS)
ptr->Trackptr = 0;
}
void MaintainVesselDatabase(UINT UserID, char Type)
{
LookupVessel(UserID); // Find Vessel - if not present, add it
if (ShipDataRec == NULL) return; // Malloc failed!!
if (Type == 'A') ShipDataRec->Class='A'; else ShipDataRec->Class='B';
if (Type == 'A')
{
memcpy(ShipDataRec->name, V_Name, 21);
memcpy(ShipDataRec->Callsign,V_Callsign,8);
ShipDataRec->Type = V_Type;
ShipDataRec->LengthA = V_LenA;
ShipDataRec->LengthB = V_LenB;
ShipDataRec->WidthC = V_WidthC;
ShipDataRec->WidthD = V_WidthD;
}
if (Type == 'B') // Type 19 Class B Details
{
memcpy(ShipDataRec->name, V_Name, 21);
ShipDataRec->Type = V_Type;
ShipDataRec->LengthA = V_LenA;
ShipDataRec->LengthB = V_LenB;
ShipDataRec->WidthC = V_WidthC;
ShipDataRec->WidthD = V_WidthD;
}
if (Type == '0') // Class B record 24A
{
memcpy(ShipDataRec->name, V_Name, 21);
}
if (Type == '1') // Class B record 24B
{
memcpy(ShipDataRec->Callsign,V_Callsign,8);
ShipDataRec->Type = V_Type;
ShipDataRec->LengthA = V_LenA;
ShipDataRec->LengthB = V_LenB;
ShipDataRec->WidthC = V_WidthC;
ShipDataRec->WidthD = V_WidthD;
}
ShipDataRec->TimeLastUpdated = NOW;
VessselDBChanged = 1; // Save on next timer tick
return;
}
void LookupVessel(UINT UserID)
{
int i;
for (i = 0; i < ShipDatabaseCount; i++)
{
ShipDataRec=ShipDatabaseRecords[i];
if (ShipDataRec->ID == UserID) return;
}
// Not found
if (ShipDatabaseCount == 0)
ShipDatabaseRecords=(struct SHIPDATABASERECORD **)malloc(sizeof(void *));
else
ShipDatabaseRecords=(struct SHIPDATABASERECORD **)realloc(ShipDatabaseRecords,(ShipDatabaseCount+1)*sizeof(void *));
ShipDataRec=(struct SHIPDATABASERECORD *)malloc(sizeof(struct SHIPDATABASERECORD));
memset(ShipDataRec, 0, sizeof(struct SHIPDATABASERECORD));
if (ShipDataRec == NULL) return;
ShipDatabaseRecords[ShipDatabaseCount]=ShipDataRec;
ShipDataRec->ID = UserID;
ShipDataRec->TimeAdded = NOW;
ShipDatabaseCount++;
sprintf(ShipDataRec->name,"%d",UserID);
}
void LoadVesselDataBase()
{
int i;
FILE *file;
char buf[256];
char *token;
char FN[256];
if (BPQDirectory[0] == 0)
{
strcpy(FN, "AIS_Vessels.txt");
}
else
{
strcpy(FN, BPQDirectory);
strcat(FN, "/");
strcat(FN, "AIS_Vessels.txt");
}
if ((file = fopen(FN,"r")) == NULL) return;
fgets(buf, 255, file);
sscanf(buf,"%d", &ShipDatabaseCount);
if (ShipDatabaseCount == 0)
{
fclose(file);
return;
}
ShipDatabaseRecords = (struct SHIPDATABASERECORD **)malloc(ShipDatabaseCount * sizeof(void *));
for (i = 0; i < ShipDatabaseCount; i++)
{
ShipDataRec = (struct SHIPDATABASERECORD *)malloc(sizeof(struct SHIPDATABASERECORD));
ShipDatabaseRecords[i] = ShipDataRec;
memset(ShipDataRec, 0, sizeof(struct SHIPDATABASERECORD));
fgets(buf, 255, file);
token = strtok(buf, "|\n" );
ShipDataRec->ID = atoi(token);
token = strtok( NULL, "|\n" );
strcpy(&ShipDataRec->name[0],token);
token = strtok( NULL, "|\n" );
strcpy(&ShipDataRec->Callsign[0],token);
token = strtok( NULL, "|\n" );
ShipDataRec->LengthA = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->LengthB = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->WidthC = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->WidthD = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->Draft = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->TimeAdded = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->TimeLastUpdated = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->Type = atoi(token);
token = strtok( NULL, "|\n" );
ShipDataRec->Class = atoi(token);
}
fclose(file);
}
void SaveVesselDataBase()
{
int i, n = 0;
FILE *file;
char buf[256];
char FN[256];
if (BPQDirectory[0] == 0)
{
strcpy(FN, "AIS_Vessels.txt");
}
else
{
strcpy(FN, BPQDirectory);
strcat(FN, "/");
strcat(FN, "AIS_Vessels.txt");
}
if ((file = fopen(FN,"w")) == NULL) return;
for (i = 0; i < ShipDatabaseCount; i++)
{
if (ShipDatabaseRecords[i]->Callsign[0] > 32) // Count those with details
n++;
}
sprintf(buf,"%d\n", n);
fputs(buf, file);
for (i = 0; i < ShipDatabaseCount; i++)
{
ShipDataRec = ShipDatabaseRecords[i];
if (ShipDataRec->Callsign[0] > 32) // No point in saving if no vessel details
{
sprintf(buf,"%d|%s|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d\n",
ShipDataRec->ID,
ShipDataRec->name,
ShipDataRec->Callsign,
ShipDataRec->LengthA,
ShipDataRec->LengthB,
ShipDataRec->WidthC,
ShipDataRec->WidthD,
ShipDataRec->Draft,
ShipDataRec->TimeAdded,
ShipDataRec->TimeLastUpdated,
ShipDataRec->Type,
ShipDataRec->Class);
fputs(buf, file);
}
}
fclose(file);
}
void LoadNavAidDataBase()
{
int i;
FILE *file;
char buf[256];
char *token;
char FN[256];
struct NAVAIDRECORD * navptr;
if (BPQDirectory[0] == 0)
{
strcpy(FN, "AIS_NavAids.txt");
}
else
{
strcpy(FN, BPQDirectory);
strcat(FN, "/");
strcat(FN, "AIS_NavAids.txt");
}
if ((file = fopen(FN,"r")) == NULL) return;
fgets(buf, 255, file);
sscanf(buf,"%d", &NavAidCount);
if (NavAidCount == 0)
{
fclose(file);
return;
}
NavRecords = (struct NAVAIDRECORD **)malloc(NavAidCount * sizeof(void *));
for (i = 0; i < NavAidCount; i++)
{
navptr = (struct NAVAIDRECORD *)malloc(sizeof(struct NAVAIDRECORD));
NavRecords[i] = navptr;
memset(navptr, 0, sizeof(struct NAVAIDRECORD));
fgets(buf, 255, file);
token = strtok(buf, "|\n" );
navptr->ID = atoi(token);
token = strtok(NULL, "|\n" );
strcpy(&navptr->name[0],token);
token = strtok(NULL, "|\n" );
navptr->lat = atof(token);
token = strtok(NULL, "|\n" );
navptr->Long = atof(token);
token = strtok(NULL, "|\n" );
navptr->TimeAdded = atoi(token);
token = strtok(NULL, "|\n" );
navptr->TimeLastUpdated = atoi(token);
}
fclose(file);
}
void SaveNavAidDataBase()
{
int i;
FILE *file;
char FN[256];
struct NAVAIDRECORD * navptr;
if (BPQDirectory[0] == 0)
{
strcpy(FN, "AIS_NavAids.txt");
}
else
{
strcpy(FN, BPQDirectory);
strcat(FN, "/");
strcat(FN, "AIS_NavAids.txt");
}
if ((file = fopen(FN,"w")) == NULL) return;
fprintf(file, "%d\n", NavAidCount);
for (i = 0; i < NavAidCount; i++)
{
navptr=NavRecords[i];
fprintf(file, "%d|%s|%f|%f|%d|%d\n", navptr->ID, navptr->name, navptr->lat, navptr->Long, navptr->TimeAdded, navptr->TimeLastUpdated);
}
fclose(file);
}
void initAIS()
{
LoadVesselDataBase();
LoadNavAidDataBase();
}
void SaveAIS()
{
SaveVesselDataBase();
SaveNavAidDataBase();
}
void initADSB()
{
_beginthread(ADSBConnect, 0, 0);
}
int ProcessAISMessage(char * msg, int len)
{
char * ptr1;
char * ptr2;
char X1[10], X2[10], X3[10], X4[10];
char Data[256];
float alt=0.0;
NOW = time(NULL);
ptr1 = &msg[7];
//' Looks like first is no of fragments in message, 2nd fragment seq no, 3rd msg seq, 4th port
//'!AIVDM,2,1,7,2,502He5h000030000000AD8hTr10u9B1IA<0000000000040000<000000000,0*54
//'!AIVDM,2,2,7,2,00000000008,2*58
if (memcmp(&msg[1],"AIVDM",5) == 0)
{
if (memchr(msg,'*',len) == 0) return 0; // No Checksum
len-=7;
ptr2=(char *)memchr(ptr1,',',8);
if (ptr2 == 0) return 0; // Duff
*(ptr2++)=0;
strcpy(X1,ptr1);
ptr1=ptr2;
ptr2=(char *)memchr(ptr1,',',8);
if (ptr2 == 0) return 0; // Duff
*(ptr2++)=0;
strcpy(X2,ptr1);
ptr1=ptr2;
ptr2=(char *)memchr(ptr1,',',8);
if (ptr2 == 0) return 0; // Duff
*(ptr2++)=0;
strcpy(X3,ptr1);
ptr1=ptr2;
if (*ptr1 == 'A')
ACount++;
else
BCount++;
ptr2=(char *)memchr(ptr1,',',8);
if (ptr2 == 0) return 0; // Duff
*(ptr2++)=0;
strcpy(X4,ptr1);
ptr1=ptr2;
ptr2=(char *)memchr(ptr1,',',78);
if (ptr2 == 0) return 0; // Duff
*(ptr2++)=0;
strcpy(Data,ptr1);
if (X1[0] == '1')
{
// Single fragment
DecodeVDM (Data);
return 0;
}
// Multipart.
if (X2[0] == '1')
{
// First of Multipart
strcpy(Stack,Data);
return 0; // ' this needs generalising
}
// Subsequent part of Multipart
strcat(Stack, Data);
if (X1[0] == X2[0])
{
// Last Part
DecodeVDM (Stack);
return 0;
}
}
return 0;
}
int mm1=0, nasa1=0, mm4=0, nasa4=0;
int DecodeVDM(char *msg)
{
int Conv, i, bits;
UCHAR val[500];
// Start value at offet 1 for compatiblilty woth Basic Code
ConvertAsciito6Bit(msg, (char *)&val[1], strlen(msg)); // Convert ascii chars to 6-bit values
Msg_Type = val[1];
UserID = ((val[2] & 15) << 26) + (val[3] << 20)+ (val[4] << 14)+ (val[5] << 8)+ (val[6] << 2) + (val[7] >>4); ;
// if (UserID == 0 ) UserID = 99998888;
bits=strlen(msg)*6;
if (Msg_Type == 1 || Msg_Type == 2 || Msg_Type == 3)
{