genericopenlibs/cstdlib/TSTLIB/tser3.cpp
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <estlib.h>
       
    17 #include <stdlib.h>	/* definition of exit() */
       
    18 #include <stdio.h>
       
    19 #include <errno.h>
       
    20 #include <string.h>
       
    21 #include <sys/unistd.h>
       
    22 #include <sys/ioctl.h>
       
    23 #include "CTEST.H"
       
    24 
       
    25 #ifdef __cplusplus
       
    26 extern "C" {
       
    27 #endif
       
    28 
       
    29 
       
    30 #define SIZE 10000
       
    31 
       
    32 int ReaderPort;
       
    33 int WriterPort;
       
    34 
       
    35 /**
       
    36 @SYMTestCaseID          SYSLIB-STDLIB-CT-1102
       
    37 @SYMTestCaseDesc	    Tests for serial port
       
    38 @SYMTestPriority 	    High
       
    39 @SYMTestActions  	    Tests by writing a string buffer to the port
       
    40 @SYMTestExpectedResults Test must not fail
       
    41 @SYMREQ                 REQ0000
       
    42 */		
       
    43 void Writer()
       
    44 	{
       
    45 	char *buffer;
       
    46 	
       
    47 	buffer = (char*)malloc(SIZE);
       
    48 	memset(buffer, '*', SIZE);
       
    49 
       
    50 	printf("\nbig write\n");
       
    51 	(void)write(WriterPort, buffer, SIZE);
       
    52 	(void)errno;
       
    53 	printf("\n>>Writer<<small read\n");
       
    54 	read(WriterPort,buffer,1);
       
    55 	printf("\nbig write\n");
       
    56 	write(WriterPort, buffer, SIZE);
       
    57 	printf("\n>>Writer<<small read\n");
       
    58 	read(WriterPort,buffer,1);
       
    59 
       
    60 	}
       
    61 
       
    62 /**
       
    63 @SYMTestCaseID          SYSLIB-STDLIB-CT-1103
       
    64 @SYMTestCaseDesc	    Tests for serial port
       
    65 @SYMTestPriority 	    High
       
    66 @SYMTestActions  	    Notifies on a read operation 
       
    67 @SYMTestExpectedResults Test must not fail
       
    68 @SYMREQ                 REQ0000
       
    69 */		
       
    70 void Watcher()
       
    71 	{
       
    72 	int res=0;
       
    73 	int notify[2] = {0,0};
       
    74 	int supported = 0;
       
    75 
       
    76 
       
    77 	res = ioctl(ReaderPort, COMMIOCTL_NOTIFYSUPPORTED, &supported);
       
    78 	printf("\nsupported notifies are %08x\n",supported);
       
    79 	while (-1 != res)
       
    80 		{
       
    81 		notify[0] = supported;
       
    82 		printf("asking for %08x\n",notify[0]);
       
    83 		res = ioctl(ReaderPort, COMMIOCTL_NOTIFY, &notify[0]);
       
    84 		printf("\n**Notify** res = %d params %d, %d, errno %d\n",res, notify[0], notify[1],errno);
       
    85 		}
       
    86 
       
    87 	}
       
    88 
       
    89 /**
       
    90 @SYMTestCaseID          SYSLIB-STDLIB-CT-1104
       
    91 @SYMTestCaseDesc	    Tests for serial port
       
    92 @SYMTestPriority 	    High
       
    93 @SYMTestActions  	    Reads to buffer from serial port,test for failure conditions
       
    94 @SYMTestExpectedResults Test must not fail
       
    95 @SYMREQ                 REQ0000
       
    96 */		
       
    97 void Reader()
       
    98 	{
       
    99 	int res;
       
   100 	char *buffer, *p;
       
   101 	int x;
       
   102 
       
   103 
       
   104 	
       
   105 	buffer = (char*)malloc(SIZE*2);
       
   106 
       
   107 	res = 0;
       
   108 	p = buffer;
       
   109 	
       
   110 
       
   111 	x = 5000;
       
   112 	res = ioctl(ReaderPort, COMMIOCTL_SETREADTIMEOUT, &x);
       
   113 
       
   114 	for(x = 0;;x++)
       
   115 		{
       
   116 		res = read(ReaderPort, p, 100);
       
   117 		if (res > 0)
       
   118 			{
       
   119 			putchar('.');
       
   120 			p += res;
       
   121 			}
       
   122 		else
       
   123 			{
       
   124 			printf("\nread failed with errno %d\n",errno);
       
   125 			break;
       
   126 			}
       
   127 		}
       
   128 		
       
   129 	printf("\n>>Reader<<small write\n");
       
   130 	res = write(ReaderPort, buffer ,1);
       
   131 
       
   132 	printf("\n>>Reader<<set threshold\n");
       
   133 	x = 100;
       
   134 	res = ioctl(ReaderPort, COMMIOCTL_SETREADTHRESHOLD, &x);
       
   135 	p = buffer;
       
   136 
       
   137 	printf("\nAnd again with a threshold of 100\n");
       
   138 	
       
   139 	for(x = 0;;x++)
       
   140 		{
       
   141 		res = read(ReaderPort, p, 100);
       
   142 		if (res > 0)
       
   143 			{
       
   144 			putchar('.');
       
   145 			p += res;
       
   146 			}
       
   147 		else
       
   148 			{
       
   149 			printf("\nread failed with errno %d\n",errno);
       
   150 			break;
       
   151 			}
       
   152 		}
       
   153 		
       
   154 	printf("\n>>Reader<<Small write\n");
       
   155 	write(ReaderPort, buffer ,1);
       
   156 	
       
   157 
       
   158 	}
       
   159 
       
   160 #ifdef __cplusplus
       
   161 }
       
   162 #endif
       
   163 
       
   164 int main(int, char**)
       
   165 	{
       
   166 	void* t1;
       
   167 	void* t2;
       
   168 	void* t3;
       
   169 	
       
   170 	SerialConfig sc;
       
   171 
       
   172 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
       
   173 
       
   174 	WriterPort = open("COM1:",0);
       
   175 	if (-1 == WriterPort)
       
   176 		exit(-1);
       
   177 	(void)ioctl(WriterPort, COMMIOCTL_GETCONFIG, &sc);
       
   178 	sc.iHandshake = 0;
       
   179 	sc.iRate = Bps115200;
       
   180 	sc.iParity = ParityNone;
       
   181 	(void)ioctl(WriterPort, COMMIOCTL_SETCONFIG, &sc);
       
   182 	
       
   183 	ReaderPort = open("COM2:",0);
       
   184 	if (-1 == ReaderPort)
       
   185 		exit(-2);
       
   186 	(void)ioctl(ReaderPort, COMMIOCTL_GETCONFIG, &sc);
       
   187 	sc.iHandshake = 0;
       
   188 	sc.iRate = Bps115200;
       
   189 	sc.iParity = ParityNone;
       
   190 	(void)ioctl(ReaderPort, COMMIOCTL_SETCONFIG, &sc);
       
   191 
       
   192 	t3 = create_thread(Watcher, "watcher1");
       
   193 	t1 = create_thread(Reader, "reader");
       
   194 	t2 = create_thread(Writer, "writer");
       
   195 
       
   196 	start_thread(t3);
       
   197 	start_thread(t2);
       
   198 	start_thread(t1);
       
   199 
       
   200 	(void)wait_for_thread(t1);
       
   201 	(void)wait_for_thread(t2);
       
   202 
       
   203 	close(WriterPort);
       
   204 	close(ReaderPort);
       
   205 	(void)wait_for_thread(t3);
       
   206 	printf("\nPress a key\n");
       
   207 	getchar();
       
   208 	return 0;
       
   209 	}
       
   210 
       
   211