summaryrefslogtreecommitdiff
path: root/bpqvkiss.c
blob: c639ceeda3c8260246ee14563554c404e94b4605 (plain)
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
//
//	DLL to inteface the BPQ Virtual COM emulator to BPQ32 switch 
//
//	Uses BPQ EXTERNAL interface
//

//	Version 1.0 November 2005
//

//  Version 1.1	October 2006

//		Write diagmnostics to BPQ console window instead of STDOUT

// Version 1.2 February 2008

//		Changes for dynamic unload of bpq32.dll

// Version 1.2.1 May 2008

//		Correct RX length (was 1 byte too long)

// Version 1.3.1 Jan 2009

//		Support Win98 VirtualCOM Driver

#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_DEPRECATE 

//#include <process.h>
//#include <time.h>

#define VERSION_MAJOR         1
#define VERSION_MINOR         0

typedef unsigned char byte;

#include "cheaders.h"
#include "bpqvkiss.h"

#include <stdlib.h>

//#define DYNLOADBPQ		// Dynamically Load BPQ32.dll
//#define EXTDLL			// Use GetMuduleHandle instead of LoadLibrary 
#include "bpq32.h"
 

static int	ASYINIT(int comport, int speed, int bpqport, BOOL Report);
int	kissencode(UCHAR * inbuff, UCHAR * outbuff, int len);
int GetRXMessage(int port, PMESSAGE buff);
void CheckReceivedData(PVCOMINFO  pVCOMInfo);
static int ReadCommBlock(PVCOMINFO  pVCOMInfo, LPSTR lpszBlock, DWORD nMaxLength );
static BOOL WriteCommBlock(int port, UCHAR * lpByte , DWORD dwBytesToWrite);

PVCOMINFO CreateInfo( int port,int speed, int bpqport )	;

#define	FEND	0xC0	// KISS CONTROL CODES 
#define	FESC	0xDB
#define	TFEND	0xDC
#define	TFESC	0xDD

static BOOL Win98 = FALSE;

struct PORTCONTROL * PORTVEC[MaxBPQPortNo + 1];

static size_t ExtProc(int fn, int port, PMESSAGE buff)
{
	int len,txlen=0;
	char txbuff[1000];

	if (VCOMInfo[port]->ComDev == (HANDLE) -1)
	{
		// Try to reopen every 30 secs

		VCOMInfo[port]->ReopenTimer++;

		if (VCOMInfo[port]->ReopenTimer < 300)
			return 0;

		VCOMInfo[port]->ReopenTimer = 0;
		
		ASYINIT(PORTVEC[port]->IOBASE, 9600, port, FALSE);

		if (VCOMInfo[port]->ComDev == (HANDLE) -1)
			return 0;
	}


	switch (fn)
	{
	case 1:				// poll

		len = GetRXMessage(port,buff);
	
//		if (len > 0)
//		{
//			// Randomly drop packets

//			if ((rand() % 7) > 5)
//			{
//				Debugprintf("VKISS Test Drop packet");
//				return 0;
//			}
//		}
		
		return len;

	case 2:				// send

		txlen = GetLengthfromBuffer((PDATAMESSAGE)buff); 

		txlen = kissencode(&buff->DEST[0], (char *) &txbuff, txlen - MSGHDDRLEN);

		WriteCommBlock(port, txbuff, txlen);
		
		return (0);


	case 3:				// CHECK IF OK TO SEND

		return (0);		// OK
			
		break;

	case 4:				// reinit
		
		CloseHandle(VCOMInfo[port]->ComDev);
		VCOMInfo[port]->ComDev =(HANDLE) -1;
		VCOMInfo[port]->ReopenTimer = 250;

		return (0);

	case 5:				// Close

		CloseHandle(VCOMInfo[port]->ComDev);

		return (0);

	}

	return 0;

}

VOID * VCOMExtInit(struct PORTCONTROL *  PortEntry)
{
	char msg[80];
	
	//
	//	Will be called once for each port to be mapped to a BPQ Virtual COM Port
	//	The VCOM port number is in IOBASE
	//

	sprintf(msg,"VKISS COM%d", PortEntry->IOBASE);
	WritetoConsole(msg);

	PORTVEC[PortEntry->PORTNUMBER] = PortEntry;
	
	CreateInfo(PortEntry->IOBASE, 9600, PortEntry->PORTNUMBER);

	// Open File
	
	ASYINIT(PortEntry->IOBASE, 9600, PortEntry->PORTNUMBER, TRUE);

	WritetoConsole("\n");

	return ExtProc;
}

static int	kissencode(UCHAR * inbuff, UCHAR * outbuff, int len)
{
	int i, txptr = 0;
	UCHAR c;

	outbuff[0] = FEND;
	outbuff[1] = 0;
	txptr = 2;

	for (i = 0; i < len; i++)
	{
		c = inbuff[i];

		switch (c)
		{
		case FEND:
			outbuff[txptr++] = FESC;
			outbuff[txptr++] = TFEND;
			break;

		case FESC:

			outbuff[txptr++] = FESC;
			outbuff[txptr++] = TFESC;
			break;

		default:

			outbuff[txptr++] = c;
		}
	}

	outbuff[txptr++] = FEND;

	return txptr;

}

