source: rtems/testsuites/samples/loopback/init.c @ 48cfe68

4.115
Last change on this file since 48cfe68 was a0b1b5ed, checked in by Sebastian Huber <sebastian.huber@…>, on 12/15/14 at 13:19:43

Delete CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM

This define was superfluous, undocumented and used inconsistently.

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