forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HALDriver.c
1904 lines (1353 loc) · 44.6 KB
/
HALDriver.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
*/
//
// DLL to inteface HAL Communications Corp Clover/Pacor controllers to BPQ32 switch
//
// Uses BPQ EXTERNAL interface
//
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include "time.h"
#include "CHeaders.h"
#include "tncinfo.h"
#include "bpq32.h"
#define HAL 1
#define SetMYCALL 0x13
#define ConnectEnable 0x52
#define ConnectDisable 0x42
#define SetEAS 0x59 // Echo as Sent
#define SetTones 0xec
#define ClearOnDisc 0x57
static char ClassName[]="HALSTATUS";
static char WindowTitle[] = "HAL";
static int RigControlRow = 185;
struct TNCINFO * TNCInfo[34]; // Records are Malloc'd
#define SOH 0x01 // CONTROL CODES
#define ETB 0x17
#define DLE 0x10
//int MaxStreams = 0;
#ifndef LINBPQ
extern HFONT hFont;
#endif
static char status[23][50] = {"IDLE", "TFC", "RQ", "ERR", "PHS", "OVER", "FSK TX",
"FSK RX", "P-MODE100", "P-MODE200", "HUFMAN ON", "HUFMAN OFF", "P-MODE SBY(LISTEN ON)",
"P-MODE SBY(LISTEN OFF)", "ISS", "IRS",
"AMTOR SBY(LISTEN ON)", "AMTOR SBY(LISTEN OFF)", "AMTOR FEC TX", "AMTOR FEC RX", "P-MODE FEC TX",
"FREE SIGNAL TX (AMTOR)", "FREE SIGNAL TX TIMED OUT (AMTOR)"};
struct TNCINFO * CreateTTYInfo(int port, int speed);
BOOL OpenConnection(int);
BOOL SetupConnection(int);
static BOOL WriteCommBlock(struct TNCINFO * TNC);
static void CheckRX(struct TNCINFO * TNC);
VOID HALPoll(int Port);
VOID ProcessDEDFrame(struct TNCINFO * TNC, UCHAR * rxbuff, int len);
VOID ProcessTermModeResponse(struct TNCINFO * TNC);
static VOID DoTNCReinit(struct TNCINFO * TNC);
VOID DoTermModeTimeout(struct TNCINFO * TNC);
VOID ProcessHALBuffer(struct TNCINFO * TNC, int Length);
VOID ProcessHALCmd(struct TNCINFO * TNC);
VOID ProcessHALData(struct TNCINFO * TNC);
VOID ProcessKHOSTPacket(struct TNCINFO * TNC, UCHAR * rxbuffer, int Len);
VOID ProcessKNormCommand(struct TNCINFO * TNC, UCHAR * rxbuffer);
VOID ProcessHostFrame(struct TNCINFO * TNC, UCHAR * rxbuffer, int Len);
VOID DoMonitor(struct TNCINFO * TNC, UCHAR * Msg, int Len);
BOOL HALConnected(struct TNCINFO * TNC, char * Call);
VOID HALDisconnected(struct TNCINFO * TNC);
static VOID EncodeAndSend(struct TNCINFO * TNC, UCHAR * txbuffer, int Len);
VOID SendCmd(struct TNCINFO * TNC, UCHAR * txbuffer, int Len);
int DLEEncode(UCHAR * inbuff, UCHAR * outbuff, int len);
int DLEDecode(UCHAR * inbuff, UCHAR * outbuff, int len);
VOID COMClearDTR(HANDLE fd);
VOID COMClearRTS(HANDLE fd);
int DoScanLine(struct TNCINFO * TNC, char * Buff, int Len);
//static HANDLE LogHandle[4] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
//char * Logs[4] = {"1", "2", "3", "4"};
//char BaseDir[]="c:";
static VOID CloseLogfile(int Flags)
{
// CloseHandle(LogHandle[Flags]);
// LogHandle[Flags] = INVALID_HANDLE_VALUE;
}
static VOID OpenLogfile(int Flags)
{
/*
UCHAR FN[MAX_PATH];
time_t T;
struct tm * tm;
T = time(NULL);
tm = gmtime(&T);
sprintf(FN,"%s\\HALLog_%02d%02d%02d_%s.bin", BaseDir, tm->tm_mday, tm->tm_hour, tm->tm_min, Logs[Flags]);
LogHandle[Flags] = CreateFile(FN,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
SetFilePointer(LogHandle[Flags], 0, 0, FILE_END);
return (LogHandle[Flags] != INVALID_HANDLE_VALUE);
*/
}
static void WriteLogLine(int Flags, char * Msg, int MsgLen)
{
// int cnt;
// WriteFile(LogHandle[Flags] ,Msg , MsgLen, &cnt, NULL);
}
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
ptr = strtok(NULL, " \t\n\r");
if (_stricmp(buf, "APPL") == 0) // Using BPQ32 COnfig
{
BPQport = Port;
p_cmd = ptr;
}
else
if (_stricmp(buf, "PORT") != 0) // Using Old Config
{
// New config without a PORT or APPL - this is a Config Command
strcpy(buf, errbuf);
strcat(buf, "\r");
BPQport = Port;
TNC = TNCInfo[BPQport] = zalloc(sizeof(struct TNCINFO));
TNC->InitScript = malloc(1000);
TNC->InitScript[0] = 0;
goto ConfigLine;
}
else
{
// Old Config from file
BPQport=0;
BPQport = atoi(ptr);
p_cmd = strtok(NULL, " \t\n\r");
if (Port && Port != BPQport)
{
// Want a particular port, and this isn't it
while(TRUE)
{
if (GetLine(buf) == 0)
return TRUE;
if (memcmp(buf, "****", 4) == 0)
return TRUE;
}
}
}
if(BPQport > 0 && BPQport < 33)
{
TNC = TNCInfo[BPQport] = zalloc(sizeof(struct TNCINFO));
TNC->InitScript = malloc(1000);
TNC->InitScript[0] = 0;
if (p_cmd != NULL)
{
if (p_cmd[0] != ';' && p_cmd[0] != '#')
TNC->ApplCmd=_strdup(p_cmd);
}
// Read Initialisation lines
while(TRUE)
{
if (GetLine(buf) == 0)
return TRUE;
ConfigLine:
strcpy(errbuf, buf);
if (memcmp(buf, "****", 4) == 0)
return TRUE;
ptr = strchr(buf, ';');
if (ptr)
{
*ptr++ = 13;
*ptr = 0;
}
if (_memicmp(buf, "WL2KREPORT", 10) == 0)
{
TNC->WL2K = DecodeWL2KReportLine(buf);
continue;
}
if (_memicmp(buf, "NEEDXONXOFF", 10) == 0)
{
TNC->XONXOFF = TRUE;
continue;
}
if (_memicmp(buf, "TONES", 5) == 0)
{
int tone1 = 0, tone2 = 0;
ptr = strtok(&buf[6], " ,/\t\n\r");
if (ptr)
{
tone1 = atoi(ptr);
ptr = strtok(NULL, " ,/\t\n\r");
if (ptr)
{
tone2 = atoi(ptr);
ptr = &TNC->InitScript[TNC->InitScriptLen];
// Try putting into FSK mode first
*(ptr++) = 0x84;
*(ptr++) = SetTones; // Set Tones (Mark, Space HI byte first)
*(ptr++) = tone1 >> 8;
*(ptr++) = tone1 & 0xff;
*(ptr++) = tone2 >> 8;
*(ptr++) = tone2 & 0xff;
TNC->InitScriptLen += 6;
continue;
}
}
goto BadLine;
}
if (_memicmp(buf, "DEFAULTMODE ", 12) == 0)
{
ptr = strtok(&buf[12], " ,\t\n\r");
if (ptr)
{
if (_stricmp(ptr, "CLOVER") == 0)
TNC->DefaultMode = Clover;
else if (_stricmp(ptr, "PACTOR") == 0)
TNC->DefaultMode = Pactor;
else if (_stricmp(ptr, "AMTOR") == 0)
TNC->DefaultMode = AMTOR;
else goto BadLine;
continue;
}
goto BadLine;
}
}
BadLine:
WritetoConsole(" Bad config record ");
WritetoConsole(errbuf);
WritetoConsole("\r\n");
}
return (TRUE);
}
static int ExtProc(int fn, int port,unsigned char * buff)
{
int txlen = 0;
UINT * buffptr;
struct TNCINFO * TNC = TNCInfo[port];
struct STREAMINFO * STREAM;
int Stream;
if (TNC == NULL)
return 0;
if (fn < 4 || fn > 5)
if (TNC->hDevice == 0)
return 0; // Port not open
STREAM = &TNC->Streams[0];
switch (fn)
{
case 1: // poll
while (TNC->PortRecord->UI_Q) // Release anything accidentally put on UI_Q
{
buffptr = Q_REM(&TNC->PortRecord->UI_Q);
ReleaseBuffer(buffptr);
}
//for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (STREAM->ReportDISC)
{
STREAM->ReportDISC = FALSE;
buff[4] = 0;
return -1;
}
}
CheckRX(TNC);
HALPoll(port);
//for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (STREAM->PACTORtoBPQ_Q !=0)
{
int datalen;
buffptr=Q_REM(&STREAM->PACTORtoBPQ_Q);
datalen=buffptr[1];
buff[4] = 0;
buff[7] = 0xf0;
memcpy(&buff[8],buffptr+2,datalen); // Data goes to +7, but we have an extra byte
datalen+=8;
PutLengthinBuffer(buff, datalen);
// buff[5]=(datalen & 0xff);
// buff[6]=(datalen >> 8);
ReleaseBuffer(buffptr);
return (1);
}
}
return 0;
case 2: // send
buffptr = GetBuff();
if (buffptr == 0) return (0); // No buffers, so ignore
// Find TNC Record
Stream = buff[4];
if (!TNC->TNCOK)
{
// Send Error Response
buffptr[1] = 36;
memcpy(buffptr+2, "No Connection to PACTOR TNC\r", 36);
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
return 0;
}
txlen = GetLengthfromBuffer(buff) - 8;
buffptr[1] = txlen;
memcpy(buffptr+2, &buff[8], txlen);
C_Q_ADD(&STREAM->BPQtoPACTOR_Q, buffptr);
STREAM->FramesQueued++;
return (0);
case 3: // CHECK IF OK TO SEND. Also used to check if TNC is responding
Stream = (int)buff;
if (STREAM->FramesQueued > 4)
return (1 | TNC->HostMode << 8);
return TNC->HostMode << 8 | STREAM->Disconnecting << 15; // OK, but lock attach if disconnecting
case 4: // reinit
return (0);
case 5: // Close
CloseCOMPort(TNCInfo[port]->hDevice);
return (0);
case 6: // Scan Control
return 0; // None Yet
}
return 0;
}
static int WebProc(struct TNCINFO * TNC, char * Buff, BOOL LOCAL)
{
int Len = sprintf(Buff, "<html><meta http-equiv=expires content=0><meta http-equiv=refresh content=15>"
"<head><title>HAL Status</title></head><body><h3>HAL Status</h3>");
Len += sprintf(&Buff[Len], "<table style=\"text-align: left; width: 480px; font-family: monospace; align=center \" border=1 cellpadding=2 cellspacing=2>");
Len += sprintf(&Buff[Len], "<tr><td width=90px>Comms State</td><td>%s</td></tr>", TNC->WEB_COMMSSTATE);
Len += sprintf(&Buff[Len], "<tr><td>TNC State</td><td>%s</td></tr>", TNC->WEB_TNCSTATE);
Len += sprintf(&Buff[Len], "<tr><td>Mode</td><td>%s</td></tr>", TNC->WEB_MODE);
Len += sprintf(&Buff[Len], "<tr><td>Status</td><td>%s</td></tr>", TNC->WEB_STATE);
Len += sprintf(&Buff[Len], "<tr><td>TX/RX State</td><td>%s</td></tr>", TNC->WEB_TXRX);
Len += sprintf(&Buff[Len], "<tr><td>Traffic</td><td>%s</td></tr>", TNC->WEB_TRAFFIC);
Len += sprintf(&Buff[Len], "<tr><td>LEDS</td><td>STBY CALL LINK ERROR TX RX</td></tr>");
Len += sprintf(&Buff[Len], "<tr><td> </td><td>%s</td></tr>", TNC->WEB_LEDS);
Len += sprintf(&Buff[Len], "</table>");
Len = DoScanLine(TNC, Buff, Len);
return Len;
}
UINT HALExtInit(EXTPORTDATA * PortEntry)
{
char msg[500];
struct TNCINFO * TNC;
int port;
char * ptr;
int len;
char Msg[80];
HWND x;
//
// Will be called once for each Pactor Port
// The COM port number is in IOBASE
//
sprintf(msg,"HAL Driver %s", PortEntry->PORTCONTROL.SerialPortName);
WritetoConsole(msg);
port=PortEntry->PORTCONTROL.PORTNUMBER;
ReadConfigFile(port, ProcessLine);
TNC = TNCInfo[port];
if (TNC == NULL)
{
// Not defined in Config file
sprintf(msg," ** Error - no info in BPQ32.cfg for this port");
WritetoConsole(msg);
return (int)ExtProc;
}
TNC->Port = port;
TNC->Hardware = H_HAL;
TNC->Interlock = PortEntry->PORTCONTROL.PORTINTERLOCK;
PortEntry->MAXHOSTMODESESSIONS = 1; // Default
TNC->PortRecord = PortEntry;
if (PortEntry->PORTCONTROL.PORTCALL[0] == 0)
{
memcpy(TNC->NodeCall, MYNODECALL, 10);
}
else
{
ConvFromAX25(&PortEntry->PORTCONTROL.PORTCALL[0], TNC->NodeCall);
}
PortEntry->PORTCONTROL.PROTOCOL = 10;
PortEntry->PORTCONTROL.PORTQUALITY = 0;
if (PortEntry->PORTCONTROL.PORTPACLEN == 0)
PortEntry->PORTCONTROL.PORTPACLEN = 100;
ptr=strchr(TNC->NodeCall, ' ');
if (ptr) *(ptr) = 0; // Null Terminate
if (TNC->DefaultMode)
TNC->CurrentMode = TNC->DefaultMode;
else
TNC->CurrentMode = Clover;
TNC->PollDelay = 999999999;
// Set Disable +?, ExpandedStatus , Channel Stats Off, ClearOnDisc, EAS and MYCALL
len = sprintf(Msg, "%c%c%c%c%c%c%s", 0xcc, 0x56, 0x41, ClearOnDisc, SetEAS, SetMYCALL, TNC->NodeCall);
len++; // We include the NULL
memcpy(&TNC->InitScript[TNC->InitScriptLen], Msg, len);
TNC->InitScriptLen += len;
TNC->WebWindowProc = WebProc;
TNC->WebWinX = 510;
TNC->WebWinY = 280;
TNC->WEB_COMMSSTATE = zalloc(100);
TNC->WEB_TNCSTATE = zalloc(100);
strcpy(TNC->WEB_TNCSTATE, "Free");
TNC->WEB_MODE = zalloc(100);
TNC->WEB_TRAFFIC = zalloc(100);
TNC->WEB_BUFFERS = zalloc(100);
TNC->WEB_STATE = zalloc(100);
TNC->WEB_TXRX = zalloc(100);
TNC->WEB_LEDS = zalloc(100);
strcpy(TNC->WEB_LEDS, " X X X X X X");
#ifndef LINBPQ
CreatePactorWindow(TNC, ClassName, WindowTitle, RigControlRow, PacWndProc, 500, 233);
x = CreateWindowEx(0, "STATIC", "Comms State", WS_CHILD | WS_VISIBLE, 10,6,120,20, TNC->hDlg, NULL, hInstance, NULL);
x = TNC->xIDC_COMMSSTATE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,6,386,20, TNC->hDlg, NULL, hInstance, NULL);
x = CreateWindowEx(0, "STATIC", "TNC State", WS_CHILD | WS_VISIBLE, 10,28,106,20, TNC->hDlg, NULL, hInstance, NULL);
x = TNC->xIDC_TNCSTATE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,28,520,20, TNC->hDlg, NULL, hInstance, NULL);
x = CreateWindowEx(0, "STATIC", "Mode", WS_CHILD | WS_VISIBLE, 10,50,80,20, TNC->hDlg, NULL, hInstance, NULL);
x = TNC->xIDC_MODE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,50,200,20, TNC->hDlg, NULL, hInstance, NULL);
x = CreateWindowEx(0, "STATIC", "Status", WS_CHILD | WS_VISIBLE, 10,72,110,20, TNC->hDlg, NULL, hInstance, NULL);
x = TNC->xIDC_STATE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,72,144,20, TNC->hDlg, NULL, hInstance, NULL);
x = CreateWindowEx(0, "STATIC", "TX/RX State", WS_CHILD | WS_VISIBLE,10,94,80,20, TNC->hDlg, NULL, hInstance, NULL);
x = TNC->xIDC_TXRX = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,116,94,374,20 , TNC->hDlg, NULL, hInstance, NULL);
x = CreateWindowEx(0, "STATIC", "Traffic", WS_CHILD | WS_VISIBLE,10,116,80,20, TNC->hDlg, NULL, hInstance, NULL);
x = TNC->xIDC_TRAFFIC = CreateWindowEx(0, "STATIC", "RX 0 TX 0 ACKED 0", WS_CHILD | WS_VISIBLE,116,116,374,20 , TNC->hDlg, NULL, hInstance, NULL);
x = CreateWindowEx(0, "STATIC", "LEDS", WS_CHILD | WS_VISIBLE,10,138,60,20, TNC->hDlg, NULL, hInstance, NULL);
SendMessage(x, WM_SETFONT, (WPARAM)hFont, 0);
x = CreateWindowEx(0, "STATIC", "STBY CALL LINK ERROR TX RX", WS_CHILD | WS_VISIBLE,116,138,280,20, TNC->hDlg, NULL, hInstance, NULL);
SendMessage(x, WM_SETFONT, (WPARAM)hFont, 0);
x = TNC->xIDC_LEDS = CreateWindowEx(0, "STATIC", " X X X X X X", WS_CHILD | WS_VISIBLE,116,158,280,20 , TNC->hDlg, NULL, hInstance, NULL);
SendMessage(x, WM_SETFONT, (WPARAM)hFont, 0);
TNC->ClientHeight = 233;
TNC->ClientWidth = 500;
MoveWindows(TNC);
#endif
OpenCOMMPort(TNC, PortEntry->PORTCONTROL.SerialPortName, PortEntry->PORTCONTROL.BAUDRATE, FALSE);
SendCmd(TNC, "\x09" , 1); // Reset
WritetoConsole("\n");
return ((int)ExtProc);
}
static VOID KISSCLOSE(int Port)
{
struct TNCINFO * conn = TNCInfo[Port];
// drop DTR and RTS
COMClearDTR(conn->hDevice);
COMClearRTS(conn->hDevice);
// purge any outstanding reads/writes and close device handle
CloseCOMPort(conn->hDevice);
return;
}
static void CheckRX(struct TNCINFO * TNC)
{
int Length, Len;
UCHAR * Xptr;
// only try to read number of bytes in queue
if (TNC->RXLen == 500)
TNC->RXLen = 0;
Len = ReadCOMBlock(TNC->hDevice, &TNC->RXBuffer[TNC->RXLen], 500 - TNC->RXLen);
if (Len == 0)
return;
TNC->RXLen += Len;
Length = TNC->RXLen;
// We need to konw whether data is received or echoed, so we can't split commands and data here.
// Pass everything to the Command Handler. It will check that there are enough bytes for the command,
// and wait for more if not.
// The USB version also uses 0x91 0x31 to eacape 0x11, 0x91 0x33 for 0x13 and 0x91 0xB1 for 0x91
// If USB version, we might get unescaped xon and xoff, which we must ignore
if (TNC->XONXOFF)
{
Xptr = memchr(&TNC->RXBuffer, 0x11, Length);
while(Xptr)
{
Debugprintf("XON Port %d", TNC->Port);
memmove(Xptr, Xptr + 1, Length-- - (TNC->RXBuffer - Xptr));
Xptr = memchr(&TNC->RXBuffer, 0x11, Length);
}
Xptr = memchr(&TNC->RXBuffer, 0x13, Length);
while(Xptr)
{
Debugprintf("XOFF Port %d", TNC->Port);
memmove(Xptr, Xptr + 1, Length-- - (TNC->RXBuffer - Xptr));
Xptr = memchr(&TNC->RXBuffer, 0x13, Length);
}
Xptr = memchr(&TNC->RXBuffer, 0x91, Length); // See if packet contains 0x91 escape
if (Xptr)
// Make sure we have the escaped char as well
if ((Xptr - &TNC->RXBuffer[0]) == Length - 1) // x91 is last char
return;
}
ProcessHALBuffer(TNC, Length);
TNC->RXLen = 0;
return;
}
static BOOL WriteCommBlock(struct TNCINFO * TNC)
{
WriteCOMBlock(TNC->hDevice, TNC->TXBuffer, TNC->TXLen);
return TRUE;
}
VOID HALPoll(int Port)
{
struct TNCINFO * TNC = TNCInfo[Port];
struct STREAMINFO * STREAM = &TNC->Streams[0];
UCHAR * Poll = TNC->TXBuffer;
char Status[80];
UCHAR TXMsg[1000];
int datalen;
if (TNC->Timeout)
{
TNC->Timeout--;
if (TNC->Timeout) // Still waiting
return;
// Timed Out
TNC->TNCOK = FALSE;
TNC->HostMode = 0;
sprintf(TNC->WEB_COMMSSTATE,"%s Open but TNC not responding", TNC->PortRecord->PORTCONTROL.SerialPortName);
SetWindowText(TNC->xIDC_COMMSSTATE, TNC->WEB_COMMSSTATE);
//for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (TNC->PortRecord->ATTACHEDSESSIONS[0]) // Connected
{
STREAM->Connected = FALSE; // Back to Command Mode
STREAM->ReportDISC = TRUE; // Tell Node
}
}
}
// if we have just restarted or TNC appears to be in terminal mode, run Initialisation Sequence
if (TNC->TNCOK)
if (!TNC->HostMode)
{
DoTNCReinit(TNC);
return;
}
if (TNC->PortRecord->ATTACHEDSESSIONS[0] && STREAM->Attached == 0)
{
// New Attach
int calllen;
char Msg[80];
STREAM->Attached = TRUE;
STREAM->BytesRXed = STREAM->BytesTXed = STREAM->BytesAcked = 0;
calllen = ConvFromAX25(TNC->PortRecord->ATTACHEDSESSIONS[0]->L4USER, STREAM->MyCall);
STREAM->MyCall[calllen] = 0;
datalen = sprintf(TXMsg, "%c%s", SetMYCALL, STREAM->MyCall);
SendCmd(TNC, TXMsg, datalen + 1); // Send the NULL
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", STREAM->MyCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
// Stop Scanning
sprintf(Msg, "%d SCANSTOP", TNC->Port);
Rig_Command(-1, Msg);
SendCmd(TNC, "\x42", 1); // Connect Enable off
return;
}
//for (Stream = 0; Stream <= MaxStreams; Stream++)
if (STREAM->Attached)
CheckForDetach(TNC, 0, STREAM, TidyClose, ForcedClose, CloseComplete);
if (TNC->NeedPACTOR)
{
TNC->NeedPACTOR--;
if (TNC->NeedPACTOR == 0)
{
int datalen;
UCHAR TXMsg[80];
datalen = sprintf(TXMsg, "%c%s", SetMYCALL, TNC->NodeCall);
SendCmd(TNC, TXMsg, datalen + 1); // Send the NULL
// Set Listen Mode
switch (TNC->CurrentMode)
{
case Pactor:
SendCmd(TNC, "\x84", 1); // FSK
SendCmd(TNC, "\x83", 1); // Select P-MODE Standby
SendCmd(TNC, "\x58", 1); // Listen
break;
case Clover:
SendCmd(TNC, "\x80", 1); // Clover
SendCmd(TNC, "\x54", 1); // Enable adaptive Clover format
SendCmd(TNC, "\x41", 1); // No Statistics
SendCmd(TNC, "\x60\x09", 2); // Robust Retries
SendCmd(TNC, "\x61\x09", 2); // Normal Retries
break;
}
SendCmd(TNC, "\x52", 1); // ConnectEnable
// Restart Scanning
sprintf(Status, "%d SCANSTART 15", TNC->Port);
Rig_Command(-1, Status);
return;
}
}
#define MAXHALTX 256
//for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (TNC->TNCOK && STREAM->BPQtoPACTOR_Q && (STREAM->BytesTXed - STREAM->BytesAcked < 600))
{
int datalen;
UINT * buffptr;
UCHAR * MsgPtr;
unsigned char TXMsg[500];
buffptr = (UINT * )STREAM->BPQtoPACTOR_Q;
datalen=buffptr[1];
MsgPtr = (UCHAR *)&buffptr[2];
if (STREAM->Connected)
{
if (TNC->SwallowSignon)
{
TNC->SwallowSignon = FALSE;
if (strstr(MsgPtr, "Connected")) // Discard *** connected
{
ReleaseBuffer(buffptr);
STREAM->FramesQueued--;
return;
}
}
// Must send data in small chunks - the Hal has limited buffer space
// If in IRS force a turnround
if (TNC->TXRXState == 'R' && TNC->CurrentMode != Clover)
{
if (TNC->TimeInRX++ > 15)
SendCmd(TNC, "\x87", 1); // Changeover to ISS
else
goto Poll;
}
TNC->TimeInRX = 0;
EncodeAndSend(TNC, MsgPtr, datalen);
buffptr=Q_REM(&STREAM->BPQtoPACTOR_Q);
ReleaseBuffer(buffptr);
WriteLogLine(2, MsgPtr, datalen);
STREAM->BytesTXed += datalen;
STREAM->FramesQueued--;
ShowTraffic(TNC);
return;
}
else
{
buffptr=Q_REM(&STREAM->BPQtoPACTOR_Q);
STREAM->FramesQueued--;
// Command. Do some sanity checking and look for things to process locally
datalen--; // Exclude CR
MsgPtr[datalen] = 0; // Null Terminate
_strupr(MsgPtr);
if (memcmp(MsgPtr, "RADIO ", 6) == 0)
{
sprintf(&MsgPtr[40], "%d %s", TNC->Port, &MsgPtr[6]);
if (Rig_Command(TNC->PortRecord->ATTACHEDSESSIONS[0]->L4CROSSLINK->CIRCUITINDEX, &MsgPtr[40]))
{
ReleaseBuffer(buffptr);
}
else
{
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "%s", &MsgPtr[40]);
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
}
return;
}
if (memcmp(MsgPtr, "MODE CLOVER", 11) == 0)
{
TNC->CurrentMode = Clover;
buffptr[1] = sprintf((UCHAR *)&buffptr[2],"HAL} Ok\r");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
SetWindowText(TNC->xIDC_MODE, "Clover");
strcpy(TNC->WEB_MODE, "Clover");
SendCmd(TNC, "\x80", 1); // Clover
SendCmd(TNC, "\x54", 1); // Enable adaptive Clover format
SendCmd(TNC, "\x41", 1); // No Statistics
return;
}
if (memcmp(MsgPtr, "MODE PACTOR", 11) == 0)
{
TNC->CurrentMode = Pactor;
buffptr[1] = sprintf((UCHAR *)&buffptr[2],"HAL} Ok\r");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
SendCmd(TNC, "\x84", 1); // FSK
SendCmd(TNC, "\x83", 1); // Select P-MODE Standby
SendCmd(TNC, "\x48", 1); // Listen Off
return;
}
if (memcmp(MsgPtr, "MODE AMTOR", 11) == 0)
{
TNC->CurrentMode = AMTOR;
buffptr[1] = sprintf((UCHAR *)&buffptr[2],"HAL} Ok\r");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
return;
}
if (MsgPtr[0] == 'C' && MsgPtr[1] == ' ' && datalen > 2) // Connect
{
memcpy(STREAM->RemoteCall, &MsgPtr[2], 9);
switch (TNC->CurrentMode)
{
case Pactor:
SendCmd(TNC, "\x84", 1); // FSK
SendCmd(TNC, "\x83", 1); // Select P-MODE Standby
datalen = sprintf(TXMsg, "\x19%s", STREAM->RemoteCall);
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s - PACTOR", STREAM->MyCall, STREAM->RemoteCall);
// DOnt set connecting till we get the 19 response so we can trap listen as a fail
break;
case Clover:
SendCmd(TNC, "\x54", 1); // Enable adaptive Clover format
SendCmd(TNC, "\x57", 1); // Enable TX buffer clear on disconnect
datalen = sprintf(TXMsg, "\x11%s", STREAM->RemoteCall);
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s - CLOVER", STREAM->MyCall, STREAM->RemoteCall);
break;
}
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
SendCmd(TNC, TXMsg, datalen + 1); // Include NULL
ReleaseBuffer(buffptr);
return;
}
if (memcmp(MsgPtr, "CLOVER ", 7) == 0)
{
memcpy(STREAM->RemoteCall, &MsgPtr[2], 9);
SendCmd(TNC, "\x54", 1); // Enable adaptive Clover format
SendCmd(TNC, "\x57", 1); // Enable TX buffer clear on disconnect
datalen = sprintf(TXMsg, "\x11%s", STREAM->RemoteCall);
SendCmd(TNC, TXMsg, datalen + 1); // Include NULL
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s - CLOVER",
STREAM->MyCall, STREAM->RemoteCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
ReleaseBuffer(buffptr);
return;
}