diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index f202d22f..67b86462 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -117,7 +117,7 @@ void* Connection::run() break; // removed closed socket - if (datalen <= 0 || strcasecmp(data, "QUIT") == 0) + if (datalen <= 0 || strncasecmp(data, "QUIT", 4) == 0) break; // send data diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 126ce567..a03e5e3a 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -195,7 +195,6 @@ public: bool missingCommand() const { return (m_command.size() == 0 ? true : false); } private: - /** private constructor - singleton pattern */ /** * @brief private construtor. * @param command is true if an command is needed. @@ -213,6 +212,7 @@ private: /** * @brief private = operator. * @param reference to an instance. + * @return reference to instance. */ Appl& operator=(const Appl&); diff --git a/src/lib/utils/daemon.h b/src/lib/utils/daemon.h index d7f3fdbd..9b4b7788 100644 --- a/src/lib/utils/daemon.h +++ b/src/lib/utils/daemon.h @@ -51,9 +51,22 @@ public: bool status() { return m_status; } private: - /** private constructor - singleton pattern */ + /** + * @brief private construtor. + */ Daemon() {} + + /** + * @brief private copy construtor. + * @param reference to an instance. + */ Daemon(const Daemon&); + + /** + * @brief private = operator. + * @param reference to an instance. + * @return reference to instance. + */ Daemon& operator=(const Daemon&); /** status of process; true if we are a daemon */ diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index 3cde40c2..b5bbabb9 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -304,13 +304,28 @@ public: void stop(); private: - /** private constructor - singleton pattern */ + /** + * @brief private construtor. + */ Logger() {} + + /** + * @brief private copy construtor. + * @param reference to an instance. + */ Logger(const Logger&); + + /** + * @brief private = operator. + * @param reference to an instance. + * @return reference to instance. + */ Logger& operator=(const Logger&); /** typedefs for a vector of type LogSink* */ typedef vector sink_t; + + /** typedefs for a vector of type LogSink* iterator */ typedef vector::iterator sinkCI_t; /** vector of available logging sinks */ diff --git a/src/lib/utils/tcpsocket.h b/src/lib/utils/tcpsocket.h index e8ba2d55..4b4e10ba 100644 --- a/src/lib/utils/tcpsocket.h +++ b/src/lib/utils/tcpsocket.h @@ -32,8 +32,10 @@ class TCPSocket { public: - /** grant access for friend classes */ + /** grant access for friend class TCPClient */ friend class TCPClient; + + /** grant access for friend class TCPServer */ friend class TCPServer; /** diff --git a/src/tools/ebusctl.cpp b/src/tools/ebusctl.cpp index aaaf56a3..b093e773 100644 --- a/src/tools/ebusctl.cpp +++ b/src/tools/ebusctl.cpp @@ -25,6 +25,7 @@ #include "tcpsocket.h" #include #include +#include using namespace std; @@ -46,6 +47,75 @@ void define_args() } +enum CommandType { + ct_open, + ct_exit, + ct_help, + ct_invalid +}; + +CommandType getCase(const string& item) +{ + if (strcasecmp(item.c_str(), "OPEN") == 0) return ct_open; + if (strcasecmp(item.c_str(), "EXIT") == 0) return ct_exit; + if (strcasecmp(item.c_str(), "HELP") == 0) return ct_help; + + return ct_invalid; +} + +bool connect(const char* host, int port, bool once=true) +{ + + TCPClient* client = new TCPClient(); + TCPSocket* socket = client->connect(host, port); + + if (socket != NULL) { + + do { + string message; + + if (once == false) { + cout << "online: "; + getline(cin, message); + } + else { + message = A.getCommand(); + for (int i = 0; i < A.numArgs(); i++) { + message += " "; + message += A.getArg(i); + } + } + + socket->send(message.c_str(), message.size()); + + if (strncasecmp(message.c_str(), "QUIT", 4) != 0 && strncasecmp(message.c_str(), "STOP", 4) != 0) { + + char data[1024]; + size_t datalen; + + datalen = socket->recv(data, sizeof(data)-1); + data[datalen] = '\0'; + + cout << data; + } + break; + + } while (once == false); + + delete socket; + + } + else + cout << "error connecting to " << host << ":" << port << endl; + + delete client; + + if (once == false) + return false; + + return true; +} + int main(int argc, char* argv[]) { // define arguments and application variables @@ -56,37 +126,68 @@ int main(int argc, char* argv[]) if (A.missingCommand() == true) { cout << "interactive mode started." << endl; - exit(EXIT_FAILURE); + + bool running = true; + + do { + string input, token; + vector cmd; + + cout << "$: "; + getline(cin, input); + + // prepare input + istringstream stream(input); + while (getline(stream, token, ' ') != 0) + cmd.push_back(token); + + if (cmd.size() == 0) + cout << "command missing" << endl; + + switch (getCase(cmd[0])) { + case ct_invalid: + cout << "command not found" << endl; + break; + + case ct_open: + { + bool ret = true; + cout << "connect to..." << endl; + if (cmd.size() == 1) + ret = connect(A.getOptVal("server"), A.getOptVal("port"), false); + else if (cmd.size() == 2) + ret = connect(cmd[1].c_str(), A.getOptVal("port"), false); + else if (cmd.size() == 3) + ret = connect(cmd[1].c_str(), atoi(cmd[2].c_str()), false); + else + cout << "open [host [port]]" << endl; + + running = ret; + } + + break; + + case ct_exit: + running = false; + break; + + case ct_help: + cout << "commands:" << endl + << " open - open connection to ebusd 'open [host [port]]'" << endl + << " exit - exit ebusctl" << endl + << " help - print this page" << endl; + break; + + default: + break; + } + + } while (running == true); + + exit(EXIT_SUCCESS); } - TCPClient* client = new TCPClient(); - TCPSocket* socket = client->connect(A.getOptVal("server"), A.getOptVal("port")); - - if (socket != NULL) { - // build message - string message(A.getCommand()); - for (int i = 0; i < A.numArgs(); i++) { - message += " "; - message += A.getArg(i); - } - - socket->send(message.c_str(), message.size()); - - char data[1024]; - size_t datalen; - - datalen = socket->recv(data, sizeof(data)-1); - data[datalen] = '\0'; - - cout << data; - - delete socket; - } - else - cout << "error connecting to " << A.getOptVal("server") - << ":" << A.getOptVal("port") << endl; - - delete client; + connect(A.getOptVal("server"), A.getOptVal("port")); exit(EXIT_SUCCESS); } diff --git a/src/tools/ebusfeed.cpp b/src/tools/ebusfeed.cpp index 11cd3a4a..776ae141 100644 --- a/src/tools/ebusfeed.cpp +++ b/src/tools/ebusfeed.cpp @@ -36,14 +36,14 @@ void define_args() { A.setVersion("ebusfeed is part of """PACKAGE_STRING""); - A.addText(" 'ebusfeed' sends a raw dump file to a local serial device (pts)\n\n" + A.addText(" 'ebusfeed' sends a dump file to a local serial device (pts)\n\n" " Usage: 1. 'socat -d -d pty,raw,echo=0 pty,raw,echo=0'\n" " 2. create symbol links to appropriate devices\n" " for example: 'ln -s /dev/pts/2 /dev/ttyUSB60'\n" " 'ln -s /dev/pts/3 /dev/ttyUSB20'\n" " 3. start ebusd: 'ebusd -f -d /dev/ttyUSB20'\n" - " 4. start ebusfeed: 'ebusfeed /path/to/raw_file'\n\n" - "Command: '/path/to/raw_file'\n\n" + " 4. start ebusfeed: 'ebusfeed /path/to/ebus_dump.bin'\n\n" + "Command: '/path/to/ebus_dump.bin'\n\n" "Options:\n"); A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, @@ -62,6 +62,11 @@ int main(int argc, char* argv[]) // parse arguments A.parseArgs(argc, argv); + if (A.missingCommand() == true) { + cout << "ebus dump file is required." << endl; + exit(EXIT_FAILURE); + } + string dev(A.getOptVal("device")); Port port(dev, true);