summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/comm/comm_io.c
blob: 9904eab5d81777b75c3d55343a28a8c6eb1612b1 (plain)
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
/**
 * WinPR: Windows Portable Runtime
 * Serial Communication API
 *
 * Copyright 2014 Hewlett-Packard Development Company, L.P.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <winpr/config.h>

#if defined __linux__ && !defined ANDROID

#include <winpr/assert.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>

#include <winpr/io.h>
#include <winpr/wlog.h>
#include <winpr/wtypes.h>

#include "comm.h"

BOOL _comm_set_permissive(HANDLE hDevice, BOOL permissive)
{
	WINPR_COMM* pComm = (WINPR_COMM*)hDevice;

	if (!CommIsHandled(hDevice))
		return FALSE;

	pComm->permissive = permissive;
	return TRUE;
}

/* Computes VTIME in deciseconds from Ti in milliseconds */
static UCHAR _vtime(ULONG Ti)
{
	/* FIXME: look for an equivalent math function otherwise let
	 * do the compiler do the optimization */
	if (Ti == 0)
		return 0;
	else if (Ti < 100)
		return 1;
	else if (Ti > 25500)
		return 255; /* 0xFF */
	else
		return Ti / 100;
}

/**
 * ERRORS:
 *   ERROR_INVALID_HANDLE
 *   ERROR_NOT_SUPPORTED
 *   ERROR_INVALID_PARAMETER
 *   ERROR_TIMEOUT
 *   ERROR_IO_DEVICE
 *   ERROR_BAD_DEVICE
 */
