|
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 |
#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 |
DCB dcb, *pDcb;
|
|
Packit |
1fb8d4 |
BOOL result;
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
ZeroMemory(&dcb, sizeof(DCB));
|
|
Packit |
1fb8d4 |
result = GetCommState(hComm, &dcb;;
|
|
Packit |
1fb8d4 |
if (result)
|
|
Packit |
1fb8d4 |
{
|
|
Packit Service |
5a9772 |
printf("GetCommState failure, should have returned false because dcb.DCBlength has been "
|
|
Packit Service |
5a9772 |
"let uninitialized\n");
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
ZeroMemory(&dcb, sizeof(DCB));
|
|
Packit |
1fb8d4 |
dcb.DCBlength = sizeof(DCB) / 2; /* improper value */
|
|
Packit |
1fb8d4 |
result = GetCommState(hComm, &dcb;;
|
|
Packit |
1fb8d4 |
if (result)
|
|
Packit |
1fb8d4 |
{
|
|
Packit Service |
5a9772 |
printf("GetCommState failure, should have return false because dcb.DCBlength was not "
|
|
Packit Service |
5a9772 |
"correctly initialized\n");
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
ZeroMemory(&dcb, sizeof(DCB));
|
|
Packit |
1fb8d4 |
dcb.DCBlength = sizeof(DCB);
|
|
Packit |
1fb8d4 |
result = GetCommState(hComm, &dcb;;
|
|
Packit |
1fb8d4 |
if (!result)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
printf("GetCommState failure: Ox%x, with adjusted DCBlength\n", GetLastError());
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
pDcb = (DCB*)calloc(2, sizeof(DCB));
|
|
Packit |
1fb8d4 |
if (!pDcb)
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
pDcb->DCBlength = sizeof(DCB) * 2;
|
|
Packit |
1fb8d4 |
result = GetCommState(hComm, pDcb);
|
|
Packit |
1fb8d4 |
result = result && (pDcb->DCBlength == sizeof(DCB) * 2);
|
|
Packit |
1fb8d4 |
free(pDcb);
|
|
Packit |
1fb8d4 |
if (!result)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
printf("GetCommState failure: 0x%x, with bigger DCBlength\n", GetLastError());
|
|
Packit |
1fb8d4 |
return FALSE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
return TRUE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
int TestGetCommState(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 |
printf("DefineCommDevice failure: 0x%x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit Service |
5a9772 |
hComm = CreateFileA("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (hComm == INVALID_HANDLE_VALUE)
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
printf("CreateFileA failure: 0x%x\n", GetLastError());
|
|
Packit |
1fb8d4 |
return EXIT_FAILURE;
|
|
Packit |
1fb8d4 |
}
|
|
Packit |
1fb8d4 |
|
|
Packit |
1fb8d4 |
if (!test_generic(hComm))
|
|
Packit |
1fb8d4 |
{
|
|
Packit |
1fb8d4 |
printf("test_generic failure (SerialDriverUnknown)\n");
|
|
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 |
printf("test_generic failure (SerialDriverSerialSys)\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 |
printf("test_generic failure (SerialDriverSerCxSys)\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 |
printf("test_generic failure (SerialDriverSerCx2Sys)\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 |
}
|