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