comm.c 69.5 KB
Newer Older
1 2 3
/* Unit test suite for comm functions
 *
 * Copyright 2003 Kevin Groeneveld
4
 * Copyright 2005 Uwe Bonnes
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26
 */

#include <stdio.h>

#include "wine/test.h"
#include "winbase.h"
#include "winnls.h"

27 28 29
#define TIMEOUT 1000   /* one second for Timeouts*/
#define SLOWBAUD 150
#define FASTBAUD 115200
30
#define TIMEDELTA 150  /* 150 ms uncertainty allowed */
31 32 33 34 35 36 37 38 39

/* Define the appropriate LOOPBACK(s) TRUE if you have a Loopback cable with
 * the mentioned shorts connected to your Serial port
 */
#define LOOPBACK_TXD_RXD  FALSE /* Sub-D 9: Short 2-3 */
#define LOOPBACK_CTS_RTS  FALSE /* Sub-D 9: Short 7-8 */
#define LOOPBACK_DTR_DSR  FALSE /* Sub-D 9: Short 4-6 */
#define LOOPBACK_DTR_RING FALSE /* Sub-D 9: Short 4-9 */
#define LOOPBACK_DTR_DCD  FALSE /* Sub-D 9: Short 4-1 */
Uwe Bonnes's avatar
Uwe Bonnes committed
40 41 42 43
/* Many Linux serial drivers have the TIOCM_LOOP flag in the TIOCM_SET ioctl
 * available. For the 8250 this is equivalent to TXD->RXD, OUT2->DCD,
 * OUT1->RI, RTS->CTS and DTR->DSR
 */
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58
typedef struct
{
	char string[100];
	BOOL result;
	BOOL old_style;
	DCB dcb1, dcb2;
	COMMTIMEOUTS timeouts1, timeouts2;
} TEST;

