forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARDOP.c
3125 lines (2288 loc) · 66.7 KB
/
ARDOP.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-2015 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
*/
//
// Interface to allow G8BPQ switch to use ARDOP Virtual TNC
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <stdio.h>
#include <time.h>
#include "CHeaders.h"
#ifdef WIN32
#include <Psapi.h>
#endif
int (WINAPI FAR *GetModuleFileNameExPtr)();
int (WINAPI FAR *EnumProcessesPtr)();
#define SD_RECEIVE 0x00
#define SD_SEND 0x01
#define SD_BOTH 0x02
#include "bpq32.h"
#include "tncinfo.h"
#define WSA_ACCEPT WM_USER + 1
#define WSA_DATA WM_USER + 2
#define WSA_CONNECT WM_USER + 3
static int Socket_Data(int sock, int error, int eventcode);
int ARDOPKillTNC(struct TNCINFO * TNC);
int ARDOPRestartTNC(struct TNCINFO * TNC);
int KillPopups(struct TNCINFO * TNC);
VOID MoveWindows(struct TNCINFO * TNC);
int SendReporttoWL2K(struct TNCINFO * TNC);
char * CheckAppl(struct TNCINFO * TNC, char * Appl);
int DoScanLine(struct TNCINFO * TNC, char * Buff, int Len);
BOOL KillOldTNC(char * Path);
int ARDOPSendData(struct TNCINFO * TNC, char * Buff, int Len);
VOID ARDOPSendCommand(struct TNCINFO * TNC, char * Buff, BOOL Queue);
VOID SendToTNC(struct TNCINFO * TNC, UCHAR * Encoded, int EncLen);
VOID ARDOPProcessDataPacket(struct TNCINFO * TNC, UCHAR * Type, UCHAR * Data, int Length);
#ifndef LINBPQ
BOOL CALLBACK EnumARDOPWindowsProc(HWND hwnd, LPARAM lParam);
#endif
static char ClassName[]="ARDOPSTATUS";
static char WindowTitle[] = "ARDOP";
static int RigControlRow = 165;
#define WINMOR
#define NARROWMODE 21
#define WIDEMODE 22
#ifndef LINBPQ
#include <commctrl.h>
#endif
extern char * PortConfig[33];
extern int SemHeldByAPI;
static RECT Rect;
struct TNCINFO * TNCInfo[34]; // Records are Malloc'd
static int ProcessLine(char * buf, int Port);
unsigned long _beginthread( void( *start_address )(), unsigned stack_size, int arglist);
// RIGCONTROL COM60 19200 ICOM IC706 5e 4 14.103/U1w 14.112/u1 18.1/U1n 10.12/l1
int GenCRC16(unsigned char * Data, unsigned short length)
{
// For CRC-16-CCITT = x^16 + x^12 +x^5 + 1 intPoly = 1021 Init FFFF
// intSeed is the seed value for the shift register and must be in the range 0-&HFFFF
int intRegister = 0xffff; //intSeed
int i,j;
int Bit;
int intPoly = 0x8810; // This implements the CRC polynomial x^16 + x^12 +x^5 + 1
for (j = 0; j < (length); j++)
{
int Mask = 0x80; // Top bit first
for (i = 0; i < 8; i++) // for each bit processing MS bit first
{
Bit = Data[j] & Mask;
Mask >>= 1;
if (intRegister & 0x8000) // Then ' the MSB of the register is set
{
// Shift left, place data bit as LSB, then divide
// Register := shiftRegister left shift 1
// Register := shiftRegister xor polynomial
if (Bit)
intRegister = 0xFFFF & (1 + (intRegister << 1));
else
intRegister = 0xFFFF & (intRegister << 1);
intRegister = intRegister ^ intPoly;
}
else
{
// the MSB is not set
// Register is not divisible by polynomial yet.
// Just shift left and bring current data bit onto LSB of shiftRegister
if (Bit)
intRegister = 0xFFFF & (1 + (intRegister << 1));
else
intRegister = 0xFFFF & (intRegister << 1);
}
}
}
return intRegister;
}
BOOL checkcrc16(unsigned char * Data, unsigned short length)
{
int intRegister = 0xffff; //intSeed
int i,j;
int Bit;
int intPoly = 0x8810; // This implements the CRC polynomial x^16 + x^12 +x^5 + 1
for (j = 0; j < (length - 2); j++) // ' 2 bytes short of data length
{
int Mask = 0x80; // Top bit first
for (i = 0; i < 8; i++) // for each bit processing MS bit first
{
Bit = Data[j] & Mask;
Mask >>= 1;
if (intRegister & 0x8000) // Then ' the MSB of the register is set
{
// Shift left, place data bit as LSB, then divide
// Register := shiftRegister left shift 1
// Register := shiftRegister xor polynomial
if (Bit)
intRegister = 0xFFFF & (1 + (intRegister << 1));
else
intRegister = 0xFFFF & (intRegister << 1);
intRegister = intRegister ^ intPoly;
}
else
{
// the MSB is not set
// Register is not divisible by polynomial yet.
// Just shift left and bring current data bit onto LSB of shiftRegister
if (Bit)
intRegister = 0xFFFF & (1 + (intRegister << 1));
else
intRegister = 0xFFFF & (intRegister << 1);
}
}
}
if (Data[length - 2] == intRegister >> 8)
if (Data[length - 1] == (intRegister & 0xFF))
return TRUE;
return FALSE;
}
static ProcessLine(char * buf, int Port)
{
UCHAR * ptr,* p_cmd;
char * p_ipad = 0;
char * p_port = 0;
unsigned short WINMORport = 0;
int BPQport;
int len=510;
struct TNCINFO * TNC;
char errbuf[256];
strcpy(errbuf, buf);
ptr = strtok(buf, " \t\n\r");
if(ptr == NULL) return (TRUE);
if(*ptr =='#') return (TRUE); // comment
if(*ptr ==';') return (TRUE); // comment
if (_stricmp(buf, "ADDR"))
return FALSE; // Must start with ADDR
ptr = strtok(NULL, " \t\n\r");
BPQport = Port;
p_ipad = ptr;
TNC = TNCInfo[BPQport] = malloc(sizeof(struct TNCINFO));
memset(TNC, 0, sizeof(struct TNCINFO));
TNC->InitScript = malloc(1000);
TNC->InitScript[0] = 0;
if (p_ipad == NULL)
p_ipad = strtok(NULL, " \t\n\r");
if (p_ipad == NULL) return (FALSE);
p_port = strtok(NULL, " \t\n\r");
if (p_port == NULL) return (FALSE);
WINMORport = atoi(p_port);
TNC->destaddr.sin_family = AF_INET;
TNC->destaddr.sin_port = htons(WINMORport);
TNC->Datadestaddr.sin_family = AF_INET;
TNC->Datadestaddr.sin_port = htons(WINMORport+1);
TNC->WINMORHostName = malloc(strlen(p_ipad)+1);
if (TNC->WINMORHostName == NULL) return TRUE;
strcpy(TNC->WINMORHostName,p_ipad);
ptr = strtok(NULL, " \t\n\r");
if (ptr)
{
if (_stricmp(ptr, "PTT") == 0)
{
ptr = strtok(NULL, " \t\n\r");
if (ptr)
{
if (_stricmp(ptr, "CI-V") == 0)
TNC->PTTMode = PTTCI_V;
else if (_stricmp(ptr, "CAT") == 0)
TNC->PTTMode = PTTCI_V;
else if (_stricmp(ptr, "RTS") == 0)
TNC->PTTMode = PTTRTS;
else if (_stricmp(ptr, "DTR") == 0)
TNC->PTTMode = PTTDTR;
else if (_stricmp(ptr, "DTRRTS") == 0)
TNC->PTTMode = PTTDTR | PTTRTS;
ptr = strtok(NULL, " \t\n\r");
}
}
}
if (ptr)
{
if (_memicmp(ptr, "PATH", 4) == 0)
{
p_cmd = strtok(NULL, "\n\r");
if (p_cmd) TNC->ProgramPath = _strdup(p_cmd);
// if (p_cmd) TNC->ProgramPath = _strdup(_strupr(p_cmd));
}
}
TNC->MaxConReq = 10; // Default
// Read Initialisation lines
while(TRUE)
{
if (GetLine(buf) == 0)
return TRUE;
strcpy(errbuf, buf);
if (memcmp(buf, "****", 4) == 0)
return TRUE;
ptr = strchr(buf, ';');
if (ptr)
{
*ptr++ = 13;
*ptr = 0;
}
if ((_memicmp(buf, "CAPTURE", 7) == 0) || (_memicmp(buf, "PLAYBACK", 8) == 0))
{} // Ignore
else
/*
if (_memicmp(buf, "PATH", 4) == 0)
{
char * Context;
p_cmd = strtok_s(&buf[5], "\n\r", &Context);
if (p_cmd) TNC->ProgramPath = _strdup(p_cmd);
}
else
*/
if (_memicmp(buf, "WL2KREPORT", 10) == 0)
TNC->WL2K = DecodeWL2KReportLine(buf);
else
if (_memicmp(buf, "BUSYHOLD", 8) == 0) // Hold Time for Busy Detect
TNC->BusyHold = atoi(&buf[8]);
else
if (_memicmp(buf, "BUSYWAIT", 8) == 0) // Wait time beofre failing connect if busy
TNC->BusyWait = atoi(&buf[8]);
else
if (_memicmp(buf, "MAXCONREQ", 9) == 0) // Hold Time for Busy Detect
TNC->MaxConReq = atoi(&buf[9]);
else
if (_memicmp(buf, "STARTINROBUST", 13) == 0)
TNC->StartInRobust = TRUE;
else
if (_memicmp(buf, "ROBUST", 6) == 0)
{
if (_memicmp(&buf[7], "TRUE", 4) == 0)
TNC->Robust = TRUE;
strcat (TNC->InitScript, buf);
}
else
strcat (TNC->InitScript, buf);
}
return (TRUE);
}
void ARDOPThread(int port);
VOID ARDOPProcessDataSocketData(int port);
int ConnecttoARDOP();
static VOID ARDOPProcessReceivedData(struct TNCINFO * TNC);
int V4ProcessReceivedData(struct TNCINFO * TNC);
VOID ARDOPReleaseTNC(struct TNCINFO * TNC);
VOID SuspendOtherPorts(struct TNCINFO * ThisTNC);
VOID ReleaseOtherPorts(struct TNCINFO * ThisTNC);
VOID WritetoTrace(struct TNCINFO * TNC, char * Msg, int Len);
#define MAXBPQPORTS 32
static time_t ltime;
static SOCKADDR_IN sinx;
static SOCKADDR_IN rxaddr;
static int addrlen=sizeof(sinx);
unsigned short int compute_crc(unsigned char *buf,int len);
VOID ARDOPSendCommand(struct TNCINFO * TNC, char * Buff, BOOL Queue)
{
// Encode and send to TNC. May be TCP or Serial
// Command Formst is C:TEXT<CR><CRC>
UINT * buffptr;
int EncLen;
unsigned short CRC;
UCHAR * Encoded;
buffptr = GetBuff();
// Have to save copy for possible retry (and possibly until previous
// command is acked
if (buffptr == NULL)
{
return;
}
Encoded = (UCHAR *)&buffptr[2];
EncLen = sprintf(Encoded, "C:%s\r", Buff);
CRC = GenCRC16(Encoded + 2, EncLen -2); // Don't include c:
Encoded[EncLen++] = CRC >> 8;
Encoded[EncLen++] = CRC & 0xFF;
buffptr[1] = EncLen;
if (Queue)
{
if (TNC->BPQtoWINMOR_Q)
{
// Something already queued
C_Q_ADD(&TNC->BPQtoWINMOR_Q, buffptr);
return;
}
// Nothing on Queue, so OK to send now
C_Q_ADD(&TNC->BPQtoWINMOR_Q, buffptr);
SendToTNC(TNC, Encoded, EncLen);
return;
}
SendToTNC(TNC, Encoded, EncLen);
ReleaseBuffer(buffptr);
return;
}
VOID SendToTNC(struct TNCINFO * TNC, UCHAR * Encoded, int EncLen)
{
int SentLen;
if (TNC->hDevice)
{
WriteCOMBlock(TNC->hDevice, Encoded, EncLen);
return;
}
if(TNC->WINMORSock)
{
SentLen = send(TNC->WINMORSock, Encoded, EncLen, 0);
if (SentLen != EncLen)
{
// WINMOR doesn't seem to recover from a blocked write. For now just reset
// if (bytes == SOCKET_ERROR)
// {
int winerr=WSAGetLastError();
char ErrMsg[80];
sprintf(ErrMsg, "ARDOP Write Failed for port %d - error code = %d\r\n", TNC->Port, winerr);
WritetoConsole(ErrMsg);
// if (winerr != WSAEWOULDBLOCK)
// {
closesocket(TNC->WINMORSock);
TNC->CONNECTED = FALSE;
return;
}
}
// else
// {
// bytes=0; // resent whole packet
// }
// }
// Partial Send or WSAEWOULDBLOCK. Save data, and send once busy clears
// Get a buffer
// buffptr=GetBuff();
// if (buffptr == 0)
// {
// No buffers, so can only break connection and try again
// closesocket(WINMORSock[MasterPort[port]]);
// CONNECTED[MasterPort[port]]=FALSE;
// return (0);
// }
// buffptr[1]=txlen-bytes; // Bytes still to send
// memcpy(buffptr+2,&txbuff[bytes],txlen-bytes);
// C_Q_ADD(&BPQtoWINMOR_Q[MasterPort[port]],buffptr);
// return (0);
}
int ARDOPSendData(struct TNCINFO * TNC, char * Buff, int Len)
{
// Encode and send to TNC. May be TCP or Serial
UINT * buffptr = GetBuff();
int EncLen;
unsigned short CRC;
// Have to save copy for possible retry (and possibly until previous
// command is acked
UCHAR * Encoded = (UCHAR *)&buffptr[2];
UCHAR * Msg = (UCHAR *)&buffptr[2];
if (buffptr == NULL)
return 0;
*(Encoded++) = 'D';
*(Encoded++) = ':';
*(Encoded++) = Len >> 8;
*(Encoded++) = Len & 0xff;
memcpy(Encoded, Buff, Len);
Encoded += Len;
EncLen = Len + 4;
CRC = GenCRC16(Msg + 2, Len + 2); // Don't include c:
*(Encoded++) = CRC >> 8;
*(Encoded++) = CRC & 0xFF;
buffptr[1] = Len + 6;
if (TNC->BPQtoWINMOR_Q)
{
// Something already queued
C_Q_ADD(&TNC->BPQtoWINMOR_Q, buffptr);
return Len;
}
// Nothing on Queue, so OK to send now
C_Q_ADD(&TNC->BPQtoWINMOR_Q, buffptr);
SendToTNC(TNC, Msg, Len + 6);
return Len;
}
VOID ARDOPChangeMYC(struct TNCINFO * TNC, char * Call)
{
UCHAR TXMsg[100];
int datalen;
if (strcmp(Call, TNC->CurrentMYC) == 0)
return; // No Change
strcpy(TNC->CurrentMYC, Call);
// ARDOPSendCommand(TNC, "CODEC FALSE");
datalen = sprintf(TXMsg, "MYCALL %s", Call);
ARDOPSendCommand(TNC, TXMsg, TRUE);
// ARDOPSendCommand(TNC, "CODEC TRUE");
// TNC->StartSent = TRUE;
// ARDOPSendCommand(TNC, "MYCALL", TRUE);
}
static int ExtProc(int fn, int port,unsigned char * buff)
{
int datalen;
UINT * buffptr;
char txbuff[500];
unsigned int bytes,txlen=0;
int Param;
HKEY hKey=0;
struct TNCINFO * TNC = TNCInfo[port];
struct STREAMINFO * STREAM = &TNC->Streams[0];
struct ScanEntry * Scan;
if (TNC == NULL)
return 0; // Port not defined
if (TNC->CONNECTED == 0)
{
// clear Q if not connected
while(TNC->BPQtoWINMOR_Q)
{
buffptr = Q_REM(&TNC->BPQtoWINMOR_Q);
if (buffptr)
ReleaseBuffer(buffptr);
}
}
switch (fn)
{
case 1: // poll
while (TNC->PortRecord->UI_Q)
{
int datalen;
char * Buffer;
char FECMsg[256] = "";
char Call[12] = " ";
struct _MESSAGE * buffptr;
buffptr = Q_REM(&TNC->PortRecord->UI_Q);
if (TNC->CONNECTED == 0)
{
// discard if not connected
ReleaseBuffer(buffptr);
continue;
}
datalen = buffptr->LENGTH - 7;
Buffer = &buffptr->DEST[0]; // Raw Frame
Buffer[datalen] = 0;
// Frame has ax.25 format header. Convert to Text
ConvFromAX25(Buffer + 7, Call); // Origin
strlop(Call, ' ');
strcat(FECMsg, Call);
strcat(FECMsg, ">");
ConvFromAX25(Buffer, Call); // Dest
strlop(Call, ' ');
strcat(FECMsg, Call);
Buffer += 14; // TO Digis
datalen -= 7;
while ((Buffer[-1] & 1) == 0)
{
Call[0] = ',';
ConvFromAX25(Buffer, &Call[1]);
strlop(&Call[1], ' ');
strcat(FECMsg, Call);
Buffer += 7; // End of addr
datalen -= 7;
}
strcat(FECMsg, "|");
if (Buffer[0] == 3) // UI
{
Buffer += 2;
datalen -= 2;
}
strcat(FECMsg, Buffer);
ARDOPSendData(TNC, FECMsg, strlen(FECMsg));
if (TNC->BusyFlags)
TNC->FECPending = 1;
else
ARDOPSendCommand(TNC,"FECSEND TRUE", TRUE);
ReleaseBuffer((UINT *)buffptr);
}
if (TNC->Busy) // Count down to clear
{
if ((TNC->BusyFlags & CDBusy) == 0) // TNC Has reported not busy
{
TNC->Busy--;
if (TNC->Busy == 0)
SetWindowText(TNC->xIDC_CHANSTATE, "Clear");
strcpy(TNC->WEB_CHANSTATE, "Clear");
}
}
if (TNC->BusyDelay)
{
// Still Busy?
if (InterlockedCheckBusy(TNC) == FALSE)
{
// No, so send
ARDOPSendCommand(TNC, TNC->ConnectCmd, TRUE);
TNC->Streams[0].Connecting = TRUE;
memset(TNC->Streams[0].RemoteCall, 0, 10);
memcpy(TNC->Streams[0].RemoteCall, &TNC->ConnectCmd[8], strlen(TNC->ConnectCmd)-10);
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s", TNC->Streams[0].MyCall, TNC->Streams[0].RemoteCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
free(TNC->ConnectCmd);
TNC->BusyDelay = 0;
}
else
{
// Wait Longer
TNC->BusyDelay--;
if (TNC->BusyDelay == 0)
{
// Timed out - Send Error Response
UINT * buffptr = GetBuff();
if (buffptr == 0) return (0); // No buffers, so ignore
buffptr[1]=39;
memcpy(buffptr+2,"Sorry, Can't Connect - Channel is busy\r", 39);
C_Q_ADD(&TNC->WINMORtoBPQ_Q, buffptr);
free(TNC->ConnectCmd);
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", TNC->Streams[0].MyCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
}
}
}
if (TNC->HeartBeat++ > 600 || (TNC->Streams[0].Connected && TNC->HeartBeat > 50)) // Every Minute unless connected
{
TNC->HeartBeat = 0;
if (TNC->CONNECTED)
{
// Probe link
if (TNC->Streams[0].Connecting || TNC->Streams[0].Connected)
fn =fn; //ARDOPSendCommand(TNC, "MODE", TRUE);
else
{
// if (time(NULL) - TNC->WinmorRestartCodecTimer > 300) // 5 mins
// {
// ARDOPSendCommand(TNC, "CODEC FALSE", TRUE);
// ARDOPSendCommand(TNC, "CODEC TRUE", TRUE);
// }
// else
ARDOPSendCommand(TNC, "STATE", TRUE);
}
}
}
if (TNC->FECMode)
{
if (TNC->FECIDTimer++ > 6000) // ID every 10 Mins
{
if (!TNC->Busy)
{
TNC->FECIDTimer = 0;
ARDOPSendCommand(TNC, "SENDID", TRUE);
}
}
if (TNC->FECPending) // Check if FEC Send needed
{
if (!TNC->Busy)
{
TNC->FECPending = 0;
ARDOPSendCommand(TNC,"FECSEND TRUE", TRUE);
}
}
}
if (STREAM->NeedDisc)
{
STREAM->NeedDisc--;
if (STREAM->NeedDisc == 0)
{
// Send the DISCONNECT
ARDOPSendCommand(TNC, "DISCONNECT", TRUE);
}
}
if (TNC->DiscPending)
{
TNC->DiscPending--;
if (TNC->DiscPending == 0)
{
// Too long in Disc Pending - Kill and Restart TNC
if (TNC->WIMMORPID)
{
ARDOPKillTNC(TNC);
ARDOPRestartTNC(TNC);
}
}
}
if (TNC->TimeSinceLast++ > 800) // Allow 10 secs for Keepalive
{
// Restart TNC
if (TNC->ProgramPath)
{
if (strstr(TNC->ProgramPath, "WINMOR TNC"))
{
struct tm * tm;
char Time[80];
TNC->Restarts++;
TNC->LastRestart = time(NULL);
tm = gmtime(&TNC->LastRestart);
sprintf_s(Time, sizeof(Time),"%04d/%02d/%02d %02d:%02dZ",
tm->tm_year +1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
MySetWindowText(TNC->xIDC_RESTARTTIME, Time);
strcpy(TNC->WEB_RESTARTTIME, Time);
sprintf_s(Time, sizeof(Time),"%d", TNC->Restarts);
MySetWindowText(TNC->xIDC_RESTARTS, Time);
strcpy(TNC->WEB_RESTARTS, Time);
ARDOPKillTNC(TNC);
ARDOPRestartTNC(TNC);
TNC->TimeSinceLast = 0;
}
}
}
if (TNC->PortRecord->ATTACHEDSESSIONS[0] && TNC->Streams[0].Attached == 0)
{
// New Attach
int calllen;
char Msg[80];
TNC->Streams[0].Attached = TRUE;
calllen = ConvFromAX25(TNC->PortRecord->ATTACHEDSESSIONS[0]->L4USER, TNC->Streams[0].MyCall);
TNC->Streams[0].MyCall[calllen] = 0;
// Stop Listening, and set MYCALL to user's call
ARDOPSendCommand(TNC, "LISTEN FALSE", TRUE);
ARDOPChangeMYC(TNC, TNC->Streams[0].MyCall);
// Stop other ports in same group
SuspendOtherPorts(TNC);
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", TNC->Streams[0].MyCall);
MySetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
// Stop Scanning
sprintf(Msg, "%d SCANSTOP", TNC->Port);
Rig_Command(-1, Msg);
}
if (TNC->Streams[0].Attached)
CheckForDetach(TNC, 0, &TNC->Streams[0], TidyClose, ForcedClose, CloseComplete);
if (TNC->Streams[0].ReportDISC)
{
TNC->Streams[0].ReportDISC = FALSE;
buff[4] = 0;
return -1;
}
if (TNC->CONNECTED == FALSE && TNC->CONNECTING == FALSE)
{
// See if time to reconnect
time(<ime);
if (ltime - TNC->lasttime > 9 )
{
ConnecttoARDOP(port);
TNC->lasttime = ltime;
}
}
// See if any frames for this port
if (TNC->Streams[0].BPQtoPACTOR_Q) //Used for CTEXT
{
UINT * buffptr = Q_REM(&TNC->Streams[0].BPQtoPACTOR_Q);
txlen=buffptr[1];
memcpy(txbuff,buffptr+2,txlen);
bytes = ARDOPSendData(TNC, &txbuff[0], txlen);
STREAM->BytesTXed += bytes;
WritetoTrace(TNC, txbuff, txlen);
ReleaseBuffer(buffptr);
}
if (TNC->WINMORtoBPQ_Q != 0)
{
buffptr=Q_REM(&TNC->WINMORtoBPQ_Q);
datalen=buffptr[1];
buff[4] = 0; // Compatibility with Kam Driver
buff[7] = 0xf0;
memcpy(&buff[8],buffptr+2,datalen); // Data goes to +7, but we have an extra byte
datalen+=8;
buff[5]=(datalen & 0xff);
buff[6]=(datalen >> 8);
ReleaseBuffer(buffptr);
return (1);
}
return (0);
case 2: // send
if (!TNC->CONNECTED)
{
// Send Error Response
UINT * buffptr = GetBuff();
if (buffptr == 0) return (0); // No buffers, so ignore
buffptr[1]=36;
memcpy(buffptr+2,"No Connection to ARDOP TNC\r", 36);
C_Q_ADD(&TNC->WINMORtoBPQ_Q, buffptr);
return 0; // Don't try if not connected
}
if (TNC->Streams[0].BPQtoPACTOR_Q) //Used for CTEXT
{
UINT * buffptr = Q_REM(&TNC->Streams[0].BPQtoPACTOR_Q);
txlen=buffptr[1];
memcpy(txbuff,buffptr+2,txlen);
bytes = ARDOPSendData(TNC, &txbuff[0], txlen);
STREAM->BytesTXed += bytes;
WritetoTrace(TNC, txbuff, txlen);
ReleaseBuffer(buffptr);
}
if (TNC->SwallowSignon)
{
TNC->SwallowSignon = FALSE; // Discard *** connected
return 0;
}
txlen=(buff[6]<<8) + buff[5]-8;
if (TNC->Streams[0].Connected)
{
STREAM->PacketsSent++;
if (STREAM->PacketsSent == 3)
{
// if (TNC->Robust)
// ARDOPSendCommand(TNC, "ROBUST TRUE");
// else
// ARDOPSendCommand(TNC, "ROBUST FALSE");
}
bytes=ARDOPSendData(TNC, &buff[8], txlen);
STREAM->BytesTXed += bytes;
WritetoTrace(TNC, &buff[8], txlen);
}
else
{
if (_memicmp(&buff[8], "D\r", 2) == 0 || _memicmp(&buff[8], "BYE\r", 4) == 0)
{
TNC->Streams[0].ReportDISC = TRUE; // Tell Node
return 0;
}
if (TNC->FECMode)
{
char Buffer[300];
int len;
// Send FEC Data
buff[8 + txlen] = 0;
len = sprintf(Buffer, "%-9s: %s", TNC->Streams[0].MyCall, &buff[8]);
ARDOPSendData(TNC, Buffer, len);