-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathINIT.C
582 lines (451 loc) · 16.4 KB
/
INIT.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
/*-----------------------------------------------------------------------------
This is a part of the Microsoft Source Code Samples.
Copyright (C) 1995 Microsoft Corporation.
All rights reserved.
This source code is only intended as a supplement to
Microsoft Development Tools and/or WinHelp documentation.
See these sources for detailed information regarding the
Microsoft samples programs.
MODULE: Init.c
PURPOSE: Intializes global data and comm port connects.
Closes comm ports and cleans up global data.
FUNCTIONS:
GlobalInitialize - Init global variables and system objects
GlobalCleanup - cleanup global variables and system objects
ClearTTYContents - Clears the tty buffer
InitNewFont - Creates a new font for the TTY child window
CreateTTYInfo - Creates the dynamic tty info structure controlling
behavior of tty
DestroyTTYInfo - deallocates tty info structure
StartThreads - Starts worker threads when a port is opened
SetupCommPort - Opens the port for the first time
WaitForThreads - Sets the thread exit event and wait for worker
threads to exit
BreakDownCommPort - Closes a connection to the comm port
DisconnectOK - Asks user if it is ok to disconnect
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <commctrl.h>
#include "mttty.h"
/*
Prototypes for functions called only within this file
*/
void StartThreads( void );
DWORD WaitForThreads( DWORD );
/*
TimeoutsDefault
We need ReadIntervalTimeout here to cause the read operations
that we do to actually timeout and become overlapped.
Specifying 1 here causes ReadFile to return very quickly
so that our reader thread will continue execution.
*/
COMMTIMEOUTS gTimeoutsDefault = { 0x01, 0, 0, 0, 0 };
/*-----------------------------------------------------------------------------
FUNCTION: GlobalInitialize
PURPOSE: Intializes global variables before any windows are created
COMMENTS: Partner to GlobalCleanup
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void GlobalInitialize()
{
int cyMenuHeight, cyCaptionHeight, cyFrameHeight;
//
// critical sections in status reporting & node management
//
InitializeCriticalSection(&gStatusCritical);
InitializeCriticalSection(&gcsWriterHeap);
InitializeCriticalSection(&gcsDataHeap);
//
// status message event
//
ghStatusMessageEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (ghStatusMessageEvent == NULL)
ErrorReporter(_T("CreateEvent (Status message event)"));
//
// thread exit event
//
ghThreadExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (ghThreadExitEvent == NULL)
ErrorReporter(_T("CreateEvent (Thread exit event)"));
//
// used in file transfer status bar
//
InitCommonControls();
//
// font for status reporting control
//
ghFontStatus = CreateStatusEditFont();
//
// the following are used for sizing the tty window and dialog windows
//
gwBaseY = HIWORD(GetDialogBaseUnits());
cyMenuHeight = GetSystemMetrics(SM_CYMENU);
cyCaptionHeight = GetSystemMetrics(SM_CYCAPTION);
cyFrameHeight = GetSystemMetrics(SM_CYFRAME);
gcyMinimumWindowHeight = cyMenuHeight + \
4 * cyCaptionHeight + \
2 * cyFrameHeight +
(SETTINGSFACTOR + STATUSFACTOR) * gwBaseY ;
return ;
}
/*-----------------------------------------------------------------------------
FUNCTION: GlobalCleanup
PURPOSE: Cleans up any global variables
COMMENTS: Partner to GlobalInitialize
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void GlobalCleanup()
{
DeleteCriticalSection(&gStatusCritical);
DeleteCriticalSection(&gcsWriterHeap);
DeleteCriticalSection(&gcsDataHeap);
DeleteObject(ghFontStatus);
CloseHandle(ghStatusMessageEvent);
CloseHandle(ghThreadExitEvent);
HeapDestroy(ghStatusMessageHeap);
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: ClearTTYContents
PURPOSE: Clears the tty buffer
RETURN: always TRUE
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
BOOL ClearTTYContents()
{
// not going to work with multibyte chars
// FillMemory(SCREEN(TTYInfo), MAXCOLS*MAXROWS, ' ');
int r, c;
for (r = 0; r < MAXROWS; r++) {
for (c = 0; c < MAXCOLS; c++) {
SCREENCHAR(TTYInfo, c, r) = ' ';
}
}
return TRUE;
}
/*-----------------------------------------------------------------------------
FUNCTION: InitNewFont(LOGFONT, COLORREF)
PURPOSE: Prepares a font for use in the TTY screen
PARAMETERS:
LogFont - New logical font for tty screen
rgbColor - New color for TTY painting
COMMENTS: Called when a new connection is made, or the TTY font
is changed by the user.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void InitNewFont(LOGFONT LogFont, COLORREF rgbColor)
{
TEXTMETRIC tm;
HDC hDC;
//
// if old one exists, then I should delete it
//
if (HTTYFONT(TTYInfo))
DeleteObject(HTTYFONT(TTYInfo));
LFTTYFONT(TTYInfo) = LogFont;
HTTYFONT(TTYInfo) = CreateFontIndirect(&(LFTTYFONT(TTYInfo)));
FGCOLOR(TTYInfo) = rgbColor;
hDC = GetDC( ghwndMain ) ;
SelectObject( hDC, HTTYFONT( TTYInfo ) ) ;
GetTextMetrics( hDC, &tm ) ;
ReleaseDC( ghwndMain, hDC ) ;
//
// character width and height
//
XCHAR( TTYInfo ) = tm.tmAveCharWidth ;
YCHAR( TTYInfo ) = tm.tmHeight + tm.tmExternalLeading ;
XOFFSET( TTYInfo ) = 0 ;
YOFFSET( TTYInfo ) = YCHAR(TTYInfo) * ROW(TTYInfo);
}
/*-----------------------------------------------------------------------------
FUNCTION: InitTTYInfo
PURPOSE: Initializes TTY structure
COMMENTS: This structure is a collection of TTY attributes
used by all parts of this program
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
2/14/96 AllenD Removed npTTYInfo
-----------------------------------------------------------------------------*/
BOOL InitTTYInfo()
{
//
// initialize generial TTY info
//
COMDEV( TTYInfo ) = NULL ;
CONNECTED( TTYInfo ) = FALSE ;
LOCALECHO( TTYInfo ) = FALSE ;
CURSORSTATE( TTYInfo ) = CS_HIDE ;
PORT( TTYInfo ) = 1 ;
BAUDRATE( TTYInfo ) = CBR_9600 ;
BYTESIZE( TTYInfo ) = 8 ;
PARITY( TTYInfo ) = NOPARITY ;
STOPBITS( TTYInfo ) = ONESTOPBIT ;
AUTOWRAP( TTYInfo ) = TRUE;
NEWLINE( TTYInfo ) = FALSE;
XSIZE( TTYInfo ) = 0 ;
YSIZE( TTYInfo ) = 0 ;
XSCROLL( TTYInfo ) = 0 ;
YSCROLL( TTYInfo ) = 0 ;
COLUMN( TTYInfo ) = 0 ;
ROW( TTYInfo ) = MAXROWS - 1 ;
DISPLAYERRORS( TTYInfo ) = TRUE ;
//
// timeouts
//
TIMEOUTSNEW( TTYInfo ) = gTimeoutsDefault;
//
// read state and status events
//
gdwReceiveState = RECEIVE_TTY;
EVENTFLAGS( TTYInfo ) = EVENTFLAGS_DEFAULT;
FLAGCHAR( TTYInfo ) = FLAGCHAR_DEFAULT;
//
// Flow Control Settings
//
DTRCONTROL( TTYInfo ) = DTR_CONTROL_ENABLE;
RTSCONTROL( TTYInfo ) = RTS_CONTROL_ENABLE;
XONCHAR( TTYInfo ) = ASCII_XON;
XOFFCHAR( TTYInfo ) = ASCII_XOFF;
XONLIMIT( TTYInfo ) = 0;
XOFFLIMIT( TTYInfo ) = 0;
CTSOUTFLOW( TTYInfo ) = FALSE;
DSROUTFLOW( TTYInfo ) = FALSE;
DSRINFLOW( TTYInfo ) = FALSE;
XONXOFFOUTFLOW(TTYInfo) = FALSE;
XONXOFFINFLOW(TTYInfo) = FALSE;
TXAFTERXOFFSENT(TTYInfo) = FALSE;
NOREADING(TTYInfo) = FALSE;
NOWRITING(TTYInfo) = FALSE;
NOEVENTS(TTYInfo) = FALSE;
NOSTATUS(TTYInfo) = FALSE;
SHOWTIMEOUTS(TTYInfo) = FALSE;
//
// setup default font information
//
LFTTYFONT( TTYInfo ).lfHeight = 12 ;
LFTTYFONT( TTYInfo ).lfWidth = 0 ;
LFTTYFONT( TTYInfo ).lfEscapement = 0 ;
LFTTYFONT( TTYInfo ).lfOrientation = 0 ;
LFTTYFONT( TTYInfo ).lfWeight = 0 ;
LFTTYFONT( TTYInfo ).lfItalic = 0 ;
LFTTYFONT( TTYInfo ).lfUnderline = 0 ;
LFTTYFONT( TTYInfo ).lfStrikeOut = 0 ;
LFTTYFONT( TTYInfo ).lfCharSet = OEM_CHARSET ;
LFTTYFONT( TTYInfo ).lfOutPrecision = OUT_DEFAULT_PRECIS ;
LFTTYFONT( TTYInfo ).lfClipPrecision = CLIP_DEFAULT_PRECIS ;
LFTTYFONT( TTYInfo ).lfQuality = DEFAULT_QUALITY ;
LFTTYFONT( TTYInfo ).lfPitchAndFamily = FIXED_PITCH | FF_MODERN ;
_tcscpy_s( LFTTYFONT( TTYInfo ).lfFaceName, 32, _T("FixedSys")) ;
InitNewFont( LFTTYFONT(TTYInfo), RGB(0,0,0));
ClearTTYContents();
return ( TRUE ) ;
}
/*-----------------------------------------------------------------------------
FUNCTION: DestroyTTYInfo
PURPOSE: Frees objects associated with the TTYInfo structure
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
2/14/96 AllenD Removed npTTYInfo
-----------------------------------------------------------------------------*/
void DestroyTTYInfo()
{
DeleteObject(HTTYFONT(TTYInfo));
}
/*-----------------------------------------------------------------------------
FUNCTION: StartThreads
PURPOSE: Creates the Reader/Status and Writer threads
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void StartThreads(void)
{
DWORD dwReadStatId;
DWORD dwWriterId;
READSTATTHREAD(TTYInfo) =
CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE) ReaderAndStatusProc,
(LPVOID) ghWndTTY,
0,
&dwReadStatId);
if (READSTATTHREAD(TTYInfo) == NULL)
ErrorInComm(_T("CreateThread(Reader/Status)"));
WRITERTHREAD(TTYInfo) =
CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE) WriterProc,
(LPVOID) NULL,
0,
&dwWriterId );
if (WRITERTHREAD(TTYInfo) == NULL)
ErrorInComm(_T("CreateThread (Writer)"));
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: SetupCommPort( void )
PURPOSE: Setup Communication Port with our settings
RETURN:
Handle of comm port is successful
NULL is error occurs
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
HANDLE SetupCommPort()
{
//
// get tty settings from settings dialog
//
UpdateTTYInfo();
//
// open communication port handle
//
COMDEV( TTYInfo ) = CreateFile( gszPort,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
0);
if (COMDEV(TTYInfo) == INVALID_HANDLE_VALUE) {
ErrorReporter(_T("CreateFile"));
return NULL;
}
//
// Save original comm timeouts and set new ones
//
if (!GetCommTimeouts( COMDEV(TTYInfo), &(TIMEOUTSORIG(TTYInfo))))
ErrorReporter(_T("GetCommTimeouts"));
//
// Set port state
//
UpdateConnection();
//
// set comm buffer sizes
//
SetupComm(COMDEV(TTYInfo), MAX_READ_BUFFER, MAX_WRITE_BUFFER);
//
// raise DTR
//
if (!EscapeCommFunction(COMDEV(TTYInfo), SETDTR))
ErrorReporter(_T("EscapeCommFunction (SETDTR)"));
//
// start threads and set initial thread state to not done
//
StartThreads();
//
// set overall connect flag
//
CONNECTED( TTYInfo ) = TRUE ;
return COMDEV(TTYInfo);
}
/*-----------------------------------------------------------------------------
FUNCTION: WaitForThreads(DWORD)
PURPOSE: Waits a specified time for the worker threads to exit
PARAMETERS:
dwTimeout - milliseconds to wait until timeout
RETURN:
WAIT_OBJECT_0 - successful wait, threads are not running
WAIT_TIMEOUT - at least one thread is still running
WAIT_FAILED - failure in WaitForMultipleObjects
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
----------------------------------------------------------------------------*/
DWORD WaitForThreads(DWORD dwTimeout)
{
HANDLE hThreads[2];
DWORD dwRes;
hThreads[0] = READSTATTHREAD(TTYInfo);
hThreads[1] = WRITERTHREAD(TTYInfo);
//
// set thread exit event here
//
SetEvent(ghThreadExitEvent);
dwRes = WaitForMultipleObjects(2, hThreads, TRUE, dwTimeout);
switch(dwRes)
{
case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1:
dwRes = WAIT_OBJECT_0;
break;
case WAIT_TIMEOUT:
if (WaitForSingleObject(READSTATTHREAD(TTYInfo), 0) == WAIT_TIMEOUT)
OutputDebugString(_T("Reader/Status Thread didn't exit.\n\r"));
if (WaitForSingleObject(WRITERTHREAD(TTYInfo), 0) == WAIT_TIMEOUT)
OutputDebugString(_T("Writer Thread didn't exit.\n\r"));
break;
default:
ErrorReporter(_T("WaitForMultipleObjects"));
break;
}
//
// reset thread exit event here
//
ResetEvent(ghThreadExitEvent);
return dwRes;
}
/*-----------------------------------------------------------------------------
FUNCTION: BreakDownCommPort
PURPOSE: Closes a connection to a comm port
RETURN:
TRUE - successful breakdown of port
FALSE - port isn't connected
COMMENTS: Waits for threads to exit,
clears DTR, restores comm port timeouts, purges any i/o
and closes all pertinent handles
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
BOOL BreakDownCommPort()
{
if (!CONNECTED(TTYInfo))
return FALSE;
CONNECTED( TTYInfo ) = FALSE;
//
// wait for the threads for a small period
//
if (WaitForThreads(20000) != WAIT_OBJECT_0)
/*
if threads haven't exited, then they will
interfere with a new connection. I must abort
the entire program.
*/
ErrorHandler(_T("Error closing port."));
//
// lower DTR
//
if (!EscapeCommFunction(COMDEV(TTYInfo), CLRDTR))
ErrorReporter(_T("EscapeCommFunction(CLRDTR)"));
//
// restore original comm timeouts
//
if (!SetCommTimeouts(COMDEV(TTYInfo), &(TIMEOUTSORIG(TTYInfo))))
ErrorReporter(_T("SetCommTimeouts (Restoration to original)"));
//
// Purge reads/writes, input buffer and output buffer
//
if (!PurgeComm(COMDEV(TTYInfo), PURGE_FLAGS))
ErrorReporter(_T("PurgeComm"));
CloseHandle(COMDEV(TTYInfo));
CloseHandle(READSTATTHREAD(TTYInfo));
CloseHandle(WRITERTHREAD(TTYInfo));
return TRUE;
}
/*-----------------------------------------------------------------------------
FUNCTION: DisconnectOK
PURPOSE: Asks user if it is OK to disconnect
RETURN:
TRUE - OK to disconnect
FALSE - Disconnect not OK
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
BOOL DisconnectOK()
{
if (!CONNECTED(TTYInfo))
return TRUE;
return ((MessageBox(ghwndMain, _T("OK to Disconnect?"), gszPort, MB_YESNO)) == IDYES);
}