int	ASYINIT(int comport, int speed, int bpqport, BOOL Report)
{
   char szPort[ 30 ];
   char buf[256];
   int n, Err;

#pragma warning( push )
#pragma warning( disable : 4996 )

#ifndef _winver

#define _winver 0x0600

#endif


   if (HIBYTE(_winver) < 5)
		Win98 = TRUE;

#pragma warning( pop ) 

   if (Win98)
	{
		VCOMInfo[bpqport]->ComDev = CreateFile( "\\\\.\\BPQVCOMM.VXD", GENERIC_READ | GENERIC_WRITE,
                  0,                    // exclusive access
	               NULL,                 // no security attrs
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL, 
                  NULL );
	}
   else{
		sprintf( szPort, "\\\\.\\pipe\\BPQCOM%d", comport ) ;

		VCOMInfo[bpqport]->ComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
                  0,                    // exclusive access
                  NULL,                 // no security attrs
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL, 
                  NULL );

			//Handle = CreateFile(Value, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);


		Err = GetLastError();

		if (VCOMInfo[bpqport]->ComDev != (HANDLE) -1)
		{
			VCOMInfo[bpqport]->NewVCOM = TRUE;
			Err = GetFileType(VCOMInfo[bpqport]->ComDev);
		}
		else
		{
			// Try old style 	

			sprintf( szPort, "\\\\.\\BPQ%d", comport ) ;

			VCOMInfo[bpqport]->ComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
                  0,                    // exclusive access
                  NULL,                 // no security attrs
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL, 
                  NULL );
		}
   }  
	if (VCOMInfo[bpqport]->ComDev == (HANDLE) -1 && Report)
	{
		n=sprintf(buf,"Virtual COM Port %d could not be opened ",comport);
		WritetoConsole(buf);

		return (FALSE) ;
	}

	return (TRUE) ;
}

static int GetRXMessage(int port, PMESSAGE buff)
{
	int len;
	PVCOMINFO pVCOMInfo ;

	if (NULL == (pVCOMInfo = VCOMInfo[port]))
		return 0;

	if (!pVCOMInfo->MSGREADY)
		CheckReceivedData(pVCOMInfo);		// Look for data in RXBUFFER and COM port

	if (pVCOMInfo->MSGREADY)
	{
		len = (int)(pVCOMInfo->RXMPTR- &pVCOMInfo->RXMSG[1]);		// Don't need KISS Control Byte
		
  		if (pVCOMInfo->RXMSG[0] != 0 && pVCOMInfo->RXMSG[0] != 12)
		{
			pVCOMInfo->MSGREADY=FALSE;
			pVCOMInfo->RXMPTR=(UCHAR *)&pVCOMInfo->RXMSG;
			return 0;						// Not KISS Data
		}

		//
		//	Remove KISS control byte
		//

		if (pVCOMInfo->RXMSG[0] == 12)
		{
			//	AckMode Frame. Return the next 2 bytes, but don't pass them to Host

			UCHAR AckResp[8];

			AckResp[0] = FEND;
			memcpy(&AckResp[1], &pVCOMInfo->RXMSG[0], 3);	//Copy Opcode and Ack Bytes
			AckResp[4] = FEND;
			WriteCommBlock(port, AckResp, 5);

			len -= 2;
			memcpy(&buff->DEST[0], &pVCOMInfo->RXMSG[3], len);
//			Debugprintf("VKISS Ackmode Frame");
		}
		else
	
			memcpy(&buff->DEST[0], &pVCOMInfo->RXMSG[1], len);

		len += MSGHDDRLEN;

		PutLengthinBuffer((PDATAMESSAGE)buff, len);
		
		//
		//	reset pointers
		//

		pVCOMInfo->MSGREADY=FALSE;
		pVCOMInfo->RXMPTR=(UCHAR *)&pVCOMInfo->RXMSG;

		return len;
	}
	else

	return 0;					// nothing doing
}

