source: rtems/testsuites/samples/loopback/init.c @ 07e1780

5
Last change on this file since 07e1780 was 07e1780, checked in by Sebastian Huber <sebastian.huber@…>, on 07/18/17 at 11:07:26

tests: Use floating point task

These tests directly or indirectly use fprintf(), etc. which may use the
floating point unit.

Update #3076.

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[5f0cd34]1/*
2 *  The license and distribution terms for this file may be
3 *  found in the file LICENSE in this distribution or at
[c499856]4 *  http://www.rtems.org/license/LICENSE.
[5f0cd34]5 */
6
[e313551]7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
[9391f6d]11#include <rtems/test.h>
12
[25b957c1]13#include <bsp.h>
14
[9391f6d]15const char rtems_test_name[] = "LOOPBACK";
[3039e18]16rtems_printer rtems_test_printer;
[9391f6d]17
[b9bea9f7]18#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
19#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
[25b957c1]20#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
21
22#define CONFIGURE_EXECUTIVE_RAM_SIZE        (512*1024)
23#define CONFIGURE_MAXIMUM_SEMAPHORES        20
24#define CONFIGURE_MAXIMUM_MESSAGE_QUEUES    20
25#define CONFIGURE_MAXIMUM_TASKS            20
26
27#define CONFIGURE_MICROSECONDS_PER_TICK    10000
28#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 50
29
30#define CONFIGURE_INIT_TASK_STACK_SIZE    (10*1024)
31#define CONFIGURE_INIT_TASK_PRIORITY    50
32#define CONFIGURE_INIT_TASK_INITIAL_MODES (RTEMS_PREEMPT | \
33                                           RTEMS_NO_TIMESLICE | \
34                                           RTEMS_NO_ASR | \
35                                           RTEMS_INTERRUPT_LEVEL(0))
[07e1780]36#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
[25b957c1]37
38#define CONFIGURE_INIT
39rtems_task Init(rtems_task_argument argument);
40
[9391f6d]41#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
42
[c8fea7a]43#include <rtems/confdefs.h>
[25b957c1]44
45#include <rtems/rtems_bsdnet.h>
46#include <rtems/error.h>
47#include <stdio.h>
48#include <stdarg.h>
[4116e57e]49#include <stdlib.h>
[25b957c1]50#include <string.h>
51#include <errno.h>
52#include <sys/socket.h>
53#include <netinet/in.h>
54
55/*
56 * Network configuration
57 */
58
59struct rtems_bsdnet_config rtems_bsdnet_config = {
[f4e67ff7]60    NULL,                   /* Network interface */
[25b957c1]61    NULL,                   /* Use fixed network configuration */
62    0,                      /* Default network task priority */
63    0,                      /* Default mbuf capacity */
64    0,                      /* Default mbuf cluster capacity */
65    "testSystem",           /* Host name */
66    "nowhere.com",          /* Domain name */
67    "127.0.0.1",            /* Gateway */
68    "127.0.0.1",            /* Log host */
69    {"127.0.0.1" },         /* Name server(s) */
70    {"127.0.0.1" },         /* NTP server(s) */
[efb0cf6]71    0,
72    0,
73    0,
74    0,
75    0
[25b957c1]76};
77
78/*
79 * Thread-safe output routines
80 */
81static rtems_id printMutex;
82static void printSafe(const char *fmt, ...)
83{
84    va_list args;
85    va_start(args, fmt);
86    rtems_semaphore_obtain(printMutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
87    vprintf(fmt, args);
88    rtems_semaphore_release(printMutex);
89    va_end(args);
90}
91#define printf printSafe
92
93/*
94 * Spawn a task
95 */
96static void spawnTask(rtems_task_entry entryPoint, rtems_task_priority priority, rtems_task_argument arg)
97{
98    rtems_status_code sc;
99    rtems_id tid;
100
101    sc = rtems_task_create(rtems_build_name('t','a','s','k'),
102            priority,
103            RTEMS_MINIMUM_STACK_SIZE+(8*1024),
104            RTEMS_PREEMPT|RTEMS_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
105            RTEMS_FLOATING_POINT|RTEMS_LOCAL,
106            &tid);
107    if (sc != RTEMS_SUCCESSFUL)
108        rtems_panic("Can't create task: %s", rtems_status_text(sc));
109    sc = rtems_task_start(tid, entryPoint, arg);
110    if (sc != RTEMS_SUCCESSFUL)
111        rtems_panic("Can't start task: %s", rtems_status_text(sc));
112}
113
114/*
115 * Server subtask
116 */
117static rtems_task workerTask(rtems_task_argument arg)
118{
119    int s = arg;
120    char msg[80];
121    char reply[100];
122    int i;
123
124    for (;;) {
125        if ((i = read(s, msg, sizeof msg)) < 0) {
126            printf("Server couldn't read message from client: %s\n", strerror(errno));
127            break;
128        }
129        if (i == 0)
130            break;
131        rtems_task_wake_after(20); /* Simulate some processing delay */
132        i = sprintf(reply, "Server received %d (%s)", i, msg);
133        if ((i = write(s, reply, i+1)) < 0) {
134            printf("Server couldn't write message to client: %s\n", strerror(errno));
135            break;
136        }
137    }
138    if (close(s) < 0)
139        printf("Can't close worker task socket: %s\n", strerror(errno));
140    printf("Worker task terminating.\n");
141    rtems_task_delete(RTEMS_SELF);
142}
143
144/*
145 * Server Task
146 */
147static rtems_task serverTask(rtems_task_argument arg)
148{
149    int s, s1;
[91435e6]150    socklen_t addrlen;
[25b957c1]151        struct sockaddr_in myAddr, farAddr;
152    rtems_task_priority myPriority;
153
154        printf("Create socket.\n");
155        s = socket(AF_INET, SOCK_STREAM, 0);
156        if (s < 0)
157                rtems_panic("Can't create socket: %s\n", strerror(errno));
158    memset(&myAddr, 0, sizeof myAddr);
159        myAddr.sin_family = AF_INET;
160        myAddr.sin_port = htons(1234);
161        myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
162        printf("Bind socket.\n");
163        if (bind(s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0)
164                rtems_panic("Can't bind socket: %s\n", strerror(errno));
165        if (listen(s, 5) < 0)
166                printf("Can't listen on socket: %s\n", strerror(errno));
167    rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &myPriority);
168    for(;;) {
169        addrlen = sizeof farAddr;
170        s1 = accept(s, (struct sockaddr *)&farAddr, &addrlen);
171        if (s1 < 0)
[f87ede5]172            if (errno == ENXIO)
173                rtems_task_delete(RTEMS_SELF);
174            else
175                rtems_panic("Can't accept connection: %s", strerror(errno));
[25b957c1]176        else
177            printf("ACCEPTED:%lX\n", ntohl(farAddr.sin_addr.s_addr));
178        spawnTask(workerTask, myPriority, s1);
179    }
180}
181
182/*
183 * The real part of the client
184 */
185static rtems_task clientWorker(int arg)
186{
187    int s;
188        struct sockaddr_in myAddr, farAddr;
189    char cbuf[50];
190    int i;
191
192        s = socket(AF_INET, SOCK_STREAM, 0);
193        if (s < 0) {
194                printf("Can't create client socket: %s\n", strerror(errno));
195        return;
196    }
197    memset(&myAddr, 0, sizeof myAddr);
198        myAddr.sin_family = AF_INET;
199        myAddr.sin_port = htons(0);
200        myAddr.sin_addr.s_addr = htonl(INADDR_ANY);
201        if (bind(s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0) {
202                printf("Can't bind socket: %s\n", strerror(errno));
203        goto close;
204    }
205    memset(&farAddr, 0, sizeof farAddr);
206        farAddr.sin_family = AF_INET;
207        farAddr.sin_port = htons(1234);
208        farAddr.sin_addr.s_addr = htonl(INADDR_ANY);
209        printf("Connect to server.\n");
210        if (connect(s, (struct sockaddr *)&farAddr, sizeof farAddr) < 0) {
211                printf("Can't connect to server: %s\n", strerror(errno));
212        goto close;
213    }
214    rtems_task_wake_after(20); /* Simulate client delay */
215    i = sprintf(cbuf, "Hi there, server (%d).", arg);
216    i++;    /* Send the '\0', too */
217    printf("Write %d-byte message to server.\n", i);
218    if (write(s, cbuf, i) < 0) {
219                printf("Can't write to server: %s\n", strerror(errno));
220        goto close;
221    }
222    if ((i = read(s, cbuf, sizeof cbuf)) < 0) {
223                printf("Can't read from server: %s\n", strerror(errno));
224        goto close;
225    }
226    printf("Read %d from server: %.*s\n", i, i, cbuf);
227    rtems_task_wake_after(20); /* Simulate client delay */
228  close:
229    printf("Client closing connection.\n");
230    if (close(s) < 0)
231        printf("Can't close client task socket: %s\n", strerror(errno));
232}
233
234/*
235 * Client Task
236 */
237static rtems_task clientTask(rtems_task_argument arg)
238{
239    clientWorker(arg);
240    printf("Client task terminating.\n");
[b767f966]241    rtems_task_delete( RTEMS_SELF );
[25b957c1]242}
243
244/*
245 * RTEMS Startup Task
246 */
247rtems_task
248Init (rtems_task_argument ignored)
249{
250    rtems_status_code sc;
251
[3039e18]252    rtems_print_printer_printf(&rtems_test_printer);
253
[9391f6d]254    rtems_test_begin();
255
[25b957c1]256    sc = rtems_semaphore_create(rtems_build_name('P','m','t','x'),
257                1,
258                RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE|RTEMS_INHERIT_PRIORITY|
259                                    RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL,
260                0,
261                &printMutex);
262    if (sc != RTEMS_SUCCESSFUL)
263        rtems_panic("Can't create printf mutex:", rtems_status_text(sc));
264    printf("\"Network\" initializing!\n");
265    rtems_bsdnet_initialize_network();
266    printf("\"Network\" initialized!\n");
267
268    printf("Try running client with no server present.\n");
269    printf("Should fail with `connection refused'.\n");
270    clientWorker(0);
271
272    printf("\nStart server.\n");
273    spawnTask(serverTask, 150, 0);
274
275    printf("\nTry running client with server present.\n");
276    spawnTask(clientTask, 120, 1);
277    rtems_task_wake_after(500);
278
279    printf("\nTry running two clients.\n");
280    spawnTask(clientTask, 120, 2);
281    spawnTask(clientTask, 120, 3);
282    rtems_task_wake_after(500);
283
284    printf("\nTry running three clients.\n");
285    spawnTask(clientTask, 120, 4);
286    spawnTask(clientTask, 120, 5);
287    spawnTask(clientTask, 120, 6);
288
[b767f966]289    rtems_task_wake_after(500);
[9391f6d]290    rtems_test_end();
[b767f966]291    exit( 0 );
[25b957c1]292}
Note: See TracBrowser for help on using the repository browser.