static TEST test[] =
{
	{
		"baud=9600 parity=e data=5 stop=1 xon=on odsr=off octs=off dtr=on rts=on idsr=on",
		TRUE, FALSE,
59 60
		{ 0x00000000, 0x00002580, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00002580, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x05, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
61 62 63 64 65 66
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"baud=0 parity=M data=6 stop=1.5 xon=off odsr=on octs=ON dtr=off rts=off idsr=OFF",
		TRUE, FALSE,
67 68
		{ 0x00000000, 0x00000000, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x06, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00000000, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x06, 0x03, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
69 70 71 72 73 74
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"BAUD=4000000000 parity=n data=7 stop=2 to=off",
		TRUE, FALSE,
75 76
		{ 0x00000000, 0xee6b2800, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xee6b2800, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x00, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
77 78 79 80 81 82
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }
	},
	{
		"Baud=115200 Parity=O Data=8 To=On",
		TRUE, FALSE,
83 84
		{ 0x00000000, 0x0001c200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x0001c200, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
85 86 87 88 89 90
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 },
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 }
	},
	{
		"PaRiTy=s           Data=7          DTR=on",
		TRUE, FALSE,
91 92
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
93 94 95 96 97 98
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"data=4",
		FALSE, FALSE,
99 100
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
101 102 103 104 105 106
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"data=9",
		FALSE, FALSE,
107 108
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
109 110 111 112 113 114
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"parity=no",
		FALSE, FALSE,
115 116
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
117 118 119 120 121 122
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"stop=0",
		FALSE, FALSE,
123 124
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
125 126 127 128 129 130
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"stop=1.501",
		FALSE, FALSE,
131 132
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
133 134 135 136 137 138
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"stop=3",
		FALSE, FALSE,
139 140
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
141 142 143 144 145 146
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"to=foobar",
		FALSE, FALSE,
147 148
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
149 150 151 152 153 154
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		" baud=9600",
		FALSE, FALSE,
155 156
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
157 158 159 160 161 162
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"baud= 9600",
		FALSE, FALSE,
163 164
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
165 166 167 168 169 170
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"baud=9600,data=8",
		FALSE, FALSE,
171 172
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
173 174 175 176 177 178
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"11,n,8,1",
		TRUE, TRUE,
179 180
		{ 0x00000000, 0x0000006e, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x0000006e, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
181 182 183 184 185 186
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"30 ,E, 5,1.5",
		TRUE, TRUE,
187 188
		{ 0x00000000, 0x0000012c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x05, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x0000012c, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x05, 0x02, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
189 190 191 192 193 194
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"60, m, 6, 2 ",
		TRUE, TRUE,
195 196
		{ 0x00000000, 0x00000258, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x06, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00000258, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x06, 0x03, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
197 198 199 200 201 202
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"12 , o , 7 , 1",
		TRUE, TRUE,
203 204
		{ 0x00000000, 0x000004b0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x000004b0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
205 206 207 208 209 210
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"24,s,8,1.5",
		TRUE, TRUE,
211 212
		{ 0x00000000, 0x00000960, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00000960, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x04, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
213 214 215 216 217 218
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"48,n,8,1,p",
		TRUE, TRUE,
219 220
		{ 0x00000000, 0x000012c0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x000012c0, 1, 1, 1, 1, 2, 1, 1, 0, 0, 1, 1, 2, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
221 222 223 224 225 226
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,8,1 , x ",
		TRUE, TRUE,
227 228
		{ 0x00000000, 0x00002580, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00002580, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
229 230 231 232 233 234
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"19, e, 7, 1, x",
		TRUE, TRUE,
235 236
		{ 0x00000000, 0x00004b00, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00004b00, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x02, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
237 238 239 240 241 242
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"0,M,7,1,P",
		TRUE, TRUE,
243 244
		{ 0x00000000, 0x00000000, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00000000, 1, 1, 1, 1, 2, 1, 1, 0, 0, 1, 1, 2, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x03, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
245 246 247 248 249 250
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"4000000000,O,7,1.5,X",
		TRUE, TRUE,
251 252
		{ 0x00000000, 0xee6b2800, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xee6b2800, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
253 254 255 256 257 258
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,8,1 to=on",
		FALSE, TRUE,
259 260
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
261 262 263 264 265 266
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,NO,8,1",
		FALSE, TRUE,
267 268
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
269 270 271 272 273 274
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,4,1",
		FALSE, TRUE,
275 276
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
277 278 279 280 281 282
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,9,1",
		FALSE, TRUE,
283 284
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
285 286 287 288 289 290
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,8,0",
		FALSE, TRUE,
291 292
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
293 294 295 296 297 298
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,8,3",
		FALSE, TRUE,
299 300
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
301 302 303 304 305 306
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"96,N,8,1,K",
		FALSE, TRUE,
307 308
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
309 310 311 312 313 314
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COM0:baud=115200",
		FALSE, FALSE,
315 316
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
317 318 319 320 321 322
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx:baud=38400 data=8",
		TRUE, FALSE,
323 324
		{ 0x00000000, 0x00009600, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00009600, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
325 326 327 328 329 330
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx  :to=on stop=1.5",
		TRUE, FALSE,
331 332
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
333 334 335 336 337 338
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 },
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000EA60 }
	},
	{
		"COMx:               baud=12345     data=7",
		TRUE, FALSE,
339 340
		{ 0x00000000, 0x00003039, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00003039, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
341 342 343 344 345 346
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx : xon=on odsr=off",
		TRUE, FALSE,
347 348
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 0, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
349 350 351 352 353 354
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COM0:9600,N,8,1",
		FALSE, TRUE,
355 356
		{ 0x00000000, 0x00000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0xffffffff, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
357 358 359 360 361 362
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx:9600,N,8,1",
		TRUE, TRUE,
363 364
		{ 0x00000000, 0x00002580, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00002580, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x08, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
365 366 367 368 369 370
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx:  11,E,7,2",
		TRUE, TRUE,
371 372
		{ 0x00000000, 0x0000006e, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x07, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x0000006e, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x07, 0x02, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
373 374 375 376 377 378
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx  :19,M,5,1",
		TRUE, TRUE,
379 380
		{ 0x00000000, 0x00004b00, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x05, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x00004b00, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x05, 0x03, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
381 382 383 384 385 386
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
	{
		"COMx  :    57600,S,6,2,x",
		TRUE, TRUE,
387 388
		{ 0x00000000, 0x0000e100, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0x00000, 0x0000, 0x0000, 0x0000, 0x06, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0000 },
		{ 0xffffffff, 0x0000e100, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0x1ffff, 0xffff, 0xffff, 0xffff, 0x06, 0x04, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xffff },
389 390 391 392 393 394 395
		{ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
		{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }
	},
};

#define TEST_COUNT (sizeof(test) / sizeof(TEST))

396
/* This function can be useful if you are modifying the test cases and want to
397 398 399
   output the contents of a DCB structure. */
/*static print_dcb(DCB *pdcb)
{
400
	printf("0x%08x, 0x%08x, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, 0x%05x, 0x%04x, 0x%04x, 0x%04x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%04x\n",
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
		pdcb->DCBlength,
		pdcb->BaudRate,
		pdcb->fBinary,
		pdcb->fParity,
		pdcb->fOutxCtsFlow,
		pdcb->fOutxDsrFlow,
		pdcb->fDtrControl,
		pdcb->fDsrSensitivity,
		pdcb->fTXContinueOnXoff,
		pdcb->fOutX,
		pdcb->fInX,
		pdcb->fErrorChar,
		pdcb->fNull,
		pdcb->fRtsControl,
		pdcb->fAbortOnError,
		pdcb->fDummy2,
		pdcb->wReserved,
		pdcb->XonLim,
		pdcb->XoffLim,
		pdcb->ByteSize,
		pdcb->Parity,
		pdcb->StopBits,
		pdcb->XonChar & 0xff,
		pdcb->XoffChar & 0xff,
		pdcb->ErrorChar & 0xff,
		pdcb->EofChar & 0xff,
		pdcb->EvtChar & 0xff,
		pdcb->wReserved1 & 0xffff );
} */

431
static void check_result(const char *function, TEST *ptest, int initial_value, BOOL result)
432 433 434 435
{
	DWORD LastError = GetLastError();
	DWORD CorrectError = (ptest->result ? 0xdeadbeef : ERROR_INVALID_PARAMETER);

436
	ok(LastError == CorrectError, "%s(\"%s\"), 0x%02x: GetLastError() returned %d, should be %d\n", function, ptest->string, initial_value, LastError, CorrectError);
437 438 439 440 441 442
	ok(result == ptest->result, "%s(\"%s\"), 0x%02x: return value should be %s\n", function, ptest->string, initial_value, ptest->result ? "TRUE" : "FALSE");
}

#define check_dcb_member(a,b) ok(pdcb1->a == pdcb2->a, "%s(\"%s\"), 0x%02x: "#a" is "b", should be "b"\n", function, ptest->string, initial_value, pdcb1->a, pdcb2->a)
#define check_dcb_member2(a,c,b) if(pdcb2->a == c) { check_dcb_member(a,b); } else { ok(pdcb1->a == pdcb2->a || pdcb1->a == c, "%s(\"%s\"), 0x%02x: "#a" is "b", should be "b" or "b"\n", function, ptest->string, initial_value, pdcb1->a, pdcb2->a, c); }

443
static void check_dcb(const char *function, TEST *ptest, int initial_value, DCB *pdcb1, DCB *pdcb2)
444 445 446
{
	/* DCBlength is a special case since Win 9x sets it but NT does not.
	   We will accept either as correct. */
447
	check_dcb_member2(DCBlength, (DWORD)sizeof(DCB), "%u");
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

	/* For old style control strings Win 9x does not set the next five members, NT does. */
	if(ptest->old_style && ptest->result)
	{
		check_dcb_member2(fOutxCtsFlow, ((unsigned int)initial_value & 1), "%u");
		check_dcb_member2(fDtrControl, ((unsigned int)initial_value & 3), "%u");
		check_dcb_member2(fOutX, ((unsigned int)initial_value & 1), "%u");
		check_dcb_member2(fInX, ((unsigned)initial_value & 1), "%u");
		check_dcb_member2(fRtsControl, ((unsigned)initial_value & 3), "%u");
	}
	else
	{
		check_dcb_member(fOutxCtsFlow, "%u");
		check_dcb_member(fDtrControl, "%u");
		check_dcb_member(fOutX, "%u");
		check_dcb_member(fInX, "%u");
		check_dcb_member(fRtsControl, "%u");
	}

	if(ptest->result)
	{
Francois Gouget's avatar
Francois Gouget committed
469
		/* For the idsr=xxx parameter, NT sets fDsrSensitivity, 9x sets
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
		   fOutxDsrFlow. */
		if(!ptest->old_style)
		{
			check_dcb_member2(fOutxDsrFlow, pdcb2->fDsrSensitivity, "%u");
			check_dcb_member2(fDsrSensitivity, pdcb2->fOutxDsrFlow, "%u");
		}
		else
		{
			/* For old style control strings Win 9x does not set the
			   fOutxDsrFlow member, NT does. */
			check_dcb_member2(fOutxDsrFlow, ((unsigned int)initial_value & 1), "%u");
			check_dcb_member(fDsrSensitivity, "%u");
		}
	}
	else
	{
		check_dcb_member(fOutxDsrFlow, "%u");
		check_dcb_member(fDsrSensitivity, "%u");
	}

	/* Check the result of the DCB members. */
491
	check_dcb_member(BaudRate, "%u");
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	check_dcb_member(fBinary, "%u");
	check_dcb_member(fParity, "%u");
	check_dcb_member(fTXContinueOnXoff, "%u");
	check_dcb_member(fErrorChar, "%u");
	check_dcb_member(fNull, "%u");
	check_dcb_member(fAbortOnError, "%u");
	check_dcb_member(fDummy2, "%u");
	check_dcb_member(wReserved, "%u");
	check_dcb_member(XonLim, "%u");
	check_dcb_member(XoffLim, "%u");
	check_dcb_member(ByteSize, "%u");
	check_dcb_member(Parity, "%u");
	check_dcb_member(StopBits, "%u");
	check_dcb_member(XonChar, "%d");
	check_dcb_member(XoffChar, "%d");
	check_dcb_member(ErrorChar, "%d");
	check_dcb_member(EofChar, "%d");
	check_dcb_member(EvtChar, "%d");
	check_dcb_member(wReserved1, "%u");
}

513
#define check_timeouts_member(a) ok(ptimeouts1->a == ptimeouts2->a, "%s(\"%s\"), 0x%02x: "#a" is %u, should be %u\n", function, ptest->string, initial_value, ptimeouts1->a, ptimeouts2->a);
514

515
static void check_timeouts(const char *function, TEST *ptest, int initial_value, COMMTIMEOUTS *ptimeouts1, COMMTIMEOUTS *ptimeouts2)
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
{
	check_timeouts_member(ReadIntervalTimeout);
	check_timeouts_member(ReadTotalTimeoutMultiplier);
	check_timeouts_member(ReadTotalTimeoutConstant);
	check_timeouts_member(WriteTotalTimeoutMultiplier);
	check_timeouts_member(WriteTotalTimeoutConstant);
}

static void test_BuildCommDCBA(TEST *ptest, int initial_value, DCB *pexpected_dcb)
{
	BOOL result;
	DCB dcb;

	/* set initial conditions */
	memset(&dcb, initial_value, sizeof(DCB));
	SetLastError(0xdeadbeef);

	result = BuildCommDCBA(ptest->string, &dcb);

	/* check results */
	check_result("BuildCommDCBA", ptest, initial_value, result);
	check_dcb("BuildCommDCBA", ptest, initial_value, &dcb, pexpected_dcb);
}

static void test_BuildCommDCBAndTimeoutsA(TEST *ptest, int initial_value, DCB *pexpected_dcb, COMMTIMEOUTS *pexpected_timeouts)
{
	BOOL result;
	DCB dcb;
	COMMTIMEOUTS timeouts;

	/* set initial conditions */
	memset(&dcb, initial_value, sizeof(DCB));
	memset(&timeouts, initial_value, sizeof(COMMTIMEOUTS));
	SetLastError(0xdeadbeef);

	result = BuildCommDCBAndTimeoutsA(ptest->string, &dcb, &timeouts);

	/* check results */
	check_result("BuildCommDCBAndTimeoutsA", ptest, initial_value, result);
	check_dcb("BuildCommDCBAndTimeoutsA", ptest, initial_value, &dcb, pexpected_dcb);
	check_timeouts("BuildCommDCBAndTimeoutsA", ptest, initial_value, &timeouts, pexpected_timeouts);
}

static void test_BuildCommDCBW(TEST *ptest, int initial_value, DCB *pexpected_dcb)
{
	BOOL result;
	DCB dcb;
	WCHAR wide_string[sizeof(ptest->string)];
564
	static int reportedDCBW = 0;
565 566 567 568 569 570 571 572 573 574

	MultiByteToWideChar(CP_ACP, 0, ptest->string, -1, wide_string, sizeof(wide_string) / sizeof(WCHAR));

	/* set initial conditions */
	memset(&dcb, initial_value, sizeof(DCB));
	SetLastError(0xdeadbeef);

	result = BuildCommDCBW(wide_string, &dcb);

	if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
575
	{
576 577
		if(!reportedDCBW++)
			win_skip("BuildCommDCBW is not implemented\n");
578
		return;
579
	}
580 581 582 583 584 585 586 587 588 589 590 591

	/* check results */
	check_result("BuildCommDCBW", ptest, initial_value, result);
	check_dcb("BuildCommDCBW", ptest, initial_value, &dcb, pexpected_dcb);
}

static void test_BuildCommDCBAndTimeoutsW(TEST *ptest, int initial_value, DCB *pexpected_dcb, COMMTIMEOUTS *pexpected_timeouts)
{
	BOOL result;
	DCB dcb;
	COMMTIMEOUTS timeouts;
	WCHAR wide_string[sizeof(ptest->string)];
592
	static int reportedDCBAndTW = 0;
593 594 595 596 597 598 599 600 601 602 603

	MultiByteToWideChar(CP_ACP, 0, ptest->string, -1, wide_string, sizeof(wide_string) / sizeof(WCHAR));

	/* set initial conditions */
	memset(&dcb, initial_value, sizeof(DCB));
	memset(&timeouts, initial_value, sizeof(COMMTIMEOUTS));
	SetLastError(0xdeadbeef);

	result = BuildCommDCBAndTimeoutsW(wide_string, &dcb, &timeouts);

	if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
604
	{
605 606
		if(!reportedDCBAndTW++)
			win_skip("BuildCommDCBAndTimeoutsW is not implemented\n");
607
		return;
608
	}
609 610

	/* check results */
611 612 613
	check_result("BuildCommDCBAndTimeoutsW", ptest, initial_value, result);
	check_dcb("BuildCommDCBAndTimeoutsW", ptest, initial_value, &dcb, pexpected_dcb);
	check_timeouts("BuildCommDCBAndTimeoutsW", ptest, initial_value, &timeouts, pexpected_timeouts);
614 615 616 617 618 619
}

static void test_BuildCommDCB(void)
{
	char port_name[] = "COMx";
	char port = 0;
620
	unsigned int i;
621 622 623 624 625 626 627 628
	char *ptr;

	/* Some of these tests require a valid COM port.  This loop will try to find
	   a valid port. */
	for(port_name[3] = '1'; port_name[3] <= '9'; port_name[3]++)
	{
		COMMCONFIG commconfig;
		DWORD size = sizeof(COMMCONFIG);
629

630 631 632 633 634 635 636 637 638 639 640 641 642 643
		if(GetDefaultCommConfig(port_name, &commconfig, &size))
		{
			port = port_name[3];
			break;
		}
	}

	if(!port)
		trace("Could not find a valid COM port.  Some tests will be skipped.\n");

	for(i = 0; i < TEST_COUNT; i++)
	{
		/* Check if this test case needs a valid COM port. */
		ptr = strstr(test[i].string, "COMx");
644

645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		/* If required, substitute valid port number into device control string. */
		if(ptr)
		{
			if(port)
				ptr[3] = port;
			else
				continue;
		}

		test_BuildCommDCBA(&test[i], 0x00, &test[i].dcb1);
		test_BuildCommDCBA(&test[i], 0xff, &test[i].dcb2);
		test_BuildCommDCBAndTimeoutsA(&test[i], 0x00, &test[i].dcb1, &test[i].timeouts1);
		test_BuildCommDCBAndTimeoutsA(&test[i], 0xff, &test[i].dcb2, &test[i].timeouts2);

		test_BuildCommDCBW(&test[i], 0x00, &test[i].dcb1);
		test_BuildCommDCBW(&test[i], 0xff, &test[i].dcb2);
		test_BuildCommDCBAndTimeoutsW(&test[i], 0x00, &test[i].dcb1, &test[i].timeouts1);
		test_BuildCommDCBAndTimeoutsW(&test[i], 0xff, &test[i].dcb2, &test[i].timeouts2);
	}
}

666 667 668 669 670
static HANDLE test_OpenComm(BOOL doOverlap)
{
    HANDLE hcom = INVALID_HANDLE_VALUE;
    char port_name[] = "COMx";
    static BOOL shown = FALSE;
Uwe Bonnes's avatar
Uwe Bonnes committed
671 672
    DWORD errors;
    COMSTAT comstat;
673 674 675 676 677 678 679 680 681 682 683 684 685 686

    /* Try to find a port */
    for(port_name[3] = '1'; port_name[3] <= '9'; port_name[3]++)
    {
	hcom = CreateFile( port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
			   (doOverlap)?FILE_FLAG_OVERLAPPED:0, NULL );
	if (hcom != INVALID_HANDLE_VALUE)
	    break;
    }
    if(!shown)
    {
	if (hcom == INVALID_HANDLE_VALUE)
	    trace("Could not find a valid COM port.  Skipping test_ReadTimeOut\n");
	else
687
	    trace("Found Com port %s. Connected devices may disturb results\n", port_name);
688 689
	/*shown = TRUE; */
    }
Uwe Bonnes's avatar
Uwe Bonnes committed
690 691
    if (hcom != INVALID_HANDLE_VALUE)
    {
692 693 694
        BOOL ret;

        ret = ClearCommError(hcom, &errors, &comstat);
695
        if (!ret && (GetLastError() == ERROR_NOT_READY || GetLastError() == ERROR_INVALID_HANDLE))
696
        {
697 698 699 700
            if (GetLastError() == ERROR_NOT_READY)
                trace("%s doesn't respond, skipping the test\n", port_name);
            else
                trace("%s is not a real serial port, skipping the test\n", port_name);
701 702 703 704 705
            CloseHandle(hcom);
            return INVALID_HANDLE_VALUE;
        }

        ok(ret, "Unexpected error %u on open\n", GetLastError());
706 707 708
        ok(comstat.cbInQue == 0, "Unexpected %d chars in InQueue\n",comstat.cbInQue);
        ok(comstat.cbOutQue == 0, "Still pending %d charcters in OutQueue\n", comstat.cbOutQue);
        ok(errors == 0, "Unexpected errors 0x%08x\n", errors);
Uwe Bonnes's avatar
Uwe Bonnes committed
709
    }
710 711 712 713 714
    return hcom;
}

static void test_GetModemStatus(HANDLE hcom)
{
715
    DWORD ModemStat = 0;
716 717

    ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
718
    trace("GetCommModemStatus returned 0x%08x->%s%s%s%s\n", ModemStat,
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	  (ModemStat &MS_RLSD_ON)?"MS_RLSD_ON ":"",
	  (ModemStat &MS_RING_ON)?"MS_RING_ON ":"",
	  (ModemStat &MS_DSR_ON)?"MS_DSR_ON ":"",
	  (ModemStat &MS_CTS_ON)?"MS_CTS_ON ":"");
}

/* When we don't write anything, Read should time out even on a loopbacked port */
static void test_ReadTimeOut(HANDLE hcom)
{
    DCB dcb;
    COMMTIMEOUTS timeouts;
    char rbuf[32];
    DWORD before, after, read, timediff, LastError;
    BOOL res;

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    dcb.BaudRate = FASTBAUD;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
Uwe Bonnes's avatar
Uwe Bonnes committed
738 739
    dcb.fRtsControl=RTS_CONTROL_ENABLE;
    dcb.fDtrControl=DTR_CONTROL_ENABLE;
740 741 742 743 744 745 746 747
    dcb.StopBits = ONESTOPBIT;
    ok(SetCommState(hcom, &dcb), "SetCommState failed\n");

    ZeroMemory( &timeouts, sizeof(timeouts));
    timeouts.ReadTotalTimeoutConstant = TIMEOUT;
    ok(SetCommTimeouts(hcom, &timeouts),"SetCommTimeouts failed\n");

    before = GetTickCount();
748
    SetLastError(0xdeadbeef);
749 750 751
    res = ReadFile(hcom, rbuf, sizeof(rbuf), &read, NULL);
    LastError = GetLastError();
    after = GetTickCount();
Uwe Bonnes's avatar
Uwe Bonnes committed
752
    ok( res == TRUE, "A timed-out read should return TRUE\n");
753
    ok( LastError == 0xdeadbeef, "err=%d\n", LastError);
754 755
    timediff = after - before;
    ok( timediff > TIMEOUT>>2 && timediff < TIMEOUT *2,
756
	"Unexpected TimeOut %d, expected %d\n", timediff, TIMEOUT);
757 758 759 760 761 762
}

static void test_waittxempty(HANDLE hcom)
{
    DCB dcb;
    COMMTIMEOUTS timeouts;
Uwe Bonnes's avatar
Uwe Bonnes committed
763
    char tbuf[]="test_waittxempty";
764 765 766 767
    DWORD before, after, written, timediff, evtmask = 0;
    BOOL res_write, res;
    DWORD baud = SLOWBAUD;

768
    trace("test_waittxempty\n");
769 770 771 772 773
    /* set a low baud rate to have ample time*/
    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    dcb.BaudRate = baud;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
Uwe Bonnes's avatar
Uwe Bonnes committed
774 775
    dcb.fRtsControl=RTS_CONTROL_ENABLE;
    dcb.fDtrControl=DTR_CONTROL_ENABLE;
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    dcb.StopBits = ONESTOPBIT;
    ok(SetCommState(hcom, &dcb), "SetCommState failed\n");

    ZeroMemory( &timeouts, sizeof(timeouts));
    timeouts.ReadTotalTimeoutConstant = TIMEOUT;
    ok(SetCommTimeouts(hcom, &timeouts),"SetCommTimeouts failed\n");

    ok(SetupComm(hcom,1024,1024),"SetUpComm failed\n");
    ok(SetCommMask(hcom, EV_TXEMPTY), "SetCommMask failed\n");

    before = GetTickCount();
    res_write=WriteFile(hcom, tbuf, sizeof(tbuf), &written, NULL);
    after = GetTickCount();
    ok(res_write == TRUE, "WriteFile failed\n");
    ok(written == sizeof(tbuf),
791
       "WriteFile: Unexpected write_size %d\n", written);
792

793
    trace("WriteFile succeeded, took %d ms to write %d Bytes at %d Baud\n",
794
	  after - before, written, baud);
795 796 797 798 799

    before = GetTickCount();
    res = WaitCommEvent(hcom, &evtmask, NULL);
    after = GetTickCount();

Uwe Bonnes's avatar
Uwe Bonnes committed
800 801
    ok(res == TRUE, "WaitCommEvent failed\n");
    ok((evtmask & EV_TXEMPTY),
802
                 "WaitCommEvent: Unexpected EvtMask 0x%08x, expected 0x%08x\n",
803 804 805 806
		 evtmask, EV_TXEMPTY);

    timediff = after - before;

807
    trace("WaitCommEvent for EV_TXEMPTY took %d ms\n", timediff);
808 809
    /* 050604: This shows a difference between XP (tested with mingw compiled crosstest):
       XP returns Writefile only after everything went out of the Serial port,
810
       while wine returns immediately.
811 812 813 814 815 816 817 818 819 820 821 822 823
       Thus on XP, WaintCommEvent after setting the CommMask for EV_TXEMPTY
       nearly return immediate,
       while on wine the most time is spent here
    */
}

/* A new open handle should not return error or have bytes in the Queues */
static void test_ClearCommErrors(HANDLE hcom)
{
    DWORD   errors;
    COMSTAT lpStat;

    ok(ClearCommError(hcom, &errors, &lpStat), "ClearCommError failed\n");
824 825 826
    ok(lpStat.cbInQue == 0, "Unexpected %d chars in InQueue\n", lpStat.cbInQue);
    ok(lpStat.cbOutQue == 0, "Unexpected %d chars in OutQueue\n", lpStat.cbOutQue);
    ok(errors == 0, "ClearCommErrors: Unexpected error 0x%08x\n", errors);
Uwe Bonnes's avatar
Uwe Bonnes committed
827
    trace("test_ClearCommErrors done\n");
828 829
}

Eric Pouech's avatar
Eric Pouech committed
830 831 832 833 834 835 836 837 838
static void test_non_pending_errors(HANDLE hcom)
{
    DCB dcb;
    DWORD err;

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    dcb.ByteSize = 255; /* likely bogus */
    ok(!SetCommState(hcom, &dcb), "SetCommState should have failed\n");
    ok(ClearCommError(hcom, &err, NULL), "ClearCommError should succeed\n");
839
    ok(!(err & CE_MODE), "ClearCommError shouldn't set CE_MODE byte in this case (%x)\n", err);
Eric Pouech's avatar
Eric Pouech committed
840 841
}

842 843 844 845 846 847
/**/
static void test_LoopbackRead(HANDLE hcom)
{
    DCB dcb;
    COMMTIMEOUTS timeouts;
    char rbuf[32];
Uwe Bonnes's avatar
Uwe Bonnes committed
848
    DWORD before, after, diff, read, read1, written, evtmask=0, i;
849
    BOOL res;
Uwe Bonnes's avatar
Uwe Bonnes committed
850
    char tbuf[]="test_LoopbackRead";
851

Uwe Bonnes's avatar
Uwe Bonnes committed
852
    trace("Starting test_LoopbackRead\n");
853 854 855 856
    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    dcb.BaudRate = FASTBAUD;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
Uwe Bonnes's avatar
Uwe Bonnes committed
857 858
    dcb.fRtsControl=RTS_CONTROL_ENABLE;
    dcb.fDtrControl=DTR_CONTROL_ENABLE;
859 860 861 862 863 864 865 866 867
    dcb.StopBits = ONESTOPBIT;
    ok(SetCommState(hcom, &dcb), "SetCommState failed\n");

    ZeroMemory( &timeouts, sizeof(timeouts));
    timeouts.ReadTotalTimeoutConstant = TIMEOUT;
    ok(SetCommTimeouts(hcom, &timeouts),"SetCommTimeouts failed\n");

    ok(SetCommMask(hcom, EV_TXEMPTY), "SetCommMask failed\n");

Uwe Bonnes's avatar
Uwe Bonnes committed
868
    before = GetTickCount();
869
    ok(WriteFile(hcom,tbuf,sizeof(tbuf),&written, NULL), "WriteFile failed\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
870
    after = GetTickCount();
871
    ok(written == sizeof(tbuf),"WriteFile %d bytes written\n", written);
Uwe Bonnes's avatar
Uwe Bonnes committed
872
    diff = after -before;
873 874

    /* make sure all bytes are written, so Readfile will succeed in one call*/
Uwe Bonnes's avatar
Uwe Bonnes committed
875 876 877
    ok(WaitCommEvent(hcom, &evtmask, NULL), "WaitCommEvent failed\n");
    before = GetTickCount();
    ok(evtmask == EV_TXEMPTY,
878
                 "WaitCommEvent: Unexpected EvtMask 0x%08x, expected 0x%08x\n",
879
		 evtmask, EV_TXEMPTY);
880
    trace("Write %d ms WaitCommEvent EV_TXEMPTY %d ms\n", diff, before- after);
881 882

    read=0;
Uwe Bonnes's avatar
Uwe Bonnes committed
883
    ok(ReadFile(hcom, rbuf, sizeof(rbuf), &read, NULL), "Readfile failed\n");
884
    ok(read == sizeof(tbuf),"ReadFile read %d bytes, expected \"%s\"\n", read,rbuf);
885

886
    /* Now do the same with a slower Baud rate.
Uwe Bonnes's avatar
Uwe Bonnes committed
887
       As we request more characters then written, we will hit the timeout
888 889 890 891 892 893
    */

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    dcb.BaudRate = 9600;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
Uwe Bonnes's avatar
Uwe Bonnes committed
894 895
    dcb.fRtsControl=RTS_CONTROL_ENABLE;
    dcb.fDtrControl=DTR_CONTROL_ENABLE;
896 897 898 899 900
    dcb.StopBits = ONESTOPBIT;
    ok(SetCommState(hcom, &dcb), "SetCommState failed\n");

    ok(SetCommMask(hcom, EV_RXCHAR), "SetCommMask failed\n");
    ok(WriteFile(hcom,tbuf,sizeof(tbuf),&written, NULL), "WriteFile failed\n");
901
    ok(written == sizeof(tbuf),"WriteFile %d bytes written\n", written);
902

Uwe Bonnes's avatar
Uwe Bonnes committed
903 904
    trace("WaitCommEventEV_RXCHAR\n");
    ok(WaitCommEvent(hcom, &evtmask, NULL), "WaitCommEvent failed\n");
905
    ok(evtmask == EV_RXCHAR, "WaitCommEvent: Unexpected EvtMask 0x%08x, expected 0x%08x\n",
906 907 908 909 910
       evtmask, EV_RXCHAR);

    before = GetTickCount();
    res = ReadFile(hcom, rbuf, sizeof(rbuf), &read, NULL);
    after = GetTickCount();
Uwe Bonnes's avatar
Uwe Bonnes committed
911
    ok(res, "Readfile failed\n");
912
    ok(read == sizeof(tbuf),"ReadFile read %d bytes\n", read);
913
    diff = after - before;
914
    trace("Readfile for %d chars took %d ms\n", read, diff);
915
    ok( (diff > TIMEOUT - TIMEDELTA) && (diff < TIMEOUT + TIMEDELTA),
916
	"Timedout Wait took %d ms, expected around %d\n", diff, TIMEOUT);
Uwe Bonnes's avatar
Uwe Bonnes committed
917 918 919 920 921 922 923 924 925 926 927 928 929

    /* now do a plain read with slow speed
     * This will result in several low level reads and a timeout to happen
     */
    dcb.BaudRate = SLOWBAUD;
    ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
    ok(WriteFile(hcom,tbuf,sizeof(tbuf),&written, NULL), "WriteFile failed\n");
    before = GetTickCount();
    read = 0;
    read1 =0;
    i=0;
    do 
    {
930
	res = ReadFile(hcom, rbuf+read, sizeof(rbuf)-read, &read1, NULL);
Uwe Bonnes's avatar
Uwe Bonnes committed
931 932 933 934 935 936
	ok(res, "Readfile failed\n");
	read += read1;
	i++;
    }
    while ((read < sizeof(tbuf)) && (i <10));
    after =  GetTickCount();
937 938
    ok( read == sizeof(tbuf),"ReadFile read %d bytes\n", read);
    trace("Plain Read for %d char at %d baud took %d ms\n", read, SLOWBAUD, after-before);
939 940 941 942
}

static void test_LoopbackCtsRts(HANDLE hcom)
{
943
    DWORD ModemStat = 0, defaultStat = 0;
944 945
    DCB dcb;

946
    memset (&dcb, 0, sizeof (dcb));
947 948 949 950 951 952 953 954 955 956 957 958 959
    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fRtsControl == RTS_CONTROL_HANDSHAKE)
    {
	trace("RTS_CONTROL_HANDSHAKE is set, so don't manipulate RTS\n");
	return;
    }
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    /* XP returns some values in the low nibble, so mask them out*/
    defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
    if(defaultStat & MS_CTS_ON)
    {
	ok(EscapeCommFunction(hcom, CLRRTS), "EscapeCommFunction failed to clear RTS\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
960
	ok ((ModemStat & MS_CTS_ON) == 0, "CTS didn't react: 0x%04x,  expected 0x%04x\n",
961 962 963
	    ModemStat, (defaultStat & ~MS_CTS_ON));
	ok(EscapeCommFunction(hcom, SETRTS), "EscapeCommFunction failed to clear RTS\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
964
	ok (ModemStat ==  defaultStat, "Failed to restore CTS: 0x%04x, expected 0x%04x\n",
965 966 967 968 969 970
	    ModemStat, defaultStat);
    }
    else
    {
	ok(EscapeCommFunction(hcom, SETRTS), "EscapeCommFunction failed to set RTS\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
971
	ok ((ModemStat & MS_CTS_ON) == MS_CTS_ON,
972
            "CTS didn't react: 0x%04x,  expected 0x%04x\n",
973 974 975
	    ModemStat, (defaultStat | MS_CTS_ON));
	ok(EscapeCommFunction(hcom, CLRRTS), "EscapeCommFunction failed to clear RTS\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
976
	ok (ModemStat ==  defaultStat, "Failed to restore CTS: 0x%04x, expected 0x%04x\n",
977 978 979 980 981 982
	    ModemStat, defaultStat);
    }
}

static void test_LoopbackDtrDcd(HANDLE hcom)
{
983
    DWORD ModemStat = 0, defaultStat = 0;
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
    DCB dcb;

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE)
    {
	trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    /* XP returns some values in the low nibble, so mask them out*/
    defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
    if(defaultStat & MS_RLSD_ON)
    {
	ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
999
	ok ((ModemStat & MS_RLSD_ON) == 0, "RLSD didn't react: 0x%04x,  expected 0x%04x\n",
1000 1001 1002
	    ModemStat, (defaultStat & ~MS_RLSD_ON));
	ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1003
	ok (ModemStat ==  defaultStat, "Failed to restore RLSD: 0x%04x, expected 0x%04x\n",
1004 1005 1006 1007 1008 1009
	    ModemStat, defaultStat);
    }
    else
    {
	ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
1010
	ok ((ModemStat & MS_RLSD_ON) == MS_RLSD_ON,
1011
            "RLSD didn't react: 0x%04x,  expected 0x%04x\n",
1012 1013 1014
	    ModemStat, (defaultStat | MS_RLSD_ON));
	ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1015
	ok (ModemStat ==  defaultStat, "Failed to restore RLSD: 0x%04x, expected 0x%04x\n",
1016 1017 1018 1019 1020 1021
	    ModemStat, defaultStat);
    }
}

static void test_LoopbackDtrDsr(HANDLE hcom)
{
1022
    DWORD ModemStat = 0, defaultStat = 0;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    DCB dcb;

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
    {
	trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    /* XP returns some values in the low nibble, so mask them out*/
    defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
    if(defaultStat & MS_DSR_ON)
    {
	ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1038
	ok ((ModemStat & MS_DSR_ON) == 0, "CTS didn't react: 0x%04x,  expected 0x%04x\n",
1039 1040 1041
	    ModemStat, (defaultStat & ~MS_DSR_ON));
	ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1042
	ok (ModemStat ==  defaultStat, "Failed to restore DSR: 0x%04x, expected 0x%04x\n",
1043 1044 1045 1046 1047 1048
	    ModemStat, defaultStat);
    }
    else
    {
	ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
1049
	ok ((ModemStat & MS_DSR_ON) == MS_DSR_ON,
1050
            "CTS didn't react: 0x%04x,expected 0x%04x\n",
1051 1052 1053
	    ModemStat, (defaultStat | MS_DSR_ON));
	ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1054
	ok (ModemStat ==  defaultStat, "Failed to restore DSR: 0x%04x, expected 0x%04x\n",
1055 1056 1057 1058 1059 1060
	    ModemStat, defaultStat);
    }
}

static void test_LoopbackDtrRing(HANDLE hcom)
{
1061
    DWORD ModemStat = 0, defaultStat = 0;
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    DCB dcb;

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == DTR_CONTROL_HANDSHAKE)
    {
	trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    /* XP returns some values in the low nibble, so mask them out*/
    defaultStat &= MS_CTS_ON|MS_DSR_ON|MS_RING_ON|MS_RLSD_ON;
    if(defaultStat & MS_RING_ON)
    {
	ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1077
	ok ((ModemStat & MS_RING_ON) == 0, "RING didn't react: 0x%04x,  expected 0x%04x\n",
1078 1079 1080
	    ModemStat, (defaultStat & ~MS_RING_ON));
	ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1081
	ok (ModemStat ==  defaultStat, "Failed to restore RING: 0x%04x, expected 0x%04x\n",
1082 1083 1084 1085 1086 1087
	    ModemStat, defaultStat);
    }
    else
    {
	ok(EscapeCommFunction(hcom, SETDTR), "EscapeCommFunction failed to set DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
1088
	ok ((ModemStat & MS_RING_ON) == MS_RING_ON,
1089
            "RING didn't react: 0x%04x,expected 0x%04x\n",
1090 1091 1092
	    ModemStat, (defaultStat | MS_RING_ON));
	ok(EscapeCommFunction(hcom, CLRDTR), "EscapeCommFunction failed to clear DTR\n");
	ok(GetCommModemStatus(hcom, &ModemStat), "GetCommModemStatus failed\n");
1093
	ok (ModemStat ==  defaultStat, "Failed to restore RING: 0x%04x, expected 0x%04x\n",
1094 1095 1096 1097 1098
	    ModemStat, defaultStat);
    }
}

/*
Uwe Bonnes's avatar
Uwe Bonnes committed
1099 1100 1101 1102
 * Set up a WaitCommEvent for anything in the receive buffer,
 * then write to TX to put a character
 * into the RX buffer
 * Need Loopback TX->RX
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
*/

static void  test_WaitRx(HANDLE hcom)
{
    OVERLAPPED overlapped, overlapped_w;
    HANDLE hComPortEvent, hComWriteEvent;
    DWORD before, after, after1, diff, success_wait = FALSE, success_write;
    DWORD err_wait, err_write, written, evtmask=0;

    ok(SetCommMask(hcom, EV_RXCHAR), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");
    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;

1118 1119
    hComWriteEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComWriteEvent != NULL, "CreateEvent res %d\n", GetLastError());
1120 1121 1122 1123
    ZeroMemory( &overlapped_w, sizeof(overlapped_w));
    overlapped_w.hEvent = hComWriteEvent;

    before = GetTickCount();
Uwe Bonnes's avatar
Uwe Bonnes committed
1124
    {success_wait = WaitCommEvent(hcom, &evtmask, &overlapped);}
1125 1126
    err_wait = GetLastError();
    after = GetTickCount();
1127
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success_wait, err_wait, evtmask);
Uwe Bonnes's avatar
Uwe Bonnes committed
1128
    ok(success_wait || err_wait == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1129 1130 1131 1132 1133
    trace("overlapped WriteCommEvent returned.\n");

    success_write= WriteFile(hcom, "X", 1, &written, &overlapped_w);
    err_write = GetLastError();
    ok(success_write || err_write == ERROR_IO_PENDING,
1134
       "overlapped WriteFile failed, err %d\n",
1135 1136 1137 1138 1139
       err_write);

    if (!success_write && (err_write == ERROR_IO_PENDING)) {
      success_write = WaitForSingleObjectEx(hComWriteEvent, TIMEOUT, TRUE);
      err_write = GetLastError();
1140
      ok(success_write == WAIT_OBJECT_0, "WaitForSingleObjectEx, res %d, err %d\n",
1141 1142 1143 1144 1145 1146
	 success_write, err_write);
    }
    Sleep(TIMEOUT >>1);
    success_write = GetOverlappedResult(hcom, &overlapped_w, &written, FALSE);
    err_write = GetLastError();

1147 1148
    trace("Write after Wait res 0x%08x err %d\n",success_write, err_write);
    ok(success_write && written ==1, "Write after Wait res 0x%08x err %d\n",
1149 1150 1151 1152 1153
       success_write, err_write);

    if (!success_wait && (err_wait == ERROR_IO_PENDING)) {
      success_wait = WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE);
      err_wait = GetLastError();
1154
      ok(success_wait == WAIT_OBJECT_0, "wait hComPortEvent, res 0x%08x, err %d\n",
1155 1156 1157 1158 1159
	 success_wait, err_wait);
    }
    success_wait = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err_wait = GetLastError();
    after1 = GetTickCount();
1160
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1161 1162
	  success_wait, err_wait, evtmask, after-before, after1-before);

1163
    ok(evtmask & EV_RXCHAR, "Detect  EV_RXCHAR: 0x%08x, expected 0x%08x\n",
1164 1165 1166
       evtmask, EV_RXCHAR);
    diff = after1 - before;
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1167
	"Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1168 1169 1170 1171 1172 1173 1174 1175

}

/* Change the controling line after the given timeout to the given state
   By the loopback, this should trigger the WaitCommEvent
*/
static DWORD CALLBACK toggle_ctlLine(LPVOID arg)
{
1176
    DWORD_PTR *args = arg;
1177 1178 1179 1180 1181 1182
    DWORD timeout = args[0];
    DWORD ctl     = args[1];
    HANDLE hcom   = (HANDLE) args[2];
    HANDLE hComPortEvent = (HANDLE) args[3];
    DWORD success, err;

1183
    trace("toggle_ctlLine timeout %d ctl 0x%08x handle %p\n", timeout, ctl, hcom );
1184
    Sleep(timeout);
1185
    ok(EscapeCommFunction(hcom, ctl),"EscapeCommFunction 0x%08x failed\n", ctl);
1186 1187 1188
    trace("toggle_ctline done\n");
    success = WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE);
    err = GetLastError();
1189
    trace("toggle_ctline WaitForSingleObjectEx res 0x%08x err %d\n",
1190 1191 1192 1193
	  success, err);
    return 0;
}

Uwe Bonnes's avatar
Uwe Bonnes committed
1194 1195 1196 1197
/*
 * Wait for a change in CTS
 * Needs Loopback from DTR to CTS
 */
1198 1199 1200 1201 1202 1203
static void  test_WaitCts(HANDLE hcom)
{
    DCB dcb;
    OVERLAPPED overlapped;
    HANDLE hComPortEvent;
    HANDLE alarmThread;
1204 1205
    DWORD_PTR args[4];
    DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat = 0;
1206 1207

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
Uwe Bonnes's avatar
Uwe Bonnes committed
1208 1209 1210
    dcb.fRtsControl=RTS_CONTROL_ENABLE;
    dcb.fDtrControl=DTR_CONTROL_ENABLE;
    ok(SetCommState(hcom, &dcb), "SetCommState failed\n");
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
    if (dcb.fDtrControl == RTS_CONTROL_DISABLE)
    {
	trace("RTS_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    args[0]= TIMEOUT >>1;
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    if(defaultStat & MS_CTS_ON)
	args[1] = CLRRTS;
    else
	args[1] = SETRTS;
1222
    args[2]=(DWORD_PTR)hcom;
1223

1224
    trace("test_WaitCts timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1225 1226 1227 1228

    ok(SetCommMask(hcom, EV_CTS), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");
1229 1230
    args[3] = (DWORD_PTR)hComPortEvent;
    alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
Uwe Bonnes's avatar
Uwe Bonnes committed
1231 1232
    /* Wait a minimum to let the thread start up */
    Sleep(10);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    trace("Thread created\n");
    ok(alarmThread !=0 , "CreateThread Failed\n");

    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;
    before = GetTickCount();
    success = WaitCommEvent(hcom, &evtmask, &overlapped);
    err = GetLastError();
    after = GetTickCount();

1243
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
Uwe Bonnes's avatar
Uwe Bonnes committed
1244
    ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1245 1246
    trace("overlapped WriteCommEvent returned.\n");
    if (!success && (err == ERROR_IO_PENDING))
Uwe Bonnes's avatar
Uwe Bonnes committed
1247 1248
	ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
		     "WaitCts hComPortEvent failed\n");
1249 1250 1251
    success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err = GetLastError();
    after1 = GetTickCount();
1252
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1253 1254
	  success, err, evtmask, after-before, after1-before);

1255
    ok(evtmask & EV_CTS, "Failed to detect  EV_CTS: 0x%08x, expected 0x%08x\n",
1256 1257 1258
		 evtmask, EV_CTS);
    ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
    if(defaultStat & MS_CTS_ON)
Uwe Bonnes's avatar
Uwe Bonnes committed
1259
	ok((evtmask & MS_CTS_ON) == 0,"CTS didn't change state!\n");
1260
    else
Uwe Bonnes's avatar
Uwe Bonnes committed
1261
	ok((evtmask & MS_CTS_ON), "CTS didn't change state!\n");
1262 1263

    diff = after1 - before;
Uwe Bonnes's avatar
Uwe Bonnes committed
1264
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1265
                  "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1266 1267 1268 1269 1270 1271 1272 1273

    /*restore RTS Settings*/
    if(defaultStat & MS_CTS_ON)
	args[1] = SETRTS;
    else
	args[1] = CLRRTS;
}

Uwe Bonnes's avatar
Uwe Bonnes committed
1274 1275 1276 1277 1278
/* Change the  Comm Mask while a Wait is going on
   WaitCommevent should return with a EVTMASK set to zero
*/
static DWORD CALLBACK reset_CommMask(LPVOID arg)
{
1279
    DWORD_PTR *args = arg;
Uwe Bonnes's avatar
Uwe Bonnes committed
1280 1281 1282
    DWORD timeout = args[0];
    HANDLE hcom   = (HANDLE) args[1];

1283
    trace(" Changing CommMask on the fly for handle %p after timeout %d\n",
Uwe Bonnes's avatar
Uwe Bonnes committed
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	  hcom, timeout);
    Sleep(timeout);
    ok(SetCommMask(hcom, 0),"SetCommMask %p failed\n", hcom);
    trace("SetCommMask changed\n");
    return 0;
}

/* Set up a Wait for a change on CTS. We don't toggle any line, but we
   reset the CommMask and expect the wait to return with a mask of 0
   No special port connections needed
*/
static void  test_AbortWaitCts(HANDLE hcom)
{
    DCB dcb;
    OVERLAPPED overlapped;
    HANDLE hComPortEvent;
    HANDLE alarmThread;
1301
    DWORD_PTR args[2];
Uwe Bonnes's avatar
Uwe Bonnes committed
1302 1303 1304 1305 1306 1307 1308 1309 1310
    DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0;

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == RTS_CONTROL_DISABLE)
    {
	trace("RTS_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    args[0]= TIMEOUT >>1;
1311
    args[1]= (DWORD_PTR)hcom;
Uwe Bonnes's avatar
Uwe Bonnes committed
1312

1313
    trace("test_AbortWaitCts timeout %ld handle %p\n",args[0], hcom);
Uwe Bonnes's avatar
Uwe Bonnes committed
1314 1315 1316 1317

    ok(SetCommMask(hcom, EV_CTS), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");
1318
    alarmThread = CreateThread(NULL, 0, reset_CommMask, args, 0, &alarmThreadId);
Uwe Bonnes's avatar
Uwe Bonnes committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    /* Wait a minimum to let the thread start up */
    Sleep(10);
    trace("Thread created\n");
    ok(alarmThread !=0 , "CreateThread Failed\n");

    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;
    before = GetTickCount();
    success = WaitCommEvent(hcom, &evtmask, &overlapped);
    err = GetLastError();
    after = GetTickCount();

1331
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
Uwe Bonnes's avatar
Uwe Bonnes committed
1332 1333 1334 1335 1336 1337 1338 1339
    ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
    trace("overlapped WriteCommEvent returned.\n");
    if (!success && (err == ERROR_IO_PENDING))
	ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
		     "AbortWaitCts hComPortEvent failed\n");
    success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err = GetLastError();
    after1 = GetTickCount();
1340
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
Uwe Bonnes's avatar
Uwe Bonnes committed
1341 1342
	  success, err, evtmask, after-before, after1-before);

1343
    ok(evtmask == 0, "Incorrect EventMask 0x%08x returned on Wait aborted bu SetCommMask, expected 0x%08x\n",
Uwe Bonnes's avatar
Uwe Bonnes committed
1344 1345 1346 1347
		 evtmask, 0);
    ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
    diff = after1 - before;
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1348
                  "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
Uwe Bonnes's avatar
Uwe Bonnes committed
1349 1350 1351 1352 1353 1354 1355

}

/*
 * Wait for a change in DSR
 * Needs Loopback from DTR to DSR
 */
1356 1357 1358 1359 1360 1361
static void  test_WaitDsr(HANDLE hcom)
{
    DCB dcb;
    OVERLAPPED overlapped;
    HANDLE hComPortEvent;
    HANDLE alarmThread;
1362 1363
    DWORD_PTR args[3];
    DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat = 0;
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
    {
	trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    args[0]= TIMEOUT >>1;
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    if(defaultStat & MS_DSR_ON)
	args[1] = CLRDTR;
    else
	args[1] = SETDTR;
1377
    args[2]= (DWORD_PTR)hcom;
1378

1379
    trace("test_WaitDsr timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1380 1381 1382 1383

    ok(SetCommMask(hcom, EV_DSR), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");
1384
    alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1385 1386 1387 1388 1389 1390 1391 1392 1393
    ok(alarmThread !=0 , "CreateThread Failed\n");

    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;
    before = GetTickCount();
    success = WaitCommEvent(hcom, &evtmask, &overlapped);
    err = GetLastError();
    after = GetTickCount();

1394
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
Uwe Bonnes's avatar
Uwe Bonnes committed
1395
    ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1396 1397
    trace("overlapped WriteCommEvent returned.\n");
    if (!success && (err == ERROR_IO_PENDING))
Uwe Bonnes's avatar
Uwe Bonnes committed
1398
	ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1399 1400 1401 1402
		     "wait hComPortEvent failed\n");
    success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err = GetLastError();
    after1 = GetTickCount();
1403
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1404 1405
	  success, err, evtmask, after-before, after1-before);

1406
    ok(evtmask & EV_DSR, "Failed to detect  EV_DSR: 0x%08x, expected 0x%08x\n",
Uwe Bonnes's avatar
Uwe Bonnes committed
1407
		 evtmask, EV_DSR);
1408 1409
    ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
    if(defaultStat & MS_DSR_ON)
Uwe Bonnes's avatar
Uwe Bonnes committed
1410
	ok((evtmask & MS_DSR_ON) == 0,"DTR didn't change state!\n");
1411
    else
Uwe Bonnes's avatar
Uwe Bonnes committed
1412
	ok((evtmask & MS_DSR_ON), "DTR didn't change state!\n");
1413 1414

    diff = after1 - before;
Uwe Bonnes's avatar
Uwe Bonnes committed
1415
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1416
                  "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1417 1418 1419 1420 1421 1422 1423 1424

    /*restore RTS Settings*/
    if(defaultStat & MS_DSR_ON)
	args[1] = SETDTR;
    else
	args[1] = CLRDTR;
}

Uwe Bonnes's avatar
Uwe Bonnes committed
1425 1426 1427 1428
/*
 * Wait for a Ring
 * Needs Loopback from DTR to RING
 */
1429 1430 1431 1432 1433 1434
static void  test_WaitRing(HANDLE hcom)
{
    DCB dcb;
    OVERLAPPED overlapped;
    HANDLE hComPortEvent;
    HANDLE alarmThread;
1435 1436
    DWORD_PTR args[3];
    DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat;
1437
    BOOL ret;
1438 1439 1440 1441 1442 1443 1444 1445

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
    {
	trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    args[0]= TIMEOUT >>1;
1446 1447 1448 1449 1450
    ok((ret = GetCommModemStatus(hcom, &defaultStat)), "GetCommModemStatus failed\n");
    if (!ret) {
	skip("modem status failed -> skip.\n");
	return;
    }
1451 1452 1453 1454
    if(defaultStat & MS_RING_ON)
	args[1] = CLRDTR;
    else
	args[1] = SETDTR;
1455
    args[2]=(DWORD_PTR) hcom;
1456

1457
    trace("test_WaitRing timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1458 1459 1460 1461

    ok(SetCommMask(hcom, EV_RING), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");
1462
    alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1463 1464 1465 1466 1467 1468 1469 1470 1471
    ok(alarmThread !=0 , "CreateThread Failed\n");

    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;
    before = GetTickCount();
    success = WaitCommEvent(hcom, &evtmask, &overlapped);
    err = GetLastError();
    after = GetTickCount();

1472
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
Uwe Bonnes's avatar
Uwe Bonnes committed
1473
    ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1474 1475
    trace("overlapped WriteCommEvent returned.\n");
    if (!success && (err == ERROR_IO_PENDING))
Uwe Bonnes's avatar
Uwe Bonnes committed
1476
	ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1477 1478 1479 1480
		     "wait hComPortEvent failed\n");
    success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err = GetLastError();
    after1 = GetTickCount();
1481
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1482 1483
	  success, err, evtmask, after-before, after1-before);

1484
    ok(evtmask & EV_RING, "Failed to detect  EV_RING: 0x%08x, expected 0x%08x\n",
1485 1486 1487
       evtmask, EV_CTS);
    ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
    if(defaultStat & MS_RING_ON)
Uwe Bonnes's avatar
Uwe Bonnes committed
1488
	ok((evtmask & MS_RING_ON) == 0,"DTR didn't change state!\n");
1489
    else
Uwe Bonnes's avatar
Uwe Bonnes committed
1490
	ok((evtmask & MS_RING_ON), "DTR didn't change state!\n");
1491 1492

    diff = after1 - before;
Uwe Bonnes's avatar
Uwe Bonnes committed
1493
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1494
                  "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1495 1496 1497 1498 1499 1500 1501

    /*restore RTS Settings*/
    if(defaultStat & MS_RING_ON)
	args[1] = SETDTR;
    else
	args[1] = CLRDTR;
}
Uwe Bonnes's avatar
Uwe Bonnes committed
1502 1503 1504 1505
/*
 * Wait for a change in DCD
 * Needs Loopback from DTR to DCD
 */
1506 1507 1508 1509 1510 1511
static void  test_WaitDcd(HANDLE hcom)
{
    DCB dcb;
    OVERLAPPED overlapped;
    HANDLE hComPortEvent;
    HANDLE alarmThread;
1512 1513
    DWORD_PTR args[3];
    DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0, defaultStat = 0;
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526

    ok(GetCommState(hcom, &dcb), "GetCommState failed\n");
    if (dcb.fDtrControl == DTR_CONTROL_DISABLE)
    {
	trace("DTR_CONTROL_HANDSHAKE is set, so don't manipulate DTR\n");
	return;
    }
    args[0]= TIMEOUT >>1;
    ok(GetCommModemStatus(hcom, &defaultStat), "GetCommModemStatus failed\n");
    if(defaultStat & MS_RLSD_ON)
	args[1] = CLRDTR;
    else
	args[1] = SETDTR;
1527
    args[2]= (DWORD_PTR)hcom;
1528

1529
    trace("test_WaitDcd timeout %ld clt 0x%08lx handle %p\n",args[0], args[1], hcom);
1530 1531 1532 1533

    ok(SetCommMask(hcom, EV_RLSD), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");
1534
    alarmThread = CreateThread(NULL, 0, toggle_ctlLine, args, 0, &alarmThreadId);
1535 1536 1537 1538 1539 1540 1541 1542 1543
    ok(alarmThread !=0 , "CreateThread Failed\n");

    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;
    before = GetTickCount();
    success = WaitCommEvent(hcom, &evtmask, &overlapped);
    err = GetLastError();
    after = GetTickCount();

1544
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
Uwe Bonnes's avatar
Uwe Bonnes committed
1545
    ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
1546 1547
    trace("overlapped WriteCommEvent returned.\n");
    if (!success && (err == ERROR_IO_PENDING))
Uwe Bonnes's avatar
Uwe Bonnes committed
1548
	ok(WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE) == 0,
1549 1550 1551 1552
		     "wait hComPortEvent failed\n");
    success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err = GetLastError();
    after1 = GetTickCount();
1553
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1554 1555
	  success, err, evtmask, after-before, after1-before);

1556
    ok(evtmask & EV_RLSD, "Failed to detect  EV_RLSD: 0x%08x, expected 0x%08x\n",
1557 1558 1559
		 evtmask, EV_CTS);
    ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");
    if(defaultStat & MS_RLSD_ON)
Uwe Bonnes's avatar
Uwe Bonnes committed
1560
	ok((evtmask & MS_RLSD_ON) == 0,"DTR didn't change state!\n");
1561
    else
Uwe Bonnes's avatar
Uwe Bonnes committed
1562
	ok((evtmask & MS_RLSD_ON), "DTR didn't change state!\n");
1563 1564

    diff = after1 - before;
Uwe Bonnes's avatar
Uwe Bonnes committed
1565
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1566
                  "Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1567 1568 1569 1570 1571 1572 1573 1574

    /*restore RTS Settings*/
    if(defaultStat & MS_RLSD_ON)
	args[1] = SETDTR;
    else
	args[1] = CLRDTR;
}

Uwe Bonnes's avatar
Uwe Bonnes committed
1575 1576 1577 1578 1579
/* 
   Set Break after timeout
*/
static DWORD CALLBACK set_CommBreak(LPVOID arg)
{
1580
    DWORD_PTR *args = arg;
Uwe Bonnes's avatar
Uwe Bonnes committed
1581 1582 1583
    DWORD timeout = args[0];
    HANDLE hcom   = (HANDLE) args[1];

1584
    trace("SetCommBreak for handle %p after timeout %d\n",
Uwe Bonnes's avatar
Uwe Bonnes committed
1585 1586 1587 1588 1589 1590 1591
	  hcom, timeout);
    Sleep(timeout);
    ok(SetCommBreak(hcom),"SetCommBreak %p failed\n", hcom);
    trace("SetCommBreak done\n");
    return 0;
}

1592
/*
Uwe Bonnes's avatar
Uwe Bonnes committed
1593 1594
   Wait for the Break condition (TX resp. RX active)
   Needs Loopback TX-RX
1595 1596 1597 1598 1599
*/
static void  test_WaitBreak(HANDLE hcom)
{
    OVERLAPPED overlapped;
    HANDLE hComPortEvent;
Uwe Bonnes's avatar
Uwe Bonnes committed
1600
    HANDLE alarmThread;
1601
    DWORD_PTR args[2];
Uwe Bonnes's avatar
Uwe Bonnes committed
1602
    DWORD alarmThreadId, before, after, after1, diff, success, err, written, evtmask=0;
1603 1604 1605 1606 1607

    ok(SetCommMask(hcom, EV_BREAK), "SetCommMask failed\n");
    hComPortEvent =  CreateEvent( NULL, TRUE, FALSE, NULL );
    ok(hComPortEvent != 0, "CreateEvent failed\n");

Uwe Bonnes's avatar
Uwe Bonnes committed
1608 1609
    trace("test_WaitBreak\n");
    args[0]= TIMEOUT >>1;
1610 1611
    args[1]= (DWORD_PTR)hcom;
    alarmThread = CreateThread(NULL, 0, set_CommBreak, args, 0, &alarmThreadId);
Uwe Bonnes's avatar
Uwe Bonnes committed
1612 1613 1614 1615 1616
    /* Wait a minimum to let the thread start up */
    Sleep(10);
    trace("Thread created\n");
    ok(alarmThread !=0 , "CreateThread Failed\n");

1617 1618 1619 1620 1621 1622 1623
    ZeroMemory( &overlapped, sizeof(overlapped));
    overlapped.hEvent = hComPortEvent;
    before = GetTickCount();
    success = WaitCommEvent(hcom, &evtmask, &overlapped);
    err = GetLastError();
    after = GetTickCount();

1624
    trace("Success 0x%08x err %d evtmask 0x%08x\n", success, err, evtmask);
1625 1626 1627 1628
    ok(success || err == ERROR_IO_PENDING, "overlapped WaitCommEvent failed\n");
    trace("overlapped WriteCommEvent returned.\n");

    if (!success && (err == ERROR_IO_PENDING))
1629 1630 1631 1632
    {
        success = WaitForSingleObjectEx(hComPortEvent, TIMEOUT, TRUE);
        ok(!success, "wait hComPortEvent res %d\n", GetLastError());
    }
1633 1634 1635
    success = GetOverlappedResult(hcom, &overlapped, &written, FALSE);
    err = GetLastError();
    after1 = GetTickCount();
1636
    trace("Success 0x%08x err %d evtmask 0x%08x diff1 %d, diff2 %d\n",
1637 1638
	  success, err, evtmask, after-before, after1-before);

1639
    ok(evtmask & EV_BREAK, "Failed to detect  EV_BREAK: 0x%08x, expected 0x%08x\n",
1640 1641 1642 1643 1644
       evtmask, EV_BREAK);
    ok(GetCommModemStatus(hcom, &evtmask), "GetCommModemStatus failed\n");

    diff = after1 - before;
    ok ((diff > (TIMEOUT>>1) -TIMEDELTA) && (diff < (TIMEOUT>>1) + TIMEDELTA),
1645
	"Unexpected time %d, expected around %d\n", diff, TIMEOUT>>1);
1646

Uwe Bonnes's avatar
Uwe Bonnes committed
1647
    ok(ClearCommBreak(hcom), "ClearCommBreak failed\n");
1648 1649
}

1650 1651 1652 1653 1654 1655
static void test_stdio(void)
{
    DCB dcb;

    /* cygwin tries this to determine the stdin handle type */
    ok( !GetCommState( GetStdHandle(STD_INPUT_HANDLE), &dcb ), "GetCommState succeeded on stdin\n" );
1656 1657
    ok( GetLastError() == ERROR_INVALID_HANDLE || GetLastError() == ERROR_INVALID_FUNCTION,
        "got error %u\n", GetLastError() );
1658 1659
}

1660 1661
START_TEST(comm)
{
1662
    HANDLE hcom;
Eric Pouech's avatar
Eric Pouech committed
1663
    /* use variables and not #define to compile the code */
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    BOOL loopback_txd_rxd  = LOOPBACK_TXD_RXD;
    BOOL loopback_rts_cts  = LOOPBACK_CTS_RTS;
    BOOL loopback_dtr_dsr  = LOOPBACK_DTR_DSR;
    BOOL loopback_dtr_ring = LOOPBACK_DTR_RING;
    BOOL loopback_dtr_dcd  = LOOPBACK_DTR_DCD;

    test_BuildCommDCB();
    hcom = test_OpenComm(FALSE);
    if (hcom != INVALID_HANDLE_VALUE)
    {
	test_GetModemStatus(hcom);
	test_ReadTimeOut(hcom);
	test_waittxempty(hcom);
	CloseHandle(hcom);
    }
    hcom = test_OpenComm(FALSE);
    if (hcom != INVALID_HANDLE_VALUE)
    {
Eric Pouech's avatar
Eric Pouech committed
1682
	Sleep(200); /* Give the laster character of test_waittxempty to drop into the receiver */
1683 1684 1685
	test_ClearCommErrors(hcom);
	CloseHandle(hcom);
    }
Eric Pouech's avatar
Eric Pouech committed
1686 1687 1688 1689 1690 1691
    hcom = test_OpenComm(FALSE);
    if (hcom != INVALID_HANDLE_VALUE)
    {
        test_non_pending_errors(hcom);
	CloseHandle(hcom);
    }
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
    if((loopback_txd_rxd) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
    {
	test_LoopbackRead(hcom);
	CloseHandle(hcom);
    }
    if((loopback_rts_cts) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
    {
	test_LoopbackCtsRts(hcom);
	CloseHandle(hcom);
    }
    if((loopback_dtr_dsr) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
    {
	test_LoopbackDtrDsr(hcom);
	CloseHandle(hcom);
    }
    if((loopback_dtr_ring) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
    {
	test_LoopbackDtrRing(hcom);
	CloseHandle(hcom);
    }
    if((loopback_dtr_dcd) && ((hcom = test_OpenComm(FALSE))!=INVALID_HANDLE_VALUE))
    {
	test_LoopbackDtrDcd(hcom);
	CloseHandle(hcom);
    }
    if((loopback_txd_rxd) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
    {
        test_WaitRx(hcom);
        CloseHandle(hcom);
    }
    if((loopback_rts_cts) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
    {
	test_WaitCts(hcom);
	CloseHandle(hcom);
    }
Uwe Bonnes's avatar
Uwe Bonnes committed
1727 1728 1729 1730 1731
    if((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE)
    {
	test_AbortWaitCts(hcom);
	CloseHandle(hcom);
    }
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
    if((loopback_dtr_dsr) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
    {
	test_WaitDsr(hcom);
	CloseHandle(hcom);
    }
    if((loopback_dtr_ring) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
    {
	test_WaitRing(hcom);
	CloseHandle(hcom);
    }
    if((loopback_dtr_dcd) && ((hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE))
    {
	test_WaitDcd(hcom);
	CloseHandle(hcom);
    }
Uwe Bonnes's avatar
Uwe Bonnes committed
1747
    if(loopback_txd_rxd && (hcom = test_OpenComm(TRUE))!=INVALID_HANDLE_VALUE)
1748 1749 1750 1751
    {
	test_WaitBreak(hcom);
	CloseHandle(hcom);
    }
1752
    test_stdio();
1753
}