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

5
Last change on this file since f703e7f was f703e7f, checked in by Sebastian Huber <sebastian.huber@…>, on 10/26/17 at 11:59:05

tests: Move rtems_test_printer definition

Statically initialize it to use printk().

Update #3170.
Update #3199.

  • Property mode set to 100644
File size: 8.4 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
[c499856]4 *  http://www.rtems.org/license/LICENSE.
[5f0cd34]5 */
6
[e313551]7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
[9391f6d]11#include <rtems/test.h>
12
[25b957c1]13#include <bsp.h>
14
[9391f6d]15const char rtems_test_name[] = "LOOPBACK";
16
[b9bea9f7]17#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
18#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
[25b957c1]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))
[07e1780]35#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
[25b957c1]36
37#define CONFIGURE_INIT
38rtems_task Init(rtems_task_argument argument);
39
[9391f6d]40#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
41
[c8fea7a]42#include <rtems/confdefs.h>
[25b957c1]43
44#include <rtems/rtems_bsdnet.h>
45#include <rtems/error.h>
46#include <stdio.h>
47#include <stdarg.h>
[4116e57e]48#include <stdlib.h>
[25b957c1]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 = {
[f4e67ff7]59    NULL,                   /* Network interface */
[25b957c1]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) */
[efb0cf6]70    0,
71    0,
72    0,
73    0,
74    0
[25b957c1]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;
[91435e6]149    socklen_t addrlen;
[25b957c1]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)
[f87ede5]171            if (errno == ENXIO)
172                rtems_task_delete(RTEMS_SELF);
173            else
174                rtems_panic("Can't accept connection: %s", strerror(errno));
[25b957c1]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");
[b767f966]240    rtems_task_delete( RTEMS_SELF );
[25b957c1]241}
242
243/*
244 * RTEMS Startup Task
245 */
246rtems_task
247Init (rtems_task_argument ignored)
248{
249    rtems_status_code sc;
250
[3039e18]251    rtems_print_printer_printf(&rtems_test_printer);
252
[9391f6d]253    rtems_test_begin();
254
[25b957c1]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
[b767f966]288    rtems_task_wake_after(500);
[9391f6d]289    rtems_test_end();
[b767f966]290    exit( 0 );
[25b957c1]291}
Note: See TracBrowser for help on using the repository browser.