BOOL CommReadFile(HANDLE hDevice, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
                  LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
{
	WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
	int biggestFd = -1;
	fd_set read_set;
	int nbFds = 0;
	COMMTIMEOUTS* pTimeouts = NULL;
	UCHAR vmin = 0;
	UCHAR vtime = 0;
	ULONGLONG Tmax = 0;
	struct timeval tmaxTimeout;
	struct timeval* pTmaxTimeout = NULL;
	struct termios currentTermios;
	EnterCriticalSection(&pComm->ReadLock); /* KISSer by the function's beginning */

	if (!CommIsHandled(hDevice))
		goto return_false;

	if (lpOverlapped != NULL)
	{
		SetLastError(ERROR_NOT_SUPPORTED);
		goto return_false;
	}

	if (lpNumberOfBytesRead == NULL)
	{
		SetLastError(ERROR_INVALID_PARAMETER); /* since we doesn't suppport lpOverlapped != NULL */
		goto return_false;
	}

	*lpNumberOfBytesRead = 0; /* will be ajusted if required ... */

	if (nNumberOfBytesToRead <= 0) /* N */
	{
		goto return_true; /* FIXME: or FALSE? */
	}

	if (tcgetattr(pComm->fd, &currentTermios) < 0)
	{
		SetLastError(ERROR_IO_DEVICE);
		goto return_false;
	}

	if (currentTermios.c_lflag & ICANON)
	{
		CommLog_Print(WLOG_WARN, "Canonical mode not supported"); /* the timeout could not be set */
		SetLastError(ERROR_NOT_SUPPORTED);
		goto return_false;
	}

	/* http://msdn.microsoft.com/en-us/library/hh439614%28v=vs.85%29.aspx
	 * http://msdn.microsoft.com/en-us/library/windows/hardware/hh439614%28v=vs.85%29.aspx
	 *
	 * ReadIntervalTimeout  | ReadTotalTimeoutMultiplier | ReadTotalTimeoutConstant | VMIN | VTIME |
	 * TMAX  | 0            |            0               |           0              |   N  |   0   |
	 * INDEF | Blocks for N bytes available. 0< Ti <MAXULONG	|            0               | 0 |
	 * N  |   Ti  | INDEF | Blocks on first byte, then use Ti between bytes. MAXULONG       | 0 | 0
	 * |   0  |   0   |   0   | Returns immediately with bytes available (don't block) MAXULONG |
	 * MAXULONG           |      0< Tc <MAXULONG     |   N  |   0   |   Tc  | Blocks on first byte
	 * during Tc or returns immediately whith bytes available MAXULONG       |            m |
	 * MAXULONG          |                      | Invalid 0            |            m |      0< Tc
	 * <MAXULONG     |   N  |   0   |  Tmax | Blocks on first byte during Tmax or returns
	 * immediately whith bytes available 0< Ti <MAXULONG    |            m               |      0<
	 * Tc <MAXULONG     |   N  |   Ti  |  Tmax | Blocks on first byte, then use Ti between bytes.
	 * Tmax is used for the whole system call.
	 */
	/* NB: timeouts are in milliseconds, VTIME are in deciseconds and is an unsigned char */
	/* FIXME: double check whether open(pComm->fd_read_event, O_NONBLOCK) doesn't conflict with
	 * above use cases */
	pTimeouts = &(pComm->timeouts);

	if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
	    (pTimeouts->ReadTotalTimeoutConstant == MAXULONG))
	{
		CommLog_Print(
		    WLOG_WARN,
		    "ReadIntervalTimeout and ReadTotalTimeoutConstant cannot be both set to MAXULONG");
		SetLastError(ERROR_INVALID_PARAMETER);
		goto return_false;
	}

	/* VMIN */

	if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
	    (pTimeouts->ReadTotalTimeoutMultiplier == 0) && (pTimeouts->ReadTotalTimeoutConstant == 0))
	{
		vmin = 0;
	}
	else
	{
		/* N */
		/* vmin = nNumberOfBytesToRead < 256 ? nNumberOfBytesToRead : 255;*/ /* 0xFF */
		/* NB: we might wait endlessly with vmin=N, prefer to
		 * force vmin=1 and return with bytes
		 * available. FIXME: is a feature disarded here? */
		vmin = 1;
	}

	/* VTIME */

	if ((pTimeouts->ReadIntervalTimeout > 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG))
	{
		/* Ti */
		vtime = _vtime(pTimeouts->ReadIntervalTimeout);
	}

	/* TMAX */
	pTmaxTimeout = &tmaxTimeout;

	if ((pTimeouts->ReadIntervalTimeout == MAXULONG) &&
	    (pTimeouts->ReadTotalTimeoutMultiplier == MAXULONG))
	{
		/* Tc */
		Tmax = pTimeouts->ReadTotalTimeoutConstant;
	}
	else
	{
		/* Tmax */
		Tmax = nNumberOfBytesToRead * pTimeouts->ReadTotalTimeoutMultiplier +
		       pTimeouts->ReadTotalTimeoutConstant;

		/* INDEFinitely */
		if ((Tmax == 0) && (pTimeouts->ReadIntervalTimeout < MAXULONG) &&
		    (pTimeouts->ReadTotalTimeoutMultiplier == 0))
			pTmaxTimeout = NULL;
	}

	if ((currentTermios.c_cc[VMIN] != vmin) || (currentTermios.c_cc[VTIME] != vtime))
	{
		currentTermios.c_cc[VMIN] = vmin;
		currentTermios.c_cc[VTIME] = vtime;

		if (tcsetattr(pComm->fd, TCSANOW, &currentTermios) < 0)
		{
			CommLog_Print(WLOG_WARN,
			              "CommReadFile failure, could not apply new timeout values: VMIN=%" PRIu8
			              ", VTIME=%" PRIu8 "",
			              vmin, vtime);
			SetLastError(ERROR_IO_DEVICE);
			goto return_false;
		}
	}

	/* wait indefinitely if pTmaxTimeout is NULL */

	if (pTmaxTimeout != NULL)
	{
		ZeroMemory(pTmaxTimeout, sizeof(struct timeval));

		if (Tmax > 0) /* return immdiately if Tmax == 0 */
		{
			pTmaxTimeout->tv_sec = Tmax / 1000;           /* s */
			pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000; /* us */
		}
	}

	/* FIXME: had expected eventfd_write() to return EAGAIN when
	 * there is no eventfd_read() but this not the case. */
	/* discard a possible and no more relevant event */
	eventfd_read(pComm->fd_read_event, NULL);
	biggestFd = pComm->fd_read;

	if (pComm->fd_read_event > biggestFd)
		biggestFd = pComm->fd_read_event;

	FD_ZERO(&read_set);
	WINPR_ASSERT(pComm->fd_read_event < FD_SETSIZE);
	WINPR_ASSERT(pComm->fd_read < FD_SETSIZE);
	FD_SET(pComm->fd_read_event, &read_set);
	FD_SET(pComm->fd_read, &read_set);
	nbFds = select(biggestFd + 1, &read_set, NULL, NULL, pTmaxTimeout);

	if (nbFds < 0)
	{
		char ebuffer[256] = { 0 };
		CommLog_Print(WLOG_WARN, "select() failure, errno=[%d] %s\n", errno,
		              winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
		SetLastError(ERROR_IO_DEVICE);
		goto return_false;
	}

	if (nbFds == 0)
	{
		/* timeout */
		SetLastError(ERROR_TIMEOUT);
		goto return_false;
	}

	/* read_set */

	if (FD_ISSET(pComm->fd_read_event, &read_set))
	{
		eventfd_t event = 0;

		if (eventfd_read(pComm->fd_read_event, &event) < 0)
		{
			if (errno == EAGAIN)
			{
				WINPR_ASSERT(FALSE); /* not quite sure this should ever happen */
				                     /* keep on */
			}
			else
			{
				char ebuffer[256] = { 0 };
				CommLog_Print(WLOG_WARN,
				              "unexpected error on reading fd_read_event, errno=[%d] %s\n", errno,
				              winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
				/* FIXME: goto return_false ? */
			}

			WINPR_ASSERT(errno == EAGAIN);
		}

		if (event == WINPR_PURGE_RXABORT)
		{
			SetLastError(ERROR_CANCELLED);
			goto return_false;
		}

		WINPR_ASSERT(event == WINPR_PURGE_RXABORT); /* no other expected event so far */
	}

	if (FD_ISSET(pComm->fd_read, &read_set))
	{
		ssize_t nbRead = 0;
		nbRead = read(pComm->fd_read, lpBuffer, nNumberOfBytesToRead);

		if (nbRead < 0)
		{
			char ebuffer[256] = { 0 };
			CommLog_Print(WLOG_WARN,
			              "CommReadFile failed, ReadIntervalTimeout=%" PRIu32
			              ", ReadTotalTimeoutMultiplier=%" PRIu32
			              ", ReadTotalTimeoutConstant=%" PRIu32 " VMIN=%u, VTIME=%u",
			              pTimeouts->ReadIntervalTimeout, pTimeouts->ReadTotalTimeoutMultiplier,
			              pTimeouts->ReadTotalTimeoutConstant, currentTermios.c_cc[VMIN],
			              currentTermios.c_cc[VTIME]);
			CommLog_Print(
			    WLOG_WARN, "CommReadFile failed, nNumberOfBytesToRead=%" PRIu32 ", errno=[%d] %s",
			    nNumberOfBytesToRead, errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));

			if (errno == EAGAIN)
			{
				/* keep on */
				goto return_true; /* expect a read-loop to be implemented on the server side */
			}
			else if (errno == EBADF)
			{
				SetLastError(ERROR_BAD_DEVICE); /* STATUS_INVALID_DEVICE_REQUEST */
				goto return_false;
			}
			else
			{
				WINPR_ASSERT(FALSE);
				SetLastError(ERROR_IO_DEVICE);
				goto return_false;
			}
		}

		if (nbRead == 0)
		{
			/* termios timeout */
			SetLastError(ERROR_TIMEOUT);
			goto return_false;
		}

		*lpNumberOfBytesRead = nbRead;

		EnterCriticalSection(&pComm->EventsLock);
		if (pComm->PendingEvents & SERIAL_EV_WINPR_WAITING)
		{
			if (pComm->eventChar != '\0' && memchr(lpBuffer, pComm->eventChar, nbRead))
				pComm->PendingEvents |= SERIAL_EV_RXCHAR;
		}
		LeaveCriticalSection(&pComm->EventsLock);
		goto return_true;
	}

	WINPR_ASSERT(FALSE);
	*lpNumberOfBytesRead = 0;
return_false:
	LeaveCriticalSection(&pComm->ReadLock);
	return FALSE;
return_true:
	LeaveCriticalSection(&pComm->ReadLock);
	return TRUE;
}

