|
Packit |
1fb8d4 |
/**
|
|
Packit |
1fb8d4 |
* WinPR: Windows Portable Runtime
|
|
Packit |
1fb8d4 |
* Serial Communication API
|
|
Packit |
1fb8d4 |
*
|
|
Packit |
1fb8d4 |
* Copyright 2014 Hewlett-Packard Development Company, L.P.
|
|
Packit |
1fb8d4 |
*
|
|
Packit |
1fb8d4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
Packit |
1fb8d4 |
* you may not use this file except in compliance with the License.
|
|
Packit |
1fb8d4 |
* You may obtain a copy of the License at
|
|
Packit |
1fb8d4 |
*
|
|
Packit |
1fb8d4 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
1fb8d4 |
*
|
|
Packit |
1fb8d4 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
1fb8d4 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
1fb8d4 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
1fb8d4 |
* See the License for the specific language governing permissions and
|
|
Packit |
1fb8d4 |
* limitations under the License.
|
|
Packit |
1fb8d4 |
*/
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
#include <stdio.h>
|
|
Packit |
1fb8d4 |
#include <sys/stat.h>
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
#ifndef _WIN32
|
|
Packit |
1fb8d4 |
#include <termios.h>
|
|
Packit |
1fb8d4 |
#endif
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
#include <winpr/comm.h>
|
|
Packit |
1fb8d4 |
#include <winpr/crt.h>
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
#include "../comm.h"
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
static BOOL test_generic(HANDLE hComm)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
COMMTIMEOUTS timeouts, timeouts2;
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
timeouts.ReadIntervalTimeout = 1;
|
|
Packit |
1fb8d4 |
timeouts.ReadTotalTimeoutMultiplier = 2;
|
|
Packit |
1fb8d4 |
timeouts.ReadTotalTimeoutConstant = 3;
|
|
Packit |
1fb8d4 |
timeouts.WriteTotalTimeoutMultiplier = 4;
|
|
Packit |
1fb8d4 |
timeouts.WriteTotalTimeoutConstant = 5;
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (!SetCommTimeouts(hComm, &timeouts))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "SetCommTimeouts failure, GetLastError: 0x%08x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
ZeroMemory(&timeouts2, sizeof(COMMTIMEOUTS));
|
|
Packit |
1fb8d4 |
if (!GetCommTimeouts(hComm, &timeouts2))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "GetCommTimeouts failure, GetLastError: 0x%08x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (memcmp(&timeouts, &timeouts2, sizeof(COMMTIMEOUTS)) != 0)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "TestTimeouts failure, didn't get back the same timeouts.\n");
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
/* not supported combination */
|
|
Packit |
1fb8d4 |
timeouts.ReadIntervalTimeout = MAXULONG;
|
|
Packit |
1fb8d4 |
timeouts.ReadTotalTimeoutConstant = MAXULONG;
|
|
Packit |
1fb8d4 |
if (SetCommTimeouts(hComm, &timeouts))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "SetCommTimeouts succeeded with ReadIntervalTimeout and ReadTotalTimeoutConstant set to MAXULONG. GetLastError: 0x%08x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (GetLastError() != ERROR_INVALID_PARAMETER)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "SetCommTimeouts failure, expected GetLastError to return ERROR_INVALID_PARAMETER and got: 0x%08x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
return TRUE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
int TestTimeouts(int argc, char* argv[])
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
struct stat statbuf;
|
|
Packit |
1fb8d4 |
BOOL result;
|
|
Packit |
1fb8d4 |
HANDLE hComm;
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (stat("/dev/ttyS0", &statbuf) < 0)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n");
|
|
Packit |
1fb8d4 |
return EXIT_SUCCESS;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
result = DefineCommDevice("COM1", "/dev/ttyS0");
|
|
Packit |
1fb8d4 |
if (!result)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "DefineCommDevice failure: 0x%x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
hComm = CreateFile("COM1",
|
|
Packit |
1fb8d4 |
GENERIC_READ | GENERIC_WRITE,
|
|
Packit |
1fb8d4 |
0, NULL, OPEN_EXISTING, 0, NULL);
|
|
Packit |
1fb8d4 |
if (hComm == INVALID_HANDLE_VALUE)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "CreateFileA failure: 0x%x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
_comm_setServerSerialDriver(hComm, SerialDriverSerialSys);
|
|
Packit |
1fb8d4 |
if (!test_generic(hComm))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "test_SerialSys failure\n");
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
_comm_setServerSerialDriver(hComm, SerialDriverSerCxSys);
|
|
Packit |
1fb8d4 |
if (!test_generic(hComm))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "test_SerCxSys failure\n");
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
_comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys);
|
|
Packit |
1fb8d4 |
if (!test_generic(hComm))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "test_SerCx2Sys failure\n");
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (!CloseHandle(hComm))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
return EXIT_SUCCESS;
|
|
Packit |
1fb8d4 |
}
|