Skip to content

Commit

Permalink
rainbow: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BrokeStudio committed Nov 4, 2023
1 parent ccb81e6 commit b941536
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 61 deletions.
74 changes: 33 additions & 41 deletions Core/NES/Mappers/Homebrew/RainbowESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
#include "Utilities/CRC32.h"

#include <assert.h>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <map>
#include <regex>
#include <sstream>
#include <stdexcept>

#ifdef _WIN32

Expand Down Expand Up @@ -165,7 +157,7 @@ uint8_t BrokeStudioFirmware::tx()

// Fill buffer with the next message (if needed)
if(this->tx_buffer.empty() && !this->tx_messages.empty()) {
std::deque<uint8_t> message = this->tx_messages.front();
deque<uint8_t> message = this->tx_messages.front();
this->tx_buffer.insert(this->tx_buffer.end(), message.begin(), message.end());
this->tx_messages.pop_front();
}
Expand All @@ -192,7 +184,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<std::deque<uint8_t>::size_type>(message_size) + 1); // Buffer size must match declared payload size
assert(this->rx_buffer.size() == static_cast<deque<uint8_t>::size_type>(message_size) + 1); // Buffer size must match declared payload size

// Process the message in RX buffer
switch(static_cast<toesp_cmds_t>(this->rx_buffer.at(1))) {
Expand Down Expand Up @@ -228,7 +220,7 @@ void BrokeStudioFirmware::processBufferedMessage()
string dbgMessage = "[Rainbow] Data: ";
static bool isRainbowDebugEnabled = RAINBOW_DEBUG_ESP > 0;
if(isRainbowDebugEnabled || (this->debug_config & 1)) {
for(std::deque<uint8_t>::const_iterator cur = this->rx_buffer.begin() + 2; cur < this->rx_buffer.end(); ++cur) {
for(deque<uint8_t>::const_iterator cur = this->rx_buffer.begin() + 2; cur < this->rx_buffer.end(); ++cur) {
dbgMessage += HexUtilities::ToHex(*cur) + " ";
}
UDBG(dbgMessage);
Expand All @@ -251,7 +243,7 @@ void BrokeStudioFirmware::processBufferedMessage()

size_t i = 0;
for(
std::deque<std::deque<uint8_t>>::iterator message = this->tx_messages.end();
deque<deque<uint8_t>>::iterator message = this->tx_messages.end();
message != this->tx_messages.begin();
) {
--message;
Expand Down Expand Up @@ -434,7 +426,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;
Expand Down Expand Up @@ -464,7 +456,7 @@ void BrokeStudioFirmware::processBufferedMessage()
static_cast<uint8_t>(fromesp_cmds_t::SERVER_SETTINGS)
});
} else {
std::deque<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(1 + 2 + 1 + this->server_settings_address.size()),
static_cast<uint8_t>(fromesp_cmds_t::SERVER_SETTINGS),
static_cast<uint8_t>(this->server_settings_port >> 8),
Expand Down Expand Up @@ -495,7 +487,7 @@ void BrokeStudioFirmware::processBufferedMessage()
static_cast<uint8_t>(fromesp_cmds_t::SERVER_SETTINGS)
});
} else {
std::deque<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(1 + 2 + 1 + this->default_server_settings_address.size()),
static_cast<uint8_t>(fromesp_cmds_t::SERVER_SETTINGS),
static_cast<uint8_t>(this->default_server_settings_port >> 8),
Expand Down Expand Up @@ -538,8 +530,8 @@ void BrokeStudioFirmware::processBufferedMessage()
{
UDBG("[Rainbow] ESP received command SERVER_SEND_MSG");
uint8_t const payload_size = static_cast<const uint8_t>(this->rx_buffer.size() - 2);
std::deque<uint8_t>::const_iterator payload_begin = this->rx_buffer.begin() + 2;
std::deque<uint8_t>::const_iterator payload_end = payload_begin + payload_size;
deque<uint8_t>::const_iterator payload_begin = this->rx_buffer.begin() + 2;
deque<uint8_t>::const_iterator payload_end = payload_begin + payload_size;

switch(this->active_protocol) {
case server_protocol_t::TCP:
Expand Down Expand Up @@ -603,7 +595,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<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(2 + 1 + this->networks[networkItem].ssid.length() + 1 + this->networks[networkItem].pass.length()),
static_cast<uint8_t>(fromesp_cmds_t::NETWORK_REGISTERED_DETAILS),
static_cast<uint8_t>(this->networks[networkItem].active ? 1 : 0),
Expand Down Expand Up @@ -678,7 +670,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<uint8_t>() };
FileStruct temp_file = { file_config.drive, filename, vector<uint8_t>() };
this->files.push_back(temp_file);
}
}
Expand All @@ -687,7 +679,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<uint8_t>() };
FileStruct temp_file = { file_config.drive, filename, vector<uint8_t>() };
this->files.push_back(temp_file);
}
}
Expand Down Expand Up @@ -728,7 +720,7 @@ void BrokeStudioFirmware::processBufferedMessage()
} else if(file_config.access_mode == static_cast<uint8_t>(file_config_flags_t::ACCESS_MODE_MANUAL)) {
string filename = this->working_file.file->filename;
filename = filename.substr(filename.find_first_of("/") + 1);
std::deque<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(3 + filename.size()),
static_cast<uint8_t>(fromesp_cmds_t::FILE_STATUS),
1,
Expand Down Expand Up @@ -930,7 +922,7 @@ void BrokeStudioFirmware::processBufferedMessage()

if(file_config.access_mode == static_cast<uint8_t>(file_config_flags_t::ACCESS_MODE_AUTO)) {
if(message_size >= 3) {
std::vector<uint8_t> existing_files;
vector<uint8_t> existing_files;
uint8_t const path = this->rx_buffer.at(3);
uint8_t page_size = NUM_FILES;
uint8_t current_page = 0;
Expand Down Expand Up @@ -965,7 +957,7 @@ void BrokeStudioFirmware::processBufferedMessage()
if(nb_files >= page_end) break;
}

std::deque<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(existing_files.size() + 2),
static_cast<uint8_t>(fromesp_cmds_t::FILE_LIST),
static_cast<uint8_t>(existing_files.size())
Expand Down Expand Up @@ -1275,19 +1267,19 @@ string BrokeStudioFirmware::getAutoFilename(uint8_t path, uint8_t file)
void BrokeStudioFirmware::readFile(uint8_t n)
{
// Get data range
std::vector<uint8_t>::const_iterator data_begin;
std::vector<uint8_t>::const_iterator data_end;
vector<uint8_t>::const_iterator data_begin;
vector<uint8_t>::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<std::vector<uint8_t>::size_type>(this->working_file.offset) + n, this->working_file.file->data.size());
data_end = this->working_file.file->data.begin() + min(static_cast<vector<uint8_t>::size_type>(this->working_file.offset) + n, this->working_file.file->data.size());
}
std::vector<uint8_t>::size_type const data_size = data_end - data_begin;
vector<uint8_t>::size_type const data_size = data_end - data_begin;

// Write response
std::deque<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(data_size + 2),
static_cast<uint8_t>(fromesp_cmds_t::FILE_DATA),
static_cast<uint8_t>(data_size)
Expand All @@ -1309,7 +1301,7 @@ void BrokeStudioFirmware::writeFile(I data_begin, I data_end)
this->working_file.file->data.resize(offset_end, 0);
}

