1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/tserial.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,355 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2005 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 +#include <stdlib.h> /* definition of exit() */
1.23 +#include <stdio.h>
1.24 +#include <errno.h>
1.25 +#include <string.h>
1.26 +#include <sys/unistd.h>
1.27 +#include <sys/ioctl.h>
1.28 +#include "ctest.h"
1.29 +
1.30 +
1.31 +int GlobalPort;
1.32 +
1.33 +void DoSharedStuff(void)
1.34 + {
1.35 + int p1, p2, ret;
1.36 + SerialConfig config;
1.37 + p1 = open("COM1:", 1);
1.38 + p2 = open("COM1:", 1);
1.39 +
1.40 + //try changing the settings
1.41 + ret = ioctl(p1, COMMIOCTL_GETCONFIG, &config);
1.42 +
1.43 + config.iRate = Bps115200;
1.44 + ret = ioctl(p2, COMMIOCTL_SETCONFIG, &config);
1.45 +
1.46 + config.iRate = Bps1200;
1.47 + ret = ioctl(p1, COMMIOCTL_GETCONFIG, &config);
1.48 +
1.49 + close(p2);
1.50 + config.iRate = Bps1200;
1.51 + ret = ioctl(p1, COMMIOCTL_GETCONFIG, &config);
1.52 + close(p1);
1.53 +
1.54 + }
1.55 +void OpenAndConfigure(void)
1.56 + {
1.57 + int res;
1.58 + SerialConfig sc;
1.59 +
1.60 + GlobalPort = open("COM1:",0);
1.61 + res = ioctl(GlobalPort, COMMIOCTL_GETCONFIG, &sc);
1.62 + sc.iRate = Bps115200;
1.63 + sc.iParity = ParityNone;
1.64 + res = ioctl(GlobalPort, COMMIOCTL_SETCONFIG, &sc);
1.65 + }
1.66 +
1.67 +
1.68 +void WaitAndRead(void)
1.69 + {
1.70 + int port = GlobalPort;
1.71 + int ret;
1.72 + char buffer[100];
1.73 +
1.74 + printf("Wait and read\n");
1.75 + ret = read(port, buffer, 10);
1.76 + printf("got it %x\n",buffer[0]);
1.77 + }
1.78 +
1.79 +
1.80 +void WriteABit(int x)
1.81 + {
1.82 + int port = GlobalPort;
1.83 + char bob[100];
1.84 + sprintf(bob, "Test Write >>%d<< >>%x<<\n", x, x);
1.85 + write(port, bob, strlen(bob));
1.86 + }
1.87 +
1.88 +void writelots(void)
1.89 + {
1.90 + int n;
1.91 + for (n=0;n<10000;n++)
1.92 + WriteABit(n);
1.93 + }
1.94 +
1.95 +
1.96 +void DoTests(void)
1.97 + {
1.98 + int port1, port2;
1.99 + int timeout = 0;
1.100 + int threshold = 0;
1.101 + int ret=0;
1.102 +
1.103 + port1 = open("COM1:", 0);
1.104 +
1.105 + //this one should fail
1.106 + port2 = open("COM1:", 0);
1.107 +
1.108 + if (-1 != port1)
1.109 + {
1.110 + //play with the signals
1.111 + int sigs[] = {0,0};
1.112 + int sig = 0;
1.113 + ret = ioctl(port1, COMMIOCTL_SETSIGNALS, &sigs[0]);
1.114 + ret = ioctl(port1, COMMIOCTL_GETSIGNALS, &sig);
1.115 + sig = 5000;
1.116 + ret = ioctl(port1, COMMIOCTL_BREAK, &sig);
1.117 + close(port1);
1.118 + }
1.119 +
1.120 + port1 = open("COM1:", 0);
1.121 + port2 = open("COM2:",0);
1.122 +
1.123 +
1.124 + if (-1 != port2)
1.125 + {
1.126 + int len;
1.127 + SerialConfig config;
1.128 + char buffer[1024];
1.129 + size_t size = 100;
1.130 +
1.131 +
1.132 + timeout = 2000;
1.133 + ret = ioctl(port2, COMMIOCTL_SETREADTIMEOUT, &timeout);
1.134 + timeout = 0;
1.135 + ret = ioctl(port2, COMMIOCTL_GETREADTIMEOUT, &timeout);
1.136 +
1.137 +
1.138 + threshold = 2000;
1.139 + ret = ioctl(port2, COMMIOCTL_SETREADTHRESHOLD, &threshold);
1.140 + threshold = 0;
1.141 + ret = ioctl(port2, COMMIOCTL_GETREADTHRESHOLD, &threshold);
1.142 +
1.143 +
1.144 + //buffer lengths
1.145 + len = 0;
1.146 + ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &len);
1.147 +
1.148 + len = 2048;
1.149 + ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &len);
1.150 +
1.151 + len = 0;
1.152 + ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &len);
1.153 +
1.154 + len = 10000000;
1.155 + ret = ioctl(port1, COMMIOCTL_SETBUFFERLENGTH, &len);
1.156 +
1.157 + len = 0;
1.158 + ret = ioctl(port1, COMMIOCTL_GETBUFFERLENGTH, &len);
1.159 +
1.160 +
1.161 + //try changing the settings
1.162 + ret = ioctl(port1, COMMIOCTL_GETCONFIG, &config);
1.163 +
1.164 + config.iRate = Bps115200;
1.165 + ret = ioctl(port1, COMMIOCTL_SETCONFIG, &config);
1.166 +
1.167 + config.iRate = Bps1200;
1.168 + ret = ioctl(port1, COMMIOCTL_GETCONFIG, &config);
1.169 +
1.170 +
1.171 + timeout = threshold = -1;
1.172 + ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
1.173 + ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &timeout);
1.174 +
1.175 + printf("Talk to me, ESC to exit\n");
1.176 + do
1.177 + {
1.178 + ret = read(port1, buffer, size);
1.179 + if (ret != -1)
1.180 + printf("look what I got >>> %c\t%x\n",buffer[0],buffer[0]);
1.181 + else
1.182 + printf("error, errno is %x \n",errno);
1.183 + }
1.184 + while (buffer[0] != 27);
1.185 +
1.186 + printf("Talk to me in blocks of 5, EXIT! to exit\n");
1.187 + threshold = 5;
1.188 + ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
1.189 + do
1.190 + {
1.191 + ret = read(port1, buffer, size);
1.192 + if (ret >=0)
1.193 + {
1.194 + buffer[ret] = 0;
1.195 + printf("look what I got >>> %s\n",buffer);
1.196 + }
1.197 + else
1.198 + printf("error\n");
1.199 + }
1.200 + while (strcmp(buffer, "EXIT!"));
1.201 + printf("Wait 10 seconds then give up\n");
1.202 + timeout = 10000;
1.203 + ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &timeout);
1.204 + threshold = 1;
1.205 + ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
1.206 + ret = read(port1, buffer, size);
1.207 + if (ret > 0)
1.208 + printf("look what I got >>> %c\t%x\n",buffer[0],buffer[0]);
1.209 + else
1.210 + printf("timeout\n");
1.211 +
1.212 +
1.213 + //no threshold, but with a 5 second timeout
1.214 + printf("Now for the hard one!\n");
1.215 + timeout = 5000;
1.216 + ret = ioctl(port1, COMMIOCTL_SETREADTIMEOUT, &timeout);
1.217 + threshold = -1;
1.218 + ret = ioctl(port1, COMMIOCTL_SETREADTHRESHOLD, &threshold);
1.219 + ret = read(port1, buffer, size);
1.220 + if (ret<0)
1.221 + printf("got an error, errno is 56 if it a timeout. erno = %d\n", errno);
1.222 + else
1.223 + printf("look what I got >>> %c\t%x\n",buffer[0],buffer[0]);
1.224 +
1.225 + printf("Going to write some stuff now\n");
1.226 + ret = write(port1, "Lardy dardy dardy cheese\n\n",26);
1.227 + printf("Written some stuff now and got a ret of %d\n", ret);
1.228 +
1.229 +
1.230 + close(port2);
1.231 + }
1.232 +
1.233 + if (-1 != port1)
1.234 + close(port1);
1.235 +
1.236 + printf("Press key to exit");
1.237 + getchar();
1.238 + }
1.239 +
1.240 +
1.241 +
1.242 +void pong(void)
1.243 + {
1.244 + char buf[50];
1.245 + int port = open("IRCOM:0", 0);
1.246 + int x;
1.247 + strcpy(buf,"test shot #");
1.248 + for (x=0;x<10;x++);
1.249 + {
1.250 + write(port, buf, strlen(buf));
1.251 + read(port,buf, 50);
1.252 + strcat(buf,"#");
1.253 + }
1.254 + close(port);
1.255 + }
1.256 +
1.257 +void ping(void)
1.258 + {
1.259 + char buf[50];
1.260 + int x;
1.261 + int port = open("IRCOM:0", 0);
1.262 + for (x=0;x<10;x++);
1.263 + {
1.264 + read(port,buf, 50);
1.265 + strcat(buf,"*");
1.266 + write(port, buf, strlen(buf));
1.267 + }
1.268 + close(port);
1.269 + }
1.270 +
1.271 +/**
1.272 +@SYMTestCaseID SYSLIB-STDLIB-CT-1112
1.273 +@SYMTestCaseDesc Tests for serial port
1.274 +@SYMTestPriority High
1.275 +@SYMTestActions Notifies a thread for events on port
1.276 +@SYMTestExpectedResults Test must not fail
1.277 +@SYMREQ REQ0000
1.278 +*/
1.279 +void WaitForNotifyThread(void)
1.280 + {
1.281 + int val[2];
1.282 + int ret = 0;
1.283 +
1.284 + while (!ret)
1.285 + {
1.286 + val[0] = KNotifyFramingError|KNotifyOverrunError|KNotifyParityError;
1.287 + val[1] = -1;
1.288 + ret = ioctl(GlobalPort, COMMIOCTL_NOTIFY, &val[0]);
1.289 + printf("NotifyThread ret = %d, val[0] is %d, val[1] is %d\n", ret, val[0], val[1]);
1.290 + }
1.291 + }
1.292 +
1.293 +/**
1.294 +@SYMTestCaseID SYSLIB-STDLIB-CT-1113
1.295 +@SYMTestCaseDesc Tests for serial port
1.296 +@SYMTestPriority High
1.297 +@SYMTestActions Keep reading infinitely from the port
1.298 +@SYMTestExpectedResults Test must not fail
1.299 +@SYMREQ REQ0000
1.300 +*/
1.301 +void readloop(void)
1.302 + {
1.303 + char buf[100];
1.304 + int ret = 0;
1.305 + while (!ret)
1.306 + {
1.307 + read(GlobalPort, buf, 100);
1.308 + printf("ReadThread got %x\n", buf[0]);
1.309 + }
1.310 + }
1.311 +
1.312 +int main(void)
1.313 + {
1.314 + void* client;
1.315 + void* t1;
1.316 + void*t2;
1.317 + void*t3;
1.318 + int err;
1.319 +
1.320 +
1.321 +// DoTests();
1.322 +
1.323 +// printf("Do it again using the CPosixServer\n");
1.324 +
1.325 + start_posix_server(); /* calls SpawnPosixServer from C++ code */
1.326 +
1.327 +// client=create_thread(DoTests, "serial tests");
1.328 +// start_thread(client);
1.329 +// err=wait_for_thread(client);
1.330 +
1.331 +
1.332 +// t3 = create_thread(OpenAndConfigure,"cfg");
1.333 +// start_thread(t3);
1.334 +// err=wait_for_thread(t3);
1.335 +
1.336 +// t1 = create_thread(WaitAndRead, "open and wait");
1.337 +// t2 = create_thread(writelots, "writelots");
1.338 +// start_thread(t1);
1.339 +// start_thread(t2);
1.340 +// err=wait_for_thread(t1);
1.341 +// err=wait_for_thread(t2);
1.342 +// close(GlobalPort);
1.343 +
1.344 +
1.345 + OpenAndConfigure();
1.346 + t1 = create_thread(WaitForNotifyThread, "notify");
1.347 + t2 = create_thread(readloop, "readloop");
1.348 +
1.349 + start_thread(t1);
1.350 + start_thread(t2);
1.351 +
1.352 + err=wait_for_thread(t2);
1.353 + err=wait_for_thread(t1);
1.354 +
1.355 + close(GlobalPort);
1.356 + return 0;
1.357 + }
1.358 +