From c31c74164e11ac20580acdd8e9834b99a011e502 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Mon, 20 May 2024 16:34:16 +0200 Subject: [PATCH] comm & dc11 devices serialization --- ESP32/comm_arduino.cpp | 19 +++++++++++++ ESP32/comm_arduino.h | 3 ++ ESP32/comm_esp32_hardwareserial.cpp | 22 +++++++++++++++ ESP32/comm_esp32_hardwareserial.h | 3 ++ comm.cpp | 44 +++++++++++++++++++++++++++++ comm.h | 7 +++++ comm_tcp_socket_client.cpp | 20 +++++++++++++ comm_tcp_socket_client.h | 3 ++ comm_tcp_socket_server.cpp | 19 +++++++++++++ comm_tcp_socket_server.h | 3 ++ dc11.cpp | 32 +++++++++++++++++++++ dc11.h | 6 ++-- 12 files changed, 177 insertions(+), 4 deletions(-) diff --git a/ESP32/comm_arduino.cpp b/ESP32/comm_arduino.cpp index 6b7090d..978cd80 100644 --- a/ESP32/comm_arduino.cpp +++ b/ESP32/comm_arduino.cpp @@ -50,4 +50,23 @@ void comm_arduino::send_data(const uint8_t *const in, const size_t n) { s->write(in, n); } + +JsonDocument comm_arduino::serialize() const +{ + JsonDocument j; + + j["comm-backend-type"] = "arduino"; + + j["name"] = name; + + return j; +} + +comm_arduino *comm_arduino::deserialize(const JsonVariantConst j) +{ + comm_arduino *r = new comm_arduino(&Serial, j["name"].as()); + r->begin(); // TODO error-checking + + return r; +} #endif diff --git a/ESP32/comm_arduino.h b/ESP32/comm_arduino.h index 716090e..f5f365b 100644 --- a/ESP32/comm_arduino.h +++ b/ESP32/comm_arduino.h @@ -18,6 +18,9 @@ public: bool begin() override; + JsonDocument serialize() const override; + static comm_arduino *deserialize(const JsonVariantConst j); + std::string get_identifier() const; bool is_connected() override; diff --git a/ESP32/comm_esp32_hardwareserial.cpp b/ESP32/comm_esp32_hardwareserial.cpp index daac7b3..9b83362 100644 --- a/ESP32/comm_esp32_hardwareserial.cpp +++ b/ESP32/comm_esp32_hardwareserial.cpp @@ -89,4 +89,26 @@ void comm_esp32_hardwareserial::send_data(const uint8_t *const in, const size_t { uart_write_bytes(uart_nr, in, n); // error checking? } + +JsonDocument comm_esp32_hardwareserial::serialize() const +{ + JsonDocument j; + + j["comm-backend-type"] = "hardware-serial"; + + j["uart"] = uart_nr; + j["rx-pin"] = rx_pin; + j["tx-pin"] = tx_pin; + j["bitratre"] = bitrate; + + return j; +} + +comm_esp32_hardwareserial *comm_esp32_hardwareserial::deserialize(const JsonVariantConst j) +{ + comm_esp32_hardwareserial *r = new comm_esp32_hardwareserial(j["uart"].as(), j["rx-pin"].as(), j["tx-pin"].as(), j["bitrate"].as()); + r->begin(); // TODO error checking + + return r; +} #endif diff --git a/ESP32/comm_esp32_hardwareserial.h b/ESP32/comm_esp32_hardwareserial.h index d76c9a5..953ddc7 100644 --- a/ESP32/comm_esp32_hardwareserial.h +++ b/ESP32/comm_esp32_hardwareserial.h @@ -20,6 +20,9 @@ public: bool begin() override; + JsonDocument serialize() const override; + static comm_esp32_hardwareserial *deserialize(const JsonVariantConst j); + std::string get_identifier() const; bool is_connected() override; diff --git a/comm.cpp b/comm.cpp index 7c15352..93d08d9 100644 --- a/comm.cpp +++ b/comm.cpp @@ -5,6 +5,15 @@ #include #include "comm.h" +#if defined(ARDUINO) +#include "comm_arduino.h" +#endif +#if defined(ESP32) +#include "comm_esp32_hardwareserial.h" +#endif +#include "comm_tcp_socket_client.h" +#include "comm_tcp_socket_server.h" +#include "log.h" comm::comm() @@ -26,3 +35,38 @@ void comm::println(const std::string & in) send_data(reinterpret_cast(in.c_str()), in.size()); send_data(reinterpret_cast("\r\n"), 2); } + +comm *comm::deserialize(const JsonVariantConst j, bus *const b) +{ + std::string type = j["comm-backend-type"]; + + comm *d = nullptr; + + if (type == "tcp-server") + d = comm_tcp_socket_server::deserialize(j); + else if (type == "tcp-client") + d = comm_tcp_socket_client::deserialize(j); +#if defined(ESP32) + else if (type == "hardware-serial") + d = comm_esp32_hardwareserial::deserialize(j); +#endif +#if defined(ARDUINO) + else if (type == "arduino") + d = comm_arduino::deserialize(j); +#endif + else { + DOLOG(warning, false, "comm::deserialize: \"%s\" not de-serialized", type.c_str()); + return nullptr; + } + + assert(d); + + if (!d->begin()) { + delete d; + DOLOG(warning, false, "comm::deserialize: begin() failed"); + return nullptr; + } + + return d; + +} diff --git a/comm.h b/comm.h index a2db043..87245cc 100644 --- a/comm.h +++ b/comm.h @@ -8,6 +8,10 @@ #include #include +#include "ArduinoJson.h" + + +class bus; class comm { @@ -17,6 +21,9 @@ public: virtual bool begin() = 0; + virtual JsonDocument serialize() const = 0; + static comm *deserialize(const JsonVariantConst j, bus *const b); + virtual std::string get_identifier() const = 0; virtual bool is_connected() = 0; diff --git a/comm_tcp_socket_client.cpp b/comm_tcp_socket_client.cpp index de55242..c157152 100644 --- a/comm_tcp_socket_client.cpp +++ b/comm_tcp_socket_client.cpp @@ -185,3 +185,23 @@ void comm_tcp_socket_client::operator()() close(cfd); } + +JsonDocument comm_tcp_socket_client::serialize() const +{ + JsonDocument j; + + j["comm-backend-type"] = "tcp-client"; + + j["host"] = host; + j["port"] = port; + + return j; +} + +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(), j["port"].as()); + r->begin(); // TODO error-checking + + return r; +} diff --git a/comm_tcp_socket_client.h b/comm_tcp_socket_client.h index 1309089..1ee445e 100644 --- a/comm_tcp_socket_client.h +++ b/comm_tcp_socket_client.h @@ -35,6 +35,9 @@ public: bool begin() override; + JsonDocument serialize() const override; + static comm_tcp_socket_client *deserialize(const JsonVariantConst j); + std::string get_identifier() const override { return host + format(":%d", port) + " (client)"; } bool is_connected() override; diff --git a/comm_tcp_socket_server.cpp b/comm_tcp_socket_server.cpp index 8732e0b..3296cba 100644 --- a/comm_tcp_socket_server.cpp +++ b/comm_tcp_socket_server.cpp @@ -231,3 +231,22 @@ void comm_tcp_socket_server::operator()() close(cfd); close(fd); } + +JsonDocument comm_tcp_socket_server::serialize() const +{ + JsonDocument j; + + j["comm-backend-type"] = "tcp-server"; + + j["port"] = port; + + return j; +} + +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()); + r->begin(); // TODO error-checking + + return r; +} diff --git a/comm_tcp_socket_server.h b/comm_tcp_socket_server.h index 2f60279..83157ae 100644 --- a/comm_tcp_socket_server.h +++ b/comm_tcp_socket_server.h @@ -34,6 +34,9 @@ public: bool begin() override; + JsonDocument serialize() const override; + static comm_tcp_socket_server *deserialize(const JsonVariantConst j); + std::string get_identifier() const override { return format(":%d", port) + " (server)"; } bool is_connected() override; diff --git a/dc11.cpp b/dc11.cpp index 48629a8..00d1b21 100644 --- a/dc11.cpp +++ b/dc11.cpp @@ -271,3 +271,35 @@ void dc11::write_word(const uint16_t addr, const uint16_t v) registers[reg] = v; } + +JsonDocument dc11::serialize() const +{ + JsonDocument j; + + JsonDocument j_interfaces; + JsonArray j_interfaces_work = j_interfaces.to(); + for(auto & c: comm_interfaces) + j_interfaces_work.add(c->serialize()); + j["interfaces"] = j_interfaces; + + for(int regnr=0; regnr<4; regnr++) + j[format("register-%d", regnr)] = registers[regnr]; + + return j; +} + +dc11 *dc11::deserialize(const JsonVariantConst j, bus *const b) +{ + std::vector interfaces; + + JsonArrayConst j_interfaces = j["interfaces"]; + for(auto v: j_interfaces) + interfaces.push_back(comm::deserialize(v, b)); + + dc11 *r = new dc11(b, interfaces); + + for(int regnr=0; regnr<4; regnr++) + r->registers[regnr] = j[format("register-%d", regnr)]; + + return r; +} diff --git a/dc11.h b/dc11.h index 2ea4b7f..96563d9 100644 --- a/dc11.h +++ b/dc11.h @@ -46,10 +46,8 @@ public: dc11(bus *const b, const std::vector & comm_interfaces); virtual ~dc11(); -#if IS_POSIX -// json_t *serialize(); -// static tty *deserialize(const json_t *const j, bus *const b, console *const cnsl); -#endif + JsonDocument serialize() const; + static dc11 *deserialize(const JsonVariantConst j, bus *const b); std::vector *get_comm_interfaces() { return &comm_interfaces; }