| 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 |
#include <winpr/comm.h> |
| 24 |
#include <winpr/crt.h> |
| 25 |
|
| 26 |
#include "../comm.h" |
| 27 |
|
| 28 |
static BOOL test_generic(HANDLE hComm) |
| 29 |
{ |
| 30 |
DCB dcb = WINPR_C_ARRAY_INIT; |
| 31 |
DCB* pDcb = nullptr; |
| 32 |
BOOL result = 0; |
| 33 |
|
| 34 |
ZeroMemory(&dcb, sizeof(DCB)); |
| 35 |
result = GetCommState(hComm, &dcb); |
| 36 |
if (result) |
| 37 |
{ |
| 38 |
printf("GetCommState failure, should have returned false because dcb.DCBlength has been " |
| 39 |
"let uninitialized\n"); |
| 40 |
return FALSE; |
| 41 |
} |
| 42 |
|
| 43 |
ZeroMemory(&dcb, sizeof(DCB)); |
| 44 |
dcb.DCBlength = sizeof(DCB) / 2; /* improper value */ |
| 45 |
result = GetCommState(hComm, &dcb); |
| 46 |
if (result) |
| 47 |
{ |
| 48 |
printf("GetCommState failure, should have return false because dcb.DCBlength was not " |
| 49 |
"correctly initialized\n"); |
| 50 |
return FALSE; |
| 51 |
} |
| 52 |
|
| 53 |
ZeroMemory(&dcb, sizeof(DCB)); |
| 54 |
dcb.DCBlength = sizeof(DCB); |
| 55 |
result = GetCommState(hComm, &dcb); |
| 56 |
if (!result) |
| 57 |
{ |
| 58 |
printf("GetCommState failure: Ox%x, with adjusted DCBlength\n", GetLastError()); |
| 59 |
return FALSE; |
| 60 |
} |
| 61 |
|
| 62 |
pDcb = (DCB*)calloc(2, sizeof(DCB)); |
| 63 |
if (!pDcb) |
| 64 |
return FALSE; |
| 65 |
pDcb->DCBlength = sizeof(DCB) * 2; |
| 66 |
result = GetCommState(hComm, pDcb); |
| 67 |
result = result && (pDcb->DCBlength == sizeof(DCB) * 2); |
| 68 |
free(pDcb); |
| 69 |
if (!result) |
| 70 |
{ |
| 71 |
printf("GetCommState failure: 0x%x, with bigger DCBlength\n", GetLastError()); |
| 72 |
return FALSE; |
| 73 |
} |
| 74 |
|
| 75 |
return TRUE; |
| 76 |
} |
| 77 |
|
| 78 |
int TestGetCommState(int argc, char* argv[]) |
| 79 |
{ |
| 80 |
struct stat statbuf = WINPR_C_ARRAY_INIT; |
| 81 |
BOOL result = 0; |
| 82 |
HANDLE hComm = nullptr; |
| 83 |
|
| 84 |
if (stat("/dev/ttyS0", &statbuf) < 0) |
| 85 |
{ |
| 86 |
(void)fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n"); |
| 87 |
return EXIT_SUCCESS; |
| 88 |
} |
| 89 |
|
| 90 |
result = DefineCommDevice("COM1", "/dev/ttyS0"); |
| 91 |
if (!result) |
| 92 |
{ |
| 93 |
printf("DefineCommDevice failure: 0x%x\n", GetLastError()); |
| 94 |
return EXIT_FAILURE; |
| 95 |
} |
| 96 |
|
| 97 |
hComm = |
| 98 |
CreateFileA("COM1", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr); |
| 99 |
|
| 100 |
if (hComm == INVALID_HANDLE_VALUE) |
| 101 |
{ |
| 102 |
printf("CreateFileA failure: 0x%x\n", GetLastError()); |
| 103 |
return EXIT_FAILURE; |
| 104 |
} |
| 105 |
|
| 106 |
if (!test_generic(hComm)) |
| 107 |
{ |
| 108 |
printf("test_generic failure (SerialDriverUnknown)\n"); |
| 109 |
return EXIT_FAILURE; |
| 110 |
} |
| 111 |
|
| 112 |
_comm_setServerSerialDriver(hComm, SerialDriverSerialSys); |
| 113 |
if (!test_generic(hComm)) |
| 114 |
{ |
| 115 |
printf("test_generic failure (SerialDriverSerialSys)\n"); |
| 116 |
return EXIT_FAILURE; |
| 117 |
} |
| 118 |
|
| 119 |
_comm_setServerSerialDriver(hComm, SerialDriverSerCxSys); |
| 120 |
if (!test_generic(hComm)) |
| 121 |
{ |
| 122 |
printf("test_generic failure (SerialDriverSerCxSys)\n"); |
| 123 |
return EXIT_FAILURE; |
| 124 |
} |
| 125 |
|
| 126 |
_comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys); |
| 127 |
if (!test_generic(hComm)) |
| 128 |
{ |
| 129 |
printf("test_generic failure (SerialDriverSerCx2Sys)\n"); |
| 130 |
return EXIT_FAILURE; |
| 131 |
} |
| 132 |
|
| 133 |
if (!CloseHandle(hComm)) |
| 134 |
{ |
| 135 |
(void)fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError()); |
| 136 |
return EXIT_FAILURE; |
| 137 |
} |
| 138 |
|
| 139 |
return EXIT_SUCCESS; |
| 140 |
} |