annotate megawifi.c @ 2008:a8e3e816a50d

Fix build breakage on OS X
author Michael Pavone <pavone@retrodev.com>
date Fri, 16 Oct 2020 22:13:07 -0700
parents 41b9509ede38
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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>
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
9 #include <sys/param.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
10 #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
11 #include <sys/socket.h>
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
12 #include <arpa/inet.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
13 #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
14 #include <netinet/in.h>
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
15 #include <netdb.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
16 #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
17 #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
18 #include <fcntl.h>
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
19 #include <time.h>
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 #include "genesis.h"
1514
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents: 1513
diff changeset
21 #include "net.h"
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
22 #include "util.h"
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23
2008
a8e3e816a50d Fix build breakage on OS X
Michael Pavone <pavone@retrodev.com>
parents: 1970
diff changeset
24 #if defined(_WIN32) || defined(__APPLE__)
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
25 # if BYTE_ORDER == LITTLE_ENDIAN
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
26 #define htobe64(val) ((((uint64_t)htonl((val)&0xFFFFFFFF))<<32) | htonl((val)>>32))
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
27 # else
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
28 #define htobe64(val) (val)
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
29 # endif
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
30 #endif
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
31
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 enum {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 TX_IDLE,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 TX_LEN1,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 TX_LEN2,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 TX_PAYLOAD,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 TX_WAIT_ETX
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 };
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 #define STX 0x7E
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 #define ETX 0x7E
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
41 #define MAX_RECV_SIZE 1460
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 #define E(N) N
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 enum {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 #include "mw_commands.c"
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 CMD_ERROR = 255
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 };
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 #undef E
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 #define E(N) #N
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 static const char *cmd_names[] = {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 #include "mw_commands.c"
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 [255] = "CMD_ERROR"
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
1535
2781b9551004 MSG_NOSIGNAL doesn't exist on all platforms
Michael Pavone <pavone@retrodev.com>
parents: 1518
diff changeset
55 #ifndef MSG_NOSIGNAL
2781b9551004 MSG_NOSIGNAL doesn't exist on all platforms
Michael Pavone <pavone@retrodev.com>
parents: 1518
diff changeset
56 #define MSG_NOSIGNAL 0
2781b9551004 MSG_NOSIGNAL doesn't exist on all platforms
Michael Pavone <pavone@retrodev.com>
parents: 1518
diff changeset
57 #endif
2781b9551004 MSG_NOSIGNAL doesn't exist on all platforms
Michael Pavone <pavone@retrodev.com>
parents: 1518
diff changeset
58
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
59 enum mw_state {
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 STATE_IDLE=1,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 STATE_AP_JOIN,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 STATE_SCAN,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 STATE_READY,
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 STATE_TRANSPARENT
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 };
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
67 enum {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
68 SOCKST_NONE = 0,
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
69 SOCKST_TCP_LISTEN,
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
70 SOCKST_TCP_EST,
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
71 SOCKST_UDP_READY
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
72 };
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
73
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
74 // TCP/UDP address message
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
75 struct mw_addr_msg {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
76 char dst_port[6];
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
77 char src_port[6];
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
78 uint8_t channel;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
79 char host[];
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
80 };
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
81
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 #define FLAG_ONLINE
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 typedef struct {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 uint32_t transmit_bytes;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 uint32_t expected_bytes;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 uint32_t receive_bytes;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 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
89 int sock_fds[15];
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 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
91 uint8_t channel_state[15];
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 uint8_t scratchpad;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 uint8_t transmit_channel;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 uint8_t transmit_state;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 uint8_t module_state;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 uint8_t flags;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 uint8_t transmit_buffer[4096];
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 uint8_t receive_buffer[4096];
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
99 struct sockaddr_in remote_addr[15]; // Needed for UDP sockets
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 } megawifi;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 static megawifi *get_megawifi(void *context)
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 m68k_context *m68k = context;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 genesis_context *gen = m68k->system;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 if (!gen->extra) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
107 socket_init();
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 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
109 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
110 mw->module_state = STATE_IDLE;
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
111 mw->flags = 0xE0; // cfg_ok, dt_ok, online
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
112 for (int i = 0; i < 15; i++) {
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
113 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
114 }
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 return gen->extra;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 static void mw_putc(megawifi *mw, uint8_t v)
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 if (mw->receive_bytes == sizeof(mw->receive_buffer)) {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 return;
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 mw->receive_buffer[mw->receive_bytes++] = v;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126
1515
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
127 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
128 {
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
129 if (count + mw->receive_bytes > sizeof(mw->receive_buffer)) {
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
130 count = sizeof(mw->receive_buffer) - mw->receive_bytes;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
131 }
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
132 memset(mw->receive_buffer + mw->receive_bytes, val, count);
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
133 mw->receive_bytes += count;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
134 }
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
135
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
136 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
137 {
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
138 if (count + mw->receive_bytes > sizeof(mw->receive_buffer)) {
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
139 count = sizeof(mw->receive_buffer) - mw->receive_bytes;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
140 }
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
141 memcpy(mw->receive_buffer + mw->receive_bytes, src, count);
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
142 mw->receive_bytes += count;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
143 }
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
144
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
145 static void mw_puts(megawifi *mw, const char *s)
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
146 {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
147 size_t len = strlen(s);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
148 mw_copy(mw, (uint8_t*)s, len);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
149 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
150
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
151 static void udp_recv(megawifi *mw, uint8_t idx)
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
152 {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
153 ssize_t recvd;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
154 int s = mw->sock_fds[idx];
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
155 struct sockaddr_in remote;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
156 socklen_t addr_len = sizeof(struct sockaddr_in);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
157
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
158 if (mw->remote_addr[idx].sin_addr.s_addr != htonl(INADDR_ANY)) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
159 // Receive only from specified address
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
160 recvd = recvfrom(s, (char*)mw->receive_buffer + 3, MAX_RECV_SIZE, 0,
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
161 (struct sockaddr*)&remote, &addr_len);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
162 if (recvd > 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
163 if (remote.sin_addr.s_addr != mw->remote_addr[idx].sin_addr.s_addr) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
164 printf("Discarding UDP packet from unknown addr %s:%d\n",
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
165 inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
166 recvd = 0;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
167 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
168 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
169 } else {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
170 // Reuse mode, data is preceded by remote IPv4 and port
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
171 recvd = recvfrom(s, (char*)mw->receive_buffer + 9, MAX_RECV_SIZE - 6,
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
172 0, (struct sockaddr*)&remote, &addr_len);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
173 if (recvd > 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
174 mw->receive_buffer[3] = remote.sin_addr.s_addr;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
175 mw->receive_buffer[4] = remote.sin_addr.s_addr>>8;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
176 mw->receive_buffer[5] = remote.sin_addr.s_addr>>16;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
177 mw->receive_buffer[6] = remote.sin_addr.s_addr>>24;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
178 mw->receive_buffer[7] = remote.sin_port;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
179 mw->receive_buffer[8] = remote.sin_port>>8;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
180 recvd += 6;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
181 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
182 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
183
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
184 if (recvd > 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
185 mw_putc(mw, STX);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
186 mw_putc(mw, (recvd >> 8) | ((idx+1) << 4));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
187 mw_putc(mw, recvd);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
188 mw->receive_bytes += recvd;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
189 mw_putc(mw, ETX);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
190 //should this set the channel flag?
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
191 } else if (recvd < 0 && !socket_error_is_wouldblock()) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
192 socket_close(mw->sock_fds[idx]);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
193 mw->channel_state[idx] = SOCKST_NONE;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
194 mw->channel_flags |= 1 << (idx + 1);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
195 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
196 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
197
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
198 static void udp_send(megawifi *mw, uint8_t idx)
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
199 {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
200 struct sockaddr_in remote;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
201 int s = mw->sock_fds[idx];
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
202 int sent;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
203 char *data = (char*)mw->transmit_buffer;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
204
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
205 if (mw->remote_addr[idx].sin_addr.s_addr != htonl(INADDR_ANY)) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
206 sent = sendto(s, data, mw->transmit_bytes, 0, (struct sockaddr*)&mw->remote_addr[idx],
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
207 sizeof(struct sockaddr_in));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
208 } else {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
209 // Reuse mode, extract address from leading bytes
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
210 // NOTE: mw->remote_addr[idx].sin_addr.s_addr == INADDR_ANY
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
211 remote.sin_addr.s_addr = *((int32_t*)data);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
212 remote.sin_port = *((int16_t*)(data + 4));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
213 remote.sin_family = AF_INET;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
214 memset(remote.sin_zero, 0, sizeof(remote.sin_zero));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
215 sent = sendto(s, data + 6, mw->transmit_bytes - 6, 0, (struct sockaddr*)&remote,
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
216 sizeof(struct sockaddr_in)) + 6;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
217 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
218 if (sent < 0 && !socket_error_is_wouldblock()) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
219 socket_close(s);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
220 mw->sock_fds[idx] = -1;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
221 mw->channel_state[idx] = SOCKST_NONE;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
222 mw->channel_flags |= 1 << (idx + 1);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
223 } else if (sent < mw->transmit_bytes) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
224 //TODO: save this data somewhere so it can be sent in poll_socket
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
225 printf("Sent %d bytes on channel %d, but %d were requested\n", sent, idx + 1, mw->transmit_bytes);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
226 }
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
227 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
228
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
229 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
230 {
aaab852803ac Get enough of MegaWifi implemented so that basic commands from wflash CLI command work
Michael Pavone <pavone@retrodev.com>
parents: 1515
diff changeset
231 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
232 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
233 }
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
234 if (mw->channel_state[channel] == SOCKST_TCP_LISTEN) {
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
235 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
236 if (res >= 0) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
237 socket_close(mw->sock_fds[channel]);
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
238 socket_blocking(res, 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
239 mw->sock_fds[channel] = res;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
240 mw->channel_state[channel] = SOCKST_TCP_EST;
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
241 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
242 } else if (errno != EAGAIN && errno != EWOULDBLOCK) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
243 socket_close(mw->sock_fds[channel]);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
244 mw->channel_state[channel] = SOCKST_NONE;
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
245 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
246 }
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
247 } else if (mw->channel_state[channel] == SOCKST_TCP_EST && 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
248 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
249 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
250 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
251 }
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
252 int bytes = recv(mw->sock_fds[channel], (char*)(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
253 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
254 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
255 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
256 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
257 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
258 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
259 //should this set the channel flag?
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
260 } else if (bytes < 0 && !socket_error_is_wouldblock()) {
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
261 socket_close(mw->sock_fds[channel]);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
262 mw->channel_state[channel] = SOCKST_NONE;
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
263 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
264 }
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
265 } else if (mw->channel_state[channel] == SOCKST_UDP_READY && !mw->receive_bytes) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
266 udp_recv(mw, channel);
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
267 }
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
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 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
271 {
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 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
273 {
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 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
275 }
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 }
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
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
278
1515
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
279 static void start_reply(megawifi *mw, uint8_t cmd)
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
280 {
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
281 mw_putc(mw, STX);
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
282 //reserve space for length
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
283 mw->receive_bytes += 2;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
284 //cmd
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
285 mw_putc(mw, 0);
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
286 mw_putc(mw, cmd);
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
287 //reserve space for length
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
288 mw->receive_bytes += 2;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
289 }
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
290
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
291 static void end_reply(megawifi *mw)
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
292 {
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
293 uint32_t len = mw->receive_bytes - 3;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
294 //LSD packet length
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
295 mw->receive_buffer[1] = len >> 8;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
296 mw->receive_buffer[2] = len;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
297 //command length
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
298 len -= 4;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
299 mw->receive_buffer[5] = len >> 8;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
300 mw->receive_buffer[6] = len;
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
301 mw_putc(mw, ETX);
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
302 }
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
303
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
304 static void cmd_ap_cfg_get(megawifi *mw)
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
305 {
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
306 char ssid[32] = {0};
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
307 char pass[64] = {0};
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
308 uint8_t slot = mw->transmit_buffer[4];
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
309
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
310 sprintf(ssid, "BLASTEM! SSID %d", slot + 1);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
311 sprintf(pass, "BLASTEM! PASS %d", slot + 1);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
312 start_reply(mw, CMD_OK);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
313 mw_putc(mw, slot);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
314 mw_putc(mw, 7); /// 11bgn
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
315 mw_copy(mw, (uint8_t*)ssid, 32);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
316 mw_copy(mw, (uint8_t*)pass, 64);
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
317 end_reply(mw);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
318 }
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
319
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
320 static void cmd_ip_cfg_get(megawifi *mw)
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
321 {
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
322 uint32_t ipv4s[5] = {0};
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
323
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
324 start_reply(mw, CMD_OK);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
325 mw_putc(mw, mw->transmit_buffer[4]);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
326 mw_putc(mw, 0);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
327 mw_putc(mw, 0);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
328 mw_putc(mw, 0);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
329 mw_copy(mw, (uint8_t*)ipv4s, sizeof(ipv4s));
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
330 end_reply(mw);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
331 }
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
332
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
333 static void cmd_tcp_con(megawifi *mw, uint32_t size)
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
334 {
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
335 struct mw_addr_msg *addr = (struct mw_addr_msg*)(mw->transmit_buffer + 4);
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
336 struct addrinfo hints;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
337 struct addrinfo *res = NULL;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
338 int s;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
339 int err;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
340
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
341 uint8_t channel = addr->channel;
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
342 if (!channel || channel > 15 || mw->sock_fds[channel - 1] >= 0) {
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
343 start_reply(mw, CMD_ERROR);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
344 end_reply(mw);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
345 return;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
346 }
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
347 channel--;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
348
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
349 memset(&hints, 0, sizeof(hints));
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
350 hints.ai_family = AF_INET;
1966
b3c2dcae7dfc Skip setting AI_NUMERICSERV in megawifi code on Windows for now so min SDK version does not need to be bumped
Michael Pavone <pavone@retrodev.com>
parents: 1962
diff changeset
351 #ifndef _WIN32
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
352 hints.ai_flags = AI_NUMERICSERV;
1966
b3c2dcae7dfc Skip setting AI_NUMERICSERV in megawifi code on Windows for now so min SDK version does not need to be bumped
Michael Pavone <pavone@retrodev.com>
parents: 1962
diff changeset
353 #endif
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
354 hints.ai_socktype = SOCK_STREAM;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
355
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
356 if ((err = getaddrinfo(addr->host, addr->dst_port, &hints, &res)) != 0) {
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
357 printf("getaddrinfo failed: %s\n", gai_strerror(err));
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
358 start_reply(mw, CMD_ERROR);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
359 end_reply(mw);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
360 return;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
361 }
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
362
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
363 s = socket(AF_INET, SOCK_STREAM, 0);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
364 if (s < 0) {
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
365 goto err;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
366 }
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
367
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
368 // Should this be handled in a separate thread to avoid blocking emulation?
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
369 if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
370 goto err;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
371 }
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
372
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
373 socket_blocking(s, 0);
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
374 mw->sock_fds[channel] = s;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
375 mw->channel_state[channel] = SOCKST_TCP_EST;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
376 mw->channel_flags |= 1 << (channel + 1);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
377 printf("Connection established on ch %d with %s:%s\n", channel + 1,
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
378 addr->host, addr->dst_port);
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
379
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
380 if (res) {
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
381 freeaddrinfo(res);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
382 }
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
383 start_reply(mw, CMD_OK);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
384 end_reply(mw);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
385 return;
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
386
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
387 err:
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
388 freeaddrinfo(res);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
389 printf("Connection to %s:%s failed, %s\n", addr->host, addr->dst_port, strerror(errno));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
390 start_reply(mw, CMD_ERROR);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
391 end_reply(mw);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
392 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
393
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
394 static void cmd_close(megawifi *mw)
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
395 {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
396 int channel = mw->transmit_buffer[4] - 1;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
397
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
398 if (channel >= 15 || mw->sock_fds[channel] < 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
399 start_reply(mw, CMD_ERROR);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
400 end_reply(mw);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
401 return;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
402 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
403
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
404 socket_close(mw->sock_fds[channel]);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
405 mw->sock_fds[channel] = -1;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
406 mw->channel_state[channel] = SOCKST_NONE;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
407 mw->channel_flags |= 1 << (channel + 1);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
408 start_reply(mw, CMD_OK);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
409 end_reply(mw);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
410 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
411
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
412 static void cmd_udp_set(megawifi *mw)
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
413 {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
414 struct mw_addr_msg *addr = (struct mw_addr_msg*)(mw->transmit_buffer + 4);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
415 unsigned int local_port, remote_port;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
416 int s;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
417 struct addrinfo *raddr;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
418 struct addrinfo hints;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
419 struct sockaddr_in local;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
420 int err;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
421
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
422 uint8_t channel = addr->channel;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
423 if (!channel || channel > 15 || mw->sock_fds[channel - 1] >= 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
424 goto err;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
425 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
426 channel--;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
427 local_port = atoi(addr->src_port);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
428 remote_port = atoi(addr->dst_port);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
429
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
430 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
431 printf("Datagram socket creation failed\n");
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
432 goto err;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
433 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
434
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
435 memset(local.sin_zero, 0, sizeof(local.sin_zero));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
436 local.sin_family = AF_INET;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
437 local.sin_addr.s_addr = htonl(INADDR_ANY);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
438 local.sin_port = htons(local_port);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
439 if (remote_port && addr->host[0]) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
440 // Communication with remote peer
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
441 printf("Set UDP ch %d, port %d to addr %s:%d\n", addr->channel,
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
442 local_port, addr->host, remote_port);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
443
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
444 memset(&hints, 0, sizeof(hints));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
445 hints.ai_family = AF_INET;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
446 #ifndef _WIN32
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
447 hints.ai_flags = AI_NUMERICSERV;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
448 #endif
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
449 hints.ai_socktype = SOCK_DGRAM;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
450
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
451 if ((err = getaddrinfo(addr->host, addr->dst_port, &hints, &raddr)) != 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
452 printf("getaddrinfo failed: %s\n", gai_strerror(err));
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
453 goto err;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
454 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
455 mw->remote_addr[channel] = *((struct sockaddr_in*)raddr->ai_addr);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
456 freeaddrinfo(raddr);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
457 } else if (local_port) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
458 // Server in reuse mode
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
459 printf("Set UDP ch %d, src port %d\n", addr->channel, local_port);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
460 mw->remote_addr[channel] = local;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
461 } else {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
462 printf("Invalid UDP socket data\n");
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
463 goto err;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
464 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
465
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
466 if (bind(s, (struct sockaddr*)&local, sizeof(struct sockaddr_in)) < 0) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
467 printf("bind to port %d failed\n", local_port);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
468 goto err;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
469 }
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
470
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
471 socket_blocking(s, 0);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
472 mw->sock_fds[channel] = s;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
473 mw->channel_state[channel] = SOCKST_UDP_READY;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
474 mw->channel_flags |= 1 << (channel + 1);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
475
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
476 start_reply(mw, CMD_OK);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
477 end_reply(mw);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
478
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
479 return;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
480
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
481 err:
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
482 start_reply(mw, CMD_ERROR);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
483 end_reply(mw);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
484 }
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
485
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
486 #define AVATAR_BYTES (32 * 48 / 2)
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
487 static void cmd_gamertag_get(megawifi *mw)
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
488 {
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
489 uint32_t id = htonl(1);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
490 char buf[AVATAR_BYTES];
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
491
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
492 start_reply(mw, CMD_OK);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
493 // TODO Get items from config file
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
494 mw_copy(mw, (uint8_t*)&id, 4);
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
495 strncpy(buf, "doragasu on Blastem!", 32);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
496 mw_copy(mw, (uint8_t*)buf, 32);
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
497 strncpy(buf, "My cool password", 32);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
498 mw_copy(mw, (uint8_t*)buf, 32);
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
499 strncpy(buf, "All your WiFi are belong to me!", 32);
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
500 mw_copy(mw, (uint8_t*)buf, 32);
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
501 memset(buf, 0, 64); // Telegram token
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
502 mw_copy(mw, (uint8_t*)buf, 64);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
503 mw_copy(mw, (uint8_t*)buf, AVATAR_BYTES); // Avatar tiles
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
504 mw_copy(mw, (uint8_t*)buf, 32); // Avatar palette
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
505 end_reply(mw);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
506 }
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
507
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
508 static void cmd_hrng_get(megawifi *mw)
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
509 {
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
510 uint16_t len = (mw->transmit_buffer[4]<<8) + mw->transmit_buffer[5];
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
511 if (len > (MAX_RECV_SIZE - 4)) {
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
512 start_reply(mw, CMD_ERROR);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
513 end_reply(mw);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
514 return;
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
515 }
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
516 // Pseudo-random, but who cares
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
517 start_reply(mw, CMD_OK);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
518 srand(time(NULL));
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
519 for (uint16_t i = 0; i < len; i++) {
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
520 mw_putc(mw, rand());
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
521 }
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
522 end_reply(mw);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
523 }
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
524
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
525 static void cmd_datetime(megawifi *mw)
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
526 {
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
527 start_reply(mw, CMD_OK);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
528 #ifdef _WIN32
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
529 __time64_t t = _time64(NULL);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
530 int64_t t_be = htobe64(t);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
531 mw_copy(mw, (uint8_t*)&t_be, sizeof(int64_t));
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
532 mw_puts(mw, _ctime64(&t));
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
533 #else
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
534 time_t t = time(NULL);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
535 int64_t t_be = htobe64(t);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
536 mw_copy(mw, (uint8_t*)&t_be, sizeof(int64_t));
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
537 mw_puts(mw, ctime(&t));
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
538 #endif
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
539
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
540 mw_putc(mw, '\0');
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
541 end_reply(mw);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
542 }
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
543
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
544 static void process_command(megawifi *mw)
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
545 {
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
546 uint32_t command = mw->transmit_buffer[0] << 8 | mw->transmit_buffer[1];
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
547 uint32_t size = mw->transmit_buffer[2] << 8 | mw->transmit_buffer[3];
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
548 if (size > mw->transmit_bytes - 4) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
549 size = mw->transmit_bytes - 4;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
550 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
551 int orig_receive_bytes = mw->receive_bytes;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
552 switch (command)
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
553 {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
554 case CMD_VERSION:
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
555 start_reply(mw, CMD_OK);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
556 mw_putc(mw, 1);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
557 mw_putc(mw, 3);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
558 mw_putc(mw, 0);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
559 mw_puts(mw, "blastem");
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
560 mw_putc(mw, '\0');
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
561 end_reply(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
562 break;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
563 case CMD_ECHO:
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
564 mw->receive_bytes = mw->transmit_bytes;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
565 memcpy(mw->receive_buffer, mw->transmit_buffer, mw->transmit_bytes);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
566 break;
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
567 case CMD_AP_CFG_GET:
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
568 cmd_ap_cfg_get(mw);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
569 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
570 case CMD_IP_CURRENT: {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
571 iface_info i;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
572 if (get_host_address(&i)) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
573 start_reply(mw, CMD_OK);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
574 //config number and reserved bytes
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
575 mw_set(mw, 0, 4);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
576 //ip
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
577 mw_copy(mw, i.ip, sizeof(i.ip));
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
578 //net mask
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
579 mw_copy(mw, i.net_mask, sizeof(i.net_mask));
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
580 //gateway guess
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
581 mw_putc(mw, i.ip[0] & i.net_mask[0]);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
582 mw_putc(mw, i.ip[1] & i.net_mask[1]);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
583 mw_putc(mw, i.ip[2] & i.net_mask[2]);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
584 mw_putc(mw, (i.ip[3] & i.net_mask[3]) + 1);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
585 //dns
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
586 static const uint8_t localhost[] = {127,0,0,1};
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
587 mw_copy(mw, localhost, sizeof(localhost));
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
588 mw_copy(mw, localhost, sizeof(localhost));
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
589
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
590 } else {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
591 start_reply(mw, CMD_ERROR);
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
592 }
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
593 end_reply(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
594 break;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
595 }
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
596 case CMD_IP_CFG_GET:
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
597 cmd_ip_cfg_get(mw);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
598 break;
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
599 case CMD_DEF_AP_CFG_GET:
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
600 start_reply(mw, CMD_OK);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
601 mw_putc(mw, 0);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
602 end_reply(mw);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
603 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
604 case CMD_AP_JOIN:
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
605 mw->module_state = STATE_READY;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
606 start_reply(mw, CMD_OK);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
607 end_reply(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
608 break;
1961
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
609 case CMD_TCP_CON:
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
610 cmd_tcp_con(mw, size);
d14630883b1d Implement CMD_TCP_CON command
doragasu <doragasu@hotmail.com>
parents: 1960
diff changeset
611 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
612 case CMD_TCP_BIND:{
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
613 if (size < 7){
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
614 start_reply(mw, CMD_ERROR);
1515
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
615 end_reply(mw);
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
616 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
617 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
618 uint8_t channel = mw->transmit_buffer[10];
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
619 if (!channel || channel > 15) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
620 start_reply(mw, CMD_ERROR);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
621 end_reply(mw);
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
622 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
623 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
624 channel--;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
625 if (mw->sock_fds[channel] >= 0) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
626 socket_close(mw->sock_fds[channel]);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
627 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
628 mw->sock_fds[channel] = socket(AF_INET, SOCK_STREAM, 0);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
629 if (mw->sock_fds[channel] < 0) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
630 start_reply(mw, CMD_ERROR);
1515
3fc129eb0653 Cleanup MegaWiFi command implementation
Michael Pavone <pavone@retrodev.com>
parents: 1514
diff changeset
631 end_reply(mw);
1514
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents: 1513
diff changeset
632 break;
4f94e0f90c83 Added support for MegaWiFi command IP_CURRENT
Michael Pavone <pavone@retrodev.com>
parents: 1513
diff changeset
633 }
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
634 int value = 1;
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
635 setsockopt(mw->sock_fds[channel], SOL_SOCKET, SO_REUSEADDR, (char*)&value, sizeof(value));
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
636 struct sockaddr_in bind_addr;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
637 memset(&bind_addr, 0, sizeof(bind_addr));
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
638 bind_addr.sin_family = AF_INET;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
639 bind_addr.sin_port = htons(mw->transmit_buffer[8] << 8 | mw->transmit_buffer[9]);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
640 if (bind(mw->sock_fds[channel], (struct sockaddr *)&bind_addr, sizeof(bind_addr)) != 0) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
641 socket_close(mw->sock_fds[channel]);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
642 mw->sock_fds[channel] = -1;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
643 start_reply(mw, CMD_ERROR);
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
644 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
645 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
646 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
647 int res = listen(mw->sock_fds[channel], 2);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
648 start_reply(mw, res ? CMD_ERROR : CMD_OK);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
649 if (res) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
650 socket_close(mw->sock_fds[channel]);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
651 mw->sock_fds[channel] = -1;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
652 } else {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
653 mw->channel_flags |= 1 << (channel + 1);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
654 mw->channel_state[channel] = SOCKST_TCP_LISTEN;
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
655 socket_blocking(mw->sock_fds[channel], 0);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
656 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
657 end_reply(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
658 break;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
659 }
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
660 case CMD_CLOSE:
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
661 cmd_close(mw);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
662 break;
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
663 case CMD_UDP_SET:
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
664 cmd_udp_set(mw);
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
665 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
666 case CMD_SOCK_STAT: {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
667 uint8_t channel = mw->transmit_buffer[4];
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
668 if (!channel || channel > 15) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
669 start_reply(mw, CMD_ERROR);
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
670 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
671 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
672 }
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
673 mw->channel_flags &= ~(1 << channel);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
674 channel--;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
675 poll_socket(mw, channel);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
676 start_reply(mw, CMD_OK);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
677 mw_putc(mw, mw->channel_state[channel]);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
678 end_reply(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
679 break;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
680 }
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
681 case CMD_DATETIME:
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
682 cmd_datetime(mw);
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
683 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
684 case CMD_SYS_STAT:
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
685 poll_all_sockets(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
686 start_reply(mw, CMD_OK);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
687 mw_putc(mw, mw->module_state);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
688 mw_putc(mw, mw->flags);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
689 mw_putc(mw, mw->channel_flags >> 8);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
690 mw_putc(mw, mw->channel_flags);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
691 end_reply(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
692 break;
1962
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
693 case CMD_GAMERTAG_GET:
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
694 cmd_gamertag_get(mw);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
695 break;
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
696 case CMD_LOG:
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
697 start_reply(mw, CMD_OK);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
698 puts((char*)&mw->transmit_buffer[4]);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
699 end_reply(mw);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
700 break;
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
701 case CMD_HRNG_GET:
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
702 cmd_hrng_get(mw);
16d46ff1f620 Implement CMD_HRNG_GET, CMD_GAMERTAG_GET and CMD_LOG.
doragasu <doragasu@hotmail.com>
parents: 1961
diff changeset
703 break;
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
704 case CMD_SERVER_URL_GET:
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
705 start_reply(mw, CMD_OK);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
706 // FIXME: This should be get from config file
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
707 mw_puts(mw, "doragasu.com");
1970
41b9509ede38 megawifi: implement CMD_DATETIME
doragasu <doragasu@hotmail.com>
parents: 1969
diff changeset
708 mw_putc(mw,'\0');
1960
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
709 end_reply(mw);
f79e16d8baa4 megawifi: stub common commands to get config.
doragasu <doragasu@hotmail.com>
parents: 1959
diff changeset
710 break;
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
711 default:
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
712 printf("Unhandled MegaWiFi command %s(%d) with length %X\n", cmd_names[command], command, size);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
713 break;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
714 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
715 }
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
716
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
717 static void process_packet(megawifi *mw)
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
718 {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
719 if (mw->transmit_channel == 0) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
720 process_command(mw);
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
721 } else {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
722 uint8_t channel = mw->transmit_channel - 1;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
723 int channel_state = mw->channel_state[channel];
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
724 int sock_fd = mw->sock_fds[channel];
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
725 if (sock_fd >= 0 && channel_state == SOCKST_TCP_EST) {
1968
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
726 int sent = send(sock_fd, (char*)mw->transmit_buffer, mw->transmit_bytes, 0);
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
727 if (sent < 0 && !socket_error_is_wouldblock()) {
c16dabdb0aad megawifi: use util module socket functions for WIN32 compatibility
doragasu <doragasu@hotmail.com>
parents: 1966
diff changeset
728 socket_close(sock_fd);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
729 mw->sock_fds[channel] = -1;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
730 mw->channel_state[channel] = SOCKST_NONE;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
731 mw->channel_flags |= 1 << mw->transmit_channel;
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
732 } else if (sent < mw->transmit_bytes) {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
733 //TODO: save this data somewhere so it can be sent in poll_socket
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
734 printf("Sent %d bytes on channel %d, but %d were requested\n", sent, mw->transmit_channel, mw->transmit_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
735 }
1969
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
736 } else if (sock_fd >= 0 && channel_state == SOCKST_UDP_READY) {
852393cdec7c megawifi: implement UDP sockets
doragasu <doragasu@hotmail.com>
parents: 1968
diff changeset
737 udp_send(mw, channel);
1959
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
738 } else {
6d99bdbf1e3e megawifi: refactor and update commands
doragasu <doragasu@hotmail.com>
parents: 1546
diff changeset
739 printf("Unhandled receive of MegaWiFi data on channel %d\n", mw->transmit_channel);
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
740 }
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
741 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
742 mw->transmit_bytes = mw->expected_bytes = 0;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
743 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
744
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
745 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
746 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
747 if (!(address & 1)) {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
748 return context;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
749 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
750 megawifi *mw = get_megawifi(context);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
751 address = address >> 1 & 7;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
752 switch (address)
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
753 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
754 case 0:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
755 switch (mw->transmit_state)
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
756 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
757 case TX_IDLE:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
758 if (value == STX) {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
759 mw->transmit_state = TX_LEN1;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
760 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
761 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
762 case TX_LEN1:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
763 mw->transmit_channel = value >> 4;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
764 mw->expected_bytes = value << 8 & 0xF00;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
765 mw->transmit_state = TX_LEN2;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
766 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
767 case TX_LEN2:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
768 mw->expected_bytes |= value;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
769 mw->transmit_state = TX_PAYLOAD;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
770 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
771 case TX_PAYLOAD:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
772 mw->transmit_buffer[mw->transmit_bytes++] = value;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
773 if (mw->transmit_bytes == mw->expected_bytes) {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
774 mw->transmit_state = TX_WAIT_ETX;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
775 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
776 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
777 case TX_WAIT_ETX:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
778 if (value == ETX) {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
779 mw->transmit_state = TX_IDLE;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
780 process_packet(mw);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
781 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
782 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
783 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
784 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
785 case 7:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
786 mw->scratchpad = value;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
787 break;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
788 default:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
789 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
790 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
791 return context;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
792 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
793
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
794 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
795 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
796 return megawifi_write_b(address | 1, context, value);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
797 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
798
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
799 uint8_t megawifi_read_b(uint32_t address, void *context)
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
800 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
801
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
802 if (!(address & 1)) {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
803 return 0xFF;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
804 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
805 megawifi *mw = get_megawifi(context);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
806 address = address >> 1 & 7;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
807 switch (address)
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
808 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
809 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
810 poll_all_sockets(mw);
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
811 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
812 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
813 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
814 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
815 }
aaab852803ac Get enough of MegaWifi implemented so that basic commands from wflash CLI command work
Michael Pavone <pavone@retrodev.com>
parents: 1515
diff changeset
816 return ret;
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
817 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
818 return 0xFF;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
819 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
820 poll_all_sockets(mw);
1513
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
821 //line status
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
822 return 0x60 | (mw->receive_read < mw->receive_bytes);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
823 case 7:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
824 return mw->scratchpad;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
825 default:
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
826 printf("Unhandled read from MegaWiFi UART register %X\n", address);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
827 return 0xFF;
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
828 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
829 }
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
830
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
831 uint16_t megawifi_read_w(uint32_t address, void *context)
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
832 {
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
833 return 0xFF00 | megawifi_read_b(address | 1, context);
8f3b6a64b658 Initial work on MegaWiFi support
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
834 }