source: rtems/testsuites/samples/loopback/init.c @ 7b00c2fa

5
Last change on this file since 7b00c2fa was 7b00c2fa, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/17 at 06:56:17

tests: Use <tmacros.h> in all tests

Update #3170.
Update #3199.

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