1 | #include <rtems.h>
|
---|
2 | #include <rtems/rtems_bsdnet.h>
|
---|
3 |
|
---|
4 | #include <sys/socket.h>
|
---|
5 | #include <sys/sockio.h>
|
---|
6 | #include <netinet/in.h>
|
---|
7 | #include <arpa/inet.h>
|
---|
8 | #include <fcntl.h>
|
---|
9 | #include <sys/filio.h>
|
---|
10 |
|
---|
11 | #include <errno.h>
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <string.h>
|
---|
14 |
|
---|
15 | #define MSGBUFSIZE 256
|
---|
16 | #define INETADDR "127.0.0.1"
|
---|
17 | #define PORT 1234
|
---|
18 | #define HOSTNAME "hostname"
|
---|
19 | #define DOMAINNAME "domainname"
|
---|
20 |
|
---|
21 | /* Define O_NDELAY here if commented out in sys/fcntl.h */
|
---|
22 | #ifndef O_NDELAY
|
---|
23 | #define O_NDELAY _FNBIO
|
---|
24 | #endif
|
---|
25 |
|
---|
26 | #define ERR(format, ...) \
|
---|
27 | printf("%s%i:" format, __FILE__, __LINE__, ##__VA_ARGS__)
|
---|
28 |
|
---|
29 | #define SEND_EVENT 0x00000001
|
---|
30 |
|
---|
31 | rtems_id send_tid;
|
---|
32 | rtems_id recv_tid;
|
---|
33 |
|
---|
34 | int recv_listen(int fd)
|
---|
35 | {
|
---|
36 | struct sockaddr_in peer_addr;
|
---|
37 | int peer_addr_len = sizeof(peer_addr);
|
---|
38 | int cnt;
|
---|
39 | int rc;
|
---|
40 | char buf[MSGBUFSIZE];
|
---|
41 |
|
---|
42 | rtems_event_send(send_tid, SEND_EVENT);
|
---|
43 |
|
---|
44 | for(cnt=1;;cnt++)
|
---|
45 | {
|
---|
46 | rc = recvfrom(fd, buf, MSGBUFSIZE, 0,
|
---|
47 | (struct sockaddr *) &peer_addr, &peer_addr_len);
|
---|
48 | if(rc>=0)
|
---|
49 | {
|
---|
50 | return cnt;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if((errno==EAGAIN) || (errno==EWOULDBLOCK)) {
|
---|
54 | rtems_task_wake_after(TOD_MILLISECONDS_TO_TICKS(10));
|
---|
55 | }
|
---|
56 | else {
|
---|
57 | return -1;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | rtems_task recv_thread(rtems_task_argument ignored)
|
---|
63 | {
|
---|
64 | int rc;
|
---|
65 | int fd;
|
---|
66 | struct sockaddr_in listen_addr;
|
---|
67 | int onoff;
|
---|
68 |
|
---|
69 | (void) ignored;
|
---|
70 |
|
---|
71 | /* Open socket */
|
---|
72 | fd = socket(AF_INET,SOCK_DGRAM,0);
|
---|
73 | if (fd<0) {
|
---|
74 | ERR("Recv: annot open socket: %s\n", strerror(errno));
|
---|
75 | goto end;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* Bind socket */
|
---|
79 | memset(&listen_addr, 0, sizeof(listen_addr));
|
---|
80 | listen_addr.sin_family = AF_INET;
|
---|
81 | listen_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
82 | listen_addr.sin_port = htons(PORT);
|
---|
83 | if(bind(fd,(struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) {
|
---|
84 | ERR("Recv: Cannot bind socket: %s\n", strerror(errno));
|
---|
85 | goto end;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* Perform tests */
|
---|
89 | printf("Blocking/Nonblocking socket test\n");
|
---|
90 |
|
---|
91 | /* Socket should be blocking after open() */
|
---|
92 | rc = recv_listen(fd);
|
---|
93 | printf("%s TEST_BLOCK rc=%i\n", (rc==1) ? "PASS" : "FAIL", rc);
|
---|
94 |
|
---|
95 | /* Set nonblocking via fcntl() using O_NONBLOCK */
|
---|
96 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
|
---|
97 | rc = recv_listen(fd);
|
---|
98 | printf("%s TEST_FCNTL_NONBLOCK rc=%i\n", (rc>1) ? "PASS" : "FAIL", rc);
|
---|
99 |
|
---|
100 | /* Set blocking via fcntl() using O_NONBLOCK */
|
---|
101 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
|
---|
102 | rc = recv_listen(fd);
|
---|
103 | printf("%s TEST_FCNTL_NOT_NONBLOCK rc=%i\n", (rc==1) ? "PASS" : "FAIL", rc);
|
---|
104 |
|
---|
105 | /* Set nonblocking via fcntl() using O_NDELAY */
|
---|
106 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NDELAY);
|
---|
107 | rc = recv_listen(fd);
|
---|
108 | printf("%s TEST_FCNTL_NDELAY rc=%i\n", (rc>1) ? "PASS" : "FAIL", rc);
|
---|
109 |
|
---|
110 | /* Set blocking via fcntl() using O_NDELAY */
|
---|
111 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NDELAY);
|
---|
112 | rc = recv_listen(fd);
|
---|
113 | printf("%s TEST_FCNTL_NOT_NDELAY rc=%i\n", (rc==1) ? "PASS" : "FAIL", rc);
|
---|
114 |
|
---|
115 | /* Set nonblocking via ioctl() */
|
---|
116 | onoff=1;
|
---|
117 | ioctl(fd, FIONBIO, (int)&onoff);
|
---|
118 | rc = recv_listen(fd);
|
---|
119 | printf("%s TEST_IOCTL_NBIO rc=%i\n", (rc>1) ? "PASS" : "FAIL", rc);
|
---|
120 |
|
---|
121 | /* Set blocking via ioctl() */
|
---|
122 | onoff=0;
|
---|
123 | ioctl(fd, FIONBIO, (int)&onoff);
|
---|
124 | rc = recv_listen(fd);
|
---|
125 | printf("%s TEST_IOCTL_NOT_NBIO rc=%i\n", (rc==1) ? "PASS" : "FAIL", rc);
|
---|
126 |
|
---|
127 | printf("Test finished\n");
|
---|
128 |
|
---|
129 | end:
|
---|
130 | close(fd);
|
---|
131 | rtems_task_delete(RTEMS_SELF);
|
---|
132 | }
|
---|
133 |
|
---|
134 | rtems_task send_thread(rtems_task_argument ignored)
|
---|
135 | {
|
---|
136 | int fd;
|
---|
137 | struct sockaddr_in dst;
|
---|
138 | rtems_event_set events;
|
---|
139 | char *msg = "Message";
|
---|
140 | (void) ignored;
|
---|
141 |
|
---|
142 | /* Open socket */
|
---|
143 | fd = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
144 | if(fd<0) {
|
---|
145 | ERR("Send: Cannot open socket: %s\n", strerror(errno));
|
---|
146 | goto end;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Set destination address */
|
---|
150 | memset(&dst,0,sizeof(dst));
|
---|
151 | dst.sin_family = AF_INET;
|
---|
152 | dst.sin_addr.s_addr = inet_addr(INETADDR);
|
---|
153 | dst.sin_port = htons(PORT);
|
---|
154 |
|
---|
155 | for(;;)
|
---|
156 | {
|
---|
157 | rtems_event_receive(SEND_EVENT, RTEMS_WAIT|RTEMS_EVENT_ANY,
|
---|
158 | RTEMS_NO_TIMEOUT, &events);
|
---|
159 | if(events & SEND_EVENT) {
|
---|
160 | rtems_task_wake_after(TOD_MILLISECONDS_TO_TICKS(100));
|
---|
161 | if(sendto(fd, msg, strlen(msg), 0,
|
---|
162 | (struct sockaddr *) &dst, sizeof(dst)) < 0) {
|
---|
163 | ERR("Send: Cannot sendto: %s\n", strerror(errno));
|
---|
164 | goto end;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | end:
|
---|
170 | (void) close(fd);
|
---|
171 | rtems_task_delete(RTEMS_SELF);
|
---|
172 | }
|
---|
173 |
|
---|
174 | rtems_task my_main(rtems_task_argument ignored)
|
---|
175 | {
|
---|
176 | rtems_status_code status;
|
---|
177 |
|
---|
178 | (void) ignored;
|
---|
179 |
|
---|
180 | rtems_bsdnet_initialize_network ();
|
---|
181 | // sethostname (HOSTNAME, strlen (HOSTNAME));
|
---|
182 |
|
---|
183 | status = rtems_task_create(rtems_build_name('S','E','N','D'),
|
---|
184 | 64,
|
---|
185 | 8192,
|
---|
186 | RTEMS_PREEMPT|RTEMS_TIMESLICE|
|
---|
187 | RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
|
---|
188 | RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
|
---|
189 | &send_tid);
|
---|
190 | if(status != RTEMS_SUCCESSFUL)
|
---|
191 | {
|
---|
192 | printf("Cannot create send thread");
|
---|
193 | }
|
---|
194 |
|
---|
195 | status = rtems_task_create(rtems_build_name('R','E','C','V'),
|
---|
196 | 64,
|
---|
197 | 8192,
|
---|
198 | RTEMS_PREEMPT|RTEMS_TIMESLICE|
|
---|
199 | RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
|
---|
200 | RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
|
---|
201 | &recv_tid);
|
---|
202 | if(status != RTEMS_SUCCESSFUL)
|
---|
203 | {
|
---|
204 | printf("Cannot create recv thread\n");
|
---|
205 | }
|
---|
206 |
|
---|
207 | status=rtems_task_start(send_tid, send_thread, 0);
|
---|
208 | if(status != RTEMS_SUCCESSFUL)
|
---|
209 | {
|
---|
210 | printf("Cannot start send thread\n");
|
---|
211 | }
|
---|
212 |
|
---|
213 | status=rtems_task_start(recv_tid, recv_thread, 0);
|
---|
214 | if(status != RTEMS_SUCCESSFUL)
|
---|
215 | {
|
---|
216 | printf("Cannot start recv thread\n");
|
---|
217 | }
|
---|
218 |
|
---|
219 | rtems_task_delete(RTEMS_SELF);
|
---|
220 | }
|
---|
221 |
|
---|
222 | static struct rtems_bsdnet_ifconfig loopback_config =
|
---|
223 | {
|
---|
224 | "lo0", /* name */
|
---|
225 | rtems_bsdnet_loopattach, /* attach function */
|
---|
226 | NULL, /* link to next interface */
|
---|
227 | "127.0.0.1", /* IP address */
|
---|
228 | "255.0.0.0", /* IP net mask */
|
---|
229 | NULL, /* Hardware address */
|
---|
230 | 0, /* Ignore broadcasts */
|
---|
231 | 0, /* MTU */
|
---|
232 | 0, /* rbuf count */
|
---|
233 | 0, /* xbuf count */
|
---|
234 | 0, /* Port */
|
---|
235 | 0, /* Interrupt number */
|
---|
236 | 0 /* bpar */
|
---|
237 | };
|
---|
238 |
|
---|
239 | struct rtems_bsdnet_config rtems_bsdnet_config =
|
---|
240 | {
|
---|
241 | &loopback_config,
|
---|
242 | NULL,
|
---|
243 | 20, /* Network task priority */
|
---|
244 | 64 * 1024, /* Mbuf capacity */
|
---|
245 | 128 * 1024, /* Mbuf cluster capacity */
|
---|
246 | HOSTNAME, /* Hostname */
|
---|
247 | DOMAINNAME, /* Domainname */
|
---|
248 | NULL, /* Gateway */
|
---|
249 | NULL, /* Log host */
|
---|
250 | {NULL,NULL,NULL}, /* DNS Servers */
|
---|
251 | {NULL,NULL,NULL} /* NTP Servers */
|
---|
252 | };
|
---|
253 |
|
---|