2 * DEC 93 Erik Bos <erik@xs4all.nl>
4 * Copyright 1996 Marcus Meissner
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
34 #include "ddk/ntddser.h"
36 #include "wine/server.h"
37 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(comm);
43 /***********************************************************************
44 * COMM_Parse* (Internal)
46 * The following COMM_Parse* functions are used by the BuildCommDCB
47 * functions to help parse the various parts of the device control string.
49 static LPCWSTR COMM_ParseStart(LPCWSTR ptr)
51 static const WCHAR comW[] = {'C','O','M',0};
53 /* The device control string may optionally start with "COMx" followed
54 by an optional ':' and spaces. */
55 if(!strncmpiW(ptr, comW, 3))
59 /* Allow any com port above 0 as Win 9x does (NT only allows
60 values for com ports which are actually present) */
61 if(*ptr < '1' || *ptr > '9')
64 /* Advance pointer past port number */
65 while(*ptr >= '0' && *ptr <= '9') ptr++;
67 /* The com port number must be followed by a ':' or ' ' */
68 if(*ptr != ':' && *ptr != ' ')
71 /* Advance pointer to beginning of next parameter */
72 while(*ptr == ' ') ptr++;
76 while(*ptr == ' ') ptr++;
79 /* The device control string must not start with a space. */
86 static LPCWSTR COMM_ParseNumber(LPCWSTR ptr, LPDWORD lpnumber)
88 if(*ptr < '0' || *ptr > '9') return NULL;
89 *lpnumber = strtoulW(ptr, NULL, 10);
90 while(*ptr >= '0' && *ptr <= '9') ptr++;
94 static LPCWSTR COMM_ParseParity(LPCWSTR ptr, LPBYTE lpparity)
96 /* Contrary to what you might expect, Windows only sets the Parity
97 member of DCB and not fParity even when parity is specified in the
98 device control string */
100 switch(toupperW(*ptr++))
103 *lpparity = EVENPARITY;
106 *lpparity = MARKPARITY;
109 *lpparity = NOPARITY;
112 *lpparity = ODDPARITY;
115 *lpparity = SPACEPARITY;
124 static LPCWSTR COMM_ParseByteSize(LPCWSTR ptr, LPBYTE lpbytesize)
128 if(!(ptr = COMM_ParseNumber(ptr, &temp)))
131 if(temp >= 5 && temp <= 8)
140 static LPCWSTR COMM_ParseStopBits(LPCWSTR ptr, LPBYTE lpstopbits)
143 static const WCHAR stopbits15W[] = {'1','.','5',0};
145 if(!strncmpW(stopbits15W, ptr, 3))
148 *lpstopbits = ONE5STOPBITS;
152 if(!(ptr = COMM_ParseNumber(ptr, &temp)))
156 *lpstopbits = ONESTOPBIT;
158 *lpstopbits = TWOSTOPBITS;
166 static LPCWSTR COMM_ParseOnOff(LPCWSTR ptr, LPDWORD lponoff)
168 static const WCHAR onW[] = {'o','n',0};
169 static const WCHAR offW[] = {'o','f','f',0};
171 if(!strncmpiW(onW, ptr, 2))
176 else if(!strncmpiW(offW, ptr, 3))
187 /***********************************************************************
188 * COMM_BuildOldCommDCB (Internal)
190 * Build a DCB using the old style settings string eg: "96,n,8,1"
192 static BOOL COMM_BuildOldCommDCB(LPCWSTR device, LPDCB lpdcb)
196 if(!(device = COMM_ParseNumber(device, &lpdcb->BaudRate)))
199 switch(lpdcb->BaudRate)
204 lpdcb->BaudRate *= 10;
210 lpdcb->BaudRate *= 100;
213 lpdcb->BaudRate = 19200;
217 while(*device == ' ') device++;
218 if(*device++ != ',') return FALSE;
219 while(*device == ' ') device++;
221 if(!(device = COMM_ParseParity(device, &lpdcb->Parity)))
224 while(*device == ' ') device++;
225 if(*device++ != ',') return FALSE;
226 while(*device == ' ') device++;
228 if(!(device = COMM_ParseByteSize(device, &lpdcb->ByteSize)))
231 while(*device == ' ') device++;
232 if(*device++ != ',') return FALSE;
233 while(*device == ' ') device++;
235 if(!(device = COMM_ParseStopBits(device, &lpdcb->StopBits)))
238 /* The last parameter for flow control is optional. */
239 while(*device == ' ') device++;
243 while(*device == ' ') device++;
244 if(*device) last = toupperW(*device++);
245 while(*device == ' ') device++;
248 /* Win NT sets the flow control members based on (or lack of) the last
249 parameter. Win 9x does not set these members. */
254 lpdcb->fOutX = FALSE;
255 lpdcb->fOutxCtsFlow = FALSE;
256 lpdcb->fOutxDsrFlow = FALSE;
257 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
258 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
263 lpdcb->fOutxCtsFlow = FALSE;
264 lpdcb->fOutxDsrFlow = FALSE;
265 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
266 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
270 lpdcb->fOutX = FALSE;
271 lpdcb->fOutxCtsFlow = TRUE;
272 lpdcb->fOutxDsrFlow = TRUE;
273 lpdcb->fDtrControl = DTR_CONTROL_HANDSHAKE;
274 lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
280 /* This should be the end of the string. */
281 if(*device) return FALSE;
286 /***********************************************************************
287 * COMM_BuildNewCommDCB (Internal)
289 * Build a DCB using the new style settings string.
290 * eg: "baud=9600 parity=n data=8 stop=1 xon=on to=on"
292 static BOOL COMM_BuildNewCommDCB(LPCWSTR device, LPDCB lpdcb, LPCOMMTIMEOUTS lptimeouts)
295 BOOL baud = FALSE, stop = FALSE;
296 static const WCHAR baudW[] = {'b','a','u','d','=',0};
297 static const WCHAR parityW[] = {'p','a','r','i','t','y','=',0};
298 static const WCHAR dataW[] = {'d','a','t','a','=',0};
299 static const WCHAR stopW[] = {'s','t','o','p','=',0};
300 static const WCHAR toW[] = {'t','o','=',0};
301 static const WCHAR xonW[] = {'x','o','n','=',0};
302 static const WCHAR odsrW[] = {'o','d','s','r','=',0};
303 static const WCHAR octsW[] = {'o','c','t','s','=',0};
304 static const WCHAR dtrW[] = {'d','t','r','=',0};
305 static const WCHAR rtsW[] = {'r','t','s','=',0};
306 static const WCHAR idsrW[] = {'i','d','s','r','=',0};
310 while(*device == ' ') device++;
312 if(!strncmpiW(baudW, device, 5))
316 if(!(device = COMM_ParseNumber(device + 5, &lpdcb->BaudRate)))
319 else if(!strncmpiW(parityW, device, 7))
321 if(!(device = COMM_ParseParity(device + 7, &lpdcb->Parity)))
324 else if(!strncmpiW(dataW, device, 5))
326 if(!(device = COMM_ParseByteSize(device + 5, &lpdcb->ByteSize)))
329 else if(!strncmpiW(stopW, device, 5))
333 if(!(device = COMM_ParseStopBits(device + 5, &lpdcb->StopBits)))
336 else if(!strncmpiW(toW, device, 3))
338 if(!(device = COMM_ParseOnOff(device + 3, &temp)))
341 lptimeouts->ReadIntervalTimeout = 0;
342 lptimeouts->ReadTotalTimeoutMultiplier = 0;
343 lptimeouts->ReadTotalTimeoutConstant = 0;
344 lptimeouts->WriteTotalTimeoutMultiplier = 0;
345 lptimeouts->WriteTotalTimeoutConstant = temp ? 60000 : 0;
347 else if(!strncmpiW(xonW, device, 4))
349 if(!(device = COMM_ParseOnOff(device + 4, &temp)))
355 else if(!strncmpiW(odsrW, device, 5))
357 if(!(device = COMM_ParseOnOff(device + 5, &temp)))
360 lpdcb->fOutxDsrFlow = temp;
362 else if(!strncmpiW(octsW, device, 5))
364 if(!(device = COMM_ParseOnOff(device + 5, &temp)))
367 lpdcb->fOutxCtsFlow = temp;
369 else if(!strncmpiW(dtrW, device, 4))
371 if(!(device = COMM_ParseOnOff(device + 4, &temp)))
374 lpdcb->fDtrControl = temp;
376 else if(!strncmpiW(rtsW, device, 4))
378 if(!(device = COMM_ParseOnOff(device + 4, &temp)))
381 lpdcb->fRtsControl = temp;
383 else if(!strncmpiW(idsrW, device, 5))
385 if(!(device = COMM_ParseOnOff(device + 5, &temp)))
388 /* Win NT sets the fDsrSensitivity member based on the
389 idsr parameter. Win 9x sets fOutxDsrFlow instead. */
390 lpdcb->fDsrSensitivity = temp;
395 /* After the above parsing, the next character (if not the end of
396 the string) should be a space */
397 if(*device && *device != ' ')
401 /* If stop bits were not specified, a default is always supplied. */
404 if(baud && lpdcb->BaudRate == 110)
405 lpdcb->StopBits = TWOSTOPBITS;
407 lpdcb->StopBits = ONESTOPBIT;
413 /**************************************************************************
414 * BuildCommDCBA (KERNEL32.@)
416 * Updates a device control block data structure with values from an
417 * ascii device control string. The device control string has two forms
418 * normal and extended, it must be exclusively in one or the other form.
422 * True on success, false on a malformed control string.
424 BOOL WINAPI BuildCommDCBA(
425 LPCSTR device, /* [in] The ascii device control string used to update the DCB. */
426 LPDCB lpdcb) /* [out] The device control block to be updated. */
428 return BuildCommDCBAndTimeoutsA(device,lpdcb,NULL);
431 /**************************************************************************
432 * BuildCommDCBAndTimeoutsA (KERNEL32.@)
434 * Updates a device control block data structure with values from an
435 * ascii device control string. Taking timeout values from a timeouts
436 * struct if desired by the control string.
440 * True on success, false bad handles etc.
442 BOOL WINAPI BuildCommDCBAndTimeoutsA(
443 LPCSTR device, /* [in] The ascii device control string. */
444 LPDCB lpdcb, /* [out] The device control block to be updated. */
445 LPCOMMTIMEOUTS lptimeouts) /* [in] The COMMTIMEOUTS structure to be updated. */
448 UNICODE_STRING deviceW;
450 TRACE("(%s,%p,%p)\n",device,lpdcb,lptimeouts);
451 if(device) RtlCreateUnicodeStringFromAsciiz(&deviceW,device);
452 else deviceW.Buffer = NULL;
454 if(deviceW.Buffer) ret = BuildCommDCBAndTimeoutsW(deviceW.Buffer,lpdcb,lptimeouts);
456 RtlFreeUnicodeString(&deviceW);
460 /**************************************************************************
461 * BuildCommDCBAndTimeoutsW (KERNEL32.@)
463 * Updates a device control block data structure with values from a
464 * unicode device control string. Taking timeout values from a timeouts
465 * struct if desired by the control string.
469 * True on success, false bad handles etc
471 BOOL WINAPI BuildCommDCBAndTimeoutsW(
472 LPCWSTR devid, /* [in] The unicode device control string. */
473 LPDCB lpdcb, /* [out] The device control block to be updated. */
474 LPCOMMTIMEOUTS lptimeouts) /* [in] The COMMTIMEOUTS structure to be updated. */
477 COMMTIMEOUTS timeouts;
481 TRACE("(%s,%p,%p)\n",debugstr_w(devid),lpdcb,lptimeouts);
483 /* Set DCBlength. (Windows NT does not do this, but 9x does) */
484 lpdcb->DCBlength = sizeof(DCB);
486 /* Make a copy of the original data structures to work with since if
487 if there is an error in the device control string the originals
488 should not be modified (except possibly DCBlength) */
489 memcpy(&dcb, lpdcb, sizeof(DCB));
490 if(lptimeouts) memcpy(&timeouts, lptimeouts, sizeof(COMMTIMEOUTS));
492 ptr = COMM_ParseStart(ptr);
496 else if(strchrW(ptr, ','))
497 result = COMM_BuildOldCommDCB(ptr, &dcb);
499 result = COMM_BuildNewCommDCB(ptr, &dcb, &timeouts);
503 memcpy(lpdcb, &dcb, sizeof(DCB));
504 if(lptimeouts) memcpy(lptimeouts, &timeouts, sizeof(COMMTIMEOUTS));
509 WARN("Invalid device control string: %s\n", debugstr_w(devid));
510 SetLastError(ERROR_INVALID_PARAMETER);
515 /**************************************************************************
516 * BuildCommDCBW (KERNEL32.@)
518 * Updates a device control block structure with values from an
519 * unicode device control string. The device control string has two forms
520 * normal and extended, it must be exclusively in one or the other form.
524 * True on success, false on a malformed control string.
526 BOOL WINAPI BuildCommDCBW(
527 LPCWSTR devid, /* [in] The unicode device control string. */
528 LPDCB lpdcb) /* [out] The device control block to be updated. */
530 return BuildCommDCBAndTimeoutsW(devid,lpdcb,NULL);
533 /*****************************************************************************
534 * SetCommBreak (KERNEL32.@)
536 * Halts the transmission of characters to a communications device.
539 * handle [in] The communications device to suspend
543 * True on success, and false if the communications device could not be found,
544 * the control is not supported.
548 * Only TIOCSBRK and TIOCCBRK are supported.
550 BOOL WINAPI SetCommBreak(HANDLE handle)
552 return DeviceIoControl(handle, IOCTL_SERIAL_SET_BREAK_ON, NULL, 0, NULL, 0, NULL, NULL);
555 /*****************************************************************************
556 * ClearCommBreak (KERNEL32.@)
558 * Resumes character transmission from a communication device.
562 * handle [in] The halted communication device whose character transmission is to be resumed
566 * True on success and false if the communications device could not be found.
570 * Only TIOCSBRK and TIOCCBRK are supported.
572 BOOL WINAPI ClearCommBreak(HANDLE handle)
574 return DeviceIoControl(handle, IOCTL_SERIAL_SET_BREAK_OFF, NULL, 0, NULL, 0, NULL, NULL);
577 /*****************************************************************************
578 * EscapeCommFunction (KERNEL32.@)
580 * Directs a communication device to perform an extended function.
584 * handle [in] The communication device to perform the extended function
585 * nFunction [in] The extended function to be performed
589 * True or requested data on successful completion of the command,
590 * false if the device is not present cannot execute the command
591 * or the command failed.
593 BOOL WINAPI EscapeCommFunction(HANDLE handle, UINT func)
599 case CLRDTR: ioc = IOCTL_SERIAL_CLR_DTR; break;
600 case CLRRTS: ioc = IOCTL_SERIAL_CLR_RTS; break;
601 case SETDTR: ioc = IOCTL_SERIAL_SET_DTR; break;
602 case SETRTS: ioc = IOCTL_SERIAL_SET_RTS; break;
603 case SETXOFF: ioc = IOCTL_SERIAL_SET_XOFF; break;
604 case SETXON: ioc = IOCTL_SERIAL_SET_XON; break;
605 case SETBREAK: ioc = IOCTL_SERIAL_SET_BREAK_ON; break;
606 case CLRBREAK: ioc = IOCTL_SERIAL_SET_BREAK_OFF; break;
607 case RESETDEV: ioc = IOCTL_SERIAL_RESET_DEVICE; break;
609 ERR("Unknown function code (%u)\n", func);
610 SetLastError(ERROR_INVALID_PARAMETER);
613 return DeviceIoControl(handle, ioc, NULL, 0, NULL, 0, NULL, NULL);
616 /********************************************************************
617 * PurgeComm (KERNEL32.@)
619 * Terminates pending operations and/or discards buffers on a
620 * communication resource.
624 * handle [in] The communication resource to be purged
625 * flags [in] Flags for clear pending/buffer on input/output
629 * True on success and false if the communications handle is bad.
631 BOOL WINAPI PurgeComm(HANDLE handle, DWORD flags)
633 return DeviceIoControl(handle, IOCTL_SERIAL_PURGE, &flags, sizeof(flags),
634 NULL, 0, NULL, NULL);
637 /*****************************************************************************
638 * ClearCommError (KERNEL32.@)
640 * Enables further I/O operations on a communications resource after
641 * supplying error and current status information.
645 * handle [in] The communication resource with the error
646 * errors [out] Flags indicating error the resource experienced
647 * lpStat [out] The status of the communication resource
650 * True on success, false if the communication resource handle is bad.
652 BOOL WINAPI ClearCommError(HANDLE handle, LPDWORD errors, LPCOMSTAT lpStat)
656 if (!DeviceIoControl(handle, IOCTL_SERIAL_GET_COMMSTATUS, NULL, 0,
657 &ss, sizeof(ss), NULL, NULL))
663 if (ss.Errors & SERIAL_ERROR_BREAK) *errors |= CE_BREAK;
664 if (ss.Errors & SERIAL_ERROR_FRAMING) *errors |= CE_FRAME;
665 if (ss.Errors & SERIAL_ERROR_OVERRUN) *errors |= CE_OVERRUN;
666 if (ss.Errors & SERIAL_ERROR_QUEUEOVERRUN) *errors |= CE_RXOVER;
667 if (ss.Errors & SERIAL_ERROR_PARITY) *errors |= CE_RXPARITY;
672 memset(lpStat, 0, sizeof(*lpStat));
674 if (ss.HoldReasons & SERIAL_TX_WAITING_FOR_CTS) lpStat->fCtsHold = TRUE;
675 if (ss.HoldReasons & SERIAL_TX_WAITING_FOR_DSR) lpStat->fDsrHold = TRUE;
676 if (ss.HoldReasons & SERIAL_TX_WAITING_FOR_DCD) lpStat->fRlsdHold = TRUE;
677 if (ss.HoldReasons & SERIAL_TX_WAITING_FOR_XON) lpStat->fXoffHold = TRUE;
678 if (ss.HoldReasons & SERIAL_TX_WAITING_XOFF_SENT) lpStat->fXoffSent = TRUE;
679 if (ss.EofReceived) lpStat->fEof = TRUE;
680 if (ss.WaitForImmediate) lpStat->fTxim = TRUE;
681 lpStat->cbInQue = ss.AmountInInQueue;
682 lpStat->cbOutQue = ss.AmountInOutQueue;
687 /*****************************************************************************
688 * SetupComm (KERNEL32.@)
690 * Called after CreateFile to hint to the communication resource to use
691 * specified sizes for input and output buffers rather than the default values.
694 * handle [in] The just created communication resource handle
695 * insize [in] The suggested size of the communication resources input buffer in bytes
696 * outsize [in] The suggested size of the communication resources output buffer in bytes
700 * True if successful, false if the communications resource handle is bad.
706 BOOL WINAPI SetupComm(HANDLE handle, DWORD insize, DWORD outsize)
708 SERIAL_QUEUE_SIZE sqs;
711 sqs.OutSize = outsize;
712 return DeviceIoControl(handle, IOCTL_SERIAL_SET_QUEUE_SIZE,
713 &sqs, sizeof(sqs), NULL, 0, NULL, NULL);
716 /*****************************************************************************
717 * GetCommMask (KERNEL32.@)
719 * Obtain the events associated with a communication device that will cause
720 * a call WaitCommEvent to return.
724 * handle [in] The communications device
725 * evtmask [out] The events which cause WaitCommEvent to return
729 * True on success, fail on bad device handle etc.
731 BOOL WINAPI GetCommMask(HANDLE handle, LPDWORD evtmask)
733 TRACE("handle %p, mask %p\n", handle, evtmask);
734 return DeviceIoControl(handle, IOCTL_SERIAL_GET_WAIT_MASK,
735 NULL, 0, evtmask, sizeof(*evtmask), NULL, NULL);
738 /*****************************************************************************
739 * SetCommMask (KERNEL32.@)
741 * There be some things we need to hear about yon there communications device.
742 * (Set which events associated with a communication device should cause
743 * a call WaitCommEvent to return.)
747 * handle [in] The communications device
748 * evtmask [in] The events that are to be monitored
752 * True on success, false on bad handle etc.
754 BOOL WINAPI SetCommMask(HANDLE handle, DWORD evtmask)
756 TRACE("handle %p, mask %x\n", handle, evtmask);
757 return DeviceIoControl(handle, IOCTL_SERIAL_SET_WAIT_MASK,
758 &evtmask, sizeof(evtmask), NULL, 0, NULL, NULL);
761 static void dump_dcb(const DCB* lpdcb)
763 TRACE("bytesize=%d baudrate=%d fParity=%d Parity=%d stopbits=%d\n",
764 lpdcb->ByteSize, lpdcb->BaudRate, lpdcb->fParity, lpdcb->Parity,
765 (lpdcb->StopBits == ONESTOPBIT) ? 1 :
766 (lpdcb->StopBits == TWOSTOPBITS) ? 2 : 0);
767 TRACE("%sIXON %sIXOFF\n", (lpdcb->fInX) ? "" : "~", (lpdcb->fOutX) ? "" : "~");
768 TRACE("fOutxCtsFlow=%d fRtsControl=%d\n", lpdcb->fOutxCtsFlow, lpdcb->fRtsControl);
769 TRACE("fOutxDsrFlow=%d fDtrControl=%d\n", lpdcb->fOutxDsrFlow, lpdcb->fDtrControl);
770 if (lpdcb->fOutxCtsFlow || lpdcb->fRtsControl == RTS_CONTROL_HANDSHAKE)
776 /*****************************************************************************
777 * SetCommState (KERNEL32.@)
779 * Re-initializes all hardware and control settings of a communications device,
780 * with values from a device control block without effecting the input and output
785 * handle [in] The communications device
786 * lpdcb [out] The device control block
790 * True on success, false on failure eg if the XonChar is equal to the XoffChar.
792 BOOL WINAPI SetCommState( HANDLE handle, LPDCB lpdcb)
794 SERIAL_BAUD_RATE sbr;
795 SERIAL_LINE_CONTROL slc;
801 SetLastError(ERROR_INVALID_PARAMETER);
806 sbr.BaudRate = lpdcb->BaudRate;
808 slc.StopBits = lpdcb->StopBits;
809 slc.Parity = lpdcb->Parity;
810 slc.WordLength = lpdcb->ByteSize;
812 shf.ControlHandShake = 0;
814 if (lpdcb->fOutxCtsFlow) shf.ControlHandShake |= SERIAL_CTS_HANDSHAKE;
815 if (lpdcb->fOutxDsrFlow) shf.ControlHandShake |= SERIAL_DSR_HANDSHAKE;
816 switch (lpdcb->fDtrControl)
818 case DTR_CONTROL_DISABLE: break;
819 case DTR_CONTROL_ENABLE: shf.ControlHandShake |= SERIAL_DTR_CONTROL; break;
820 case DTR_CONTROL_HANDSHAKE: shf.ControlHandShake |= SERIAL_DTR_HANDSHAKE;break;
822 SetLastError(ERROR_INVALID_PARAMETER);
825 switch (lpdcb->fDtrControl)
827 case RTS_CONTROL_DISABLE: break;
828 case RTS_CONTROL_ENABLE: shf.FlowReplace |= SERIAL_RTS_CONTROL; break;
829 case RTS_CONTROL_HANDSHAKE: shf.FlowReplace |= SERIAL_RTS_HANDSHAKE; break;
830 case RTS_CONTROL_TOGGLE: shf.FlowReplace |= SERIAL_RTS_CONTROL |
831 SERIAL_RTS_HANDSHAKE; break;
833 SetLastError(ERROR_INVALID_PARAMETER);
836 if (lpdcb->fDsrSensitivity) shf.ControlHandShake |= SERIAL_DSR_SENSITIVITY;
837 if (lpdcb->fAbortOnError) shf.ControlHandShake |= SERIAL_ERROR_ABORT;
839 if (lpdcb->fErrorChar) shf.FlowReplace |= SERIAL_ERROR_CHAR;
840 if (lpdcb->fNull) shf.FlowReplace |= SERIAL_NULL_STRIPPING;
841 if (lpdcb->fTXContinueOnXoff) shf.FlowReplace |= SERIAL_XOFF_CONTINUE;
842 if (lpdcb->fOutX) shf.FlowReplace |= SERIAL_AUTO_TRANSMIT;
843 if (lpdcb->fInX) shf.FlowReplace |= SERIAL_AUTO_RECEIVE;
845 shf.XonLimit = lpdcb->XonLim;
846 shf.XoffLimit = lpdcb->XoffLim;
848 sc.EofChar = lpdcb->EofChar;
849 sc.ErrorChar = lpdcb->ErrorChar;
851 sc.EventChar = lpdcb->EvtChar;
852 sc.XonChar = lpdcb->XonChar;
853 sc.XoffChar = lpdcb->XoffChar;
855 /* note: change DTR/RTS lines after setting the comm attributes,
856 * so flow control does not interfere.
858 return (DeviceIoControl(handle, IOCTL_SERIAL_SET_BAUD_RATE,
859 &sbr, sizeof(sbr), NULL, 0, NULL, NULL) &&
860 DeviceIoControl(handle, IOCTL_SERIAL_SET_LINE_CONTROL,
861 &slc, sizeof(slc), NULL, 0, NULL, NULL) &&
862 DeviceIoControl(handle, IOCTL_SERIAL_SET_HANDFLOW,
863 &shf, sizeof(shf), NULL, 0, NULL, NULL) &&
864 DeviceIoControl(handle, IOCTL_SERIAL_SET_CHARS,
865 &sc, sizeof(sc), NULL, 0, NULL, NULL));
869 /*****************************************************************************
870 * GetCommState (KERNEL32.@)
872 * Fills in a device control block with information from a communications device.
875 * handle [in] The communications device
876 * lpdcb [out] The device control block
880 * True on success, false if the communication device handle is bad etc
884 * XonChar and XoffChar are not set.
886 BOOL WINAPI GetCommState(HANDLE handle, LPDCB lpdcb)
888 SERIAL_BAUD_RATE sbr;
889 SERIAL_LINE_CONTROL slc;
893 TRACE("handle %p, ptr %p\n", handle, lpdcb);
897 SetLastError(ERROR_INVALID_PARAMETER);
901 if (!DeviceIoControl(handle, IOCTL_SERIAL_GET_BAUD_RATE,
902 NULL, 0, &sbr, sizeof(sbr), NULL, NULL) ||
903 !DeviceIoControl(handle, IOCTL_SERIAL_GET_LINE_CONTROL,
904 NULL, 0, &slc, sizeof(slc), NULL, NULL) ||
905 !DeviceIoControl(handle, IOCTL_SERIAL_GET_HANDFLOW,
906 NULL, 0, &shf, sizeof(shf), NULL, NULL) ||
907 !DeviceIoControl(handle, IOCTL_SERIAL_GET_CHARS,
908 NULL, 0, &sc, sizeof(sc), NULL, NULL))
911 memset(lpdcb, 0, sizeof(*lpdcb));
912 lpdcb->DCBlength = sizeof(*lpdcb);
914 /* yes, they seem no never be (re)set on NT */
918 lpdcb->BaudRate = sbr.BaudRate;
920 lpdcb->StopBits = slc.StopBits;
921 lpdcb->Parity = slc.Parity;
922 lpdcb->ByteSize = slc.WordLength;
924 if (shf.ControlHandShake & SERIAL_CTS_HANDSHAKE) lpdcb->fOutxCtsFlow = 1;
925 if (shf.ControlHandShake & SERIAL_DSR_HANDSHAKE) lpdcb->fOutxDsrFlow = 1;
926 switch (shf.ControlHandShake & (SERIAL_DTR_CONTROL | SERIAL_DTR_HANDSHAKE))
928 case 0: lpdcb->fDtrControl = DTR_CONTROL_DISABLE; break;
929 case SERIAL_DTR_CONTROL: lpdcb->fDtrControl = DTR_CONTROL_ENABLE; break;
930 case SERIAL_DTR_HANDSHAKE: lpdcb->fDtrControl = DTR_CONTROL_HANDSHAKE; break;
932 switch (shf.FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
934 case 0: lpdcb->fRtsControl = RTS_CONTROL_DISABLE; break;
935 case SERIAL_RTS_CONTROL: lpdcb->fRtsControl = RTS_CONTROL_ENABLE; break;
936 case SERIAL_RTS_HANDSHAKE: lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE; break;
937 case SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE:
938 lpdcb->fRtsControl = RTS_CONTROL_TOGGLE; break;
940 if (shf.ControlHandShake & SERIAL_DSR_SENSITIVITY) lpdcb->fDsrSensitivity = 1;
941 if (shf.ControlHandShake & SERIAL_ERROR_ABORT) lpdcb->fAbortOnError = 1;
942 if (shf.FlowReplace & SERIAL_ERROR_CHAR) lpdcb->fErrorChar = 1;
943 if (shf.FlowReplace & SERIAL_NULL_STRIPPING) lpdcb->fNull = 1;
944 if (shf.FlowReplace & SERIAL_XOFF_CONTINUE) lpdcb->fTXContinueOnXoff = 1;
945 lpdcb->XonLim = shf.XonLimit;
946 lpdcb->XoffLim = shf.XoffLimit;
948 if (shf.FlowReplace & SERIAL_AUTO_TRANSMIT) lpdcb->fOutX = 1;
949 if (shf.FlowReplace & SERIAL_AUTO_RECEIVE) lpdcb->fInX = 1;
951 lpdcb->EofChar = sc.EofChar;
952 lpdcb->ErrorChar = sc.ErrorChar;
953 lpdcb->EvtChar = sc.EventChar;
954 lpdcb->XonChar = sc.XonChar;
955 lpdcb->XoffChar = sc.XoffChar;
963 /*****************************************************************************
964 * TransmitCommChar (KERNEL32.@)
966 * Transmits a single character in front of any pending characters in the
967 * output buffer. Usually used to send an interrupt character to a host.
970 * hComm [in] The communication device in need of a command character
971 * chTransmit [in] The character to transmit
975 * True if the call succeeded, false if the previous command character to the
976 * same device has not been sent yet the handle is bad etc.
979 BOOL WINAPI TransmitCommChar(HANDLE hComm, CHAR chTransmit)
981 return DeviceIoControl(hComm, IOCTL_SERIAL_IMMEDIATE_CHAR,
982 &chTransmit, sizeof(chTransmit), NULL, 0, NULL, NULL);
986 /*****************************************************************************
987 * GetCommTimeouts (KERNEL32.@)
989 * Obtains the request timeout values for the communications device.
992 * hComm [in] The communications device
993 * lptimeouts [out] The struct of request timeouts
997 * True on success, false if communications device handle is bad
998 * or the target structure is null.
1000 BOOL WINAPI GetCommTimeouts(HANDLE hComm, LPCOMMTIMEOUTS lptimeouts)
1004 TRACE("(%p, %p)\n", hComm, lptimeouts);
1007 SetLastError(ERROR_INVALID_PARAMETER);
1010 if (!DeviceIoControl(hComm, IOCTL_SERIAL_GET_TIMEOUTS,
1011 NULL, 0, &st, sizeof(st), NULL, NULL))
1013 lptimeouts->ReadIntervalTimeout = st.ReadIntervalTimeout;
1014 lptimeouts->ReadTotalTimeoutMultiplier = st.ReadTotalTimeoutMultiplier;
1015 lptimeouts->ReadTotalTimeoutConstant = st.ReadTotalTimeoutConstant;
1016 lptimeouts->WriteTotalTimeoutMultiplier = st.WriteTotalTimeoutMultiplier;
1017 lptimeouts->WriteTotalTimeoutConstant = st.WriteTotalTimeoutConstant;
1021 /*****************************************************************************
1022 * SetCommTimeouts (KERNEL32.@)
1024 * Sets the timeouts used when reading and writing data to/from COMM ports.
1027 * hComm [in] handle of COMM device
1028 * lptimeouts [in] pointer to COMMTIMEOUTS structure
1030 * ReadIntervalTimeout
1031 * - converted and passes to linux kernel as c_cc[VTIME]
1032 * ReadTotalTimeoutMultiplier, ReadTotalTimeoutConstant
1033 * - used in ReadFile to calculate GetOverlappedResult's timeout
1034 * WriteTotalTimeoutMultiplier, WriteTotalTimeoutConstant
1035 * - used in WriteFile to calculate GetOverlappedResult's timeout
1039 * True if the timeouts were set, false otherwise.
1041 BOOL WINAPI SetCommTimeouts(HANDLE hComm, LPCOMMTIMEOUTS lptimeouts)
1045 TRACE("(%p, %p)\n", hComm, lptimeouts);
1047 if (lptimeouts == NULL)
1049 SetLastError(ERROR_INVALID_PARAMETER);
1052 st.ReadIntervalTimeout = lptimeouts->ReadIntervalTimeout;
1053 st.ReadTotalTimeoutMultiplier = lptimeouts->ReadTotalTimeoutMultiplier;
1054 st.ReadTotalTimeoutConstant = lptimeouts->ReadTotalTimeoutConstant;
1055 st.WriteTotalTimeoutMultiplier = lptimeouts->WriteTotalTimeoutMultiplier;
1056 st.WriteTotalTimeoutConstant = lptimeouts->WriteTotalTimeoutConstant;
1058 return DeviceIoControl(hComm, IOCTL_SERIAL_SET_TIMEOUTS,
1059 &st, sizeof(st), NULL, 0, NULL, NULL);
1062 /***********************************************************************
1063 * GetCommModemStatus (KERNEL32.@)
1065 * Obtains the four control register bits if supported by the hardware.
1069 * hFile [in] The communications device
1070 * lpModemStat [out] The control register bits
1074 * True if the communications handle was good and for hardware that
1075 * control register access, false otherwise.
1077 BOOL WINAPI GetCommModemStatus(HANDLE hFile, LPDWORD lpModemStat)
1079 return DeviceIoControl(hFile, IOCTL_SERIAL_GET_MODEMSTATUS,
1080 NULL, 0, lpModemStat, sizeof(DWORD), NULL, NULL);
1083 /***********************************************************************
1084 * WaitCommEvent (KERNEL32.@)
1086 * Wait until something interesting happens on a COMM port.
1087 * Interesting things (events) are set by calling SetCommMask before
1088 * this function is called.
1091 * TRUE if successful
1094 * The set of detected events will be written to *lpdwEventMask
1095 * ERROR_IO_PENDING will be returned the overlapped structure was passed
1098 * Only supports EV_RXCHAR and EV_TXEMPTY
1100 BOOL WINAPI WaitCommEvent(
1101 HANDLE hFile, /* [in] handle of comm port to wait for */
1102 LPDWORD lpdwEvents, /* [out] event(s) that were detected */
1103 LPOVERLAPPED lpOverlapped) /* [in/out] for Asynchronous waiting */
1105 return DeviceIoControl(hFile, IOCTL_SERIAL_WAIT_ON_MASK, NULL, 0,
1106 lpdwEvents, sizeof(DWORD), NULL, lpOverlapped);
1109 /***********************************************************************
1110 * GetCommProperties (KERNEL32.@)
1112 * This function fills in a structure with the capabilities of the
1113 * communications port driver.
1117 * TRUE on success, FALSE on failure
1118 * If successful, the lpCommProp structure be filled in with
1119 * properties of the comm port.
1121 BOOL WINAPI GetCommProperties(
1122 HANDLE hFile, /* [in] handle of the comm port */
1123 LPCOMMPROP lpCommProp) /* [out] pointer to struct to be filled */
1125 FIXME("(%p %p )\n",hFile,lpCommProp);
1130 * These values should be valid for LINUX's serial driver
1131 * FIXME: Perhaps they deserve an #ifdef LINUX
1133 memset(lpCommProp,0,sizeof(COMMPROP));
1134 lpCommProp->wPacketLength = 1;
1135 lpCommProp->wPacketVersion = 1;
1136 lpCommProp->dwServiceMask = SP_SERIALCOMM;
1137 lpCommProp->dwReserved1 = 0;
1138 lpCommProp->dwMaxTxQueue = 4096;
1139 lpCommProp->dwMaxRxQueue = 4096;
1140 lpCommProp->dwMaxBaud = BAUD_115200;
1141 lpCommProp->dwProvSubType = PST_RS232;
1142 lpCommProp->dwProvCapabilities = PCF_DTRDSR | PCF_PARITY_CHECK | PCF_RTSCTS | PCF_TOTALTIMEOUTS;
1143 lpCommProp->dwSettableParams = SP_BAUD | SP_DATABITS | SP_HANDSHAKING |
1144 SP_PARITY | SP_PARITY_CHECK | SP_STOPBITS ;
1145 lpCommProp->dwSettableBaud = BAUD_075 | BAUD_110 | BAUD_134_5 | BAUD_150 |
1146 BAUD_300 | BAUD_600 | BAUD_1200 | BAUD_1800 | BAUD_2400 | BAUD_4800 |
1147 BAUD_9600 | BAUD_19200 | BAUD_38400 | BAUD_57600 | BAUD_115200 ;
1148 lpCommProp->wSettableData = DATABITS_5 | DATABITS_6 | DATABITS_7 | DATABITS_8 ;
1149 lpCommProp->wSettableStopParity = STOPBITS_10 | STOPBITS_15 | STOPBITS_20 |
1150 PARITY_NONE | PARITY_ODD |PARITY_EVEN | PARITY_MARK | PARITY_SPACE;
1151 lpCommProp->dwCurrentTxQueue = lpCommProp->dwMaxTxQueue;
1152 lpCommProp->dwCurrentRxQueue = lpCommProp->dwMaxRxQueue;
1157 /***********************************************************************
1159 * The functionality of CommConfigDialogA, GetDefaultCommConfig and
1160 * SetDefaultCommConfig is implemented in a DLL (usually SERIALUI.DLL).
1161 * This is dependent on the type of COMM port, but since it is doubtful
1162 * anybody will get around to implementing support for fancy serial
1163 * ports in WINE, this is hardcoded for the time being. The name of
1164 * this DLL should be stored in and read from the system registry in
1165 * the hive HKEY_LOCAL_MACHINE, key
1166 * System\\CurrentControlSet\\Services\\Class\\Ports\\????
1167 * where ???? is the port number... that is determined by PNP
1168 * The DLL should be loaded when the COMM port is opened, and closed
1169 * when the COMM port is closed. - MJM 20 June 2000
1170 ***********************************************************************/
1171 static const WCHAR lpszSerialUI[] = {
1172 's','e','r','i','a','l','u','i','.','d','l','l',0 };
1175 /***********************************************************************
1176 * CommConfigDialogA (KERNEL32.@)
1178 * Raises a dialog that allows the user to configure a comm port.
1179 * Fills the COMMCONFIG struct with information specified by the user.
1180 * This function should call a similar routine in the COMM driver...
1184 * TRUE on success, FALSE on failure
1185 * If successful, the lpCommConfig structure will contain a new
1186 * configuration for the comm port, as specified by the user.
1189 * The library with the CommConfigDialog code is never unloaded.
1190 * Perhaps this should be done when the comm port is closed?
1192 BOOL WINAPI CommConfigDialogA(
1193 LPCSTR lpszDevice, /* [in] name of communications device */
1194 HWND hWnd, /* [in] parent window for the dialog */
1195 LPCOMMCONFIG lpCommConfig) /* [out] pointer to struct to fill */
1197 LPWSTR lpDeviceW = NULL;
1201 TRACE("(%s, %p, %p)\n", debugstr_a(lpszDevice), hWnd, lpCommConfig);
1205 len = MultiByteToWideChar( CP_ACP, 0, lpszDevice, -1, NULL, 0 );
1206 lpDeviceW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1207 MultiByteToWideChar( CP_ACP, 0, lpszDevice, -1, lpDeviceW, len );
1209 r = CommConfigDialogW(lpDeviceW, hWnd, lpCommConfig);
1210 HeapFree( GetProcessHeap(), 0, lpDeviceW );
1214 /***********************************************************************
1215 * CommConfigDialogW (KERNEL32.@)
1217 * See CommConfigDialogA.
1219 BOOL WINAPI CommConfigDialogW(
1220 LPCWSTR lpszDevice, /* [in] name of communications device */
1221 HWND hWnd, /* [in] parent window for the dialog */
1222 LPCOMMCONFIG lpCommConfig) /* [out] pointer to struct to fill */
1224 FARPROC pCommConfigDialog;
1225 HMODULE hConfigModule;
1226 DWORD res = ERROR_INVALID_PARAMETER;
1228 TRACE("(%s, %p, %p)\n", debugstr_w(lpszDevice), hWnd, lpCommConfig);
1229 hConfigModule = LoadLibraryW(lpszSerialUI);
1231 if (hConfigModule) {
1232 pCommConfigDialog = GetProcAddress(hConfigModule, "drvCommConfigDialogW");
1233 if (pCommConfigDialog) {
1234 res = pCommConfigDialog(lpszDevice, hWnd, lpCommConfig);
1236 FreeLibrary(hConfigModule);
1239 if (res) SetLastError(res);
1240 return (res == ERROR_SUCCESS);
1243 /***********************************************************************
1244 * GetCommConfig (KERNEL32.@)
1246 * Fill in the COMMCONFIG structure for the comm port hFile
1250 * TRUE on success, FALSE on failure
1251 * If successful, lpCommConfig contains the comm port configuration.
1256 BOOL WINAPI GetCommConfig(
1257 HANDLE hFile, /* [in] The communications device. */
1258 LPCOMMCONFIG lpCommConfig, /* [out] The communications configuration of the device (if it fits). */
1259 LPDWORD lpdwSize) /* [in/out] Initially the size of the configuration buffer/structure,
1260 afterwards the number of bytes copied to the buffer or
1261 the needed size of the buffer. */
1265 TRACE("(%p, %p, %p) *lpdwSize: %u\n", hFile, lpCommConfig, lpdwSize, lpdwSize ? *lpdwSize : 0 );
1267 if(lpCommConfig == NULL)
1269 r = *lpdwSize < sizeof(COMMCONFIG); /* TRUE if not enough space */
1270 *lpdwSize = sizeof(COMMCONFIG);
1274 lpCommConfig->dwSize = sizeof(COMMCONFIG);
1275 lpCommConfig->wVersion = 1;
1276 lpCommConfig->wReserved = 0;
1277 r = GetCommState(hFile,&lpCommConfig->dcb);
1278 lpCommConfig->dwProviderSubType = PST_RS232;
1279 lpCommConfig->dwProviderOffset = 0;
1280 lpCommConfig->dwProviderSize = 0;
1285 /***********************************************************************
1286 * SetCommConfig (KERNEL32.@)
1288 * Sets the configuration of the communications device.
1292 * True on success, false if the handle was bad is not a communications device.
1294 BOOL WINAPI SetCommConfig(
1295 HANDLE hFile, /* [in] The communications device. */
1296 LPCOMMCONFIG lpCommConfig, /* [in] The desired configuration. */
1297 DWORD dwSize) /* [in] size of the lpCommConfig struct */
1299 TRACE("(%p, %p, %u)\n", hFile, lpCommConfig, dwSize);
1300 return SetCommState(hFile,&lpCommConfig->dcb);
1303 /***********************************************************************
1304 * SetDefaultCommConfigW (KERNEL32.@)
1306 * Initializes the default configuration for a communication device.
1309 * lpszDevice [I] Name of the device targeted for configuration
1310 * lpCommConfig [I] PTR to a buffer with the configuration for the device
1311 * dwSize [I] Number of bytes in the buffer
1315 * Success: TRUE, and default configuration saved
1318 BOOL WINAPI SetDefaultCommConfigW(LPCWSTR lpszDevice, LPCOMMCONFIG lpCommConfig, DWORD dwSize)
1320 FARPROC lpfnSetDefaultCommConfig;
1321 HMODULE hConfigModule;
1324 TRACE("(%s, %p, %u)\n", debugstr_w(lpszDevice), lpCommConfig, dwSize);
1326 hConfigModule = LoadLibraryW(lpszSerialUI);
1330 lpfnSetDefaultCommConfig = GetProcAddress(hConfigModule, "drvSetDefaultCommConfigW");
1331 if (lpfnSetDefaultCommConfig)
1332 r = lpfnSetDefaultCommConfig(lpszDevice, lpCommConfig, dwSize);
1334 FreeLibrary(hConfigModule);
1340 /***********************************************************************
1341 * SetDefaultCommConfigA (KERNEL32.@)
1343 * Initializes the default configuration for a communication device.
1345 * See SetDefaultCommConfigW.
1348 BOOL WINAPI SetDefaultCommConfigA(LPCSTR lpszDevice, LPCOMMCONFIG lpCommConfig, DWORD dwSize)
1351 LPWSTR lpDeviceW = NULL;
1354 TRACE("(%s, %p, %u)\n", debugstr_a(lpszDevice), lpCommConfig, dwSize);
1358 len = MultiByteToWideChar( CP_ACP, 0, lpszDevice, -1, NULL, 0 );
1359 lpDeviceW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1360 MultiByteToWideChar( CP_ACP, 0, lpszDevice, -1, lpDeviceW, len );
1362 r = SetDefaultCommConfigW(lpDeviceW,lpCommConfig,dwSize);
1363 HeapFree( GetProcessHeap(), 0, lpDeviceW );
1368 /***********************************************************************
1369 * GetDefaultCommConfigW (KERNEL32.@)
1371 * Acquires the default configuration of the specified communication device. (unicode)
1375 * True on successful reading of the default configuration,
1376 * if the device is not found or the buffer is too small.
1378 BOOL WINAPI GetDefaultCommConfigW(
1379 LPCWSTR lpszName, /* [in] The unicode name of the device targeted for configuration. */
1380 LPCOMMCONFIG lpCC, /* [out] The default configuration for the device. */
1381 LPDWORD lpdwSize) /* [in/out] Initially the size of the default configuration buffer,
1382 afterwards the number of bytes copied to the buffer or
1383 the needed size of the buffer. */
1385 FARPROC pGetDefaultCommConfig;
1386 HMODULE hConfigModule;
1387 DWORD res = ERROR_INVALID_PARAMETER;
1389 TRACE("(%s, %p, %p) *lpdwSize: %u\n", debugstr_w(lpszName), lpCC, lpdwSize, lpdwSize ? *lpdwSize : 0 );
1390 hConfigModule = LoadLibraryW(lpszSerialUI);
1392 if (hConfigModule) {
1393 pGetDefaultCommConfig = GetProcAddress(hConfigModule, "drvGetDefaultCommConfigW");
1394 if (pGetDefaultCommConfig) {
1395 res = pGetDefaultCommConfig(lpszName, lpCC, lpdwSize);
1397 FreeLibrary(hConfigModule);
1400 if (res) SetLastError(res);
1401 return (res == ERROR_SUCCESS);
1404 /**************************************************************************
1405 * GetDefaultCommConfigA (KERNEL32.@)
1407 * Acquires the default configuration of the specified communication device. (ascii)
1411 * True on successful reading of the default configuration,
1412 * if the device is not found or the buffer is too small.
1414 BOOL WINAPI GetDefaultCommConfigA(
1415 LPCSTR lpszName, /* [in] The ascii name of the device targeted for configuration. */
1416 LPCOMMCONFIG lpCC, /* [out] The default configuration for the device. */
1417 LPDWORD lpdwSize) /* [in/out] Initially the size of the default configuration buffer,
1418 afterwards the number of bytes copied to the buffer or
1419 the needed size of the buffer. */
1422 UNICODE_STRING lpszNameW;
1424 TRACE("(%s, %p, %p) *lpdwSize: %u\n", debugstr_a(lpszName), lpCC, lpdwSize, lpdwSize ? *lpdwSize : 0 );
1425 if(lpszName) RtlCreateUnicodeStringFromAsciiz(&lpszNameW,lpszName);
1426 else lpszNameW.Buffer = NULL;
1428 ret = GetDefaultCommConfigW(lpszNameW.Buffer,lpCC,lpdwSize);
1430 RtlFreeUnicodeString(&lpszNameW);