annotate megawifi.c @ 1969:852393cdec7c

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