From 6b812eec1c128d3fdf90351c46123b7bd6855a0d Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Fri, 17 May 2024 19:37:39 +0200 Subject: [PATCH] proper c++ class init --- ESP32/main.ino | 5 +++++ comm.h | 2 ++ comm_posix_tty.cpp | 16 ++++++++++++---- comm_posix_tty.h | 9 ++++++--- comm_tcp_socket_client.cpp | 8 +++++++- comm_tcp_socket_client.h | 2 ++ comm_tcp_socket_server.cpp | 8 +++++++- comm_tcp_socket_server.h | 2 ++ debugger.cpp | 9 ++++++++- main.cpp | 5 +++++ 10 files changed, 56 insertions(+), 10 deletions(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index 2491d1e..aa4b17f 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -225,6 +225,11 @@ void start_network(console *const c) DOLOG(info, false, "Configuring DC11 device for TCP socket on port %d", port); } + for(auto & c: comm_interfaces) { + if (c->begin() == false) + DOLOG(warning, false, "Failed to configure %s", c->get_identifier().c_str()); + } + dc11 *dc11_ = new dc11(b, comm_interfaces); b->add_DC11(dc11_); diff --git a/comm.h b/comm.h index 3553b6f..d9c5739 100644 --- a/comm.h +++ b/comm.h @@ -15,6 +15,8 @@ public: comm(); virtual ~comm(); + virtual bool begin() = 0; + virtual std::string get_identifier() const = 0; virtual bool is_connected() = 0; diff --git a/comm_posix_tty.cpp b/comm_posix_tty.cpp index 33d6ceb..ca8e610 100644 --- a/comm_posix_tty.cpp +++ b/comm_posix_tty.cpp @@ -21,12 +21,18 @@ #include "log.h" -comm_posix_tty::comm_posix_tty(const std::string & device, const int bitrate) +comm_posix_tty::comm_posix_tty(const std::string & device, const int bitrate) : + device(device), + bitrate(bitrate) +{ +} + +bool comm_posix_tty::begin() { fd = open(device.c_str(), O_RDWR); if (fd == -1) { DOLOG(warning, false, "com_posix_tty failed to access %s: %s", device.c_str(), strerror(errno)); - return; // TODO error handling + return false; // TODO error handling } // from https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c @@ -35,7 +41,7 @@ comm_posix_tty::comm_posix_tty(const std::string & device, const int bitrate) DOLOG(warning, false, "com_posix_tty tcgetattr failed: %s", strerror(errno)); close(fd); fd = -1; - return; + return false; } cfsetospeed(&tty, bitrate); @@ -63,8 +69,10 @@ comm_posix_tty::comm_posix_tty(const std::string & device, const int bitrate) DOLOG(warning, false, "com_posix_tty tcsetattr failed: %s", strerror(errno)); close(fd); fd = -1; - return; + return false; } + + return true; } comm_posix_tty::~comm_posix_tty() diff --git a/comm_posix_tty.h b/comm_posix_tty.h index ffc06f1..2a01884 100644 --- a/comm_posix_tty.h +++ b/comm_posix_tty.h @@ -8,14 +8,17 @@ class comm_posix_tty: public comm { private: - std::string dev; - int fd { -1 }; + const std::string device; + const int bitrate; + int fd { -1 }; public: comm_posix_tty(const std::string & dev, const int bitrate); virtual ~comm_posix_tty(); - std::string get_identifier() const override { return dev; } + bool begin() override; + + std::string get_identifier() const override { return device; } bool is_connected() override; diff --git a/comm_tcp_socket_client.cpp b/comm_tcp_socket_client.cpp index 68ef664..de55242 100644 --- a/comm_tcp_socket_client.cpp +++ b/comm_tcp_socket_client.cpp @@ -48,7 +48,6 @@ comm_tcp_socket_client::comm_tcp_socket_client(const std::string & host, const i host(host), port(port) { - th = new std::thread(std::ref(*this)); } comm_tcp_socket_client::~comm_tcp_socket_client() @@ -61,6 +60,13 @@ comm_tcp_socket_client::~comm_tcp_socket_client() } } +bool comm_tcp_socket_client::begin() +{ + th = new std::thread(std::ref(*this)); + + return true; +} + bool comm_tcp_socket_client::is_connected() { std::unique_lock lck(cfd_lock); diff --git a/comm_tcp_socket_client.h b/comm_tcp_socket_client.h index ada088d..1309089 100644 --- a/comm_tcp_socket_client.h +++ b/comm_tcp_socket_client.h @@ -33,6 +33,8 @@ public: comm_tcp_socket_client(const std::string & host, const int port); virtual ~comm_tcp_socket_client(); + bool begin() override; + 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 4d670f2..8732e0b 100644 --- a/comm_tcp_socket_server.cpp +++ b/comm_tcp_socket_server.cpp @@ -70,7 +70,6 @@ static bool setup_telnet_session(const int fd) comm_tcp_socket_server::comm_tcp_socket_server(const int port) : port(port) { - th = new std::thread(std::ref(*this)); } comm_tcp_socket_server::~comm_tcp_socket_server() @@ -83,6 +82,13 @@ comm_tcp_socket_server::~comm_tcp_socket_server() } } +bool comm_tcp_socket_server::begin() +{ + th = new std::thread(std::ref(*this)); + + return true; +} + bool comm_tcp_socket_server::is_connected() { std::unique_lock lck(cfd_lock); diff --git a/comm_tcp_socket_server.h b/comm_tcp_socket_server.h index 2b29b6c..2f60279 100644 --- a/comm_tcp_socket_server.h +++ b/comm_tcp_socket_server.h @@ -32,6 +32,8 @@ public: comm_tcp_socket_server(const int port); virtual ~comm_tcp_socket_server(); + bool begin() override; + std::string get_identifier() const override { return format(":%d", port) + " (server)"; } bool is_connected() override; diff --git a/debugger.cpp b/debugger.cpp index 1c054e6..f4ab9cd 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -268,7 +268,8 @@ void configure_comm(console *const cnsl, std::vector & device_list) size_t device_nr = ch_dev - 'A'; - int ch_opt = wait_for_key("1. TCP client, 2. TCP server, 3. serial device, 9. to abort", cnsl, { '1', '2', '3', '9' }); + int ch_opt = wait_for_key("1. TCP client, 2. TCP server, 3. serial device, 9. to abort", cnsl, { '1', '2', '3', '9' }); + bool rc = false; if (ch_opt == '1') { std::string temp_host = cnsl->read_line("host: "); @@ -277,6 +278,7 @@ void configure_comm(console *const cnsl, std::vector & device_list) if (temp_host.empty() == false && temp_port.empty() == false) { delete device_list.at(device_nr); device_list.at(device_nr) = new comm_tcp_socket_client(temp_host, std::stoi(temp_port)); + rc = device_list.at(device_nr)->begin(); } } else if (ch_opt == '2') { @@ -284,6 +286,7 @@ void configure_comm(console *const cnsl, std::vector & device_list) if (temp.empty() == false) { delete device_list.at(device_nr); device_list.at(device_nr) = new comm_tcp_socket_server(std::stoi(temp)); + rc = device_list.at(device_nr)->begin(); } } else if (ch_opt == '3') { @@ -293,12 +296,16 @@ void configure_comm(console *const cnsl, std::vector & device_list) if (temp_dev.empty() == false && temp_bitrate.empty() == false) { delete device_list.at(device_nr); device_list.at(device_nr) = new comm_posix_tty(temp_dev, std::stoi(temp_bitrate)); + rc = device_list.at(device_nr)->begin(); } #else // TODO cnsl->put_string_lf("Not implemented yet"); #endif } + + if (ch_opt != 9 && rc == false) + cnsl->put_string_lf("Failed to initialize device"); } } diff --git a/main.cpp b/main.cpp index 5db6754..7f64326 100644 --- a/main.cpp +++ b/main.cpp @@ -620,6 +620,11 @@ int main(int argc, char *argv[]) DOLOG(info, false, "Configuring DC11 device for TCP socket on port %d", port); } + for(auto & c: comm_interfaces) { + if (c->begin() == false) + DOLOG(warning, false, "Failed to configure %s", c->get_identifier().c_str()); + } + dc11 *dc11_ = new dc11(b, comm_interfaces); b->add_DC11(dc11_); //