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