os/ossrv/genericopenlibs/cstdlib/TSTLIB/tserial.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2005 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 #include <stdlib.h>	/* definition of exit() */
    20 #include <stdio.h>
    21 #include <errno.h>
    22 #include <string.h>
    23 #include <sys/unistd.h>
    24 #include <sys/ioctl.h>
    25 #include "ctest.h"
    26 
    27 
    28 int GlobalPort;
    29 
    30 void DoSharedStuff(void)
    31 	{
    32 		int p1, p2, ret;
    33 		SerialConfig config;
    34 		p1 = open("COM1:",  1);
    35 		p2 = open("COM1:",  1);
    36 
    37 		//try changing the settings
    38 		ret = ioctl(p1, COMMIOCTL_GETCONFIG, &config);
    39 
    40 		config.iRate = Bps115200;
    41 		ret = ioctl(p2, COMMIOCTL_SETCONFIG, &config);
    42 
    43 		config.iRate = Bps1200;
    44 		ret = ioctl(p1, COMMIOCTL_GETCONFIG, &config);
    45 
    46 		close(p2);
    47 		config.iRate = Bps1200;
    48 		ret = ioctl(p1, COMMIOCTL_GETCONFIG, &config);
    49 		close(p1);
    50 
    51 	}
    52 void OpenAndConfigure(void)
    53 	{
    54 	int res;
    55 	SerialConfig sc;
    56 
    57 	GlobalPort = open("COM1:",0);
    58 	res = ioctl(GlobalPort, COMMIOCTL_GETCONFIG, &sc);
    59 	sc.iRate = Bps115200;
    60 	sc.iParity = ParityNone;
    61 	res = ioctl(GlobalPort, COMMIOCTL_SETCONFIG, &sc);
    62 	}
    63 
    64 
    65 void WaitAndRead(void)
    66 	{
    67 	int port = GlobalPort;
    68 	int ret;
    69 	char buffer[100];
    70 
    71 	printf("Wait and read\n");
    72 	ret = read(port, buffer, 10);
    73 	printf("got it %x\n",buffer[0]);
    74 	}
    75 
    76 
    77 void WriteABit(int x)
    78 	{
    79 	int port = GlobalPort;
    80 	char bob[100];
    81 	sprintf(bob, "Test Write >>%d<<    >>%x<<\n", x, x);
    82 	write(port, bob, strlen(bob));
    83 	}
    84 
    85 void writelots(void)
    86 	{
    87 	int n;
    88 	for (n=0;n<10000;n++)
    89 		WriteABit(n);
    90 	}
    91 
    92 
    93 void DoTests(void)
    94 	{	
    95 	int port1, port2;
    96 	int timeout = 0;
    97 	int threshold = 0;
    98 	int ret=0;
    99 
   100 	port1 = open("COM1:",  0);
   101 
   102 	//this one should fail
   103 	port2 = open("COM1:",  0);
   104 
   105 	if (-1 != port1)
   106 		{
   107 		//play with the signals
   108 		int sigs[] = {0,0};
   109  		int sig = 0;
   110 		ret = ioctl(port1, COMMIOCTL_SETSIGNALS, &sigs[0]);
   111 		ret = ioctl(port1, COMMIOCTL_GETSIGNALS, &sig);
   112 		sig = 5000;
   113 		ret = ioctl(port1, COMMIOCTL_BREAK, &sig);
   114 		close(port1);
   115 		}
   116 
   117 	port1 = open("COM1:",  0);
   118 	port2 = open("COM2:",0);
   119 
   120 
   121 	if (-1 != port2)
   122 		{
   123 		int len;
   124 		SerialConfig config;
   125 		char buffer[1024];
   126 		size_t size = 100;
   127 
   128 
   129 		timeout = 2000;
   130 		ret = ioctl(port2, COMMIOCTL_SETREADTIMEOUT, &timeout);
   131 		timeout = 0;
   132 		ret = ioctl(port2, COMMIOCTL_GETREADTIMEOUT, &timeout);
   133 
   134 				
   135 		threshold = 2000;
   136 		ret = ioctl(port2, COMMIOCTL_SETREADTHRESHOLD, &threshold);
   137 		threshold = 0;
   138 		ret = ioctl(port2, COMMIOCTL_GETREADTHRESHOLD, &threshold);
   139 
   140 
   141 		//buffer lengths
   142 		len = 0;
   143 		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &len);
   144 
   145 		len = 2048;
   146 		ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &len);
   147 
   148 		len = 0;
   149 		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &len);
   150 
   151 		len = 10000000;
   152 		ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &len);
   153 		
   154 		len = 0;
   155 		ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &len);
   156 
   157 
   158 		//try changing the settings
   159 		ret = ioctl(port1, COMMIOCTL_GETCONFIG, &config);
   160 
   161 		config.iRate = Bps115200;
   162 		ret = ioctl(port1, COMMIOCTL_SETCONFIG, &config);
   163 
   164 		config.iRate = Bps1200;
   165 		ret = ioctl(port1, COMMIOCTL_GETCONFIG, &config);
   166 
   167 
   168 		timeout = threshold = -1;
   169 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
   170 		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &timeout);
   171 		
   172 		printf("Talk to me, ESC to exit\n");
   173 		do
   174 			{
   175 			ret = read(port1, buffer, size);
   176 			if (ret != -1)
   177 				printf("look what I got >>> %c\t%x\n",buffer[0],buffer[0]);
   178 			else
   179 				printf("error, errno is %x \n",errno);
   180 			}
   181 		while (buffer[0] != 27);
   182 
   183 		printf("Talk to me in blocks of 5, EXIT! to exit\n");
   184 		threshold = 5;
   185 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
   186 		do
   187 			{
   188 			ret = read(port1, buffer, size);
   189 			if (ret >=0)
   190 				{
   191 				buffer[ret] = 0;
   192 				printf("look what I got >>> %s\n",buffer);
   193 				}
   194 			else
   195 				printf("error\n");
   196 			}
   197 		while (strcmp(buffer, "EXIT!"));
   198 		printf("Wait 10 seconds then give up\n");
   199 		timeout = 10000;
   200 		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &timeout);
   201 		threshold = 1;
   202 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
   203 		ret = read(port1, buffer, size);
   204 		if (ret > 0)
   205 			printf("look what I got >>> %c\t%x\n",buffer[0],buffer[0]);
   206 		else
   207 			printf("timeout\n");
   208 
   209 		
   210 		//no threshold, but with a 5 second timeout
   211 		printf("Now for the hard one!\n");
   212 		timeout = 5000;
   213 		ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &timeout);
   214 		threshold = -1;
   215 		ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
   216 		ret = read(port1, buffer, size);
   217 		if (ret<0)
   218 			printf("got an error, errno is 56 if it a timeout. erno = %d\n", errno);
   219 		else
   220 			printf("look what I got >>> %c\t%x\n",buffer[0],buffer[0]);
   221 
   222 		printf("Going to write some stuff now\n");
   223 		ret = write(port1, "Lardy dardy dardy cheese\n\n",26);
   224 		printf("Written some stuff now and got a ret of %d\n", ret);
   225 
   226 
   227 		close(port2);
   228 		}
   229 	
   230 	if (-1 != port1)
   231 		close(port1);
   232 
   233 	printf("Press key to exit");
   234 	getchar();
   235 	}
   236 
   237 
   238 
   239 void pong(void)
   240 	{
   241 		char buf[50];
   242 		int port = open("IRCOM:0", 0);
   243 		int x;
   244 		strcpy(buf,"test shot #");
   245 		for (x=0;x<10;x++);
   246 			{
   247 			write(port, buf, strlen(buf));
   248 			read(port,buf, 50);
   249 			strcat(buf,"#");
   250 			}
   251 		close(port);
   252 	}
   253 
   254 void ping(void)
   255 	{
   256 		char buf[50];
   257 		int x;
   258 		int port = open("IRCOM:0", 0);
   259 		for (x=0;x<10;x++);
   260 			{
   261 			read(port,buf, 50);
   262 			strcat(buf,"*");
   263 			write(port, buf, strlen(buf));
   264 			}
   265 		close(port);
   266 	}
   267 
   268 /**
   269 @SYMTestCaseID          SYSLIB-STDLIB-CT-1112
   270 @SYMTestCaseDesc	    Tests for serial port
   271 @SYMTestPriority 	    High
   272 @SYMTestActions  	    Notifies a thread for events on port 
   273 @SYMTestExpectedResults Test must not fail
   274 @SYMREQ                 REQ0000
   275 */					
   276 void WaitForNotifyThread(void)
   277 	{
   278 	int val[2];
   279 	int ret = 0;
   280 	
   281 	while (!ret)
   282 		{
   283 		val[0] = KNotifyFramingError|KNotifyOverrunError|KNotifyParityError;
   284 		val[1] = -1;
   285 		ret = ioctl(GlobalPort, COMMIOCTL_NOTIFY, &val[0]);
   286 		printf("NotifyThread ret = %d, val[0] is %d, val[1] is %d\n", ret, val[0], val[1]);
   287 		}
   288 	}
   289 
   290 /**
   291 @SYMTestCaseID          SYSLIB-STDLIB-CT-1113
   292 @SYMTestCaseDesc	    Tests for serial port
   293 @SYMTestPriority 	    High
   294 @SYMTestActions  	    Keep reading infinitely from the port
   295 @SYMTestExpectedResults Test must not fail
   296 @SYMREQ                 REQ0000
   297 */					
   298 void readloop(void)
   299 	{
   300 	char buf[100];
   301 	int ret = 0;
   302 	while (!ret)
   303 		{
   304 		read(GlobalPort, buf, 100);
   305 		printf("ReadThread got %x\n", buf[0]);
   306 		}
   307 	}
   308 
   309 int main(void)
   310 	{
   311 	void* client;
   312 	void* t1;
   313 	void*t2;
   314 	void*t3;
   315 	int err;
   316 	
   317 
   318 //	DoTests();
   319 
   320 //	printf("Do it again using the CPosixServer\n");
   321 
   322 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
   323 
   324 //	client=create_thread(DoTests, "serial tests");
   325 //	start_thread(client);
   326 //	err=wait_for_thread(client);
   327 
   328 	
   329 //	t3 = create_thread(OpenAndConfigure,"cfg");
   330 //	start_thread(t3);
   331 //	err=wait_for_thread(t3);
   332 
   333 //	t1 = create_thread(WaitAndRead, "open and wait");
   334 //	t2 = create_thread(writelots, "writelots");
   335 //	start_thread(t1);
   336 //	start_thread(t2);
   337 //	err=wait_for_thread(t1);
   338 //	err=wait_for_thread(t2);
   339 //	close(GlobalPort);
   340 
   341 	
   342 	OpenAndConfigure();
   343 	t1 = create_thread(WaitForNotifyThread, "notify");
   344 	t2 = create_thread(readloop, "readloop");
   345 
   346 	start_thread(t1);
   347 	start_thread(t2);
   348 
   349 	err=wait_for_thread(t2);
   350 	err=wait_for_thread(t1);
   351 	
   352 	close(GlobalPort);
   353 	return 0;
   354 	}
   355