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