summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/termios/init.c
blob: 96c7e0934e1732d410115d63f0ee67d8c66b2ef7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * RTEMS configuration/initialization
 * 
 * This program may be distributed and used for any purpose.
 * I ask only that you:
 *	1. Leave this author information intact.
 *	2. Document any changes you make.
 *
 * W. Eric Norum
 * Saskatchewan Accelerator Laboratory
 * University of Saskatchewan
 * Saskatoon, Saskatchewan, CANADA
 * eric@skatter.usask.ca
 *
 *  $Id$
 */

#include <bsp.h>

#define CONFIGURE_TEST_NEEDS_CLOCK_DRIVER
#define CONFIGURE_TEST_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_MAXIMUM_SEMAPHORES		20
#define CONFIGURE_MAXIMUM_TIMERS		5
#define CONFIGURE_MAXIMUM_PERIODS		1

#define CONFIGURE_MICROSECONDS_PER_TICK	        1000

#define CONFIGURE_INIT
rtems_task Init (rtems_task_argument argument);

#include <confdefs.h>

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <string.h>

/*
 * Test raw (ICANON=0) input
 */
static void
testRawInput (int vmin, int vtime)
{
	int i;
	struct termios old, new;
	rtems_interval ticksPerSecond, then, now;
	unsigned int msec;
	unsigned long count;
	int nread;
	unsigned char cbuf[100];

	printf ("*** Raw input  VMIN=%d  VTIME=%d ***\n", vmin, vtime);
	rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticksPerSecond);
	i = tcgetattr (fileno (stdin), &old);
	if (i < 0) {
		printf ("tcgetattr failed: %s\n", strerror (errno));
		return;
	}
	new = old;
	new.c_lflag &= ~(ICANON|ECHO|ECHONL|ECHOK|ECHOE|ECHOPRT|ECHOCTL);
	new.c_cc[VMIN] = vmin;
	new.c_cc[VTIME] = vtime;
	i = tcsetattr (fileno (stdin), TCSANOW, &new);
	if (i < 0) {
		printf ("tcsetattr failed: %s\n", strerror (errno));
		return;
	}
	do {
		rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &then);
		count = 0;
		for (;;) {
			nread = read (fileno (stdin), cbuf, sizeof cbuf);
			if (nread < 0) {
				printf ("Read error: %s\n", strerror (errno));
				goto out;
			}
			count++;
			if (nread != 0)
				break;
		}
		rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now);
		msec = (now - then) * 1000 / ticksPerSecond;
		printf ("Count:%-10lu  Interval:%3u.%3.3d  Char:",
					count, msec / 1000, msec % 1000);
		for (i = 0 ; i < nread ; i++)
			printf (" %2.2x", cbuf[i]);
		printf ("\n");
	} while (cbuf[0] != 'q');
    out:
	i = tcsetattr (fileno (stdin), TCSANOW, &old);
	if (i < 0)
		printf ("tcsetattr failed: %s\n", strerror (errno));
	printf ("*** End of Raw input  VMIN=%d  VTIME=%d ***\n", vmin, vtime);
}

/*
 * RTEMS Startup Task
 */
rtems_task
Init (rtems_task_argument ignored)
{
	int i, j;

	printf( "\n\n*** HELLO WORLD TEST ***\n" );
	printf( "Hello World\n" );
	printf( "*** END OF HELLO WORLD TEST ***\n" );

        printf( "\n\ntype 'q' to exit raw input tests\n\n" );

	for (;;) {
		/*
		 * Test  blocking, line-oriented input
		 */
		do {
			printf (">>> ");
			fflush (stdout);
			i = scanf (" %d", &j);
			printf ("Return: %d Value: %d\n", i, j);
		} while (i != 0);

		/*
		 * Consume what scanf rejected
		 */
		while ((i = getchar ()) != '\n')
			if (i == EOF)
				break;

		/*
		 * Test character-oriented input
		 */
		testRawInput (0, 0);
		testRawInput (0, 20);
		testRawInput (5, 0);
		testRawInput (5, 20);
	}
	exit (1);
}