proper c++ class init

This commit is contained in:
folkert van heusden 2024-05-17 19:37:39 +02:00
parent 6913212ce3
commit 6b812eec1c
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
10 changed files with 56 additions and 10 deletions

View file

@ -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_);

2
comm.h
View file

@ -15,6 +15,8 @@ public:
comm();
virtual ~comm();
virtual bool begin() = 0;
virtual std::string get_identifier() const = 0;
virtual bool is_connected() = 0;

View file

@ -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()

View file

@ -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;

View file

@ -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<std::mutex> lck(cfd_lock);

View file

@ -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;

View file

@ -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<std::mutex> lck(cfd_lock);

View file

@ -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;

View file

@ -268,7 +268,8 @@ void configure_comm(console *const cnsl, std::vector<comm *> & 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<comm *> & 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<comm *> & 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<comm *> & 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");
}
}

View file

@ -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_);
//