From fe0bd447b9783ee29a5943f5a61ba1fb31cc7802 Mon Sep 17 00:00:00 2001 From: BrokeStudio Date: Sat, 4 Nov 2023 18:06:35 +0100 Subject: [PATCH] rainbow: cleanup --- Core/NES/Mappers/Homebrew/RainbowESP.cpp | 84 ++++++++++-------------- Core/NES/Mappers/Homebrew/RainbowESP.h | 54 +++++++++------ 2 files changed, 69 insertions(+), 69 deletions(-) diff --git a/Core/NES/Mappers/Homebrew/RainbowESP.cpp b/Core/NES/Mappers/Homebrew/RainbowESP.cpp index 719ba0b50..cb337dd93 100644 --- a/Core/NES/Mappers/Homebrew/RainbowESP.cpp +++ b/Core/NES/Mappers/Homebrew/RainbowESP.cpp @@ -6,18 +6,10 @@ #include "Utilities/HexUtilities.h" #include "Utilities/CRC32.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - #ifdef _WIN32 +#define ERR_MSG_SIZE 513 + // UDP networking #pragma comment(lib,"ws2_32.lib") //Winsock Library #define WIN32_LEAN_AND_MEAN @@ -30,8 +22,6 @@ typedef SSIZE_T ssize_t; #define cast_network_payload(x) reinterpret_cast(x) #define close_sock(x) closesocket(x) -#define ERR_MSG_SIZE 513 - #else // UDP networking @@ -45,10 +35,6 @@ typedef SSIZE_T ssize_t; #define cast_network_payload(x) reinterpret_cast(x) #define close_sock(x) ::close(x) -#ifndef min -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#endif - #endif #ifndef RAINBOW_DEBUG_ESP @@ -165,7 +151,7 @@ uint8_t BrokeStudioFirmware::tx() // Fill buffer with the next message (if needed) if(this->tx_buffer.empty() && !this->tx_messages.empty()) { - std::deque message = this->tx_messages.front(); + deque message = this->tx_messages.front(); this->tx_buffer.insert(this->tx_buffer.end(), message.begin(), message.end()); this->tx_messages.pop_front(); } @@ -192,7 +178,7 @@ void BrokeStudioFirmware::processBufferedMessage() assert(this->rx_buffer.size() >= 2); // Buffer must contain exactly one message, minimal message is two bytes (length + type) uint8_t const message_size = this->rx_buffer.front(); assert(message_size >= 1); // minimal payload is one byte (type) - assert(this->rx_buffer.size() == static_cast::size_type>(message_size) + 1); // Buffer size must match declared payload size + assert(this->rx_buffer.size() == static_cast::size_type>(message_size) + 1); // Buffer size must match declared payload size // Process the message in RX buffer switch(static_cast(this->rx_buffer.at(1))) { @@ -228,7 +214,7 @@ void BrokeStudioFirmware::processBufferedMessage() string dbgMessage = "[Rainbow] Data: "; static bool isRainbowDebugEnabled = RAINBOW_DEBUG_ESP > 0; if(isRainbowDebugEnabled || (this->debug_config & 1)) { - for(std::deque::const_iterator cur = this->rx_buffer.begin() + 2; cur < this->rx_buffer.end(); ++cur) { + for(deque::const_iterator cur = this->rx_buffer.begin() + 2; cur < this->rx_buffer.end(); ++cur) { dbgMessage += HexUtilities::ToHex(*cur) + " "; } UDBG(dbgMessage); @@ -251,7 +237,7 @@ void BrokeStudioFirmware::processBufferedMessage() size_t i = 0; for( - std::deque>::iterator message = this->tx_messages.end(); + deque>::iterator message = this->tx_messages.end(); message != this->tx_messages.begin(); ) { --message; @@ -434,7 +420,7 @@ void BrokeStudioFirmware::processBufferedMessage() if(n == 0) { n = 4; } - //this->ping_thread = std::thread(&BrokeStudioFirmware::pingRequest, this, n); // TODO: add ping support + //this->ping_thread = thread(&BrokeStudioFirmware::pingRequest, this, n); // TODO: add ping support } } break; @@ -464,7 +450,7 @@ void BrokeStudioFirmware::processBufferedMessage() static_cast(fromesp_cmds_t::SERVER_SETTINGS) }); } else { - std::deque message({ + deque message({ static_cast(1 + 2 + 1 + this->server_settings_address.size()), static_cast(fromesp_cmds_t::SERVER_SETTINGS), static_cast(this->server_settings_port >> 8), @@ -495,7 +481,7 @@ void BrokeStudioFirmware::processBufferedMessage() static_cast(fromesp_cmds_t::SERVER_SETTINGS) }); } else { - std::deque message({ + deque message({ static_cast(1 + 2 + 1 + this->default_server_settings_address.size()), static_cast(fromesp_cmds_t::SERVER_SETTINGS), static_cast(this->default_server_settings_port >> 8), @@ -538,8 +524,8 @@ void BrokeStudioFirmware::processBufferedMessage() { UDBG("[Rainbow] ESP received command SERVER_SEND_MSG"); uint8_t const payload_size = static_cast(this->rx_buffer.size() - 2); - std::deque::const_iterator payload_begin = this->rx_buffer.begin() + 2; - std::deque::const_iterator payload_end = payload_begin + payload_size; + deque::const_iterator payload_begin = this->rx_buffer.begin() + 2; + deque::const_iterator payload_end = payload_begin + payload_size; switch(this->active_protocol) { case server_protocol_t::TCP: @@ -603,7 +589,7 @@ void BrokeStudioFirmware::processBufferedMessage() if(message_size == 2) { uint8_t networkItem = this->rx_buffer.at(2); if(networkItem > NUM_NETWORKS - 1) networkItem = NUM_NETWORKS - 1; - std::deque message({ + deque message({ static_cast(2 + 1 + this->networks[networkItem].ssid.length() + 1 + this->networks[networkItem].pass.length()), static_cast(fromesp_cmds_t::NETWORK_REGISTERED_DETAILS), static_cast(this->networks[networkItem].active ? 1 : 0), @@ -678,7 +664,7 @@ void BrokeStudioFirmware::processBufferedMessage() filename = getAutoFilename(path, file); int i = findFile(file_config.drive, filename); if(i == -1) { - FileStruct temp_file = { file_config.drive, filename, std::vector() }; + FileStruct temp_file = { file_config.drive, filename, vector() }; this->files.push_back(temp_file); } } @@ -687,7 +673,7 @@ void BrokeStudioFirmware::processBufferedMessage() filename = string(this->rx_buffer.begin() + 4, this->rx_buffer.begin() + 4 + path_length); int i = findFile(file_config.drive, filename); if(i == -1) { - FileStruct temp_file = { file_config.drive, filename, std::vector() }; + FileStruct temp_file = { file_config.drive, filename, vector() }; this->files.push_back(temp_file); } } @@ -728,7 +714,7 @@ void BrokeStudioFirmware::processBufferedMessage() } else if(file_config.access_mode == static_cast(file_config_flags_t::ACCESS_MODE_MANUAL)) { string filename = this->working_file.file->filename; filename = filename.substr(filename.find_first_of("/") + 1); - std::deque message({ + deque message({ static_cast(3 + filename.size()), static_cast(fromesp_cmds_t::FILE_STATUS), 1, @@ -930,7 +916,7 @@ void BrokeStudioFirmware::processBufferedMessage() if(file_config.access_mode == static_cast(file_config_flags_t::ACCESS_MODE_AUTO)) { if(message_size >= 3) { - std::vector existing_files; + vector existing_files; uint8_t const path = this->rx_buffer.at(3); uint8_t page_size = NUM_FILES; uint8_t current_page = 0; @@ -965,7 +951,7 @@ void BrokeStudioFirmware::processBufferedMessage() if(nb_files >= page_end) break; } - std::deque message({ + deque message({ static_cast(existing_files.size() + 2), static_cast(fromesp_cmds_t::FILE_LIST), static_cast(existing_files.size()) @@ -1275,19 +1261,19 @@ string BrokeStudioFirmware::getAutoFilename(uint8_t path, uint8_t file) void BrokeStudioFirmware::readFile(uint8_t n) { // Get data range - std::vector::const_iterator data_begin; - std::vector::const_iterator data_end; + vector::const_iterator data_begin; + vector::const_iterator data_end; if(this->working_file.offset >= this->working_file.file->data.size()) { data_begin = this->working_file.file->data.end(); data_end = data_begin; } else { data_begin = this->working_file.file->data.begin() + this->working_file.offset; - data_end = this->working_file.file->data.begin() + min(static_cast::size_type>(this->working_file.offset) + n, this->working_file.file->data.size()); + data_end = this->working_file.file->data.begin() + min(static_cast::size_type>(this->working_file.offset) + n, this->working_file.file->data.size()); } - std::vector::size_type const data_size = data_end - data_begin; + vector::size_type const data_size = data_end - data_begin; // Write response - std::deque message({ + deque message({ static_cast(data_size + 2), static_cast(fromesp_cmds_t::FILE_DATA), static_cast(data_size) @@ -1309,7 +1295,7 @@ void BrokeStudioFirmware::writeFile(I data_begin, I data_end) this->working_file.file->data.resize(offset_end, 0); } - for(std::vector::size_type i = this->working_file.offset; i < offset_end; ++i) { + for(vector::size_type i = this->working_file.offset; i < offset_end; ++i) { this->working_file.file->data[i] = *data_begin; ++data_begin; } @@ -1355,7 +1341,7 @@ void BrokeStudioFirmware::saveFiles() void BrokeStudioFirmware::saveFile(uint8_t drive, char const* filename) { - std::ofstream ofs(filename, std::fstream::binary); + ofstream ofs(filename, std::fstream::binary); if(ofs.fail()) { MessageManager::Log("[Rainbow] Couldn't open RAINBOW_FILESYSTEM_FILE (" + string(filename) + ")"); return; @@ -1449,7 +1435,7 @@ void BrokeStudioFirmware::loadFiles() void BrokeStudioFirmware::loadFile(uint8_t drive, char const* filename) { - std::ifstream ifs(filename, std::fstream::binary); + ifstream ifs(filename, std::fstream::binary); if(ifs.fail()) { MessageManager::Log("[Rainbow] Couldn't open RAINBOW_FILESYSTEM_FILE (" + string(filename) + ")"); return; @@ -1558,7 +1544,7 @@ void BrokeStudioFirmware::sendUdpDatagramToServer(I begin, I end) if(this->udp_socket != -1) { size_t message_size = end - begin; - std::vector aggregated; + vector aggregated; aggregated.reserve(message_size); aggregated.insert(aggregated.end(), begin, end); @@ -1597,7 +1583,7 @@ void BrokeStudioFirmware::sendTcpDataToServer(I begin, I end) if(this->tcp_socket != -1) { size_t message_size = end - begin; - std::vector aggregated; + vector aggregated; aggregated.reserve(message_size); aggregated.insert(aggregated.end(), begin, end); @@ -1620,7 +1606,7 @@ void BrokeStudioFirmware::sendTcpDataToServer(I begin, I end) } } -std::deque BrokeStudioFirmware::read_socket(int socket) +deque BrokeStudioFirmware::read_socket(int socket) { fd_set rfds; FD_ZERO(&rfds); @@ -1642,7 +1628,7 @@ std::deque BrokeStudioFirmware::read_socket(int socket) } else if(n_readable > 0) { if(FD_ISSET(socket, &rfds)) { size_t const MAX_MSG_SIZE = 254; - std::vector data; + vector data; data.resize(MAX_MSG_SIZE); sockaddr_in addr_from; @@ -1669,7 +1655,7 @@ std::deque BrokeStudioFirmware::read_socket(int socket) } UDBG(dbgMessage); #endif - std::deque message({ + deque message({ static_cast(msg_len + 1), static_cast(fromesp_cmds_t::MESSAGE_FROM_SERVER) }); @@ -1681,14 +1667,14 @@ std::deque BrokeStudioFirmware::read_socket(int socket) } } } - return std::deque(); + return deque(); } void BrokeStudioFirmware::receiveDataFromServer() { // TCP if(this->tcp_socket != -1) { - std::deque message = read_socket(this->tcp_socket); + deque message = read_socket(this->tcp_socket); if(!message.empty()) { this->tx_messages.push_back(message); } @@ -1696,7 +1682,7 @@ void BrokeStudioFirmware::receiveDataFromServer() // UDP if(this->udp_socket != -1) { - std::deque message = read_socket(this->udp_socket); + deque message = read_socket(this->udp_socket); if(!message.empty()) { this->tx_messages.push_back(message); } @@ -1880,7 +1866,7 @@ namespace { size_t download_write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) { - std::vector* data = reinterpret_cast*>(userdata); + vector* data = reinterpret_cast*>(userdata); data->insert(data->end(), reinterpret_cast(ptr), reinterpret_cast(ptr + size * nmemb)); return size * nmemb; } @@ -1949,7 +1935,7 @@ void BrokeStudioFirmware::downloadFile(string const& url, uint8_t path, uint8_t */ /* disable CURL for now // Download file - std::vector data; + vector data; curl_easy_setopt(this->curl_handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(this->curl_handle, CURLOPT_WRITEDATA, (void*)&data); CURLcode res = curl_easy_perform(this->curl_handle); diff --git a/Core/NES/Mappers/Homebrew/RainbowESP.h b/Core/NES/Mappers/Homebrew/RainbowESP.h index a23d460b9..731634917 100644 --- a/Core/NES/Mappers/Homebrew/RainbowESP.h +++ b/Core/NES/Mappers/Homebrew/RainbowESP.h @@ -4,10 +4,24 @@ #define CURL_STATICLIB //#include "NES/Mappers/Homebrew/cURL/curl.h" +#include #include #include #include #include +#include + +using std::pair; +using std::array; +using std::vector; +using std::min; +using std::max; +using std::ifstream; +using std::ofstream; +using std::string; +using std::atomic; +using std::thread; +using std::deque; #ifdef _WIN32 #include @@ -37,8 +51,8 @@ static const uint8_t DBG_CFG_NETWORK = 0x04; struct NetworkInfo { - std::string ssid; - std::string pass; + string ssid; + string pass; bool active; }; @@ -51,8 +65,8 @@ struct FileConfig struct FileStruct { uint8_t drive; - std::string filename; - std::vector data; + string filename; + vector data; }; struct WorkingFile @@ -263,9 +277,9 @@ class BrokeStudioFirmware void processBufferedMessage(); FileConfig parseFileConfig(uint8_t config); - int findFile(uint8_t drive, std::string filename); - int findPath(uint8_t drive, std::string path); - std::string getAutoFilename(uint8_t path, uint8_t file); + int findFile(uint8_t drive, string filename); + int findPath(uint8_t drive, string path); + string getAutoFilename(uint8_t path, uint8_t file); void readFile(uint8_t n); template void writeFile(I data_begin, I data_end); @@ -287,30 +301,30 @@ class BrokeStudioFirmware //void pingRequest(uint8_t n); //void receivePingResult(); - std::pair resolve_server_address(); - static std::deque read_socket(int socket); + pair resolve_server_address(); + static deque read_socket(int socket); void initDownload(); - //static std::pair curle_to_net_error(CURLcode curle); - void downloadFile(std::string const& url, uint8_t path, uint8_t file); + //static pair curle_to_net_error(CURLcode curle); + void downloadFile(string const& url, uint8_t path, uint8_t file); void cleanupDownload(); private: - std::deque rx_buffer; - std::deque tx_buffer; - std::deque> tx_messages; + deque rx_buffer; + deque tx_buffer; + deque> tx_messages; bool isEspFlashFilePresent = false; bool isSdCardFilePresent = false; WorkingFile working_file; - std::vector files; + vector files; - std::array networks; + array networks; server_protocol_t active_protocol = server_protocol_t::TCP; - std::string default_server_settings_address; + string default_server_settings_address; uint16_t default_server_settings_port = 0; - std::string server_settings_address; + string server_settings_address; uint16_t server_settings_port = 0; uint8_t debug_config = 0; @@ -320,8 +334,8 @@ class BrokeStudioFirmware uint8_t ping_avg = 0; uint8_t ping_max = 0; uint8_t ping_lost = 0; - std::atomic ping_ready; - std::thread ping_thread; + atomic ping_ready; + thread ping_thread; int udp_socket = -1; sockaddr server_addr;