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 | |
---|
15 | const 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 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
29 | |
---|
30 | #define CONFIGURE_INIT_TASK_STACK_SIZE (10*1024) |
---|
31 | #define CONFIGURE_INIT_TASK_PRIORITY 50 |
---|
32 | #define CONFIGURE_INIT_TASK_INITIAL_MODES (RTEMS_PREEMPT | \ |
---|
33 | RTEMS_NO_TIMESLICE | \ |
---|
34 | RTEMS_NO_ASR | \ |
---|
35 | RTEMS_INTERRUPT_LEVEL(0)) |
---|
36 | |
---|
37 | #define CONFIGURE_INIT |
---|
38 | rtems_task Init(rtems_task_argument argument); |
---|
39 | |
---|
40 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
41 | |
---|
42 | #include <rtems/confdefs.h> |
---|
43 | |
---|
44 | #include <rtems/rtems_bsdnet.h> |
---|
45 | #include <rtems/error.h> |
---|
46 | #include <stdio.h> |
---|
47 | #include <stdarg.h> |
---|
48 | #include <stdlib.h> |
---|
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 | |
---|
58 | struct rtems_bsdnet_config rtems_bsdnet_config = { |
---|
59 | NULL, /* Network interface */ |
---|
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) */ |
---|
70 | 0, |
---|
71 | 0, |
---|
72 | 0, |
---|
73 | 0, |
---|
74 | 0 |
---|
75 | }; |
---|
76 | |
---|
77 | /* |
---|
78 | * Thread-safe output routines |
---|
79 | */ |
---|
80 | static rtems_id printMutex; |
---|
81 | static 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 | */ |
---|
95 | static 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 | */ |
---|
116 | static 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 | */ |
---|
146 | static rtems_task serverTask(rtems_task_argument arg) |
---|
147 | { |
---|
148 | int s, s1; |
---|
149 | socklen_t addrlen; |
---|
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) |
---|
171 | rtems_panic("Can't accept connection: %s", strerror(errno)); |
---|
172 | else |
---|
173 | printf("ACCEPTED:%lX\n", ntohl(farAddr.sin_addr.s_addr)); |
---|
174 | spawnTask(workerTask, myPriority, s1); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | /* |
---|
179 | * The real part of the client |
---|
180 | */ |
---|
181 | static rtems_task clientWorker(int arg) |
---|
182 | { |
---|
183 | int s; |
---|
184 | struct sockaddr_in myAddr, farAddr; |
---|
185 | char cbuf[50]; |
---|
186 | int i; |
---|
187 | |
---|
188 | s = socket(AF_INET, SOCK_STREAM, 0); |
---|
189 | if (s < 0) { |
---|
190 | printf("Can't create client socket: %s\n", strerror(errno)); |
---|
191 | return; |
---|
192 | } |
---|
193 | memset(&myAddr, 0, sizeof myAddr); |
---|
194 | myAddr.sin_family = AF_INET; |
---|
195 | myAddr.sin_port = htons(0); |
---|
196 | myAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
197 | if (bind(s, (struct sockaddr *)&myAddr, sizeof myAddr) < 0) { |
---|
198 | printf("Can't bind socket: %s\n", strerror(errno)); |
---|
199 | goto close; |
---|
200 | } |
---|
201 | memset(&farAddr, 0, sizeof farAddr); |
---|
202 | farAddr.sin_family = AF_INET; |
---|
203 | farAddr.sin_port = htons(1234); |
---|
204 | farAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
205 | printf("Connect to server.\n"); |
---|
206 | if (connect(s, (struct sockaddr *)&farAddr, sizeof farAddr) < 0) { |
---|
207 | printf("Can't connect to server: %s\n", strerror(errno)); |
---|
208 | goto close; |
---|
209 | } |
---|
210 | rtems_task_wake_after(20); /* Simulate client delay */ |
---|
211 | i = sprintf(cbuf, "Hi there, server (%d).", arg); |
---|
212 | i++; /* Send the '\0', too */ |
---|
213 | printf("Write %d-byte message to server.\n", i); |
---|
214 | if (write(s, cbuf, i) < 0) { |
---|
215 | printf("Can't write to server: %s\n", strerror(errno)); |
---|
216 | goto close; |
---|
217 | } |
---|
218 | if ((i = read(s, cbuf, sizeof cbuf)) < 0) { |
---|
219 | printf("Can't read from server: %s\n", strerror(errno)); |
---|
220 | goto close; |
---|
221 | } |
---|
222 | printf("Read %d from server: %.*s\n", i, i, cbuf); |
---|
223 | rtems_task_wake_after(20); /* Simulate client delay */ |
---|
224 | close: |
---|
225 | printf("Client closing connection.\n"); |
---|
226 | if (close(s) < 0) |
---|
227 | printf("Can't close client task socket: %s\n", strerror(errno)); |
---|
228 | } |
---|
229 | |
---|
230 | /* |
---|
231 | * Client Task |
---|
232 | */ |
---|
233 | static rtems_task clientTask(rtems_task_argument arg) |
---|
234 | { |
---|
235 | clientWorker(arg); |
---|
236 | printf("Client task terminating.\n"); |
---|
237 | rtems_task_delete( RTEMS_SELF ); |
---|
238 | } |
---|
239 | |
---|
240 | /* |
---|
241 | * RTEMS Startup Task |
---|
242 | */ |
---|
243 | rtems_task |
---|
244 | Init (rtems_task_argument ignored) |
---|
245 | { |
---|
246 | rtems_status_code sc; |
---|
247 | |
---|
248 | rtems_test_begin(); |
---|
249 | |
---|
250 | sc = rtems_semaphore_create(rtems_build_name('P','m','t','x'), |
---|
251 | 1, |
---|
252 | RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE|RTEMS_INHERIT_PRIORITY| |
---|
253 | RTEMS_NO_PRIORITY_CEILING|RTEMS_LOCAL, |
---|
254 | 0, |
---|
255 | &printMutex); |
---|
256 | if (sc != RTEMS_SUCCESSFUL) |
---|
257 | rtems_panic("Can't create printf mutex:", rtems_status_text(sc)); |
---|
258 | printf("\"Network\" initializing!\n"); |
---|
259 | rtems_bsdnet_initialize_network(); |
---|
260 | printf("\"Network\" initialized!\n"); |
---|
261 | |
---|
262 | printf("Try running client with no server present.\n"); |
---|
263 | printf("Should fail with `connection refused'.\n"); |
---|
264 | clientWorker(0); |
---|
265 | |
---|
266 | printf("\nStart server.\n"); |
---|
267 | spawnTask(serverTask, 150, 0); |
---|
268 | |
---|
269 | printf("\nTry running client with server present.\n"); |
---|
270 | spawnTask(clientTask, 120, 1); |
---|
271 | rtems_task_wake_after(500); |
---|
272 | |
---|
273 | printf("\nTry running two clients.\n"); |
---|
274 | spawnTask(clientTask, 120, 2); |
---|
275 | spawnTask(clientTask, 120, 3); |
---|
276 | rtems_task_wake_after(500); |
---|
277 | |
---|
278 | printf("\nTry running three clients.\n"); |
---|
279 | spawnTask(clientTask, 120, 4); |
---|
280 | spawnTask(clientTask, 120, 5); |
---|
281 | spawnTask(clientTask, 120, 6); |
---|
282 | |
---|
283 | rtems_task_wake_after(500); |
---|
284 | rtems_test_end(); |
---|
285 | exit( 0 ); |
---|
286 | } |
---|