for(std::vector<uint8_t>::size_type i = this->working_file.offset; i < offset_end; ++i) {
for(vector<uint8_t>::size_type i = this->working_file.offset; i < offset_end; ++i) {
this->working_file.file->data[i] = *data_begin;
++data_begin;
}
Expand Down Expand Up @@ -1355,7 +1347,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;
Expand Down Expand Up @@ -1449,7 +1441,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;
Expand Down Expand Up @@ -1558,7 +1550,7 @@ void BrokeStudioFirmware::sendUdpDatagramToServer(I begin, I end)

if(this->udp_socket != -1) {
size_t message_size = end - begin;
std::vector<uint8_t> aggregated;
vector<uint8_t> aggregated;
aggregated.reserve(message_size);
aggregated.insert(aggregated.end(), begin, end);

Expand Down Expand Up @@ -1597,7 +1589,7 @@ void BrokeStudioFirmware::sendTcpDataToServer(I begin, I end)

if(this->tcp_socket != -1) {
size_t message_size = end - begin;
std::vector<uint8_t> aggregated;
vector<uint8_t> aggregated;
aggregated.reserve(message_size);
aggregated.insert(aggregated.end(), begin, end);

Expand All @@ -1620,7 +1612,7 @@ void BrokeStudioFirmware::sendTcpDataToServer(I begin, I end)
}
}

std::deque<uint8_t> BrokeStudioFirmware::read_socket(int socket)
deque<uint8_t> BrokeStudioFirmware::read_socket(int socket)
{
fd_set rfds;
FD_ZERO(&rfds);
Expand All @@ -1642,7 +1634,7 @@ std::deque<uint8_t> BrokeStudioFirmware::read_socket(int socket)
} else if(n_readable > 0) {
if(FD_ISSET(socket, &rfds)) {
size_t const MAX_MSG_SIZE = 254;
std::vector<uint8_t> data;
vector<uint8_t> data;
data.resize(MAX_MSG_SIZE);

sockaddr_in addr_from;
Expand All @@ -1669,7 +1661,7 @@ std::deque<uint8_t> BrokeStudioFirmware::read_socket(int socket)
}
UDBG(dbgMessage);
#endif
std::deque<uint8_t> message({
deque<uint8_t> message({
static_cast<uint8_t>(msg_len + 1),
static_cast<uint8_t>(fromesp_cmds_t::MESSAGE_FROM_SERVER)
});
Expand All @@ -1681,22 +1673,22 @@ std::deque<uint8_t> BrokeStudioFirmware::read_socket(int socket)
}
}
}
return std::deque<uint8_t>();
return deque<uint8_t>();
}

