FreeRDP
TestControlSettings.c
1 
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 = { 0 };
32  BOOL result = 0;
33  HANDLE hComm = NULL;
34  DCB dcb = { 0 };
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, NULL, OPEN_EXISTING, 0, NULL);
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 }