Debian Codegraph @

winpr/libwinpr/comm/test/TestControlSettings.c

raw
1 /**
2 * WinPR: Windows Portable Runtime
3 * Serial Communication API
4 *
5 * Copyright 2014 Hewlett-Packard Development Company, L.P.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #include <stdio.h>
21
22 #include <sys/stat.h>
23
24 #include <winpr/comm.h>
25 #include <winpr/crt.h>
26
27 #include "../comm.h"
28
29 int TestControlSettings(int argc, char* argv[])
30 {
31 struct stat statbuf = WINPR_C_ARRAY_INIT;
32 BOOL result = 0;
33 HANDLE hComm = nullptr;
34 DCB dcb = WINPR_C_ARRAY_INIT;
35
36 if (stat("/dev/ttyS0", &statbuf) < 0)
37 {
38 (void)fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n");
39 return EXIT_SUCCESS;
40 }
41
42 result = DefineCommDevice("COM1", "/dev/ttyS0");
43 if (!result)
44 {
45 (void)fprintf(stderr, "DefineCommDevice failure: 0x%x\n", GetLastError());
46 return EXIT_FAILURE;
47 }
48
49 hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
50 if (hComm == INVALID_HANDLE_VALUE)
51 {
52 (void)fprintf(stderr, "CreateFileA failure: 0x%x\n", GetLastError());
53 return EXIT_FAILURE;
54 }
55
56 ZeroMemory(&dcb, sizeof(DCB));
57 dcb.DCBlength = sizeof(DCB);
58 if (!GetCommState(hComm, &dcb))
59 {
60 (void)fprintf(stderr, "GetCommState failure; GetLastError(): %08x\n", GetLastError());
61 return FALSE;
62 }
63
64 /* Test 1 */
65
66 dcb.ByteSize = 5;
67 dcb.StopBits = ONESTOPBIT;
68 dcb.Parity = MARKPARITY;
69
70 if (!SetCommState(hComm, &dcb))
71 {
72 (void)fprintf(stderr, "SetCommState failure; GetLastError(): %08x\n", GetLastError());
73 return FALSE;
74 }
75
76 ZeroMemory(&dcb, sizeof(DCB));
77 dcb.DCBlength = sizeof(DCB);
78 if (!GetCommState(hComm, &dcb))
79 {
80 (void)fprintf(stderr, "GetCommState failure; GetLastError(): %08x\n", GetLastError());
81 return FALSE;
82 }
83
84 if ((dcb.ByteSize != 5) || (dcb.StopBits != ONESTOPBIT) || (dcb.Parity != MARKPARITY))
85 {
86 (void)fprintf(stderr, "test1 failed.\n");
87 return FALSE;
88 }
89
90 /* Test 2 */
91
92 dcb.ByteSize = 8;
93 dcb.StopBits = ONESTOPBIT;
94 dcb.Parity = NOPARITY;
95
96 if (!SetCommState(hComm, &dcb))
97 {
98 (void)fprintf(stderr, "SetCommState failure; GetLastError(): %08x\n", GetLastError());
99 return FALSE;
100 }
101
102 ZeroMemory(&dcb, sizeof(DCB));
103 dcb.DCBlength = sizeof(DCB);
104 if (!GetCommState(hComm, &dcb))
105 {
106 (void)fprintf(stderr, "GetCommState failure; GetLastError(): %08x\n", GetLastError());
107 return FALSE;
108 }
109
110 if ((dcb.ByteSize != 8) || (dcb.StopBits != ONESTOPBIT) || (dcb.Parity != NOPARITY))
111 {
112 (void)fprintf(stderr, "test2 failed.\n");
113 return FALSE;
114 }
115
116 if (!CloseHandle(hComm))
117 {
118 (void)fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError());
119 return EXIT_FAILURE;
120 }
121
122 return EXIT_SUCCESS;
123 }