proper c++ class init
This commit is contained in:
parent
6913212ce3
commit
6b812eec1c
10 changed files with 56 additions and 10 deletions
|
@ -225,6 +225,11 @@ void start_network(console *const c)
|
||||||
DOLOG(info, false, "Configuring DC11 device for TCP socket on port %d", port);
|
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);
|
dc11 *dc11_ = new dc11(b, comm_interfaces);
|
||||||
b->add_DC11(dc11_);
|
b->add_DC11(dc11_);
|
||||||
|
|
||||||
|
|
2
comm.h
2
comm.h
|
@ -15,6 +15,8 @@ public:
|
||||||
comm();
|
comm();
|
||||||
virtual ~comm();
|
virtual ~comm();
|
||||||
|
|
||||||
|
virtual bool begin() = 0;
|
||||||
|
|
||||||
virtual std::string get_identifier() const = 0;
|
virtual std::string get_identifier() const = 0;
|
||||||
|
|
||||||
virtual bool is_connected() = 0;
|
virtual bool is_connected() = 0;
|
||||||
|
|
|
@ -21,12 +21,18 @@
|
||||||
#include "log.h"
|
#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);
|
fd = open(device.c_str(), O_RDWR);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
DOLOG(warning, false, "com_posix_tty failed to access %s: %s", device.c_str(), strerror(errno));
|
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
|
// 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));
|
DOLOG(warning, false, "com_posix_tty tcgetattr failed: %s", strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
fd = -1;
|
fd = -1;
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
cfsetospeed(&tty, bitrate);
|
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));
|
DOLOG(warning, false, "com_posix_tty tcsetattr failed: %s", strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
fd = -1;
|
fd = -1;
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
comm_posix_tty::~comm_posix_tty()
|
comm_posix_tty::~comm_posix_tty()
|
||||||
|
|
|
@ -8,14 +8,17 @@
|
||||||
class comm_posix_tty: public comm
|
class comm_posix_tty: public comm
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::string dev;
|
const std::string device;
|
||||||
|
const int bitrate;
|
||||||
int fd { -1 };
|
int fd { -1 };
|
||||||
|
|
||||||
public:
|
public:
|
||||||
comm_posix_tty(const std::string & dev, const int bitrate);
|
comm_posix_tty(const std::string & dev, const int bitrate);
|
||||||
virtual ~comm_posix_tty();
|
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;
|
bool is_connected() override;
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,6 @@ comm_tcp_socket_client::comm_tcp_socket_client(const std::string & host, const i
|
||||||
host(host),
|
host(host),
|
||||||
port(port)
|
port(port)
|
||||||
{
|
{
|
||||||
th = new std::thread(std::ref(*this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
comm_tcp_socket_client::~comm_tcp_socket_client()
|
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()
|
bool comm_tcp_socket_client::is_connected()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lck(cfd_lock);
|
std::unique_lock<std::mutex> lck(cfd_lock);
|
||||||
|
|
|
@ -33,6 +33,8 @@ public:
|
||||||
comm_tcp_socket_client(const std::string & host, const int port);
|
comm_tcp_socket_client(const std::string & host, const int port);
|
||||||
virtual ~comm_tcp_socket_client();
|
virtual ~comm_tcp_socket_client();
|
||||||
|
|
||||||
|
bool begin() override;
|
||||||
|
|
||||||
std::string get_identifier() const override { return host + format(":%d", port) + " (client)"; }
|
std::string get_identifier() const override { return host + format(":%d", port) + " (client)"; }
|
||||||
|
|
||||||
bool is_connected() override;
|
bool is_connected() override;
|
||||||
|
|
|
@ -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)
|
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()
|
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()
|
bool comm_tcp_socket_server::is_connected()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lck(cfd_lock);
|
std::unique_lock<std::mutex> lck(cfd_lock);
|
||||||
|
|
|
@ -32,6 +32,8 @@ public:
|
||||||
comm_tcp_socket_server(const int port);
|
comm_tcp_socket_server(const int port);
|
||||||
virtual ~comm_tcp_socket_server();
|
virtual ~comm_tcp_socket_server();
|
||||||
|
|
||||||
|
bool begin() override;
|
||||||
|
|
||||||
std::string get_identifier() const override { return format(":%d", port) + " (server)"; }
|
std::string get_identifier() const override { return format(":%d", port) + " (server)"; }
|
||||||
|
|
||||||
bool is_connected() override;
|
bool is_connected() override;
|
||||||
|
|
|
@ -269,6 +269,7 @@ void configure_comm(console *const cnsl, std::vector<comm *> & device_list)
|
||||||
size_t device_nr = ch_dev - 'A';
|
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') {
|
if (ch_opt == '1') {
|
||||||
std::string temp_host = cnsl->read_line("host: ");
|
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) {
|
if (temp_host.empty() == false && temp_port.empty() == false) {
|
||||||
delete device_list.at(device_nr);
|
delete device_list.at(device_nr);
|
||||||
device_list.at(device_nr) = new comm_tcp_socket_client(temp_host, std::stoi(temp_port));
|
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') {
|
else if (ch_opt == '2') {
|
||||||
|
@ -284,6 +286,7 @@ void configure_comm(console *const cnsl, std::vector<comm *> & device_list)
|
||||||
if (temp.empty() == false) {
|
if (temp.empty() == false) {
|
||||||
delete device_list.at(device_nr);
|
delete device_list.at(device_nr);
|
||||||
device_list.at(device_nr) = new comm_tcp_socket_server(std::stoi(temp));
|
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') {
|
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) {
|
if (temp_dev.empty() == false && temp_bitrate.empty() == false) {
|
||||||
delete device_list.at(device_nr);
|
delete device_list.at(device_nr);
|
||||||
device_list.at(device_nr) = new comm_posix_tty(temp_dev, std::stoi(temp_bitrate));
|
device_list.at(device_nr) = new comm_posix_tty(temp_dev, std::stoi(temp_bitrate));
|
||||||
|
rc = device_list.at(device_nr)->begin();
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
// TODO
|
// TODO
|
||||||
cnsl->put_string_lf("Not implemented yet");
|
cnsl->put_string_lf("Not implemented yet");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ch_opt != 9 && rc == false)
|
||||||
|
cnsl->put_string_lf("Failed to initialize device");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
main.cpp
5
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);
|
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);
|
dc11 *dc11_ = new dc11(b, comm_interfaces);
|
||||||
b->add_DC11(dc11_);
|
b->add_DC11(dc11_);
|
||||||
//
|
//
|
||||||
|
|
Loading…
Add table
Reference in a new issue