source: rtems/testsuites/samples/loopback/init.c @ 5f0cd34

4.115
Last change on this file since 5f0cd34 was 5f0cd34, checked in by Joel Sherrill <joel.sherrill@…>, on 05/12/12 at 16:12:57

samples - Eliminate missing prototype warnings

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