Skip to content

Commit

Permalink
deserialize dc11-configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed May 20, 2024
1 parent 6f20af4 commit a9d0f89
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 39 deletions.
2 changes: 1 addition & 1 deletion comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ comm *comm::deserialize(const JsonVariantConst j, bus *const b)

if (!d->begin()) {
delete d;
DOLOG(warning, false, "comm::deserialize: begin() failed");
DOLOG(warning, false, "comm::deserialize: begin() \"%s\" failed", type.c_str());
return nullptr;
}

Expand Down
1 change: 0 additions & 1 deletion comm_tcp_socket_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ JsonDocument comm_tcp_socket_client::serialize() const
comm_tcp_socket_client *comm_tcp_socket_client::deserialize(const JsonVariantConst j)
{
comm_tcp_socket_client *r = new comm_tcp_socket_client(j["host"].as<std::string>(), j["port"].as<int>());
r->begin(); // TODO error-checking

return r;
}
17 changes: 10 additions & 7 deletions comm_tcp_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ comm_tcp_socket_server::~comm_tcp_socket_server()
th->join();
delete th;
}

if (fd != INVALID_SOCKET)
close(fd);
if (cfd != INVALID_SOCKET)
close(cfd);

DOLOG(debug, false, "comm_tcp_socket_server: destructor for port %d finished", port);
}

bool comm_tcp_socket_server::begin()
Expand Down Expand Up @@ -159,7 +166,7 @@ void comm_tcp_socket_server::operator()()
fd = socket(AF_INET, SOCK_STREAM, 0);

int reuse_addr = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse_addr, sizeof(reuse_addr)) == -1) {
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&reuse_addr), sizeof(reuse_addr)) == -1) {
close(fd);
fd = INVALID_SOCKET;

Expand All @@ -176,10 +183,10 @@ void comm_tcp_socket_server::operator()()
listen_addr.sin_port = htons(port);

if (bind(fd, reinterpret_cast<struct sockaddr *>(&listen_addr), sizeof(listen_addr)) == -1) {
DOLOG(warning, true, "Cannot bind to port %d (send_datacomm_tcp_socket_server): %s", port, strerror(errno));

close(fd);
fd = INVALID_SOCKET;

DOLOG(warning, true, "Cannot bind to port %d (send_datacomm_tcp_socket_server)", port);
return;
}

Expand Down Expand Up @@ -227,9 +234,6 @@ void comm_tcp_socket_server::operator()()
}

DOLOG(info, true, "comm_tcp_socket_server thread terminating");

close(cfd);
close(fd);
}

JsonDocument comm_tcp_socket_server::serialize() const
Expand All @@ -246,7 +250,6 @@ JsonDocument comm_tcp_socket_server::serialize() const
comm_tcp_socket_server *comm_tcp_socket_server::deserialize(const JsonVariantConst j)
{
comm_tcp_socket_server *r = new comm_tcp_socket_server(j["port"].as<int>());
r->begin(); // TODO error-checking

return r;
}
5 changes: 5 additions & 0 deletions dc11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ dc11::~dc11()
th->join();
delete th;
}

for(auto & c : comm_interfaces) {
DOLOG(debug, false, "Stopping %s", c->get_identifier().c_str());
delete c;
}
}

void dc11::show_state(console *const cnsl) const
Expand Down
43 changes: 29 additions & 14 deletions debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void start_network(console *const cnsl);
extern SdFs SD;
#endif

#define SERIAL_CFG_FILE "/dc11.json"
#define SERIAL_CFG_FILE "dc11.json"

#if !defined(BUILD_FOR_RP2040)
std::optional<disk_backend *> select_nbd_server(console *const cnsl)
Expand Down Expand Up @@ -657,7 +657,7 @@ void tm11_unload_tape(bus *const b)
b->getTM11()->unload();
}

