forked from otya128/winevdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomm.c
966 lines (837 loc) · 23.6 KB
/
comm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*
* DEC 93 Erik Bos <[email protected]>
*
* Copyright 1996 Marcus Meissner
*
* Copyright 2001 Mike McCormack
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* History:
*
* Mar 31, 1999. Ove Kåven <[email protected]>
* - Implemented buffers and EnableCommNotification.
*
* Apr 3, 1999. Lawson Whitney <[email protected]>
* - Fixed the modem control part of EscapeCommFunction16.
*
* Mar 3, 1999. Ove Kåven <[email protected]>
* - Use port indices instead of unixfds for win16
* - Moved things around (separated win16 and win32 routines)
* - Added some hints on how to implement buffers and EnableCommNotification.
*
* May 26, 1997. Fixes and comments by Rick Richardson <[email protected]> [RER]
* - ptr->fd wasn't getting cleared on close.
* - GetCommEventMask() and GetCommError() didn't do much of anything.
* IMHO, they are still wrong, but they at least implement the RXCHAR
* event and return I/O queue sizes, which makes the app I'm interested
* in (analog devices EZKIT DSP development system) work.
*
* August 12, 1997. Take a bash at SetCommEventMask - Lawson Whitney
* July 6, 1998. Fixes and comments by Valentijn Sessink
* <[email protected]> [V]
* Oktober 98, Rein Klazes [RHK]
* A program that wants to monitor the modem status line (RLSD/DCD) may
* poll the modem status register in the commMask structure. I update the bit
* in GetCommError, waiting for an implementation of communication events.
*
*/
#include "config.h"
#include "wine/port.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "windef.h"
#include "winbase.h"
#include "wine/winuser16.h"
#include "user_private.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(comm);
/* window's semi documented modem status register */
#define COMM_MSR_OFFSET 35
#define MSR_CTS 0x10
#define MSR_DSR 0x20
#define MSR_RI 0x40
#define MSR_RLSD 0x80
#define MSR_MASK (MSR_CTS|MSR_DSR|MSR_RI|MSR_RLSD)
#define FLAG_LPT 0x80
#define MAX_PORTS 9
struct DosDeviceStruct {
HANDLE handle;
BOOL unget;
int ungetchar;
int evtchar;
/* events */
int commerror, eventmask;
/* buffers */
char *outbuf;
unsigned obuf_size,obuf_head,obuf_tail;
/* notifications */
HWND wnd;
int n_read, n_write;
/* save terminal states */
DCB16 dcb;
/* pointer to unknown(==undocumented) comm structure */
SEGPTR seg_unknown;
BYTE unknown[40];
HANDLE eventth;
HANDLE writeth;
HANDLE writeev;
CRITICAL_SECTION writelock;
BOOL exit;
};
static struct DosDeviceStruct COM[MAX_PORTS];
static struct DosDeviceStruct LPT[MAX_PORTS];
static struct DosDeviceStruct *GetDeviceStruct(int index)
{
if ((index&0x7F)<=MAX_PORTS) {
if (!(index&FLAG_LPT)) {
if (COM[index].handle)
return &COM[index];
} else {
index &= 0x7f;
if (LPT[index].handle)
return &LPT[index];
}
}
return NULL;
}
static int WinError(void)
{
TRACE("errno = %d\n", errno);
switch (errno) {
default:
return CE_IOE;
}
}
static DWORD WINAPI eventth(LPVOID cid)
{
struct DosDeviceStruct *ptr = GetDeviceStruct(cid);
DWORD evtmask = EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_PERR | EV_RING | EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_TXEMPTY;
DWORD temperror, mstat = 0;
COMSTAT stat;
DWORD count = -1;
OVERLAPPED ov;
int ret = 0;
ZeroMemory(&ov, sizeof(ov));
ov.hEvent = CreateEventW(NULL, 0, 0, NULL);
if(ov.hEvent == INVALID_HANDLE_VALUE)
return -1;
ClearCommError(ptr->handle, &temperror, &stat);
SetCommMask(ptr->handle, evtmask);
while (ptr->handle)
{
DWORD ret;
if (!WaitCommEvent(ptr->handle, &evtmask, &ov))
{
DWORD ret = 0, error = GetLastError();
if(error == ERROR_IO_PENDING)
{
if (!(ret = GetOverlappedResult(ptr->handle, &ov, &count, TRUE)))
error = GetLastError();
}
if (!ret && (error == ERROR_OPERATION_ABORTED))
break;
}
if (ptr->exit)
break;
if(GetCommModemStatus(ptr->handle, &mstat))
*((WORD *)(ptr->unknown + COMM_MSR_OFFSET)) = mstat;
*(WORD*)(ptr->unknown) |= evtmask;
if ((evtmask & ptr->eventmask) && ptr->wnd)
PostMessageA(ptr->wnd, WM_COMMNOTIFY, cid, CN_EVENT);
if (!ClearCommError(ptr->handle, &temperror, &stat))
{
ret = -1;
break;
}
if ((ptr->n_read != -1) && (ptr->n_read < stat.cbInQue) && ptr->wnd)
PostMessageA(ptr->wnd, WM_COMMNOTIFY, cid, CN_RECEIVE);
if ((ptr->n_write != -1) && (ptr->n_write > stat.cbOutQue) && ptr->wnd)
PostMessageA(ptr->wnd, WM_COMMNOTIFY, cid, CN_TRANSMIT);
}
CloseHandle(ov.hEvent);
return ret;
}
static DWORD WINAPI writeth(LPVOID cid)
{
struct DosDeviceStruct *ptr = GetDeviceStruct(cid);
OVERLAPPED write_ov = {0};
write_ov.hEvent = CreateEventW(NULL, 0, TRUE, NULL);
HANDLE handles[] = {ptr->writeev, write_ov.hEvent};
while (ptr->handle)
{
DWORD ret;
ret = WaitForSingleObjectEx(ptr->writeev, INFINITE, TRUE);
if ((ret == WAIT_ABANDONED) || ptr->exit)
break;
if (ret == WAIT_OBJECT_0)
{
EnterCriticalSection(&ptr->writelock);
int len, bleft = ((ptr->obuf_tail <= ptr->obuf_head) ? ptr->obuf_head : ptr->obuf_size) - ptr->obuf_tail;
while (bleft)
{
if (!WriteFile(ptr->handle, ptr->outbuf + ptr->obuf_tail, bleft, &len, &write_ov))
{
DWORD ret = 0, error = GetLastError();
if(error == ERROR_IO_PENDING)
{
LeaveCriticalSection(&ptr->writelock);
while ((ret = WaitForMultipleObjects(2, handles, FALSE, INFINITE)) == WAIT_OBJECT_0)
{
if (ptr->exit)
{
CancelIo(ptr->handle);
CloseHandle(write_ov.hEvent);
return 0;
}
}
EnterCriticalSection(&ptr->writelock);
if (ret == (WAIT_OBJECT_0 + 1))
{
if (!(ret = GetOverlappedResult(ptr->handle, &write_ov, &len, TRUE)))
error = GetLastError();
}
else if (ret == -1)
{
error = GetLastError();
ret = 0;
}
else
ret = error;
}
if (!ret)
{
ERR("async write failed, error %d\n", error);
ptr->commerror = CE_RXOVER;
CancelIo(ptr->handle);
break;
}
}
TRACE("async write completed %d bytes\n",len);
/* update the buffer pointers */
int prev = ((ptr->obuf_tail > ptr->obuf_head) ? ptr->obuf_size : 0) + ptr->obuf_head - ptr->obuf_tail;
ptr->obuf_tail += len;
if (ptr->obuf_tail >= ptr->obuf_size)
ptr->obuf_tail = 0;
/* write from output queue */
bleft = ((ptr->obuf_tail <= ptr->obuf_head) ? ptr->obuf_head : ptr->obuf_size) - ptr->obuf_tail;
}
LeaveCriticalSection(&ptr->writelock);
}
}
CloseHandle(write_ov.hEvent);
return 0;
}
/*****************************************************************************
* COMM16_DCBtoDCB16 (Internal)
*/
static INT16 COMM16_DCBtoDCB16(const DCB *lpdcb, LPDCB16 lpdcb16)
{
if(lpdcb->BaudRate<0x10000)
lpdcb16->BaudRate = lpdcb->BaudRate;
else if(lpdcb->BaudRate==115200)
lpdcb16->BaudRate = 57601;
else {
WARN("Baud rate can't be converted\n");
lpdcb16->BaudRate = 57601;
}
lpdcb16->ByteSize = lpdcb->ByteSize;
lpdcb16->fParity = lpdcb->fParity;
lpdcb16->Parity = lpdcb->Parity;
lpdcb16->StopBits = lpdcb->StopBits;
lpdcb16->RlsTimeout = 50;
lpdcb16->CtsTimeout = 50;
lpdcb16->DsrTimeout = 50;
lpdcb16->fNull = 0;
lpdcb16->fChEvt = 0;
lpdcb16->fBinary = 1;
lpdcb16->fDtrflow = (lpdcb->fDtrControl==DTR_CONTROL_HANDSHAKE);
lpdcb16->fRtsflow = (lpdcb->fRtsControl==RTS_CONTROL_HANDSHAKE);
lpdcb16->fOutxCtsFlow = lpdcb->fOutxCtsFlow;
lpdcb16->fOutxDsrFlow = lpdcb->fOutxDsrFlow;
lpdcb16->fDtrDisable = (lpdcb->fDtrControl==DTR_CONTROL_DISABLE);
lpdcb16->fRtsDisable = (lpdcb->fDtrControl==RTS_CONTROL_DISABLE);
lpdcb16->fInX = lpdcb->fInX;
lpdcb16->fOutX = lpdcb->fOutX;
/*
lpdcb16->XonChar =
lpdcb16->XoffChar =
*/
lpdcb16->XonLim = 10;
lpdcb16->XoffLim = 10;
return 0;
}
/**************************************************************************
* BuildCommDCB (USER.213)
*
* According to the ECMA-234 (368.3) the function will return FALSE on
* success, otherwise it will return -1.
*/
INT16 WINAPI BuildCommDCB16(LPCSTR device, LPDCB16 lpdcb)
{
/* "COM1:96,n,8,1" */
/* 012345 */
int port;
DCB dcb;
TRACE("(%s), ptr %p\n", device, lpdcb);
if (strncasecmp(device,"COM",3))
return -1;
port = device[3] - '0';
if (port-- == 0) {
ERR("BUG ! COM0 can't exist!\n");
return -1;
}
device += 4;
do
{
if (!*device)
return -1;
device++;
}
while (!isalnum(*device));
lpdcb->Id = port;
dcb.DCBlength = sizeof(DCB);
if (strchr(device,'=')) /* block new style */
return -1;
if(!BuildCommDCBA(device,&dcb))
return -1;
return COMM16_DCBtoDCB16(&dcb, lpdcb);
}
/*****************************************************************************
* OpenComm (USER.200)
*/
INT16 WINAPI OpenComm16(LPCSTR device,UINT16 cbInQueue,UINT16 cbOutQueue)
{
int port;
HANDLE handle;
TRACE("%s, %d, %d\n", device, cbInQueue, cbOutQueue);
if (strlen(device) < 4)
return IE_BADID;
port = device[3] - '0';
if (port-- == 0)
ERR("BUG ! COM0 or LPT0 don't exist !\n");
if (!strncasecmp(device,"COM",3))
{
if (COM[port].handle)
return IE_OPEN;
handle = CreateFileA(device, GENERIC_READ|GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 );
if (handle == INVALID_HANDLE_VALUE) {
ERR("Open %s failed Err %d\n", device, GetLastError());
return IE_HARDWARE;
} else {
memset(COM[port].unknown, 0, sizeof(COM[port].unknown));
COM[port].seg_unknown = 0;
COM[port].handle = handle;
COM[port].commerror = 0;
COM[port].eventmask = 0;
COM[port].evtchar = 0; /* FIXME: default? */
/* save terminal state */
GetCommState16(port,&COM[port].dcb);
/* init priority characters */
COM[port].unget = FALSE;
/* allocate buffers */
COM[port].obuf_size = cbOutQueue;
COM[port].obuf_head = COM[port].obuf_tail = 0;
COM[port].outbuf = HeapAlloc( GetProcessHeap(), 0, cbOutQueue);
if(!COM[port].outbuf || !SetupComm(handle, cbInQueue, cbOutQueue))
{
CloseHandle(COM[port].handle);
return IE_MEMORY;
}
COMMTIMEOUTS cto = { MAXDWORD, 0, 0, 0, 1000 };
SetCommTimeouts(handle, &cto);
COM[port].exit = FALSE;
COM[port].writeev = CreateEventW(NULL, 0, 0, NULL);
InitializeCriticalSection(&COM[port].writelock);
COM[port].eventth = CreateThread(NULL, 0, eventth, (LPVOID)port, 0, NULL);
COM[port].writeth = CreateThread(NULL, 0, writeth, (LPVOID)port, 0, NULL);
return port;
}
}
else
if (!strncasecmp(device,"LPT",3)) {
if (LPT[port].handle)
return IE_OPEN;
handle = CreateFileA(device, GENERIC_READ|GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, 0 );
if (handle == INVALID_HANDLE_VALUE) {
return IE_HARDWARE;
} else {
LPT[port].handle = handle;
LPT[port].commerror = 0;
LPT[port].eventmask = 0;
return port|FLAG_LPT;
}
}
return IE_BADID;
}
/*****************************************************************************
* CloseComm (USER.207)
*/
INT16 WINAPI CloseComm16(INT16 cid)
{
struct DosDeviceStruct *ptr;
TRACE("cid=%d\n", cid);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no cid=%d found!\n", cid);
return -1;
}
if (!(cid&FLAG_LPT)) {
DWORD count;
/* COM port */
UnMapLS( COM[cid].seg_unknown );
/* reset modem lines */
SetCommState16(&COM[cid].dcb);
HeapFree(GetProcessHeap(), 0, ptr->outbuf);
ptr->exit = TRUE;
ptr->obuf_tail = ptr->obuf_head;
SetCommMask(ptr->handle, 0);
PurgeComm(ptr->handle, PURGE_TXABORT);
SetEvent(ptr->writeev);
WaitForSingleObject(ptr->eventth, 1000);
WaitForSingleObject(ptr->writeth, 1000);
CloseHandle(ptr->writeev);
DeleteCriticalSection(&ptr->writelock);
}
if (!CloseHandle(ptr->handle)) {
ptr->commerror = WinError();
/* FIXME: should we clear ptr->handle here? */
return -1;
} else {
ptr->commerror = 0;
ptr->handle = 0;
return 0;
}
}
/*****************************************************************************
* SetCommBreak (USER.210)
*/
INT16 WINAPI SetCommBreak16(INT16 cid)
{
struct DosDeviceStruct *ptr;
TRACE("cid=%d\n", cid);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no cid=%d found!\n", cid);
return -1;
}
return !SetCommBreak(ptr->handle);
}
/*****************************************************************************
* ClearCommBreak (USER.211)
*/
INT16 WINAPI ClearCommBreak16(INT16 cid)
{
struct DosDeviceStruct *ptr;
TRACE("cid=%d\n", cid);
if (!(ptr = GetDeviceStruct(cid))) {
FIXME("no cid=%d found!\n", cid);
return -1;
}
return !ClearCommBreak(ptr->handle);
}
/*****************************************************************************
* EscapeCommFunction (USER.214)
*/
LONG WINAPI EscapeCommFunction16(UINT16 cid,UINT16 nFunction)
{
struct DosDeviceStruct *ptr;
TRACE("cid=%d, function=%d\n", cid, nFunction);
switch(nFunction) {
case GETMAXCOM:
TRACE("GETMAXCOM\n");
return 4; /* FIXME */
case GETMAXLPT:
TRACE("GETMAXLPT\n");
return FLAG_LPT + 3; /* FIXME */
case GETBASEIRQ:
TRACE("GETBASEIRQ\n");
/* FIXME: use tables */
/* just fake something for now */
if (cid & FLAG_LPT) {
/* LPT1: irq 7, LPT2: irq 5 */
return (cid & 0x7f) ? 5 : 7;
} else {
/* COM1: irq 4, COM2: irq 3,
COM3: irq 4, COM4: irq 3 */
return 4 - (cid & 1);
}
}
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no cid=%d found!\n", cid);
return -1;
}
switch (nFunction) {
case RESETDEV:
case CLRDTR:
case CLRRTS:
case SETDTR:
case SETRTS:
case SETXOFF:
case SETXON:
if(EscapeCommFunction(ptr->handle,nFunction))
return 0;
else {
ptr->commerror = WinError();
return -1;
}
case CLRBREAK:
case SETBREAK:
default:
WARN("(cid=%d,nFunction=%d): Unknown function\n",
cid, nFunction);
}
return -1;
}
/*****************************************************************************
* FlushComm (USER.215)
*/
INT16 WINAPI FlushComm16(INT16 cid,INT16 fnQueue)
{
DWORD queue;
struct DosDeviceStruct *ptr;
TRACE("cid=%d, queue=%d\n", cid, fnQueue);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no cid=%d found!\n", cid);
return -1;
}
switch (fnQueue) {
case 0:
queue = PURGE_TXABORT;
ptr->obuf_tail = ptr->obuf_head;
break;
case 1:
queue = PURGE_RXABORT;
break;
default:
WARN("(cid=%d,fnQueue=%d):Unknown queue\n",
cid, fnQueue);
return -1;
}
if (!PurgeComm(ptr->handle,queue)) {
ptr->commerror = WinError();
return -1;
} else {
ptr->commerror = 0;
return 0;
}
}
/********************************************************************
* GetCommError (USER.203)
*/
INT16 WINAPI GetCommError16(INT16 cid,LPCOMSTAT16 lpStat)
{
DWORD temperror;
struct DosDeviceStruct *ptr;
if ((UINT16)cid >= 256)
return 0;
if ((ptr = GetDeviceStruct(cid)) == NULL) {
/* Some programs frequently call GetCommError(0, ) */
/* FIXME("no handle for cid = %0x!\n",cid); */
return CE_MODE;
}
if (cid&FLAG_LPT) {
WARN(" cid %d not comm port\n",cid);
return CE_MODE;
}
if (lpStat) {
COMSTAT stat;
if (!ClearCommError(ptr->handle, &temperror, &stat))
return CE_MODE;
lpStat->status = *((BYTE *)&stat);
lpStat->cbInQue = stat.cbInQue + ptr->unget;
lpStat->cbOutQue = stat.cbOutQue;
TRACE("cid %d, error %d, stat %d in %d out %d\n",
cid, ptr->commerror, lpStat->status, lpStat->cbInQue,
lpStat->cbOutQue);
}
else {
if (!ClearCommError(ptr->handle, &temperror, NULL))
return CE_MODE;
TRACE("cid %d, error %d, lpStat NULL\n",
cid, ptr->commerror);
}
temperror |= ptr->commerror;
ptr->commerror = 0;
return(temperror);
}
/*****************************************************************************
* SetCommEventMask (USER.208)
*/
SEGPTR WINAPI SetCommEventMask16(INT16 cid,UINT16 fuEvtMask)
{
struct DosDeviceStruct *ptr;
TRACE("cid %d,mask %d\n",cid,fuEvtMask);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return 0;
}
ptr->eventmask = fuEvtMask;
if (cid&FLAG_LPT) {
WARN(" cid %d not comm port\n",cid);
return 0;
}
if (!COM[cid].seg_unknown) COM[cid].seg_unknown = MapLS( COM[cid].unknown );
return COM[cid].seg_unknown;
}
/*****************************************************************************
* GetCommEventMask (USER.209)
*/
UINT16 WINAPI GetCommEventMask16(INT16 cid,UINT16 fnEvtClear)
{
struct DosDeviceStruct *ptr;
WORD events;
TRACE("cid %d, mask %d\n", cid, fnEvtClear);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return 0;
}
if (cid&FLAG_LPT) {
WARN(" cid %d not comm port\n",cid);
return 0;
}
events = *(WORD*)(COM[cid].unknown) & COM[cid].eventmask;
*(WORD*)(COM[cid].unknown) &= ~fnEvtClear;
return events;
}
/*****************************************************************************
* SetCommState (USER.201)
*/
INT16 WINAPI SetCommState16(LPDCB16 lpdcb)
{
struct DosDeviceStruct *ptr;
DCB dcb;
TRACE("cid %d, ptr %p\n", lpdcb->Id, lpdcb);
if ((ptr = GetDeviceStruct(lpdcb->Id)) == NULL) {
FIXME("no handle for cid = %0x!\n",lpdcb->Id);
return -1;
}
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
/*
* according to MSDN, we should first interpret lpdcb->BaudRate as follows:
* 1. if the baud rate is a CBR constant, interpret it.
* 2. if it is greater than 57600, the baud rate is 115200
* 3. use the actual baudrate
* steps 2 and 3 are equivalent to 16550 baudrate divisor = 115200/BaudRate
*/
switch(lpdcb->BaudRate)
{
case CBR_110: dcb.BaudRate = 110; break;
case CBR_300: dcb.BaudRate = 300; break;
case CBR_600: dcb.BaudRate = 600; break;
case CBR_1200: dcb.BaudRate = 1200; break;
case CBR_2400: dcb.BaudRate = 2400; break;
case CBR_4800: dcb.BaudRate = 4800; break;
case CBR_9600: dcb.BaudRate = 9600; break;
case CBR_14400: dcb.BaudRate = 14400; break;
case CBR_19200: dcb.BaudRate = 19200; break;
case CBR_38400: dcb.BaudRate = 38400; break;
case CBR_56000: dcb.BaudRate = 56000; break;
case CBR_128000: dcb.BaudRate = 128000; break;
case CBR_256000: dcb.BaudRate = 256000; break;
default:
if(lpdcb->BaudRate>57600)
dcb.BaudRate = 115200;
else
dcb.BaudRate = lpdcb->BaudRate;
}
dcb.ByteSize=lpdcb->ByteSize;
dcb.StopBits=lpdcb->StopBits;
dcb.fBinary = 1;
dcb.fParity=lpdcb->fParity;
dcb.Parity=lpdcb->Parity;
dcb.fOutxCtsFlow = lpdcb->fOutxCtsFlow;
dcb.fOutxDsrFlow = lpdcb->fOutxDsrFlow;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
if (lpdcb->fDtrflow)
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
if (lpdcb->fDtrDisable)
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
if (lpdcb->fRtsflow)
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
if (lpdcb->fRtsDisable)
dcb.fRtsControl = RTS_CONTROL_DISABLE;
ptr->evtchar = lpdcb->EvtChar;
dcb.fInX = lpdcb->fInX;
dcb.fOutX = lpdcb->fOutX;
if (!SetCommState(ptr->handle,&dcb)) {
ptr->commerror = WinError();
return -1;
} else {
ptr->commerror = 0;
return 0;
}
}
/*****************************************************************************
* GetCommState (USER.202)
*/
INT16 WINAPI GetCommState16(INT16 cid, LPDCB16 lpdcb)
{
struct DosDeviceStruct *ptr;
DCB dcb;
TRACE("cid %d, ptr %p\n", cid, lpdcb);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return -1;
}
if (!GetCommState(ptr->handle,&dcb)) {
ptr->commerror = WinError();
return -1;
}
lpdcb->Id = cid;
COMM16_DCBtoDCB16(&dcb,lpdcb);
lpdcb->EvtChar = ptr->evtchar;
return 0;
}
/*****************************************************************************
* TransmitCommChar (USER.206)
*/
INT16 WINAPI TransmitCommChar16(INT16 cid,CHAR chTransmit)
{
struct DosDeviceStruct *ptr;
TRACE("cid %d, data %d\n", cid, chTransmit);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return -1;
}
return TransmitCommChar(ptr->handle, chTransmit) ? 0 : -1;
}
/*****************************************************************************
* UngetCommChar (USER.212)
*/
INT16 WINAPI UngetCommChar16(INT16 cid,CHAR chUnget)
{
struct DosDeviceStruct *ptr;
TRACE("cid %d (char %d)\n", cid, chUnget);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return -1;
}
if (ptr->unget) {
/* character already queued */
return -1;
}
ptr->ungetchar = chUnget;
ptr->unget = TRUE;
ptr->commerror = 0;
return 0;
}
/*****************************************************************************
* ReadComm (USER.204)
*/
INT16 WINAPI ReadComm16(INT16 cid,LPSTR lpvBuf,INT16 cbRead)
{
int length;
struct DosDeviceStruct *ptr;
LPSTR orgBuf = lpvBuf;
OVERLAPPED ov;
DWORD count = -1;
ZeroMemory(&ov, sizeof(ov));
ov.hEvent = CreateEventW(NULL, 0, 0, NULL);
if(ov.hEvent == INVALID_HANDLE_VALUE)
return -1;
TRACE("cid %d, ptr %p, length %d\n", cid, lpvBuf, cbRead);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return -1;
}
if (ptr->unget)
{
*orgBuf = ptr->ungetchar;
orgBuf++;
cbRead--;
}
if (!ReadFile(ptr->handle, orgBuf, cbRead, &length, &ov))
{
if (GetLastError() == ERROR_IO_PENDING)
GetOverlappedResult(ptr->handle, &ov, &length, TRUE);
}
CloseHandle(ov.hEvent);
if (ptr->unget)
{
ptr->unget = FALSE;
length++;
}
if (length)
TRACE("%d bytes read\n", length);
return length;
}
/*****************************************************************************
* WriteComm (USER.205)
*/
INT16 WINAPI WriteComm16(INT16 cid, LPSTR lpvBuf, INT16 cbWrite)
{
int length;
struct DosDeviceStruct *ptr;
TRACE("cid %d, ptr %p, length %d\n",
cid, lpvBuf, cbWrite);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return -1;
}
if (cid&FLAG_LPT)
WriteFile(ptr->handle, lpvBuf, cbWrite, &length, NULL);
else
{
int count;
length = 0;
EnterCriticalSection(&ptr->writelock);
while (length < cbWrite) {
count = ((ptr->obuf_tail > ptr->obuf_head) ?
(ptr->obuf_tail-1) : ptr->obuf_size) - ptr->obuf_head;
if (!count) break;
if ((cbWrite - length) < count)
count = cbWrite - length;
memcpy(ptr->outbuf + ptr->obuf_head, lpvBuf, count);
ptr->obuf_head += count;
if (ptr->obuf_head >= ptr->obuf_size)
ptr->obuf_head = 0;
lpvBuf += count;
length += count;
}
LeaveCriticalSection(&ptr->writelock);
SetEvent(ptr->writeev);
}
if (length < cbWrite)
{
ptr->commerror = CE_TXFULL;
return -length;
}
return length;
}
/***********************************************************************
* EnableCommNotification (USER.245)
*/
BOOL16 WINAPI EnableCommNotification16( INT16 cid, HWND16 hwnd,
INT16 cbWriteNotify, INT16 cbOutQueue )
{
struct DosDeviceStruct *ptr;
TRACE("(%d, %x, %d, %d)\n", cid, hwnd, cbWriteNotify, cbOutQueue);
if ((ptr = GetDeviceStruct(cid)) == NULL) {
FIXME("no handle for cid = %0x!\n",cid);
return -1;
}
ptr->wnd = WIN_Handle32( hwnd );
ptr->n_read = cbWriteNotify;
ptr->n_write = cbOutQueue;
return TRUE;
}