Mercurial > repos > blastem
comparison megawifi.c @ 1534:c59adc305e46 nuklear_ui
Merge
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 24 Mar 2018 22:18:23 -0700 |
parents | 713b504dc577 |
children | 2781b9551004 |
comparison
equal
deleted
inserted
replaced
1528:855210dca5b9 | 1534:c59adc305e46 |
---|---|
1 #include <stdlib.h> | |
2 #include <stdint.h> | |
3 #include <string.h> | |
4 #include <sys/types.h> | |
5 #include <sys/socket.h> | |
6 #include <unistd.h> | |
7 #include <netinet/in.h> | |
8 #include <errno.h> | |
9 #include <fcntl.h> | |
10 #include "genesis.h" | |
11 #include "net.h" | |
12 | |
13 enum { | |
14 TX_IDLE, | |
15 TX_LEN1, | |
16 TX_LEN2, | |
17 TX_PAYLOAD, | |
18 TX_WAIT_ETX | |
19 }; | |
20 #define STX 0x7E | |
21 #define ETX 0x7E | |
22 #define MAX_RECV_SIZE 1440 | |
23 | |
24 #define E(N) N | |
25 enum { | |
26 #include "mw_commands.c" | |
27 CMD_ERROR = 255 | |
28 }; | |
29 #undef E | |
30 #define E(N) #N | |
31 static const char *cmd_names[] = { | |
32 #include "mw_commands.c" | |
33 [255] = "CMD_ERROR" | |
34 }; | |
35 | |
36 enum { | |
37 STATE_IDLE=1, | |
38 STATE_AP_JOIN, | |
39 STATE_SCAN, | |
40 STATE_READY, | |
41 STATE_TRANSPARENT | |
42 }; | |
43 | |
44 #define FLAG_ONLINE | |
45 | |
46 typedef struct { | |
47 uint32_t transmit_bytes; | |
48 uint32_t expected_bytes; | |
49 uint32_t receive_bytes; | |
50 uint32_t receive_read; | |
51 int sock_fds[15]; | |
52 uint16_t channel_flags; | |
53 uint8_t channel_state[15]; | |
54 uint8_t scratchpad; | |
55 uint8_t transmit_channel; | |
56 uint8_t transmit_state; | |
57 uint8_t module_state; | |
58 uint8_t flags; | |
59 uint8_t transmit_buffer[4096]; | |
60 uint8_t receive_buffer[4096]; | |
61 } megawifi; | |
62 | |
63 static megawifi *get_megawifi(void *context) | |
64 { | |
65 m68k_context *m68k = context; | |
66 genesis_context *gen = m68k->system; | |
67 if (!gen->extra) { | |
68 gen->extra = calloc(1, sizeof(megawifi)); | |
69 megawifi *mw = gen->extra; | |
70 mw->module_state = STATE_IDLE; | |
71 for (int i = 0; i < 15; i++) | |
72 { | |
73 mw->sock_fds[i] = -1; | |
74 } | |
75 } | |
76 return gen->extra; | |
77 } | |
78 | |
79 static void mw_putc(megawifi *mw, uint8_t v) | |
80 { | |
81 if (mw->receive_bytes == sizeof(mw->receive_buffer)) { | |
82 return; | |
83 } | |
84 mw->receive_buffer[mw->receive_bytes++] = v; | |
85 } | |
86 | |
87 static void mw_set(megawifi *mw, uint8_t val, uint32_t count) | |
88 { | |
89 if (count + mw->receive_bytes > sizeof(mw->receive_buffer)) { | |
90 count = sizeof(mw->receive_buffer) - mw->receive_bytes; | |
91 } | |
92 memset(mw->receive_buffer + mw->receive_bytes, val, count); | |
93 mw->receive_bytes += count; | |
94 } | |
95 | |
96 static void mw_copy(megawifi *mw, const uint8_t *src, uint32_t count) | |
97 { | |
98 if (count + mw->receive_bytes > sizeof(mw->receive_buffer)) { | |
99 count = sizeof(mw->receive_buffer) - mw->receive_bytes; | |
100 } | |
101 memcpy(mw->receive_buffer + mw->receive_bytes, src, count); | |
102 mw->receive_bytes += count; | |
103 } | |
104 | |
105 static void mw_puts(megawifi *mw, char *s) | |
106 { | |
107 uint32_t len = strlen(s); | |
108 if ((mw->receive_bytes + len) > sizeof(mw->receive_buffer)) { | |
109 return; | |
110 } | |
111 memcpy(mw->receive_buffer + mw->receive_bytes, s, len); | |
112 mw->receive_bytes += len; | |
113 } | |
114 | |
115 static void poll_socket(megawifi *mw, uint8_t channel) | |
116 { | |
117 if (mw->sock_fds[channel] < 0) { | |
118 return; | |
119 } | |
120 if (mw->channel_state[channel] == 1) { | |
121 int res = accept(mw->sock_fds[channel], NULL, NULL); | |
122 if (res >= 0) { | |
123 close(mw->sock_fds[channel]); | |
124 fcntl(res, F_SETFL, O_NONBLOCK); | |
125 mw->sock_fds[channel] = res; | |
126 mw->channel_state[channel] = 2; | |
127 mw->channel_flags |= 1 << (channel + 1); | |
128 } else if (errno != EAGAIN && errno != EWOULDBLOCK) { | |
129 close(mw->sock_fds[channel]); | |
130 mw->channel_state[channel] = 0; | |
131 mw->channel_flags |= 1 << (channel + 1); | |
132 } | |
133 } else if (mw->channel_state[channel] == 2 && mw->receive_bytes < sizeof(mw->receive_buffer) - 4) { | |
134 size_t max = sizeof(mw->receive_buffer) - 4 - mw->receive_bytes; | |
135 if (max > MAX_RECV_SIZE) { | |
136 max = MAX_RECV_SIZE; | |
137 } | |
138 int bytes = recv(mw->sock_fds[channel], mw->receive_buffer + mw->receive_bytes + 3, max, 0); | |
139 if (bytes > 0) { | |
140 mw_putc(mw, STX); | |
141 mw_putc(mw, bytes >> 8 | (channel+1) << 4); | |
142 mw_putc(mw, bytes); | |
143 mw->receive_bytes += bytes; | |
144 mw_putc(mw, ETX); | |
145 //should this set the channel flag? | |
146 } else if (bytes < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | |
147 close(mw->sock_fds[channel]); | |
148 mw->channel_state[channel] = 0; | |
149 mw->channel_flags |= 1 << (channel + 1); | |
150 } | |
151 } | |
152 } | |
153 | |
154 static void poll_all_sockets(megawifi *mw) | |
155 { | |
156 for (int i = 0; i < 15; i++) | |
157 { | |
158 poll_socket(mw, i); | |
159 } | |
160 } | |
161 | |
162 static void start_reply(megawifi *mw, uint8_t cmd) | |
163 { | |
164 mw_putc(mw, STX); | |
165 //reserve space for length | |
166 mw->receive_bytes += 2; | |
167 //cmd | |
168 mw_putc(mw, 0); | |
169 mw_putc(mw, cmd); | |
170 //reserve space for length | |
171 mw->receive_bytes += 2; | |
172 } | |
173 | |
174 static void end_reply(megawifi *mw) | |
175 { | |
176 uint32_t len = mw->receive_bytes - 3; | |
177 //LSD packet length | |
178 mw->receive_buffer[1] = len >> 8; | |
179 mw->receive_buffer[2] = len; | |
180 //command length | |
181 len -= 4; | |
182 mw->receive_buffer[5] = len >> 8; | |
183 mw->receive_buffer[6] = len; | |
184 mw_putc(mw, ETX); | |
185 } | |
186 | |
187 static void process_packet(megawifi *mw) | |
188 { | |
189 if (mw->transmit_channel == 0) { | |
190 uint32_t command = mw->transmit_buffer[0] << 8 | mw->transmit_buffer[1]; | |
191 uint32_t size = mw->transmit_buffer[2] << 8 | mw->transmit_buffer[3]; | |
192 if (size > mw->transmit_bytes - 4) { | |
193 size = mw->transmit_bytes - 4; | |
194 } | |
195 int orig_receive_bytes = mw->receive_bytes; | |
196 switch (command) | |
197 { | |
198 case CMD_VERSION: | |
199 start_reply(mw, CMD_OK); | |
200 mw_putc(mw, 1); | |
201 mw_putc(mw, 0); | |
202 mw_puts(mw, "blastem"); | |
203 end_reply(mw); | |
204 break; | |
205 case CMD_ECHO: | |
206 mw->receive_bytes = mw->transmit_bytes; | |
207 memcpy(mw->receive_buffer, mw->transmit_buffer, mw->transmit_bytes); | |
208 break; | |
209 case CMD_IP_CURRENT: { | |
210 iface_info i; | |
211 if (get_host_address(&i)) { | |
212 start_reply(mw, CMD_OK); | |
213 //config number and reserved bytes | |
214 mw_set(mw, 0, 4); | |
215 //ip | |
216 mw_copy(mw, i.ip, sizeof(i.ip)); | |
217 //net mask | |
218 mw_copy(mw, i.net_mask, sizeof(i.net_mask)); | |
219 //gateway guess | |
220 mw_putc(mw, i.ip[0] & i.net_mask[0]); | |
221 mw_putc(mw, i.ip[1] & i.net_mask[1]); | |
222 mw_putc(mw, i.ip[2] & i.net_mask[2]); | |
223 mw_putc(mw, (i.ip[3] & i.net_mask[3]) + 1); | |
224 //dns | |
225 static const uint8_t localhost[] = {127,0,0,1}; | |
226 mw_copy(mw, localhost, sizeof(localhost)); | |
227 mw_copy(mw, localhost, sizeof(localhost)); | |
228 | |
229 } else { | |
230 start_reply(mw, CMD_ERROR); | |
231 } | |
232 end_reply(mw); | |
233 break; | |
234 } | |
235 case CMD_AP_JOIN: | |
236 mw->module_state = STATE_READY; | |
237 start_reply(mw, CMD_OK); | |
238 end_reply(mw); | |
239 break; | |
240 case CMD_TCP_BIND:{ | |
241 if (size < 7){ | |
242 start_reply(mw, CMD_ERROR); | |
243 end_reply(mw); | |
244 break; | |
245 } | |
246 uint8_t channel = mw->transmit_buffer[10]; | |
247 if (!channel || channel > 15) { | |
248 start_reply(mw, CMD_ERROR); | |
249 end_reply(mw); | |
250 break; | |
251 } | |
252 channel--; | |
253 if (mw->sock_fds[channel] >= 0) { | |
254 close(mw->sock_fds[channel]); | |
255 } | |
256 mw->sock_fds[channel] = socket(AF_INET, SOCK_STREAM, 0); | |
257 if (mw->sock_fds[channel] < 0) { | |
258 start_reply(mw, CMD_ERROR); | |
259 end_reply(mw); | |
260 break; | |
261 } | |
262 int value = 1; | |
263 setsockopt(mw->sock_fds[channel], SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)); | |
264 struct sockaddr_in bind_addr; | |
265 memset(&bind_addr, 0, sizeof(bind_addr)); | |
266 bind_addr.sin_family = AF_INET; | |
267 bind_addr.sin_port = htons(mw->transmit_buffer[8] << 8 | mw->transmit_buffer[9]); | |
268 if (bind(mw->sock_fds[channel], (struct sockaddr *)&bind_addr, sizeof(bind_addr)) != 0) { | |
269 close(mw->sock_fds[channel]); | |
270 mw->sock_fds[channel] = -1; | |
271 start_reply(mw, CMD_ERROR); | |
272 end_reply(mw); | |
273 break; | |
274 } | |
275 int res = listen(mw->sock_fds[channel], 2); | |
276 start_reply(mw, res ? CMD_ERROR : CMD_OK); | |
277 if (res) { | |
278 close(mw->sock_fds[channel]); | |
279 mw->sock_fds[channel] = -1; | |
280 } else { | |
281 mw->channel_flags |= 1 << (channel + 1); | |
282 mw->channel_state[channel] = 1; | |
283 fcntl(mw->sock_fds[channel], F_SETFL, O_NONBLOCK); | |
284 } | |
285 end_reply(mw); | |
286 break; | |
287 } | |
288 case CMD_SOCK_STAT: { | |
289 uint8_t channel = mw->transmit_buffer[4]; | |
290 if (!channel || channel > 15) { | |
291 start_reply(mw, CMD_ERROR); | |
292 end_reply(mw); | |
293 break; | |
294 } | |
295 mw->channel_flags &= ~(1 << channel); | |
296 channel--; | |
297 poll_socket(mw, channel); | |
298 start_reply(mw, CMD_OK); | |
299 mw_putc(mw, mw->channel_state[channel]); | |
300 end_reply(mw); | |
301 break; | |
302 } | |
303 case CMD_SYS_STAT: | |
304 poll_all_sockets(mw); | |
305 start_reply(mw, CMD_OK); | |
306 mw_putc(mw, mw->module_state); | |
307 mw_putc(mw, mw->flags); | |
308 mw_putc(mw, mw->channel_flags >> 8); | |
309 mw_putc(mw, mw->channel_flags); | |
310 end_reply(mw); | |
311 break; | |
312 default: | |
313 printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size); | |
314 break; | |
315 } | |
316 } else if (mw->sock_fds[mw->transmit_channel - 1] >= 0 && mw->channel_state[mw->transmit_channel - 1] == 2) { | |
317 uint8_t channel = mw->transmit_channel - 1; | |
318 int sent = send(mw->sock_fds[channel], mw->transmit_buffer, mw->transmit_bytes, MSG_NOSIGNAL); | |
319 if (sent < 0 && errno != EAGAIN && errno != EWOULDBLOCK) { | |
320 close(mw->sock_fds[channel]); | |
321 mw->sock_fds[channel] = -1; | |
322 mw->channel_state[channel] = 0; | |
323 mw->channel_flags |= 1 << mw->transmit_channel; | |
324 } else if (sent < mw->transmit_bytes) { | |
325 //TODO: save this data somewhere so it can be sent in poll_socket | |
326 printf("Sent %d bytes on channel %d, but %d were requested\n", sent, mw->transmit_channel, mw->transmit_bytes); | |
327 } | |
328 } else { | |
329 printf("Unhandled receive of MegaWiFi data on channel %d\n", mw->transmit_channel); | |
330 } | |
331 mw->transmit_bytes = mw->expected_bytes = 0; | |
332 } | |
333 | |
334 void *megawifi_write_b(uint32_t address, void *context, uint8_t value) | |
335 { | |
336 if (!(address & 1)) { | |
337 return context; | |
338 } | |
339 megawifi *mw = get_megawifi(context); | |
340 address = address >> 1 & 7; | |
341 switch (address) | |
342 { | |
343 case 0: | |
344 switch (mw->transmit_state) | |
345 { | |
346 case TX_IDLE: | |
347 if (value == STX) { | |
348 mw->transmit_state = TX_LEN1; | |
349 } | |
350 break; | |
351 case TX_LEN1: | |
352 mw->transmit_channel = value >> 4; | |
353 mw->expected_bytes = value << 8 & 0xF00; | |
354 mw->transmit_state = TX_LEN2; | |
355 break; | |
356 case TX_LEN2: | |
357 mw->expected_bytes |= value; | |
358 mw->transmit_state = TX_PAYLOAD; | |
359 break; | |
360 case TX_PAYLOAD: | |
361 mw->transmit_buffer[mw->transmit_bytes++] = value; | |
362 if (mw->transmit_bytes == mw->expected_bytes) { | |
363 mw->transmit_state = TX_WAIT_ETX; | |
364 } | |
365 break; | |
366 case TX_WAIT_ETX: | |
367 if (value == ETX) { | |
368 mw->transmit_state = TX_IDLE; | |
369 process_packet(mw); | |
370 } | |
371 break; | |
372 } | |
373 break; | |
374 case 7: | |
375 mw->scratchpad = value; | |
376 break; | |
377 default: | |
378 printf("Unhandled write to MegaWiFi UART register %X: %X\n", address, value); | |
379 } | |
380 return context; | |
381 } | |
382 | |
383 void *megawifi_write_w(uint32_t address, void *context, uint16_t value) | |
384 { | |
385 return megawifi_write_b(address | 1, context, value); | |
386 } | |
387 | |
388 uint8_t megawifi_read_b(uint32_t address, void *context) | |
389 { | |
390 | |
391 if (!(address & 1)) { | |
392 return 0xFF; | |
393 } | |
394 megawifi *mw = get_megawifi(context); | |
395 address = address >> 1 & 7; | |
396 switch (address) | |
397 { | |
398 case 0: | |
399 poll_all_sockets(mw); | |
400 if (mw->receive_read < mw->receive_bytes) { | |
401 uint8_t ret = mw->receive_buffer[mw->receive_read++]; | |
402 if (mw->receive_read == mw->receive_bytes) { | |
403 mw->receive_read = mw->receive_bytes = 0; | |
404 } | |
405 return ret; | |
406 } | |
407 return 0xFF; | |
408 case 5: | |
409 poll_all_sockets(mw); | |
410 //line status | |
411 return 0x60 | (mw->receive_read < mw->receive_bytes); | |
412 case 7: | |
413 return mw->scratchpad; | |
414 default: | |
415 printf("Unhandled read from MegaWiFi UART register %X\n", address); | |
416 return 0xFF; | |
417 } | |
418 } | |
419 | |
420 uint16_t megawifi_read_w(uint32_t address, void *context) | |
421 { | |
422 return 0xFF00 | megawifi_read_b(address | 1, context); | |
423 } |