void serdc11(console *const cnsl, bus *const b, const std::string & filename)
void serdc11(console *const cnsl, bus *const b)
{
dc11 *d = b->getDC11();
if (!d) {
Expand All @@ -670,7 +670,7 @@ void serdc11(console *const cnsl, bus *const b, const std::string & filename)
bool ok = false;

#if IS_POSIX
FILE *fh = fopen(filename.c_str(), "w");
FILE *fh = fopen(SERIAL_CFG_FILE, "w");
if (fh) {
state_writer ws { fh };
serializeJsonPretty(j, ws);
Expand All @@ -679,16 +679,31 @@ void serdc11(console *const cnsl, bus *const b, const std::string & filename)
ok = true;
}
#elif defined(ESP32)
File dataFile = LittleFS.open(SERIAL_CFG_FILE, "w");
if (dataFile) {
serializeJsonPretty(j, dataFile);
dataFile.close();
File data_file = LittleFS.open("/" SERIAL_CFG_FILE, "w");
if (data_file) {
serializeJsonPretty(j, data_file);
data_file.close();

ok = true;
}
#endif

cnsl->put_string_lf(format("Serialize to %s: %s", filename.c_str(), ok ? "OK" : "failed"));
cnsl->put_string_lf(format("Serialize to " SERIAL_CFG_FILE ": %s", ok ? "OK" : "failed"));
}

void deserdc11(console *const cnsl, bus *const b)
{
auto rc = deserialize_file(SERIAL_CFG_FILE);
if (rc.has_value() == false) {
cnsl->put_string_lf("Failed to deserialize " SERIAL_CFG_FILE);
return;
}

b->del_DC11();

b->add_DC11(dc11::deserialize(rc.value(), b));

cnsl->put_string_lf(format("Deserialized " SERIAL_CFG_FILE));
}

void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const stop_event)
Expand Down Expand Up @@ -1107,13 +1122,13 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto

continue;
}
else if (parts[0] == "serdc11" && parts.size() == 2) {
serdc11(cnsl, b, parts[1]);
else if (cmd == "serdc11") {
serdc11(cnsl, b);

continue;
}
else if (parts[0] == "dserdc11" && parts.size() == 2) {
// TODO
else if (cmd == "dserdc11") {
deserdc11(cnsl, b);

continue;
}
Expand Down Expand Up @@ -1173,8 +1188,8 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto
"ramsize x - set ram size (page count (8 kB), decimal)",
"bl - set bootloader (rl02 or rk05)",
"cdc11 - configure DC11 device",
"serdc11 x - store DC11 device settings",
"dserdc11 x - load DC11 device settings",
"serdc11 - store DC11 device settings",
"dserdc11 - load DC11 device settings",
#if IS_POSIX
"ser x - serialize state to a file",
// "dser - deserialize state from a file",
Expand Down
4 changes: 4 additions & 0 deletions log.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license

#include "gen.h"
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
Expand All @@ -11,6 +12,9 @@
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#if defined(ESP32)
#include <Arduino.h>
#endif
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
Expand Down
5 changes: 0 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,8 @@ int main(int argc, char *argv[])

cnsl->stop_thread();

auto dc11_devices = *b->getDC11()->get_comm_interfaces(); // TODO fix RAII

delete b;

for(auto & c: dc11_devices)
delete c;

delete cnsl;

return 0;
Expand Down
30 changes: 19 additions & 11 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#if defined(ESP32) || defined(BUILD_FOR_RP2040)
#include <Arduino.h>
#include <LittleFS.h>
#include "rp2040.h"
#include <sys/socket.h>
#elif defined(_WIN32)
Expand Down Expand Up @@ -34,8 +35,6 @@
#include "win32.h"
#endif

#include "log.h"


void setBit(uint16_t & v, const int bit, const bool vb)
{
Expand Down Expand Up @@ -250,15 +249,18 @@ void update_word(uint16_t *const w, const bool msb, const uint8_t v)
}
}

void set_nodelay(const int fd)
bool set_nodelay(const int fd)
{
int flags = 1;
#if defined(__FreeBSD__) || defined(ESP32) || defined(_WIN32)
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&flags), sizeof(flags)) == -1)
return false;
#else
if (setsockopt(fd, SOL_TCP, TCP_NODELAY, reinterpret_cast<void *>(&flags), sizeof(flags)) == -1)
return false;
#endif
DOLOG(warning, true, "Cannot disable nagle algorithm");

return true;
}

std::string get_endpoint_name(const int fd)
Expand All @@ -274,11 +276,19 @@ std::string get_endpoint_name(const int fd)

std::optional<JsonDocument> deserialize_file(const std::string & filename)
{
JsonDocument j;

#if defined(ESP32)
File data_file = LittleFS.open(filename.c_str(), "r");
if (!data_file)
return { };

deserializeJson(j, data_file);
data_file.close();
#else
FILE *fh = fopen(filename.c_str(), "r");
if (!fh) {
DOLOG(warning, false, "Failed to open %s", filename.c_str());
if (!fh)
return { };
}

std::string j_in;
char buffer[4096];
Expand All @@ -292,12 +302,10 @@ std::optional<JsonDocument> deserialize_file(const std::string & filename)

fclose(fh);

JsonDocument j;
DeserializationError error = deserializeJson(j, j_in);
if (error) {
DOLOG(warning, false, "Failed to de-serialize %s", filename.c_str());
if (error)
return { };
}
#endif

return j;
}

0 comments on commit a9d0f89

Please sign in to comment.