summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/comm/test/TestCommMonitor.c
blob: fe28a86bfbbb3c3a81c0af57913913642343fcd0 (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
#include <winpr/crt.h>
#include <winpr/comm.h>
#include <winpr/file.h>
#include <winpr/synch.h>
#include <winpr/handle.h>

int TestCommMonitor(int argc, char* argv[])
{
	HANDLE hComm;
	DWORD dwError;
	BOOL fSuccess;
	DWORD dwEvtMask;
	OVERLAPPED overlapped = { 0 };
	LPCSTR lpFileName = "\\\\.\\COM1";

	hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
	                    FILE_FLAG_OVERLAPPED, NULL);

	if (!hComm || (hComm == INVALID_HANDLE_VALUE))
	{
		printf("CreateFileA failure: %s\n", lpFileName);
		return -1;
	}

	fSuccess = SetCommMask(hComm, EV_CTS | EV_DSR);

	if (!fSuccess)
	{
		printf("SetCommMask failure: GetLastError() = %" PRIu32 "\n", GetLastError());
		return -1;
	}

	if (!(overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
	{
		printf("CreateEvent failed: GetLastError() = %" PRIu32 "\n", GetLastError());
		return -1;
	}

	if (WaitCommEvent(hComm, &dwEvtMask, &overlapped))
	{
		if (dwEvtMask & EV_DSR)
		{
			printf("EV_DSR\n");
		}

		if (dwEvtMask & EV_CTS)
		{
			printf("EV_CTS\n");
		}
	}
	else
	{
		dwError = GetLastError();

		if (dwError == ERROR_IO_PENDING)
		{
			printf("ERROR_IO_PENDING\n");
		}
		else
		{
			printf("WaitCommEvent failure: GetLastError() = %" PRIu32 "\n", dwError);
			return -1;
		}
	}

	CloseHandle(hComm);

	return 0;
}