forked from erikarn/LinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TNCEmulators.c
5736 lines (4189 loc) · 110 KB
/
TNCEmulators.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
*/
// TNC Emulator Module for BPQ32 switch
// Supports TNC2 and WA8DED Hostmode interfaces
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include "CHeaders.h"
#define LF 10
#define CR 13
#define Connect(stream) SessionControl(stream,1,0)
#define Disconnect(stream) SessionControl(stream,2,0)
#define ReturntoNode(stream) SessionControl(stream,3,0)
#define ConnectUsingAppl(stream, appl) SessionControl(stream, 0, appl)
int APIENTRY SendMsg(int stream, char * msg, int len);
VOID SENDPACKET(struct TNCDATA * TNC);
VOID CHECKCTS(struct TNCDATA * TNC);
VOID SETCOMMANDMODE(struct TNCDATA * TNC);
VOID SETCOMM00(struct TNCDATA * TNC);
VOID SENDREPLY(struct TNCDATA * TNC, char * Msg, int Len);
VOID TNCCOMMAND(struct TNCDATA * TNC);
VOID TNC2PutChar(struct TNCDATA * TNC, int Char);
VOID KBECHO(struct TNCDATA * TNC, int Char);
VOID KBNORM(struct TNCDATA * TNC, int Char);
VOID PUTCHARINBUFFER(struct TNCDATA * TNC, int Char);
VOID TNC2GetChar(struct TNCDATA * TNC, int * returnedchar, int * moretocome);
VOID CONNECTTONODE(struct TNCDATA * TNC);
DllImport int APIENTRY ChangeSessionCallsign(int Stream, unsigned char * AXCall);
DllImport int APIENTRY GetCallsign(int stream, char * callsign);
VOID GETDATA(struct TNCDATA * TNC);
VOID DOCONMODECHANGE(struct TNCDATA * TNC);
VOID SEND_CONNECTED(struct TNCDATA * TNC);
VOID READCHANGE(int Stream);
VOID DOMONITORING(int NeedTrace);
int APIENTRY DecodeFrame(MESSAGE * msg, char * buffer, int Stamp);
time_t APIENTRY GetRaw(int stream, char * msg, int * len, int * count);
BOOL TfPut(struct TNCDATA * TNC, UCHAR character);
int IntDecodeFrame(MESSAGE * msg, char * buffer, int Stamp, UINT Mask, BOOL APRS, BOOL MCTL);
int DATAPOLL(struct TNCDATA * TNC, struct StreamInfo * Channel);
int STATUSPOLL(struct TNCDATA * TNC, struct StreamInfo * Channel);
int PROCESSHOSTPACKET(struct StreamInfo * Channel, struct TNCDATA * TNC);
VOID ProcessKPacket(struct TNCDATA * conn, UCHAR * rxbuffer, int Len);
VOID ProcessPacket(struct TNCDATA * conn, UCHAR * rxbuffer, int Len);;
int KANTConnected(struct TNCDATA * conn, struct StreamInfo * channel, int Stream);
int KANTDisconnected(struct TNCDATA * conn, struct StreamInfo * channel, int Stream);
VOID SendKISSData(struct TNCDATA * conn, UCHAR * txbuffer, int Len);
VOID ProcessSCSPacket(struct TNCDATA * conn, UCHAR * rxbuffer, int Length);
VOID TNCPoll();
unsigned long _beginthread( void( *start_address )(), unsigned stack_size, void * arglist);
VOID DisableAppl(struct TNCDATA * TNC);
int BPQSerialSetPollDelay(HANDLE hDevice, int PollDelay);
#define TNCBUFFLEN 1024
extern struct TNCDATA * TNCCONFIGTABLE;
struct TNCDATA * TNC2TABLE; // malloc'ed
extern int NUMBEROFTNCPORTS;
struct TNCDATA TDP; // Only way I can think of to get offets to port data into cmd table
// MODEFLAG DEFINITIONS
#define COMMAND 1
#define TRANS 2
#define CONV 4
// APPLFLAGS BITS
//CMD_TO_APPL EQU 1B ; PASS COMMAND TO APPLICATION
//MSG_TO_USER EQU 10B ; SEND "CONNECTED" TO USER
//MSG_TO_APPL EQU 100B ; SEND "CONECTED" TO APPL
extern char pgm[256];
int CloseDelay = 10; // Close after connect fail delay
MESSAGE MONITORDATA; // RAW FRAME FROM NODE
char NEWCALL[11];
//TABLELEN DW TYPE TNCDATA
char LNKSTATEMSG[] = "Link state is: ";
char CONNECTEDMSG[] = "CONNECTED to ";
char WHATMSG[] = "Eh?\rcmd:";
char CMDMSG[] = "cmd:";
char DISCONNMSG[] = "\r*** DISCONNECTED\r";
char CONMSG1[] = "\r*** CONNECTED to ";
char CONCALL[10];
char SIGNON[] = "\r\rG8BPQ TNC2 EMULATOR\r\r";
char CONMSG[] ="\r*** CONNECTED to ";
char SWITCH[] = "SWITCH\r";
char SWITCHSP[] = "SWITCH ";
char WAS[] = " was ";
char VIA[] = " via ";
char OFF[] = "OFF\r";
char ON[] = "ON \r";
// BPQ Serial Device Support
// On W2K and above, BPQVIrtualCOM.sys provides a pair of cross-connected devices, and a control channel
// to enumerate, add and delete devices.
// On Win98 BPQVCOMM.VXD provides a single IOCTL interface, over which calls for each COM device are multiplexed
#ifdef WIN32
#define IOCTL_SERIAL_SET_BAUD_RATE CTL_CODE(FILE_DEVICE_SERIAL_PORT, 1,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_QUEUE_SIZE CTL_CODE(FILE_DEVICE_SERIAL_PORT, 2,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_LINE_CONTROL CTL_CODE(FILE_DEVICE_SERIAL_PORT, 3,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_BREAK_ON CTL_CODE(FILE_DEVICE_SERIAL_PORT, 4,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_BREAK_OFF CTL_CODE(FILE_DEVICE_SERIAL_PORT, 5,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_IMMEDIATE_CHAR CTL_CODE(FILE_DEVICE_SERIAL_PORT, 6,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_TIMEOUTS CTL_CODE(FILE_DEVICE_SERIAL_PORT, 7,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_TIMEOUTS CTL_CODE(FILE_DEVICE_SERIAL_PORT, 8,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_DTR CTL_CODE(FILE_DEVICE_SERIAL_PORT, 9,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_CLR_DTR CTL_CODE(FILE_DEVICE_SERIAL_PORT,10,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_RESET_DEVICE CTL_CODE(FILE_DEVICE_SERIAL_PORT,11,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_RTS CTL_CODE(FILE_DEVICE_SERIAL_PORT,12,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_CLR_RTS CTL_CODE(FILE_DEVICE_SERIAL_PORT,13,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_XOFF CTL_CODE(FILE_DEVICE_SERIAL_PORT,14,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_XON CTL_CODE(FILE_DEVICE_SERIAL_PORT,15,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_WAIT_MASK CTL_CODE(FILE_DEVICE_SERIAL_PORT,16,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_WAIT_MASK CTL_CODE(FILE_DEVICE_SERIAL_PORT,17,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_WAIT_ON_MASK CTL_CODE(FILE_DEVICE_SERIAL_PORT,18,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_PURGE CTL_CODE(FILE_DEVICE_SERIAL_PORT,19,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_BAUD_RATE CTL_CODE(FILE_DEVICE_SERIAL_PORT,20,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_LINE_CONTROL CTL_CODE(FILE_DEVICE_SERIAL_PORT,21,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_CHARS CTL_CODE(FILE_DEVICE_SERIAL_PORT,22,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_CHARS CTL_CODE(FILE_DEVICE_SERIAL_PORT,23,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_HANDFLOW CTL_CODE(FILE_DEVICE_SERIAL_PORT,24,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_HANDFLOW CTL_CODE(FILE_DEVICE_SERIAL_PORT,25,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_MODEMSTATUS CTL_CODE(FILE_DEVICE_SERIAL_PORT,26,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_COMMSTATUS CTL_CODE(FILE_DEVICE_SERIAL_PORT,27,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_XOFF_COUNTER CTL_CODE(FILE_DEVICE_SERIAL_PORT,28,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_PROPERTIES CTL_CODE(FILE_DEVICE_SERIAL_PORT,29,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GET_DTRRTS CTL_CODE(FILE_DEVICE_SERIAL_PORT,30,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_IS_COM_OPEN CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_GETDATA CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x801,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SETDATA CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x802,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_CTS CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x803,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_DSR CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x804,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_SET_DCD CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x805,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_CLR_CTS CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x806,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_CLR_DSR CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x807,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_SERIAL_CLR_DCD CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x808,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_BPQ_ADD_DEVICE CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x809,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_BPQ_DELETE_DEVICE CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x80a,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_BPQ_LIST_DEVICES CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x80b,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_BPQ_SET_POLLDELAY CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x80c,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define IOCTL_BPQ_SET_DEBUGMASK CTL_CODE(FILE_DEVICE_SERIAL_PORT,0x80d,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define W98_SERIAL_IS_COM_OPEN 0x800
#define W98_SERIAL_GETDATA 0x801
#define W98_SERIAL_SETDATA 0x802
#define W98_SERIAL_SET_CTS 0x803
#define W98_SERIAL_SET_DSR 0x804
#define W98_SERIAL_SET_DCD 0x805
#define W98_SERIAL_CLR_CTS 0x806
#define W98_SERIAL_CLR_DSR 0x807
#define W98_SERIAL_CLR_DCD 0x808
#define W98_BPQ_ADD_DEVICE 0x809
#define W98_BPQ_DELETE_DEVICE 0x80a
#define W98_BPQ_LIST_DEVICES 0x80b
#define W98_BPQ_SET_POLLDELAY 0x80c
#define W98_BPQ_SET_DEBUGMASK 0x80d
#define W98_SERIAL_GET_COMMSTATUS 27
#define W98_SERIAL_GET_DTRRTS 30
#define DebugModemStatus 1
#define DebugCOMStatus 2
#define DebugWaitCompletion 4
#define DebugReadCompletion 8
HANDLE hControl;
BOOL Win98;
typedef struct _SERIAL_STATUS {
ULONG Errors;
ULONG HoldReasons;
ULONG AmountInInQueue;
ULONG AmountInOutQueue;
BOOL EofReceived;
BOOL WaitForImmediate;
} SERIAL_STATUS,*PSERIAL_STATUS;
#endif
#ifndef WIN32
// #include <pty.h>
HANDLE LinuxOpenPTY(char * Name)
{
// Open a Virtual COM Port
HANDLE hDevice, slave;;
char slavedevice[80];
int ret;
u_long param=1;
struct termios term;
#if defined(MACBPQ) || defined(__FreeBSD__)
// Create a pty pair
openpty(&hDevice, &slave, &slavedevice[0], NULL, NULL);
close(slave);
#elif defined(__Linux__)
hDevice = posix_openpt(O_RDWR|O_NOCTTY);
if (hDevice == -1 || grantpt (hDevice) == -1 || unlockpt (hDevice) == -1 ||
ptsname_r(hDevice, slavedevice, 80) != 0)
{
perror("Create PTY pair failed");
return -1;
}
#endif
printf("slave device: %s. ", slavedevice);
if (tcgetattr(hDevice, &term) == -1)
{
perror("tty_speed: tcgetattr");
return FALSE;
}
cfmakeraw(&term);
if (tcsetattr(hDevice, TCSANOW, &term) == -1)
{
perror("tcsetattr");
return -1;
}
ioctl(hDevice, FIONBIO, ¶m);
chmod(slavedevice, S_IRUSR|S_IRGRP|S_IWUSR|S_IWGRP|S_IROTH|S_IWOTH);
unlink (Name);
ret = symlink (slavedevice, Name);
if (ret == 0)
printf ("symlink to %s created\n", Name);
else
printf ("symlink to %s failed\n", Name);
return hDevice;
}
#else
HANDLE BPQOpenSerialPort(struct TNCDATA * TNC, DWORD * lasterror)
{
// Open a Virtual COM Port
int port = TNC->ComPort;
char szPort[80];
HANDLE hDevice;
int Err;
*lasterror=0;
if (Win98)
{
sprintf( szPort, "\\\\.\\COM%d",port) ;
hDevice = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (hDevice == (HANDLE) -1 )
{
// If In Use(5) ok, else fail
if (GetLastError() == 5)
return (HANDLE)(port<<16); // Port Number is a pseudohandle to the device
return (HANDLE) -1;
}
CloseHandle(hDevice);
return (HANDLE)(port<<16); // Port Number is a pseudohandle to the device
}
// Try New Style VCOM firsrt
sprintf( szPort, "\\\\.\\pipe\\BPQCOM%d", port ) ;
hDevice = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
Err = GetLastError();
if (hDevice != (HANDLE) -1)
{
TNC->NewVCOM = TRUE;
TNC->PortEnabled = TRUE;
Err = GetFileType(hDevice);
}
else
{
// Try old style
sprintf(szPort, "\\\\.\\BPQ%d", port ) ;
hDevice = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (TNC->PollDelay)
BPQSerialSetPollDelay(hDevice, TNC->PollDelay);
}
if (hDevice == (HANDLE) -1 )
{
*lasterror=GetLastError();
}
return hDevice;
}
#endif
int BPQSerialSetCTS(HANDLE hDevice)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl,(UINT)(UINT)hDevice | W98_SERIAL_SET_CTS,NULL,0,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_SET_CTS,NULL,0,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialSetDSR(HANDLE hDevice)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_SET_DSR,NULL,0,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_SET_DSR, NULL,0,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialSetDCD(HANDLE hDevice)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_SET_DCD,NULL,0,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_SET_DCD,NULL,0,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialClrCTS(HANDLE hDevice)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_CLR_CTS,NULL,0,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_CLR_CTS,NULL,0,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialClrDSR(HANDLE hDevice)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_CLR_DSR,NULL,0,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_CLR_DSR,NULL,0,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialClrDCD(HANDLE hDevice)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_CLR_DCD,NULL,0,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_CLR_DCD, NULL,0,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialSendData(struct TNCDATA * TNC, UCHAR * Message,int MsgLen)
{
HANDLE hDevice = TNC->hDevice;
ULONG bytesReturned;
// Host Mode code calls BPQSerialSendData for all ports, so it a real port, pass on to real send routine
if (!TNC->VCOM)
return WriteCOMBlock(TNC->hDevice, Message, MsgLen);
#ifndef WIN32
// Linux usies normal IO for all ports
return WriteCOMBlock(TNC->hDevice, Message, MsgLen);
#else
if (MsgLen > 4096 ) return ERROR_INVALID_PARAMETER;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_SETDATA,Message,MsgLen,NULL,0, &bytesReturned,NULL);
else
{
if (TNC->NewVCOM)
{
// Have to escape all oxff chars, as these are used to get status info
UCHAR NewMessage[1000];
UCHAR * ptr1 = Message;
UCHAR * ptr2 = NewMessage;
UCHAR c;
int Length = MsgLen;
while (Length != 0)
{
c = *(ptr1++);
*(ptr2++) = c;
if (c == 0xff)
{
*(ptr2++) = c;
MsgLen++;
}
Length--;
}
return WriteFile(hDevice, NewMessage, MsgLen, &bytesReturned, NULL);
}
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_SETDATA,Message,MsgLen,NULL,0, &bytesReturned,NULL);
}
#endif
}
int BPQSerialGetData(struct TNCDATA * TNC, UCHAR * Message, unsigned int BufLen, ULONG * MsgLen)
{
#ifdef WIN32
DWORD dwLength = 0;
DWORD Available = 0;
HANDLE hDevice = TNC->hDevice;
int Length, RealLen = 0;
if (BufLen > 4096 ) return ERROR_INVALID_PARAMETER;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_GETDATA,NULL,0,Message,BufLen,MsgLen,NULL);
if (TNC->NewVCOM)
{
int ret = PeekNamedPipe(hDevice, NULL, 0, NULL, &Available, NULL);
if (ret == 0)
{
ret = GetLastError();
if (ret == ERROR_BROKEN_PIPE)
{
CloseHandle(hDevice);
hDevice = INVALID_HANDLE_VALUE;
return 0;
}
}
if (Available > BufLen)
Available = BufLen;
if (Available)
{
UCHAR * ptr1 = Message;
UCHAR * ptr2 = Message;
UCHAR c;
ReadFile(hDevice, Message, Available, &Length, NULL);
// Have to look for FF escape chars
RealLen = Length;
while (Length != 0)
{
c = *(ptr1++);
Length--;
if (c == 0xff)
{
c = c = *(ptr1++);
Length--;
if (c == 0xff) // ff ff means ff
{
RealLen--;
}
else
{
// This is connection statua from other end
RealLen -= 2;
// TNC->PortEnabled = c;
continue;
}
}
*(ptr2++) = c;
}
}
*MsgLen = RealLen;
return 0;
}
return DeviceIoControl(hDevice,IOCTL_SERIAL_GETDATA,NULL,0,Message,BufLen,MsgLen,NULL);
}
#else
return 0;
}
#endif
int BPQSerialGetQCounts(HANDLE hDevice,ULONG * RXCount, ULONG * TXCount)
{
#ifndef WIN32
return 0;
#else
SERIAL_STATUS Resp;
int MsgLen;
int ret;
if (Win98)
ret = DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_GET_COMMSTATUS,NULL,0,&Resp,sizeof(SERIAL_STATUS),&MsgLen,NULL);
else
ret = DeviceIoControl(hDevice,IOCTL_SERIAL_GET_COMMSTATUS,NULL,0,&Resp,sizeof(SERIAL_STATUS),&MsgLen,NULL);
*RXCount=Resp.AmountInInQueue;
*TXCount=Resp.AmountInOutQueue;
return ret;
#endif
}
int BPQSerialGetDeviceList(HANDLE hDevice,ULONG * Slot,ULONG * Port)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
return DeviceIoControl (hDevice,IOCTL_BPQ_LIST_DEVICES,Slot,4,Port,4,&bytesReturned,NULL);
#endif
}
int BPQSerialIsCOMOpen(HANDLE hDevice,ULONG * Count)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_IS_COM_OPEN,NULL,0,Count,4,&bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_IS_COM_OPEN,NULL,0,Count,4,&bytesReturned,NULL);
#endif
}
int BPQSerialGetDTRRTS(HANDLE hDevice, ULONG * Flags)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_SERIAL_GET_DTRRTS,NULL,0,Flags,4,&bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_SERIAL_GET_DTRRTS,NULL,0,Flags,4,&bytesReturned,NULL);
#endif
}
int BPQSerialSetPollDelay(HANDLE hDevice, int PollDelay)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
if (Win98)
return DeviceIoControl(hControl, (UINT)hDevice | W98_BPQ_SET_POLLDELAY,&PollDelay,4,NULL,0, &bytesReturned,NULL);
else
return DeviceIoControl(hDevice,IOCTL_BPQ_SET_POLLDELAY,&PollDelay,4,NULL,0, &bytesReturned,NULL);
#endif
}
int BPQSerialSetDebugMask(HANDLE hDevice, int DebugMask)
{
#ifndef WIN32
return 0;
#else
ULONG bytesReturned;
return DeviceIoControl(hDevice, IOCTL_BPQ_SET_DEBUGMASK, &DebugMask, 4, NULL, 0, &bytesReturned,NULL);
#endif
}
int LocalSessionState(int stream, int * state, int * change, BOOL ACK)
{
// Get current Session State. Any state changed is ACK'ed
// automatically. See BPQHOST functions 4 and 5.
// Local version without semaphore or checktimer
BPQVECSTRUC * HOST = &BPQHOSTVECTOR[stream -1]; // API counts from 1
// CX = 0 if stream disconnected or CX = 1 if stream connected
// DX = 0 if no change of state since last read, or DX = 1 if
// the connected/disconnected state has changed since
// last read (ie. delta-stream status).
// HOSTFLAGS = Bit 80 = Allocated
// Bit 40 = Disc Request
// Bit 20 = Stay Flag
// Bit 02 and 01 State Change Bits
if ((HOST->HOSTFLAGS & 3) == 0)
// No Chaange
*change = 0;
else
*change = 1;
if (HOST->HOSTSESSION) // LOCAL SESSION
// Connected
*state = 1;
else
*state = 0;
if (ACK)
HOST->HOSTFLAGS &= 0xFC; // Clear Change Bits
return 0;
}
VOID ONOFF(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
// PROCESS COMMANDS WITH ON/OFF PARAM
char Param = *Tail;
UINT offset, DPBASE;
UCHAR * valueptr;
UCHAR oldvalue, newvalue = 0xff;
char Response[80];
int len;
valueptr = (UCHAR *)TNC;
offset = (UINT)CMD->CMDFLAG;
DPBASE = (UINT)&TDP;
offset -= DPBASE;
valueptr += offset;
oldvalue = (UCHAR)*valueptr;
switch(Param)
{
case ' ':
break;
case 'Y':
newvalue = 1;
break;
case 'N':
newvalue = 0;
break;
case 'O':
if (Tail[1] == 'N')
newvalue = 1;
else
newvalue = 0;
break;
}
if (newvalue == 255)
{
len = sprintf(Response, "%s %s\r", CMD->String, (oldvalue)?"ON":"OFF");
}
else
{
len = sprintf(Response, "%s was %s\r", CMD->String, (oldvalue)?"ON":"OFF");
*valueptr = newvalue;
}
SENDREPLY(TNC, Response, len);
}
VOID ONOFF_CONOK(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
ONOFF(TNC, Tail, CMD);
// UPDATE APPL FLAGS ON NODE PORT
if (TNC->CONOK)
SetAppl(TNC->BPQPort, TNC->APPLFLAGS, TNC->APPLICATION);
else
SetAppl(TNC->BPQPort, TNC->APPLFLAGS, 0);
}
VOID SETMYCALL(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
char Response[80];
int len;
char Call[10] = " ";
if (*Tail == ' ')
{
// REQUEST FOR CURRENT STATE
len = sprintf(Response, "MYCALL %s\r", TNC->MYCALL);
}
else
{
strlop(Tail,' ');;
memcpy(Call, Tail, strlen(Tail) + 1);
len = sprintf(Response, "MYCALL was %s\r", TNC->MYCALL);
memcpy(TNC->MYCALL, Call, 10);
}
SENDREPLY(TNC, Response, len);
}
VOID BTEXT(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
}
VOID VALUE(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
// PROCESS COMMANDS WITH decimal value
char Param = *Tail;
UINT offset, DPBASE;
UCHAR * valueptr;
int oldvalue, newvalue;
char Response[80];
int len;
valueptr = (UCHAR *)TNC;
offset = (UINT)CMD->CMDFLAG;
DPBASE = (UINT)&TDP;
offset -= DPBASE;
valueptr += offset;
oldvalue = *valueptr;
strlop(Tail, ' ');
if (Tail[0])
{
newvalue = atoi(Tail);
len = sprintf(Response, "%s was %d\r", CMD->String, oldvalue);
*valueptr = newvalue;
}
else
{
len = sprintf(Response, "%s %d\r", CMD->String, oldvalue);
}
SENDREPLY(TNC, Response, len);
}
VOID VALHEX(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
// PROCESS COMMANDS WITH decimal value
char Param = *Tail;
UINT offset, DPBASE;
UCHAR * valueptr;
UINT * intvalueptr;
UINT oldvalue, newvalue;
char Response[80];
int len;
valueptr = (UCHAR *)TNC;
offset = (UINT)CMD->CMDFLAG;
DPBASE = (UINT)&TDP;
offset -= DPBASE;
valueptr += offset;
intvalueptr = (UINT *)valueptr;
oldvalue = *intvalueptr;
strlop(Tail, ' ');
if (Tail[0])
{
newvalue = (UINT)strtol(Tail, NULL, 16);
len = sprintf(Response, "%s was $%x\r", CMD->String, oldvalue);
*intvalueptr = newvalue;
}
else
{
len = sprintf(Response, "%s $%x\r", CMD->String, oldvalue);
}
SENDREPLY(TNC, Response, len);
}
VOID APPL_VALHEX(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
int ApplNum = 1;
UINT APPLMASK;
VALHEX(TNC, Tail, CMD);
// UPDATE APPL FLAGS ON NODE PORT
if (TNC->CONOK)
SetAppl(TNC->BPQPort, TNC->APPLFLAGS, TNC->APPLICATION);
else
SetAppl(TNC->BPQPort, TNC->APPLFLAGS, 0);
// Set MYCALL to APPLCALL
APPLMASK = TNC->APPLICATION;
ApplNum = 1;
while (APPLMASK && (APPLMASK & 1) == 0)
{
ApplNum++;
APPLMASK >>= 1;
}
if (TNC->CONOK && TNC->APPLICATION)
memcpy(TNC->MYCALL, GetApplCall(ApplNum), 10);
}
VOID CSWITCH(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
char Response[80];
int len;
len = sprintf(Response, "%s", CMDMSG);
SENDREPLY(TNC, Response, len);
CONNECTTONODE(TNC);
}
VOID CONMODE(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
SENDREPLY(TNC, CMDMSG, 4);
}
VOID TNCCONV(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
TNC->MODEFLAG |= CONV;
TNC->MODEFLAG &= ~(COMMAND+TRANS);
}
VOID TNCNODE(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
// CONNECT TO NODE
TNC->VMSR |= 0x88; // SET CONNECTED
TNC->MODEFLAG |= CONV; // INTO CONVERSE MODE
TNC->MODEFLAG &= ~(COMMAND+TRANS);
CONNECTTONODE(TNC);
READCHANGE(TNC->BPQPort); //CLEAR STATUS CHANGE (TO AVOID SUPURIOUS "CONNECTED TO")
}
VOID TNCCONNECT(struct TNCDATA * TNC, char * Tail, CMDX * CMD)
{
char Response[80];
int len;
char Call[10] = "";
if (*Tail == ' ')
{
// REQUEST FOR CURRENT STATE
len = sprintf(Response, "%s", LNKSTATEMSG);
if (TNC->VMSR & 0x80)
{
GetCallsign(TNC->BPQPort, Call);
strlop(Call, ' ');
len = sprintf(Response, "%s%s%s\r", LNKSTATEMSG, CONNECTEDMSG, Call);
}
else
{
len = sprintf(Response, "%s%s", LNKSTATEMSG, DISCONNMSG+5);
}
SENDREPLY(TNC, Response, len);
return;
}
// CONNECT, BUT NOT TO SWITCH - CONNECT TO NODE, THEN PASS TO IT FOR PROCESSING
TNCNODE(TNC, Tail, CMD);