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

4.104.114.84.95
Last change on this file since b9bea9f7 was b9bea9f7, checked in by Joel Sherrill <joel.sherrill@…>, on 04/05/07 at 15:22:58

2007-04-05 Joel Sherrill <joel@…>

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