forked from g8bpq/linbpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APRSCode.c
9223 lines (6823 loc) · 201 KB
/
APRSCode.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
/*
Copyright 2001-2022 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 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 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
// Module to implement APRS "New Paradigm" Digipeater and APRS-IS Gateway
// First Version, November 2011
#pragma data_seg("_BPQDATA")
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include "CHeaders.h"
#include "bpq32.h"
#include <time.h>
#include "kernelresource.h"
#include "tncinfo.h"
#include "bpqaprs.h"
#ifndef WIN32
#include <unistd.h>
#include <sys/mman.h>
#include <sys/un.h>
int sfd;
struct sockaddr_un my_addr, peer_addr;
socklen_t peer_addr_size;
#endif
#define MAXAGE 3600 * 12 // 12 Hours
#define MAXCALLS 20 // Max Flood, Trace and Digi
#define GATETIMELIMIT 40 * 60 // Don't gate to RF if station not heard for this time (40 mins)
static BOOL APIENTRY GETSENDNETFRAMEADDR();
static VOID DoSecTimer();
static VOID DoMinTimer();
static int APRSProcessLine(char * buf);
static BOOL APRSReadConfigFile();
VOID APRSISThread(void * Report);
VOID __cdecl Debugprintf(const char * format, ...);
VOID __cdecl Consoleprintf(const char * format, ...);
BOOL APIENTRY Send_AX(PMESSAGE Block, DWORD Len, UCHAR Port);
VOID Send_AX_Datagram(PDIGIMESSAGE Block, DWORD Len, UCHAR Port);
char * strlop(char * buf, char delim);
int APRSDecodeFrame(char * msg, char * buffer, time_t Stamp, uint64_t Mask); // Unsemaphored DecodeFrame
APRSHEARDRECORD * UpdateHeard(UCHAR * Call, int Port);
BOOL CheckforDups(char * Call, char * Msg, int Len);
VOID ProcessQuery(char * Query);
VOID ProcessSpecificQuery(char * Query, int Port, char * Origin, char * DestPlusDigis);
VOID CheckandDigi(DIGIMESSAGE * Msg, int Port, int FirstUnused, int Digis, int Len);
VOID SendBeacon(int toPort, char * Msg, BOOL SendISStatus, BOOL SendSOGCOG);
Dll BOOL APIENTRY PutAPRSMessage(char * Frame, int Len);
VOID ProcessAPRSISMsg(char * APRSMsg);
static VOID SendtoDigiPorts(PDIGIMESSAGE Block, DWORD Len, UCHAR Port);
APRSHEARDRECORD * FindStationInMH(char * call);
BOOL OpenGPSPort();
void PollGPSIn();
int CountLocalStations();
BOOL SendAPPLAPRSMessage(char * Frame);
VOID SendAPRSMessage(char * Message, int toPort);
static VOID TCPConnect(void * unuxed);
struct STATIONRECORD * DecodeAPRSISMsg(char * msg);
struct STATIONRECORD * ProcessRFFrame(char * buffer, int len, int * ourMessage);
VOID APRSSecTimer();
double myDistance(double laa, double loa, BOOL KM);
struct STATIONRECORD * FindStation(char * Call, BOOL AddIfNotFound);
int DecodeAPRSPayload(char * Payload, struct STATIONRECORD * Station);
BOOL KillOldTNC(char * Path);
int FromLOC(char * Locator, double * pLat, double * pLon);
BOOL ToLOC(double Lat, double Lon , char * Locator);
BOOL InternalSendAPRSMessage(char * Text, char * Call);
void UndoTransparency(char * input);
char * __cdecl Cmdprintf(TRANSPORTENTRY * Session, char * Bufferptr, const char * format, ...);
char * GetStandardPage(char * FN, int * Len);
VOID WriteMiniDump();
BOOL ProcessConfig();
int ProcessAISMessage(char * msg, int len);
int read_png(unsigned char *bytes);
VOID sendandcheck(SOCKET sock, const char * Buffer, int Len);
void SaveAPRSMessage(struct APRSMESSAGE * ptr);
void ClearSavedMessages();
void GetSavedAPRSMessages();
static VOID GPSDConnect(void * unused);
int CanPortDigi(int Port);
extern int SemHeldByAPI;
extern int APRSMONDECODE();
extern struct ConsoleInfo MonWindow;
extern char VersionString[];
BOOL SaveAPRSMsgs = 0;
BOOL LogAPRSIS = FALSE;
// All data should be initialised to force into shared segment
static char ConfigClassName[]="CONFIG";
extern BPQVECSTRUC * APRSMONVECPTR;
extern int MONDECODE();
extern VOID * zalloc(int len);
extern BOOL StartMinimized;
extern char TextVerstring[];
extern HWND hConsWnd;
extern HKEY REGTREE;
extern char LOCATOR[80];
extern char LOC[7];
static int SecTimer = 10;
static int MinTimer = 60;
BOOL APRSApplConnected = FALSE;
BOOL APRSWeb = FALSE;
void * APPL_Q = 0; // Queue of frames for APRS Appl
void * APPLTX_Q = 0; // Queue of frames from APRS Appl
uint64_t APRSPortMask = 0;
char APRSCall[10] = "";
char APRSDest[10] = "APBPQ1";
char WXCall[10];
UCHAR AXCall[7] = "";
char CallPadded[10] = " ";
char GPSPort[80] = "";
int GPSSpeed = 0;
char GPSRelay[80] = "";
BOOL GateLocal = FALSE;
double GateLocalDistance = 0.0;
int MaxDigisforIS = 7; // Dont send to IS if more digis uued to reach us
char WXFileName[MAX_PATH];
char WXComment[80];
BOOL SendWX = FALSE;
int WXInterval = 30;
int WXCounter = 29 * 60;
char APRSCall[10];
char LoppedAPRSCall[10];
BOOL WXPort[MaxBPQPortNo + 1]; // Ports to send WX to
BOOL GPSOK = 0;
char LAT[] = "0000.00N"; // in standard APRS Format
char LON[] = "00000.00W"; //in standard APRS Format
char HostName[80]; // for BlueNMEA
int HostPort = 4352;
char GPSDHost[80];
int GPSDPort = 2947;
extern int ADSBPort;
extern char ADSBHost[];
BOOL BlueNMEAOK = FALSE;
int BlueNMEATimer = 0;
BOOL GPSDOK = FALSE;
int GPSDTimer = 0;
BOOL GPSSetsLocator = 0; // Update Map Location from GPS
double SOG, COG; // From GPS
double Lat = 0.0;
double Lon = 0.0;
BOOL PosnSet = FALSE;
/*
The null position should be include the \. symbol (unknown/indeterminate
position). For example, a Position Report for a station with unknown position
will contain the coordinates …0000.00N\00000.00W.…
*/
char * FloodCalls = 0; // Calls to relay using N-n without tracing
char * TraceCalls = 0; // Calls to relay using N-n with tracing
char * DigiCalls = 0; // Calls for normal relaying
UCHAR FloodAX[MAXCALLS][7] = {0};
UCHAR TraceAX[MAXCALLS][7] = {0};
UCHAR DigiAX[MAXCALLS][7] = {0};
int FloodLen[MAXCALLS];
int TraceLen[MAXCALLS];
int DigiLen[MAXCALLS];
int ISPort = 0;
char ISHost[256] = "";
int ISPasscode = 0;
char NodeFilter[1000] = "m/50"; // Filter when the isn't an application
char ISFilter[1000] = "m/50"; // Current Filter
char APPLFilter[1000] = ""; // Filter when an Applcation is running
extern BOOL IGateEnabled;
char StatusMsg[256] = ""; // Must be in shared segment
int StatusMsgLen = 0;
char * BeaconPath[65] = {0};
char CrossPortMap[65][65] = {0};
char APRSBridgeMap[65][65] = {0};
UCHAR BeaconHeader[65][10][7] = {""}; // Dest, Source and up to 8 digis
int BeaconHddrLen[65] = {0}; // Actual Length used
UCHAR GatedHeader[65][10][7] = {""}; // Dest, Source and up to 8 digis for messages gated from IS
int GatedHddrLen[65] = {0}; // Actual Length used
char CFGSYMBOL = 'a';
char CFGSYMSET = 'B';
char SYMBOL = '='; // Unknown Locaton
char SYMSET = '/';
char * PHG = 0; // Optional PHG (Power-Height-Gain) string for beacon
BOOL TraceDigi = FALSE; // Add Trace to packets relayed on Digi Calls
BOOL SATGate = FALSE; // Delay Gating to IS directly heard packets
BOOL RXOnly = FALSE; // Run as RX only IGATE, ie don't gate anything to RF
BOOL DefaultLocalTime = FALSE;
BOOL DefaultDistKM = FALSE;
int multiple = 0; // Allows multiple copies of LinBPQ/APRS on one machine
extern BOOL needAIS;
extern unsigned long long IconData[]; // Symbols as a png image.
typedef struct _ISDELAY
{
struct _ISDELAY * Next;
char * ISMSG;
time_t SendTIme;
} ISDELAY;
ISDELAY * SatISQueue = NULL;
int MaxTraceHops = 2;
int MaxFloodHops = 2;
int BeaconInterval = 0;
int MobileBeaconInterval = 0;
time_t LastMobileBeacon = 0;
int BeaconCounter = 0;
int IStatusCounter = 3600; // Used to send ?ISTATUS? Responses
//int StatusCounter = 0; // Used to send Status Messages
char RunProgram[128] = ""; // Program to start
BOOL APRSISOpen = FALSE;
BOOL BeacontoIS = TRUE;
int ISDelayTimer = 0; // Time before trying to reopen APRS-IS link
char APRSDESTS[][7] = {"AIR*", "ALL*", "AP*", "BEACON", "CQ*", "GPS*", "DF*", "DGPS*", "DRILL*",
"DX*", "ID*", "JAVA*", "MAIL*", "MICE*", "QST*", "QTH*", "RTCM*", "SKY*",
"SPACE*", "SPC*", "SYM*", "TEL*", "TEST*", "TLM*", "WX*", "ZIP"};
UCHAR AXDESTS[30][7] = {""};
int AXDESTLEN[30] = {0};
UCHAR axTCPIP[7];
UCHAR axRFONLY[7];
UCHAR axNOGATE[7];
int MessageCount = 0;
struct PortInfo
{
int Index;
int ComPort;
char PortType[2];
BOOL NewVCOM; // Using User Mode Virtual COM Driver
int ReopenTimer; // Retry if open failed delay
int RTS;
int CTS;
int DCD;
int DTR;
int DSR;
char Params[20]; // Init Params (eg 9600,n,8)
char PortLabel[20];
HANDLE hDevice;
BOOL Created;
BOOL PortEnabled;
int FLOWCTRL;
int gpsinptr;
#ifdef WIN32
OVERLAPPED Overlapped;
OVERLAPPED OverlappedRead;
#endif
char GPSinMsg[160];
int GPSTypeFlag; // GPS Source flags
BOOL RMCOnly; // Only send RMC msgs to this port
};
struct PortInfo InPorts[1] = {0};
// Heard Station info
#define MAXHEARD 1000
int HEARDENTRIES = 0;
int MAXHEARDENTRIES = 0;
int MHLEN = sizeof(APRSHEARDRECORD);
// Area is allocated as needed
APRSHEARDRECORD MHTABLE[MAXHEARD] = {0};
APRSHEARDRECORD * MHDATA = &MHTABLE[0];
static SOCKET sock = (SOCKET) NULL;
//Duplicate suppression Code
#define MAXDUPS 100 // Number to keep
#define DUPSECONDS 28 // Time to Keep
struct DUPINFO
{
time_t DupTime;
int DupLen;
char DupUser[8]; // Call in ax.35 format
char DupText[100];
};
struct DUPINFO DupInfo[MAXDUPS];
struct OBJECT
{
struct OBJECT * Next;
UCHAR Path[10][7]; // Dest, Source and up to 8 digis
int PathLen; // Actual Length used
char Message[81];
char PortMap[MaxBPQPortNo + 1];
int Interval;
int Timer;
};
struct OBJECT * ObjectList; // List of objects to send;
int ObjectCount = 0;
#include <math.h>
#define M_PI 3.14159265358979323846
int RetryCount = 4;
int RetryTimer = 45;
int ExpireTime = 120;
int TrackExpireTime = 1440;
BOOL SuppressNullPosn = FALSE;
BOOL DefaultNoTracks = FALSE;
int MaxStations = 1000;
int SharedMemorySize = 0;
RECT Rect, MsgRect, StnRect;
char Key[80];
// function prototypes
VOID RefreshMessages();
// a few global variables
char APRSDir[MAX_PATH] = "BPQAPRS";
char DF[MAX_PATH];
#define FEND 0xC0 // KISS CONTROL CODES
#define FESC 0xDB
#define TFEND 0xDC
#define TFESC 0xDD
int StationCount = 0;
UCHAR NextSeq = 1;
// Stationrecords are stored in a shared memory segment. based at APRSStationMemory (normally 0x43000000)
// A pointer to the first is placed at the start of this
struct STATIONRECORD ** StationRecords = NULL;
struct STATIONRECORD * StationRecordPool = NULL;
struct APRSMESSAGE * MessageRecordPool = NULL;
struct SharedMem * SMEM;
UCHAR * Shared;
UCHAR * StnRecordBase;
VOID SendObject(struct OBJECT * Object);
VOID MonitorAPRSIS(char * Msg, int MsgLen, BOOL TX);
#ifndef WIN32
#define WSAEWOULDBLOCK 11
#endif
HANDLE hMapFile;
// Logging
static int LogAge = 14;
#ifdef WIN32
int DeleteAPRSLogFiles()
{
WIN32_FIND_DATA ffd;
char szDir[MAX_PATH];
char File[MAX_PATH];
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
LARGE_INTEGER ft;
time_t now = time(NULL);
int Age;
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '\*' to the directory name.
strcpy(szDir, GetLogDirectory());
strcat(szDir, "/logs/APRS*.log");
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
return dwError;
// Walk directory
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
OutputDebugString(ffd.cFileName);
}
else
{
ft.HighPart = ffd.ftCreationTime.dwHighDateTime;
ft.LowPart = ffd.ftCreationTime.dwLowDateTime;
ft.QuadPart -= 116444736000000000;
ft.QuadPart /= 10000000;
Age = (int)((now - ft.LowPart) / 86400);
if (Age > LogAge)
{
sprintf(File, "%s/logs/%s%c", GetLogDirectory(), ffd.cFileName, 0);
Debugprintf("Deleting %s", File);
DeleteFile(File);
}
}
}
while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
return dwError;
}
#else
#include <dirent.h>
int APRSFilter(const struct dirent * dir)
{
return (memcmp(dir->d_name, "APRS", 4) == 0 && strstr(dir->d_name, ".log"));
}
int DeleteAPRSLogFiles()
{
struct dirent **namelist;
int n;
struct stat STAT;
time_t now = time(NULL);
int Age = 0, res;
char FN[256];
n = scandir("logs", &namelist, APRSFilter, alphasort);
if (n < 0)
perror("scandir");
else
{
while(n--)
{
sprintf(FN, "logs/%s", namelist[n]->d_name);
if (stat(FN, &STAT) == 0)
{
Age = (now - STAT.st_mtime) / 86400;
if (Age > LogAge)
{
Debugprintf("Deleting %s\n", FN);
unlink(FN);
}
}
free(namelist[n]);
}
free(namelist);
}
return 0;
}
#endif
int APRSWriteLog(char * msg)
{
FILE *file;
UCHAR Value[MAX_PATH];
time_t T;
struct tm * tm;
if (LogAPRSIS == 0)
return 0;
if (strchr(msg, '\n') == 0)
strcat(msg, "\r\n");
T = time(NULL);
tm = gmtime(&T);
if (GetLogDirectory()[0] == 0)
{
strcpy(Value, "logs/APRS_");
}
else
{
strcpy(Value, GetLogDirectory());
strcat(Value, "/");
strcat(Value, "logs/APRS_");
}
sprintf(Value, "%s%02d%02d%02d.log", Value,
tm->tm_year - 100, tm->tm_mon+1, tm->tm_mday);
if ((file = fopen(Value, "ab")) == NULL)
return FALSE;
fputs(msg, file);
fclose(file);
return 0;
}
int ISSend(SOCKET sock, char * Msg, int Len, int flags)
{
int Loops = 0;
int Sent;
MonitorAPRSIS(Msg, Len, TRUE);
Sent = send(sock, Msg, Len, flags);
while (Sent != Len && Loops++ < 300) // 10 secs max
{
if ((Sent == SOCKET_ERROR) && (WSAGetLastError() != WSAEWOULDBLOCK))
return SOCKET_ERROR;
if (Sent > 0) // something sent
{
Len -= Sent;
memmove(Msg, &Msg[Sent], Len);
}
Sleep(30);
Sent = send(sock, Msg, Len, flags);
}
return Sent;
}
void * endofStations;
Dll BOOL APIENTRY Init_APRS()
{
int i;
char * DCall;
#ifdef WIN32
HKEY hKey=0;
int retCode, Vallen, Type;
#else
int fd;
char RX_SOCK_PATH[] = "BPQAPRSrxsock";
char TX_SOCK_PATH[] = "BPQAPRStxsock";
char SharedName[256];
char * ptr1;
#endif
struct STATIONRECORD * Stn1, * Stn2;
struct APRSMESSAGE * Msg1, * Msg2;
// Clear tables in case a restart
StationRecords = NULL;
StationCount = 0;
HEARDENTRIES = 0;
MAXHEARDENTRIES = 0;
MobileBeaconInterval = 0;
BeaconInterval = 0;
DeleteAPRSLogFiles();
memset(MHTABLE, 0, sizeof(MHTABLE));
ConvToAX25(MYNODECALL, MYCALL);
ConvToAX25("TCPIP", axTCPIP);
ConvToAX25("RFONLY", axRFONLY);
ConvToAX25("NOGATE", axNOGATE);
memset(&FloodAX[0][0], 0, sizeof(FloodAX));
memset(&TraceAX[0][0], 0, sizeof(TraceAX));
memset(&DigiAX[0][0], 0, sizeof(DigiAX));
APRSPortMask = 0;
memset(BeaconPath, sizeof(BeaconPath), 0);
memset(&CrossPortMap[0][0], 0, sizeof(CrossPortMap));
memset(&APRSBridgeMap[0][0], 0, sizeof(APRSBridgeMap));
for (i = 1; i <= MaxBPQPortNo; i++)
{
if (CanPortDigi(i))
CrossPortMap[i][i] = TRUE; // Set Defaults - Same Port
CrossPortMap[i][0] = TRUE; // and APRS-IS
}
PosnSet = 0;
ObjectList = NULL;
ObjectCount = 0;
ISPort = ISHost[0] = ISPasscode = 0;
if (APRSReadConfigFile() == 0)
return FALSE;
if (APRSCall[0] == 0)
{
strcpy(APRSCall, MYNODECALL);
strlop(APRSCall, ' ');
strcpy(LoppedAPRSCall, APRSCall);
memcpy(CallPadded, APRSCall, (int)strlen(APRSCall)); // Call Padded to 9 chars for APRS Messaging
ConvToAX25(APRSCall, AXCall);
}
if (WXCall[0] == 0)
strcpy(WXCall, APRSCall);
// Caluclate size of Shared Segment
SharedMemorySize = sizeof(struct STATIONRECORD) * (MaxStations + 4) +
sizeof(struct APRSMESSAGE) * (MAXMESSAGES + 4) + 32; // 32 for header
#ifndef WIN32
// Create a Shared Memory Object
Shared = NULL;
// Append last bit of current directory to shared name
ptr1 = BPQDirectory;
while (strchr(ptr1, '/'))
{
ptr1 = strchr(ptr1, '/');
ptr1++;
}
if (multiple)
sprintf(SharedName, "/BPQAPRSSharedMem%s", ptr1);
else
strcpy(SharedName, "/BPQAPRSSharedMem");
printf("Using Shared Memory %s\n", SharedName);
#ifndef WIN32
fd = shm_open(SharedName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fd == -1)
{
perror("Create Shared Memory");
printf("Create APRS Shared Memory Failed\n");
}
else
{
if (ftruncate(fd, SharedMemorySize))
{
perror("Extend Shared Memory");
printf("Extend APRS Shared Memory Failed\n");
}
else
{
// Map shared memory object
Shared = mmap((void *)APRSSHAREDMEMORYBASE,
SharedMemorySize,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (Shared == MAP_FAILED)
{
perror("Map Shared Memory");
printf("Map APRS Shared Memory Failed\n");
Shared = NULL;
}
if (Shared != (void *)APRSSHAREDMEMORYBASE)
{
printf("Map APRS Shared Memory Allocated at %x\n", Shared);
Shared = NULL;
}
}
}
#endif
printf("Map APRS Shared Memory Allocated at %p\n", Shared);
if (Shared == NULL)
{
printf("APRS not using shared memory\n");
Shared = malloc(SharedMemorySize);
printf("APRS Non-Shared Memory Allocated at %x\n", Shared);
}
#else
#ifndef LINBPQ
retCode = RegOpenKeyEx (REGTREE,
"SOFTWARE\\G8BPQ\\BPQ32",
0,
KEY_QUERY_VALUE,
&hKey);
if (retCode == ERROR_SUCCESS)
{
Vallen = 4;
retCode = RegQueryValueEx(hKey, "IGateEnabled", 0, &Type, (UCHAR *)&IGateEnabled, &Vallen);
}
#endif
// Create Memory Mapping for Station List
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
SharedMemorySize, // maximum object size (low-order DWORD)
"BPQAPRSStationsMappingObject");// name of mapping object
if (hMapFile == NULL)
{
Consoleprintf("Could not create file mapping object (%d).\n", GetLastError());
return 0;
}
UnmapViewOfFile((void *)APRSSHAREDMEMORYBASE);
Shared = (LPTSTR) MapViewOfFileEx(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
SharedMemorySize,
(void *)APRSSHAREDMEMORYBASE);
if (Shared == NULL)
{
Consoleprintf("Could not map view of file (%d).\n", GetLastError());
CloseHandle(hMapFile);
return 0;
}
#endif
// First record has pointer to table
memset(Shared, 0, SharedMemorySize);
StnRecordBase = Shared + 32;
SMEM = (struct SharedMem *)Shared;
SMEM->Version = 1;
SMEM->SharedMemLen = SharedMemorySize;
SMEM->NeedRefresh = TRUE;
SMEM->Arch = sizeof(void *);
SMEM->SubVersion = 1;
Stn1 = (struct STATIONRECORD *)StnRecordBase;
StationRecords = (struct STATIONRECORD **)Stn1;
Stn1++;
StationRecordPool = Stn1;
for (i = 1; i < MaxStations; i++) // Already have first
{
Stn2 = Stn1;
Stn2++;
Stn1->Next = Stn2;
Stn1 = Stn2;
}
Debugprintf("End of Stations %p", Stn1);
endofStations = Stn1;
Stn1 += 2; // Try to fix corruption of messages.
// Build Message Record Pool
Msg1 = (struct APRSMESSAGE *)Stn1;
MessageRecordPool = Msg1;
for (i = 1; i < MAXMESSAGES; i++) // Already have first
{
Msg2 = Msg1;
Msg2++;
Msg1->Next = Msg2;
Msg1 = Msg2;
}
if (PosnSet == 0)
{
SYMBOL = '.';
SYMSET = '\\'; // Undefined Posn Symbol
}
else
{
// Convert posn to floating degrees
char LatDeg[3], LonDeg[4];
memcpy(LatDeg, LAT, 2);
LatDeg[2]=0;
Lat=atof(LatDeg) + (atof(LAT+2)/60);
if (LAT[7] == 'S') Lat=-Lat;
memcpy(LonDeg, LON, 3);
LonDeg[3]=0;
Lon=atof(LonDeg) + (atof(LON+3)/60);
if (LON[8]== 'W') Lon=-Lon;
SYMBOL = CFGSYMBOL;
SYMSET = CFGSYMSET;
}
// First record has control info for APRS Mapping App
Stn1 = (struct STATIONRECORD *)StnRecordBase;
memcpy(Stn1->Callsign, APRSCall, 10);
Stn1->Lat = Lat;
Stn1->Lon = Lon;
Stn1->LastPort = MaxStations;
#ifndef WIN32
// Open unix socket for messaging app
sfd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sfd == -1)
{
perror("Socket");
}
else
{
u_long param=1;
ioctl(sfd, FIONBIO, ¶m); // Set non-blocking
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, TX_SOCK_PATH, sizeof(my_addr.sun_path) - 1);
memset(&peer_addr, 0, sizeof(struct sockaddr_un));
peer_addr.sun_family = AF_UNIX;
strncpy(peer_addr.sun_path, RX_SOCK_PATH, sizeof(peer_addr.sun_path) - 1);
unlink(TX_SOCK_PATH);
if (bind(sfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_un)) == -1)
perror("bind");
}
#endif
// Convert Dest ADDRS to AX.25
for (i = 0; i < 26; i++)
{
DCall = &APRSDESTS[i][0];
if (strchr(DCall, '*'))
AXDESTLEN[i] = (int)strlen(DCall) - 1;
else
AXDESTLEN[i] = 6;
ConvToAX25(DCall, &AXDESTS[i][0]);
}
// Process any Object Definitions
// Setup Heard Data Area
HEARDENTRIES = 0;
MAXHEARDENTRIES = MAXHEARD;
APRSMONVECPTR->HOSTAPPLFLAGS = 0x80; // Request Monitoring
if (ISPort && IGateEnabled)
{
_beginthread(APRSISThread, 0, (VOID *) TRUE);
}
if (GPSPort[0])
OpenGPSPort();
WritetoConsole("APRS Digi/Gateway Enabled\n");
APRSWeb = TRUE;
read_png((unsigned char *)IconData);
// Reload saved messages
if (SaveAPRSMsgs)
GetSavedAPRSMessages();
// If a Run parameter was supplied, run the program
if (RunProgram[0] == 0)
return TRUE;
#ifndef WIN32
{
char * arg_list[] = {NULL, NULL};
pid_t child_pid;
signal(SIGCHLD, SIG_IGN); // Silently (and portably) reap children.
// Fork and Exec program
printf("Trying to start %s\n", RunProgram);
arg_list[0] = RunProgram;