source: rtems/testsuites/samples/loopback/init.c @ b682f4cb

5
Last change on this file since b682f4cb was 3039e18, checked in by Chris Johns <chrisj@…>, on 05/26/16 at 05:51:44

testsuite: Fix networking samples to use the RTEMS printer.

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