/**
 * ERRORS:
 *   ERROR_INVALID_HANDLE
 *   ERROR_NOT_SUPPORTED
 *   ERROR_INVALID_PARAMETER
 *   ERROR_BAD_DEVICE
 */
BOOL CommWriteFile(HANDLE hDevice, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
                   LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
{
	WINPR_COMM* pComm = (WINPR_COMM*)hDevice;
	struct timeval tmaxTimeout;
	struct timeval* pTmaxTimeout = NULL;
	EnterCriticalSection(&pComm->WriteLock); /* KISSer by the function's beginning */

	if (!CommIsHandled(hDevice))
		goto return_false;

	if (lpOverlapped != NULL)
	{
		SetLastError(ERROR_NOT_SUPPORTED);
		goto return_false;
	}

	if (lpNumberOfBytesWritten == NULL)
	{
		SetLastError(ERROR_INVALID_PARAMETER); /* since we doesn't suppport lpOverlapped != NULL */
		goto return_false;
	}

	*lpNumberOfBytesWritten = 0; /* will be ajusted if required ... */

	if (nNumberOfBytesToWrite <= 0)
	{
		goto return_true; /* FIXME: or FALSE? */
	}

	/* FIXME: had expected eventfd_write() to return EAGAIN when
	 * there is no eventfd_read() but this not the case. */
	/* discard a possible and no more relevant event */
	eventfd_read(pComm->fd_write_event, NULL);
	/* ms */
	ULONGLONG Tmax = nNumberOfBytesToWrite * pComm->timeouts.WriteTotalTimeoutMultiplier +
	                 pComm->timeouts.WriteTotalTimeoutConstant;
	/* NB: select() may update the timeout argument to indicate
	 * how much time was left. Keep the timeout variable out of
	 * the while() */
	pTmaxTimeout = &tmaxTimeout;
	ZeroMemory(pTmaxTimeout, sizeof(struct timeval));

	if (Tmax > 0)
	{
		pTmaxTimeout->tv_sec = Tmax / 1000;           /* s */
		pTmaxTimeout->tv_usec = (Tmax % 1000) * 1000; /* us */
	}
	else if ((pComm->timeouts.WriteTotalTimeoutMultiplier == 0) &&
	         (pComm->timeouts.WriteTotalTimeoutConstant == 0))
	{
		pTmaxTimeout = NULL;
	}

	/* else return immdiately */

	while (*lpNumberOfBytesWritten < nNumberOfBytesToWrite)
	{
		int biggestFd = -1;
		fd_set event_set;
		fd_set write_set;
		int nbFds = 0;
		biggestFd = pComm->fd_write;

		if (pComm->fd_write_event > biggestFd)
			biggestFd = pComm->fd_write_event;

		FD_ZERO(&event_set);
		FD_ZERO(&write_set);
		WINPR_ASSERT(pComm->fd_write_event < FD_SETSIZE);
		WINPR_ASSERT(pComm->fd_write < FD_SETSIZE);
		FD_SET(pComm->fd_write_event, &event_set);
		FD_SET(pComm->fd_write, &write_set);
		nbFds = select(biggestFd + 1, &event_set, &write_set, NULL, pTmaxTimeout);

		if (nbFds < 0)
		{
			char ebuffer[256] = { 0 };
			CommLog_Print(WLOG_WARN, "select() failure, errno=[%d] %s\n", errno,
			              winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
			SetLastError(ERROR_IO_DEVICE);
			goto return_false;
		}

		if (nbFds == 0)
		{
			/* timeout */
			SetLastError(ERROR_TIMEOUT);
			goto return_false;
		}

		/* event_set */

		if (FD_ISSET(pComm->fd_write_event, &event_set))
		{
			eventfd_t event = 0;

			if (eventfd_read(pComm->fd_write_event, &event) < 0)
			{
				if (errno == EAGAIN)
				{
					WINPR_ASSERT(FALSE); /* not quite sure this should ever happen */
					                     /* keep on */
				}
				else
				{
					char ebuffer[256] = { 0 };
					CommLog_Print(WLOG_WARN,
					              "unexpected error on reading fd_write_event, errno=[%d] %s\n",
					              errno, winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
					/* FIXME: goto return_false ? */
				}

				WINPR_ASSERT(errno == EAGAIN);
			}

			if (event == WINPR_PURGE_TXABORT)
			{
				SetLastError(ERROR_CANCELLED);
				goto return_false;
			}

			WINPR_ASSERT(event == WINPR_PURGE_TXABORT); /* no other expected event so far */
		}

		/* write_set */

		if (FD_ISSET(pComm->fd_write, &write_set))
		{
			ssize_t nbWritten = 0;
			nbWritten = write(pComm->fd_write, ((const BYTE*)lpBuffer) + (*lpNumberOfBytesWritten),
			                  nNumberOfBytesToWrite - (*lpNumberOfBytesWritten));

			if (nbWritten < 0)
			{
				char ebuffer[256] = { 0 };
				CommLog_Print(WLOG_WARN,
				              "CommWriteFile failed after %" PRIu32
				              " bytes written, errno=[%d] %s\n",
				              *lpNumberOfBytesWritten, errno,
				              winpr_strerror(errno, ebuffer, sizeof(ebuffer)));

				if (errno == EAGAIN)
				{
					/* keep on */
					continue;
				}
				else if (errno == EBADF)
				{
					SetLastError(ERROR_BAD_DEVICE); /* STATUS_INVALID_DEVICE_REQUEST */
					goto return_false;
				}
				else
				{
					WINPR_ASSERT(FALSE);
					SetLastError(ERROR_IO_DEVICE);
					goto return_false;
				}
			}

			*lpNumberOfBytesWritten += nbWritten;
		}
	} /* while */

	/* FIXME: this call to tcdrain() doesn't look correct and
	 * might hide a bug but was required while testing a serial
	 * printer. Its driver was expecting the modem line status
	 * SERIAL_MSR_DSR true after the sending which was never
	 * happenning otherwise. A purge was also done before each
	 * Write operation. The serial port was opened with:
	 * DesiredAccess=0x0012019F. The printer worked fine with
	 * mstsc. */
	tcdrain(pComm->fd_write);

return_true:
	LeaveCriticalSection(&pComm->WriteLock);
	return TRUE;

return_false:
	LeaveCriticalSection(&pComm->WriteLock);
	return FALSE;
}

#endif /* __linux__ */