1 | /* Init |
---|
2 | * |
---|
3 | * This routine is the initialization task for this test program. |
---|
4 | * |
---|
5 | * Don't forget to change the IP addresses |
---|
6 | */ |
---|
7 | |
---|
8 | #define TEST_INIT |
---|
9 | |
---|
10 | #define CONFIGURE_EXECUTIVE_RAM_SIZE (512*1024) |
---|
11 | #define CONFIGURE_MAXIMUM_SEMAPHORES 20 |
---|
12 | #define CONFIGURE_MAXIMUM_TASKS 20 |
---|
13 | #define CONFIGURE_MAXIMUM_TIMERS 10 |
---|
14 | #define CONFIGURE_MAXIMUM_PERIODS 1 |
---|
15 | |
---|
16 | #include "system.h" |
---|
17 | |
---|
18 | #include <rtems/error.h> |
---|
19 | #include <ka9q/rtems_ka9q.h> |
---|
20 | #include <ka9q/netuser.h> |
---|
21 | #include <ka9q/socket.h> |
---|
22 | #include <netinet/in.h> |
---|
23 | #include <time.h> |
---|
24 | |
---|
25 | #define ARGUMENT 0 |
---|
26 | |
---|
27 | /* define and include for socket test */ |
---|
28 | /*#define PINKFLOYD */ |
---|
29 | |
---|
30 | /*#define SERVER*/ |
---|
31 | #define CLIENT |
---|
32 | |
---|
33 | /*#define UDP*/ /* if enabled, testing UDP socket else TCP socket */ |
---|
34 | |
---|
35 | #include "buffer.h" |
---|
36 | |
---|
37 | |
---|
38 | #define DEFAULT_IP_SERVER "194.2.81.61" |
---|
39 | #define DEFAULT_LISTEN_IP_SERVER "194.2.81.126" |
---|
40 | #define DEFAULT_IP_CLIENT "194.2.81.126" |
---|
41 | #define DEFAULT_LISTEN_IP_CLIENT "194.2.81.61" |
---|
42 | |
---|
43 | rtems_task Init( |
---|
44 | rtems_task_argument argument |
---|
45 | ) |
---|
46 | { |
---|
47 | unsigned int oldPri; |
---|
48 | rtems_name task_name; |
---|
49 | rtems_id tid; |
---|
50 | rtems_status_code status; |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | printf("\n\n*** SAMPLE SINGLE PROCESSOR APPLICATION ***\n\r" ); |
---|
55 | printf("Creating and starting an application task\n\r" ); |
---|
56 | fflush(stdout); |
---|
57 | |
---|
58 | rtems_task_set_priority (RTEMS_SELF, 30, &oldPri); |
---|
59 | |
---|
60 | rtems_ka9q_start (10); |
---|
61 | |
---|
62 | #ifdef PINKFLOYD |
---|
63 | if (rtems_ka9q_execute_command("attach rtems " |
---|
64 | "broadcast n " |
---|
65 | "ip 194.2.81.127 " |
---|
66 | "irno 5 " |
---|
67 | "bpar 0xd0000 " |
---|
68 | "port 0x240 " |
---|
69 | "ether 00:E0:29:12:3f:85 ")) |
---|
70 | #else |
---|
71 | if (rtems_ka9q_execute_command("attach rtems " |
---|
72 | "broadcast y " |
---|
73 | "ip 194.2.81.61 " |
---|
74 | "irno 3 " |
---|
75 | "bpar 0xcc000 " |
---|
76 | "port 0x260 " |
---|
77 | "ether 00:E0:29:12:40:77 ")) |
---|
78 | #endif |
---|
79 | rtems_panic ("Can't attach Ethernet driver."); |
---|
80 | |
---|
81 | if (rtems_ka9q_execute_command("ifconfig rtems " |
---|
82 | "broadcast 255.255.255.255")) |
---|
83 | rtems_panic ("Can't configure Ethernet driver."); |
---|
84 | |
---|
85 | if (rtems_ka9q_execute_command ("arp add 255.255.255.255 " |
---|
86 | "ether FF:FF:FF:FF:FF:FF")) |
---|
87 | rtems_panic ("Can't add broadcast entry to ARP table."); |
---|
88 | |
---|
89 | if (rtems_ka9q_execute_command ("ifconfig rtems " |
---|
90 | "netmask 255.255.255.0")) |
---|
91 | rtems_panic ("Can't set netmask."); |
---|
92 | |
---|
93 | if (rtems_ka9q_execute_command ("route add default rtems")) |
---|
94 | rtems_panic ("Can't add default route."); |
---|
95 | |
---|
96 | rtems_ka9q_execute_command ("route"); |
---|
97 | |
---|
98 | if (rtems_ka9q_execute_command ("arp gratuitous rtems")) |
---|
99 | rtems_panic ("Can't send gratuitous ARP.\n"); |
---|
100 | |
---|
101 | printf("End of INIT\n\r"); |
---|
102 | fflush(stdout); |
---|
103 | |
---|
104 | #ifdef SERVER |
---|
105 | printf("Creation of SERVER SocketTask\n\r"); |
---|
106 | fflush(stdout); |
---|
107 | |
---|
108 | task_name = rtems_build_name( 'T', 'A', '1', ' ' ); |
---|
109 | if ((status = rtems_task_create( task_name, 20, RTEMS_MINIMUM_STACK_SIZE, |
---|
110 | RTEMS_INTERRUPT_LEVEL(0), |
---|
111 | RTEMS_DEFAULT_ATTRIBUTES, &tid )) |
---|
112 | != RTEMS_SUCCESSFUL){ |
---|
113 | printf("status = %d\n",status); |
---|
114 | rtems_panic ("Can't create task.\n"); |
---|
115 | } |
---|
116 | |
---|
117 | #ifdef UDP |
---|
118 | status = rtems_task_start(tid, testUdpServerSocket, 0); |
---|
119 | #else |
---|
120 | status = rtems_task_start(tid, testTcpServerSocket, 0); |
---|
121 | #endif |
---|
122 | |
---|
123 | #endif /* SERVER */ |
---|
124 | |
---|
125 | #ifdef CLIENT |
---|
126 | printf("Creation of CLIENT SocketTask\n\r"); |
---|
127 | fflush(stdout); |
---|
128 | |
---|
129 | task_name = rtems_build_name( 'T', 'A', '2', ' ' ); |
---|
130 | if ((status = rtems_task_create( task_name, 20, RTEMS_MINIMUM_STACK_SIZE, |
---|
131 | RTEMS_INTERRUPT_LEVEL(0), |
---|
132 | RTEMS_DEFAULT_ATTRIBUTES, &tid )) |
---|
133 | != RTEMS_SUCCESSFUL){ |
---|
134 | printf("status = %d\n",status); |
---|
135 | rtems_panic ("Can't create task.\n"); |
---|
136 | } |
---|
137 | |
---|
138 | #ifdef UDP |
---|
139 | status = rtems_task_start(tid, testUdpClientSocket, 0); |
---|
140 | #else |
---|
141 | status = rtems_task_start(tid, testTcpClientSocket, 0); |
---|
142 | #endif |
---|
143 | |
---|
144 | #endif /* CLIENT */ |
---|
145 | |
---|
146 | status = rtems_task_delete( RTEMS_SELF ); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | rtems_task testUdpServerSocket( |
---|
152 | rtems_task_argument argument |
---|
153 | ){ |
---|
154 | |
---|
155 | int sd; |
---|
156 | struct sockaddr_in server, from; |
---|
157 | int fromlen; |
---|
158 | char *tabChar; |
---|
159 | int rc; |
---|
160 | unsigned long msgId; |
---|
161 | char *server_name= "localhost"; |
---|
162 | unsigned short port; |
---|
163 | |
---|
164 | /****************************************/ |
---|
165 | /* Now, we are going to test the socket */ |
---|
166 | /****************************************/ |
---|
167 | |
---|
168 | printf("Task testServerSocket : created\n\r"); |
---|
169 | fflush(stdout); |
---|
170 | |
---|
171 | port = DEFAULT_PORT_SERVER; |
---|
172 | server_name = DEFAULT_IP_SERVER; |
---|
173 | |
---|
174 | bzero((void *)(&server), sizeof(server)); |
---|
175 | server.sin_family = AF_INET; |
---|
176 | server.sin_addr.s_addr = aton (server_name); |
---|
177 | |
---|
178 | server.sin_port = htons(port); |
---|
179 | |
---|
180 | sd = socket(AF_INET,SOCK_DGRAM, 0); |
---|
181 | if (sd == -1) { |
---|
182 | printf(" socket failed errno = %d\n", errno); |
---|
183 | return ; |
---|
184 | } |
---|
185 | |
---|
186 | rc = bind(sd, (struct sockaddr *)&server, sizeof(server)) ; |
---|
187 | if (rc == -1) { |
---|
188 | printf(" bind failed errno = %d\n", errno); |
---|
189 | return ; |
---|
190 | } |
---|
191 | |
---|
192 | tabChar = AllocBuffer(); |
---|
193 | |
---|
194 | if(!tabChar) { |
---|
195 | printf("malloc failed ...\n"); |
---|
196 | return ; |
---|
197 | } |
---|
198 | |
---|
199 | bzero((void *)(&from), sizeof(from)); |
---|
200 | from.sin_family = AF_INET; |
---|
201 | from.sin_addr.s_addr = aton (DEFAULT_LISTEN_IP_SERVER); |
---|
202 | fromlen = sizeof(from); |
---|
203 | |
---|
204 | msgId = 0; |
---|
205 | |
---|
206 | for(;;){ |
---|
207 | rc=recvfrom(sd, tabChar, SIZE_MAX_RCV_BUFFER, 0, (struct sockaddr *)&from, &fromlen); |
---|
208 | |
---|
209 | if (rc < 0) { |
---|
210 | printf("\n Recvfrom error %d\n\n",errno); |
---|
211 | fflush(stdout); |
---|
212 | } |
---|
213 | else{ |
---|
214 | msgId = CheckBuffer(tabChar); |
---|
215 | } |
---|
216 | if (msgId == 0) |
---|
217 | break ; |
---|
218 | } |
---|
219 | |
---|
220 | FreeBuffer(tabChar); |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | rtems_task testUdpClientSocket( |
---|
226 | rtems_task_argument argument |
---|
227 | ){ |
---|
228 | |
---|
229 | int sd; |
---|
230 | struct sockaddr_in server; |
---|
231 | char *tabChar; |
---|
232 | int i; |
---|
233 | char *server_name= "localhost"; |
---|
234 | unsigned short port; |
---|
235 | unsigned long lenChar; |
---|
236 | struct timespec timeToWait, timeRem; |
---|
237 | |
---|
238 | /****************************************/ |
---|
239 | /* Now, we are going to test the socket */ |
---|
240 | /****************************************/ |
---|
241 | |
---|
242 | printf("Task testClientSocket : created\n\r"); |
---|
243 | fflush(stdout); |
---|
244 | |
---|
245 | port = DEFAULT_PORT_CLIENT; |
---|
246 | server_name = DEFAULT_IP_CLIENT; |
---|
247 | |
---|
248 | bzero((void *)(&server), sizeof(server)); |
---|
249 | server.sin_family = AF_INET; |
---|
250 | server.sin_addr.s_addr = aton (server_name); |
---|
251 | |
---|
252 | server.sin_port = htons(port); |
---|
253 | |
---|
254 | sd = socket(AF_INET,SOCK_DGRAM, 0); |
---|
255 | if (sd == -1) { |
---|
256 | printf(" socket failed errno = %d\n", errno); |
---|
257 | return ; |
---|
258 | } |
---|
259 | |
---|
260 | while(1) { |
---|
261 | tabChar = BuildBuffer(); |
---|
262 | lenChar = *((unsigned long *)(tabChar)); |
---|
263 | i = sendto(sd, tabChar, lenChar, 0, (struct sockaddr *)&server, sizeof(server)); |
---|
264 | |
---|
265 | if (i == -1) { |
---|
266 | fprintf(stderr,"sendto() failed (at least local errors): %d\n", errno); |
---|
267 | return ; |
---|
268 | } |
---|
269 | |
---|
270 | timeToWait.tv_sec = 1; |
---|
271 | timeToWait.tv_nsec = 0; |
---|
272 | nanosleep(&timeToWait, &timeRem); |
---|
273 | FreeBuffer(tabChar); |
---|
274 | } |
---|
275 | |
---|
276 | } |
---|
277 | |
---|
278 | rtems_task testTcpServerSocket( |
---|
279 | rtems_task_argument argument |
---|
280 | ){ |
---|
281 | int sd, psd; |
---|
282 | struct sockaddr_in server; |
---|
283 | struct sockaddr_in from; |
---|
284 | int fromlen; |
---|
285 | char *tabChar; |
---|
286 | int rc; |
---|
287 | int i; |
---|
288 | char *server_name= "localhost"; |
---|
289 | unsigned short port; |
---|
290 | |
---|
291 | |
---|
292 | /****************************************/ |
---|
293 | /* Now, we are going to test the socket */ |
---|
294 | /****************************************/ |
---|
295 | |
---|
296 | printf("Task testServerSocket : created\n\r"); |
---|
297 | fflush(stdout); |
---|
298 | |
---|
299 | port = DEFAULT_PORT_SERVER; |
---|
300 | server_name = DEFAULT_IP_SERVER; |
---|
301 | |
---|
302 | bzero((void *)(&server), sizeof(server)); |
---|
303 | server.sin_family = AF_INET; |
---|
304 | server.sin_addr.s_addr = aton (server_name); |
---|
305 | |
---|
306 | server.sin_port = htons(port); |
---|
307 | |
---|
308 | sd = socket (AF_INET,SOCK_STREAM,0); |
---|
309 | printf("socket() returns <%d>\n", sd); |
---|
310 | fflush(stdout); |
---|
311 | |
---|
312 | if (sd<0) { |
---|
313 | printf("socket() call failure: opening stream socket\n"); |
---|
314 | fflush(stdout); |
---|
315 | exit(-1); |
---|
316 | } |
---|
317 | |
---|
318 | rc = bind( sd, (struct sockaddr *)&server, sizeof(server) ) ; |
---|
319 | if (rc == -1) { |
---|
320 | printf("bind failed errno = %d\n", errno); |
---|
321 | fflush(stdout); |
---|
322 | close_s(sd); |
---|
323 | return; |
---|
324 | } |
---|
325 | printf("bind() returns <%d>\n", rc); |
---|
326 | fflush(stdout); |
---|
327 | |
---|
328 | printf("Server Port is: %d\n", ntohs(server.sin_port)); |
---|
329 | fflush(stdout); |
---|
330 | rc = listen(sd,5); |
---|
331 | printf("listen() returns <%d>\n", rc); |
---|
332 | fflush(stdout); |
---|
333 | fromlen = sizeof(from); |
---|
334 | |
---|
335 | tabChar = AllocBuffer(); |
---|
336 | if(!tabChar) { |
---|
337 | printf("malloc failed ...\n"); |
---|
338 | fflush(stdout); |
---|
339 | return; |
---|
340 | } |
---|
341 | |
---|
342 | psd = accept(sd,(struct sockaddr *)&from, &fromlen); |
---|
343 | printf("accept() returns <%d>\n", psd); |
---|
344 | fflush(stdout); |
---|
345 | |
---|
346 | for(;;){ |
---|
347 | rc = recv(psd, tabChar, SIZE_MAX_RCV_BUFFER, 0 ); |
---|
348 | |
---|
349 | if (rc == -1) { |
---|
350 | printf("recv() failed errno = %d\n", errno); |
---|
351 | fflush(stdout); |
---|
352 | close_s(sd); |
---|
353 | return; |
---|
354 | } |
---|
355 | |
---|
356 | if (rc == 0) { |
---|
357 | continue; |
---|
358 | } |
---|
359 | |
---|
360 | if (CheckBuffer(tabChar) == 0){ |
---|
361 | printf("Received %d bytes, data [%s] from client\n",rc,tabChar); |
---|
362 | printf("Packet received:<%d> bytes\n", rc); |
---|
363 | printf("Received:<"); |
---|
364 | fflush(stdout); |
---|
365 | tabChar += 8; |
---|
366 | for (i=0; i<rc ; i++) printf("%c",tabChar[i]); |
---|
367 | printf(">\n"); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | FreeBuffer(tabChar); |
---|
372 | |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | rtems_task testTcpClientSocket( |
---|
377 | rtems_task_argument argument |
---|
378 | ){ |
---|
379 | int sd; |
---|
380 | struct sockaddr_in server; |
---|
381 | /*struct hostent *hp;*/ |
---|
382 | struct sockaddr_in from; |
---|
383 | int fromlen; |
---|
384 | int i,retval; |
---|
385 | char *tabChar; |
---|
386 | char *server_name= "localhost"; |
---|
387 | unsigned short port; |
---|
388 | unsigned long lenChar; |
---|
389 | struct timespec timeToWait, timeRem; |
---|
390 | |
---|
391 | /****************************************/ |
---|
392 | /* Now, we are going to test the socket */ |
---|
393 | /****************************************/ |
---|
394 | |
---|
395 | printf("Task testServerSocket : created\n\r"); |
---|
396 | fflush(stdout); |
---|
397 | |
---|
398 | port = DEFAULT_PORT_CLIENT; |
---|
399 | server_name = DEFAULT_IP_CLIENT; |
---|
400 | |
---|
401 | bzero((void *)(&server), sizeof(server)); |
---|
402 | |
---|
403 | sd = socket (AF_INET,SOCK_STREAM,0); |
---|
404 | printf("socket() result:<%d>\n", sd); |
---|
405 | fflush(stdout); |
---|
406 | |
---|
407 | if (sd<0) { |
---|
408 | printf("opening stream socket\n"); |
---|
409 | fflush(stdout); |
---|
410 | return; |
---|
411 | } |
---|
412 | |
---|
413 | server.sin_addr.s_addr = aton (server_name); |
---|
414 | |
---|
415 | server.sin_family = AF_INET; |
---|
416 | server.sin_port = htons(port); |
---|
417 | |
---|
418 | /* Connect to TCP/SERVER */ |
---|
419 | if ( (retval = connect(sd, (struct sockaddr *)&server, sizeof(server))) < 0 ) { |
---|
420 | close_s(sd); |
---|
421 | perror("connecting stream socket"); |
---|
422 | fflush(stdout); |
---|
423 | exit(0); |
---|
424 | } |
---|
425 | printf("connect() result:<%d>\n", retval); |
---|
426 | fflush(stdout); |
---|
427 | fromlen = sizeof(from); |
---|
428 | if ( (retval = getpeername(sd,(struct sockaddr *)&from,&fromlen)) <0){ |
---|
429 | perror("could't get peername\n"); |
---|
430 | fflush(stdout); |
---|
431 | exit(1); |
---|
432 | } |
---|
433 | printf("getpeername() result:<%d>\n", retval); |
---|
434 | printf("Connected to TCP/Server:"); |
---|
435 | printf("%s:%d\n", inet_ntoa((int32)(from.sin_addr.s_addr)), ntohs(from.sin_port)); |
---|
436 | fflush(stdout); |
---|
437 | |
---|
438 | printf("Now, let's send packets..."); |
---|
439 | fflush(stdout); |
---|
440 | |
---|
441 | while(1) { |
---|
442 | |
---|
443 | tabChar = BuildBuffer(); |
---|
444 | lenChar = *((unsigned long *)(tabChar)); |
---|
445 | |
---|
446 | i = send(sd, tabChar, lenChar, 0); |
---|
447 | if (i == -1) |
---|
448 | { |
---|
449 | fprintf(stderr,"send() failed (at least locally detected errors)\n"); |
---|
450 | fflush(stdout); |
---|
451 | return; |
---|
452 | } |
---|
453 | |
---|
454 | timeToWait.tv_sec = 1; |
---|
455 | timeToWait.tv_nsec = 0; |
---|
456 | nanosleep(&timeToWait, &timeRem); |
---|
457 | FreeBuffer(tabChar); |
---|
458 | |
---|
459 | } |
---|
460 | |
---|
461 | close_s(sd); |
---|
462 | } |
---|
463 | |
---|