genericopenlibs/cstdlib/TSTLIB/tser1.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <stdlib.h>	/* definition of exit() */
       
    21 #include <stdio.h>
       
    22 #include <errno.h>
       
    23 #include <string.h>
       
    24 #include <sys/unistd.h>
       
    25 #include <sys/ioctl.h>
       
    26 #include "CTEST.H"
       
    27 
       
    28 
       
    29 int GlobalPort;
       
    30 
       
    31 test_Data;
       
    32 int close_console=0;
       
    33 
       
    34 /**
       
    35 @SYMTestCaseID          SYSLIB-STDLIB-CT-1099
       
    36 @SYMTestCaseDesc	    Tests for serial port
       
    37 @SYMTestPriority 	    High
       
    38 @SYMTestActions  	    Tests for serial port,and under posix server
       
    39 @SYMTestExpectedResults Test must not fail
       
    40 @SYMREQ                 REQ0000
       
    41 */		
       
    42 void DoTests(char*port)
       
    43 	{	
       
    44 	int port1, port2;
       
    45 	int int1 = 0;
       
    46 	int int2 = 0;
       
    47 	int ret=0;
       
    48 	int sigs[] = {0,0};
       
    49  	int sig = 0;
       
    50 
       
    51 	SerialConfig config;
       
    52 	SerialConfig newconfig;
       
    53 
       
    54 	char buffer[1024];
       
    55 	size_t size = 100;
       
    56 
       
    57 	memset(&newconfig, 0, sizeof(newconfig));
       
    58 
       
    59 	test_Next("Open port");
       
    60 	
       
    61 	port1 = open(port,  0);	//exclusive
       
    62 	if (-1 != port1)
       
    63 		{
       
    64 
       
    65 		//this one should fail
       
    66 		port2 = open(port,  0);
       
    67 		test_errno(-1==port2, EACCES);
       
    68 
       
    69 		
       
    70 		test_Next("set signals");
       
    71 		//play with the signals
       
    72 		ret = ioctl(port1, COMMIOCTL_SETSIGNALS, &sigs[0]);
       
    73 		test(-1 != ret);
       
    74 
       
    75 		test_Next("get signals");
       
    76 		ret = ioctl(port1, COMMIOCTL_GETSIGNALS, &sig);
       
    77 		test(-1 != ret);
       
    78 
       
    79 		//try changing the settings
       
    80 		test_Next("port configuration");
       
    81 		ret = ioctl(port1, COMMIOCTL_GETCONFIG, &config);
       
    82 
       
    83 		config.iRate = Bps115200;
       
    84 		config.iDataBits = DBits6;
       
    85 		config.iStopBits=Stop2;
       
    86 		config.iParity=ParityOdd;
       
    87 		config.iHandshake=0;
       
    88 		config.iParityError=ConfigParityErrorFail;
       
    89 		config.iFifo=3;
       
    90 		config.iSpecialRate=0;
       
    91 		config.iTerminatorCount=0;
       
    92 		config.iTerminator[0]=0;
       
    93 		config.iXonChar=1;
       
    94 		config.iXoffChar=2;
       
    95 		config.iParityErrorChar=3;
       
    96 		config.iSIREnable=SIRDisable;
       
    97 		config.iSIRSettings=0;
       
    98 
       
    99 		ret = ioctl(port1, COMMIOCTL_SETCONFIG, &config);
       
   100 		ret = ioctl(port1, COMMIOCTL_GETCONFIG, &newconfig);
       
   101 
       
   102 		test(config.iRate == newconfig.iRate);
       
   103 		test(config.iDataBits==newconfig.iDataBits);
       
   104 		test(config.iStopBits==newconfig.iStopBits);
       
   105 		test(config.iParity==newconfig.iParity);
       
   106 		test(config.iHandshake==newconfig.iHandshake);
       
   107 		test(config.iParityError==newconfig.iParityError);
       
   108 		test(config.iFifo==newconfig.iFifo);
       
   109 		test(config.iSpecialRate==newconfig.iSpecialRate);
       
   110 		test(config.iTerminatorCount==newconfig.iTerminatorCount);
       
   111 		test(config.iTerminator[0]==newconfig.iTerminator[0]);
       
   112 		test(config.iXonChar==newconfig.iXonChar);
       
   113 		test(config.iXoffChar==newconfig.iXoffChar);
       
   114 		test(config.iParityErrorChar==newconfig.iParityErrorChar);
       
   115 		test(config.iSIREnable==newconfig.iSIREnable);
       
   116 		test(config.iSIRSettings==newconfig.iSIRSettings);
       
   117 
       
   118 		test_Next("Buffer lengths");
       
   119 		int1 = 0;
       
   120 		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &int1);
       
   121 		test(ret != -1);
       
   122 		test(int1 != 0);
       
   123 
       
   124 		int1 = 2048;
       
   125 		ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &int1);
       
   126 		test(ret != -1);
       
   127 
       
   128 		int2 = 0;
       
   129 		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &int2);
       
   130 		test(ret != -1);
       
   131 		test(int1 == int2);
       
   132 
       
   133 
       
   134 		int1 = 100000000;
       
   135 		ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &int1);
       
   136 		test(ret != -1);
       
   137 		
       
   138 		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &int1);
       
   139 		test(ret != -1);
       
   140 		test(int1 == int2);	//shoudn't have changed, val too big.
       
   141 
       
   142 
       
   143 		test_Next("break");
       
   144 		sig = 5000;
       
   145 		ret = ioctl(port1, COMMIOCTL_BREAK, &sig);
       
   146 		//break isn't supported
       
   147 		test_errno(-1==ret, ENOSYS);
       
   148 
       
   149 
       
   150 		test_Next("read timeout");
       
   151 		int1 = 2000;
       
   152 		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &int1);
       
   153 		test (ret != -1);
       
   154 
       
   155 		int2 = 0;
       
   156 		ret = ioctl(port1, COMMIOCTL_GETREADTIMEOUT, &int2);
       
   157 		test (ret != -1);
       
   158 		test(int2 == int1);
       
   159 
       
   160 				
       
   161 		test_Next("read threshold");
       
   162 		int1 = 2000;
       
   163 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &int1);
       
   164 		test (ret != -1);
       
   165 
       
   166 		int2 = 0;
       
   167 		ret = ioctl(port1, COMMIOCTL_GETREADTHRESHOLD, &int2);
       
   168 		test(ret != -1);
       
   169 		test(int1 == int2);
       
   170 
       
   171 
       
   172 
       
   173 		//signals
       
   174 		test_Next("test notifications supported");
       
   175 		ret = ioctl(port1, COMMIOCTL_NOTIFYSUPPORTED, &int1);
       
   176 		test((int1 & (KNotifyFramingError|KNotifyOverrunError|KNotifyParityError)) == (KNotifyFramingError|KNotifyOverrunError|KNotifyParityError));
       
   177 		printf("Notifications supported are %08x\n",int1);
       
   178 
       
   179 		test_Next("Test read timeout with a threshold");
       
   180 		int2 = 3000;
       
   181 		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &int2);
       
   182 		test (ret != -1);
       
   183 		
       
   184 		int1 = 1;
       
   185 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &int1);
       
   186 		test (ret != -1);
       
   187 
       
   188 		ret = read(port1, buffer, size);
       
   189 		test_errno(-1==ret, ETIMEDOUT);
       
   190 
       
   191 		//no threshold, but with a 1 second timeout
       
   192 		test_Next("no threshold with timeout");
       
   193 		int2 = 5000;
       
   194 		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &int2);
       
   195 		test (ret != -1);
       
   196 
       
   197 		int1 = -1;
       
   198 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &int1);
       
   199 		test (ret != -1);
       
   200 
       
   201 		ret = read(port1, buffer, size);
       
   202 		test_errno(-1==ret, ETIMEDOUT);
       
   203 
       
   204 
       
   205 		close(port1);
       
   206 		}
       
   207 	else
       
   208 		printf("failed to open port %s with errno %d\n", port,errno);
       
   209 	}
       
   210 
       
   211 
       
   212 
       
   213 void DoTestsSerial(void)
       
   214 	{
       
   215 	DoTests("COM1:");
       
   216 
       
   217 	if (close_console)
       
   218 		{
       
   219 		test_Close();
       
   220 		close(0);
       
   221 		close(1);
       
   222 		close(2);
       
   223 		}
       
   224 	}
       
   225 
       
   226 
       
   227 
       
   228 int main(void)
       
   229 	{
       
   230 	void* client;
       
   231 	int err;
       
   232 	
       
   233 	test_Title("Serial port test1");
       
   234 	test_Next("Do tests with local server for COM1:");
       
   235 	DoTests("COM1:");
       
   236 	test_Next("Do tests with local server for COM2:");
       
   237 	DoTests("COM2:");
       
   238 	test_Next("Do tests with local server for COM3:");
       
   239 	DoTests("COM3:");
       
   240 	test_Next("Do tests with local server for COM4:");
       
   241 	DoTests("COM4:");
       
   242 
       
   243 
       
   244 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
       
   245 
       
   246 	close_console=1;
       
   247 	test_Next("Do tests with posix server for COM1:");
       
   248 	client=create_thread(DoTestsSerial, "serial tests");
       
   249 	test(client);
       
   250 
       
   251 	start_thread(client);
       
   252 	err=wait_for_thread(client);
       
   253 	test(0 == err);
       
   254 
       
   255 
       
   256 	test_Close();
       
   257 	return 0;
       
   258 	}
       
   259