source: trunk/src/kernel32/comm.cpp@ 22018

Last change on this file since 22018 was 21302, checked in by ydario, 16 years ago

Kernel32 updates.

File size: 37.8 KB
Line 
1/* $Id: comm.cpp,v 1.9 2002-02-09 12:45:11 sandervl Exp $ */
2
3/*
4 * Comport functions
5 *
6 * Copyright 1998 Patrick Haller (?)
7 * Copyright 1998 Felix Maschek (?)
8 * Copyright 2000 Markus Montkowski
9 *
10 * Partly based on Wine code (BuildCommDCBAndTimeoutsA; misc\comm.c)
11 *
12 * Copyright 1996 Marcus Meissner, Erik Bos
13 *
14 * Project Odin Software License can be found in LICENSE.TXT
15 *
16 */
17#include <os2win.h>
18#include <odinwrap.h>
19#include <ctype.h>
20#include <string.h>
21#include <stdio.h>
22#include "winreg.h"
23#include "global.h"
24#include "winnt.h"
25#include "winerror.h"
26#include "winreg.h"
27
28#include "unicode.h"
29
30#include "handlemanager.h"
31#include "hmhandle.h"
32#include "hmcomm.h"
33
34
35#define DBG_LOCALLOG DBG_comm
36#include "dbglocal.h"
37
38ODINDEBUGCHANNEL(KERNEL32-COMM)
39
40static BOOL HMCommGetDefaultCommConfig( HANDLE hCommDev,
41 LPCOMMCONFIG lpCC,
42 LPDWORD lpdwSize);
43static BOOL HMCommSetDefaultCommConfig( HANDLE hCommDev,
44 LPCOMMCONFIG lpCC,
45 DWORD dwSize);
46
47/*****************************************************************************
48 * @returns True on success and fills the COMMCONFIG structure
49 * @param lpDef Pointer to device-control string
50 * @param lpDCB Pointer to device-control buffer
51 * @remark
52 * @status untested
53 * @author Markus Montkowski
54 *****************************************************************************/
55
56BOOL WIN32API BuildCommDCBA( LPCSTR lpDef, LPDCB lpDCB )
57{
58 return BuildCommDCBAndTimeoutsA(lpDef,lpDCB,NULL);
59}
60
61//------------------------------------------------------------------------------
62
63BOOL WIN32API BuildCommDCBW( LPCWSTR lpDef, LPDCB lpDCB )
64{
65 char *asciiname;
66 BOOL rc;
67
68 asciiname = UnicodeToAsciiString((LPWSTR)lpDef);
69 rc = BuildCommDCBAndTimeoutsA(asciiname, lpDCB, NULL);
70 FreeAsciiString(asciiname);
71 return(rc);
72}
73
74//------------------------------------------------------------------------------
75
76BOOL ValidCOMPort(int Port)
77{
78 // @@@ Todo check in the handlemanager for a registered COM DeviceHandle for this number
79 // We currently only do static add COM1-COM8 so we simply check for 8
80 return (Port <=8);
81}
82
83/*****************************************************************************
84 * @returns True on success and fills the COMMCONFIG structure
85 * @param lpDef Pointer to device-control string
86 * @param lpDCB Pointer to device-control buffer
87 * @param lpCommTimeouts Pointer to COMMTIMEOUTS
88 * @remark
89 * @status partly implemented
90 * @author Markus Montkowski
91 *****************************************************************************/
92
93
94BOOL WIN32API BuildCommDCBAndTimeoutsA( LPCSTR lpDef, LPDCB lpDCB, LPCOMMTIMEOUTS lpCommTimeouts )
95{
96 int port,i;
97 char szNumber[4];
98 char *ptr,*temp;
99
100 dprintf(("(%s,%p,%p)\n",lpDef,lpDCB,lpCommTimeouts));
101
102 port = -1;
103
104 if (!strnicmp(lpDef,"COM",3))
105 {
106
107 for(i=0;((lpDef[3+i]!=':') && (i<3));i++)
108 szNumber[i] = lpDef[3+i];
109 szNumber[i] = 0;
110
111 port = atoi(szNumber);
112 if (port==0 || lpDef[i]!=':')
113 {
114 SetLastError(ERROR_INVALID_PARAMETER);
115 return FALSE;
116 }
117
118 if (!ValidCOMPort(port))
119 {
120 SetLastError(ERROR_FILE_NOT_FOUND);
121 return FALSE;
122 }
123 temp=(LPSTR)(lpDef+4+i);
124 }
125 else
126 temp=(LPSTR)lpDef;
127
128 lpDCB->DCBlength = sizeof(DCB);
129 if (strchr(temp,','))
130 {
131 // old style
132
133 char last=temp[strlen(temp)-1];
134
135 ptr = strtok(temp, ", ");
136
137
138 lpDCB->BaudRate = atoi(ptr);
139 dprintf(("baudrate (%d)\n", lpDCB->BaudRate));
140
141 ptr = strtok(NULL, ", ");
142 if (islower(*ptr))
143 *ptr = toupper(*ptr);
144
145 dprintf(("parity (%c)\n", *ptr));
146 lpDCB->fParity = TRUE;
147 switch (*ptr)
148 {
149 case 'N':
150 lpDCB->Parity = NOPARITY;
151 lpDCB->fParity = FALSE;
152 break;
153 case 'E':
154 lpDCB->Parity = EVENPARITY;
155 break;
156 case 'M':
157 lpDCB->Parity = MARKPARITY;
158 break;
159 case 'O':
160 lpDCB->Parity = ODDPARITY;
161 break;
162 default:
163 SetLastError(ERROR_INVALID_PARAMETER);
164 dprintf(("Unknown parity `%c'!\n", *ptr));
165 return FALSE;
166 }
167
168 ptr = strtok(NULL, ", ");
169 dprintf(("charsize (%c)\n", *ptr));
170 lpDCB->ByteSize = *ptr - '0';
171
172 if((lpDCB->ByteSize<5) ||
173 (lpDCB->ByteSize>8))
174 {
175 SetLastError(ERROR_INVALID_PARAMETER);
176 dprintf(("Unsupported bytesize `%d'!\n", lpDCB->ByteSize));
177 return FALSE;
178 }
179
180 ptr = strtok(NULL, ", ");
181 dprintf(("stopbits (%c%c%c)\n", *ptr,*(ptr+1)=='.'?'.':' ',*(ptr+1)=='.'?*(ptr+2):' '));
182 switch (*ptr)
183 {
184 case '1':
185 if(*(ptr+1)=='.')
186 {
187 if(*(ptr+2)=='5')
188 lpDCB->StopBits = ONE5STOPBITS;
189 else
190 {
191 SetLastError(ERROR_INVALID_PARAMETER);
192 dprintf(("Unsupported # of stopbits !\n"));
193 return FALSE;
194 }
195 }
196 else
197 lpDCB->StopBits = ONESTOPBIT;
198 break;
199 case '2':
200 lpDCB->StopBits = TWOSTOPBITS;
201 break;
202 default:
203 SetLastError(ERROR_INVALID_PARAMETER);
204 dprintf(("Unknown # of stopbits `%c'!\n", *ptr));
205 return FALSE;
206 }
207
208 if(lpDCB->BaudRate==110)
209 lpDCB->StopBits =2;
210
211 if(((lpDCB->ByteSize==5)&&(lpDCB->StopBits==TWOSTOPBITS))||
212 ((lpDCB->ByteSize!=5)&&(lpDCB->StopBits==ONE5STOPBITS)) )
213 {
214 dprintf(("Unsupported Combination of Bytesize `%d' and StopBits %s!\n",
215 lpDCB->ByteSize,lpDCB->StopBits==ONE5STOPBITS?"1.5":"2"));
216 SetLastError(ERROR_INVALID_PARAMETER);
217 return FALSE;
218 }
219
220 lpDCB->fBinary = TRUE;
221 lpDCB->fNull = FALSE;
222
223 if (last == 'x')
224 {
225 lpDCB->fInX = TRUE;
226 lpDCB->fOutX = TRUE;
227 lpDCB->fOutxCtsFlow = FALSE;
228 lpDCB->fOutxDsrFlow = FALSE;
229 lpDCB->fDtrControl = DTR_CONTROL_ENABLE;
230 lpDCB->fRtsControl = RTS_CONTROL_ENABLE;
231 }
232 else
233 if (last=='p')
234 {
235 lpDCB->fInX = FALSE;
236 lpDCB->fOutX = FALSE;
237 lpDCB->fOutxCtsFlow = TRUE;
238 lpDCB->fOutxDsrFlow = TRUE;
239 lpDCB->fDtrControl = DTR_CONTROL_HANDSHAKE;
240 lpDCB->fRtsControl = RTS_CONTROL_HANDSHAKE;
241 }
242 else
243 {
244 lpDCB->fInX = FALSE;
245 lpDCB->fOutX = FALSE;
246 lpDCB->fOutxCtsFlow = FALSE;
247 lpDCB->fOutxDsrFlow = FALSE;
248 lpDCB->fDtrControl = DTR_CONTROL_ENABLE;
249 lpDCB->fRtsControl = RTS_CONTROL_ENABLE;
250 }
251 lpDCB->XonChar = 0;
252 lpDCB->XoffChar = 0;
253 lpDCB->ErrorChar = 0;
254 lpDCB->fErrorChar = 0;
255 lpDCB->EofChar = 0;
256 lpDCB->EvtChar = 0;
257 lpDCB->XonLim = 0;
258 lpDCB->XoffLim = 0;
259 return TRUE;
260 }
261
262 ptr=strtok(temp," ");
263 while (ptr)
264 {
265 DWORD flag,x;
266
267 flag=0;
268 if (!strnicmp("baud=",ptr,5))
269 {
270 if (!sscanf(ptr+5,"%ld",&x))
271 {
272 dprintf(("Couldn't parse %s\n",ptr));
273 SetLastError(ERROR_INVALID_PARAMETER);
274 return FALSE;
275 }
276 lpDCB->BaudRate = x;
277 flag=1;
278 }
279 if (!strnicmp("stop=",ptr,5))
280 {
281 if (!sscanf(ptr+5,"%ld",&x))
282 {
283 dprintf(("Couldn't parse %s\n",ptr));
284 SetLastError(ERROR_INVALID_PARAMETER);
285 return FALSE;
286 }
287 lpDCB->StopBits = x;
288 flag=1;
289 }
290 if (!strnicmp("data=",ptr,5))
291 {
292 if (!sscanf(ptr+5,"%ld",&x))
293 {
294 dprintf(("Couldn't parse %s\n",ptr));
295 SetLastError(ERROR_INVALID_PARAMETER);
296 return FALSE;
297 }
298 lpDCB->ByteSize = x;
299 flag=1;
300 }
301 if (!strnicmp("parity=",ptr,7))
302 {
303 lpDCB->fParity = TRUE;
304 switch (ptr[8])
305 {
306 case 'N':case 'n':
307 lpDCB->fParity = FALSE;
308 lpDCB->Parity = NOPARITY;
309 break;
310 case 'E':case 'e':
311 lpDCB->Parity = EVENPARITY;
312 break;
313 case 'O':case 'o':
314 lpDCB->Parity = ODDPARITY;
315 break;
316 case 'M':case 'm':
317 lpDCB->Parity = MARKPARITY;
318 break;
319 }
320 flag=1;
321 }
322 if (!strnicmp("to=",ptr,3))
323 {
324 if (!strnicmp("on",ptr+3,2))
325 {
326 if(NULL==lpCommTimeouts)
327 {
328 dprintf(("TO=ON and no lpCommTimeout"));
329 SetLastError(ERROR_INVALID_PARAMETER);
330 return FALSE;
331 }
332 else
333 {
334 // @@@ Todo Implement timout handling
335 flag=1;
336 }
337 }
338 if (!strnicmp("off",ptr+3,3))
339 {
340 flag=1;
341 }
342 }
343
344 if (!flag)
345 {
346 dprintf(("Unhandled specifier '%s', please report.\n",ptr));
347 SetLastError(ERROR_INVALID_PARAMETER);
348 return FALSE;
349 }
350 ptr=strtok(NULL," ");
351 }
352
353 if (lpDCB->BaudRate==110)
354 lpDCB->StopBits = 2;
355 return TRUE;
356}
357
358//------------------------------------------------------------------------------
359
360BOOL WIN32API BuildCommDCBAndTimeoutsW( LPCWSTR lpDef, LPDCB lpDCB, LPCOMMTIMEOUTS lpCommTimeouts )
361{
362 char *asciiname;
363 BOOL rc;
364
365 asciiname = UnicodeToAsciiString((LPWSTR)lpDef);
366 rc = BuildCommDCBAndTimeoutsA(asciiname, lpDCB, lpCommTimeouts);
367 FreeAsciiString(asciiname);
368 return(rc);
369}
370
371//------------------------------------------------------------------------------
372
373typedef BOOL (* WIN32API COMMDLGFUNC)(LPCSTR, HWND, LPCOMMCONFIG );
374
375BOOL WIN32API CommConfigDialogA( LPCSTR lpszName, HWND hWnd, LPCOMMCONFIG lpCC )
376{
377 COMMDLGFUNC lpfnCommDialog;
378 HMODULE hConfigModule;
379 char szSerialUI[MAX_PATH+1];
380 char szKeyname[5];
381 BOOL r;
382 HKEY hkPorts, hkName;
383 LONG rc;
384 DWORD dwType,dwSize;
385 int port;
386
387 dprintf(("CommConfigDialogA (%p %x %p)\n",lpszName, hWnd, lpCC));
388
389 if( strnicmp(lpszName,"com",3) ||
390 strlen(lpszName)<4 ||
391 strlen(lpszName)>7)
392 {
393 SetLastError(ERROR_INVALID_PARAMETER);
394 return FALSE;
395 }
396 port = atoi(lpszName+3);
397 if( (0==port) ||(port>9999))
398 {
399 SetLastError(ERROR_INVALID_PARAMETER);
400 return FALSE;
401 }
402 port--;
403
404 sprintf(szKeyname,"%04d",port);
405 dprintf(("CommConfigDialogA look in reg for port %s",szKeyname));
406 rc = RegOpenKeyExA( HKEY_LOCAL_MACHINE,
407 "System\\CurrentControlSet\\Services\\Class\\Ports",
408 0,
409 KEY_READ,
410 &hkPorts);
411
412 if(rc!=ERROR_SUCCESS)
413 {
414 SetLastError(ERROR_DEV_NOT_EXIST);
415 return FALSE;
416 }
417
418 rc = RegOpenKeyExA( hkPorts,
419 szKeyname,
420 0,
421 KEY_READ,
422 &hkName);
423
424 if(rc!=ERROR_SUCCESS)
425 {
426 SetLastError(ERROR_DEV_NOT_EXIST);
427 RegCloseKey(hkPorts);
428 return FALSE;
429 }
430
431 dwSize = sizeof(szSerialUI);
432
433 rc = RegQueryValueExA( hkName,
434 "ConfigDialog",
435 NULL,
436 &dwType,
437 (LPBYTE)szSerialUI,
438 &dwSize);
439
440 RegCloseKey(hkName);
441 RegCloseKey(hkPorts);
442 if( (rc!=ERROR_SUCCESS) && (dwType!=REG_SZ) )
443 {
444 SetLastError(ERROR_DEV_NOT_EXIST);
445 return FALSE;
446 }
447
448 hConfigModule = LoadLibraryA(szSerialUI);
449 if(!hConfigModule)
450 return FALSE;
451
452 lpfnCommDialog = (COMMDLGFUNC)GetProcAddress(hConfigModule, (LPCSTR)3L);
453
454 if(!lpfnCommDialog)
455 return FALSE;
456
457 SetLastError(ERROR_SUCCESS);
458
459 r = lpfnCommDialog(lpszName,hWnd,lpCC);
460
461 FreeLibrary(hConfigModule);
462
463 return r;
464}
465
466//------------------------------------------------------------------------------
467
468BOOL WIN32API CommConfigDialogW( LPCWSTR lpszName, HWND hWnd, LPCOMMCONFIG lpCC )
469{
470 char *asciiname;
471 BOOL rc;
472
473 asciiname = UnicodeToAsciiString((LPWSTR)lpszName);
474 rc = CommConfigDialogA(asciiname,hWnd,lpCC);
475 FreeAsciiString(asciiname);
476 return rc;
477}
478
479/*****************************************************************************
480 * @returns True on success and fills the COMMCONFIG structure
481 * @param lpszName Pointer to devicename
482 * @param lpCC Pointer to COMMCONFIG buffer.
483 * @param lpdwSize [in] Pointer to size of Buffer pointed to by lpCC
484 * [out] Number of bytes copied to the buffer
485 * [error] If buffer to small Number of bytes needed
486 * @remark
487 * @status untested
488 * @author Markus Montkowski
489 *****************************************************************************/
490
491BOOL WIN32API GetDefaultCommConfigA(LPCSTR lpszName, LPCOMMCONFIG lpCC,
492 LPDWORD lpdwSize)
493{
494 HFILE hCOM;
495 BOOL rc;
496
497 dprintf(("GetDefaultCommConfigA untested stub \n"));
498 SetLastError(ERROR_SUCCESS);
499
500 if(IsBadReadPtr(lpszName,5) ||
501 IsBadWritePtr(lpdwSize,sizeof(DWORD)) ||
502 IsBadWritePtr(lpCC,*lpdwSize) )
503 {
504 SetLastError(ERROR_INVALID_PARAMETER); /* set win32 error information */
505 return(FALSE);
506 }
507
508 if(strnicmp(lpszName,"COM",3) &&
509 strnicmp(lpszName,"\\COM",4) &&
510 strnicmp(lpszName,"\\dev\\COM",8) )
511 {
512 SetLastError(ERROR_FILE_NOT_FOUND); /* set win32 error information */
513 return(FALSE);
514 }
515
516 hCOM = HMCreateFile( lpszName,
517 GENERIC_READ | GENERIC_WRITE,
518 0,
519 NULL,
520 OPEN_EXISTING,
521 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
522 NULL);
523 if(0!=hCOM)
524 {
525 rc = HMCommGetDefaultCommConfig(hCOM, lpCC, lpdwSize);
526 HMCloseHandle(hCOM);
527 return(rc);
528 }
529 return(FALSE);
530}
531
532//------------------------------------------------------------------------------
533
534BOOL WIN32API GetDefaultCommConfigW( LPCWSTR lpszName, LPCOMMCONFIG lpCC, LPDWORD lpdwSize )
535{
536 char *asciiname;
537 BOOL rc;
538
539 asciiname = UnicodeToAsciiString((LPWSTR)lpszName);
540 rc = GetDefaultCommConfigA(asciiname, lpCC, lpdwSize);
541 FreeAsciiString(asciiname);
542 return(rc);
543}
544
545/*****************************************************************************
546 * @returns True on Success
547 * @param lpszName Pointer to devicename
548 * @param lpCC Pointer to COMMCONFIG buffer.
549 * @param dwSize Size of Buffer pointed to by lpCC
550 * @remark
551 * @status untested
552 * @author Markus Montkowski
553 *****************************************************************************/
554
555BOOL SetDefaultCommConfigA(LPCSTR lpszName, LPCOMMCONFIG lpCC, DWORD dwSize )
556{
557 HFILE hCOM;
558 BOOL rc;
559
560 dprintf(("SetDefaultCommConfigA untested stub \n"));
561 SetLastError(ERROR_INVALID_PARAMETER); /* set win32 error information */
562 rc = FALSE;
563
564 if(!IsBadReadPtr(lpszName,5) &&
565 !IsBadWritePtr(lpCC,dwSize)&&
566 lpCC->dwSize== dwSize )
567 {
568 switch(lpCC->dwProviderSubType)
569 {
570 case PST_RS232:
571 if(strnicmp(lpszName,"COM",3) &&
572 strnicmp(lpszName,"\\COM",4) &&
573 strnicmp(lpszName,"\\dev\\COM",8) )
574 {
575 SetLastError(ERROR_FILE_NOT_FOUND); /* set win32 error information */
576 return(FALSE);
577 }
578
579 SetLastError(ERROR_SUCCESS);
580
581 hCOM = HMCreateFile( lpszName,
582 GENERIC_READ | GENERIC_WRITE,
583 0,
584 NULL,
585 OPEN_EXISTING,
586 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
587 NULL);
588 if(0!=hCOM)
589 {
590 rc = HMCommSetDefaultCommConfig(hCOM, lpCC, dwSize);
591 HMCloseHandle(hCOM);
592 return(rc);
593 }
594 break;
595 case PST_PARALLELPORT:
596 case PST_MODEM:
597 default:
598 SetLastError(ERROR_FILE_NOT_FOUND);
599 dprintf(("SetDefaultCommConfigA: ProviderSubType &d Not implemented (FALSE)\n",lpCC->dwProviderSubType));
600 break;
601 }
602 }
603 return(rc);
604}
605
606//------------------------------------------------------------------------------
607
608BOOL WIN32API SetDefaultCommConfigW( LPCWSTR lpszName, LPCOMMCONFIG lpCC, DWORD dwSize )
609{
610 char *asciiname;
611 BOOL rc;
612
613 asciiname = UnicodeToAsciiString((LPWSTR)lpszName);
614 rc = SetDefaultCommConfigA(asciiname, lpCC, dwSize);
615 FreeAsciiString(asciiname);
616 return(rc);
617}
618
619/*****************************************************************************
620 * Name : HMCOMGetCommState
621 * Purpose : router function for GetCommState
622 * Parameters:
623 * Variables :
624 * Result :
625 * Remark :
626 * Status :
627 *
628 * Author : Achim Hasenmueller [Sat, 1999/11/27 13:40]
629 *****************************************************************************/
630
631BOOL WIN32API GetCommState(HANDLE hCommDev, LPDCB lpdcb)
632{
633 BOOL bResult; /* result from the device handler's API */
634 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
635
636 /* validate handle */
637 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
638 if (pHMHandle == NULL) /* error ? */
639 {
640 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
641 }
642
643 if(IsBadWritePtr(lpdcb,sizeof(DCB)))
644 {
645 SetLastError(ERROR_INVALID_PARAMETER);
646 return FALSE;
647 }
648
649 bResult = pHMHandle->pDeviceHandler->GetCommState(&pHMHandle->hmHandleData,
650 lpdcb);
651
652 return (bResult); /* deliver return code */
653}
654//******************************************************************************
655//******************************************************************************
656BOOL WIN32API WaitCommEvent( HANDLE hCommDev,
657 LPDWORD lpfdwEvtMask,
658 LPOVERLAPPED lpo)
659{
660 BOOL bResult; /* result from the device handler's API */
661 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
662
663 /* validate handle */
664 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
665 if (pHMHandle == NULL) /* error ? */
666 {
667 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
668 }
669
670 if(NULL!=lpo && IsBadReadPtr(lpo,sizeof(OVERLAPPED)) )
671 {
672 SetLastError(ERROR_INVALID_PARAMETER);
673 return FALSE;
674 }
675
676 bResult = pHMHandle->pDeviceHandler->WaitCommEvent( &pHMHandle->hmHandleData,
677 lpfdwEvtMask,
678 lpo);
679
680 return (bResult); /* deliver return code */
681}
682//******************************************************************************
683//******************************************************************************
684BOOL WIN32API GetCommProperties( HANDLE hCommDev,
685 LPCOMMPROP lpcmmp)
686{
687 BOOL bResult; /* result from the device handler's API */
688 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
689
690 /* validate handle */
691 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
692 if (pHMHandle == NULL) /* error ? */
693 {
694 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
695 }
696
697 if(IsBadWritePtr(lpcmmp,sizeof(COMMPROP)) )
698 {
699 SetLastError(ERROR_INVALID_PARAMETER);
700 return FALSE;
701 }
702
703 bResult = pHMHandle->pDeviceHandler->GetCommProperties( &pHMHandle->hmHandleData,
704 lpcmmp);
705
706 return (bResult); /* deliver return code */
707}
708//******************************************************************************
709//******************************************************************************
710BOOL WIN32API GetCommMask( HANDLE hCommDev,
711 LPDWORD lpfdwEvtMask)
712{
713 BOOL bResult; /* result from the device handler's API */
714 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
715
716 /* validate handle */
717 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
718 if (pHMHandle == NULL) /* error ? */
719 {
720 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
721 }
722
723 if(IsBadWritePtr(lpfdwEvtMask,sizeof(DWORD)) )
724 {
725 SetLastError(ERROR_INVALID_PARAMETER);
726 return FALSE;
727 }
728
729 bResult = pHMHandle->pDeviceHandler->GetCommMask( &pHMHandle->hmHandleData,
730 lpfdwEvtMask);
731
732 return (bResult); /* deliver return code */
733}
734//******************************************************************************
735//******************************************************************************
736BOOL WIN32API SetCommMask( HANDLE hCommDev,
737 DWORD fdwEvtMask)
738{
739 BOOL bResult; /* result from the device handler's API */
740 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
741
742 /* validate handle */
743 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
744 if (pHMHandle == NULL) /* error ? */
745 {
746 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
747 }
748
749 bResult = pHMHandle->pDeviceHandler->SetCommMask( &pHMHandle->hmHandleData,
750 fdwEvtMask);
751
752 return (bResult); /* deliver return code */
753}
754//******************************************************************************
755//******************************************************************************
756BOOL WIN32API PurgeComm( HANDLE hCommDev,
757 DWORD fdwAction)
758{
759 BOOL bResult; /* result from the device handler's API */
760 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
761
762 /* validate handle */
763 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
764 if (pHMHandle == NULL) /* error ? */
765 {
766 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
767 }
768
769 bResult = pHMHandle->pDeviceHandler->PurgeComm( &pHMHandle->hmHandleData,
770 fdwAction);
771
772 return (bResult); /* deliver return code */
773}
774//******************************************************************************
775//******************************************************************************
776BOOL WIN32API ClearCommError( HANDLE hCommDev,
777 LPDWORD lpdwErrors,
778 LPCOMSTAT lpcst)
779{
780 BOOL bResult; /* result from the device handler's API */
781 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
782
783 /* validate handle */
784 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
785 if (pHMHandle == NULL) /* error ? */
786 {
787 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
788 }
789
790 if((lpdwErrors != NULL && IsBadWritePtr(lpdwErrors,sizeof(DWORD))) ||
791 (NULL!=lpcst && IsBadWritePtr(lpcst,sizeof(COMSTAT)) ) )
792 {
793 SetLastError(ERROR_INVALID_PARAMETER);
794 return FALSE;
795 }
796
797 bResult = pHMHandle->pDeviceHandler->ClearCommError(&pHMHandle->hmHandleData,
798 lpdwErrors,
799 lpcst);
800
801 return (bResult); /* deliver return code */
802}
803//******************************************************************************
804//******************************************************************************
805BOOL WIN32API SetCommState( HANDLE hCommDev,
806 LPDCB lpdcb)
807{
808 BOOL bResult; /* result from the device handler's API */
809 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
810
811 /* validate handle */
812 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
813 if (pHMHandle == NULL) /* error ? */
814 {
815 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
816 }
817
818 if(IsBadReadPtr(lpdcb,sizeof(DCB)) )
819 {
820 SetLastError(ERROR_INVALID_PARAMETER);
821 return FALSE;
822 }
823
824 bResult = pHMHandle->pDeviceHandler->SetCommState(&pHMHandle->hmHandleData,
825 lpdcb);
826
827 return (bResult); /* deliver return code */
828}
829//******************************************************************************
830//******************************************************************************
831BOOL WIN32API GetCommTimeouts( HANDLE hCommDev,
832 LPCOMMTIMEOUTS lpctmo)
833{
834 BOOL bResult; /* result from the device handler's API */
835 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
836
837 /* validate handle */
838 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
839 if (pHMHandle == NULL) /* error ? */
840 {
841 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
842 }
843
844 if(IsBadWritePtr(lpctmo,sizeof(COMMTIMEOUTS)) )
845 {
846 SetLastError(ERROR_INVALID_PARAMETER);
847 return FALSE;
848 }
849
850 bResult = pHMHandle->pDeviceHandler->GetCommTimeouts( &pHMHandle->hmHandleData,
851 lpctmo);
852
853 return (bResult); /* deliver return code */
854}
855//******************************************************************************
856//******************************************************************************
857BOOL WIN32API GetCommModemStatus( HANDLE hCommDev,
858 LPDWORD lpModemStat )
859{
860 BOOL bResult; /* result from the device handler's API */
861 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
862
863 /* validate handle */
864 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
865 if (pHMHandle == NULL) /* error ? */
866 {
867 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
868 }
869
870 if(IsBadWritePtr(lpModemStat,sizeof(DWORD)) )
871 {
872 SetLastError(ERROR_INVALID_PARAMETER);
873 return FALSE;
874 }
875
876 bResult = pHMHandle->pDeviceHandler->GetCommModemStatus( &pHMHandle->hmHandleData,
877 lpModemStat);
878
879 return (bResult); /* deliver return code */
880}
881//******************************************************************************
882//******************************************************************************
883BOOL WIN32API SetCommTimeouts( HANDLE hCommDev,
884 LPCOMMTIMEOUTS lpctmo)
885{
886 BOOL bResult; /* result from the device handler's API */
887 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
888
889 /* validate handle */
890 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
891 if (pHMHandle == NULL) /* error ? */
892 {
893 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
894 }
895
896 if(IsBadReadPtr(lpctmo,sizeof(COMMTIMEOUTS)) )
897 {
898 SetLastError(ERROR_INVALID_PARAMETER);
899 return FALSE;
900 }
901
902 bResult = pHMHandle->pDeviceHandler->SetCommTimeouts( &pHMHandle->hmHandleData,
903 lpctmo);
904
905 return (bResult); /* deliver return code */
906}
907//******************************************************************************
908//******************************************************************************
909BOOL WIN32API TransmitCommChar( HANDLE hCommDev,
910 CHAR cChar )
911{
912 BOOL bResult; /* result from the device handler's API */
913 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
914
915 /* validate handle */
916 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
917 if (pHMHandle == NULL) /* error ? */
918 {
919 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
920 }
921
922 bResult = pHMHandle->pDeviceHandler->TransmitCommChar( &pHMHandle->hmHandleData,
923 cChar);
924
925 return (bResult); /* deliver return code */
926}
927//******************************************************************************
928//******************************************************************************
929BOOL WIN32API SetCommConfig( HANDLE hCommDev,
930 LPCOMMCONFIG lpCC,
931 DWORD dwSize )
932{
933 BOOL bResult; /* result from the device handler's API */
934 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
935
936 /* validate handle */
937 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
938 if (pHMHandle == NULL) /* error ? */
939 {
940 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
941 }
942
943 if( IsBadReadPtr(lpCC,sizeof(COMMCONFIG)) ||
944 dwSize < sizeof(COMMCONFIG) )
945 {
946 SetLastError(ERROR_INVALID_PARAMETER);
947 return FALSE;
948 }
949
950 bResult = pHMHandle->pDeviceHandler->SetCommConfig( &pHMHandle->hmHandleData,
951 lpCC,
952 dwSize);
953
954 return (bResult); /* deliver return code */
955}
956//******************************************************************************
957//******************************************************************************
958BOOL WIN32API SetCommBreak( HANDLE hCommDev )
959{
960 BOOL bResult; /* result from the device handler's API */
961 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
962
963 /* validate handle */
964 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
965 if (pHMHandle == NULL) /* error ? */
966 {
967 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
968 }
969
970 bResult = pHMHandle->pDeviceHandler->SetCommBreak( &pHMHandle->hmHandleData);
971
972 return (bResult); /* deliver return code */
973}
974//******************************************************************************
975//******************************************************************************
976BOOL WIN32API GetCommConfig( HANDLE hCommDev,
977 LPCOMMCONFIG lpCC,
978 LPDWORD lpdwSize )
979{
980 BOOL bResult; /* result from the device handler's API */
981 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
982
983 /* validate handle */
984 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
985 if (pHMHandle == NULL) /* error ? */
986 {
987 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
988 }
989
990 if(IsBadWritePtr(lpdwSize,sizeof(DWORD)) )
991 {
992 SetLastError(ERROR_INVALID_PARAMETER);
993 return FALSE;
994 }
995
996 if( IsBadWritePtr(lpCC,sizeof(COMMCONFIG)) ||
997 *lpdwSize< sizeof(COMMCONFIG) )
998 {
999 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1000 *lpdwSize= sizeof(COMMCONFIG);
1001 return FALSE;
1002 }
1003
1004 bResult = pHMHandle->pDeviceHandler->GetCommConfig( &pHMHandle->hmHandleData,
1005 lpCC,
1006 lpdwSize);
1007
1008 return (bResult); /* deliver return code */
1009}
1010//******************************************************************************
1011//******************************************************************************
1012BOOL WIN32API EscapeCommFunction( HANDLE hCommDev,
1013 UINT dwFunc )
1014{
1015 BOOL bResult; /* result from the device handler's API */
1016 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1017
1018 /* validate handle */
1019 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
1020 if (pHMHandle == NULL) /* error ? */
1021 {
1022 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
1023 }
1024
1025 switch(dwFunc)
1026 {
1027 case CLRDTR:
1028 case CLRRTS:
1029 case SETDTR:
1030 case SETRTS:
1031 case SETXOFF:
1032 case SETXON:
1033 bResult = pHMHandle->pDeviceHandler->EscapeCommFunction( &pHMHandle->hmHandleData,
1034 dwFunc);
1035 break;
1036 case SETBREAK:
1037 bResult = pHMHandle->pDeviceHandler->SetCommBreak(&pHMHandle->hmHandleData);
1038 break;
1039 case CLRBREAK:
1040 bResult = pHMHandle->pDeviceHandler->ClearCommBreak(&pHMHandle->hmHandleData);
1041 break;
1042 default:
1043 SetLastError(ERROR_INVALID_PARAMETER);
1044 bResult = FALSE;
1045 }
1046
1047
1048 return (bResult); /* deliver return code */
1049}
1050//******************************************************************************
1051//******************************************************************************
1052BOOL WIN32API SetupComm( HANDLE hCommDev,
1053 DWORD dwInQueue,
1054 DWORD dwOutQueue)
1055{
1056 BOOL bResult; /* result from the device handler's API */
1057 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1058
1059 /* validate handle */
1060 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
1061 if (pHMHandle == NULL) /* error ? */
1062 {
1063 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
1064 }
1065
1066 bResult = pHMHandle->pDeviceHandler->SetupComm(&pHMHandle->hmHandleData,
1067 dwInQueue,
1068 dwOutQueue);
1069
1070 return (bResult); /* deliver return code */
1071}
1072//******************************************************************************
1073//******************************************************************************
1074BOOL WIN32API ClearCommBreak(HANDLE hCommDev)
1075{
1076 BOOL bResult; /* result from the device handler's API */
1077 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1078
1079 /* validate handle */
1080 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
1081 if (pHMHandle == NULL) /* error ? */
1082 {
1083 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
1084 }
1085
1086 bResult = pHMHandle->pDeviceHandler->ClearCommBreak(&pHMHandle->hmHandleData);
1087
1088 return (bResult); /* deliver return code */
1089}
1090//******************************************************************************
1091//******************************************************************************
1092static BOOL HMCommSetDefaultCommConfig( HANDLE hCommDev,
1093 LPCOMMCONFIG lpCC,
1094 DWORD dwSize)
1095{
1096 BOOL bResult; /* result from the device handler's API */
1097 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1098
1099 /* validate handle */
1100 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
1101 if (pHMHandle == NULL) /* error ? */
1102 {
1103 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
1104 }
1105
1106 if( (lpCC!=NULL) &&
1107 ( IsBadReadPtr(lpCC,sizeof(COMMCONFIG)) ||
1108 dwSize != sizeof(COMMCONFIG) ) )
1109 {
1110 SetLastError(ERROR_INVALID_PARAMETER);
1111 return FALSE;
1112 }
1113
1114 bResult = pHMHandle->pDeviceHandler->SetDefaultCommConfig(&pHMHandle->hmHandleData,
1115 lpCC,
1116 dwSize);
1117
1118 return (bResult); /* deliver return code */
1119}
1120//******************************************************************************
1121//******************************************************************************
1122static BOOL HMCommGetDefaultCommConfig( HANDLE hCommDev,
1123 LPCOMMCONFIG lpCC,
1124 LPDWORD lpdwSize)
1125{
1126 BOOL bResult; /* result from the device handler's API */
1127 PHMHANDLE pHMHandle; /* pointer to the handle structure in the table */
1128
1129 /* validate handle */
1130 pHMHandle = HMHandleQueryPtr(hCommDev); /* get the index */
1131 if (pHMHandle == NULL) /* error ? */
1132 {
1133 return FALSE; //last error set by HMHandleQueryPtr (ERROR_INVALID_HANDLE)
1134 }
1135
1136 if(IsBadWritePtr(lpdwSize,sizeof(DWORD)))
1137 {
1138 SetLastError(ERROR_INVALID_PARAMETER);
1139 return FALSE;
1140 }
1141
1142 bResult = pHMHandle->pDeviceHandler->GetDefaultCommConfig( &pHMHandle->hmHandleData,
1143 lpCC,
1144 lpdwSize);
1145
1146 return (bResult); /* deliver return code */
1147}
1148//******************************************************************************
1149//******************************************************************************
Note: See TracBrowser for help on using the repository browser.