| 1 |
/** |
| 2 |
* WinPR: Windows Portable Runtime |
| 3 |
* Serial Communication API |
| 4 |
* |
| 5 |
* Copyright 2014 Marc-Andre Moreau <[email protected]> |
| 6 |
* Copyright 2014 Hewlett-Packard Development Company, L.P. |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, software |
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
* See the License for the specific language governing permissions and |
| 18 |
* limitations under the License. |
| 19 |
*/ |
| 20 |
|
| 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 = WINPR_C_ARRAY_INIT; |
| 32 |
BOOL success = FALSE; |
| 33 |
LPCSTR lpFileName = "\\\\.\\COM1"; |
| 34 |
COMMPROP commProp = WINPR_C_ARRAY_INIT; |
| 35 |
struct stat statbuf = WINPR_C_ARRAY_INIT; |
| 36 |
|
| 37 |
HANDLE hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, |
| 38 |
0, nullptr); |
| 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 parameter */ |
| 63 |
nullptr, CREATE_NEW, /* invalid parameter */ |
| 64 |
0, (HANDLE)1234); /* invalid parameter */ |
| 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, nullptr, OPEN_EXISTING, 0, |
| 74 |
nullptr); |
| 75 |
|
| 76 |
if (!hComm || (hComm == INVALID_HANDLE_VALUE)) |
| 77 |
{ |
| 78 |
(void)fprintf(stderr, "CreateFileA failure: %s GetLastError() = 0x%08x\n", lpFileName, |
| 79 |
GetLastError()); |
| 80 |
return EXIT_FAILURE; |
| 81 |
} |
| 82 |
|
| 83 |
/* TODO: a second call to CreateFileA should failed and |
| 84 |
* GetLastError should return ERROR_SHARING_VIOLATION */ |
| 85 |
|
| 86 |
dcb.DCBlength = sizeof(DCB); |
| 87 |
success = GetCommState(hComm, &dcb); |
| 88 |
if (!success) |
| 89 |
{ |
| 90 |
(void)fprintf(stderr, "GetCommState failure: GetLastError() = Ox%x\n", GetLastError()); |
| 91 |
return EXIT_FAILURE; |
| 92 |
} |
| 93 |
|
| 94 |
(void)fprintf(stderr, |
| 95 |
"BaudRate: %" PRIu32 " ByteSize: %" PRIu8 " Parity: %" PRIu8 " StopBits: %" PRIu8 |
| 96 |
"\n", |
| 97 |
dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits); |
| 98 |
|
| 99 |
if (!GetCommProperties(hComm, &commProp)) |
| 100 |
{ |
| 101 |
(void)fprintf(stderr, "GetCommProperties failure: GetLastError(): 0x%08x\n", |
| 102 |
GetLastError()); |
| 103 |
return EXIT_FAILURE; |
| 104 |
} |
| 105 |
|
| 106 |
if ((commProp.dwSettableBaud & BAUD_57600) <= 0) |
| 107 |
{ |
| 108 |
(void)fprintf(stderr, "BAUD_57600 unsupported!\n"); |
| 109 |
return EXIT_FAILURE; |
| 110 |
} |
| 111 |
|
| 112 |
if ((commProp.dwSettableBaud & BAUD_14400) > 0) |
| 113 |
{ |
| 114 |
(void)fprintf(stderr, "BAUD_14400 supported!\n"); |
| 115 |
return EXIT_FAILURE; |
| 116 |
} |
| 117 |
|
| 118 |
dcb.BaudRate = CBR_57600; |
| 119 |
dcb.ByteSize = 8; |
| 120 |
dcb.Parity = NOPARITY; |
| 121 |
dcb.StopBits = ONESTOPBIT; |
| 122 |
|
| 123 |
success = SetCommState(hComm, &dcb); |
| 124 |
|
| 125 |
if (!success) |
| 126 |
{ |
| 127 |
(void)fprintf(stderr, "SetCommState failure: GetLastError() = 0x%x\n", GetLastError()); |
| 128 |
return EXIT_FAILURE; |
| 129 |
} |
| 130 |
|
| 131 |
success = GetCommState(hComm, &dcb); |
| 132 |
|
| 133 |
if (!success) |
| 134 |
{ |
| 135 |
(void)fprintf(stderr, "GetCommState failure: GetLastError() = 0x%x\n", GetLastError()); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
if ((dcb.BaudRate != CBR_57600) || (dcb.ByteSize != 8) || (dcb.Parity != NOPARITY) || |
| 140 |
(dcb.StopBits != ONESTOPBIT)) |
| 141 |
{ |
| 142 |
(void)fprintf(stderr, |
| 143 |
"Got an unexpected value among: BaudRate: %" PRIu32 " ByteSize: %" PRIu8 |
| 144 |
" Parity: %" PRIu8 " StopBits: %" PRIu8 "\n", |
| 145 |
dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits); |
| 146 |
} |
| 147 |
|
| 148 |
(void)CloseHandle(hComm); |
| 149 |
|
| 150 |
return 0; |
| 151 |
} |