forked from erikarn/LinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FLDigi.c
3714 lines (2692 loc) · 79.4 KB
/
FLDigi.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
*/
//
// FLARQ Emulator/FLDIGI Interface for BPQ32
//
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include "CHeaders.h"
int (WINAPI FAR *GetModuleFileNameExPtr)();
int (WINAPI FAR *EnumProcessesPtr)();
#include <stdio.h>
#include <time.h>
#include "tncinfo.h"
#include "bpq32.h"
#define VERSION_MAJOR 2
#define VERSION_MINOR 0
#define SD_RECEIVE 0x00
#define SD_SEND 0x01
#define SD_BOTH 0x02
#define DLE 0x10
#define SOH 1
#define STX 2
#define EOT 4
#define FEND 0xC0
#define FESC 0xDB
#define TFEND 0xDC
#define TFESC 0xDD
#define TIMESTAMP 352
#define CONTIMEOUT 1200
#define AGWHDDRLEN sizeof(struct AGWHEADER)
unsigned long _beginthread( void( *start_address )(), unsigned stack_size, int arglist);
extern int (WINAPI FAR *GetModuleFileNameExPtr)();
//int ResetExtDriver(int num);
extern char * PortConfig[33];
int SemHeldByAPI;
struct TNCINFO * TNCInfo[34]; // Records are Malloc'd
static void ConnecttoFLDigiThread(int port);
void CreateMHWindow();
int Update_MH_List(struct in_addr ipad, char * call, char proto);
static int ConnecttoFLDigi();
static int ProcessReceivedData(int bpqport);
static ProcessLine(char * buf, int Port);
int KillTNC(struct TNCINFO * TNC);
static int RestartTNC(struct TNCINFO * TNC);
VOID ProcessFLDigiPacket(struct TNCINFO * TNC, char * Message, int Len);
VOID ProcessFLDigiKISSPacket(struct TNCINFO * TNC, char * Message, int Len);
struct TNCINFO * GetSessionKey(char * key, struct TNCINFO * TNC);
VOID SendARQData(struct TNCINFO * TNC, UINT * Buffer);
static VOID DoMonitorHddr(struct TNCINFO * TNC, struct AGWHEADER * RXHeader, UCHAR * Msg);
VOID SendRPBeacon(struct TNCINFO * TNC);
VOID FLReleaseTNC(struct TNCINFO * TNC);
unsigned int CalcCRC(UCHAR * ptr, int Len);
VOID ARQTimer(struct TNCINFO * TNC);
VOID QueueAndSend(struct TNCINFO * TNC, struct ARQINFO * ARQ, SOCKET sock, char * Msg, int MsgLen);
VOID SaveAndSend(struct TNCINFO * TNC, struct ARQINFO * ARQ, SOCKET sock, char * Msg, int MsgLen);
VOID ProcessARQStatus(struct TNCINFO * TNC, struct ARQINFO * ARQ, char *Input);
VOID SendXMLPoll(struct TNCINFO * TNC);
static int ProcessXMLData(int port);
VOID CheckFLDigiData(struct TNCINFO * TNC);
VOID SendPacket(struct TNCINFO * TNC, UCHAR * Msg, int MsgLen);
int KissEncode(UCHAR * inbuff, UCHAR * outbuff, int len);
VOID SendXMLCommand(struct TNCINFO * TNC, char * Command, char * Value, char ParamType);
VOID FLSlowTimer(struct TNCINFO * TNC);
VOID SendKISSCommand(struct TNCINFO * TNC, char * Msg);
int DoScanLine(struct TNCINFO * TNC, char * Buff, int Len);
VOID SuspendOtherPorts(struct TNCINFO * ThisTNC);
VOID ReleaseOtherPorts(struct TNCINFO * ThisTNC);
char * strlop(char * buf, char delim);
extern UCHAR BPQDirectory[];
#define MAXBPQPORTS 32
#define MAXMPSKPORTS 16
//LOGFONT LFTTYFONT ;
//HFONT hFont ;
static int MPSKChannel[MAXBPQPORTS+1]; // BPQ Port to MPSK Port
static int BPQPort[MAXMPSKPORTS][MAXBPQPORTS+1]; // MPSK Port and Connection to BPQ Port
static int MPSKtoBPQ_Q[MAXBPQPORTS+1]; // Frames for BPQ, indexed by BPQ Port
static int BPQtoMPSK_Q[MAXBPQPORTS+1]; // Frames for MPSK. indexed by MPSK port. Only used it TCP session is blocked
// Each port may be on a different machine. We only open one connection to each MPSK instance
static char * MPSKSignon[MAXBPQPORTS+1]; // Pointer to message for secure signin
static unsigned int MPSKInst = 0;
static int AttachedProcesses=0;
static HWND hResWnd,hMHWnd;
static BOOL GotMsg;
static HANDLE STDOUT=0;
//SOCKET sock;
static SOCKADDR_IN sinx;
static SOCKADDR_IN rxaddr;
static SOCKADDR_IN destaddr[MAXBPQPORTS+1];
static int addrlen=sizeof(sinx);
//static short MPSKPort=0;
static time_t ltime,lasttime[MAXBPQPORTS+1];
static BOOL CONNECTING[MAXBPQPORTS+1];
static BOOL CONNECTED[MAXBPQPORTS+1];
//HANDLE hInstance;
static char WindowTitle[] = "FLDIGI";
static char ClassName[] = "FLDIGISTATUS";
static int RigControlRow = 165;
static fd_set readfs;
static fd_set writefs;
static fd_set errorfs;
static struct timeval timeout;
int Blocksizes[10] = {0,2,4,8,16,32,64,128,256,512};
static int ExtProc(int fn, int port,unsigned char * buff)
{
UINT * buffptr;
char txbuff[500];
unsigned int txlen=0;
struct TNCINFO * TNC = TNCInfo[port];
int Stream = 0;
struct STREAMINFO * STREAM;
int TNCOK;
if (TNC == NULL)
return 0; // Port not defined
// Look for attach on any call
// for (Stream = 0; Stream <= 1; Stream++)
{
STREAM = &TNC->Streams[Stream];
if (TNC->PortRecord->ATTACHEDSESSIONS[Stream] && TNC->Streams[Stream].Attached == 0)
{
char Cmd[80];
// New Attach
int calllen;
STREAM->Attached = TRUE;
TNC->FLInfo->RAW = FALSE;
calllen = ConvFromAX25(TNC->PortRecord->ATTACHEDSESSIONS[Stream]->L4USER, STREAM->MyCall);
STREAM->MyCall[calllen] = 0;
STREAM->FramesOutstanding = 0;
SuspendOtherPorts(TNC); // Dont allow connects on interlocked ports
// Stop Scanning
sprintf(Cmd, "%d SCANSTOP", TNC->Port);
Rig_Command(-1, Cmd);
sprintf(TNC->WEB_TNCSTATE, "In Use by %s", TNC->Streams[0].MyCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
/* len = sprintf(Cmd, "%cSTOP_BEACON_ARQ_FAE\x1b", '\x1a');
if (TNC->MPSKInfo->TX)
TNC->CmdSet = TNC->CmdSave = _strdup(Cmd); // Savde till not transmitting
else
SendPacket(TNC->WINMORDataSock, Cmd, len, 0);
*/
}
}
switch (fn)
{
case 7:
// 100 mS Timer.
// See if waiting for busy to clear before sending a connect
if (TNC->BusyDelay)
{
// Still Busy?
if (InterlockedCheckBusy(TNC) == FALSE)
{
// No, so send connect
struct ARQINFO * ARQ = TNC->ARQInfo;
int SendLen;
char Reply[80];
SendLen = sprintf(Reply, "c%s:42 %s:24 %c 7 T60R5W10",
STREAM->MyCall, STREAM->RemoteCall, ARQ->OurStream);
strcpy(TNC->WEB_PROTOSTATE, "Connecting");
SetWindowText(TNC->xIDC_PROTOSTATE, TNC->WEB_PROTOSTATE);
ARQ->ARQState = ARQ_ACTIVE;
ARQ->ARQTimerState = ARQ_CONNECTING;
SaveAndSend(TNC, ARQ, TNC->WINMORDataSock, Reply, SendLen);
STREAM->Connecting = TRUE;
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s", STREAM->MyCall, STREAM->RemoteCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
strcpy(TNC->WEB_PROTOSTATE, "Connecting");
SetWindowText(TNC->xIDC_PROTOSTATE, TNC->WEB_PROTOSTATE);
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->Streams[0].PACTORtoBPQ_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 (STREAM->NeedDisc)
{
STREAM->NeedDisc--;
if (STREAM->NeedDisc == 0)
{
// Send the DISCONNECT
TidyClose(TNC, 0);
}
}
ARQTimer(TNC);
SendXMLPoll(TNC);
TNC->SlowTimer--;
if (TNC->SlowTimer < 0)
{
TNC->SlowTimer = 100;
FLSlowTimer(TNC); // 10 Secs
}
return 0;
case 1: // poll
if (TNC->CONNECTED == FALSE && TNC->CONNECTING == FALSE && TNC->FLInfo->KISSMODE == FALSE)
{
// See if time to reconnect
time( <ime );
if (ltime-lasttime[port] >9 )
{
ConnecttoFLDigi(port);
lasttime[port]=ltime;
}
}
FD_ZERO(&readfs);
if (TNC->CONNECTED)
if (TNC->WINMORSock)
FD_SET(TNC->WINMORSock,&readfs);
if (TNC->CONNECTED || TNC->FLInfo->KISSMODE)
FD_SET(TNC->WINMORDataSock,&readfs);
// FD_ZERO(&writefs);
// if (TNC->BPQtoWINMOR_Q) FD_SET(TNC->WINMORDataSock,&writefs); // Need notification of busy clearing
FD_ZERO(&errorfs);
if (TNC->CONNECTED)
if (TNC->WINMORSock)
FD_SET(TNC->WINMORSock,&errorfs);
if (TNC->CONNECTED || TNC->FLInfo->KISSMODE)
FD_SET(TNC->WINMORDataSock,&errorfs);
if (select(TNC->WINMORDataSock + 1, &readfs, &writefs, &errorfs, &timeout) > 0)
{
// See what happened
if (FD_ISSET(TNC->WINMORDataSock,&readfs))
{
// data available
ProcessReceivedData(port);
}
if (FD_ISSET(TNC->WINMORSock,&readfs))
{
// data available
ProcessXMLData(port);
}
if (FD_ISSET(TNC->WINMORDataSock,&writefs))
{
if (BPQtoMPSK_Q[port] == 0)
{
// Connect success
TNC->CONNECTED = TRUE;
TNC->CONNECTING = FALSE;
sprintf(TNC->WEB_COMMSSTATE, "Connected to FLDIGI");
SetWindowText(TNC->xIDC_COMMSSTATE, TNC->WEB_COMMSSTATE);
// If required, send signon
// SendPacket(TNC->WINMORDataSock,"\x1a", 1, 0);
// SendPacket(TNC->WINMORDataSock,"DIGITAL MODE ?", 14, 0);
// SendPacket(TNC->WINMORDataSock,"\x1b", 1, 0);
// EnumWindows(EnumTNCWindowsProc, (LPARAM)TNC);
}
else
{
// Write block has cleared. Send rest of packet
buffptr=Q_REM(&BPQtoMPSK_Q[port]);
txlen=buffptr[1];
memcpy(txbuff,buffptr+2,txlen);
SendPacket(TNC, &txbuff[0], txlen);
ReleaseBuffer(buffptr);
}
}
if (FD_ISSET(TNC->WINMORDataSock,&errorfs) || FD_ISSET(TNC->WINMORSock,&errorfs))
{
// if connecting, then failed, if connected then has just disconnected
// if (CONNECTED[port])
// if (!CONNECTING[port])
// {
// i=sprintf(ErrMsg, "MPSK Connection lost for BPQ Port %d\r\n", port);
// WritetoConsole(ErrMsg);
// }
CONNECTING[port]=FALSE;
CONNECTED[port]=FALSE;
}
}
// See if any frames for this port
for (Stream = 0; Stream <= 1; Stream++)
{
STREAM = &TNC->Streams[Stream];
if (STREAM->Attached)
CheckForDetach(TNC, Stream, STREAM, TidyClose, ForcedClose, CloseComplete);
if (STREAM->ReportDISC)
{
STREAM->ReportDISC = FALSE;
buff[4] = Stream;
return -1;
}
// if Busy, send buffer status poll
if (STREAM->PACTORtoBPQ_Q == 0)
{
if (STREAM->DiscWhenAllSent)
{
STREAM->DiscWhenAllSent--;
if (STREAM->DiscWhenAllSent == 0)
STREAM->ReportDISC = TRUE; // Dont want to leave session attached. Causes too much confusion
}
}
else
{
int datalen;
buffptr=Q_REM(&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);
ReleaseBuffer(buffptr);
return (1);
}
}
if (TNC->PortRecord->UI_Q)
{
struct _MESSAGE * buffptr;
int SendLen;
char Reply[256];
int UILen;
char * UIMsg;
buffptr = Q_REM(&TNC->PortRecord->UI_Q);
UILen = buffptr->LENGTH;
UILen -= 23;
UIMsg = buffptr->L2DATA;
UIMsg[UILen] = 0;
if (UILen < 129 && TNC->Streams[0].Attached == FALSE) // Be sensible!
{
// >00uG8BPQ:72 TestA
SendLen = sprintf(Reply, "u%s:72 %s", TNC->NodeCall, UIMsg);
SendPacket(TNC, Reply, SendLen);
}
ReleaseBuffer(buffptr);
}
return (0);
case 2: // send
if (!TNC->CONNECTED) return 0; // Don't try if not connected to TNC
Stream = buff[4];
STREAM = &TNC->Streams[Stream];
// txlen=(buff[6]<<8) + buff[5] - 8;
txlen = GetLengthfromBuffer(buff) - 8;
if (STREAM->Connected)
{
buffptr = GetBuff();
if (buffptr == 0) return (0); // No buffers, so ignore
buffptr[1] = txlen;
memcpy(buffptr+2, &buff[8], txlen);
C_Q_ADD(&TNC->Streams[Stream].BPQtoPACTOR_Q, buffptr);
return (0);
}
else
{
buff[8 + txlen] = 0;
_strupr(&buff[8]);
if (_memicmp(&buff[8], "D\r", 2) == 0)
{
if (STREAM->Connected)
TidyClose(TNC, buff[4]);
STREAM->ReportDISC = TRUE; // Tell Node
return 0;
}
// See if Local command (eg RADIO)
if (_memicmp(&buff[8], "RADIO ", 6) == 0)
{
sprintf(&buff[8], "%d %s", TNC->Port, &buff[14]);
if (Rig_Command(TNC->PortRecord->ATTACHEDSESSIONS[0]->L4CROSSLINK->CIRCUITINDEX, &buff[8]))
{
}
else
{
UINT * buffptr = GetBuff();
if (buffptr == 0) return 1; // No buffers, so ignore
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "%s", &buff[8]);
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
}
return 1;
}
if (_memicmp(&buff[8], "MODEM ", 6) == 0)
{
_strupr(&buff[8]);
buff[7 + txlen] = 0;
// If in KISS mode, send as a KISS command Frame
if (TNC->FLInfo->KISSMODE)
{
sprintf(txbuff, "MODEM:%s MODEM:", &buff[14]);
SendKISSCommand(TNC, txbuff);
}
else
{
SendXMLCommand(TNC, "modem.set_by_name", &buff[14], 'S');
}
TNC->InternalCmd = TRUE;
return 1;
}
if (_memicmp(&buff[8], "FREQ ", 5) == 0)
{
_strupr(&buff[8]);
buff[7 + txlen] = 0;
// If in KISS mode, send as a KISS command Frame
if (TNC->FLInfo->KISSMODE)
{
sprintf(txbuff, "WFF:%s WFF:",&buff[13]);
SendKISSCommand(TNC, txbuff);
}
else
{
SendXMLCommand(TNC, "modem.set_carrier", &buff[13], 'I');
}
TNC->InternalCmd = TRUE;
return 1;
}
if (_memicmp(&buff[8], "SQUELCH ", 8) == 0)
{
_strupr(&buff[8]);
buff[7 + txlen] = 0;
// Only works in KISS
if (TNC->FLInfo->KISSMODE)
{
if (_memicmp(&buff[16], "ON", 2) == 0)
sprintf(txbuff, "KPSQL:ON KPSQL:");
else if (_memicmp(&buff[16], "OFF", 3) == 0)
sprintf(txbuff, "KPSQL:OFF KPSQL:");
else
txlen = sprintf(txbuff, "KPSQLS:%s KPSQLS:", &buff[16]);
SendKISSCommand(TNC, txbuff);
TNC->InternalCmd = TRUE;
}
return 1;
}
if (_memicmp(&buff[8], "KPSATT ", 7) == 0)
{
_strupr(&buff[8]);
buff[7 + txlen] = 0;
// If in KISS mode, send as a KISS command Frame
if (TNC->FLInfo->KISSMODE)
{
sprintf(txbuff, "KPSATT:%s KPSATT:", &buff[15]);
SendKISSCommand(TNC, txbuff);
TNC->InternalCmd = TRUE;
}
return 1;
}
if (STREAM->Connecting && _memicmp(&buff[8], "ABORT", 5) == 0)
{
// len = sprintf(Command,"%cSTOP_SELECTIVE_CALL_ARQ_FAE\x1b", '\x1a');
// if (TNC->MPSKInfo->TX)
// TNC->CmdSet = TNC->CmdSave = _strdup(Command); // Save till not transmitting
// else
// SendPacket(TNC->WINMORDataSock, Command, len, 0);
// TNC->InternalCmd = TRUE;
return (0);
}
if (_memicmp(&buff[8], "MODE", 4) == 0)
{
UINT * buffptr = GetBuff();
buff[7 + txlen] = 0; // Remove CR
if (strstr(&buff[8], "RAW"))
TNC->FLInfo->RAW = TRUE;
else if (strstr(&buff[8], "KISS"))
TNC->FLInfo->RAW = FALSE;
else
{
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "FLDigi} Error - Invalid Mode\r");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
return 1;
}
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "FLDigi} Ok - Mode is %s\r",
(TNC->FLInfo->RAW)?"RAW":"KISS");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
return 1;
}
if (_memicmp(&buff[8], "INUSE?", 6) == 0)
{
// Return Error if in use, OK if not
UINT * buffptr = GetBuff();
int s = 0;
while(s <= 1)
{
if (s != Stream)
{
if (TNC->PortRecord->ATTACHEDSESSIONS[s])
{
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "FLDig} Error - In use\r");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
return 1; // Busy
}
}
s++;
}
buffptr[1] = sprintf((UCHAR *)&buffptr[2], "FLDigi} Ok - Not in use\r");
C_Q_ADD(&STREAM->PACTORtoBPQ_Q, buffptr);
return 1;
}
// See if a Connect Command.
if (toupper(buff[8]) == 'C' && buff[9] == ' ' && txlen > 2) // Connect
{
char * ptr;
char * context;
struct ARQINFO * ARQ = TNC->ARQInfo;
int SendLen;
char Reply[80];
_strupr(&buff[8]);
buff[8 + txlen] = 0;
memset(ARQ, 0, sizeof(struct ARQINFO)); // Reset ARQ State
ARQ->TXSeq = ARQ->TXLastACK = 63; // Last Sent
ARQ->RXHighest = ARQ->RXNoGaps = 63; // Last Received
ARQ->OurStream = (rand() % 78) + 49; // To give some protection against other stuff on channel
ARQ->FarStream = 48; // Not yet defined
TNC->FLInfo->FLARQ = FALSE;
memset(STREAM->RemoteCall, 0, 10);
ptr = strtok_s(&buff[10], " ,\r", &context);
strcpy(STREAM->RemoteCall, ptr);
// See if Busy
if (InterlockedCheckBusy(TNC))
{
// Channel Busy. Unless override set, wait
if (TNC->OverrideBusy == 0)
{
// Save Command, and wait up to 10 secs
sprintf(TNC->WEB_TNCSTATE, "Waiting for clear channel");
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
TNC->BusyDelay = TNC->BusyWait * 10; // BusyWait secs
return 0;
}
}
TNC->OverrideBusy = FALSE;
//<SOH>00cG8BPQ:1025 G8BPQ:24 0 7 T60R5W10FA36<EOT>
SendLen = sprintf(Reply, "c%s:42 %s:24 %c 7 T60R5W10",
STREAM->MyCall, STREAM->RemoteCall, ARQ->OurStream);
strcpy(TNC->WEB_PROTOSTATE, "Connecting");
SetWindowText(TNC->xIDC_PROTOSTATE, TNC->WEB_PROTOSTATE);
ARQ->ARQState = ARQ_ACTIVE;
ARQ->ARQTimerState = ARQ_CONNECTING;
SaveAndSend(TNC, ARQ, TNC->WINMORDataSock, Reply, SendLen);
STREAM->Connecting = TRUE;
sprintf(TNC->WEB_TNCSTATE, "%s Connecting to %s", STREAM->MyCall, STREAM->RemoteCall);
SetWindowText(TNC->xIDC_TNCSTATE, TNC->WEB_TNCSTATE);
strcpy(TNC->WEB_PROTOSTATE, "Connecting");
SetWindowText(TNC->xIDC_PROTOSTATE, TNC->WEB_PROTOSTATE);
return 0;
}
// Send any other command to FLDIGI
_strupr(&buff[8]);
buff[7 + txlen] = 0;
// If in KISS mode, send as a KISS command Frame
if (TNC->FLInfo->KISSMODE)
{
char outbuff[1000];
int newlen;
buff[7] = 6; // KISS Control
newlen = KissEncode(&buff[7], outbuff, txlen);
sendto(TNC->WINMORDataSock, outbuff, newlen, 0, (struct sockaddr *)&TNC->Datadestaddr, sizeof(struct sockaddr));
}
else
{
SendXMLCommand(TNC, "modem.set_by_name", &buff[8], 'S');
}
TNC->InternalCmd = TRUE;
}
return (0);
case 3:
Stream = (int)buff;
TNCOK = TNC->CONNECTED;
STREAM = &TNC->Streams[Stream];
{
// Busy if TX Window reached
struct ARQINFO * ARQ = TNC->ARQInfo;
int Outstanding;
Outstanding = ARQ->TXSeq - ARQ->TXLastACK;
if (Outstanding < 0)
Outstanding += 64;
TNC->PortRecord->FramesQueued = Outstanding + TNC->Streams[0].BPQtoPACTOR_Q; // Save for Appl Level Queued Frames
if (Outstanding > ARQ->TXWindow)
return (1 | TNCOK << 8 | STREAM->Disconnecting << 15); // 3rd Nibble is frames unacked
else
return TNCOK << 8 | STREAM->Disconnecting << 15;
}
return TNCOK << 8 | STREAM->Disconnecting << 15; // OK, but lock attach if disconnecting
case 4: // reinit
shutdown(TNC->WINMORSock, SD_BOTH);
shutdown(TNC->WINMORDataSock, SD_BOTH);
Sleep(100);
closesocket(TNC->WINMORSock);
closesocket(TNC->WINMORDataSock);
TNC->CONNECTED = FALSE;
if (TNC->WeStartedTNC)
{
KillTNC(TNC);
RestartTNC(TNC);
}
return (0);
case 5: // Close
shutdown(TNC->WINMORSock, SD_BOTH);
shutdown(TNC->WINMORDataSock, SD_BOTH);
Sleep(100);
closesocket(TNC->WINMORSock);
closesocket(TNC->WINMORDataSock);
if (TNC->WeStartedTNC)
{
KillTNC(TNC);
}
return 0;
}
return 0;
}
#ifndef LINBPQ
int FindFLDIGI(char * Path)
{
HANDLE hProc;
char ExeName[256] = "";
char FLDIGIName[256];
DWORD Pid = 0;
DWORD Processes[1024], Needed, Count;
unsigned int i;
if (EnumProcessesPtr == NULL)
return 0; // Cant get PID
if (!EnumProcessesPtr(Processes, sizeof(Processes), &Needed))
return TRUE;
// Path is to .bat, so need to strip extension of both names
strcpy(FLDIGIName, Path);
strlop(FLDIGIName, '.');
// Calculate how many process identifiers were returned.
Count = Needed / sizeof(DWORD);
for (i = 0; i < Count; i++)
{
if (Processes[i] != 0)
{
hProc = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, Processes[i]);
if (hProc)
{
GetModuleFileNameExPtr(hProc, 0, ExeName, 255);
CloseHandle(hProc);
strlop(ExeName, '.');
if (_stricmp(ExeName, FLDIGIName) == 0)
return Processes[i];
}
}
}
return 0;
}
static KillTNC(struct TNCINFO * TNC)
{
HANDLE hProc;
if (TNC->PTTMode)
Rig_PTT(TNC->RIG, FALSE); // Make sure PTT is down
if (TNC->ProgramPath)
TNC->WIMMORPID = FindFLDIGI(TNC->ProgramPath);
if (TNC->WIMMORPID == 0) return 0;
hProc = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, TNC->WIMMORPID);
if (hProc)
{
TerminateProcess(hProc, 0);
CloseHandle(hProc);
}
TNC->WeStartedTNC = 0; // So we don't try again
return 0;
}
#endif
static RestartTNC(struct TNCINFO * TNC)
{
if (TNC->ProgramPath == NULL)
return 0;
_strlwr(TNC->ProgramPath);
if (_memicmp(TNC->ProgramPath, "REMOTE:", 7) == 0)
{
int n;
// Try to start TNC on a remote host
SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in destaddr;
Debugprintf("trying to restart FLDIGI %s", TNC->ProgramPath);
if (sock == INVALID_SOCKET)
return 0;
destaddr.sin_family = AF_INET;
destaddr.sin_addr.s_addr = inet_addr(TNC->WINMORHostName);
destaddr.sin_port = htons(8500);
if (destaddr.sin_addr.s_addr == INADDR_NONE)
{
// Resolve name to address
struct hostent * HostEnt = gethostbyname (TNC->WINMORHostName);
if (!HostEnt)
return 0; // Resolve failed
memcpy(&destaddr.sin_addr.s_addr,HostEnt->h_addr,4);
}
n = sendto(sock, TNC->ProgramPath, strlen(TNC->ProgramPath), 0, (struct sockaddr *)&destaddr, sizeof(destaddr));
Debugprintf("Restart FLDIGI - sento returned %d", n);
Sleep(100);
closesocket(sock);
return 1; // Cant tell if it worked, but assume ok
}
#ifndef LINBPQ
{
STARTUPINFO SInfo; // pointer to STARTUPINFO
PROCESS_INFORMATION PInfo; // pointer to PROCESS_INFORMATION