static void CheckReceivedData(PVCOMINFO pVCOMInfo)
{
 	UCHAR c;

	if (pVCOMInfo->RXBCOUNT == 0)
	{	
		//
		//	Check com buffer
		//
	
		pVCOMInfo->RXBCOUNT = ReadCommBlock(pVCOMInfo, (LPSTR) &pVCOMInfo->RXBUFFER, MAXBLOCK-1 );
		pVCOMInfo->RXBPTR=(UCHAR *)&pVCOMInfo->RXBUFFER; 
	}

	if (pVCOMInfo->RXBCOUNT == 0)
		return;

	while (pVCOMInfo->RXBCOUNT != 0)
	{
		pVCOMInfo->RXBCOUNT--;

		c = *(pVCOMInfo->RXBPTR++);

		if (pVCOMInfo->ESCFLAG)
		{
			//
			//	FESC received - next should be TFESC or TFEND

			pVCOMInfo->ESCFLAG = FALSE;

			if (c == TFESC)
				c=FESC;
	
			if (c == TFEND)
				c=FEND;

		}
		else
		{
			switch (c)
			{
			case FEND:		
	
				//
				//	Either start of message or message complete
				//
				
				if (pVCOMInfo->RXMPTR == (UCHAR *)&pVCOMInfo->RXMSG)
					continue;

				pVCOMInfo->MSGREADY=TRUE;
				return;

			case FESC:
		
				pVCOMInfo->ESCFLAG = TRUE;
				continue;

			}
		}
		
		//
		//	Ok, a normal char
		//

		*(pVCOMInfo->RXMPTR++) = c;

	}

	if (pVCOMInfo->RXMPTR - (UCHAR *)&pVCOMInfo->RXMSG > 500)
		pVCOMInfo->RXMPTR=(UCHAR *)&pVCOMInfo->RXMSG;
	
 	return;
}

static PVCOMINFO CreateInfo( int port,int speed, int bpqport )
{
   PVCOMINFO pVCOMInfo ;

   if (NULL == (pVCOMInfo =
                   (PVCOMINFO) LocalAlloc( LPTR, sizeof( VCOMINFO ) )))
      return ( (PVCOMINFO) -1 ) ;

 	pVCOMInfo->RXBCOUNT=0;
	pVCOMInfo->MSGREADY=FALSE;
	pVCOMInfo->RXBPTR=(UCHAR *)&pVCOMInfo->RXBUFFER; 
	pVCOMInfo->RXMPTR=(UCHAR *)&pVCOMInfo->RXMSG;
   
	pVCOMInfo->ComDev = 0 ;
	pVCOMInfo->Connected = FALSE ;
	pVCOMInfo->Port = port;

	VCOMInfo[bpqport]=pVCOMInfo;
	
	return (pVCOMInfo);
}

static BOOL NEAR DestroyTTYInfo( int port )
{
   PVCOMINFO pVCOMInfo ;

   if (NULL == (pVCOMInfo = VCOMInfo[port]))
      return ( FALSE ) ;

   LocalFree( pVCOMInfo ) ;

   VCOMInfo[port] = 0;

   return ( TRUE ) ;

} 

static int ReadCommBlock(PVCOMINFO  pVCOMInfo, LPSTR lpszBlock, DWORD nMaxLength)
{
	DWORD dwLength = 0;
	DWORD Available = 0;

	if (Win98)
		DeviceIoControl(pVCOMInfo->ComDev, (pVCOMInfo->Port << 16) | W98_SERIAL_GETDATA,
					NULL,0,lpszBlock,nMaxLength, &dwLength,NULL);

	else if (pVCOMInfo->NewVCOM)
	{
		int ret = PeekNamedPipe(pVCOMInfo->ComDev, NULL, 0, NULL, &Available, NULL);

		if (ret == 0)
		{
			ret = GetLastError();

			if (ret == ERROR_BROKEN_PIPE)
			{
				CloseHandle(pVCOMInfo->ComDev);
				pVCOMInfo->ComDev = INVALID_HANDLE_VALUE;
				return 0;
			}
		}

		if (Available > nMaxLength)
			Available = nMaxLength;
		
		if (Available)
		{
			UCHAR * ptr1 = lpszBlock;
			UCHAR * ptr2 = lpszBlock;
			UCHAR c;
			int Length;
			
			ReadFile(pVCOMInfo->ComDev, lpszBlock, Available, &dwLength, NULL);

			// Have to look foro FF escape chars

			Length = dwLength;

			while (Length != 0)
			{
				c = *(ptr1++);
				Length--;

				if (c == 0xff)
				{
					c = c = *(ptr1++);
					Length--;
					
					if (c == 0xff)			// ff ff means ff
					{
						dwLength--;
					}
					else
					{
						// This is connection statua from other end

						dwLength -= 2;
						pVCOMInfo->NewVCOMConnected = c;
						continue;
					}
				}
				*(ptr2++) = c;
			}
		}
	}

	else
		DeviceIoControl(
			pVCOMInfo->ComDev,IOCTL_SERIAL_GETDATA,NULL,0,lpszBlock,nMaxLength, &dwLength,NULL);

   return (dwLength);

}

static BOOL WriteCommBlock(int port, UCHAR * Message, DWORD MsgLen)
{
	ULONG bytesReturned;

//	if ((rand() % 100) > 80)
//		return 0;

	if (Win98)
		return DeviceIoControl(
			VCOMInfo[port]->ComDev,(VCOMInfo[port]->Port << 16) | W98_SERIAL_SETDATA,Message,MsgLen,NULL,0, &bytesReturned,NULL);

	else if (VCOMInfo[port]->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(VCOMInfo[port]->ComDev, NewMessage, MsgLen, &bytesReturned, NULL);
	}
	else
		return DeviceIoControl(
			VCOMInfo[port]->ComDev,IOCTL_SERIAL_SETDATA,Message,MsgLen,NULL,0, &bytesReturned,NULL);

}