forked from g8bpq/OldLinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dragon.c
2257 lines (1642 loc) · 51.3 KB
/
Dragon.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
*/
//
// P4Dragon support Module. Extracted from SCSPactor
//#ifdef WIN32
//#define WRITELOG
//#endif
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#define MaxStreams 10 // First is used for Pactor, even though Pactor uses channel 31
#include "CHeaders.h"
#include "tncinfo.h"
#include "bpq32.h"
#ifndef WIN32
#ifndef MACBPQ
#include <sys/ioctl.h>
#include <linux/serial.h>
#endif
#endif
static char ClassName[]="DRAGONSTATUS";
static char WindowTitle[] = "P4Dragon";
static int RigControlRow = 185;
#define NARROWMODE 12 // PI/II
#define WIDEMODE 16 // PIII only
extern UCHAR BPQDirectory[];
extern char * PortConfig[33];
extern BOOL RIG_DEBUG;
static RECT Rect;
struct TNCINFO * TNCInfo[34]; // Records are Malloc'd
extern char * RigConfigMsg[35];
VOID __cdecl Debugprintf(const char * format, ...);
char NodeCall[11]; // Nodecall, Null Terminated
unsigned long _beginthread( void( *start_address )(), unsigned stack_size, int arglist);
int DoScanLine(struct TNCINFO * TNC, char * Buff, int Len);
VOID SuspendOtherPorts(struct TNCINFO * ThisTNC);
VOID ReleaseOtherPorts(struct TNCINFO * ThisTNC);
VOID DragonSuspentPort(struct TNCINFO * TNC);
VOID DragonReleasePort(struct TNCINFO * TNC);
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];
BPQport = Port;
TNC = TNCInfo[BPQport] = malloc(sizeof(struct TNCINFO));
memset(TNC, 0, sizeof(struct TNCINFO));
TNC->InitScript = malloc(1000);
TNC->InitScript[0] = 0;
TNC->Dragon = TRUE;
goto ConfigLine;
// 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, "APPL", 4) == 0)
{
p_cmd = strtok(&buf[5], " \t\n\r");
if (p_cmd && p_cmd[0] != ';' && p_cmd[0] != '#')
TNC->ApplCmd=_strdup(_strupr(p_cmd));
}
else
if (_memicmp(buf, "PACKETCHANNELS", 14) == 0) // Packet Channels
TNC->PacketChannels = atoi(&buf[14]);
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, "USEAPPLCALLS", 12) == 0 && buf[12] != 'F' && buf[12] != 'f')
TNC->UseAPPLCalls = TRUE;
else
if (_memicmp(buf, "USEAPPLCALLSFORPACTOR", 21) == 0)
TNC->UseAPPLCallsforPactor = TRUE;
else
if (_memicmp(buf, "DRAGON", 6) == 0)
{
if (_memicmp(&buf[7], "SINGLE", 6) == 0)
TNC->DragonSingle = TRUE;
}
else
if (_memicmp(buf, "FORCE ROBUST", 12) == 0)
TNC->ForceRobust = TNC->RobustDefault = TRUE;
else
if (_memicmp(buf, "MAXLEVEL", 8) == 0) // Maximum Pactor Level to use.
TNC->MaxLevel = atoi(&buf[8]);
else
if (_memicmp(buf, "WL2KREPORT", 10) == 0)
TNC->WL2K = DecodeWL2KReportLine(buf);
else
strcat (TNC->InitScript, buf);
}
return (TRUE);
}
static BOOL WriteCommBlock(struct TNCINFO * TNC)
{
WriteCOMBlock(TNC->hDevice, TNC->TXBuffer, TNC->TXLen);
TNC->Timeout = 20; // 2 secs
return TRUE;
}
struct TNCINFO * CreateTTYInfo(int port, int speed);
BOOL OpenConnection(int);
BOOL SetupConnection(int);
BOOL CloseConnection(struct TNCINFO * conn);static BOOL WriteCommBlock(struct TNCINFO * TNC);
BOOL DestroyTTYInfo(int port);
void SCSCheckRX(struct TNCINFO * TNC);
VOID SCSPoll(int Port);
VOID CRCStuffAndSend(struct TNCINFO * TNC, UCHAR * Msg, int Len);
unsigned short int compute_crc(unsigned char *buf,int len);
int Unstuff(UCHAR * MsgIn, UCHAR * MsgOut, int len);
VOID ProcessDEDFrame(struct TNCINFO * TNC, UCHAR * rxbuff, int len);
VOID ProcessTermModeResponse(struct TNCINFO * TNC);
VOID ExitHost(struct TNCINFO * TNC);
VOID DoTNCReinit(struct TNCINFO * TNC);
VOID DoTermModeTimeout(struct TNCINFO * TNC);
static VOID DoMonitor(struct TNCINFO * TNC, UCHAR * Msg, int Len);
int Switchmode(struct TNCINFO * TNC, int Mode);
VOID SwitchToPacketOnly(struct TNCINFO * TNC);
char status[8][8];
char ModeText[8][14];
char PactorLevelText[5][14];
char PleveltoMode[5];
#ifdef WRITELOG
static HANDLE LogHandle[32] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
#endif
//char * Logs[4] = {"1", "2", "3", "4"};
static char BaseDir[MAX_PATH]="c:\\";
static VOID CloseLogFile(int Flags)
{
#ifdef WRITELOG
CloseHandle(LogHandle[Flags]);
LogHandle[Flags] = INVALID_HANDLE_VALUE;
#endif
}
static BOOL OpenLogFile(int Flags)
{
#ifdef WRITELOG
UCHAR FN[MAX_PATH];
time_t T;
struct tm * tm;
T = time(NULL);
tm = gmtime(&T);
sprintf(FN,"%s\\SCSLog_%02d%02d_%d.txt", BPQDirectory, tm->tm_mon + 1, tm->tm_mday, 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);
#endif
return 0;
}
static void WriteLogLine(int Flags, char * Msg, int MsgLen)
{
#ifdef WRITELOG
int cnt;
WriteFile(LogHandle[Flags], Msg , MsgLen, &cnt, NULL);
WriteFile(LogHandle[Flags], "\r\n" , 2, &cnt, NULL);
#endif
}
static int ExtProc(int fn, int port,unsigned char * buff)
{
int txlen = 0;
UINT * buffptr;
struct TNCINFO * TNC = TNCInfo[port];
int Param;
int Stream = 0;
struct STREAMINFO * STREAM;
char PLevel;
struct ScanEntry * Scan;
if (TNC == NULL)
return 0;
if (TNC->hDevice == 0)
{
// Clear anything from UI_Q
while (TNC->PortRecord->UI_Q)
{
buffptr = Q_REM(&TNC->PortRecord->UI_Q);
ReleaseBuffer(buffptr);
}
// Try to reopen every 30 secs
if (fn > 3 && fn < 7)
goto ok;
TNC->ReopenTimer++;
if (TNC->ReopenTimer < 300)
return 0;
TNC->ReopenTimer = 0;
OpenCOMMPort(TNC, TNC->PortRecord->PORTCONTROL.SerialPortName, TNC->PortRecord->PORTCONTROL.BAUDRATE, TRUE);
if (TNC->hDevice == 0)
return 0;
#ifndef WIN32
#ifndef MACBPQ
if (TNC->Dragon)
{
struct serial_struct sstruct;
// Need to set custom baud rate
if (ioctl(TNC->hDevice, TIOCGSERIAL, &sstruct) < 0)
{
Debugprintf("Error: Dragon could not get comm ioctl\n");
}
else
{
// set custom divisor to get 829440 baud
sstruct.custom_divisor = 29;
sstruct.flags |= ASYNC_SPD_CUST;
// set serial_struct
if (ioctl(TNC->hDevice, TIOCSSERIAL, &sstruct) < 0)
Debugprintf("Error: Dragon could not set custom comm baud divisor\n");
else
Debugprintf("Dragon custom baud rate set\n");
}
}
#endif
#endif
}
ok:
switch (fn)
{
case 7:
// 100 mS Timer. May now be needed, as Poll can be called more frequently in some circumstances
SCSCheckRX(TNC);
SCSPoll(port);
return 0;
case 1: // poll
for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (TNC->Streams[Stream].ReportDISC)
{
TNC->Streams[Stream].ReportDISC = FALSE;
buff[4] = Stream;
return -1;
}
}
if (TNC->EnterExit)
return 0; // Switching to Term mode to change bandwidth
for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (TNC->Streams[Stream].PACTORtoBPQ_Q !=0)
{
int datalen;
buffptr=Q_REM(&TNC->Streams[Stream].PACTORtoBPQ_Q);
datalen=buffptr[1];
buff[4] = Stream;
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
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(&TNC->Streams[Stream].PACTORtoBPQ_Q, buffptr);
return 0;
}
txlen = GetLengthfromBuffer(buff) - 8;
buffptr[1] = txlen;
memcpy(buffptr+2, &buff[8], txlen);
C_Q_ADD(&TNC->Streams[Stream].BPQtoPACTOR_Q, buffptr);
TNC->Streams[Stream].FramesOutstanding++;
return (0);
case 3: // CHECK IF OK TO SEND. Also used to check if TNC is responding
Stream = (int)buff;
STREAM = &TNC->Streams[Stream];
if (Stream == 0)
{
if (STREAM->FramesOutstanding > 4)
return (1 | TNC->HostMode << 8 | STREAM->Disconnecting << 15);
}
else
{
if (STREAM->FramesOutstanding > 3 || TNC->Buffers < 200)
return (1 | TNC->HostMode << 8 | STREAM->Disconnecting << 15); }
return TNC->HostMode << 8 | STREAM->Disconnecting << 15; // OK, but lock attach if disconnecting
case 4: // reinit
// Ensure in Pactor
TNC->TXBuffer[2] = 31;
TNC->TXBuffer[3] = 0x1;
TNC->TXBuffer[4] = 0x1;
memcpy(&TNC->TXBuffer[5], "PT", 2);
CRCStuffAndSend(TNC, TNC->TXBuffer, 7);
Sleep(25);
ExitHost(TNC);
Sleep(50);
CloseCOMPort(TNC->hDevice);
TNC->hDevice =(HANDLE) -1;
TNC->ReopenTimer = 250;
TNC->HostMode = FALSE;
return (0);
case 5: // Close
// Ensure in Pactor
TNC->TXBuffer[2] = 31;
TNC->TXBuffer[3] = 0x1;
TNC->TXBuffer[4] = 0x1;
memcpy(&TNC->TXBuffer[5], "PT", 2);
CRCStuffAndSend(TNC, TNC->TXBuffer, 7);
Sleep(25);
ExitHost(TNC);
Sleep(25);
CloseCOMPort(TNCInfo[port]->hDevice);
return (0);
case 6: // Scan Interface
Param = (int)buff;
switch (Param)
{
case 1: // Request Permission
if (TNC->TNCOK)
{
// If been in Sync a long time, or if using applcalls and
// Scan had been locked too long just let it change
if (TNC->UseAPPLCallsforPactor)
{
if (TNC->PTCStatus == 6) // Sync
{
int insync = time(NULL) - TNC->TimeEnteredSYNCMode;
if (insync > 4)
{
Debugprintf("SCS Scan - in SYNC for %d Secs - allow change regardless", insync);
return 0;
}
}
else if (TNC->TimeScanLocked)
{
int timeLocked = time(NULL) - TNC->TimeScanLocked;
if (timeLocked > 4)
{
Debugprintf("SCS Scan - Scan Locked for %d Secs - allow change regardless", timeLocked);
TNC->TimeScanLocked = 0;
return 0;
}
}
}
TNC->WantToChangeFreq = TRUE;
TNC->OKToChangeFreq = FALSE;
return TRUE;
}
return 0; // Don't lock scan if TNC isn't responding
case 2: // Check Permission
return TNC->OKToChangeFreq;
case 3: // Release Permission
TNC->WantToChangeFreq = FALSE;
if (TNC->DontReleasePermission) // Disable connects during this interval?
{
TNC->DontReleasePermission = FALSE;
if (TNC->SyncSupported == FALSE)
TNC->TimeScanLocked = time(NULL) + 100; // Make sure doesnt time out
return 0;
}
TNC->DontWantToChangeFreq = TRUE;
return 0;
default: // Param is Address of a struct ScanEntry
Scan = (struct ScanEntry *)buff;
PLevel = Scan->PMaxLevel;
if (PLevel == 0 && (Scan->HFPacketMode || Scan->RPacketMode))
{
// Switch to Packet for this Interval
if (RIG_DEBUG)
Debugprintf("Dragon Switching to Packet, %d", TNC->HFPacket);
if (TNC->HFPacket == FALSE)
SwitchToPacketOnly(TNC);
return 0;
}
if (PLevel > '0' && PLevel < '5') // 1 - 4
{
if (TNC->Bandwidth != PLevel)
{
TNC->Bandwidth = PLevel;
TNC->MinLevel = Scan->PMinLevel - '0';
Switchmode(TNC, PLevel - '0');
}
if (TNC->UseAPPLCallsforPactor && Scan->APPLCALL[0])
{
// Switch callsign
STREAM = &TNC->Streams[0];
STREAM->CmdSet = STREAM->CmdSave = malloc(100);
strcpy(STREAM->MyCall, Scan->APPLCALL);
sprintf(STREAM->CmdSet, "I%s\rI\r", STREAM->MyCall);
if (RIG_DEBUG)
Debugprintf("SCS Pactor APPLCALL Set to %s", STREAM->MyCall);
}
else
{
if (TNC->HFPacket)
SwitchToPactor(TNC);
}
}
if (Scan->RPacketMode)
if (TNC->RobustTime)
SwitchToPacket(TNC); // Always start in packet, switch to pactor after RobustTime ticks
if (PLevel == '0')
TNC->DontReleasePermission = TRUE; // Dont allow connects in this interval
else
TNC->DontReleasePermission = FALSE;
return 0;
}
}
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>SCS Pactor Status</title></head><body><h3>SCS Pactor 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>Buffers</td><td>%s</td></tr>", TNC->WEB_BUFFERS);
Len += sprintf(&Buff[Len], "<tr><td>Traffic</td><td>%s</td></tr>", TNC->WEB_TRAFFIC);
Len += sprintf(&Buff[Len], "<tr><td>Mode</td><td>%s</td></tr>", TNC->WEB_PACTORLEVEL);
Len += sprintf(&Buff[Len], "</table>");
Len = DoScanLine(TNC, Buff, Len);
return Len;
}
UINT DragonExtInit(EXTPORTDATA * PortEntry)
{
char msg[500];
struct TNCINFO * TNC;
int port;
char * ptr;
int Stream = 0;
char * TempScript;
//
// Will be called once for each Pactor Port
// The COM port number is in IOBASE
//
sprintf(msg,"Dragon %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\n");
WritetoConsole(msg);
return (int) ExtProc;
}
TNC->Port = port;
TNC->Hardware = H_SCS;
if (TNC->BusyHold == 0)
TNC->BusyHold = 3;
if (TNC->BusyWait == 0)
TNC->BusyWait = 10;
if (TNC->MaxLevel == 0)
TNC->MaxLevel = 3;
// Set up DED addresses for streams (first stream (Pactor) = DED 31
TNC->Streams[0].DEDStream = 31;
for (Stream = 1; Stream <= MaxStreams; Stream++)
{
TNC->Streams[Stream].DEDStream = Stream;
}
if (TNC->PacketChannels > MaxStreams)
TNC->PacketChannels = MaxStreams;
PortEntry->MAXHOSTMODESESSIONS = TNC->PacketChannels + 1;
PortEntry->PERMITGATEWAY = TRUE; // Can change ax.25 call on each stream
PortEntry->SCANCAPABILITIES = CONLOCK; // Scan Control 3 stage/conlock
TNC->PortRecord = PortEntry;
TNC->Interlock = PortEntry->PORTCONTROL.PORTINTERLOCK;
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;
TNC->SuspendPortProc = DragonSuspentPort;
TNC->ReleasePortProc = DragonReleasePort;
PortEntry->PORTCONTROL.UICAPABLE = TRUE;
ptr=strchr(TNC->NodeCall, ' ');
if (ptr) *(ptr) = 0; // Null Terminate
// get NODECALL for RP tests
memcpy(NodeCall, MYNODECALL, 10);
ptr=strchr(NodeCall, ' ');
if (ptr) *(ptr) = 0; // Null Terminate
// Set TONES to 4
TempScript = malloc(1000);
strcpy(TempScript, "QUIT\r"); // In case in pac: mode
strcat(TempScript, "TONES 4\r"); // Tones may be changed but I want this as standard
strcat(TempScript, "MAXERR 30\r"); // Max retries
strcat(TempScript, "MODE 0\r"); // ASCII mode, no PTC II compression (Forwarding will use FBB Compression)
strcat(TempScript, "MAXSUM 20\r"); // Max count for memory ARQ
strcat(TempScript, "CWID 0 2\r"); // CW ID disabled
strcat(TempScript, "PTCC 0\r"); // Dragon out of PTC Compatibility Mode
strcat(TempScript, "VER\r"); // Try to determine Controller Type
sprintf(msg, "MYLEVEL %d\r", TNC->MaxLevel);
strcat(TempScript, msg); // Default Level to MAXLEVEL
strcat(TempScript, TNC->InitScript);
free(TNC->InitScript);
TNC->InitScript = TempScript;
// Others go on end so they can't be overriden
strcat(TNC->InitScript, "ADDLF 0\r"); // Auto Line Feed disabled
strcat(TNC->InitScript, "ARX 0\r"); // Amtor Phasing disabled
strcat(TNC->InitScript, "BELL 0\r"); // Disable Bell
strcat(TNC->InitScript, "BC 0\r"); // FEC reception is disabled
strcat(TNC->InitScript, "BKCHR 2\r"); // Breakin Char = 2
strcat(TNC->InitScript, "CHOBELL 0\r"); // Changeover Bell off
strcat(TNC->InitScript, "CMSG 0\r"); // Connect Message Off
strcat(TNC->InitScript, "LFIGNORE 0\r"); // No insertion of Line feed
strcat(TNC->InitScript, "LISTEN 0\r"); // Pactor Listen disabled
strcat(TNC->InitScript, "MAIL 0\r"); // Disable internal mailbox reporting
strcat(TNC->InitScript, "REMOTE 0\r"); // Disable remote control
strcat(TNC->InitScript, "PAC CBELL 0\r"); //
strcat(TNC->InitScript, "PAC CMSG 0\r"); //
strcat(TNC->InitScript, "PAC PRBOX 0\r"); // Turn off Packet Radio Mailbox
// Automatic Status must be enabled for BPQ32
// Pactor must use Host Mode Chanel 31
// PDuplex must be set. The Node code relies on automatic IRS/ISS changeover
// 5 second duplex timer
strcat(TNC->InitScript, "STATUS 2\rPTCHN 31\rPDUPLEX 1\rPDTIMER 5\r");
sprintf(msg, "MYCALL %s\rPAC MYCALL %s\r", TNC->NodeCall, TNC->NodeCall);
strcat(TNC->InitScript, msg);
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_PACTORLEVEL = zalloc(100);
#ifndef LINBPQ
CreatePactorWindow(TNC, ClassName, WindowTitle, RigControlRow, PacWndProc, 235, 500);
CreateWindowEx(0, "STATIC", "Comms State", WS_CHILD | WS_VISIBLE, 10,6,120,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_COMMSSTATE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,6,386,20, TNC->hDlg, NULL, hInstance, NULL);
CreateWindowEx(0, "STATIC", "TNC State", WS_CHILD | WS_VISIBLE, 10,28,106,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_TNCSTATE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,28,520,20, TNC->hDlg, NULL, hInstance, NULL);
CreateWindowEx(0, "STATIC", "Mode", WS_CHILD | WS_VISIBLE, 10,50,80,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_MODE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,50,200,20, TNC->hDlg, NULL, hInstance, NULL);
CreateWindowEx(0, "STATIC", "Status", WS_CHILD | WS_VISIBLE, 10,72,110,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_STATE = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE, 116,72,144,20, TNC->hDlg, NULL, hInstance, NULL);
CreateWindowEx(0, "STATIC", "TX/RX State", WS_CHILD | WS_VISIBLE,10,94,80,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_TXRX = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,116,94,374,20 , TNC->hDlg, NULL, hInstance, NULL);
CreateWindowEx(0, "STATIC", "Buffers", WS_CHILD | WS_VISIBLE,10,116,80,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_BUFFERS = CreateWindowEx(0, "STATIC", "", WS_CHILD | WS_VISIBLE,116,116,374,20 , TNC->hDlg, NULL, hInstance, NULL);
CreateWindowEx(0, "STATIC", "Traffic", WS_CHILD | WS_VISIBLE,10,138,80,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_TRAFFIC = CreateWindowEx(0, "STATIC", "RX 0 TX 0 ACKED 0", WS_CHILD | WS_VISIBLE,116,138,374,20 , TNC->hDlg, NULL, hInstance, NULL);
TNC->xIDC_PACTORLEVEL = CreateWindowEx(0, "STATIC", "Mode", WS_CHILD | WS_VISIBLE,10,160,430,20, TNC->hDlg, NULL, hInstance, NULL);
TNC->ClientHeight = 240;
TNC->ClientWidth = 500;
MoveWindows(TNC);
#endif
OpenCOMMPort(TNC, PortEntry->PORTCONTROL.SerialPortName, PortEntry->PORTCONTROL.BAUDRATE, FALSE);
#ifndef WIN32
#ifndef MACBPQ
if (TNC->Dragon)
{
struct serial_struct sstruct;
// Need to set custom baud rate
if (ioctl(TNC->hDevice, TIOCGSERIAL, &sstruct) < 0)
{
printf("Error: Dragon could not get comm ioctl\n");
}
else
{
// set custom divisor to get 829440 baud
sstruct.custom_divisor = 29;
sstruct.flags |= ASYNC_SPD_CUST;
// set serial_struct
if (ioctl(TNC->hDevice, TIOCSSERIAL, &sstruct) < 0)
Debugprintf("Error: Dragon could not set custom comm baud divisor\n");
else
Debugprintf("Dragon custom baud rate set\n");
}
}
#endif
#endif
if (TNC->RobustDefault)
SwitchToPacket(TNC);
WritetoConsole("\n");
return ((int)ExtProc);
}
VOID DragonPoll(int Port)
{
struct TNCINFO * TNC = TNCInfo[Port];
UCHAR * Poll = TNC->TXBuffer;
char Status[80];
int Stream = 0;
int nn;
struct STREAMINFO * STREAM;
if (TNC->MinLevelTimer)
{
TNC->MinLevelTimer--;
if (TNC->MinLevelTimer == 0)
{
// Failed to reach min level in 15 secs
STREAM = &TNC->Streams[0];
if (STREAM->Connected)
{
UINT * buffptr;
Debugprintf("Required Min Level not reached - disconnecting");
// Discard Queued Data, Send a Message, then a disconnect
while (STREAM->BPQtoPACTOR_Q)
ReleaseBuffer(Q_REM(&STREAM->BPQtoPACTOR_Q));
STREAM->NeedDisc = 15; // 1 secs
buffptr = GetBuff();
if (buffptr == 0) return; // No buffers, so ignore
buffptr[1] = sprintf((char *)&buffptr[2],
"This port only allows Pactor Level %d or above - Disconnecting\r\n", TNC->MinLevel);
C_Q_ADD(&STREAM->BPQtoPACTOR_Q, buffptr);
}
}
}
if (TNC->SwitchToPactor)
{
TNC->SwitchToPactor--;
if (TNC->SwitchToPactor == 0)
SwitchToPactor(TNC);
}
for (Stream = 0; Stream <= MaxStreams; Stream++)
{
if (TNC->PortRecord->ATTACHEDSESSIONS[Stream] && TNC->Streams[Stream].Attached == 0)
{
// New Attach
// If Pactor, stop scanning and take out of listen mode.
// Set call to connecting user's call
// If Stream 0 Put in Pactor Mode so Busy Detect will work
int calllen=0;
TNC->Streams[Stream].Attached = TRUE;
calllen = ConvFromAX25(TNC->PortRecord->ATTACHEDSESSIONS[Stream]->L4USER, TNC->Streams[Stream].MyCall);
TNC->Streams[Stream].MyCall[calllen] = 0;
if (Stream == 0)
{
// Release Scan Lock if it is held
if (TNC->DontReleasePermission)
{
TNC->DontReleasePermission = FALSE;
TNC->DontWantToChangeFreq = TRUE;
}
TNC->Streams[Stream].CmdSet = TNC->Streams[Stream].CmdSave = malloc(100);
sprintf(TNC->Streams[Stream].CmdSet, "I%s\r", "SCSPTC");
Debugprintf("SCS Pactor CMDSet = %s", TNC->Streams[Stream].CmdSet);
SuspendOtherPorts(TNC); // Prevent connects on other ports in same scan gruop
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", TNC->Streams[0].MyCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
// Stop Scanner
sprintf(Status, "%d SCANSTOP", TNC->Port);
TNC->SwitchToPactor = 0; // Cancel any RP to Pactor switch
Rig_Command(-1, Status);
}
}
}
if (TNC->Timeout)
{
TNC->Timeout--;
if (TNC->Timeout) // Still waiting
return;
TNC->Retries--;
if(TNC->Retries)
{
WriteCommBlock(TNC); // Retransmit Block
return;
}
// Retried out.
if (TNC->HostMode == 0)
{
DoTermModeTimeout(TNC);
return;
}
// Retried out in host mode - Clear any connection and reinit the TNC
Debugprintf("PACTOR - Link to TNC Lost");
TNC->TNCOK = FALSE;
sprintf(TNC->WEB_COMMSSTATE,"%s Open but TNC not responding", TNC->PortRecord->PORTCONTROL.SerialPortName);
SetWindowText(TNC->xIDC_COMMSSTATE, TNC->WEB_COMMSSTATE);
// Clear anything from UI_Q
while (TNC->PortRecord->UI_Q)
{
UINT * buffptr = Q_REM(&TNC->PortRecord->UI_Q);
ReleaseBuffer(buffptr);
}
TNC->HostMode = 0;
TNC->ReinitState = 0;