os/ossrv/genericopenlibs/cstdlib/TSTLIB/tser1.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/tser1.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,259 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +
    1.23 +#include <stdlib.h>	/* definition of exit() */
    1.24 +#include <stdio.h>
    1.25 +#include <errno.h>
    1.26 +#include <string.h>
    1.27 +#include <sys/unistd.h>
    1.28 +#include <sys/ioctl.h>
    1.29 +#include "CTEST.H"
    1.30 +
    1.31 +
    1.32 +int GlobalPort;
    1.33 +
    1.34 +test_Data;
    1.35 +int close_console=0;
    1.36 +
    1.37 +/**
    1.38 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1099
    1.39 +@SYMTestCaseDesc	    Tests for serial port
    1.40 +@SYMTestPriority 	    High
    1.41 +@SYMTestActions  	    Tests for serial port,and under posix server
    1.42 +@SYMTestExpectedResults Test must not fail
    1.43 +@SYMREQ                 REQ0000
    1.44 +*/		
    1.45 +void DoTests(char*port)
    1.46 +	{	
    1.47 +	int port1, port2;
    1.48 +	int int1 = 0;
    1.49 +	int int2 = 0;
    1.50 +	int ret=0;
    1.51 +	int sigs[] = {0,0};
    1.52 + 	int sig = 0;
    1.53 +
    1.54 +	SerialConfig config;
    1.55 +	SerialConfig newconfig;
    1.56 +
    1.57 +	char buffer[1024];
    1.58 +	size_t size = 100;
    1.59 +
    1.60 +	memset(&newconfig, 0, sizeof(newconfig));
    1.61 +
    1.62 +	test_Next("Open port");
    1.63 +	
    1.64 +	port1 = open(port,  0);	//exclusive
    1.65 +	if (-1 != port1)
    1.66 +		{
    1.67 +
    1.68 +		//this one should fail
    1.69 +		port2 = open(port,  0);
    1.70 +		test_errno(-1==port2, EACCES);
    1.71 +
    1.72 +		
    1.73 +		test_Next("set signals");
    1.74 +		//play with the signals
    1.75 +		ret = ioctl(port1, COMMIOCTL_SETSIGNALS, &sigs[0]);
    1.76 +		test(-1 != ret);
    1.77 +
    1.78 +		test_Next("get signals");
    1.79 +		ret = ioctl(port1, COMMIOCTL_GETSIGNALS, &sig);
    1.80 +		test(-1 != ret);
    1.81 +
    1.82 +		//try changing the settings
    1.83 +		test_Next("port configuration");
    1.84 +		ret = ioctl(port1, COMMIOCTL_GETCONFIG, &config);
    1.85 +
    1.86 +		config.iRate = Bps115200;
    1.87 +		config.iDataBits = DBits6;
    1.88 +		config.iStopBits=Stop2;
    1.89 +		config.iParity=ParityOdd;
    1.90 +		config.iHandshake=0;
    1.91 +		config.iParityError=ConfigParityErrorFail;
    1.92 +		config.iFifo=3;
    1.93 +		config.iSpecialRate=0;
    1.94 +		config.iTerminatorCount=0;
    1.95 +		config.iTerminator[0]=0;
    1.96 +		config.iXonChar=1;
    1.97 +		config.iXoffChar=2;
    1.98 +		config.iParityErrorChar=3;
    1.99 +		config.iSIREnable=SIRDisable;
   1.100 +		config.iSIRSettings=0;
   1.101 +
   1.102 +		ret = ioctl(port1, COMMIOCTL_SETCONFIG, &config);
   1.103 +		ret = ioctl(port1, COMMIOCTL_GETCONFIG, &newconfig);
   1.104 +
   1.105 +		test(config.iRate == newconfig.iRate);
   1.106 +		test(config.iDataBits==newconfig.iDataBits);
   1.107 +		test(config.iStopBits==newconfig.iStopBits);
   1.108 +		test(config.iParity==newconfig.iParity);
   1.109 +		test(config.iHandshake==newconfig.iHandshake);
   1.110 +		test(config.iParityError==newconfig.iParityError);
   1.111 +		test(config.iFifo==newconfig.iFifo);
   1.112 +		test(config.iSpecialRate==newconfig.iSpecialRate);
   1.113 +		test(config.iTerminatorCount==newconfig.iTerminatorCount);
   1.114 +		test(config.iTerminator[0]==newconfig.iTerminator[0]);
   1.115 +		test(config.iXonChar==newconfig.iXonChar);
   1.116 +		test(config.iXoffChar==newconfig.iXoffChar);
   1.117 +		test(config.iParityErrorChar==newconfig.iParityErrorChar);
   1.118 +		test(config.iSIREnable==newconfig.iSIREnable);
   1.119 +		test(config.iSIRSettings==newconfig.iSIRSettings);
   1.120 +
   1.121 +		test_Next("Buffer lengths");
   1.122 +		int1 = 0;
   1.123 +		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &int1);
   1.124 +		test(ret != -1);
   1.125 +		test(int1 != 0);
   1.126 +
   1.127 +		int1 = 2048;
   1.128 +		ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &int1);
   1.129 +		test(ret != -1);
   1.130 +
   1.131 +		int2 = 0;
   1.132 +		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &int2);
   1.133 +		test(ret != -1);
   1.134 +		test(int1 == int2);
   1.135 +
   1.136 +
   1.137 +		int1 = 100000000;
   1.138 +		ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &int1);
   1.139 +		test(ret != -1);
   1.140 +		
   1.141 +		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &int1);
   1.142 +		test(ret != -1);
   1.143 +		test(int1 == int2);	//shoudn't have changed, val too big.
   1.144 +
   1.145 +
   1.146 +		test_Next("break");
   1.147 +		sig = 5000;
   1.148 +		ret = ioctl(port1, COMMIOCTL_BREAK, &sig);
   1.149 +		//break isn't supported
   1.150 +		test_errno(-1==ret, ENOSYS);
   1.151 +
   1.152 +
   1.153 +		test_Next("read timeout");
   1.154 +		int1 = 2000;
   1.155 +		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &int1);
   1.156 +		test (ret != -1);
   1.157 +
   1.158 +		int2 = 0;
   1.159 +		ret = ioctl(port1, COMMIOCTL_GETREADTIMEOUT, &int2);
   1.160 +		test (ret != -1);
   1.161 +		test(int2 == int1);
   1.162 +
   1.163 +				
   1.164 +		test_Next("read threshold");
   1.165 +		int1 = 2000;
   1.166 +		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &int1);
   1.167 +		test (ret != -1);
   1.168 +
   1.169 +		int2 = 0;
   1.170 +		ret = ioctl(port1, COMMIOCTL_GETREADTHRESHOLD, &int2);
   1.171 +		test(ret != -1);
   1.172 +		test(int1 == int2);
   1.173 +
   1.174 +
   1.175 +
   1.176 +		//signals
   1.177 +		test_Next("test notifications supported");
   1.178 +		ret = ioctl(port1, COMMIOCTL_NOTIFYSUPPORTED, &int1);
   1.179 +		test((int1 & (KNotifyFramingError|KNotifyOverrunError|KNotifyParityError)) == (KNotifyFramingError|KNotifyOverrunError|KNotifyParityError));
   1.180 +		printf("Notifications supported are %08x\n",int1);
   1.181 +
   1.182 +		test_Next("Test read timeout with a threshold");
   1.183 +		int2 = 3000;
   1.184 +		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &int2);
   1.185 +		test (ret != -1);
   1.186 +		
   1.187 +		int1 = 1;
   1.188 +		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &int1);
   1.189 +		test (ret != -1);
   1.190 +
   1.191 +		ret = read(port1, buffer, size);
   1.192 +		test_errno(-1==ret, ETIMEDOUT);
   1.193 +
   1.194 +		//no threshold, but with a 1 second timeout
   1.195 +		test_Next("no threshold with timeout");
   1.196 +		int2 = 5000;
   1.197 +		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &int2);
   1.198 +		test (ret != -1);
   1.199 +
   1.200 +		int1 = -1;
   1.201 +		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &int1);
   1.202 +		test (ret != -1);
   1.203 +
   1.204 +		ret = read(port1, buffer, size);
   1.205 +		test_errno(-1==ret, ETIMEDOUT);
   1.206 +
   1.207 +
   1.208 +		close(port1);
   1.209 +		}
   1.210 +	else
   1.211 +		printf("failed to open port %s with errno %d\n", port,errno);
   1.212 +	}
   1.213 +
   1.214 +
   1.215 +
   1.216 +void DoTestsSerial(void)
   1.217 +	{
   1.218 +	DoTests("COM1:");
   1.219 +
   1.220 +	if (close_console)
   1.221 +		{
   1.222 +		test_Close();
   1.223 +		close(0);
   1.224 +		close(1);
   1.225 +		close(2);
   1.226 +		}
   1.227 +	}
   1.228 +
   1.229 +
   1.230 +
   1.231 +int main(void)
   1.232 +	{
   1.233 +	void* client;
   1.234 +	int err;
   1.235 +	
   1.236 +	test_Title("Serial port test1");
   1.237 +	test_Next("Do tests with local server for COM1:");
   1.238 +	DoTests("COM1:");
   1.239 +	test_Next("Do tests with local server for COM2:");
   1.240 +	DoTests("COM2:");
   1.241 +	test_Next("Do tests with local server for COM3:");
   1.242 +	DoTests("COM3:");
   1.243 +	test_Next("Do tests with local server for COM4:");
   1.244 +	DoTests("COM4:");
   1.245 +
   1.246 +
   1.247 +	start_posix_server();	/* calls SpawnPosixServer from C++ code */
   1.248 +
   1.249 +	close_console=1;
   1.250 +	test_Next("Do tests with posix server for COM1:");
   1.251 +	client=create_thread(DoTestsSerial, "serial tests");
   1.252 +	test(client);
   1.253 +
   1.254 +	start_thread(client);
   1.255 +	err=wait_for_thread(client);
   1.256 +	test(0 == err);
   1.257 +
   1.258 +
   1.259 +	test_Close();
   1.260 +	return 0;
   1.261 +	}
   1.262 +