2014-04-22 19:04:07 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Serial Communication API
|
|
|
|
*
|
|
|
|
* Copyright 2014 Hewlett-Packard Development Company, L.P.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-07-03 21:01:45 +04:00
|
|
|
#include <sys/stat.h>
|
2014-04-22 19:04:07 +04:00
|
|
|
|
|
|
|
#include <winpr/comm.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2014-04-29 00:32:27 +04:00
|
|
|
#include "../comm.h"
|
|
|
|
|
|
|
|
static BOOL test_generic(HANDLE hComm)
|
2014-04-22 19:04:07 +04:00
|
|
|
{
|
|
|
|
DCB dcb, *pDcb;
|
|
|
|
BOOL result;
|
|
|
|
|
2014-04-29 06:04:09 +04:00
|
|
|
ZeroMemory(&dcb, sizeof(DCB));
|
2014-04-22 19:04:07 +04:00
|
|
|
result = GetCommState(hComm, &dcb);
|
|
|
|
if (result)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("GetCommState failure, should have returned false because dcb.DCBlength has been "
|
|
|
|
"let uninitialized\n");
|
2014-04-29 00:32:27 +04:00
|
|
|
return FALSE;
|
2014-04-22 19:04:07 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 06:04:09 +04:00
|
|
|
ZeroMemory(&dcb, sizeof(DCB));
|
2014-04-22 19:04:07 +04:00
|
|
|
dcb.DCBlength = sizeof(DCB) / 2; /* improper value */
|
|
|
|
result = GetCommState(hComm, &dcb);
|
|
|
|
if (result)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("GetCommState failure, should have return false because dcb.DCBlength was not "
|
|
|
|
"correctly initialized\n");
|
2014-04-29 00:32:27 +04:00
|
|
|
return FALSE;
|
2014-04-22 19:04:07 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 06:04:09 +04:00
|
|
|
ZeroMemory(&dcb, sizeof(DCB));
|
2014-04-22 19:04:07 +04:00
|
|
|
dcb.DCBlength = sizeof(DCB);
|
|
|
|
result = GetCommState(hComm, &dcb);
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
printf("GetCommState failure: Ox%x, with adjusted DCBlength\n", GetLastError());
|
2014-04-29 00:32:27 +04:00
|
|
|
return FALSE;
|
2014-04-22 19:04:07 +04:00
|
|
|
}
|
2014-06-16 21:18:45 +04:00
|
|
|
|
2017-05-30 11:46:43 +03:00
|
|
|
pDcb = (DCB*)calloc(2, sizeof(DCB));
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!pDcb)
|
|
|
|
return FALSE;
|
2014-04-22 19:04:07 +04:00
|
|
|
pDcb->DCBlength = sizeof(DCB) * 2;
|
|
|
|
result = GetCommState(hComm, pDcb);
|
|
|
|
result = result && (pDcb->DCBlength == sizeof(DCB) * 2);
|
|
|
|
free(pDcb);
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
printf("GetCommState failure: 0x%x, with bigger DCBlength\n", GetLastError());
|
2014-04-29 00:32:27 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestGetCommState(int argc, char* argv[])
|
|
|
|
{
|
2014-07-03 21:01:45 +04:00
|
|
|
struct stat statbuf;
|
2014-04-29 00:32:27 +04:00
|
|
|
BOOL result;
|
|
|
|
HANDLE hComm;
|
|
|
|
|
2014-07-03 21:01:45 +04:00
|
|
|
if (stat("/dev/ttyS0", &statbuf) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n");
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-04-29 00:32:27 +04:00
|
|
|
result = DefineCommDevice("COM1", "/dev/ttyS0");
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
printf("DefineCommDevice failure: 0x%x\n", GetLastError());
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
hComm = CreateFileA("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
2014-07-17 20:02:47 +04:00
|
|
|
|
2014-04-29 00:32:27 +04:00
|
|
|
if (hComm == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
printf("CreateFileA failure: 0x%x\n", GetLastError());
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!test_generic(hComm))
|
|
|
|
{
|
2014-06-18 17:58:08 +04:00
|
|
|
printf("test_generic failure (SerialDriverUnknown)\n");
|
2014-04-29 00:32:27 +04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-18 17:58:08 +04:00
|
|
|
_comm_setServerSerialDriver(hComm, SerialDriverSerialSys);
|
2014-04-29 00:32:27 +04:00
|
|
|
if (!test_generic(hComm))
|
|
|
|
{
|
2014-06-18 17:58:08 +04:00
|
|
|
printf("test_generic failure (SerialDriverSerialSys)\n");
|
2014-04-29 00:32:27 +04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-18 17:58:08 +04:00
|
|
|
_comm_setServerSerialDriver(hComm, SerialDriverSerCxSys);
|
2014-04-29 00:32:27 +04:00
|
|
|
if (!test_generic(hComm))
|
|
|
|
{
|
2014-06-18 17:58:08 +04:00
|
|
|
printf("test_generic failure (SerialDriverSerCxSys)\n");
|
2014-04-29 00:32:27 +04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-06-18 17:58:08 +04:00
|
|
|
_comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys);
|
2014-04-29 00:32:27 +04:00
|
|
|
if (!test_generic(hComm))
|
|
|
|
{
|
2014-06-18 17:58:08 +04:00
|
|
|
printf("test_generic failure (SerialDriverSerCx2Sys)\n");
|
2014-04-22 19:04:07 +04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-29 06:04:09 +04:00
|
|
|
if (!CloseHandle(hComm))
|
|
|
|
{
|
2014-08-11 11:17:56 +04:00
|
|
|
fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError());
|
2014-04-29 06:04:09 +04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-04-22 19:04:07 +04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|