Blame winpr/libwinpr/comm/test/TestCommConfig.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * Serial Communication API
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
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 <sys/stat.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/comm.h>
Packit 1fb8d4
#include <winpr/file.h>
Packit 1fb8d4
#include <winpr/synch.h>
Packit 1fb8d4
#include <winpr/handle.h>
Packit 1fb8d4
Packit 1fb8d4
int TestCommConfig(int argc, char* argv[])
Packit 1fb8d4
{
Packit 1fb8d4
	DCB dcb;
Packit 1fb8d4
	HANDLE hComm;
Packit 1fb8d4
	BOOL success;
Packit 1fb8d4
	LPCSTR lpFileName = "\\\\.\\COM1";
Packit 1fb8d4
	COMMPROP commProp;
Packit 1fb8d4
	struct stat statbuf;
Packit 1fb8d4
Packit Service 5a9772
	hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (hComm && (hComm != INVALID_HANDLE_VALUE))
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr,
Packit Service 5a9772
		        "CreateFileA failure: could create a handle on a not yet defined device: %s\n",
Packit Service 5a9772
		        lpFileName);
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
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
	success = DefineCommDevice(lpFileName, "/dev/ttyS0");
Packit Service 5a9772
	if (!success)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "DefineCommDevice failure: %s\n", lpFileName);
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE,
Packit Service 5a9772
	                    FILE_SHARE_WRITE, /* invalid parmaeter */
Packit Service 5a9772
	                    NULL, CREATE_NEW, /* invalid parameter */
Packit Service 5a9772
	                    0, (HANDLE)1234); /* invalid parmaeter */
Packit 1fb8d4
	if (hComm != INVALID_HANDLE_VALUE)
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr,
Packit Service 5a9772
		        "CreateFileA failure: could create a handle with some invalid parameters %s\n",
Packit Service 5a9772
		        lpFileName);
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	hComm = CreateFileA(lpFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (!hComm || (hComm == INVALID_HANDLE_VALUE))
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr, "CreateFileA failure: %s GetLastError() = 0x%08x\n", lpFileName,
Packit Service 5a9772
		        GetLastError());
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* TODO: a second call to CreateFileA should failed and
Packit 1fb8d4
	 * GetLastError should return ERROR_SHARING_VIOLATION */
Packit 1fb8d4
Packit 1fb8d4
	ZeroMemory(&dcb, sizeof(DCB));
Packit 1fb8d4
	dcb.DCBlength = sizeof(DCB);
Packit 1fb8d4
	success = GetCommState(hComm, &dcb;;
Packit 1fb8d4
	if (!success)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "GetCommState failure: GetLastError() = Ox%x\n", GetLastError());
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	fprintf(stderr,
Packit Service 5a9772
	        "BaudRate: %" PRIu32 " ByteSize: %" PRIu8 " Parity: %" PRIu8 " StopBits: %" PRIu8 "\n",
Packit Service 5a9772
	        dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
Packit 1fb8d4
Packit 1fb8d4
	ZeroMemory(&commProp, sizeof(COMMPROP));
Packit 1fb8d4
	if (!GetCommProperties(hComm, &commProp))
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "GetCommProperties failure: GetLastError(): 0x%08x\n", GetLastError());
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((commProp.dwSettableBaud & BAUD_57600) <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "BAUD_57600 unsupported!\n");
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((commProp.dwSettableBaud & BAUD_14400) > 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "BAUD_14400 supported!\n");
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	dcb.BaudRate = CBR_57600;
Packit 1fb8d4
	dcb.ByteSize = 8;
Packit 1fb8d4
	dcb.Parity = NOPARITY;
Packit 1fb8d4
	dcb.StopBits = ONESTOPBIT;
Packit 1fb8d4
Packit 1fb8d4
	success = SetCommState(hComm, &dcb;;
Packit 1fb8d4
Packit 1fb8d4
	if (!success)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "SetCommState failure: GetLastError() = 0x%x\n", GetLastError());
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	success = GetCommState(hComm, &dcb;;
Packit 1fb8d4
Packit 1fb8d4
	if (!success)
Packit 1fb8d4
	{
Packit 1fb8d4
		fprintf(stderr, "GetCommState failure: GetLastError() = 0x%x\n", GetLastError());
Packit 1fb8d4
		return 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if ((dcb.BaudRate != CBR_57600) || (dcb.ByteSize != 8) || (dcb.Parity != NOPARITY) ||
Packit Service 5a9772
	    (dcb.StopBits != ONESTOPBIT))
Packit 1fb8d4
	{
Packit Service 5a9772
		fprintf(stderr,
Packit Service 5a9772
		        "Got an unexpeted value among: BaudRate: %" PRIu32 " ByteSize: %" PRIu8
Packit Service 5a9772
		        " Parity: %" PRIu8 " StopBits: %" PRIu8 "\n",
Packit Service 5a9772
		        dcb.BaudRate, dcb.ByteSize, dcb.Parity, dcb.StopBits);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	CloseHandle(hComm);
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}