| 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 |
#include <sys/stat.h> |
| 22 |
|
| 23 |
#ifndef _WIN32 |
| 24 |
#include <termios.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <winpr/comm.h> |
| 28 |
#include <winpr/crt.h> |
| 29 |
|
| 30 |
#include "../comm.h" |
| 31 |
|
| 32 |
static BOOL test_generic(HANDLE hComm) |
| 33 |
{ |
| 34 |
COMMTIMEOUTS timeouts = WINPR_C_ARRAY_INIT; |
| 35 |
COMMTIMEOUTS timeouts2 = WINPR_C_ARRAY_INIT; |
| 36 |
|
| 37 |
timeouts.ReadIntervalTimeout = 1; |
| 38 |
timeouts.ReadTotalTimeoutMultiplier = 2; |
| 39 |
timeouts.ReadTotalTimeoutConstant = 3; |
| 40 |
timeouts.WriteTotalTimeoutMultiplier = 4; |
| 41 |
timeouts.WriteTotalTimeoutConstant = 5; |
| 42 |
|
| 43 |
if (!SetCommTimeouts(hComm, &timeouts)) |
| 44 |
{ |
| 45 |
(void)fprintf(stderr, "SetCommTimeouts failure, GetLastError: 0x%08x\n", GetLastError()); |
| 46 |
return FALSE; |
| 47 |
} |
| 48 |
|
| 49 |
if (!GetCommTimeouts(hComm, &timeouts2)) |
| 50 |
{ |
| 51 |
(void)fprintf(stderr, "GetCommTimeouts failure, GetLastError: 0x%08x\n", GetLastError()); |
| 52 |
return FALSE; |
| 53 |
} |
| 54 |
|
| 55 |
if (memcmp(&timeouts, &timeouts2, sizeof(COMMTIMEOUTS)) != 0) |
| 56 |
{ |
| 57 |
(void)fprintf(stderr, "TestTimeouts failure, didn't get back the same timeouts.\n"); |
| 58 |
return FALSE; |
| 59 |
} |
| 60 |
|
| 61 |
/* not supported combination */ |
| 62 |
timeouts.ReadIntervalTimeout = MAXULONG; |
| 63 |
timeouts.ReadTotalTimeoutConstant = MAXULONG; |
| 64 |
if (SetCommTimeouts(hComm, &timeouts)) |
| 65 |
{ |
| 66 |
(void)fprintf( |
| 67 |
stderr, |
| 68 |
"SetCommTimeouts succeeded with ReadIntervalTimeout and ReadTotalTimeoutConstant " |
| 69 |
"set to MAXULONG. GetLastError: 0x%08x\n", |
| 70 |
GetLastError()); |
| 71 |
return FALSE; |
| 72 |
} |
| 73 |
|
| 74 |
if (GetLastError() != ERROR_INVALID_PARAMETER) |
| 75 |
{ |
| 76 |
(void)fprintf( |
| 77 |
stderr, |
| 78 |
"SetCommTimeouts failure, expected GetLastError to return ERROR_INVALID_PARAMETER " |
| 79 |
"and got: 0x%08x\n", |
| 80 |
GetLastError()); |
| 81 |
return FALSE; |
| 82 |
} |
| 83 |
|
| 84 |
return TRUE; |
| 85 |
} |
| 86 |
|
| 87 |
int TestTimeouts(int argc, char* argv[]) |
| 88 |
{ |
| 89 |
struct stat statbuf; |
| 90 |
BOOL result = 0; |
| 91 |
HANDLE hComm = nullptr; |
| 92 |
|
| 93 |
if (stat("/dev/ttyS0", &statbuf) < 0) |
| 94 |
{ |
| 95 |
(void)fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n"); |
| 96 |
return EXIT_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
result = DefineCommDevice("COM1", "/dev/ttyS0"); |
| 100 |
if (!result) |
| 101 |
{ |
| 102 |
(void)fprintf(stderr, "DefineCommDevice failure: 0x%x\n", GetLastError()); |
| 103 |
return EXIT_FAILURE; |
| 104 |
} |
| 105 |
|
| 106 |
hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); |
| 107 |
if (hComm == INVALID_HANDLE_VALUE) |
| 108 |
{ |
| 109 |
(void)fprintf(stderr, "CreateFileA failure: 0x%x\n", GetLastError()); |
| 110 |
return EXIT_FAILURE; |
| 111 |
} |
| 112 |
|
| 113 |
_comm_setServerSerialDriver(hComm, SerialDriverSerialSys); |
| 114 |
if (!test_generic(hComm)) |
| 115 |
{ |
| 116 |
(void)fprintf(stderr, "test_SerialSys failure\n"); |
| 117 |
return EXIT_FAILURE; |
| 118 |
} |
| 119 |
|
| 120 |
_comm_setServerSerialDriver(hComm, SerialDriverSerCxSys); |
| 121 |
if (!test_generic(hComm)) |
| 122 |
{ |
| 123 |
(void)fprintf(stderr, "test_SerCxSys failure\n"); |
| 124 |
return EXIT_FAILURE; |
| 125 |
} |
| 126 |
|
| 127 |
_comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys); |
| 128 |
if (!test_generic(hComm)) |
| 129 |
{ |
| 130 |
(void)fprintf(stderr, "test_SerCx2Sys failure\n"); |
| 131 |
return EXIT_FAILURE; |
| 132 |
} |
| 133 |
|
| 134 |
if (!CloseHandle(hComm)) |
| 135 |
{ |
| 136 |
(void)fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError()); |
| 137 |
return EXIT_FAILURE; |
| 138 |
} |
| 139 |
|
| 140 |
return EXIT_SUCCESS; |
| 141 |
} |