Mercurial > repos > blastem
comparison megawifi.c @ 1515:3fc129eb0653
Cleanup MegaWiFi command implementation
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 16 Jan 2018 19:21:37 -0800 |
parents | 4f94e0f90c83 |
children | aaab852803ac |
comparison
equal
deleted
inserted
replaced
1514:4f94e0f90c83 | 1515:3fc129eb0653 |
---|---|
68 return; | 68 return; |
69 } | 69 } |
70 mw->receive_buffer[mw->receive_bytes++] = v; | 70 mw->receive_buffer[mw->receive_bytes++] = v; |
71 } | 71 } |
72 | 72 |
73 static void mw_set(megawifi *mw, uint8_t val, uint32_t count) | |
74 { | |
75 if (count + mw->receive_bytes > sizeof(mw->receive_buffer)) { | |
76 count = sizeof(mw->receive_buffer) - mw->receive_bytes; | |
77 } | |
78 memset(mw->receive_buffer + mw->receive_bytes, val, count); | |
79 mw->receive_bytes += count; | |
80 } | |
81 | |
82 static void mw_copy(megawifi *mw, const uint8_t *src, uint32_t count) | |
83 { | |
84 if (count + mw->receive_bytes > sizeof(mw->receive_buffer)) { | |
85 count = sizeof(mw->receive_buffer) - mw->receive_bytes; | |
86 } | |
87 memcpy(mw->receive_buffer + mw->receive_bytes, src, count); | |
88 mw->receive_bytes += count; | |
89 } | |
90 | |
73 static void mw_puts(megawifi *mw, char *s) | 91 static void mw_puts(megawifi *mw, char *s) |
74 { | 92 { |
75 uint32_t len = strlen(s); | 93 uint32_t len = strlen(s); |
76 if ((mw->receive_bytes + len) > sizeof(mw->receive_buffer)) { | 94 if ((mw->receive_bytes + len) > sizeof(mw->receive_buffer)) { |
77 return; | 95 return; |
78 } | 96 } |
79 memcpy(mw->receive_buffer + mw->receive_bytes, s, len); | 97 memcpy(mw->receive_buffer + mw->receive_bytes, s, len); |
80 mw->receive_bytes += len; | 98 mw->receive_bytes += len; |
99 } | |
100 | |
101 static void start_reply(megawifi *mw, uint8_t cmd) | |
102 { | |
103 mw_putc(mw, STX); | |
104 //reserve space for length | |
105 mw->receive_bytes += 2; | |
106 //cmd | |
107 mw_putc(mw, 0); | |
108 mw_putc(mw, cmd); | |
109 //reserve space for length | |
110 mw->receive_bytes += 2; | |
111 } | |
112 | |
113 static void end_reply(megawifi *mw) | |
114 { | |
115 uint32_t len = mw->receive_bytes - 3; | |
116 //LSD packet length | |
117 mw->receive_buffer[1] = len >> 8; | |
118 mw->receive_buffer[2] = len; | |
119 //command length | |
120 len -= 4; | |
121 mw->receive_buffer[5] = len >> 8; | |
122 mw->receive_buffer[6] = len; | |
123 mw_putc(mw, ETX); | |
81 } | 124 } |
82 | 125 |
83 static void process_packet(megawifi *mw) | 126 static void process_packet(megawifi *mw) |
84 { | 127 { |
85 if (mw->transmit_channel == 0) { | 128 if (mw->transmit_channel == 0) { |
87 uint32_t size = mw->transmit_buffer[2] << 8 | mw->transmit_buffer[3]; | 130 uint32_t size = mw->transmit_buffer[2] << 8 | mw->transmit_buffer[3]; |
88 if (size > mw->transmit_bytes - 4) { | 131 if (size > mw->transmit_bytes - 4) { |
89 size = mw->transmit_bytes - 4; | 132 size = mw->transmit_bytes - 4; |
90 } | 133 } |
91 mw->receive_read = mw->receive_bytes = 0; | 134 mw->receive_read = mw->receive_bytes = 0; |
92 printf("Received MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size); | |
93 switch (command) | 135 switch (command) |
94 { | 136 { |
95 case CMD_VERSION: | 137 case CMD_VERSION: |
96 //LSD header | 138 start_reply(mw, CMD_OK); |
97 mw_putc(mw, 0x7E); | |
98 mw_putc(mw, 0); | |
99 mw->receive_bytes += 1; //reserve space for LSB of len | |
100 //cmd | |
101 mw_putc(mw, 0); | |
102 mw_putc(mw, CMD_OK); | |
103 //length | |
104 mw_putc(mw, 0); | |
105 mw->receive_bytes += 1; //reserve space for LSB of len | |
106 mw_putc(mw, 1); | 139 mw_putc(mw, 1); |
107 mw_putc(mw, 0); | 140 mw_putc(mw, 0); |
108 mw_puts(mw, "blastem"); | 141 mw_puts(mw, "blastem"); |
109 mw->receive_buffer[2] = mw->receive_bytes - 3; | 142 end_reply(mw); |
110 mw->receive_buffer[6] = mw->receive_bytes - 7; | |
111 mw_putc(mw, 0x7E); | |
112 break; | 143 break; |
113 case CMD_ECHO: | 144 case CMD_ECHO: |
114 mw->receive_bytes = mw->transmit_bytes; | 145 mw->receive_bytes = mw->transmit_bytes; |
115 memcpy(mw->receive_buffer, mw->transmit_buffer, mw->transmit_bytes); | 146 memcpy(mw->receive_buffer, mw->transmit_buffer, mw->transmit_bytes); |
116 break; | 147 break; |
117 case CMD_AP_JOIN: | 148 case CMD_AP_JOIN: |
118 mw->module_state = STATE_READY; | 149 mw->module_state = STATE_READY; |
119 mw_putc(mw, 0x7E); | 150 start_reply(mw, CMD_OK); |
120 mw_putc(mw, 0); | 151 end_reply(mw); |
121 mw_putc(mw, 4); | |
122 //cmd | |
123 mw_putc(mw, 0); | |
124 mw_putc(mw, CMD_OK); | |
125 //length | |
126 mw_putc(mw, 0); | |
127 mw_putc(mw, 0); | |
128 mw_putc(mw, 0x7E); | |
129 break; | 152 break; |
130 case CMD_SYS_STAT: | 153 case CMD_SYS_STAT: |
131 //LSD header | 154 start_reply(mw, CMD_OK); |
132 mw_putc(mw, 0x7E); | |
133 mw_putc(mw, 0); | |
134 mw_putc(mw, 8); | |
135 //cmd | |
136 mw_putc(mw, 0); | |
137 mw_putc(mw, CMD_OK); | |
138 //length | |
139 mw_putc(mw, 0); | |
140 mw_putc(mw, 4); | |
141 mw_putc(mw, mw->module_state); | 155 mw_putc(mw, mw->module_state); |
142 mw_putc(mw, mw->flags); | 156 mw_putc(mw, mw->flags); |
143 mw_putc(mw, mw->channel_flags >> 8); | 157 mw_putc(mw, mw->channel_flags >> 8); |
144 mw_putc(mw, mw->channel_flags); | 158 mw_putc(mw, mw->channel_flags); |
145 mw_putc(mw, 0x7E); | 159 end_reply(mw); |
146 break; | 160 break; |
147 case CMD_IP_CURRENT: { | 161 case CMD_IP_CURRENT: { |
148 //LSD header | |
149 mw_putc(mw, 0x7E); | |
150 mw_putc(mw, 0); | |
151 mw_putc(mw, 28); | |
152 //cmd | |
153 mw_putc(mw, 0); | |
154 mw_putc(mw, CMD_OK); | |
155 //length | |
156 mw_putc(mw, 0); | |
157 mw_putc(mw, 24); | |
158 | |
159 iface_info i; | 162 iface_info i; |
160 get_host_address(&i); | 163 if (get_host_address(&i)) { |
161 //config number and reserved bytes | 164 start_reply(mw, CMD_OK); |
162 mw_putc(mw, 0); | 165 //config number and reserved bytes |
163 mw_putc(mw, 0); | 166 mw_set(mw, 0, 4); |
164 mw_putc(mw, 0); | 167 //ip |
165 mw_putc(mw, 0); | 168 mw_copy(mw, i.ip, sizeof(i.ip)); |
166 //ip | 169 //net mask |
167 mw_putc(mw, i.ip[0]); | 170 mw_copy(mw, i.net_mask, sizeof(i.net_mask)); |
168 mw_putc(mw, i.ip[1]); | 171 //gateway guess |
169 mw_putc(mw, i.ip[2]); | 172 mw_putc(mw, i.ip[0] & i.net_mask[0]); |
170 mw_putc(mw, i.ip[3]); | 173 mw_putc(mw, i.ip[1] & i.net_mask[1]); |
171 //net mask | 174 mw_putc(mw, i.ip[2] & i.net_mask[2]); |
172 mw_putc(mw, i.net_mask[0]); | 175 mw_putc(mw, (i.ip[3] & i.net_mask[3]) + 1); |
173 mw_putc(mw, i.net_mask[1]); | 176 //dns |
174 mw_putc(mw, i.net_mask[2]); | 177 static const uint8_t localhost[] = {127,0,0,1}; |
175 mw_putc(mw, i.net_mask[3]); | 178 mw_copy(mw, localhost, sizeof(localhost)); |
176 //gateway guess | 179 mw_copy(mw, localhost, sizeof(localhost)); |
177 mw_putc(mw, i.ip[0] & i.net_mask[0]); | 180 |
178 mw_putc(mw, i.ip[1] & i.net_mask[1]); | 181 } else { |
179 mw_putc(mw, i.ip[2] & i.net_mask[2]); | 182 start_reply(mw, CMD_ERROR); |
180 mw_putc(mw, (i.ip[3] & i.net_mask[3]) + 1); | 183 } |
181 //dns | 184 end_reply(mw); |
182 mw_putc(mw, 127); | |
183 mw_putc(mw, 0); | |
184 mw_putc(mw, 0); | |
185 mw_putc(mw, 1); | |
186 mw_putc(mw, 127); | |
187 mw_putc(mw, 0); | |
188 mw_putc(mw, 0); | |
189 mw_putc(mw, 1); | |
190 mw_putc(mw, 0x7E); | |
191 break; | 185 break; |
192 } | 186 } |
193 default: | 187 default: |
194 printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size); | 188 printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size); |
195 break; | 189 break; |