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

4.104.115
Last change on this file since efb0cf6 was efb0cf6, checked in by Ralf Corsepius <ralf.corsepius@…>, on 06/04/09 at 06:50:04

2009-06-04 Ralf Corsépius <ralf.corsepius@…>

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