void BrokeStudioFirmware::receiveDataFromServer()
{
// TCP
if(this->tcp_socket != -1) {
std::deque<uint8_t> message = read_socket(this->tcp_socket);
deque<uint8_t> message = read_socket(this->tcp_socket);
if(!message.empty()) {
this->tx_messages.push_back(message);
}
}

// UDP
if(this->udp_socket != -1) {
std::deque<uint8_t> message = read_socket(this->udp_socket);
deque<uint8_t> message = read_socket(this->udp_socket);
if(!message.empty()) {
this->tx_messages.push_back(message);
}
Expand Down Expand Up @@ -1880,7 +1872,7 @@ namespace
{
size_t download_write_callback(char* ptr, size_t size, size_t nmemb, void* userdata)
{
std::vector<uint8_t>* data = reinterpret_cast<std::vector<uint8_t>*>(userdata);
vector<uint8_t>* data = reinterpret_cast<vector<uint8_t>*>(userdata);
data->insert(data->end(), reinterpret_cast<uint8_t*>(ptr), reinterpret_cast<uint8_t*>(ptr + size * nmemb));
return size * nmemb;
}
Expand Down Expand Up @@ -1949,7 +1941,7 @@ void BrokeStudioFirmware::downloadFile(string const& url, uint8_t path, uint8_t
*/
/* disable CURL for now
// Download file
std::vector<uint8_t> data;
vector<uint8_t> 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);
Expand Down
43 changes: 23 additions & 20 deletions Core/NES/Mappers/Homebrew/RainbowESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <deque>
#include <thread>

using std::pair;
using std::array;

#ifdef _WIN32
#include <winsock2.h>
#else
Expand Down Expand Up @@ -37,8 +40,8 @@ static const uint8_t DBG_CFG_NETWORK = 0x04;

struct NetworkInfo
{
std::string ssid;
std::string pass;
string ssid;
string pass;
bool active;
};

Expand All @@ -51,8 +54,8 @@ struct FileConfig
struct FileStruct
{
uint8_t drive;
std::string filename;
std::vector<uint8_t> data;
string filename;
vector<uint8_t> data;
};

struct WorkingFile
Expand Down Expand Up @@ -263,9 +266,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<class I>
void writeFile(I data_begin, I data_end);
Expand All @@ -287,30 +290,30 @@ class BrokeStudioFirmware
//void pingRequest(uint8_t n);
//void receivePingResult();

std::pair<bool, sockaddr> resolve_server_address();
static std::deque<uint8_t> read_socket(int socket);
pair<bool, sockaddr> resolve_server_address();
static deque<uint8_t> read_socket(int socket);

void initDownload();
//static std::pair<uint8_t, uint8_t> curle_to_net_error(CURLcode curle);
void downloadFile(std::string const& url, uint8_t path, uint8_t file);
//static pair<uint8_t, uint8_t> curle_to_net_error(CURLcode curle);
void downloadFile(string const& url, uint8_t path, uint8_t file);
void cleanupDownload();

private:
std::deque<uint8_t> rx_buffer;
std::deque<uint8_t> tx_buffer;
std::deque<std::deque<uint8_t>> tx_messages;
deque<uint8_t> rx_buffer;
deque<uint8_t> tx_buffer;
deque<deque<uint8_t>> tx_messages;

bool isEspFlashFilePresent = false;
bool isSdCardFilePresent = false;
WorkingFile working_file;
std::vector<FileStruct> files;
vector<FileStruct> files;

std::array<NetworkInfo, NUM_NETWORKS> networks;
array<NetworkInfo, NUM_NETWORKS> 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;
Expand All @@ -320,8 +323,8 @@ class BrokeStudioFirmware
uint8_t ping_avg = 0;
uint8_t ping_max = 0;
uint8_t ping_lost = 0;
std::atomic<bool> ping_ready;
std::thread ping_thread;
atomic<bool> ping_ready;
thread ping_thread;

int udp_socket = -1;
sockaddr server_addr;
Expand Down

0 comments on commit b941536

Please sign in to comment.