Fixed GetProcessHeap() for the STRICT mode.
[wine.git] / misc / comm.c
blob75a02e25c3f60612ae6d33553fc4be23a191898f
1 /*
2 * DEC 93 Erik Bos <erik@xs4all.nl>
4 * Copyright 1996 Marcus Meissner
6 * Mar 31, 1999. Ove Kåven <ovek@arcticnet.no>
7 * - Implemented buffers and EnableCommNotification.
9 * Apr 3, 1999. Lawson Whitney <lawson_whitney@juno.com>
10 * - Fixed the modem control part of EscapeCommFunction16.
12 * Mar 3, 1999. Ove Kåven <ovek@arcticnet.no>
13 * - Use port indices instead of unixfds for win16
14 * - Moved things around (separated win16 and win32 routines)
15 * - Added some hints on how to implement buffers and EnableCommNotification.
17 * May 26, 1997. Fixes and comments by Rick Richardson <rick@dgii.com> [RER]
18 * - ptr->fd wasn't getting cleared on close.
19 * - GetCommEventMask() and GetCommError() didn't do much of anything.
20 * IMHO, they are still wrong, but they at least implement the RXCHAR
21 * event and return I/O queue sizes, which makes the app I'm interested
22 * in (analog devices EZKIT DSP development system) work.
24 * August 12, 1997. Take a bash at SetCommEventMask - Lawson Whitney
25 * <lawson_whitney@juno.com>
26 * July 6, 1998. Fixes and comments by Valentijn Sessink
27 * <vsessink@ic.uva.nl> [V]
28 * Oktober 98, Rein Klazes [RHK]
29 * A program that wants to monitor the modem status line (RLSD/DCD) may
30 * poll the modem status register in the commMask structure. I update the bit
31 * in GetCommError, waiting for an implementation of communication events.
35 #include "config.h"
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <termios.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #ifdef HAVE_STRINGS_H
43 # include <strings.h>
44 #endif
45 #include <errno.h>
46 #include <ctype.h>
47 #include <sys/stat.h>
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
50 #endif
51 #include <sys/ioctl.h>
52 #include <unistd.h>
54 #include "windef.h"
55 #include "wingdi.h"
56 #include "wine/winuser16.h"
57 #include "comm.h"
58 #ifdef HAVE_SYS_MODEM_H
59 # include <sys/modem.h>
60 #endif
61 #ifdef HAVE_SYS_STRTIO_H
62 # include <sys/strtio.h>
63 #endif
64 #include "heap.h"
65 #include "options.h"
67 #include "server.h"
68 #include "process.h"
69 #include "winerror.h"
70 #include "services.h"
71 #include "callback.h"
72 #include "file.h"
74 #include "debugtools.h"
76 DEFAULT_DEBUG_CHANNEL(comm);
78 #ifndef TIOCINQ
79 #define TIOCINQ FIONREAD
80 #endif
82 /* window's semi documented modem status register */
83 #define COMM_MSR_OFFSET 35
84 #define MSR_CTS 0x10
85 #define MSR_DSR 0x20
86 #define MSR_RI 0x40
87 #define MSR_RLSD 0x80
88 #define MSR_MASK (MSR_CTS|MSR_DSR|MSR_RI|MSR_RLSD)
90 #define FLAG_LPT 0x80
92 struct DosDeviceStruct COM[MAX_PORTS];
93 struct DosDeviceStruct LPT[MAX_PORTS];
94 /* pointers to unknown(==undocumented) comm structure */
95 LPCVOID *unknown[MAX_PORTS];
96 /* save terminal states */
97 static struct termios m_stat[MAX_PORTS];
99 /* update window's semi documented modem status register */
100 /* see knowledge base Q101417 */
101 static void COMM_MSRUpdate( UCHAR * pMsr, unsigned int mstat)
103 UCHAR tmpmsr=0;
104 if(mstat & TIOCM_CTS) tmpmsr |= MSR_CTS;
105 if(mstat & TIOCM_DSR) tmpmsr |= MSR_DSR;
106 if(mstat & TIOCM_RI) tmpmsr |= MSR_RI;
107 if(mstat & TIOCM_CAR) tmpmsr |= MSR_RLSD;
108 *pMsr = (*pMsr & ~MSR_MASK) | tmpmsr;
111 void COMM_Init(void)
113 int x;
114 char option[10], temp[256], *btemp;
115 struct stat st;
117 for (x=0; x!=MAX_PORTS; x++) {
118 strcpy(option,"COMx");
119 option[3] = '1' + x;
120 option[4] = '\0';
122 PROFILE_GetWineIniString( "serialports", option, "*",
123 temp, sizeof(temp) );
124 if (!strcmp(temp, "*") || *temp == '\0')
125 COM[x].devicename = NULL;
126 else {
127 btemp = strchr(temp,',');
128 if (btemp != NULL) {
129 *btemp++ = '\0';
130 COM[x].baudrate = atoi(btemp);
131 } else {
132 COM[x].baudrate = -1;
134 stat(temp, &st);
135 if (!S_ISCHR(st.st_mode))
136 WARN("Can't use `%s' as %s !\n", temp, option);
137 else
138 if ((COM[x].devicename = malloc(strlen(temp)+1)) == NULL)
139 WARN("Can't malloc for device info!\n");
140 else {
141 COM[x].fd = 0;
142 strcpy(COM[x].devicename, temp);
144 TRACE("%s = %s\n", option, COM[x].devicename);
147 strcpy(option, "LPTx");
148 option[3] = '1' + x;
149 option[4] = '\0';
151 PROFILE_GetWineIniString( "parallelports", option, "*",
152 temp, sizeof(temp) );
153 if (!strcmp(temp, "*") || *temp == '\0')
154 LPT[x].devicename = NULL;
155 else {
156 stat(temp, &st);
157 if (!S_ISCHR(st.st_mode))
158 WARN("Can't use `%s' as %s !\n", temp, option);
159 else
160 if ((LPT[x].devicename = malloc(strlen(temp)+1)) == NULL)
161 WARN("Can't malloc for device info!\n");
162 else {
163 LPT[x].fd = 0;
164 strcpy(LPT[x].devicename, temp);
166 TRACE("%s = %s\n", option, LPT[x].devicename);
173 static struct DosDeviceStruct *GetDeviceStruct(int fd)
175 if ((fd&0x7F)<=MAX_PORTS) {
176 if (!(fd&FLAG_LPT)) {
177 if (COM[fd].fd)
178 return &COM[fd];
179 } else {
180 fd &= 0x7f;
181 if (LPT[fd].fd)
182 return &LPT[fd];
186 return NULL;
189 static int GetCommPort_fd(int fd)
191 int x;
193 for (x=0; x<MAX_PORTS; x++) {
194 if (COM[x].fd == fd)
195 return x;
198 return -1;
201 static int ValidCOMPort(int x)
203 return(x < MAX_PORTS ? (int) COM[x].devicename : 0);
206 static int ValidLPTPort(int x)
208 return(x < MAX_PORTS ? (int) LPT[x].devicename : 0);
211 static int WinError(void)
213 TRACE("errno = %d\n", errno);
214 switch (errno) {
215 default:
216 return CE_IOE;
220 static unsigned comm_inbuf(struct DosDeviceStruct *ptr)
222 return ((ptr->ibuf_tail > ptr->ibuf_head) ? ptr->ibuf_size : 0)
223 + ptr->ibuf_head - ptr->ibuf_tail;
226 static unsigned comm_outbuf(struct DosDeviceStruct *ptr)
228 return ((ptr->obuf_tail > ptr->obuf_head) ? ptr->obuf_size : 0)
229 + ptr->obuf_head - ptr->obuf_tail;
232 static int COMM_WhackModem(int fd, unsigned int andy, unsigned int orrie)
234 unsigned int mstat, okay;
235 okay = ioctl(fd, TIOCMGET, &mstat);
236 if (okay) return okay;
237 if (andy) mstat &= andy;
238 mstat |= orrie;
239 return ioctl(fd, TIOCMSET, &mstat);
242 static void CALLBACK comm_notification( ULONG_PTR private )
244 struct DosDeviceStruct *ptr = (struct DosDeviceStruct *)private;
245 int prev, bleft, len;
246 WORD mask = 0;
247 int cid = GetCommPort_fd(ptr->fd);
249 TRACE("async notification\n");
250 /* read data from comm port */
251 prev = comm_inbuf(ptr);
252 do {
253 bleft = ((ptr->ibuf_tail > ptr->ibuf_head) ? (ptr->ibuf_tail-1) : ptr->ibuf_size)
254 - ptr->ibuf_head;
255 len = read(ptr->fd, ptr->inbuf + ptr->ibuf_head, bleft?bleft:1);
256 if (len > 0) {
257 if (!bleft) {
258 ptr->commerror = CE_RXOVER;
259 } else {
260 /* check for events */
261 if ((ptr->eventmask & EV_RXFLAG) &&
262 memchr(ptr->inbuf + ptr->ibuf_head, ptr->evtchar, len)) {
263 *(WORD*)(unknown[cid]) |= EV_RXFLAG;
264 mask |= CN_EVENT;
266 if (ptr->eventmask & EV_RXCHAR) {
267 *(WORD*)(unknown[cid]) |= EV_RXCHAR;
268 mask |= CN_EVENT;
270 /* advance buffer position */
271 ptr->ibuf_head += len;
272 if (ptr->ibuf_head >= ptr->ibuf_size)
273 ptr->ibuf_head = 0;
276 } while (len > 0);
277 /* check for notification */
278 if (ptr->wnd && (ptr->n_read>0) && (prev<ptr->n_read) &&
279 (comm_inbuf(ptr)>=ptr->n_read)) {
280 /* passed the receive notification threshold */
281 mask |= CN_RECEIVE;
284 /* write any TransmitCommChar character */
285 if (ptr->xmit>=0) {
286 len = write(ptr->fd, &(ptr->xmit), 1);
287 if (len > 0) ptr->xmit = -1;
289 /* write from output queue */
290 prev = comm_outbuf(ptr);
291 do {
292 bleft = ((ptr->obuf_tail <= ptr->obuf_head) ? ptr->obuf_head : ptr->obuf_size)
293 - ptr->obuf_tail;
294 len = bleft ? write(ptr->fd, ptr->outbuf + ptr->obuf_tail, bleft) : 0;
295 if (len > 0) {
296 ptr->obuf_tail += len;
297 if (ptr->obuf_tail >= ptr->obuf_size)
298 ptr->obuf_tail = 0;
299 /* flag event */
300 if (ptr->obuf_tail == ptr->obuf_head) {
301 if (ptr->s_write) {
302 SERVICE_Delete( ptr->s_write );
303 ptr->s_write = INVALID_HANDLE_VALUE;
305 if (ptr->eventmask & EV_TXEMPTY) {
306 *(WORD*)(unknown[cid]) |= EV_TXEMPTY;
307 mask |= CN_EVENT;
311 } while (len > 0);
312 /* check for notification */
313 if (ptr->wnd && (ptr->n_write>0) && (prev>=ptr->n_write) &&
314 (comm_outbuf(ptr)<ptr->n_write)) {
315 /* passed the transmit notification threshold */
316 mask |= CN_TRANSMIT;
319 /* send notifications, if any */
320 if (ptr->wnd && mask) {
321 TRACE("notifying %04x: cid=%d, mask=%02x\n", ptr->wnd, cid, mask);
322 Callout.PostMessage16(ptr->wnd, WM_COMMNOTIFY, cid, mask);
326 static void comm_waitread(struct DosDeviceStruct *ptr)
328 if (ptr->s_read != INVALID_HANDLE_VALUE) return;
329 ptr->s_read = SERVICE_AddObject( FILE_DupUnixHandle( ptr->fd,
330 GENERIC_READ | SYNCHRONIZE ),
331 comm_notification,
332 (ULONG_PTR)ptr );
335 static void comm_waitwrite(struct DosDeviceStruct *ptr)
337 if (ptr->s_write != INVALID_HANDLE_VALUE) return;
338 ptr->s_write = SERVICE_AddObject( FILE_DupUnixHandle( ptr->fd,
339 GENERIC_WRITE | SYNCHRONIZE ),
340 comm_notification,
341 (ULONG_PTR)ptr );
344 /**************************************************************************
345 * BuildCommDCB (USER.213)
347 BOOL16 WINAPI BuildCommDCB16(LPCSTR device, LPDCB16 lpdcb)
349 /* "COM1:9600,n,8,1" */
350 /* 012345 */
351 int port;
352 char *ptr, temp[256];
354 TRACE("(%s), ptr %p\n", device, lpdcb);
356 if (!lstrncmpiA(device,"COM",3)) {
357 port = device[3] - '0';
360 if (port-- == 0) {
361 ERR("BUG ! COM0 can't exist!.\n");
362 return -1;
365 if (!ValidCOMPort(port)) {
366 FIXME("invalid COM port %d?\n",port);
367 return -1;
370 memset(lpdcb, 0, sizeof(DCB16)); /* initialize */
372 lpdcb->Id = port;
374 if (!*(device+4))
375 return 0;
377 if (*(device+4) != ':')
378 return -1;
380 strcpy(temp,device+5);
381 ptr = strtok(temp, ", ");
383 if (COM[port].baudrate > 0)
384 lpdcb->BaudRate = COM[port].baudrate;
385 else
386 lpdcb->BaudRate = atoi(ptr);
387 TRACE("baudrate (%d)\n", lpdcb->BaudRate);
389 ptr = strtok(NULL, ", ");
390 if (islower(*ptr))
391 *ptr = toupper(*ptr);
393 TRACE("parity (%c)\n", *ptr);
394 lpdcb->fParity = TRUE;
395 switch (*ptr) {
396 case 'N':
397 lpdcb->Parity = NOPARITY;
398 lpdcb->fParity = FALSE;
399 break;
400 case 'E':
401 lpdcb->Parity = EVENPARITY;
402 break;
403 case 'M':
404 lpdcb->Parity = MARKPARITY;
405 break;
406 case 'O':
407 lpdcb->Parity = ODDPARITY;
408 break;
409 default:
410 WARN("Unknown parity `%c'!\n", *ptr);
411 return -1;
414 ptr = strtok(NULL, ", ");
415 TRACE("charsize (%c)\n", *ptr);
416 lpdcb->ByteSize = *ptr - '0';
418 ptr = strtok(NULL, ", ");
419 TRACE("stopbits (%c)\n", *ptr);
420 switch (*ptr) {
421 case '1':
422 lpdcb->StopBits = ONESTOPBIT;
423 break;
424 case '2':
425 lpdcb->StopBits = TWOSTOPBITS;
426 break;
427 default:
428 WARN("Unknown # of stopbits `%c'!\n", *ptr);
429 return -1;
433 return 0;
436 /*****************************************************************************
437 * OpenComm (USER.200)
439 INT16 WINAPI OpenComm16(LPCSTR device,UINT16 cbInQueue,UINT16 cbOutQueue)
441 int port,fd;
443 TRACE("%s, %d, %d\n", device, cbInQueue, cbOutQueue);
445 if (strlen(device) < 4)
446 return IE_BADID;
448 port = device[3] - '0';
450 if (port-- == 0)
451 ERR("BUG ! COM0 or LPT0 don't exist !\n");
453 if (!lstrncmpiA(device,"COM",3)) {
455 TRACE("%s = %s\n", device, COM[port].devicename);
457 if (!ValidCOMPort(port))
458 return IE_BADID;
460 if (COM[port].fd)
461 return IE_OPEN;
463 fd = open(COM[port].devicename, O_RDWR | O_NONBLOCK);
464 if (fd == -1) {
465 ERR("error=%d\n", errno);
466 return IE_HARDWARE;
467 } else {
468 unknown[port] = SEGPTR_ALLOC(40);
469 bzero(unknown[port],40);
470 COM[port].fd = fd;
471 COM[port].commerror = 0;
472 COM[port].eventmask = 0;
473 COM[port].evtchar = 0; /* FIXME: default? */
474 /* save terminal state */
475 tcgetattr(fd,&m_stat[port]);
476 /* set default parameters */
477 if(COM[port].baudrate>-1){
478 DCB16 dcb;
479 GetCommState16(port, &dcb);
480 dcb.BaudRate=COM[port].baudrate;
481 /* more defaults:
482 * databits, parity, stopbits
484 SetCommState16( &dcb);
486 /* init priority characters */
487 COM[port].unget = -1;
488 COM[port].xmit = -1;
489 /* allocate buffers */
490 COM[port].ibuf_size = cbInQueue;
491 COM[port].ibuf_head = COM[port].ibuf_tail= 0;
492 COM[port].obuf_size = cbOutQueue;
493 COM[port].obuf_head = COM[port].obuf_tail = 0;
495 COM[port].inbuf = malloc(cbInQueue);
496 if (COM[port].inbuf) {
497 COM[port].outbuf = malloc(cbOutQueue);
498 if (!COM[port].outbuf)
499 free(COM[port].inbuf);
500 } else COM[port].outbuf = NULL;
501 if (!COM[port].outbuf) {
502 /* not enough memory */
503 tcsetattr(COM[port].fd,TCSANOW,&m_stat[port]);
504 close(COM[port].fd);
505 ERR("out of memory");
506 return IE_MEMORY;
509 COM[port].s_read = INVALID_HANDLE_VALUE;
510 COM[port].s_write = INVALID_HANDLE_VALUE;
511 comm_waitread( &COM[port] );
512 return port;
515 else
516 if (!lstrncmpiA(device,"LPT",3)) {
518 if (!ValidLPTPort(port))
519 return IE_BADID;
521 if (LPT[port].fd)
522 return IE_OPEN;
524 fd = open(LPT[port].devicename, O_RDWR | O_NONBLOCK, 0);
525 if (fd == -1) {
526 return IE_HARDWARE;
527 } else {
528 LPT[port].fd = fd;
529 LPT[port].commerror = 0;
530 LPT[port].eventmask = 0;
531 return port|FLAG_LPT;
534 return 0;
537 /*****************************************************************************
538 * CloseComm (USER.207)
540 INT16 WINAPI CloseComm16(INT16 cid)
542 struct DosDeviceStruct *ptr;
544 TRACE("cid=%d\n", cid);
545 if ((ptr = GetDeviceStruct(cid)) == NULL) {
546 FIXME("no cid=%d found!\n", cid);
547 return -1;
549 if (!(cid&FLAG_LPT)) {
550 /* COM port */
551 SEGPTR_FREE(unknown[cid]); /* [LW] */
553 SERVICE_Delete( COM[cid].s_write );
554 SERVICE_Delete( COM[cid].s_read );
555 /* free buffers */
556 free(ptr->outbuf);
557 free(ptr->inbuf);
559 /* reset modem lines */
560 tcsetattr(ptr->fd,TCSANOW,&m_stat[cid]);
563 if (close(ptr->fd) == -1) {
564 ptr->commerror = WinError();
565 /* FIXME: should we clear ptr->fd here? */
566 return -1;
567 } else {
568 ptr->commerror = 0;
569 ptr->fd = 0;
570 return 0;
574 /*****************************************************************************
575 * SetCommBreak (USER.210)
577 INT16 WINAPI SetCommBreak16(INT16 cid)
579 struct DosDeviceStruct *ptr;
581 TRACE("cid=%d\n", cid);
582 if ((ptr = GetDeviceStruct(cid)) == NULL) {
583 FIXME("no cid=%d found!\n", cid);
584 return -1;
587 ptr->suspended = 1;
588 ptr->commerror = 0;
589 return 0;
592 /*****************************************************************************
593 * ClearCommBreak (USER.211)
595 INT16 WINAPI ClearCommBreak16(INT16 cid)
597 struct DosDeviceStruct *ptr;
599 TRACE("cid=%d\n", cid);
600 if (!(ptr = GetDeviceStruct(cid))) {
601 FIXME("no cid=%d found!\n", cid);
602 return -1;
604 ptr->suspended = 0;
605 ptr->commerror = 0;
606 return 0;
609 /*****************************************************************************
610 * EscapeCommFunction (USER.214)
612 LONG WINAPI EscapeCommFunction16(UINT16 cid,UINT16 nFunction)
614 int max;
615 struct DosDeviceStruct *ptr;
616 struct termios port;
618 TRACE("cid=%d, function=%d\n", cid, nFunction);
619 if ((nFunction != GETMAXCOM) && (nFunction != GETMAXLPT)) {
620 if ((ptr = GetDeviceStruct(cid)) == NULL) {
621 FIXME("no cid=%d found!\n", cid);
622 return -1;
624 if (tcgetattr(ptr->fd,&port) == -1) {
625 TRACE("tcgetattr failed\n");
626 ptr->commerror=WinError();
627 return -1;
629 } else ptr = NULL;
631 switch (nFunction) {
632 case RESETDEV:
633 TRACE("RESETDEV\n");
634 break;
636 case GETMAXCOM:
637 TRACE("GETMAXCOM\n");
638 for (max = MAX_PORTS;!COM[max].devicename;max--)
640 return max;
641 break;
643 case GETMAXLPT:
644 TRACE("GETMAXLPT\n");
645 for (max = MAX_PORTS;!LPT[max].devicename;max--)
647 return FLAG_LPT + max;
648 break;
650 case GETBASEIRQ:
651 TRACE("GETBASEIRQ\n");
652 /* FIXME: use tables */
653 /* just fake something for now */
654 if (cid & FLAG_LPT) {
655 /* LPT1: irq 7, LPT2: irq 5 */
656 return (cid & 0x7f) ? 5 : 7;
657 } else {
658 /* COM1: irq 4, COM2: irq 3,
659 COM3: irq 4, COM4: irq 3 */
660 return 4 - (cid & 1);
662 break;
664 case CLRDTR:
665 TRACE("CLRDTR\n");
666 #ifdef TIOCM_DTR
667 return COMM_WhackModem(ptr->fd, ~TIOCM_DTR, 0);
668 #endif
669 case CLRRTS:
670 TRACE("CLRRTS\n");
671 #ifdef TIOCM_RTS
672 return COMM_WhackModem(ptr->fd, ~TIOCM_RTS, 0);
673 #endif
675 case SETDTR:
676 TRACE("SETDTR\n");
677 #ifdef TIOCM_DTR
678 return COMM_WhackModem(ptr->fd, 0, TIOCM_DTR);
679 #endif
681 case SETRTS:
682 TRACE("SETRTS\n");
683 #ifdef TIOCM_RTS
684 return COMM_WhackModem(ptr->fd, 0, TIOCM_RTS);
685 #endif
687 case SETXOFF:
688 TRACE("SETXOFF\n");
689 port.c_iflag |= IXOFF;
690 break;
692 case SETXON:
693 TRACE("SETXON\n");
694 port.c_iflag |= IXON;
695 break;
697 default:
698 WARN("(cid=%d,nFunction=%d): Unknown function\n",
699 cid, nFunction);
700 break;
703 if (tcsetattr(ptr->fd, TCSADRAIN, &port) == -1) {
704 ptr->commerror = WinError();
705 return -1;
706 } else {
707 ptr->commerror = 0;
708 return 0;
712 /*****************************************************************************
713 * FlushComm (USER.215)
715 INT16 WINAPI FlushComm16(INT16 cid,INT16 fnQueue)
717 int queue;
718 struct DosDeviceStruct *ptr;
720 TRACE("cid=%d, queue=%d\n", cid, fnQueue);
721 if ((ptr = GetDeviceStruct(cid)) == NULL) {
722 FIXME("no cid=%d found!\n", cid);
723 return -1;
725 switch (fnQueue) {
726 case 0:
727 queue = TCOFLUSH;
728 ptr->obuf_tail = ptr->obuf_head;
729 break;
730 case 1:
731 queue = TCIFLUSH;
732 ptr->ibuf_head = ptr->ibuf_tail;
733 break;
734 default:
735 WARN("(cid=%d,fnQueue=%d):Unknown queue\n",
736 cid, fnQueue);
737 return -1;
739 if (tcflush(ptr->fd, queue)) {
740 ptr->commerror = WinError();
741 return -1;
742 } else {
743 ptr->commerror = 0;
744 return 0;
748 /********************************************************************
749 * GetCommError (USER.203)
751 INT16 WINAPI GetCommError16(INT16 cid,LPCOMSTAT16 lpStat)
753 int temperror;
754 struct DosDeviceStruct *ptr;
755 unsigned char *stol;
756 unsigned int mstat;
758 if ((ptr = GetDeviceStruct(cid)) == NULL) {
759 FIXME("no handle for cid = %0x!.\n",cid);
760 return -1;
762 if (cid&FLAG_LPT) {
763 WARN(" cid %d not comm port\n",cid);
764 return CE_MODE;
766 stol = (unsigned char *)unknown[cid] + COMM_MSR_OFFSET;
767 ioctl(ptr->fd,TIOCMGET,&mstat);
768 COMM_MSRUpdate( stol, mstat);
770 if (lpStat) {
771 lpStat->status = 0;
773 lpStat->cbOutQue = comm_outbuf(ptr);
774 lpStat->cbInQue = comm_inbuf(ptr);
776 TRACE("cid %d, error %d, lpStat %d %d %d stol %x\n",
777 cid, ptr->commerror, lpStat->status, lpStat->cbInQue,
778 lpStat->cbOutQue, *stol);
780 else
781 TRACE("cid %d, error %d, lpStat NULL stol %x\n",
782 cid, ptr->commerror, *stol);
784 /* Return any errors and clear it */
785 temperror = ptr->commerror;
786 ptr->commerror = 0;
787 return(temperror);
790 /*****************************************************************************
791 * SetCommEventMask (USER.208)
793 SEGPTR WINAPI SetCommEventMask16(INT16 cid,UINT16 fuEvtMask)
795 struct DosDeviceStruct *ptr;
796 unsigned char *stol;
797 int repid;
798 unsigned int mstat;
800 TRACE("cid %d,mask %d\n",cid,fuEvtMask);
801 if ((ptr = GetDeviceStruct(cid)) == NULL) {
802 FIXME("no handle for cid = %0x!.\n",cid);
803 return (SEGPTR)NULL;
806 ptr->eventmask = fuEvtMask;
808 if ((cid&FLAG_LPT) || !ValidCOMPort(cid)) {
809 WARN(" cid %d not comm port\n",cid);
810 return (SEGPTR)NULL;
812 /* it's a COM port ? -> modify flags */
813 stol = (unsigned char *)unknown[cid] + COMM_MSR_OFFSET;
814 repid = ioctl(ptr->fd,TIOCMGET,&mstat);
815 TRACE(" ioctl %d, msr %x at %p %p\n",repid,mstat,stol,unknown[cid]);
816 COMM_MSRUpdate( stol, mstat);
818 TRACE(" modem dcd construct %x\n",*stol);
819 return SEGPTR_GET(unknown[cid]);
822 /*****************************************************************************
823 * GetCommEventMask (USER.209)
825 UINT16 WINAPI GetCommEventMask16(INT16 cid,UINT16 fnEvtClear)
827 struct DosDeviceStruct *ptr;
828 WORD events;
830 TRACE("cid %d, mask %d\n", cid, fnEvtClear);
831 if ((ptr = GetDeviceStruct(cid)) == NULL) {
832 FIXME("no handle for cid = %0x!.\n",cid);
833 return 0;
836 if ((cid&FLAG_LPT) || !ValidCOMPort(cid)) {
837 WARN(" cid %d not comm port\n",cid);
838 return 0;
841 events = *(WORD*)(unknown[cid]) & fnEvtClear;
842 *(WORD*)(unknown[cid]) &= ~fnEvtClear;
843 return events;
846 /*****************************************************************************
847 * SetCommState16 (USER.201)
849 INT16 WINAPI SetCommState16(LPDCB16 lpdcb)
851 struct termios port;
852 struct DosDeviceStruct *ptr;
854 TRACE("cid %d, ptr %p\n", lpdcb->Id, lpdcb);
855 if ((ptr = GetDeviceStruct(lpdcb->Id)) == NULL) {
856 FIXME("no handle for cid = %0x!.\n",lpdcb->Id);
857 return -1;
859 if (tcgetattr(ptr->fd, &port) == -1) {
860 ptr->commerror = WinError();
861 return -1;
864 port.c_cc[VMIN] = 0;
865 port.c_cc[VTIME] = 1;
867 #ifdef IMAXBEL
868 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|IMAXBEL);
869 #else
870 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR);
871 #endif
872 port.c_iflag |= (IGNBRK);
874 port.c_oflag &= ~(OPOST);
876 port.c_cflag &= ~(HUPCL);
877 port.c_cflag |= CLOCAL | CREAD;
879 port.c_lflag &= ~(ICANON|ECHO|ISIG);
880 port.c_lflag |= NOFLSH;
882 TRACE("baudrate %d\n",lpdcb->BaudRate);
883 #ifdef CBAUD
884 port.c_cflag &= ~CBAUD;
885 switch (lpdcb->BaudRate) {
886 case 110:
887 case CBR_110:
888 port.c_cflag |= B110;
889 break;
890 case 300:
891 case CBR_300:
892 port.c_cflag |= B300;
893 break;
894 case 600:
895 case CBR_600:
896 port.c_cflag |= B600;
897 break;
898 case 1200:
899 case CBR_1200:
900 port.c_cflag |= B1200;
901 break;
902 case 2400:
903 case CBR_2400:
904 port.c_cflag |= B2400;
905 break;
906 case 4800:
907 case CBR_4800:
908 port.c_cflag |= B4800;
909 break;
910 case 9600:
911 case CBR_9600:
912 port.c_cflag |= B9600;
913 break;
914 case 19200:
915 case CBR_19200:
916 port.c_cflag |= B19200;
917 break;
918 case 38400:
919 case CBR_38400:
920 port.c_cflag |= B38400;
921 break;
922 #ifdef B57600
923 case 57600:
924 port.c_cflag |= B57600;
925 break;
926 #endif
927 #ifdef B115200
928 case 57601:
929 port.c_cflag |= B115200;
930 break;
931 #endif
932 default:
933 ptr->commerror = IE_BAUDRATE;
934 return -1;
936 #elif !defined(__EMX__)
937 switch (lpdcb->BaudRate) {
938 case 110:
939 case CBR_110:
940 port.c_ospeed = B110;
941 break;
942 case 300:
943 case CBR_300:
944 port.c_ospeed = B300;
945 break;
946 case 600:
947 case CBR_600:
948 port.c_ospeed = B600;
949 break;
950 case 1200:
951 case CBR_1200:
952 port.c_ospeed = B1200;
953 break;
954 case 2400:
955 case CBR_2400:
956 port.c_ospeed = B2400;
957 break;
958 case 4800:
959 case CBR_4800:
960 port.c_ospeed = B4800;
961 break;
962 case 9600:
963 case CBR_9600:
964 port.c_ospeed = B9600;
965 break;
966 case 19200:
967 case CBR_19200:
968 port.c_ospeed = B19200;
969 break;
970 case 38400:
971 case CBR_38400:
972 port.c_ospeed = B38400;
973 break;
974 default:
975 ptr->commerror = IE_BAUDRATE;
976 return -1;
978 port.c_ispeed = port.c_ospeed;
979 #endif
980 TRACE("bytesize %d\n",lpdcb->ByteSize);
981 port.c_cflag &= ~CSIZE;
982 switch (lpdcb->ByteSize) {
983 case 5:
984 port.c_cflag |= CS5;
985 break;
986 case 6:
987 port.c_cflag |= CS6;
988 break;
989 case 7:
990 port.c_cflag |= CS7;
991 break;
992 case 8:
993 port.c_cflag |= CS8;
994 break;
995 default:
996 ptr->commerror = IE_BYTESIZE;
997 return -1;
1000 TRACE("fParity %d Parity %d\n",lpdcb->fParity, lpdcb->Parity);
1001 port.c_cflag &= ~(PARENB | PARODD);
1002 if (lpdcb->fParity)
1003 port.c_iflag |= INPCK;
1004 else
1005 port.c_iflag &= ~INPCK;
1006 switch (lpdcb->Parity) {
1007 case NOPARITY:
1008 break;
1009 case ODDPARITY:
1010 port.c_cflag |= (PARENB | PARODD);
1011 break;
1012 case EVENPARITY:
1013 port.c_cflag |= PARENB;
1014 break;
1015 default:
1016 ptr->commerror = IE_BYTESIZE;
1017 return -1;
1021 TRACE("stopbits %d\n",lpdcb->StopBits);
1023 switch (lpdcb->StopBits) {
1024 case ONESTOPBIT:
1025 port.c_cflag &= ~CSTOPB;
1026 break;
1027 case TWOSTOPBITS:
1028 port.c_cflag |= CSTOPB;
1029 break;
1030 default:
1031 ptr->commerror = IE_BYTESIZE;
1032 return -1;
1034 #ifdef CRTSCTS
1036 if (lpdcb->fDtrflow || lpdcb->fRtsflow || lpdcb->fOutxCtsFlow)
1037 port.c_cflag |= CRTSCTS;
1039 if (lpdcb->fDtrDisable)
1040 port.c_cflag &= ~CRTSCTS;
1041 #endif
1042 if (lpdcb->fInX)
1043 port.c_iflag |= IXON;
1044 else
1045 port.c_iflag &= ~IXON;
1046 if (lpdcb->fOutX)
1047 port.c_iflag |= IXOFF;
1048 else
1049 port.c_iflag &= ~IXOFF;
1051 ptr->evtchar = lpdcb->EvtChar;
1053 if (tcsetattr(ptr->fd, TCSADRAIN, &port) == -1) {
1054 ptr->commerror = WinError();
1055 return FALSE;
1056 } else {
1057 ptr->commerror = 0;
1058 return 0;
1062 /*****************************************************************************
1063 * GetCommState (USER.202)
1065 INT16 WINAPI GetCommState16(INT16 cid, LPDCB16 lpdcb)
1067 int speed;
1068 struct DosDeviceStruct *ptr;
1069 struct termios port;
1071 TRACE("cid %d, ptr %p\n", cid, lpdcb);
1072 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1073 FIXME("no handle for cid = %0x!.\n",cid);
1074 return -1;
1076 if (tcgetattr(ptr->fd, &port) == -1) {
1077 ptr->commerror = WinError();
1078 return -1;
1080 lpdcb->Id = cid;
1081 #ifndef __EMX__
1082 #ifdef CBAUD
1083 speed = port.c_cflag & CBAUD;
1084 #else
1085 speed = port.c_ospeed;
1086 #endif
1087 switch(speed) {
1088 case B110:
1089 lpdcb->BaudRate = 110;
1090 break;
1091 case B300:
1092 lpdcb->BaudRate = 300;
1093 break;
1094 case B600:
1095 lpdcb->BaudRate = 600;
1096 break;
1097 case B1200:
1098 lpdcb->BaudRate = 1200;
1099 break;
1100 case B2400:
1101 lpdcb->BaudRate = 2400;
1102 break;
1103 case B4800:
1104 lpdcb->BaudRate = 4800;
1105 break;
1106 case B9600:
1107 lpdcb->BaudRate = 9600;
1108 break;
1109 case B19200:
1110 lpdcb->BaudRate = 19200;
1111 break;
1112 case B38400:
1113 lpdcb->BaudRate = 38400;
1114 break;
1115 #ifdef B57600
1116 case B57600:
1117 lpdcb->BaudRate = 57600;
1118 break;
1119 #endif
1120 #ifdef B115200
1121 case B115200:
1122 lpdcb->BaudRate = 57601;
1123 break;
1124 #endif
1126 #endif
1127 switch (port.c_cflag & CSIZE) {
1128 case CS5:
1129 lpdcb->ByteSize = 5;
1130 break;
1131 case CS6:
1132 lpdcb->ByteSize = 6;
1133 break;
1134 case CS7:
1135 lpdcb->ByteSize = 7;
1136 break;
1137 case CS8:
1138 lpdcb->ByteSize = 8;
1139 break;
1142 if(port.c_iflag & INPCK)
1143 lpdcb->fParity = TRUE;
1144 else
1145 lpdcb->fParity = FALSE;
1146 switch (port.c_cflag & (PARENB | PARODD)) {
1147 case 0:
1148 lpdcb->Parity = NOPARITY;
1149 break;
1150 case PARENB:
1151 lpdcb->Parity = EVENPARITY;
1152 break;
1153 case (PARENB | PARODD):
1154 lpdcb->Parity = ODDPARITY;
1155 break;
1158 if (port.c_cflag & CSTOPB)
1159 lpdcb->StopBits = TWOSTOPBITS;
1160 else
1161 lpdcb->StopBits = ONESTOPBIT;
1163 lpdcb->RlsTimeout = 50;
1164 lpdcb->CtsTimeout = 50;
1165 lpdcb->DsrTimeout = 50;
1166 lpdcb->fNull = 0;
1167 lpdcb->fChEvt = 0;
1168 lpdcb->fBinary = 1;
1169 lpdcb->fDtrDisable = 0;
1171 #ifdef CRTSCTS
1173 if (port.c_cflag & CRTSCTS) {
1174 lpdcb->fDtrflow = 1;
1175 lpdcb->fRtsflow = 1;
1176 lpdcb->fOutxCtsFlow = 1;
1177 lpdcb->fOutxDsrFlow = 1;
1178 } else
1179 #endif
1180 lpdcb->fDtrDisable = 1;
1182 if (port.c_iflag & IXON)
1183 lpdcb->fInX = 1;
1184 else
1185 lpdcb->fInX = 0;
1187 if (port.c_iflag & IXOFF)
1188 lpdcb->fOutX = 1;
1189 else
1190 lpdcb->fOutX = 0;
1192 lpdcb->XonChar =
1193 lpdcb->XoffChar =
1195 lpdcb->XonLim = 10;
1196 lpdcb->XoffLim = 10;
1198 lpdcb->EvtChar = ptr->evtchar;
1200 ptr->commerror = 0;
1201 return 0;
1204 /*****************************************************************************
1205 * TransmitCommChar (USER.206)
1207 INT16 WINAPI TransmitCommChar16(INT16 cid,CHAR chTransmit)
1209 struct DosDeviceStruct *ptr;
1211 TRACE("cid %d, data %d \n", cid, chTransmit);
1212 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1213 FIXME("no handle for cid = %0x!.\n",cid);
1214 return -1;
1217 if (ptr->suspended) {
1218 ptr->commerror = IE_HARDWARE;
1219 return -1;
1222 if (ptr->xmit >= 0) {
1223 /* character already queued */
1224 /* FIXME: which error would Windows return? */
1225 ptr->commerror = CE_TXFULL;
1226 return -1;
1229 if (ptr->obuf_head == ptr->obuf_tail) {
1230 /* transmit queue empty, try to transmit directly */
1231 if (write(ptr->fd, &chTransmit, 1) == -1) {
1232 /* didn't work, queue it */
1233 ptr->xmit = chTransmit;
1234 comm_waitwrite(ptr);
1236 } else {
1237 /* data in queue, let this char be transmitted next */
1238 ptr->xmit = chTransmit;
1239 comm_waitwrite(ptr);
1242 ptr->commerror = 0;
1243 return 0;
1246 /*****************************************************************************
1247 * UngetCommChar (USER.212)
1249 INT16 WINAPI UngetCommChar16(INT16 cid,CHAR chUnget)
1251 struct DosDeviceStruct *ptr;
1253 TRACE("cid %d (char %d)\n", cid, chUnget);
1254 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1255 FIXME("no handle for cid = %0x!.\n",cid);
1256 return -1;
1259 if (ptr->suspended) {
1260 ptr->commerror = IE_HARDWARE;
1261 return -1;
1264 if (ptr->unget>=0) {
1265 /* character already queued */
1266 /* FIXME: which error would Windows return? */
1267 ptr->commerror = CE_RXOVER;
1268 return -1;
1271 ptr->unget = chUnget;
1273 ptr->commerror = 0;
1274 return 0;
1277 /*****************************************************************************
1278 * ReadComm (USER.204)
1280 INT16 WINAPI ReadComm16(INT16 cid,LPSTR lpvBuf,INT16 cbRead)
1282 int status, length;
1283 struct DosDeviceStruct *ptr;
1284 LPSTR orgBuf = lpvBuf;
1286 TRACE("cid %d, ptr %p, length %d\n", cid, lpvBuf, cbRead);
1287 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1288 FIXME("no handle for cid = %0x!.\n",cid);
1289 return -1;
1292 if (ptr->suspended) {
1293 ptr->commerror = IE_HARDWARE;
1294 return -1;
1297 /* read unget character */
1298 if (ptr->unget>=0) {
1299 *lpvBuf++ = ptr->unget;
1300 ptr->unget = -1;
1302 length = 1;
1303 } else
1304 length = 0;
1306 /* read from receive buffer */
1307 while (length < cbRead) {
1308 status = ((ptr->ibuf_head < ptr->ibuf_tail) ?
1309 ptr->ibuf_size : ptr->ibuf_head) - ptr->ibuf_tail;
1310 if (!status) break;
1311 if ((cbRead - length) < status)
1312 status = cbRead - length;
1314 memcpy(lpvBuf, ptr->inbuf + ptr->ibuf_tail, status);
1315 ptr->ibuf_tail += status;
1316 if (ptr->ibuf_tail >= ptr->ibuf_size)
1317 ptr->ibuf_tail = 0;
1318 lpvBuf += status;
1319 length += status;
1322 TRACE("%.*s\n", length, orgBuf);
1323 ptr->commerror = 0;
1324 return length;
1327 /*****************************************************************************
1328 * WriteComm (USER.205)
1330 INT16 WINAPI WriteComm16(INT16 cid, LPSTR lpvBuf, INT16 cbWrite)
1332 int status, length;
1333 struct DosDeviceStruct *ptr;
1335 TRACE("cid %d, ptr %p, length %d\n",
1336 cid, lpvBuf, cbWrite);
1337 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1338 FIXME("no handle for cid = %0x!.\n",cid);
1339 return -1;
1342 if (ptr->suspended) {
1343 ptr->commerror = IE_HARDWARE;
1344 return -1;
1347 TRACE("%.*s\n", cbWrite, lpvBuf );
1349 length = 0;
1350 while (length < cbWrite) {
1351 if ((ptr->obuf_head == ptr->obuf_tail) && (ptr->xmit < 0)) {
1352 /* no data queued, try to write directly */
1353 status = write(ptr->fd, lpvBuf, cbWrite - length);
1354 if (status > 0) {
1355 lpvBuf += status;
1356 length += status;
1357 continue;
1360 /* can't write directly, put into transmit buffer */
1361 status = ((ptr->obuf_tail > ptr->obuf_head) ?
1362 (ptr->obuf_tail-1) : ptr->obuf_size) - ptr->obuf_head;
1363 if (!status) break;
1364 if ((cbWrite - length) < status)
1365 status = cbWrite - length;
1366 memcpy(lpvBuf, ptr->outbuf + ptr->obuf_head, status);
1367 ptr->obuf_head += status;
1368 if (ptr->obuf_head >= ptr->obuf_size)
1369 ptr->obuf_head = 0;
1370 lpvBuf += status;
1371 length += status;
1372 comm_waitwrite(ptr);
1375 ptr->commerror = 0;
1376 return length;
1379 /***********************************************************************
1380 * EnableCommNotification (USER.246)
1382 BOOL16 WINAPI EnableCommNotification16( INT16 cid, HWND16 hwnd,
1383 INT16 cbWriteNotify, INT16 cbOutQueue )
1385 struct DosDeviceStruct *ptr;
1387 TRACE("(%d, %x, %d, %d)\n", cid, hwnd, cbWriteNotify, cbOutQueue);
1388 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1389 FIXME("no handle for cid = %0x!.\n",cid);
1390 return -1;
1392 ptr->wnd = hwnd;
1393 ptr->n_read = cbWriteNotify;
1394 ptr->n_write = cbOutQueue;
1395 return TRUE;
1399 /**************************************************************************
1400 * BuildCommDCBA (KERNEL32.14)
1402 BOOL WINAPI BuildCommDCBA(LPCSTR device,LPDCB lpdcb)
1404 return BuildCommDCBAndTimeoutsA(device,lpdcb,NULL);
1407 /**************************************************************************
1408 * BuildCommDCBAndTimeoutsA (KERNEL32.15)
1410 BOOL WINAPI BuildCommDCBAndTimeoutsA(LPCSTR device, LPDCB lpdcb,
1411 LPCOMMTIMEOUTS lptimeouts)
1413 int port;
1414 char *ptr,*temp;
1416 TRACE("(%s,%p,%p)\n",device,lpdcb,lptimeouts);
1418 if (!lstrncmpiA(device,"COM",3)) {
1419 port=device[3]-'0';
1420 if (port--==0) {
1421 ERR("BUG! COM0 can't exists!.\n");
1422 return FALSE;
1424 if (!ValidCOMPort(port))
1425 return FALSE;
1426 if (*(device+4)!=':')
1427 return FALSE;
1428 temp=(LPSTR)(device+5);
1429 } else
1430 temp=(LPSTR)device;
1432 memset(lpdcb, 0, sizeof(DCB)); /* initialize */
1434 lpdcb->DCBlength = sizeof(DCB);
1435 if (strchr(temp,',')) { /* old style */
1436 DCB16 dcb16;
1437 BOOL16 ret;
1438 char last=temp[strlen(temp)-1];
1440 ret=BuildCommDCB16(device,&dcb16);
1441 if (!ret)
1442 return FALSE;
1443 lpdcb->BaudRate = dcb16.BaudRate;
1444 lpdcb->ByteSize = dcb16.ByteSize;
1445 lpdcb->fBinary = dcb16.fBinary;
1446 lpdcb->Parity = dcb16.Parity;
1447 lpdcb->fParity = dcb16.fParity;
1448 lpdcb->fNull = dcb16.fNull;
1449 lpdcb->StopBits = dcb16.StopBits;
1450 if (last == 'x') {
1451 lpdcb->fInX = TRUE;
1452 lpdcb->fOutX = TRUE;
1453 lpdcb->fOutxCtsFlow = FALSE;
1454 lpdcb->fOutxDsrFlow = FALSE;
1455 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
1456 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
1457 } else if (last=='p') {
1458 lpdcb->fInX = FALSE;
1459 lpdcb->fOutX = FALSE;
1460 lpdcb->fOutxCtsFlow = TRUE;
1461 lpdcb->fOutxDsrFlow = TRUE;
1462 lpdcb->fDtrControl = DTR_CONTROL_HANDSHAKE;
1463 lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
1464 } else {
1465 lpdcb->fInX = FALSE;
1466 lpdcb->fOutX = FALSE;
1467 lpdcb->fOutxCtsFlow = FALSE;
1468 lpdcb->fOutxDsrFlow = FALSE;
1469 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
1470 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
1472 lpdcb->XonChar = dcb16.XonChar;
1473 lpdcb->XoffChar = dcb16.XoffChar;
1474 lpdcb->ErrorChar= dcb16.PeChar;
1475 lpdcb->fErrorChar= dcb16.fPeChar;
1476 lpdcb->EofChar = dcb16.EofChar;
1477 lpdcb->EvtChar = dcb16.EvtChar;
1478 lpdcb->XonLim = dcb16.XonLim;
1479 lpdcb->XoffLim = dcb16.XoffLim;
1480 return TRUE;
1482 ptr=strtok(temp," ");
1483 while (ptr) {
1484 DWORD flag,x;
1486 flag=0;
1487 if (!strncmp("baud=",ptr,5)) {
1488 if (!sscanf(ptr+5,"%ld",&x))
1489 WARN("Couldn't parse %s\n",ptr);
1490 lpdcb->BaudRate = x;
1491 flag=1;
1493 if (!strncmp("stop=",ptr,5)) {
1494 if (!sscanf(ptr+5,"%ld",&x))
1495 WARN("Couldn't parse %s\n",ptr);
1496 lpdcb->StopBits = x;
1497 flag=1;
1499 if (!strncmp("data=",ptr,5)) {
1500 if (!sscanf(ptr+5,"%ld",&x))
1501 WARN("Couldn't parse %s\n",ptr);
1502 lpdcb->ByteSize = x;
1503 flag=1;
1505 if (!strncmp("parity=",ptr,7)) {
1506 lpdcb->fParity = TRUE;
1507 switch (ptr[8]) {
1508 case 'N':case 'n':
1509 lpdcb->fParity = FALSE;
1510 lpdcb->Parity = NOPARITY;
1511 break;
1512 case 'E':case 'e':
1513 lpdcb->Parity = EVENPARITY;
1514 break;
1515 case 'O':case 'o':
1516 lpdcb->Parity = ODDPARITY;
1517 break;
1518 case 'M':case 'm':
1519 lpdcb->Parity = MARKPARITY;
1520 break;
1522 flag=1;
1524 if (!flag)
1525 ERR("Unhandled specifier '%s', please report.\n",ptr);
1526 ptr=strtok(NULL," ");
1528 if (lpdcb->BaudRate==110)
1529 lpdcb->StopBits = 2;
1530 return TRUE;
1533 /**************************************************************************
1534 * BuildCommDCBAndTimeoutsW (KERNEL32.16)
1536 BOOL WINAPI BuildCommDCBAndTimeoutsW( LPCWSTR devid, LPDCB lpdcb,
1537 LPCOMMTIMEOUTS lptimeouts )
1539 LPSTR devidA;
1540 BOOL ret;
1542 TRACE("(%p,%p,%p)\n",devid,lpdcb,lptimeouts);
1543 devidA = HEAP_strdupWtoA( GetProcessHeap(), 0, devid );
1544 ret=BuildCommDCBAndTimeoutsA(devidA,lpdcb,lptimeouts);
1545 HeapFree( GetProcessHeap(), 0, devidA );
1546 return ret;
1549 /**************************************************************************
1550 * BuildCommDCBW (KERNEL32.17)
1552 BOOL WINAPI BuildCommDCBW(LPCWSTR devid,LPDCB lpdcb)
1554 return BuildCommDCBAndTimeoutsW(devid,lpdcb,NULL);
1557 /*****************************************************************************
1558 * COMM_GetReadFd
1559 * Returns a file descriptor for reading.
1560 * Make sure to close the handle afterwards!
1562 static int COMM_GetReadFd( HANDLE handle)
1564 int fd;
1565 struct get_read_fd_request *req = get_req_buffer();
1566 req->handle = handle;
1567 server_call_fd( REQ_GET_READ_FD, -1, &fd );
1568 return fd;
1571 /*****************************************************************************
1572 * COMM_GetWriteFd
1573 * Returns a file descriptor for writing.
1574 * Make sure to close the handle afterwards!
1576 static int COMM_GetWriteFd( HANDLE handle)
1578 int fd = -1;
1579 struct get_write_fd_request *req = get_req_buffer();
1580 req->handle = handle;
1581 server_call_fd( REQ_GET_WRITE_FD, -1, &fd );
1582 return fd;
1585 /* FIXME: having these global for win32 for now */
1586 int commerror=0,eventmask=0;
1588 /*****************************************************************************
1589 * SetCommBreak (KERNEL32.449)
1591 BOOL WINAPI SetCommBreak(HANDLE handle)
1593 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1594 int fd,result;
1596 fd = COMM_GetWriteFd(handle);
1597 if(fd<0) {
1598 TRACE("COMM_GetWriteFd failed\n");
1599 return FALSE;
1601 result = ioctl(fd,TIOCSBRK,0);
1602 close(fd);
1603 if (result ==-1)
1605 TRACE("ioctl failed\n");
1606 SetLastError(ERROR_NOT_SUPPORTED);
1607 return FALSE;
1609 return TRUE;
1610 #else
1611 FIXME("ioctl not available\n");
1612 SetLastError(ERROR_NOT_SUPPORTED);
1613 return FALSE;
1614 #endif
1617 /*****************************************************************************
1618 * ClearCommBreak (KERNEL32.20)
1620 BOOL WINAPI ClearCommBreak(HANDLE handle)
1622 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1623 int fd,result;
1625 fd = COMM_GetWriteFd(handle);
1626 if(fd<0) {
1627 TRACE("COMM_GetWriteFd failed\n");
1628 return FALSE;
1630 result = ioctl(fd,TIOCCBRK,0);
1631 close(fd);
1632 if (result ==-1)
1634 TRACE("ioctl failed\n");
1635 SetLastError(ERROR_NOT_SUPPORTED);
1636 return FALSE;
1638 return TRUE;
1639 #else
1640 FIXME("ioctl not available\n");
1641 SetLastError(ERROR_NOT_SUPPORTED);
1642 return FALSE;
1643 #endif
1646 /*****************************************************************************
1647 * EscapeCommFunction (KERNEL32.214)
1649 BOOL WINAPI EscapeCommFunction(HANDLE handle,UINT nFunction)
1651 int fd,direct=FALSE,result=FALSE;
1652 struct termios port;
1654 TRACE("handle %d, function=%d\n", handle, nFunction);
1655 fd = COMM_GetWriteFd(handle);
1656 if(fd<0) {
1657 FIXME("handle %d not found.\n",handle);
1658 return FALSE;
1661 if (tcgetattr(fd,&port) == -1) {
1662 commerror=WinError();
1663 close(fd);
1664 return FALSE;
1667 switch (nFunction) {
1668 case RESETDEV:
1669 TRACE("\n");
1670 break;
1672 case CLRDTR:
1673 TRACE("CLRDTR\n");
1674 #ifdef TIOCM_DTR
1675 direct=TRUE;
1676 result= COMM_WhackModem(fd, ~TIOCM_DTR, 0);
1677 break;
1678 #endif
1680 case CLRRTS:
1681 TRACE("CLRRTS\n");
1682 #ifdef TIOCM_RTS
1683 direct=TRUE;
1684 result= COMM_WhackModem(fd, ~TIOCM_RTS, 0);
1685 break;
1686 #endif
1688 case SETDTR:
1689 TRACE("SETDTR\n");
1690 #ifdef TIOCM_DTR
1691 direct=TRUE;
1692 result= COMM_WhackModem(fd, 0, TIOCM_DTR);
1693 break;
1694 #endif
1696 case SETRTS:
1697 TRACE("SETRTS\n");
1698 #ifdef TIOCM_DTR
1699 direct=TRUE;
1700 result= COMM_WhackModem(fd, 0, TIOCM_RTS);
1701 break;
1702 #endif
1704 case SETXOFF:
1705 TRACE("SETXOFF\n");
1706 port.c_iflag |= IXOFF;
1707 break;
1709 case SETXON:
1710 TRACE("SETXON\n");
1711 port.c_iflag |= IXON;
1712 break;
1713 case SETBREAK:
1714 TRACE("setbreak\n");
1715 #ifdef TIOCSBRK
1716 direct=TRUE;
1717 result = ioctl(fd,TIOCSBRK,0);
1718 break;
1719 #endif
1720 case CLRBREAK:
1721 TRACE("clrbreak\n");
1722 #ifdef TIOCSBRK
1723 direct=TRUE;
1724 result = ioctl(fd,TIOCCBRK,0);
1725 break;
1726 #endif
1727 default:
1728 WARN("(handle=%d,nFunction=%d): Unknown function\n",
1729 handle, nFunction);
1730 break;
1733 if (!direct)
1734 if (tcsetattr(fd, TCSADRAIN, &port) == -1) {
1735 commerror = WinError();
1736 close(fd);
1737 return FALSE;
1738 } else
1739 result= TRUE;
1740 else
1742 if (result == -1)
1744 result= FALSE;
1745 commerror=WinError();
1747 else
1748 result = TRUE;
1750 close(fd);
1751 return result;
1754 /********************************************************************
1755 * PurgeComm (KERNEL32.557)
1757 BOOL WINAPI PurgeComm( HANDLE handle, DWORD flags)
1759 int fd;
1761 TRACE("handle %d, flags %lx\n", handle, flags);
1763 fd = COMM_GetWriteFd(handle);
1764 if(fd<0) {
1765 FIXME("no handle %d found\n",handle);
1766 return FALSE;
1770 ** not exactly sure how these are different
1771 ** Perhaps if we had our own internal queues, one flushes them
1772 ** and the other flushes the kernel's buffers.
1774 if(flags&PURGE_TXABORT)
1775 tcflush(fd,TCOFLUSH);
1776 if(flags&PURGE_RXABORT)
1777 tcflush(fd,TCIFLUSH);
1778 if(flags&PURGE_TXCLEAR)
1779 tcflush(fd,TCOFLUSH);
1780 if(flags&PURGE_RXCLEAR)
1781 tcflush(fd,TCIFLUSH);
1782 close(fd);
1784 return 1;
1787 /*****************************************************************************
1788 * ClearCommError (KERNEL32.21)
1790 BOOL WINAPI ClearCommError(INT handle,LPDWORD errors,LPCOMSTAT lpStat)
1792 int fd;
1794 fd=COMM_GetReadFd(handle);
1795 if(0>fd)
1797 FIXME("no handle %d found\n",handle);
1798 return FALSE;
1801 if (lpStat)
1803 lpStat->status = 0;
1805 #ifdef TIOCOUTQ
1806 if(ioctl(fd, TIOCOUTQ, &lpStat->cbOutQue))
1807 WARN("ioctl returned error\n");
1808 #else
1809 lpStat->cbOutQue = 0; /* FIXME: find a different way to find out */
1810 #endif
1812 if(ioctl(fd, TIOCINQ, &lpStat->cbInQue))
1813 WARN("ioctl returned error\n");
1815 TRACE("handle %d cbInQue = %ld cbOutQue = %ld\n",
1816 handle, lpStat->cbInQue, lpStat->cbOutQue);
1819 close(fd);
1821 if(errors)
1822 *errors = 0;
1825 ** After an asynchronous write opperation, the
1826 ** app will call ClearCommError to see if the
1827 ** results are ready yet. It waits for ERROR_IO_PENDING
1829 commerror = ERROR_IO_PENDING;
1831 return TRUE;
1834 /*****************************************************************************
1835 * SetupComm (KERNEL32.676)
1837 BOOL WINAPI SetupComm( HANDLE handle, DWORD insize, DWORD outsize)
1839 int fd;
1841 FIXME("insize %ld outsize %ld unimplemented stub\n", insize, outsize);
1842 fd=COMM_GetWriteFd(handle);
1843 if(0>fd) {
1844 FIXME("handle %d not found?\n",handle);
1845 return FALSE;
1847 close(fd);
1848 return TRUE;
1851 /*****************************************************************************
1852 * GetCommMask (KERNEL32.156)
1854 BOOL WINAPI GetCommMask(HANDLE handle,LPDWORD evtmask)
1856 int fd;
1858 TRACE("handle %d, mask %p\n", handle, evtmask);
1859 if(0>(fd=COMM_GetReadFd(handle)))
1861 FIXME("no handle %d found\n",handle);
1862 return FALSE;
1864 close(fd);
1865 *evtmask = eventmask;
1866 TRACE("%s%s%s%s%s%s%s%s%s\n",
1867 (eventmask&EV_BREAK)?"EV_BREAK":"",
1868 (eventmask&EV_CTS)?"EV_CTS":"",
1869 (eventmask&EV_DSR)?"EV_DSR":"",
1870 (eventmask&EV_ERR)?"EV_ERR":"",
1871 (eventmask&EV_RING)?"EV_RING":"",
1872 (eventmask&EV_RLSD)?"EV_RLSD":"",
1873 (eventmask&EV_RXCHAR)?"EV_RXCHAR":"",
1874 (eventmask&EV_RXFLAG)?"EV_RXFLAG":"",
1875 (eventmask&EV_TXEMPTY)?"EV_TXEMPTY":"");
1877 return TRUE;
1880 /*****************************************************************************
1881 * SetCommMask (KERNEL32.451)
1883 BOOL WINAPI SetCommMask(INT handle,DWORD evtmask)
1885 int fd;
1887 TRACE("handle %d, mask %lx\n", handle, evtmask);
1888 TRACE("%s%s%s%s%s%s%s%s%s\n",
1889 (evtmask&EV_BREAK)?"EV_BREAK":"",
1890 (evtmask&EV_CTS)?"EV_CTS":"",
1891 (evtmask&EV_DSR)?"EV_DSR":"",
1892 (evtmask&EV_ERR)?"EV_ERR":"",
1893 (evtmask&EV_RING)?"EV_RING":"",
1894 (evtmask&EV_RLSD)?"EV_RLSD":"",
1895 (evtmask&EV_RXCHAR)?"EV_RXCHAR":"",
1896 (evtmask&EV_RXFLAG)?"EV_RXFLAG":"",
1897 (evtmask&EV_TXEMPTY)?"EV_TXEMPTY":"");
1899 if(0>(fd=COMM_GetWriteFd(handle))) {
1900 FIXME("no handle %d found\n",handle);
1901 return FALSE;
1903 close(fd);
1904 eventmask = evtmask;
1905 return TRUE;
1908 /*****************************************************************************
1909 * SetCommState (KERNEL32.452)
1911 BOOL WINAPI SetCommState(INT handle,LPDCB lpdcb)
1913 struct termios port;
1914 int fd;
1916 TRACE("handle %d, ptr %p\n", handle, lpdcb);
1917 TRACE("bytesize %d baudrate %ld fParity %d Parity %d stopbits %d\n",
1918 lpdcb->ByteSize,lpdcb->BaudRate,lpdcb->fParity, lpdcb->Parity,
1919 (lpdcb->StopBits == ONESTOPBIT)?1:
1920 (lpdcb->StopBits == TWOSTOPBITS)?2:0);
1921 TRACE("%s %s\n",(lpdcb->fInX)?"IXON":"~IXON",
1922 (lpdcb->fOutX)?"IXOFF":"~IXOFF");
1924 if ((fd = COMM_GetWriteFd(handle)) < 0) {
1925 FIXME("no handle %d found\n",handle);
1926 return FALSE;
1929 if ((tcgetattr(fd,&port)) == -1) {
1930 int save_error = errno;
1931 commerror = WinError();
1932 close( fd );
1933 #ifdef HAVE_STRERROR
1934 ERR("tcgetattr error '%s'\n", strerror(save_error));
1935 #else
1936 ERR("tcgetattr error %d\n", save_error);
1937 #endif
1938 return FALSE;
1941 port.c_cc[VMIN] = 0;
1942 port.c_cc[VTIME] = 1;
1944 #ifdef IMAXBEL
1945 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|IMAXBEL);
1946 #else
1947 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR);
1948 #endif
1949 port.c_iflag |= (IGNBRK);
1951 port.c_oflag &= ~(OPOST);
1953 port.c_cflag &= ~(HUPCL);
1954 port.c_cflag |= CLOCAL | CREAD;
1956 port.c_lflag &= ~(ICANON|ECHO|ISIG);
1957 port.c_lflag |= NOFLSH;
1960 ** MJM - removed default baudrate settings
1961 ** TRACE(comm,"baudrate %ld\n",lpdcb->BaudRate);
1963 #ifdef CBAUD
1964 port.c_cflag &= ~CBAUD;
1965 switch (lpdcb->BaudRate) {
1966 case 110:
1967 case CBR_110:
1968 port.c_cflag |= B110;
1969 break;
1970 case 300:
1971 case CBR_300:
1972 port.c_cflag |= B300;
1973 break;
1974 case 600:
1975 case CBR_600:
1976 port.c_cflag |= B600;
1977 break;
1978 case 1200:
1979 case CBR_1200:
1980 port.c_cflag |= B1200;
1981 break;
1982 case 2400:
1983 case CBR_2400:
1984 port.c_cflag |= B2400;
1985 break;
1986 case 4800:
1987 case CBR_4800:
1988 port.c_cflag |= B4800;
1989 break;
1990 case 9600:
1991 case CBR_9600:
1992 port.c_cflag |= B9600;
1993 break;
1994 case 19200:
1995 case CBR_19200:
1996 port.c_cflag |= B19200;
1997 break;
1998 case 38400:
1999 case CBR_38400:
2000 port.c_cflag |= B38400;
2001 break;
2002 #ifdef B57600
2003 case 57600:
2004 port.c_cflag |= B57600;
2005 break;
2006 #endif
2007 #ifdef B115200
2008 case 115200:
2009 port.c_cflag |= B115200;
2010 break;
2011 #endif
2012 #ifdef B230400
2013 case 230400:
2014 port.c_cflag |= B230400;
2015 break;
2016 #endif
2017 #ifdef B460800
2018 case 460600:
2019 port.c_cflag |= B460800;
2020 break;
2021 #endif
2022 default:
2023 commerror = IE_BAUDRATE;
2024 close( fd );
2025 ERR("baudrate %ld\n",lpdcb->BaudRate);
2026 return FALSE;
2028 #elif !defined(__EMX__)
2029 switch (lpdcb->BaudRate) {
2030 case 110:
2031 case CBR_110:
2032 port.c_ospeed = B110;
2033 break;
2034 case 300:
2035 case CBR_300:
2036 port.c_ospeed = B300;
2037 break;
2038 case 600:
2039 case CBR_600:
2040 port.c_ospeed = B600;
2041 break;
2042 case 1200:
2043 case CBR_1200:
2044 port.c_ospeed = B1200;
2045 break;
2046 case 2400:
2047 case CBR_2400:
2048 port.c_ospeed = B2400;
2049 break;
2050 case 4800:
2051 case CBR_4800:
2052 port.c_ospeed = B4800;
2053 break;
2054 case 9600:
2055 case CBR_9600:
2056 port.c_ospeed = B9600;
2057 break;
2058 case 19200:
2059 case CBR_19200:
2060 port.c_ospeed = B19200;
2061 break;
2062 case 38400:
2063 case CBR_38400:
2064 port.c_ospeed = B38400;
2065 break;
2066 default:
2067 commerror = IE_BAUDRATE;
2068 close( fd );
2069 ERR("baudrate %d \n",lpdcb->BaudRate);
2070 return FALSE;
2072 port.c_ispeed = port.c_ospeed;
2073 #endif
2074 port.c_cflag &= ~CSIZE;
2075 switch (lpdcb->ByteSize) {
2076 case 5:
2077 port.c_cflag |= CS5;
2078 break;
2079 case 6:
2080 port.c_cflag |= CS6;
2081 break;
2082 case 7:
2083 port.c_cflag |= CS7;
2084 break;
2085 case 8:
2086 port.c_cflag |= CS8;
2087 break;
2088 default:
2089 commerror = IE_BYTESIZE;
2090 close( fd );
2091 ERR("ByteSize\n");
2092 return FALSE;
2095 port.c_cflag &= ~(PARENB | PARODD);
2096 if (lpdcb->fParity)
2097 port.c_iflag |= INPCK;
2098 else
2099 port.c_iflag &= ~INPCK;
2100 switch (lpdcb->Parity) {
2101 case NOPARITY:
2102 break;
2103 case ODDPARITY:
2104 port.c_cflag |= (PARENB | PARODD);
2105 break;
2106 case EVENPARITY:
2107 port.c_cflag |= PARENB;
2108 break;
2109 default:
2110 commerror = IE_BYTESIZE;
2111 close( fd );
2112 ERR("Parity\n");
2113 return FALSE;
2117 switch (lpdcb->StopBits) {
2118 case ONESTOPBIT:
2119 port.c_cflag &= ~CSTOPB;
2120 break;
2121 case TWOSTOPBITS:
2122 port.c_cflag |= CSTOPB;
2123 break;
2124 default:
2125 commerror = IE_BYTESIZE;
2126 close( fd );
2127 ERR("StopBits\n");
2128 return FALSE;
2130 #ifdef CRTSCTS
2131 if ( lpdcb->fOutxCtsFlow ||
2132 lpdcb->fDtrControl == DTR_CONTROL_ENABLE||
2133 lpdcb->fRtsControl == RTS_CONTROL_ENABLE
2136 port.c_cflag |= CRTSCTS;
2137 TRACE("CRTSCTS\n");
2140 if (lpdcb->fDtrControl == DTR_CONTROL_DISABLE)
2142 port.c_cflag &= ~CRTSCTS;
2143 TRACE("~CRTSCTS\n");
2146 #endif
2147 if (lpdcb->fInX)
2148 port.c_iflag |= IXON;
2149 else
2150 port.c_iflag &= ~IXON;
2151 if (lpdcb->fOutX)
2152 port.c_iflag |= IXOFF;
2153 else
2154 port.c_iflag &= ~IXOFF;
2156 if (tcsetattr(fd,TCSANOW,&port)==-1) { /* otherwise it hangs with pending input*/
2157 int save_error=errno;
2158 commerror = WinError();
2159 close( fd );
2160 #ifdef HAVE_STRERROR
2161 ERR("tcgetattr error '%s'\n", strerror(save_error));
2162 #else
2163 ERR("tcgetattr error %d\n", save_error);
2164 #endif
2165 return FALSE;
2166 } else {
2167 commerror = 0;
2168 close( fd );
2169 return TRUE;
2174 /*****************************************************************************
2175 * GetCommState (KERNEL32.159)
2177 BOOL WINAPI GetCommState(INT handle, LPDCB lpdcb)
2179 struct termios port;
2180 int fd,speed;
2182 TRACE("handle %d, ptr %p\n", handle, lpdcb);
2184 if ((fd = COMM_GetReadFd(handle)) < 0)
2186 ERR("can't get COMM_GetReadFd\n");
2187 return FALSE;
2189 if (tcgetattr(fd, &port) == -1) {
2190 int save_error=errno;
2191 #ifdef HAVE_STRERROR
2192 ERR("tcgetattr error '%s'\n", strerror(save_error));
2193 #else
2194 ERR("tcgetattr error %d\n", save_error);
2195 #endif
2196 commerror = WinError();
2197 close( fd );
2198 return FALSE;
2200 close( fd );
2201 #ifndef __EMX__
2202 #ifdef CBAUD
2203 speed= (port.c_cflag & CBAUD);
2204 #else
2205 speed= (cfgetospeed(&port));
2206 #endif
2207 switch (speed) {
2208 case B110:
2209 lpdcb->BaudRate = 110;
2210 break;
2211 case B300:
2212 lpdcb->BaudRate = 300;
2213 break;
2214 case B600:
2215 lpdcb->BaudRate = 600;
2216 break;
2217 case B1200:
2218 lpdcb->BaudRate = 1200;
2219 break;
2220 case B2400:
2221 lpdcb->BaudRate = 2400;
2222 break;
2223 case B4800:
2224 lpdcb->BaudRate = 4800;
2225 break;
2226 case B9600:
2227 lpdcb->BaudRate = 9600;
2228 break;
2229 case B19200:
2230 lpdcb->BaudRate = 19200;
2231 break;
2232 case B38400:
2233 lpdcb->BaudRate = 38400;
2234 break;
2235 #ifdef B57600
2236 case B57600:
2237 lpdcb->BaudRate = 57600;
2238 break;
2239 #endif
2240 #ifdef B115200
2241 case B115200:
2242 lpdcb->BaudRate = 115200;
2243 break;
2244 #endif
2245 #ifdef B230400
2246 case B230400:
2247 lpdcb->BaudRate = 230400;
2248 break;
2249 #endif
2250 #ifdef B460800
2251 case B460800:
2252 lpdcb->BaudRate = 460800;
2253 break;
2254 #endif
2255 default:
2256 ERR("unknown speed %x \n",speed);
2258 #endif
2259 switch (port.c_cflag & CSIZE) {
2260 case CS5:
2261 lpdcb->ByteSize = 5;
2262 break;
2263 case CS6:
2264 lpdcb->ByteSize = 6;
2265 break;
2266 case CS7:
2267 lpdcb->ByteSize = 7;
2268 break;
2269 case CS8:
2270 lpdcb->ByteSize = 8;
2271 break;
2272 default:
2273 ERR("unknown size %x \n",port.c_cflag & CSIZE);
2276 if(port.c_iflag & INPCK)
2277 lpdcb->fParity = TRUE;
2278 else
2279 lpdcb->fParity = FALSE;
2280 switch (port.c_cflag & (PARENB | PARODD)) {
2281 case 0:
2282 lpdcb->Parity = NOPARITY;
2283 break;
2284 case PARENB:
2285 lpdcb->Parity = EVENPARITY;
2286 break;
2287 case (PARENB | PARODD):
2288 lpdcb->Parity = ODDPARITY;
2289 break;
2292 if (port.c_cflag & CSTOPB)
2293 lpdcb->StopBits = TWOSTOPBITS;
2294 else
2295 lpdcb->StopBits = ONESTOPBIT;
2297 lpdcb->fNull = 0;
2298 lpdcb->fBinary = 1;
2300 #ifdef CRTSCTS
2302 if (port.c_cflag & CRTSCTS) {
2303 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
2304 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
2305 lpdcb->fOutxCtsFlow = 1;
2306 lpdcb->fOutxDsrFlow = 1;
2307 } else
2308 #endif
2310 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
2311 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
2313 if (port.c_iflag & IXON)
2314 lpdcb->fInX = 1;
2315 else
2316 lpdcb->fInX = 0;
2318 if (port.c_iflag & IXOFF)
2319 lpdcb->fOutX = 1;
2320 else
2321 lpdcb->fOutX = 0;
2323 lpdcb->XonChar =
2324 lpdcb->XoffChar =
2326 lpdcb->XonLim = 10;
2327 lpdcb->XoffLim = 10;
2329 commerror = 0;
2331 TRACE("OK\n");
2333 TRACE("bytesize %d baudrate %ld fParity %d Parity %d stopbits %d\n",
2334 lpdcb->ByteSize,lpdcb->BaudRate,lpdcb->fParity, lpdcb->Parity,
2335 (lpdcb->StopBits == ONESTOPBIT)?1:
2336 (lpdcb->StopBits == TWOSTOPBITS)?2:0);
2337 TRACE("%s %s\n",(lpdcb->fInX)?"IXON":"~IXON",
2338 (lpdcb->fOutX)?"IXOFF":"~IXOFF");
2339 #ifdef CRTSCTS
2340 if ( lpdcb->fOutxCtsFlow ||
2341 lpdcb->fDtrControl == DTR_CONTROL_ENABLE||
2342 lpdcb->fRtsControl == RTS_CONTROL_ENABLE
2344 TRACE("CRTSCTS\n");
2346 if (lpdcb->fDtrControl == DTR_CONTROL_DISABLE)
2347 TRACE("~CRTSCTS\n");
2349 #endif
2350 return TRUE;
2353 /*****************************************************************************
2354 * TransmitCommChar (KERNEL32.535)
2356 BOOL WINAPI TransmitCommChar(INT cid,CHAR chTransmit)
2358 struct DosDeviceStruct *ptr;
2360 FIXME("(%d,'%c'), use win32 handle!\n",cid,chTransmit);
2361 if ((ptr = GetDeviceStruct(cid)) == NULL)
2362 FIXME("no handle for cid = %0x!.\n",cid);
2363 return FALSE;
2365 if (ptr->suspended) {
2366 ptr->commerror = IE_HARDWARE;
2367 return FALSE;
2369 if (write(ptr->fd, (void *) &chTransmit, 1) == -1) {
2370 ptr->commerror = WinError();
2371 return FALSE;
2372 } else {
2373 ptr->commerror = 0;
2374 return TRUE;
2378 /*****************************************************************************
2379 * GetCommTimeouts (KERNEL32.160)
2381 BOOL WINAPI GetCommTimeouts(HANDLE hcom,LPCOMMTIMEOUTS lptimeouts)
2383 FIXME("(%x,%p):stub.\n",hcom,lptimeouts);
2384 return TRUE;
2387 /*****************************************************************************
2388 * SetCommTimeouts (KERNEL32.453)
2390 BOOL WINAPI SetCommTimeouts(HANDLE hcom,LPCOMMTIMEOUTS lptimeouts) {
2391 /* struct DosDeviceStruct *ptr; */
2392 struct termios tios;
2393 int fd;
2395 FIXME("(%x,%p):stub.\n",hcom,lptimeouts);
2397 if ((ptr = GetDeviceStruct(hcom)) == NULL) {
2398 FIXME("no handle for cid = %0x!.\n",hcom);
2399 return FALSE;
2403 fd = COMM_GetWriteFd(hcom);
2404 if (fd < 0) {
2405 FIXME("no fd for cid = %0x!.\n",hcom);
2406 return FALSE;
2410 FIXME("ReadIntervalTimeout %ld\n",lptimeouts->ReadIntervalTimeout);
2411 FIXME("ReadTotalTimeoutMultiplier %ld\n",lptimeouts->ReadTotalTimeoutMultiplier);
2412 FIXME("ReadTotalTimeoutConstant %ld\n",lptimeouts->ReadTotalTimeoutConstant);
2413 FIXME("WriteTotalTimeoutMultiplier %ld\n",lptimeouts->WriteTotalTimeoutMultiplier);
2414 FIXME("WriteTotalTimeoutConstant %ld\n",lptimeouts->WriteTotalTimeoutConstant);
2417 if (-1==tcgetattr(fd,&tios)) {
2418 FIXME("tcgetattr on fd %d failed!\n",fd);
2419 return FALSE;
2421 /* VTIME is in 1/10 seconds */
2422 tios.c_cc[VTIME]= (lptimeouts->ReadIntervalTimeout+99)/100;
2423 if (-1==tcsetattr(fd,0,&tios)) {
2424 FIXME("tcsetattr on fd %d failed!\n",fd);
2425 return FALSE;
2427 return TRUE;
2430 /***********************************************************************
2431 * GetCommModemStatus (KERNEL32.285)
2433 BOOL WINAPI GetCommModemStatus(HANDLE hFile,LPDWORD lpModemStat )
2435 int fd,mstat, result=FALSE;
2437 *lpModemStat=0;
2438 #ifdef TIOCMGET
2439 fd = COMM_GetWriteFd(hFile);
2440 if(fd<0)
2441 return FALSE;
2442 result = ioctl(fd, TIOCMGET, &mstat);
2443 close(fd);
2444 if (result == -1)
2446 TRACE("ioctl failed\n");
2447 return FALSE;
2449 if (mstat & TIOCM_CTS)
2450 *lpModemStat |= MS_CTS_ON;
2451 if (mstat & TIOCM_DSR)
2452 *lpModemStat |= MS_DSR_ON;
2453 if (mstat & TIOCM_RNG)
2454 *lpModemStat |= MS_RING_ON;
2455 /*FIXME: Not really sure about RLSD UB 990810*/
2456 if (mstat & TIOCM_CAR)
2457 *lpModemStat |= MS_RLSD_ON;
2458 TRACE("%s%s%s%s\n",
2459 (*lpModemStat &MS_RLSD_ON)?"MS_RLSD_ON ":"",
2460 (*lpModemStat &MS_RING_ON)?"MS_RING_ON ":"",
2461 (*lpModemStat &MS_DSR_ON)?"MS_DSR_ON ":"",
2462 (*lpModemStat &MS_CTS_ON)?"MS_CTS_ON ":"");
2463 return TRUE;
2464 #else
2465 return FALSE;
2466 #endif
2468 /***********************************************************************
2469 * WaitCommEvent (KERNEL32.719)
2471 BOOL WINAPI WaitCommEvent(HANDLE hFile,LPDWORD eventmask ,LPOVERLAPPED overlapped)
2473 FIXME("(%d %p %p )\n",hFile, eventmask,overlapped);
2474 return TRUE;
2477 /***********************************************************************
2478 * GetCommProperties (KERNEL32.???)
2480 BOOL WINAPI GetCommProperties(HANDLE hFile, LPDCB *dcb)
2482 FIXME("(%d %p )\n",hFile,dcb);
2483 return TRUE;
2486 /***********************************************************************
2487 * SetCommProperties (KERNEL32.???)
2489 BOOL WINAPI SetCommProperties(HANDLE hFile, LPDCB dcb)
2491 FIXME("(%d %p )\n",hFile,dcb);
2492 return TRUE;