| e8555c89 Andy McCall |
1 |
#ifndef EMULATEDESP32_HH |
| 2 |
#define EMULATEDESP32_HH |
| 3 |
|
| 4 |
#include "BooleanSetting.hh" |
| 5 |
#include "DeviceConfig.hh" |
| 6 |
#include "EmuTime.hh" |
| 7 |
#include "Socket.hh" |
| 8 |
#include "circular_buffer.hh" |
| 9 |
|
| 10 |
#include <array> |
| 11 |
#include <atomic> |
| 12 |
#include <chrono> |
| 13 |
#include <condition_variable> |
| 14 |
#include <cstdint> |
| 15 |
#include <functional> |
| 16 |
#include <memory> |
| 17 |
#include <mutex> |
| 18 |
#include <span> |
| 19 |
#include <string> |
| 20 |
#include <string_view> |
| 21 |
#include <thread> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
namespace openmsx { |
| 25 |
|
| 26 |
|
| 27 |
class CliComm; |
| 28 |
namespace OpenSSL { |
| 29 |
struct ErrorCode; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class EmulatedEsp32 |
| 40 |
{ |
| 41 |
public: |
| 42 |
EmulatedEsp32(const DeviceConfig& config, std::function<void(std::span<const uint8_t>)> sink); |
| 43 |
~EmulatedEsp32(); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
void recvByte(uint8_t value, EmuTime time); |
| 49 |
void setBaudRate(unsigned baud) const; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
void resetState(); |
| 56 |
|
| 57 |
void resetFifo(); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
void start(); |
| 64 |
void stop(); |
| 65 |
|
| 66 |
private: |
| 67 |
|
| 68 |
void espThreadFunc(); |
| 69 |
void pushBootText(std::string_view text) const; |
| 70 |
void resetParser(); |
| 71 |
|
| 72 |
|
| 73 |
void processCommand(); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
[[nodiscard]] bool espWaitForWork(std::unique_lock<std::mutex>& lock, |
| 78 |
std::function<void()>& unlocked); |
| 79 |
|
| 80 |
|
| 81 |
void espParseByte(uint8_t b, std::function<void()>& unlocked); |
| 82 |
void tcpReaderThreadFunc(int connIdx); |
| 83 |
|
| 84 |
|
| 85 |
class Connection; |
| 86 |
struct ListenSocket; |
| 87 |
static constexpr int MAX_CONNECTIONS = 4; |
| 88 |
|
| 89 |
|
| 90 |
void tcpAcceptClient(Connection* conn) const; |
| 91 |
void tcpDriveHandshake(Connection* conn, int connIdx); |
| 92 |
void tcpTlsFail(Connection* conn, int connIdx, uint8_t reason); |
| 93 |
|
| 94 |
|
| 95 |
void logTlsError(OpenSSL::ErrorCode code) const; |
| 96 |
|
| 97 |
|
| 98 |
[[nodiscard]] bool tcpReadData(Connection* conn, int connIdx); |
| 99 |
void tcpDrainSsl(Connection* conn, int connIdx); |
| 100 |
void tcpReadClient(Connection* conn) const; |
| 101 |
|
| 102 |
|
| 103 |
[[nodiscard]] bool tcpPollConnection(Connection* conn, int connIdx); |
| 104 |
|
| 105 |
Connection* allocateConnection(); |
| 106 |
void resetConnectionSlot(int i); |
| 107 |
void freeConnection(int idx); |
| 108 |
Connection* getConnection(int idx) const; |
| 109 |
int getFreeConnectionSlot() const; |
| 110 |
|
| 111 |
|
| 112 |
void handleCustomCommand(uint8_t cmd, std::span<const uint8_t> data); |
| 113 |
void handleUnapiCommand(uint8_t cmd, std::span<const uint8_t> data); |
| 114 |
|
| 115 |
|
| 116 |
void sendQuickResponse(uint8_t cmdByte, uint8_t errorCode); |
| 117 |
void sendResponse(uint8_t cmdByte, uint8_t errorCode, |
| 118 |
std::span<const uint8_t> data = {}); |
| 119 |
void sendRawResponse(std::span<const uint8_t> data); |
| 120 |
|
| 121 |
|
| 122 |
std::vector<uint8_t> lastResponse; |
| 123 |
void saveLastResponse(std::span<const uint8_t> data); |
| 124 |
|
| 125 |
|
| 126 |
void cmdReset(std::span<const uint8_t> data); |
| 127 |
void cmdWarmReset(std::span<const uint8_t> data); |
| 128 |
void cmdQuery(std::span<const uint8_t> data); |
| 129 |
void cmdGetVersion(std::span<const uint8_t> data); |
| 130 |
void cmdRetry(std::span<const uint8_t> data); |
| 131 |
void cmdScanAP(std::span<const uint8_t> data); |
| 132 |
void cmdScanResults(std::span<const uint8_t> data); |
| 133 |
void cmdConnectAP(std::span<const uint8_t> data); |
| 134 |
void cmdGetAPStatus(std::span<const uint8_t> data); |
| 135 |
void cmdGetBoard(std::span<const uint8_t> data); |
| 136 |
void cmdFirmwareUpdate(uint8_t cmd, std::span<const uint8_t> data); |
| 137 |
void cmdGetSettings(std::span<const uint8_t> data); |
| 138 |
void cmdAutoClock(uint8_t cmd, std::span<const uint8_t> data); |
| 139 |
void cmdSetWiFiTimer(std::span<const uint8_t> data); |
| 140 |
void cmdGetDateTime(std::span<const uint8_t> data); |
| 141 |
|
| 142 |
|
| 143 |
void unimplementedCmd(uint8_t cmd); |
| 144 |
void cmdGetInfo(std::span<const uint8_t> data); |
| 145 |
void cmdGetCapab(std::span<const uint8_t> data); |
| 146 |
void cmdGetIPInfo(std::span<const uint8_t> data); |
| 147 |
void cmdNetState(std::span<const uint8_t> data); |
| 148 |
void cmdDnsQ(std::span<const uint8_t> data); |
| 149 |
void cmdDnsQNew(std::span<const uint8_t> data); |
| 150 |
void cmdUdpOpen(std::span<const uint8_t> data); |
| 151 |
void cmdUdpClose(std::span<const uint8_t> data); |
| 152 |
void cmdUdpState(std::span<const uint8_t> data); |
| 153 |
void cmdUdpSend(std::span<const uint8_t> data); |
| 154 |
void cmdUdpRcv(std::span<const uint8_t> data); |
| 155 |
void cmdTcpOpen(std::span<const uint8_t> data); |
| 156 |
void cmdTcpOpenPassive(uint16_t localPort, uint8_t flags); |
| 157 |
void cmdTcpOpenActive(uint32_t remoteIP, uint16_t remotePort, |
| 158 |
uint16_t localPort, uint8_t flags, |
| 159 |
const std::string& hostname, |
| 160 |
bool useTls, bool verifyCert); |
| 161 |
void cmdTcpClose(std::span<const uint8_t> data); |
| 162 |
void cmdTcpAbort(std::span<const uint8_t> data); |
| 163 |
void cmdTcpState(std::span<const uint8_t> data); |
| 164 |
void cmdTcpSend(std::span<const uint8_t> data); |
| 165 |
void cmdTcpRcv(std::span<const uint8_t> data); |
| 166 |
void cmdCfgAutoIP(std::span<const uint8_t> data); |
| 167 |
void cmdCfgIP(std::span<const uint8_t> data); |
| 168 |
|
| 169 |
|
| 170 |
[[nodiscard]] bool parseIP(const std::string& str, std::span<uint8_t, 4> ipOut) const; |
| 171 |
|
| 172 |
|
| 173 |
void pickEphemeralTcpPort(uint16_t& port); |
| 174 |
|
| 175 |
|
| 176 |
BooleanSetting enabledSetting; |
| 177 |
|
| 178 |
|
| 179 |
CliComm& cliComm; |
| 180 |
|
| 181 |
|
| 182 |
cb_queue<uint8_t> msxToEspFifo; |
| 183 |
mutable std::mutex msxToEspMutex; |
| 184 |
std::condition_variable msxToEspCond; |
| 185 |
|
| 186 |
|
| 187 |
std::thread espThread; |
| 188 |
std::atomic<bool> espRunning{false}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
bool bootStartPending = false; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
enum class ParserState : uint8_t { IDLE, WAIT_DATA_SIZE, GET_DATA }; |
| 198 |
ParserState parserState = ParserState::IDLE; |
| 199 |
uint8_t parserCmdByte = 0; |
| 200 |
uint8_t parserSizeStep = 0; |
| 201 |
uint16_t parserExpectedSize = 0; |
| 202 |
std::vector<uint8_t> parserDataBuf; |
| 203 |
std::chrono::steady_clock::time_point parserDeadline; |
| 204 |
|
| 205 |
enum class BootStage : uint8_t { NONE, READY_LOOP }; |
| 206 |
BootStage bootStage = BootStage::NONE; |
| 207 |
uint8_t bootReadyRetries = 0; |
| 208 |
std::chrono::steady_clock::time_point bootEventTime; |
| 209 |
|
| 210 |
|
| 211 |
mutable std::mutex connectionsMutex; |
| 212 |
std::array<std::unique_ptr<Connection>, MAX_CONNECTIONS> connections; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
std::array<std::atomic<uint8_t>, MAX_CONNECTIONS> closeReason = {}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
std::vector<std::weak_ptr<ListenSocket>> listenSockets; |
| 228 |
|
| 229 |
|
| 230 |
[[no_unique_address]] SocketActivator socketActivator; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
std::function<void(std::span<const uint8_t>)> sink; |
| 235 |
}; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
#endif |