FreeRDP
TestCommConfig.c
1 
21 #include <sys/stat.h>
22 
23 #include <winpr/crt.h>
24 #include <winpr/comm.h>
25 #include <winpr/file.h>
26 #include <winpr/synch.h>
27 #include <winpr/handle.h>
28 
29 int TestCommConfig(int argc, char* argv[])
30 {
31  DCB dcb = { 0 };
32  BOOL success = FALSE;
33  LPCSTR lpFileName = "\\\\.\\COM1";
34  COMMPROP commProp = { 0 };
35  struct stat statbuf = { 0 };
36 
37  HANDLE hComm =
38  CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
39 
40  if (hComm && (hComm != INVALID_HANDLE_VALUE))
41  {
42  (void)fprintf(
43  stderr, "CreateFileA failure: could create a handle on a not yet defined device: %s\n",
44  lpFileName);
45  return EXIT_FAILURE;
46  }
47 
48  if (stat("/dev/ttyS0", &statbuf) < 0)
49  {
50  (void)fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n");
51  return EXIT_SUCCESS;
52  }
53 
54  success = DefineCommDevice(lpFileName, "/dev/ttyS0");
55  if (!success)
56  {
57  (void)fprintf(stderr, "DefineCommDevice failure: %s\n", lpFileName);
58  return EXIT_FAILURE;
59  }
60 
61  hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE,
62  FILE_SHARE_WRITE, /* invalid parmaeter */
63  NULL, CREATE_NEW, /* invalid parameter */
64  0, (HANDLE)1234); /* invalid parmaeter */
65  if (hComm != INVALID_HANDLE_VALUE)
66  {
67  (void)fprintf(
68  stderr, "CreateFileA failure: could create a handle with some invalid parameters %s\n",
69  lpFileName);
70  return EXIT_FAILURE;
71  }
72 
73  hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
74 
75  if (!hComm || (hComm == INVALID_HANDLE_VALUE))
76  {
77  (void)fprintf(stderr, "CreateFileA failure: %s GetLastError() = 0x%08x\n", lpFileName,
78  GetLastError());
79  return EXIT_FAILURE;
80  }
81 
82  /* TODO: a second call to CreateFileA should failed and
83  * GetLastError should return ERROR_SHARING_VIOLATION */
84 
85  dcb.DCBlength = sizeof(DCB);
86  success = GetCommState(hComm, &dcb);
87  if (!success)
88  {
89  (void)fprintf(stderr, "GetCommState failure: GetLastError() = Ox%x\n", GetLastError());
90  return EXIT_FAILURE;
91  }
92 
93  (void)fprintf(stderr,
94  "BaudRate: %" PRIu32 " ByteSize: %" PRIu8 " Parity: %" PRIu8 " StopBits: %" PRIu8
95  "\n",
96  dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
97 
98  if (!GetCommProperties(hComm, &commProp))
99  {
100  (void)fprintf(stderr, "GetCommProperties failure: GetLastError(): 0x%08x\n",
101  GetLastError());
102  return EXIT_FAILURE;
103  }
104 
105  if ((commProp.dwSettableBaud & BAUD_57600) <= 0)
106  {
107  (void)fprintf(stderr, "BAUD_57600 unsupported!\n");
108  return EXIT_FAILURE;
109  }
110 
111  if ((commProp.dwSettableBaud & BAUD_14400) > 0)
112  {
113  (void)fprintf(stderr, "BAUD_14400 supported!\n");
114  return EXIT_FAILURE;
115  }
116 
117  dcb.BaudRate = CBR_57600;
118  dcb.ByteSize = 8;
119  dcb.Parity = NOPARITY;
120  dcb.StopBits = ONESTOPBIT;
121 
122  success = SetCommState(hComm, &dcb);
123 
124  if (!success)
125  {
126  (void)fprintf(stderr, "SetCommState failure: GetLastError() = 0x%x\n", GetLastError());
127  return EXIT_FAILURE;
128  }
129 
130  success = GetCommState(hComm, &dcb);
131 
132  if (!success)
133  {
134  (void)fprintf(stderr, "GetCommState failure: GetLastError() = 0x%x\n", GetLastError());
135  return 0;
136  }
137 
138  if ((dcb.BaudRate != CBR_57600) || (dcb.ByteSize != 8) || (dcb.Parity != NOPARITY) ||
139  (dcb.StopBits != ONESTOPBIT))
140  {
141  (void)fprintf(stderr,
142  "Got an unexpeted value among: BaudRate: %" PRIu32 " ByteSize: %" PRIu8
143  " Parity: %" PRIu8 " StopBits: %" PRIu8 "\n",
144  dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
145  }
146 
147  (void)CloseHandle(hComm);
148 
149  return 0;
150 }