From 2f9138b43e81a845e882fe35f7eecfc4361a56e4 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 9 Nov 2014 20:27:40 +0100 Subject: [PATCH 01/38] ebusd: main.cpp renamed into ebusd.cpp; daemon option 'aquiretime' added. --- configure.ac | 2 +- src/ebusd/Makefile.am | 2 +- src/ebusd/{main.cpp => ebusd.cpp} | 6 +++++- src/ebusd/ebusloop.cpp | 13 +++++++++---- src/ebusd/ebusloop.h | 1 + 5 files changed, 17 insertions(+), 7 deletions(-) rename src/ebusd/{main.cpp => ebusd.cpp} (96%) diff --git a/configure.ac b/configure.ac index 37d4ab21..c9a30b66 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ AC_INIT([ebusd], [0.5.0], [ebusd@liwest.at], [ebusd], [https://github.com/yuhu-/ AC_PROG_CXX AC_CONFIG_AUX_DIR([build]) -AC_CONFIG_SRCDIR([src/ebusd/main.cpp]) +AC_CONFIG_SRCDIR([src/ebusd/ebusd.cpp]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile src/lib/ebus/Makefile diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index 742edf10..bf57211a 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -15,7 +15,7 @@ ebusd_SOURCES = message.h \ ebusloop.h \ baseloop.cpp \ baseloop.h \ - main.cpp + ebusd.cpp ebusd_LDADD = $(top_srcdir)/src/lib/utils/libutils.a \ $(top_srcdir)/src/lib/ebus/libebus.a \ diff --git a/src/ebusd/main.cpp b/src/ebusd/ebusd.cpp similarity index 96% rename from src/ebusd/main.cpp rename to src/ebusd/ebusd.cpp index 459c5579..6aa90d8c 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/ebusd.cpp @@ -60,7 +60,11 @@ void define_args() Appl::type_int, Appl::opt_mandatory); A.addItem("p_recvtimeout", Appl::Param(15000), "", "recvtimeout", - "receive timeout in 'us' (15000)\n", + "receive timeout in 'us' (15000)", + Appl::type_long, Appl::opt_mandatory); + + A.addItem("p_acquiretime", Appl::Param(4200), "", "acquiretime", + "waiting time for bus acquire in 'us' (4200)\n", Appl::type_long, Appl::opt_mandatory); A.addItem("p_pollinterval", Appl::Param(5), "", "pollinterval", diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/ebusloop.cpp index 6a2f04ab..793a0f4a 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/ebusloop.cpp @@ -45,6 +45,8 @@ EBusLoop::EBusLoop(Commands* commands) m_sendRetries = A.getParam("p_sendretries"); m_lockRetries = A.getParam("p_lockretries"); + + m_acquireTime = A.getParam("p_acquiretime"); } EBusLoop::~EBusLoop() @@ -111,18 +113,18 @@ void* EBusLoop::run() busCommand->setResult(std::string(), RESULT_OK); } else { + sendRetries = 0; L.log(bus, event, " send retry failed", sendRetries); if (busCommand->isPoll() == true) delete m_sendBuffer.remove(); else busCommand->sendSignal(); - - sendRetries = 0; } } else { sendRetries = 0; + if (busCommand->isPoll() == true) { m_commands->storePolData(busCommand->getMessageStr().c_str()); // TODO use getResult() delete busCommand; @@ -138,14 +140,14 @@ void* EBusLoop::run() L.log(bus, trace, " acquire bus failed"); if (lockRetries >= m_lockRetries) { + lockRetries = 0; L.log(bus, event, " lock bus failed"); + BusCommand* busCommand = m_sendBuffer.remove(); if (busCommand->isPoll() == true) delete busCommand; else busCommand->sendSignal(); - - lockRetries = 0; } else { lockRetries++; @@ -294,6 +296,9 @@ int EBusLoop::acquireBus() return RESULT_ERR_SEND; } + // wait ~4200 usec for receive + usleep(m_acquireTime); + // receive 1 byte - must be QQ numRecv = m_port->recv(0); diff --git a/src/ebusd/ebusloop.h b/src/ebusd/ebusloop.h index 1605451e..95c9731d 100644 --- a/src/ebusd/ebusloop.h +++ b/src/ebusd/ebusloop.h @@ -71,6 +71,7 @@ private: long m_recvTimeout; int m_sendRetries; int m_lockRetries; + long m_acquireTime; unsigned char fetchByte(); void collectCycData(const int numRecv); From af4f822f5de986dd7dcc8b03483d8a2abfc736ce Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 9 Nov 2014 23:21:55 +0100 Subject: [PATCH 02/38] daemon option '--version' added. --- src/ebusd/ebusd.cpp | 18 +++++++++++++++--- src/lib/utils/appl.h | 6 +++--- src/lib/utils/daemon.h | 6 +++--- src/lib/utils/logger.h | 6 +++--- src/lib/utils/notify.h | 6 +++--- src/lib/utils/tcpsocket.h | 6 +++--- src/lib/utils/thread.h | 6 +++--- src/lib/utils/wqueue.h | 6 +++--- 8 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 6aa90d8c..1e5def66 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -17,11 +17,13 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ +#include "config.h" #include "logger.h" #include "daemon.h" #include "appl.h" #include "baseloop.h" #include +#include using namespace libebus; @@ -119,6 +121,10 @@ void define_args() "\tprint daemon settings\n", Appl::type_bool, Appl::opt_none); + A.addItem("p_version", Appl::Param(false), "v", "version", + "\tprint ebusd version\n", + Appl::type_bool, Appl::opt_none); + A.addItem("p_help", Appl::Param(false), "h", "help", "\tprint this message", Appl::type_bool, Appl::opt_none); @@ -177,17 +183,23 @@ int main(int argc, char* argv[]) exit(EXIT_FAILURE); } - // print Help + // print version + if (A.getParam("p_version") == true) { + std::cerr << PACKAGE_STRING << std::endl; + exit(EXIT_SUCCESS); + } + + // print help if (A.getParam("p_help") == true) { A.printArgs(); exit(EXIT_SUCCESS); } - // print Daemon settings + // print daemon settings if (A.getParam("p_settings") == true) A.printSettings(); - // make me Daemon + // make me daemon if (A.getParam("p_foreground") == true) { L += new LogConsole(calcAreas(A.getParam("p_logareas")), calcLevel(A.getParam("p_loglevel")), diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index d9684644..392d947d 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_APPL_H_ -#define LIBCORE_APPL_H_ +#ifndef LIBUTILS_APPL_H_ +#define LIBUTILS_APPL_H_ #include #include @@ -109,4 +109,4 @@ private: void addParam(const char* name, const std::string arg, Datatype datatype); }; -#endif // LIBCORE_APPL_H_ +#endif // LIBUTILS_APPL_H_ diff --git a/src/lib/utils/daemon.h b/src/lib/utils/daemon.h index 37ac41b3..ac01d42b 100644 --- a/src/lib/utils/daemon.h +++ b/src/lib/utils/daemon.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_DAEMON_H_ -#define LIBCORE_DAEMON_H_ +#ifndef LIBUTILS_DAEMON_H_ +#define LIBUTILS_DAEMON_H_ class Daemon { @@ -45,4 +45,4 @@ private: }; -#endif // LIBCORE_DAEMON_H_ +#endif // LIBUTILS_DAEMON_H_ diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index d704e2c1..e91261b8 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_LOGGER_H_ -#define LIBCORE_LOGGER_H_ +#ifndef LIBUTILS_LOGGER_H_ +#define LIBUTILS_LOGGER_H_ #include "wqueue.h" #include "thread.h" @@ -178,4 +178,4 @@ private: }; -#endif // LIBCORE_LOGGER_H_ +#endif // LIBUTILS_LOGGER_H_ diff --git a/src/lib/utils/notify.h b/src/lib/utils/notify.h index 91909662..accc6339 100644 --- a/src/lib/utils/notify.h +++ b/src/lib/utils/notify.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_NOTIFY_H_ -#define LIBCORE_NOTIFY_H_ +#ifndef LIBUTILS_NOTIFY_H_ +#define LIBUTILS_NOTIFY_H_ #include #include @@ -51,6 +51,6 @@ private: }; -#endif // LIBCORE_NOTIFY_H_ +#endif // LIBUTILS_NOTIFY_H_ diff --git a/src/lib/utils/tcpsocket.h b/src/lib/utils/tcpsocket.h index 335cfb64..fa507204 100644 --- a/src/lib/utils/tcpsocket.h +++ b/src/lib/utils/tcpsocket.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_TCPSOCKET_H_ -#define LIBCORE_TCPSOCKET_H_ +#ifndef LIBUTILS_TCPSOCKET_H_ +#define LIBUTILS_TCPSOCKET_H_ #include #include @@ -84,5 +84,5 @@ private: }; -#endif // LIBCORE_TCPSOCKET_H_ +#endif // LIBUTILS_TCPSOCKET_H_ diff --git a/src/lib/utils/thread.h b/src/lib/utils/thread.h index 913735a3..03ecfd0d 100644 --- a/src/lib/utils/thread.h +++ b/src/lib/utils/thread.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_THREAD_H_ -#define LIBCORE_THREAD_H_ +#ifndef LIBUTILS_THREAD_H_ +#define LIBUTILS_THREAD_H_ #include @@ -43,4 +43,4 @@ private: }; -#endif // LIBCORE_THREAD_H_ +#endif // LIBUTILS_THREAD_H_ diff --git a/src/lib/utils/wqueue.h b/src/lib/utils/wqueue.h index 0509b87e..99c4fc48 100644 --- a/src/lib/utils/wqueue.h +++ b/src/lib/utils/wqueue.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBCORE_WQUEUE_H_ -#define LIBCORE_WQUEUE_H_ +#ifndef LIBUTILS_WQUEUE_H_ +#define LIBUTILS_WQUEUE_H_ #include #include @@ -96,4 +96,4 @@ private: }; -#endif // LIBCORE_WQUEUE_H_ +#endif // LIBUTILS_WQUEUE_H_ From db91a1d47dc6da6737aedd6fe89f689c47d1f040 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Mon, 10 Nov 2014 19:14:42 +0100 Subject: [PATCH 03/38] class Appl: integrated 'version and help' options. --- src/ebusctl/ebusctl.cpp | 22 ++++++---------------- src/ebusd/ebusd.cpp | 41 ++++++++++------------------------------- src/lib/utils/appl.cpp | 40 ++++++++++++++++++++++++++++------------ src/lib/utils/appl.h | 9 +++++++-- 4 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index 5749f7b1..2bcf9357 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -17,9 +17,10 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ +#include "config.h" +#include "appl.h" #include "port.h" #include "decode.h" -#include "appl.h" #include "tcpsocket.h" #include #include @@ -63,9 +64,7 @@ void define_args() "port (8888)\n", Appl::type_int, Appl::opt_mandatory); - A.addItem("p_help", Appl::Param(false), "h", "help", - "print this message", - Appl::type_bool, Appl::opt_none); + A.addVersion("ebusctl is part of """PACKAGE_STRING""); } @@ -166,20 +165,11 @@ void scanVaillant(TCPSocket* socket, const std::string address) int main(int argc, char* argv[]) { - // define Arguments and Application variables + // define arguments and application variables define_args(); - // parse Arguments - if (A.parseArgs(argc, argv) == false) { - A.printArgs(); - exit(EXIT_FAILURE); - } - - // print Help - if (A.getParam("p_help") == true) { - A.printArgs(); - exit(EXIT_SUCCESS); - } + // parse arguments + A.parseArgs(argc, argv); if (strcasecmp(A.getArg(0).c_str(), "feed") == 0) { std::string dev(A.getParam("p_device")); diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 1e5def66..c5eacde9 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -121,13 +121,7 @@ void define_args() "\tprint daemon settings\n", Appl::type_bool, Appl::opt_none); - A.addItem("p_version", Appl::Param(false), "v", "version", - "\tprint ebusd version\n", - Appl::type_bool, Appl::opt_none); - - A.addItem("p_help", Appl::Param(false), "h", "help", - "\tprint this message", - Appl::type_bool, Appl::opt_none); + A.addVersion(""PACKAGE_STRING""); } void shutdown() @@ -140,11 +134,11 @@ void shutdown() signal(SIGINT, SIG_DFL); signal(SIGTERM, SIG_DFL); - // delete Daemon pid file + // delete daemon pid file if (D.status() == true) D.stop(); - // stop Logger + // stop logger L.log(bas, event, "ebusd stopped"); L.stop(); L.join(); @@ -174,26 +168,11 @@ void signal_handler(int sig) int main(int argc, char* argv[]) { - // define Arguments and Application variables + // define arguments and application variables define_args(); - // parse Arguments - if (A.parseArgs(argc, argv) == false) { - A.printArgs(); - exit(EXIT_FAILURE); - } - - // print version - if (A.getParam("p_version") == true) { - std::cerr << PACKAGE_STRING << std::endl; - exit(EXIT_SUCCESS); - } - - // print help - if (A.getParam("p_help") == true) { - A.printArgs(); - exit(EXIT_SUCCESS); - } + // parse arguments + A.parseArgs(argc, argv); // print daemon settings if (A.getParam("p_settings") == true) @@ -211,18 +190,18 @@ int main(int argc, char* argv[]) "logfile", A.getParam("p_logfile")); } - // trap Signals that we expect to receive + // trap signals that we expect to receive signal(SIGHUP, signal_handler); signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); - // start Logger + // start logger L.start("logger"); - // wait for Logger be ready + // wait for logger be ready usleep(100000); L.log(bas, event, "ebusd started"); - // create BaseLoop + // create baseloop baseloop = new BaseLoop(); baseloop->start(); diff --git a/src/lib/utils/appl.cpp b/src/lib/utils/appl.cpp index 263f9554..83e22c69 100644 --- a/src/lib/utils/appl.cpp +++ b/src/lib/utils/appl.cpp @@ -59,7 +59,18 @@ void Appl::addItem(const char* name, Param param, const char* shortname, } } -void Appl::printArgs() +void Appl::addVersion(const char* version) +{ + m_version = version; +} + +void Appl::printVersion() +{ + std::cerr << m_version << std::endl; + exit(EXIT_SUCCESS); +} + +void Appl::printHelp() { std::cerr << std::endl << "Usage:" << std::endl << " " << m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ; @@ -77,10 +88,11 @@ void Appl::printArgs() << std::endl; } - std::cerr << std::endl; + std::cerr << "-v | --version\n-h | --help" << std::endl << std::endl; + exit(EXIT_SUCCESS); } -bool Appl::parseArgs(int argc, char* argv[]) +void Appl::parseArgs(int argc, char* argv[]) { std::vector _argv(argv, argv + argc); m_argc = argc; @@ -93,10 +105,10 @@ bool Appl::parseArgs(int argc, char* argv[]) // is next item an added argument? if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos) { if (checkArg(m_argv[i].substr(2), m_argv[i+1]) == false) - return false; + printHelp(); } else { if (checkArg(m_argv[i].substr(2), "") == false) - return false; + printHelp(); } // find option with short format '-' @@ -109,10 +121,10 @@ bool Appl::parseArgs(int argc, char* argv[]) if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos && j+1 == m_argv[i].size()) { if (checkArg(m_argv[i].substr(j,1), m_argv[i+1]) == false) - return false; + printHelp(); } else { if (checkArg(m_argv[i].substr(j,1), "") == false) - return false; + printHelp(); } } } @@ -122,7 +134,7 @@ bool Appl::parseArgs(int argc, char* argv[]) // check args if (m_argNum > 0) { if (m_argc < (m_argNum + 1)) - return false; + printHelp(); for (size_t i = 1; i < m_argc; i++) { @@ -134,10 +146,8 @@ bool Appl::parseArgs(int argc, char* argv[]) } if (m_argValues.size() < m_argNum) - return false; + printHelp(); } - - return true; } void Appl::printSettings() @@ -175,6 +185,12 @@ void Appl::printSettings() bool Appl::checkArg(const std::string& name, const std::string& arg) { + if (strcmp(name.c_str(), "v") == 0 || strcmp(name.c_str(), "version") == 0) + printVersion(); + + if (strcmp(name.c_str(), "h") == 0 || strcmp(name.c_str(), "help") == 0) + printHelp(); + for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) { if (a_it->shortname == name || a_it->longname == name) { if (a_it->optiontype == opt_mandatory && arg.size() == 0) { @@ -191,7 +207,7 @@ bool Appl::checkArg(const std::string& name, const std::string& arg) } } - std::cerr << m_argv[0].substr(2) << ": Unknown Option -- " << name << std::endl; + std::cerr << std::endl << "unknown option '" << name << "'" << std::endl; return false; } diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 392d947d..15a9ca77 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -69,9 +69,9 @@ public: const char* longname, const char* description, Datatype datatype, Optiontype optiontype); - void printArgs(); + void addVersion(const char* version); - bool parseArgs(int argc, char* argv[]); + void parseArgs(int argc, char* argv[]); void printSettings(); private: @@ -100,6 +100,8 @@ private: std::string m_argTxt; size_t m_argNum; + const char* m_version; + std::vector m_argValues; bool checkArg(const std::string& name, const std::string& arg); @@ -107,6 +109,9 @@ private: void addParam(const char* name, Param param) { m_params[name] = param; } void addParam(const char* name, const std::string arg, Datatype datatype); + + void printVersion(); + void printHelp(); }; #endif // LIBUTILS_APPL_H_ From 694f22c718641ef3cf8948a347600f2a2486e308 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 15:09:52 +0100 Subject: [PATCH 04/38] class EBusLoop: newCommands renamed to reload. --- src/ebusd/baseloop.cpp | 2 +- src/ebusd/ebusloop.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index c23607fd..af48fc1e 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -353,7 +353,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) delete m_commands; m_commands = commands; - m_ebusloop->newCommands(m_commands); + m_ebusloop->reload(m_commands); result << "done"; break; diff --git a/src/ebusd/ebusloop.h b/src/ebusd/ebusloop.h index 95c9731d..3d39f099 100644 --- a/src/ebusd/ebusloop.h +++ b/src/ebusd/ebusloop.h @@ -48,7 +48,7 @@ public: void dump() { m_dumpState == true ? m_dumpState = false : m_dumpState = true ; } void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; } - void newCommands(Commands* commands) { m_commands = commands; } + void reload(Commands* commands) { m_commands = commands; } private: Commands* m_commands; From e8d4fbaff564afa2dd41eb0b32106aeb1a29ab26 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 20:07:59 +0100 Subject: [PATCH 05/38] class Network: needless function 'addQueue' removed. --- src/ebusd/baseloop.cpp | 5 ++--- src/ebusd/baseloop.h | 4 ++-- src/ebusd/network.cpp | 5 +++-- src/ebusd/network.h | 6 ++---- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index af48fc1e..4e47de62 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -39,8 +39,7 @@ BaseLoop::BaseLoop() m_ebusloop->start("ebusloop"); // create Network - m_network = new Network(A.getParam("p_localhost")); - m_network->addQueue(&m_queue); + m_network = new Network(A.getParam("p_localhost"), &m_msgQueue); m_network->start("network"); } @@ -68,7 +67,7 @@ void BaseLoop::start() std::string result; // recv new message from client - Message* message = m_queue.remove(); + Message* message = m_msgQueue.remove(); std::string data = message->getData(); data.erase(std::remove(data.begin(), data.end(), '\r'), data.end()); diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index 9e68555c..1e59401d 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -36,14 +36,14 @@ public: void start(); - void addMessage(Message* message) { m_queue.add(message); } + void addMessage(Message* message) { m_msgQueue.add(message); } private: Commands* m_commands; EBusLoop* m_ebusloop; Network* m_network; - WQueue m_queue; + WQueue m_msgQueue; enum ClientCommand { get, // get ebus data diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index ee0a649b..79f44168 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -25,7 +25,8 @@ extern LogInstance& L; extern Appl& A; -Network::Network(const bool localhost) : m_listening(false), m_running(false) +Network::Network(const bool localhost, WQueue* msgQueue) + : m_msgQueue(msgQueue), m_listening(false), m_running(false) { if (localhost == true) m_Server = new TCPServer(A.getParam("p_port"), "127.0.0.1"); @@ -100,7 +101,7 @@ void* Network::run() if (socket == NULL) continue; - Connection* connection = new Connection(socket, m_queue); + Connection* connection = new Connection(socket, m_msgQueue); if (connection == NULL) continue; diff --git a/src/ebusd/network.h b/src/ebusd/network.h index fc04ffc7..6a633a65 100644 --- a/src/ebusd/network.h +++ b/src/ebusd/network.h @@ -26,17 +26,15 @@ class Network : public Thread { public: - Network(const bool localhost); + Network(const bool localhost, WQueue* msgQueue); ~Network(); - void addQueue(WQueue* queue) { m_queue = queue; } - void* run(); void stop() const { m_notify.notify(); usleep(100000); } private: std::list m_connections; - WQueue* m_queue; + WQueue* m_msgQueue; TCPServer* m_Server; Notify m_notify; bool m_listening; From 2c7edad06791a33ddb98863d185512a0b53d4334 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 20:12:33 +0100 Subject: [PATCH 06/38] cosmetic --- src/ebusd/baseloop.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 4e47de62..54051bd6 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -27,36 +27,36 @@ extern Appl& A; BaseLoop::BaseLoop() { - // create Commands DB + // create commands DB m_commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); L.log(bas, trace, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); L.log(bas, event, " polling DB: %d ", m_commands->sizePolDB()); - // create EBusLoop + // create ebusloop m_ebusloop = new EBusLoop(m_commands); m_ebusloop->start("ebusloop"); - // create Network + // create network m_network = new Network(A.getParam("p_localhost"), &m_msgQueue); m_network->start("network"); } BaseLoop::~BaseLoop() { - // free Network + // free network if (m_network != NULL) delete m_network; - // free EBusLoop + // free ebusloop if (m_ebusloop != NULL) { m_ebusloop->stop(); m_ebusloop->join(); delete m_ebusloop; } - // free Commands DB + // free commands DB if (m_commands != NULL) delete m_commands; } @@ -343,7 +343,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) } { - // create Commands DB + // create commands DB Commands* commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); L.log(bas, trace, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); From 162681bb9f6302739f5aca7427436a295037da6e Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 20:12:33 +0100 Subject: [PATCH 07/38] cosmetic --- src/ebusd/baseloop.cpp | 14 +++++++------- src/ebusd/network.cpp | 18 +++++++++--------- src/ebusd/network.h | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 4e47de62..54051bd6 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -27,36 +27,36 @@ extern Appl& A; BaseLoop::BaseLoop() { - // create Commands DB + // create commands DB m_commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); L.log(bas, trace, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); L.log(bas, event, " polling DB: %d ", m_commands->sizePolDB()); - // create EBusLoop + // create ebusloop m_ebusloop = new EBusLoop(m_commands); m_ebusloop->start("ebusloop"); - // create Network + // create network m_network = new Network(A.getParam("p_localhost"), &m_msgQueue); m_network->start("network"); } BaseLoop::~BaseLoop() { - // free Network + // free network if (m_network != NULL) delete m_network; - // free EBusLoop + // free ebusloop if (m_ebusloop != NULL) { m_ebusloop->stop(); m_ebusloop->join(); delete m_ebusloop; } - // free Commands DB + // free commands DB if (m_commands != NULL) delete m_commands; } @@ -343,7 +343,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) } { - // create Commands DB + // create commands DB Commands* commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); L.log(bas, trace, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index 79f44168..f054d14e 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -29,11 +29,11 @@ Network::Network(const bool localhost, WQueue* msgQueue) : m_msgQueue(msgQueue), m_listening(false), m_running(false) { if (localhost == true) - m_Server = new TCPServer(A.getParam("p_port"), "127.0.0.1"); + m_tcpServer = new TCPServer(A.getParam("p_port"), "127.0.0.1"); else - m_Server = new TCPServer(A.getParam("p_port"), "0.0.0.0"); + m_tcpServer = new TCPServer(A.getParam("p_port"), "0.0.0.0"); - if (m_Server != NULL && m_Server->start() == 0) + if (m_tcpServer != NULL && m_tcpServer->start() == 0) m_listening = true; } @@ -51,7 +51,7 @@ Network::~Network() if (m_running == true) stop(); - delete m_Server; + delete m_tcpServer; } void* Network::run() @@ -67,10 +67,10 @@ void* Network::run() FD_ZERO(&checkfds); FD_SET(m_notify.notifyFD(), &checkfds); - FD_SET(m_Server->getFD(), &checkfds); + FD_SET(m_tcpServer->getFD(), &checkfds); - (m_notify.notifyFD() > m_Server->getFD()) ? - (maxfd = m_notify.notifyFD()) : (maxfd = m_Server->getFD()); + (m_notify.notifyFD() > m_tcpServer->getFD()) ? + (maxfd = m_notify.notifyFD()) : (maxfd = m_tcpServer->getFD()); for (;;) { fd_set readfds; @@ -96,8 +96,8 @@ void* Network::run() } // new data from socket - if (FD_ISSET(m_Server->getFD(), &readfds)) { - TCPSocket* socket = m_Server->newSocket(); + if (FD_ISSET(m_tcpServer->getFD(), &readfds)) { + TCPSocket* socket = m_tcpServer->newSocket(); if (socket == NULL) continue; diff --git a/src/ebusd/network.h b/src/ebusd/network.h index 6a633a65..1bc9a26d 100644 --- a/src/ebusd/network.h +++ b/src/ebusd/network.h @@ -35,7 +35,7 @@ public: private: std::list m_connections; WQueue* m_msgQueue; - TCPServer* m_Server; + TCPServer* m_tcpServer; Notify m_notify; bool m_listening; bool m_running; From 773f5a6e30ae64b190563efb58fc8eda35a4b11b Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 20:21:08 +0100 Subject: [PATCH 08/38] dependency tip removed from readme file. --- README | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README b/README index fe5c6c19..c4bb56c2 100644 --- a/README +++ b/README @@ -35,11 +35,6 @@ Features - help print help page -Dependency ----------- - * glibc - - Build ----- From 70ea02b143d663266641a13d05d15a8cd5244440 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 20:57:07 +0100 Subject: [PATCH 09/38] documentation for class Message added. --- src/ebusd/baseloop.cpp | 2 +- src/ebusd/connection.cpp | 2 +- src/ebusd/message.h | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 54051bd6..caa3af70 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -85,7 +85,7 @@ void BaseLoop::start() // send result to client result += '\n'; - Connection* connection = static_cast(message->getSource()); + Connection* connection = message->getConnection(); connection->addResult(Message(result)); delete message; diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index f48bfdd5..2ba605ba 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -77,7 +77,7 @@ void* Connection::run() break; // removed closed socket - if (datalen <= 0 || strncasecmp(data, "quit", 4) == 0) + if (datalen <= 0 || strcasecmp(data, "QUIT") == 0) break; // send data diff --git a/src/ebusd/message.h b/src/ebusd/message.h index 8344643e..55ee0f68 100644 --- a/src/ebusd/message.h +++ b/src/ebusd/message.h @@ -22,20 +22,47 @@ #include +/** forward declaration for class Connection */ +class Connection; +/** + * @brief class for data/message transfer between connection and baseloop + */ class Message { public: - Message(const std::string data, void* source = NULL) : m_data(data), m_source(source) {} - Message(const Message& src) : m_data(src.m_data), m_source(src.m_source) {} + /** + * @brief constructs a new instance with message and source client address + * @param data from client + * @param connection to return result to correct client + */ + Message(const std::string data, Connection* connection=NULL) + : m_data(data), m_connection(connection) {} + /** + * @brief copy constructor. + * @param src message object for copy + */ + Message(const Message& src) : m_data(src.m_data), m_connection(src.m_connection) {} + + /** + * @brief data from client + * @return data string + */ std::string getData() const { return m_data; } - void* getSource() const { return m_source; } + + /** + * @brief original connection + * @return pointer to connection + */ + Connection* getConnection() const { return m_connection; } private: + /** the data/message string */ std::string m_data; - void* m_source; + /** the source connection */ + Connection* m_connection; }; From 2695dd141d89b757e3ad0c236dd54a638f8129a6 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 11 Nov 2014 21:26:42 +0100 Subject: [PATCH 10/38] class Connection: needless functions removed; count replaced with sum for debugging purpose. --- src/ebusd/connection.cpp | 8 ++++---- src/ebusd/connection.h | 10 ++++------ src/ebusd/network.cpp | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index 2ba605ba..f0d6b2d2 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -23,7 +23,7 @@ extern LogInstance& L; -int Connection::m_count = -1; +int Connection::m_sum = 0; void Connection::addResult(Message message) { @@ -85,10 +85,10 @@ void* Connection::run() m_data->add(new Message(data, this)); // wait for result - L.log(net, debug, "[%08x] wait for result", getID()); + L.log(net, debug, "[%05d] wait for result", getID()); Message* message = m_result.remove(); - L.log(net, debug, "[%08x] result added", getID()); + L.log(net, debug, "[%05d] result added", getID()); std::string result(message->getData()); if (m_socket->isValid() == true) @@ -104,7 +104,7 @@ void* Connection::run() delete m_socket; m_running = false; - L.log(net, trace, "[%08x] connection closed", getID()); + L.log(net, trace, "[%05d] connection closed", getID()); return NULL; } diff --git a/src/ebusd/connection.h b/src/ebusd/connection.h index 49c0d690..5e9b160c 100644 --- a/src/ebusd/connection.h +++ b/src/ebusd/connection.h @@ -31,9 +31,7 @@ class Connection : public Thread public: Connection(TCPSocket* socket, WQueue* data) - : m_socket(socket), m_data(data), m_running(false) { m_count++; } - - ~Connection() { m_count--; } + : m_socket(socket), m_data(data), m_running(false) { m_sum++; m_id = m_sum;} void addResult(Message message); @@ -41,8 +39,7 @@ public: void stop() const { m_notify.notify(); } bool isRunning() const { return m_running; } - pthread_t getID() { return this->self(); } - int numConnections() const { return m_count; } + int getID() { return m_id; } private: TCPSocket* m_socket; @@ -50,8 +47,9 @@ private: WQueue m_result; Notify m_notify; bool m_running; + int m_id; - static int m_count; + static int m_sum; }; diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index f054d14e..168ddd2e 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -107,7 +107,7 @@ void* Network::run() connection->start("connection"); m_connections.push_back(connection); - L.log(net, trace, "[%08x] connection opened %s", connection->getID(), socket->getIP().c_str()); + L.log(net, trace, "[%05d] connection opened %s", connection->getID(), socket->getIP().c_str()); } } From b89a441f99da88b78eb3c66dfbe4ff8272316d35 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Thu, 13 Nov 2014 14:18:37 +0100 Subject: [PATCH 11/38] configure.ac: AC_CHECK_HEADERS added. --- configure.ac | 10 ++++++++++ src/lib/ebus/port.cpp | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index c9a30b66..a7faba9b 100644 --- a/configure.ac +++ b/configure.ac @@ -3,6 +3,16 @@ AC_INIT([ebusd], [0.5.0], [ebusd@liwest.at], [ebusd], [https://github.com/yuhu-/ AC_PROG_CXX +AC_CHECK_HEADERS([arpa/inet.h \ + dirent.h \ + fcntl.h \ + netdb.h \ + netinet/in.h \ + pthread.h \ + sys/ioctl.h \ + sys/socket.h \ + termios.h]) + AC_CONFIG_AUX_DIR([build]) AC_CONFIG_SRCDIR([src/ebusd/ebusd.cpp]) AC_CONFIG_HEADERS([config.h]) diff --git a/src/lib/ebus/port.cpp b/src/lib/ebus/port.cpp index c815182b..4cbbb552 100644 --- a/src/lib/ebus/port.cpp +++ b/src/lib/ebus/port.cpp @@ -141,7 +141,7 @@ void DeviceSerial::openDevice(const std::string deviceName, const bool noDeviceC newSettings.c_cc[VTIME] = 0; // empty device buffer - tcflush(m_fd, TCIOFLUSH); + tcflush(m_fd, TCIFLUSH); // activate new settings of serial device tcsetattr(m_fd, TCSANOW, &newSettings); From 5e07f0152d494036c81b7d25c815b21caca42cb6 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Thu, 13 Nov 2014 15:01:54 +0100 Subject: [PATCH 12/38] needless includes directives (header files) removed. --- configure.ac | 2 -- src/lib/ebus/port.cpp | 4 ---- src/lib/utils/tcpsocket.cpp | 4 ---- src/lib/utils/tcpsocket.h | 2 -- 4 files changed, 12 deletions(-) diff --git a/configure.ac b/configure.ac index a7faba9b..ecdd93d3 100644 --- a/configure.ac +++ b/configure.ac @@ -7,10 +7,8 @@ AC_CHECK_HEADERS([arpa/inet.h \ dirent.h \ fcntl.h \ netdb.h \ - netinet/in.h \ pthread.h \ sys/ioctl.h \ - sys/socket.h \ termios.h]) AC_CONFIG_AUX_DIR([build]) diff --git a/src/lib/ebus/port.cpp b/src/lib/ebus/port.cpp index 4cbbb552..b0d2c6c2 100644 --- a/src/lib/ebus/port.cpp +++ b/src/lib/ebus/port.cpp @@ -20,14 +20,10 @@ #include "port.h" #include #include -#include #include #include -#include -#include #include #include -#include namespace libebus diff --git a/src/lib/utils/tcpsocket.cpp b/src/lib/utils/tcpsocket.cpp index 627d0d3c..eb06454e 100644 --- a/src/lib/utils/tcpsocket.cpp +++ b/src/lib/utils/tcpsocket.cpp @@ -19,11 +19,7 @@ #include "tcpsocket.h" #include -#include -#include -#include #include -#include #include #include #include diff --git a/src/lib/utils/tcpsocket.h b/src/lib/utils/tcpsocket.h index fa507204..8f86f8bb 100644 --- a/src/lib/utils/tcpsocket.h +++ b/src/lib/utils/tcpsocket.h @@ -20,8 +20,6 @@ #ifndef LIBUTILS_TCPSOCKET_H_ #define LIBUTILS_TCPSOCKET_H_ -#include -#include #include #include From baf60895efc1d5c9a9bbacd67e518ea90a9e88d2 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Thu, 13 Nov 2014 16:31:32 +0100 Subject: [PATCH 13/38] class Connection, Network, Port: select() replaced by ppoll(). --- configure.ac | 1 + src/ebusd/connection.cpp | 38 ++++++++++++++++++-------------------- src/ebusd/network.cpp | 38 ++++++++++++++++++-------------------- src/lib/ebus/port.cpp | 20 +++++++++++++------- src/lib/ebus/port.h | 2 +- 5 files changed, 51 insertions(+), 48 deletions(-) diff --git a/configure.ac b/configure.ac index ecdd93d3..a5cebf73 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,7 @@ AC_CHECK_HEADERS([arpa/inet.h \ dirent.h \ fcntl.h \ netdb.h \ + poll.h \ pthread.h \ sys/ioctl.h \ termios.h]) diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index f0d6b2d2..b82011cf 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -20,6 +20,7 @@ #include "connection.h" #include "logger.h" #include +#include extern LogInstance& L; @@ -35,39 +36,36 @@ void* Connection::run() { m_running = true; - int maxfd; - fd_set checkfds; - struct timeval timeout; + int ret, nfds = 2; + struct pollfd fds[nfds]; + struct timespec tdiff; - FD_ZERO(&checkfds); - FD_SET(m_notify.notifyFD(), &checkfds); - FD_SET(m_socket->getFD(), &checkfds); + // set select timeout 10 secs + tdiff.tv_sec = 10; + tdiff.tv_nsec = 0; - (m_notify.notifyFD() > m_socket->getFD()) ? - (maxfd = m_notify.notifyFD()) : (maxfd = m_socket->getFD()); + memset(fds, 0, sizeof(fds)); + + fds[0].fd = m_notify.notifyFD(); + fds[0].events = POLLIN; + + fds[1].fd = m_socket->getFD(); + fds[1].events = POLLIN; for (;;) { - fd_set readfds; - int ret; + // wait for new fd event + ret = ppoll(fds, nfds, &tdiff, NULL); - // set select timeout 10 secs - timeout.tv_sec = 10; - timeout.tv_usec = 0; - - // set readfds to inital checkfds - readfds = checkfds; - - ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout); if (ret == 0) { continue; } // new data from notify - if (FD_ISSET(m_notify.notifyFD(), &readfds)) + if (fds[0].revents & POLLIN) break; // new data from socket - if (FD_ISSET(m_socket->getFD(), &readfds)) { + if (fds[1].revents & POLLIN) { char data[256]; size_t datalen; diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index 168ddd2e..f8448d4d 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -20,6 +20,7 @@ #include "network.h" #include "logger.h" #include "appl.h" +#include extern LogInstance& L; extern Appl& A; @@ -61,42 +62,39 @@ void* Network::run() m_running = true; - int maxfd; - fd_set checkfds; - struct timeval timeout; + int ret, nfds = 2; + struct pollfd fds[nfds]; + struct timespec tdiff; - FD_ZERO(&checkfds); - FD_SET(m_notify.notifyFD(), &checkfds); - FD_SET(m_tcpServer->getFD(), &checkfds); + // set select timeout 1 secs + tdiff.tv_sec = 1; + tdiff.tv_nsec = 0; - (m_notify.notifyFD() > m_tcpServer->getFD()) ? - (maxfd = m_notify.notifyFD()) : (maxfd = m_tcpServer->getFD()); + memset(fds, 0, sizeof(fds)); + + fds[0].fd = m_notify.notifyFD(); + fds[0].events = POLLIN; + + fds[1].fd = m_tcpServer->getFD(); + fds[1].events = POLLIN; for (;;) { - fd_set readfds; - int ret; + // wait for new fd event + ret = ppoll(fds, nfds, &tdiff, NULL); - // set select timeout 1 secs - timeout.tv_sec = 1; - timeout.tv_usec = 0; - - // set readfds to inital checkfds - readfds = checkfds; - - ret = select(maxfd + 1, &readfds, NULL, NULL, &timeout); if (ret == 0) { cleanConnections(); continue; } // new data from notify - if (FD_ISSET(m_notify.notifyFD(), &readfds)) { + if (fds[0].revents & POLLIN) { m_running = false; break; } // new data from socket - if (FD_ISSET(m_tcpServer->getFD(), &readfds)) { + if (fds[1].revents & POLLIN) { TCPSocket* socket = m_tcpServer->newSocket(); if (socket == NULL) continue; diff --git a/src/lib/ebus/port.cpp b/src/lib/ebus/port.cpp index b0d2c6c2..132ee039 100644 --- a/src/lib/ebus/port.cpp +++ b/src/lib/ebus/port.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace libebus @@ -68,18 +69,23 @@ ssize_t Device::recvBytes(const long timeout, size_t maxCount) return -1; // TODO RESULT_ERR_DEVICE if (timeout > 0) { - fd_set readfds; - struct timeval tdiff; + int ret, nfds = 1; + struct pollfd fds[nfds]; + struct timespec tdiff; // set select timeout tdiff.tv_sec = 0; - tdiff.tv_usec = timeout; + tdiff.tv_nsec = timeout*1000; - FD_ZERO(&readfds); - FD_SET(m_fd, &readfds); + memset(fds, 0, sizeof(fds)); - if (select(m_fd + 1, &readfds, NULL, NULL, &tdiff) != 1) - return -2; // TODO RESULT_ERR_TIMEOUT + fds[0].fd = m_fd; + fds[0].events = POLLIN; + + ret = ppoll(fds, nfds, &tdiff, NULL); + + if (ret == -1) return -1; // TODO RESULT_ERR_DEVICE + if (ret == 0) return -2; // TODO RESULT_ERR_TIMEOUT } if (maxCount > sizeof(m_buffer)) diff --git a/src/lib/ebus/port.h b/src/lib/ebus/port.h index 7dc8e53a..83badc92 100644 --- a/src/lib/ebus/port.h +++ b/src/lib/ebus/port.h @@ -83,7 +83,7 @@ public: /** * @brief recvBytes read bytes from opened file descriptor. - * @param timeout max time out for new input data. + * @param timeoutmax time for new input data [usec]. * @param maxCount max size of receive buffer. * @return number of read bytes or -1 if an error has occured. */ From 9702a3479c4467fa92d0db8c2c6e8eae57d17ad3 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Fri, 14 Nov 2014 14:57:56 +0100 Subject: [PATCH 14/38] needless enum Status removed. --- src/lib/utils/logger.cpp | 14 +++++++------- src/lib/utils/logger.h | 20 +++++--------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/lib/utils/logger.cpp b/src/lib/utils/logger.cpp index c6708a7a..7921851e 100644 --- a/src/lib/utils/logger.cpp +++ b/src/lib/utils/logger.cpp @@ -66,8 +66,8 @@ int calcLevel(const std::string level) } -LogMessage::LogMessage(const int area, const int level, const std::string text, const Status status) - : m_area(area), m_level(level), m_text(text), m_status(status) +LogMessage::LogMessage(const int area, const int level, const std::string text, const bool run) + : m_area(area), m_level(level), m_text(text), m_run(run) { char time[24]; struct timeval tv; @@ -96,7 +96,7 @@ void* LogSink::run() { while (1) { LogMessage* message = m_queue.remove(); - if (message->getStatus() == LogMessage::End) { + if (message->isRunning() == false) { delete message; while (m_queue.size() == true) { LogMessage* message = m_queue.remove(); @@ -190,7 +190,7 @@ void LogInstance::log(const int area, const int level, const std::string& data, if (vasprintf(&tmp, data.c_str(), ap) != -1) { std::string buffer(tmp); - m_messages.add(new LogMessage(LogMessage(area, level, buffer, LogMessage::Run))); + m_messages.add(new LogMessage(LogMessage(area, level, buffer))); } va_end(ap); @@ -213,9 +213,9 @@ void* LogInstance::run() if (((*iter)->getAreas() & message->getArea() && (*iter)->getLevel() >= message->getLevel()) - && message->getStatus() == LogMessage::Run) { + && message->isRunning() == true) { (*iter)->addMessage(*message); - } else if (message->getStatus() == LogMessage::End) { + } else if (message->isRunning() == false) { (*iter)->addMessage(*message); m_running = false; } @@ -231,6 +231,6 @@ void* LogInstance::run() void LogInstance::stop() { - m_messages.add(new LogMessage(LogMessage(bas, error, "", LogMessage::End))); + m_messages.add(new LogMessage(LogMessage(bas, error, "", false))); usleep(100000); } diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index e91261b8..f8ac4b39 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -39,31 +39,27 @@ class LogMessage { public: - enum Status { Run, End }; - - LogMessage(const int area, const int level, const std::string text, const Status status); - - ~LogMessage() {} + LogMessage(const int area, const int level, const std::string text, const bool run=true); LogMessage(const LogMessage& src) : m_area(src.m_area), m_level(src.m_level), m_text(src.m_text), - m_status(src.m_status), m_time(src.m_time) {} + m_run(src.m_run), m_time(src.m_time) {} void operator= (const LogMessage& src) { m_area = src.m_area; m_level = src.m_level; m_text = src.m_text; - m_status = src.m_status; m_time = src.m_time; } + m_run = src.m_run; m_time = src.m_time; } int getArea() const { return (m_area); } int getLevel() const { return(m_level); } std::string getText() const { return (m_text.c_str()); } - Status getStatus() const { return (m_status); } + bool isRunning() const { return (m_run); } std::string getTime() const { return (m_time.c_str()); } private: int m_area; int m_level; std::string m_text; - Status m_status; + bool m_run; std::string m_time; }; @@ -77,8 +73,6 @@ public: LogSink(const int areas, const int level, const Type type, const char* name) : m_areas(areas), m_level(level), m_type(type), m_name(name) {} - virtual ~LogSink() {} - void addMessage(const LogMessage& message); void* run(); @@ -113,8 +107,6 @@ public: : LogSink(areas, level, Console, name), m_instance(++m_numInstance) { this->start(name); } - ~LogConsole() {} - private: const int m_instance; static int m_numInstance; @@ -131,8 +123,6 @@ public: : LogSink(areas, level, Logfile, name), m_filename(filename), m_instance(++m_numInstance) { this->start(name); } - ~LogFile() {} - private: std::string m_filename; const int m_instance; From d27afa928c442e5ea58e7aa42fe342c5d26ab837 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Fri, 14 Nov 2014 16:41:05 +0100 Subject: [PATCH 15/38] check for pthread function 'pthread_setname_np' added. --- Makefile.am | 2 +- configure.ac | 6 +++++- src/lib/utils/thread.cpp | 5 +++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile.am b/Makefile.am index 2bf1f2d5..ef6ef5c0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = src/lib/ebus src/lib/ebus/test src/lib/utils src/ebusd src/ebusctl +SUBDIRS = src/lib/utils src/lib/ebus src/lib/ebus/test src/ebusd src/ebusctl distclean-local: -rm -rf autom4te.cache diff --git a/configure.ac b/configure.ac index a5cebf73..589f43f5 100644 --- a/configure.ac +++ b/configure.ac @@ -12,13 +12,17 @@ AC_CHECK_HEADERS([arpa/inet.h \ sys/ioctl.h \ termios.h]) +AC_CHECK_LIB([pthread], [pthread_setname_np], + AC_DEFINE([HAVE_PTHREAD_SETNAME_NP], [1], ["Define to 1 if pthread has pthread_setname_np"]), + AC_MSG_RESULT([Could not find pthread_setname_np in pthread])) + AC_CONFIG_AUX_DIR([build]) AC_CONFIG_SRCDIR([src/ebusd/ebusd.cpp]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([Makefile + src/lib/utils/Makefile src/lib/ebus/Makefile src/lib/ebus/test/Makefile - src/lib/utils/Makefile src/ebusd/Makefile src/ebusctl/Makefile]) diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index 48dbc250..7393587b 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -17,6 +17,7 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ +#include "config.h" #include "thread.h" static void* runThread(void* arg) @@ -39,7 +40,11 @@ int Thread::start(const char* name) int result = pthread_create(&m_threadid, NULL, runThread, this); if (result == 0) { + +#ifdef HAVE_PTHREAD_SETNAME_NP pthread_setname_np(m_threadid, name); +#endif + m_running = true; } From 4cc9055e71a48ae587f292e95018be8e3804c0cd Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Fri, 14 Nov 2014 20:56:15 +0100 Subject: [PATCH 16/38] check against HAVE_CONFIG_H added. --- src/ebusctl/ebusctl.cpp | 5 ++++- src/ebusd/ebusd.cpp | 5 ++++- src/lib/utils/thread.cpp | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index 2bcf9357..3f2d1caf 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -17,7 +17,10 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#include "config.h" +#ifdef HAVE_CONFIG_H +#include +#endif + #include "appl.h" #include "port.h" #include "decode.h" diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index c5eacde9..797783d0 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -17,7 +17,10 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#include "config.h" +#ifdef HAVE_CONFIG_H +#include +#endif + #include "logger.h" #include "daemon.h" #include "appl.h" diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index 7393587b..fa4ca38f 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -17,7 +17,10 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#include "config.h" +#ifdef HAVE_CONFIG_H +#include +#endif + #include "thread.h" static void* runThread(void* arg) From 32733008937ecaa85c08851b6de70282055bec2c Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sat, 15 Nov 2014 23:03:33 +0100 Subject: [PATCH 17/38] class Appl reworked. --- src/ebusctl/ebusctl.cpp | 55 +++---- src/ebusd/baseloop.cpp | 16 +- src/ebusd/ebusd.cpp | 123 ++++++--------- src/ebusd/ebusloop.cpp | 28 ++-- src/ebusd/network.cpp | 4 +- src/lib/utils/appl.cpp | 331 +++++++++++++++++++++------------------- src/lib/utils/appl.h | 208 +++++++++++++++++-------- 7 files changed, 416 insertions(+), 349 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index 3f2d1caf..8279ece1 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -35,39 +35,34 @@ using namespace libebus; -Appl& A = Appl::Instance(); +Appl& A = Appl::Instance(true); void define_args() { - A.addArgs("COMMAND {ARGS...}\n\n" - " local commands:\n" + A.setVersion("ebusctl is part of """PACKAGE_STRING""); + + A.addText(" local commands:\n" " 'scan' scans the bus and identifies the participants\n\n" " 'feed' sends a dump file to a local virtual serial device\n" " (hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)\n\n" " remote commands:\n" - " send 'help' to server", 1); + " send 'help' to server\n\n" + " Options:\n"); - A.addItem("p_device", Appl::Param("/dev/ttyUSB60"), "d", "device", - "virtual serial device (/dev/ttyUSB60)", - Appl::type_string, Appl::opt_mandatory); + A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, + "virtual serial device (/dev/ttyUSB60)"); - A.addItem("p_file", Appl::Param(""), "f", "file", - "dump file with raw data", - Appl::type_string, Appl::opt_mandatory); + A.addOption("file", "f", OptVal("/tmp/ebus_dump.bin"),dt_string, ot_mandatory, + "dump file name (/tmp/ebus_dump.bin)"); - A.addItem("p_time", Appl::Param(10000), "t", "time", - "delay between 2 bytes in 'us' (10000)\n", - Appl::type_long, Appl::opt_mandatory); + A.addOption("time", "t", OptVal(10000), dt_long, ot_mandatory, + "delay between 2 bytes in 'us' (10000)\n"); - A.addItem("p_server", Appl::Param("localhost"), "s", "server", - "name or ip (localhost)", - Appl::type_string, Appl::opt_mandatory); + A.addOption("server", "s", OptVal("localhost"), dt_string, ot_mandatory, + "name or ip (localhost)"); - A.addItem("p_port", Appl::Param(8888), "p", "port", - "port (8888)\n", - Appl::type_int, Appl::opt_mandatory); - - A.addVersion("ebusctl is part of """PACKAGE_STRING""); + A.addOption("port", "p", OptVal(8888), dt_int, ot_mandatory, + "port (8888)\n"); } @@ -175,14 +170,14 @@ int main(int argc, char* argv[]) A.parseArgs(argc, argv); if (strcasecmp(A.getArg(0).c_str(), "feed") == 0) { - std::string dev(A.getParam("p_device")); + std::string dev(A.getOptVal("device")); Port port(dev, true); port.open(); if(port.isOpen() == true) { std::cout << "openPort successful." << std::endl; - std::fstream file(A.getParam("p_file"), std::ios::in | std::ios::binary); + std::fstream file(A.getOptVal("file"), std::ios::in | std::ios::binary); if(file.is_open() == true) { @@ -192,25 +187,25 @@ int main(int argc, char* argv[]) << static_cast(byte) << std::endl; port.send(&byte, 1); - usleep(A.getParam("p_time")); + usleep(A.getOptVal("time")); } file.close(); } else { - std::cout << "error opening file " << A.getParam("p_file") << std::endl; + std::cout << "error opening file " << A.getOptVal("file") << std::endl; } port.close(); if(port.isOpen() == false) std::cout << "closePort successful." << std::endl; } else { - std::cout << "error opening device " << A.getParam("p_device") << std::endl; + std::cout << "error opening device " << A.getOptVal("device") << std::endl; } } else { TCPClient* client = new TCPClient(); - TCPSocket* socket = client->connect(A.getParam("p_server"), A.getParam("p_port")); + TCPSocket* socket = client->connect(A.getOptVal("server"), A.getOptVal("port")); if (socket != NULL) { @@ -262,7 +257,7 @@ int main(int argc, char* argv[]) } else { // build message std::string message(A.getArg(0)); - for (size_t i = 1; i < A.numArg(); i++) { + for (int i = 1; i < A.numArgs(); i++) { message += " "; message += A.getArg(i); } @@ -280,8 +275,8 @@ int main(int argc, char* argv[]) delete socket; } else { - std::cout << "error connecting to " << A.getParam("p_server") - << ":" << A.getParam("p_port") << std::endl; + std::cout << "error connecting to " << A.getOptVal("server") + << ":" << A.getOptVal("port") << std::endl; } delete client; diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index caa3af70..e4054867 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -28,8 +28,8 @@ extern Appl& A; BaseLoop::BaseLoop() { // create commands DB - m_commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); - L.log(bas, trace, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); + m_commands = ConfigCommands(A.getOptVal("ebusconfdir"), CSV).getCommands(); + L.log(bas, trace, "ebus configuration dir: %s", A.getOptVal("ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); L.log(bas, event, " polling DB: %d ", m_commands->sizePolDB()); @@ -39,7 +39,7 @@ BaseLoop::BaseLoop() m_ebusloop->start("ebusloop"); // create network - m_network = new Network(A.getParam("p_localhost"), &m_msgQueue); + m_network = new Network(A.getOptVal("localhost"), &m_msgQueue); m_network->start("network"); } @@ -147,7 +147,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - std::string ebusCommand(A.getParam("p_address")); + std::string ebusCommand(A.getOptVal("address")); ebusCommand += m_commands->getEbusCommand(index); std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); @@ -188,7 +188,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) if (index >= 0) { - std::string ebusCommand(A.getParam("p_address")); + std::string ebusCommand(A.getOptVal("address")); ebusCommand += m_commands->getEbusCommand(index); // encode data @@ -268,7 +268,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) } { - std::string ebusCommand(A.getParam("p_address")); + std::string ebusCommand(A.getOptVal("address")); cmd[1].erase(std::remove_if(cmd[1].begin(), cmd[1].end(), isspace), cmd[1].end()); ebusCommand += cmd[1]; std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); @@ -344,8 +344,8 @@ std::string BaseLoop::decodeMessage(const std::string& data) { // create commands DB - Commands* commands = ConfigCommands(A.getParam("p_ebusconfdir"), CSV).getCommands(); - L.log(bas, trace, "ebus configuration dir: %s", A.getParam("p_ebusconfdir")); + Commands* commands = ConfigCommands(A.getOptVal("ebusconfdir"), CSV).getCommands(); + L.log(bas, trace, "ebus configuration dir: %s", A.getOptVal("ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); L.log(bas, event, " polling DB: %d ", m_commands->sizePolDB()); diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 797783d0..5ab43aaf 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -38,93 +38,70 @@ BaseLoop* baseloop; void define_args() { - A.addArgs("", 0); - A.addItem("p_address", Appl::Param("FF"), "a", "address", - "\tebus device address (FF)", - Appl::type_string, Appl::opt_mandatory); + A.setVersion(""PACKAGE_STRING""); - A.addItem("p_device", Appl::Param("/dev/ttyUSB0"), "d", "device", - "\tebus device (serial or network) (/dev/ttyUSB0)", - Appl::type_string, Appl::opt_mandatory); + A.addText("test"); - A.addItem("p_nodevicecheck", Appl::Param(false), "n", "nodevicecheck", - "disable valid ebus device test\n", - Appl::type_bool, Appl::opt_none); + A.addOption("address", "a", OptVal("FF"), dt_string, ot_mandatory, + "\tebus device address (FF)"); - A.addItem("p_sendretries", Appl::Param(2), "s", "sendretries", - "number retries send ebus command (2)", - Appl::type_int, Appl::opt_mandatory); + A.addOption("device", "d", OptVal("/dev/ttyUSB0"), dt_string, ot_mandatory, + "\tebus device (serial or network) (/dev/ttyUSB0)"); - A.addItem("p_lockretries", Appl::Param(2), "", "lockretries", - "number retries to lock ebus (2)", - Appl::type_int, Appl::opt_mandatory); + A.addOption("nodevicecheck", "n", OptVal(false), dt_bool, ot_none, + "disable valid ebus device test\n"); - A.addItem("p_lockcounter", Appl::Param(5), "", "lockcounter", - "number of SYN to unlock send function (5)", - Appl::type_int, Appl::opt_mandatory); + A.addOption("sendretries", "s", OptVal(2), dt_int, ot_mandatory, + "number retries send ebus command (2)"); - A.addItem("p_recvtimeout", Appl::Param(15000), "", "recvtimeout", - "receive timeout in 'us' (15000)", - Appl::type_long, Appl::opt_mandatory); + A.addOption("lockretries", "", OptVal(2), dt_int, ot_mandatory, + "number retries to lock ebus (2)"); - A.addItem("p_acquiretime", Appl::Param(4200), "", "acquiretime", - "waiting time for bus acquire in 'us' (4200)\n", - Appl::type_long, Appl::opt_mandatory); + A.addOption("lockcounter", "", OptVal(5), dt_int, ot_mandatory, + "number of SYN to unlock send function (5)"); - A.addItem("p_pollinterval", Appl::Param(5), "", "pollinterval", - "polling interval in 's' (5)\n", - Appl::type_int, Appl::opt_mandatory); + A.addOption("recvtimeout", "", OptVal(15000), dt_long, ot_mandatory, + "receive timeout in 'us' (15000)"); - A.addItem("p_ebusconfdir", Appl::Param("/etc/ebusd"), "e", "ebusconfdir", - "directory for ebus configuration (/etc/ebusd)\n", - Appl::type_string, Appl::opt_mandatory); + A.addOption("acquiretime", "", OptVal(4200), dt_long, ot_mandatory, + "waiting time for bus acquire in 'us' (4200)\n"); - A.addItem("p_foreground", Appl::Param(false), "f", "foreground", - "run in foreground\n", - Appl::type_bool, Appl::opt_none); + A.addOption("pollinterval", "", OptVal(5), dt_int, ot_mandatory, + "polling interval in 's' (5)\n"); - A.addItem("p_port", Appl::Param(8888), "p", "port", - "\tlisten port (8888)", - Appl::type_int, Appl::opt_mandatory); + A.addOption("ebusconfdir", "e", OptVal("/etc/ebusd"), dt_string, ot_mandatory, + "directory for ebus configuration (/etc/ebusd)\n"); - A.addItem("p_localhost", Appl::Param(false), "", "localhost", - "listen localhost only\n", - Appl::type_bool, Appl::opt_none); + A.addOption("foreground", "f", OptVal(false), dt_bool, ot_none, + "run in foreground\n"); - A.addItem("p_logfile", Appl::Param("/var/log/ebusd.log"), "l", "logfile", - "\tlog file name (/var/log/ebusd.log)", - Appl::type_string, Appl::opt_mandatory); + A.addOption("port", "p", OptVal(8888), dt_int, ot_mandatory, + "\tlisten port (8888)"); - A.addItem("p_logareas", Appl::Param("all"), "", "logareas", - "\tlog areas - bas|net|bus|cyc|all (all)", - Appl::type_string, Appl::opt_mandatory); + A.addOption("localhost", "", OptVal(false), dt_bool, ot_none, + "listen localhost only\n"); - A.addItem("p_loglevel", Appl::Param("trace"), "", "loglevel", - "\tlog level - error|event|trace|debug (event)", - Appl::type_string, Appl::opt_mandatory); + A.addOption("logfile", "l", OptVal("/var/log/ebusd.log"), dt_string, ot_mandatory, + "\tlog file name (/var/log/ebusd.log)"); - A.addItem("p_lograwdata", Appl::Param(false), "", "lograwdata", - "log raw data (bytes)\n", - Appl::type_bool, Appl::opt_none); + A.addOption("logareas", "", OptVal("all"), dt_string, ot_mandatory, + "\tlog areas - bas|net|bus|cyc|all (all)"); - A.addItem("p_dump", Appl::Param(false), "D", "dump", - "\tenable dump", - Appl::type_bool, Appl::opt_none); + A.addOption("loglevel", "", OptVal("trace"), dt_string, ot_mandatory, + "\tlog level - error|event|trace|debug (event)"); - A.addItem("p_dumpfile", Appl::Param("/tmp/ebus_dump.bin"), "", "dumpfile", - "\tdump file name (/tmp/ebus_dump.bin)", - Appl::type_string, Appl::opt_mandatory); + A.addOption("lograwdata", "", OptVal(false), dt_bool, ot_none, + "log raw data (bytes)\n"); - A.addItem("p_dumpsize", Appl::Param(100), "", "dumpsize", - "\tmax size for dump file in 'kB' (100)\n", - Appl::type_long, Appl::opt_mandatory); + A.addOption("dump", "D", OptVal(false), dt_bool, ot_none, + "\tenable dump"); - A.addItem("p_settings", Appl::Param(false), "", "settings", - "\tprint daemon settings\n", - Appl::type_bool, Appl::opt_none); + A.addOption("dumpfile", "", OptVal("/tmp/ebus_dump.bin"), dt_string, ot_mandatory, + "\tdump file name (/tmp/ebus_dump.bin)"); - A.addVersion(""PACKAGE_STRING""); + A.addOption("dumpsize", "", OptVal(100), dt_long, ot_mandatory, + "\tmax size for dump file in 'kB' (100)\n"); } void shutdown() @@ -177,20 +154,16 @@ int main(int argc, char* argv[]) // parse arguments A.parseArgs(argc, argv); - // print daemon settings - if (A.getParam("p_settings") == true) - A.printSettings(); - // make me daemon - if (A.getParam("p_foreground") == true) { - L += new LogConsole(calcAreas(A.getParam("p_logareas")), - calcLevel(A.getParam("p_loglevel")), + if (A.getOptVal("foreground") == true) { + L += new LogConsole(calcAreas(A.getOptVal("logareas")), + calcLevel(A.getOptVal("loglevel")), "logconsole"); } else { D.run("/var/run/ebusd.pid"); - L += new LogFile(calcAreas(A.getParam("p_logareas")), - calcLevel(A.getParam("p_loglevel")), - "logfile", A.getParam("p_logfile")); + L += new LogFile(calcAreas(A.getOptVal("logareas")), + calcLevel(A.getOptVal("loglevel")), + "logfile", A.getOptVal("logfile")); } // trap signals that we expect to receive diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/ebusloop.cpp index 793a0f4a..7d248cb8 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/ebusloop.cpp @@ -27,26 +27,26 @@ extern Appl& A; EBusLoop::EBusLoop(Commands* commands) : m_commands(commands), m_stop(false), m_lockCounter(0), m_priorRetry(false) { - m_port = new Port(A.getParam("p_device"), A.getParam("p_nodevicecheck")); + m_port = new Port(A.getOptVal("device"), A.getOptVal("nodevicecheck")); m_port->open(); if (m_port->isOpen() == false) - L.log(bus, error, "can't open %s", A.getParam("p_device")); + L.log(bus, error, "can't open %s", A.getOptVal("device")); - m_dump = new Dump(A.getParam("p_dumpfile"), A.getParam("p_dumpsize")); - m_dumpState = A.getParam("p_dump"); + m_dump = new Dump(A.getOptVal("dumpfile"), A.getOptVal("dumpsize")); + m_dumpState = A.getOptVal("dump"); - m_logRawData = A.getParam("p_lograwdata"); + m_logRawData = A.getOptVal("lograwdata"); - m_pollInterval = A.getParam("p_pollinterval"); + m_pollInterval = A.getOptVal("pollinterval"); - m_recvTimeout = A.getParam("p_recvtimeout"); + m_recvTimeout = A.getOptVal("recvtimeout"); - m_sendRetries = A.getParam("p_sendretries"); + m_sendRetries = A.getOptVal("sendretries"); - m_lockRetries = A.getParam("p_lockretries"); + m_lockRetries = A.getOptVal("lockretries"); - m_acquireTime = A.getParam("p_acquiretime"); + m_acquireTime = A.getOptVal("acquiretime"); } EBusLoop::~EBusLoop() @@ -134,7 +134,7 @@ void* EBusLoop::run() } lockRetries = 0; - m_lockCounter = A.getParam("p_lockcounter"); + m_lockCounter = A.getOptVal("lockcounter"); } else if (busResult == RESULT_ERR_BUS_LOST) { L.log(bus, trace, " acquire bus failed"); @@ -154,7 +154,7 @@ void* EBusLoop::run() L.log(bus, trace, " lock retry %d", lockRetries); } - m_lockCounter = A.getParam("p_lockcounter"); + m_lockCounter = A.getOptVal("lockcounter"); } } @@ -166,7 +166,7 @@ void* EBusLoop::run() m_port->open(); if (m_port->isOpen() == false) - L.log(bus, error, "can't open %s", A.getParam("p_device")); + L.log(bus, error, "can't open %s", A.getOptVal("device")); } @@ -271,7 +271,7 @@ void EBusLoop::addPollCommand() tmp += (*m_commands)[index][2]; L.log(bus, event, " polling [%4d] %s", index, tmp.c_str()); - std::string ebusCommand(A.getParam("p_address")); + std::string ebusCommand(A.getOptVal("address")); ebusCommand += m_commands->getEbusCommand(index); std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index f8448d4d..2762a09e 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -30,9 +30,9 @@ Network::Network(const bool localhost, WQueue* msgQueue) : m_msgQueue(msgQueue), m_listening(false), m_running(false) { if (localhost == true) - m_tcpServer = new TCPServer(A.getParam("p_port"), "127.0.0.1"); + m_tcpServer = new TCPServer(A.getOptVal("port"), "127.0.0.1"); else - m_tcpServer = new TCPServer(A.getParam("p_port"), "0.0.0.0"); + m_tcpServer = new TCPServer(A.getOptVal("port"), "0.0.0.0"); if (m_tcpServer != NULL && m_tcpServer->start() == 0) m_listening = true; diff --git a/src/lib/utils/appl.cpp b/src/lib/utils/appl.cpp index 83e22c69..8023984e 100644 --- a/src/lib/utils/appl.cpp +++ b/src/lib/utils/appl.cpp @@ -22,46 +22,162 @@ #include #include -Appl& Appl::Instance() +Appl& Appl::Instance(const bool command) { - static Appl instance; + static Appl instance(command); return instance; } Appl::~Appl() { - m_args.clear(); - m_params.clear(); + m_opts.clear(); + m_optvals.clear(); } -void Appl::addArgs(const std::string argTxt, const int argNum) +void Appl::addText(const char* text) { - m_argTxt = argTxt; - m_argNum = argNum; + opt_t opt; + opt.name = "__text_only__"; + opt.shortname = ""; + opt.datatype = dt_none; + opt.optiontype = ot_none; + opt.description = text; + m_opts.push_back(opt); } -void Appl::addItem(const char* name, Param param, const char* shortname, - const char* longname, const char* description, - Datatype datatype, Optiontype optiontype) +void Appl::addOption(const char* name, const char* shortname, OptVal optval, + DataType datatype, OptionType optiontype, const char* description) { - if (strlen(name) != 0) - m_params[name] = param; + if (strlen(name) != 0) { - if (strlen(longname) != 0) { - Arg arg; - arg.name = name; - arg.shortname = shortname; - arg.longname = longname; - arg.description = description; - arg.datatype = datatype; - arg.optiontype = optiontype; - m_args.push_back(arg); + m_optvals[name] = optval; + + opt_t opt; + opt.name = name; + opt.shortname = shortname; + opt.datatype = datatype; + opt.optiontype = optiontype; + opt.description = description; + m_opts.push_back(opt); } } -void Appl::addVersion(const char* version) +void Appl::parseArgs(int argc, char* argv[]) { - m_version = version; + std::vector _argv(argv, argv + argc); + m_argv = _argv; + + // walk through all arguments + for (int i = 1; i < argc; i++) { + + // find option with long format '--' + if (_argv[i].rfind("--") == 0 && _argv[i].size() > 2) { + + // is next item an added argument? + if (i+1 < argc && _argv[i+1].rfind("-", 0) == std::string::npos) { + if (checkOption(_argv[i].substr(2), _argv[i+1]) == false) + printHelp(); + } + else { + if (checkOption(_argv[i].substr(2), "") == false) + printHelp(); + } + + // find option with short format '-' + } else if (_argv[i].rfind("-") == 0 && _argv[i].size() > 1) { + + // walk through all characters + for (size_t j = 1; j < _argv[i].size(); j++) { + + // only last charater could have an argument + if (i+1 < argc && _argv[i+1].rfind("-", 0) == std::string::npos + && j+1 == _argv[i].size()) { + if (checkOption(_argv[i].substr(j,1), _argv[i+1]) == false) + printHelp(); + } + else { + if (checkOption(_argv[i].substr(j,1), "") == false) + printHelp(); + } + } + } + + } + + // check command + if (m_needCommand == true) { + for (int i = 1; i < argc; i++) { + + if (_argv[i].rfind("-", 0) != std::string::npos) { + i++; + continue; + } + m_arguments.push_back(_argv[i]); + } + + if (m_arguments.size() == 0) { + std::cerr << std::endl << "command needed" << std::endl; + printHelp(); + } + } + +} + +bool Appl::checkOption(const std::string& option, const std::string& value) +{ + if (strcmp(option.c_str(), "settings") == 0) + printSettings(); + + if (strcmp(option.c_str(), "v") == 0 || strcmp(option.c_str(), "version") == 0) + printVersion(); + + if (strcmp(option.c_str(), "h") == 0 || strcmp(option.c_str(), "help") == 0) + printHelp(); + + for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) { + if (o_it->shortname == option || o_it->name == option) { + + // need this option and argument? + if (o_it->optiontype == ot_mandatory && value.size() == 0) { + std::cerr << std::endl << "option requires an argument '" + << option << "'" << std::endl; + return false; + } + + // add given value to option + if ((o_it->optiontype == ot_optional && value.size() != 0) + || o_it->optiontype != ot_optional) + setOptVal(o_it->name, value, o_it->datatype); + + return true; + } + } + + std::cerr << std::endl << "unknown option '" << option << "'" << std::endl; + return false; +} + +void Appl::setOptVal(const char* option, const std::string value, DataType datatype) +{ + switch (datatype) { + case dt_bool: + m_optvals[option] = true; + break; + case dt_int: + m_optvals[option] = strtol(value.c_str(), NULL, 10); + break; + case dt_long: + m_optvals[option] = strtol(value.c_str(), NULL, 10); + break; + case dt_float: + m_optvals[option] = static_cast(strtod(value.c_str(), NULL)); + break; + case dt_string: + m_optvals[option] = value.c_str(); + break; + default: + break; + } } void Appl::printVersion() @@ -75,163 +191,60 @@ void Appl::printHelp() std::cerr << std::endl << "Usage:" << std::endl << " " << m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ; - if (m_argTxt.size() != 0) - std::cerr << " " << m_argTxt; + if (m_needCommand == true) + std::cerr << " COMMAND {ARGS...}" << std::endl << std::endl; + else + std::cerr << std::endl << std::endl; - std::cerr << std::endl << std::endl << "Options:" << std::endl << std::endl; - - for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) { - const char* c = (strlen(a_it->shortname) == 1) ? a_it->shortname : " "; - std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c - << " | --" << a_it->longname - << "\t" << a_it->description - << std::endl; + for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) { + if (strcmp(o_it->name, "__text_only__") == 0) + continue; + else { + const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " "; + std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c + << " | --" << o_it->name + << "\t" << o_it->description + << std::endl; + } } - std::cerr << "-v | --version\n-h | --help" << std::endl << std::endl; + std::cerr << " | --settings\n-v | --version\n-h | --help" << std::endl << std::endl; exit(EXIT_SUCCESS); } -void Appl::parseArgs(int argc, char* argv[]) -{ - std::vector _argv(argv, argv + argc); - m_argc = argc; - m_argv = _argv; - - for (size_t i = 1; i < m_argc; i++) { - - // find option with long format '--' - if (m_argv[i].rfind("--") == 0 && m_argv[i].size() > 2) { - // is next item an added argument? - if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos) { - if (checkArg(m_argv[i].substr(2), m_argv[i+1]) == false) - printHelp(); - } else { - if (checkArg(m_argv[i].substr(2), "") == false) - printHelp(); - } - - // find option with short format '-' - } else if (m_argv[i].rfind("-") == 0 && m_argv[i].size() > 1) { - - // walk through all characters - for (size_t j = 1; j < m_argv[i].size(); j++) { - - // only last charater could have an argument - if (i+1 < m_argc && m_argv[i+1].rfind("-", 0) == std::string::npos - && j+1 == m_argv[i].size()) { - if (checkArg(m_argv[i].substr(j,1), m_argv[i+1]) == false) - printHelp(); - } else { - if (checkArg(m_argv[i].substr(j,1), "") == false) - printHelp(); - } - } - } - - } - - // check args - if (m_argNum > 0) { - if (m_argc < (m_argNum + 1)) - printHelp(); - - for (size_t i = 1; i < m_argc; i++) { - - if (m_argv[i].rfind("-", 0) != std::string::npos) { - i++; - continue; - } - m_argValues.push_back(m_argv[i]); - } - - if (m_argValues.size() < m_argNum) - printHelp(); - } -} - void Appl::printSettings() { - std::cerr << std::endl << "Settings:" << std::endl; + std::cerr << std::endl << "Settings:" << std::endl << std::endl; - for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) { - const char* c = (strlen(a_it->shortname) == 1) ? a_it->shortname : " "; + for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) { + if (strcmp(o_it->name, "__text_only__") == 0) + continue; + + const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " "; std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c - << " | --" << a_it->longname + << " | --" << o_it->name << " = "; - if (a_it->datatype == type_bool) { - if (getParam(a_it->name) == true) + if (o_it->datatype == dt_bool) { + if (getOptVal(o_it->name) == true) std::cerr << "yes" << std::endl; else std::cerr << "no" << std::endl; } - else if (a_it->datatype == type_int) { - std::cerr << getParam(a_it->name) << std::endl; + else if (o_it->datatype == dt_int) { + std::cerr << getOptVal(o_it->name) << std::endl; } - else if (a_it->datatype == type_long) { - std::cerr << getParam(a_it->name) << std::endl; + else if (o_it->datatype == dt_long) { + std::cerr << getOptVal(o_it->name) << std::endl; } - else if (a_it->datatype == type_float) { - std::cerr << getParam(a_it->name) << std::endl; + else if (o_it->datatype == dt_float) { + std::cerr << getOptVal(o_it->name) << std::endl; } - else if (a_it->datatype == type_string) { - std::cerr << getParam(a_it->name) << std::endl; + else if (o_it->datatype == dt_string) { + std::cerr << getOptVal(o_it->name) << std::endl; } } std::cerr << std::endl; + exit(EXIT_SUCCESS); } - -bool Appl::checkArg(const std::string& name, const std::string& arg) -{ - if (strcmp(name.c_str(), "v") == 0 || strcmp(name.c_str(), "version") == 0) - printVersion(); - - if (strcmp(name.c_str(), "h") == 0 || strcmp(name.c_str(), "help") == 0) - printHelp(); - - for (a_it = m_args.begin(); a_it < m_args.end(); a_it++) { - if (a_it->shortname == name || a_it->longname == name) { - if (a_it->optiontype == opt_mandatory && arg.size() == 0) { - std::cerr << std::endl << "option requires an argument '" - << name << "'" << std::endl; - return false; - } - - if ((a_it->optiontype == opt_optional && arg.size() != 0) - || a_it->optiontype != opt_optional) - addParam(a_it->name, arg, a_it->datatype); - - return true; - } - } - - std::cerr << std::endl << "unknown option '" << name << "'" << std::endl; - return false; -} - -void Appl::addParam(const char* name, const std::string arg, Datatype datatype) -{ - switch (datatype) { - case type_bool: - m_params[name] = true; - break; - case type_int: - m_params[name] = strtol(arg.c_str(), NULL, 10); - break; - case type_long: - m_params[name] = strtol(arg.c_str(), NULL, 10); - break; - case type_float: - m_params[name] = static_cast(strtod(arg.c_str(), NULL)); - break; - case type_string: - m_params[name] = arg.c_str(); - break; - default: - break; - } -} - - diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 15a9ca77..35d9657d 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -25,93 +25,179 @@ #include #include +/** the available data types. */ +enum DataType { + dt_none, // default value + dt_bool, // + dt_int, // + dt_long, // + dt_float, // + dt_string, // +}; + +/** option types. */ +enum OptionType { + ot_none, // default value + ot_optional, // a value is optional + ot_mandatory, // a value is mandatory +}; + +/** structure for defining application options */ +typedef struct opt { + const char* name; + const char* shortname; + const char* description; + DataType datatype; + OptionType optiontype; +} opt_t; + +/** union for option values */ +union OptVal { + bool b; + int i; + long l; + float f; + const char* c; + + OptVal() { memset(this, 0, sizeof(OptVal)); } + OptVal(bool _b) : b(_b) {} + OptVal(int _i) : i(_i) {} + OptVal(long _l) : l(_l) {} + OptVal(float _f) : f(_f) {} + OptVal(const char* _c) : c(_c) {} +}; + + +/** + * @brief class for all kinds of application parameters. + */ class Appl { -private: - struct Option; - public: - enum Datatype { type_none, type_bool, type_int, type_long, type_float, type_string }; - enum Optiontype { opt_none, opt_optional, opt_mandatory }; - - union Param { - bool b; - int i; - long l; - float f; - const char* c; - - Param() { memset(this, 0, sizeof(Param)); } - Param(bool _b) : b(_b) {} - Param(int _i) : i(_i) {} - Param(long _l) : l(_l) {} - Param(float _f) : f(_f) {} - Param(const char* _c) : c(_c) {} - }; - - template - T getParam(const char* name) - { - p_it = m_params.find(name); - return (reinterpret_cast(p_it->second)); - } - - static Appl& Instance(); + /** + * @brief create an Appl instance and return the reference. + * @param command is true if an command is needed. + * @return the reference to instance. + */ + static Appl& Instance(const bool command=false); + /** + * @brief destructor. + */ ~Appl(); - void addArgs(const std::string argTxt, const int argNum); - size_t numArg() const { return m_argValues.size(); } - std::string getArg(const int argNum) const { return m_argValues[argNum]; } + /** + * @brief save application version string. + * @param version string + */ + void setVersion(const char* version) { m_version = version; } - void addItem(const char* name, Param param, const char* shortname, - const char* longname, const char* description, - Datatype datatype, Optiontype optiontype); + /** + * @brief create new entry of application option only for help page. + * @param text string to print + */ + void addText(const char* text); - void addVersion(const char* version); + /** + * @brief create new entry of application option. + * @param name the long name + * @param shortname optional short name + * @param optval value of option + * @param datatype data type of option value + * @param optiontype type of given option + * @param description hint text for help page + */ + void addOption(const char* name, const char* shortname, OptVal optval, + DataType datatype, OptionType optiontype, const char* description); + /** + * @brief returns the value of the interested option + * @param the interested option + * @return casted value + */ + template + T getOptVal(const char* name) + { + ov_it = m_optvals.find(name); + return (reinterpret_cast(ov_it->second)); + } + + /** + * @brief parse application arguments. + * @param argc the number of options + * @param argv the given options + */ void parseArgs(int argc, char* argv[]); - void printSettings(); + + /** + * @brief returns the number of saved commands and arguments + * @return number of commands and arguments + */ + int numArgs() const { return m_arguments.size(); } + + /** + * @brief returns the string of an interested argument (0 = command) + * @param num number of interested argument + * @return string value + */ + std::string getArg(const int num) const { return m_arguments[num]; } private: - Appl() {} + /** private constructor - singleton pattern*/ + Appl(const bool command) : m_needCommand(command) {} Appl(const Appl&); - Appl& operator= (const Appl&); + Appl& operator=(const Appl&); - struct Arg { - const char* name; - const char* shortname; - const char* longname; - const char* description; - Datatype datatype; - Optiontype optiontype; - }; + /** application options */ + std::vector m_opts; + std::vector::const_iterator o_it; - size_t m_argc; + /** map option - value */ + std::map m_optvals; + std::map::iterator ov_it; + + /** given arguments */ std::vector m_argv; - std::vector m_args; - std::vector::const_iterator a_it; - - std::map m_params; - std::map::iterator p_it; - - std::string m_argTxt; - size_t m_argNum; - + /** application version string */ const char* m_version; - std::vector m_argValues; + /** true if the application need a command */ + bool m_needCommand; - bool checkArg(const std::string& name, const std::string& arg); + /** arguments (argument 0 = command) string */ + std::vector m_arguments; - void addParam(const char* name, Param param) { m_params[name] = param; } + /** + * @brief checks the passed parameter if this is a valid option. + * @param option to check + * @param value to save if paramter is a valid option + */ + bool checkOption(const std::string& option, const std::string& value); - void addParam(const char* name, const std::string arg, Datatype datatype); + /** + * @brief save the passed value to option. + * @param option name + * @param value to save + * @param datatype of given option + */ + void setOptVal(const char* option, const std::string value, DataType datatype); + /** + * @brief print application version. + */ void printVersion(); + + /** + * @brief print help page. + */ void printHelp(); + + /** + * @brief print used option settings. + */ + void printSettings(); }; #endif // LIBUTILS_APPL_H_ From 5db9c5c7741d869eb3f4d22dc96924a2e1ea6930 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 16 Nov 2014 00:33:28 +0100 Subject: [PATCH 18/38] moving 'scan' functionality from ebusctl into ebusd started. --- src/ebusctl/ebusctl.cpp | 7 ++++--- src/ebusd/baseloop.cpp | 42 ++++++++++++++++++++++++++++------------- src/ebusd/baseloop.h | 2 ++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index 8279ece1..a3bea69c 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -208,14 +208,14 @@ int main(int argc, char* argv[]) TCPSocket* socket = client->connect(A.getOptVal("server"), A.getOptVal("port")); if (socket != NULL) { - +/* if (strcasecmp(A.getArg(0).c_str(), "scan") == 0) { addManufacturer(); // send command to all slaves for (size_t i = 0; i < s.size(); i++) { // build message - std::string message("hex ms "); + std::string message("hex "); message += s[i]; message += "070400"; @@ -255,6 +255,7 @@ int main(int argc, char* argv[]) sleep(2); } } else { +*/ // build message std::string message(A.getArg(0)); for (int i = 1; i < A.numArgs(); i++) { @@ -271,7 +272,7 @@ int main(int argc, char* argv[]) data[datalen] = '\0'; std::cout << data; - } + //~ } delete socket; } else { diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index e4054867..5a6c6a0c 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -291,8 +291,22 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; + case scan: + if (cmd.size() < 1 || cmd.size() > 2) { + result << "usage: 'scan'" << std::endl + << " 'scan full'"; + break; + } + + //~ if (strcasecmp(cmd[1].c_str(), "FULL") == 0) + //~ // scan(FULL); + //~ else + //~ // scan(); + + break; + case log: - if (cmd.size () != 3 ) { + if (cmd.size() != 3 ) { result << "usage: 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl << " 'log level level' (level: error|event|trace|debug)"; break; @@ -360,18 +374,20 @@ std::string BaseLoop::decodeMessage(const std::string& data) case help: result << "commands:" << std::endl - << " get - fetch ebus data 'get class cmd (sub)'" << std::endl - << " set - set ebus values 'set class cmd value'" << std::endl - << " cyc - fetch cycle data 'cyc class cmd (sub)'" << std::endl - << " hex - send given hex value 'hex type value' (value: ZZPBSBNNDx)" << std::endl << std::endl - << " log - change log areas 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl - << " - change log level 'log level level' (level: error|event|trace|debug)" << std::endl << std::endl - << " raw - toggle log raw data" << std::endl - << " dump - toggle dump state" << std::endl << std::endl - << " reload - reload ebus configuration" << std::endl << std::endl - << " stop - stop daemon" << std::endl - << " quit - close connection" << std::endl << std::endl - << " help - print this page"; + << " get - fetch ebus data 'get class cmd (sub)'" << std::endl + << " set - set ebus values 'set class cmd value'" << std::endl + << " cyc - fetch cycle data 'cyc class cmd (sub)'" << std::endl + << " hex - send given hex value 'hex type value' (value: ZZPBSBNNDx)" << std::endl << std::endl + << " scan - scan ebus kown addresses 'scan'" << std::endl + << " - scan ebus all addresses 'scan full'" << std::endl << std::endl + << " log - change log areas 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl + << " - change log level 'log level level' (level: error|event|trace|debug)" << std::endl << std::endl + << " raw - toggle log raw data 'raw'" << std::endl + << " dump - toggle dump state 'dump'" << std::endl << std::endl + << " reload - reload ebus configuration 'reload'" << std::endl << std::endl + << " stop - stop daemon 'stop'" << std::endl + << " quit - close connection 'quit'" << std::endl << std::endl + << " help - print this page 'help"; break; default: diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index 1e59401d..1ef4426e 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -50,6 +50,7 @@ private: set, // set ebus value cyc, // fetch cycle data hex, // send hex value + scan, // scan ebus log, // logger settings raw, // toggle log raw data dump, // toggle dump state @@ -64,6 +65,7 @@ private: if (strcasecmp(item.c_str(), "SET") == 0) return set; if (strcasecmp(item.c_str(), "CYC") == 0) return cyc; if (strcasecmp(item.c_str(), "HEX") == 0) return hex; + if (strcasecmp(item.c_str(), "SCAN") == 0) return scan; if (strcasecmp(item.c_str(), "LOG") == 0) return log; if (strcasecmp(item.c_str(), "RAW") == 0) return raw; if (strcasecmp(item.c_str(), "DUMP") == 0) return dump; From e4e709ec6a7fac14fbe4832ac02178c695b38d87 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 16 Nov 2014 10:08:58 +0100 Subject: [PATCH 19/38] documentation for class WQueue and Notify added. --- src/lib/utils/Makefile.am | 12 ++++++----- src/lib/utils/notify.cpp | 42 +++++++++++++++++++++++++++++++++++++++ src/lib/utils/notify.h | 35 ++++++++++++++++++++------------ src/lib/utils/wqueue.h | 31 ++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 src/lib/utils/notify.cpp diff --git a/src/lib/utils/Makefile.am b/src/lib/utils/Makefile.am index d10f81d6..3f9640ac 100644 --- a/src/lib/utils/Makefile.am +++ b/src/lib/utils/Makefile.am @@ -4,18 +4,20 @@ AM_CXXFLAGS = -fpic \ noinst_LIBRARIES = libutils.a -libutils_a_SOURCES = wqueue.h \ - notify.h \ - appl.cpp \ +libutils_a_SOURCES = appl.cpp \ appl.h \ daemon.cpp \ daemon.h \ logger.cpp \ logger.h \ + notify.cpp \ + notify.h \ + tcpsocket.cpp \ + tcpsocket.h \ thread.cpp \ thread.h \ - tcpsocket.cpp \ - tcpsocket.h + wqueue.h + distclean-local: -rm -f Makefile.in diff --git a/src/lib/utils/notify.cpp b/src/lib/utils/notify.cpp new file mode 100644 index 00000000..d9416162 --- /dev/null +++ b/src/lib/utils/notify.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) Roland Jax 2012-2014 + * + * This file is part of ebusd. + * + * ebusd is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ebusd is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ebusd. If not, see http://www.gnu.org/licenses/. + */ + +#include "notify.h" + +Notify::Notify() +{ + int pipefd[2]; + int ret = pipe(pipefd); + + if (ret == 0) { + m_recvfd = pipefd[0]; + m_sendfd = pipefd[1]; + + fcntl(m_sendfd, F_SETFL, O_NONBLOCK); + } + +} + +Notify::~Notify() +{ + close(m_sendfd); + close(m_recvfd); +} + + diff --git a/src/lib/utils/notify.h b/src/lib/utils/notify.h index accc6339..05b1491b 100644 --- a/src/lib/utils/notify.h +++ b/src/lib/utils/notify.h @@ -23,30 +23,39 @@ #include #include +/** + * @brief class to notify other thread per pipe. + */ class Notify { public: - Notify() - { - int pipefd[2]; - int ret = pipe(pipefd); + /** + * @brief constructs a new instance and do notifying. + */ + Notify(); - if (ret == 0) { - m_recvfd = pipefd[0]; - m_sendfd = pipefd[1]; + /** + * @brief destructor. + */ + ~Notify(); - fcntl(m_sendfd, F_SETFL, O_NONBLOCK); - } + /** + * @brief file descriptor to watch for notify event. + * @return the notification value + */ + int notifyFD() { return m_recvfd; } - } - virtual ~Notify() { close(m_sendfd); close(m_recvfd); } - - int notifyFD() const { return m_recvfd; } + /** + * @brief write notify event to file descriptor. + * @return result of writing notification + */ int notify() const { return write(m_sendfd,"1",1); } private: + /** file descriptor to watch */ int m_recvfd; + /** file descriptor to notify */ int m_sendfd; }; diff --git a/src/lib/utils/wqueue.h b/src/lib/utils/wqueue.h index 99c4fc48..cf696370 100644 --- a/src/lib/utils/wqueue.h +++ b/src/lib/utils/wqueue.h @@ -23,22 +23,36 @@ #include #include -template class WQueue +/** + * @brief queue class template for all kinds data types with exclusiv lock. + */ +template +class WQueue { public: + /** + * @brief constructs a new instance. + */ WQueue() { pthread_mutex_init(&m_mutex, NULL); pthread_cond_init(&m_cond, NULL); } + /** + * @brief destructor. + */ ~WQueue() { pthread_mutex_destroy(&m_mutex); pthread_cond_destroy(&m_cond); } + /** + * @brief add a new item to the end of queue. + * @param item + */ void add(T item) { pthread_mutex_lock(&m_mutex); @@ -49,6 +63,10 @@ public: pthread_mutex_unlock(&m_mutex); } + /** + * @brief remove the first item from queue. + * @return the item + */ T remove() { pthread_mutex_lock(&m_mutex); @@ -64,6 +82,10 @@ public: return item; } + /** + * @brief return the first item from queue without remove. + * @return the item + */ T next() { pthread_mutex_lock(&m_mutex); @@ -78,6 +100,10 @@ public: return item; } + /** + * @brief the number of entries inside queue. + * @return the size + */ int size() { pthread_mutex_lock(&m_mutex); @@ -90,8 +116,11 @@ public: } private: + /** the queue itself */ std::list m_queue; + /** mutex variable for exclusive lock */ pthread_mutex_t m_mutex; + /** condition variable for exclusive lock */ pthread_cond_t m_cond; }; From ada6a066fe2f0e743401f8436932e8d17b00e46c Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 16 Nov 2014 14:25:23 +0100 Subject: [PATCH 20/38] documentation for class Daemon added. --- src/lib/utils/daemon.cpp | 4 ++-- src/lib/utils/daemon.h | 46 ++++++++++++++++++++++++++++++++++------ src/lib/utils/logger.h | 2 +- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/lib/utils/daemon.cpp b/src/lib/utils/daemon.cpp index 18c35a3e..36ecd155 100644 --- a/src/lib/utils/daemon.cpp +++ b/src/lib/utils/daemon.cpp @@ -32,10 +32,10 @@ Daemon& Daemon::Instance() return instance; } -void Daemon::run(const char* file) +void Daemon::run(const char* pidfile) { m_status = false; - m_pidfile = file; + m_pidfile = pidfile; m_pidfd = 0; pid_t pid; diff --git a/src/lib/utils/daemon.h b/src/lib/utils/daemon.h index ac01d42b..4ce8780a 100644 --- a/src/lib/utils/daemon.h +++ b/src/lib/utils/daemon.h @@ -20,27 +20,59 @@ #ifndef LIBUTILS_DAEMON_H_ #define LIBUTILS_DAEMON_H_ +/** + * @brief class to daemonize a process. + */ class Daemon { public: + /** + * @brief create an daemon instance and return the reference. + * @return the reference to instance. + */ static Daemon& Instance(); - ~Daemon() {} - void run(const char* file); + /** + * @brief daemonize act process. + * @param pidfile the name of the pid file. + */ + void run(const char* pidfile); + + /** + * @brief stop daemon and delete the pid file. + */ void stop() { pidfile_close(); } + + /** + * @brief show actual status if daemonize. + * @return true if process is a daemon. + */ bool status() { return m_status; } private: - bool m_status; - const char* m_pidfile; - int m_pidfd; - + /** private constructor - singleton pattern */ Daemon() {} Daemon(const Daemon&); - Daemon& operator= (const Daemon&); + Daemon& operator=(const Daemon&); + /** status of process; true if we are a daemon */ + bool m_status; + /** name of the pid file*/ + const char* m_pidfile; + /** file descriptor of the pid file */ + int m_pidfd; + + /** + * @brief creates a pid file for process. + * @return true if success. + */ bool pidfile_open(); + + /** + * @brief close and delete the pid file. + * @return true if success. + */ bool pidfile_close(); }; diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index f8ac4b39..adae3738 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -155,7 +155,7 @@ public: private: LogInstance() {} LogInstance(const LogInstance&); - LogInstance& operator= (const LogInstance&); + LogInstance& operator=(const LogInstance&); typedef std::vector sink_t; typedef std::vector::iterator sinkCI_t; From e5452282adbfb9ddc1f93df0d25fba5a7c44cf22 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 16 Nov 2014 14:49:14 +0100 Subject: [PATCH 21/38] documentation for class Thread added. --- src/lib/utils/appl.h | 2 +- src/lib/utils/thread.cpp | 6 +++++- src/lib/utils/thread.h | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 35d9657d..80894c9f 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -76,7 +76,7 @@ class Appl public: /** - * @brief create an Appl instance and return the reference. + * @brief create an appl instance and return the reference. * @param command is true if an command is needed. * @return the reference to instance. */ diff --git a/src/lib/utils/thread.cpp b/src/lib/utils/thread.cpp index fa4ca38f..4ea84136 100644 --- a/src/lib/utils/thread.cpp +++ b/src/lib/utils/thread.cpp @@ -23,9 +23,13 @@ #include "thread.h" +/** + * @brief static function which will be called on thread startup. + * @return void pointer. + */ static void* runThread(void* arg) { - return ((Thread *)arg)->run(); + return ((Thread*)arg)->run(); } Thread::~Thread() diff --git a/src/lib/utils/thread.h b/src/lib/utils/thread.h index 03ecfd0d..4d4e15dc 100644 --- a/src/lib/utils/thread.h +++ b/src/lib/utils/thread.h @@ -22,23 +22,60 @@ #include +/** + * @brief wrapper class for pthread. + */ class Thread { public: + /** + * @brief constructor. + */ Thread() : m_threadid(0), m_running(false), m_detached(false) {} + + /** + * @brief virtual destructor. + */ virtual ~Thread(); + /** + * @brief create the thread and set name for process list. + * @param name the thread name which show in process list. + * @return value of thread creating. + */ int start(const char* name); + + /** + * @brief join the thread. + * @return value of thread joining. + */ int join(); + + /** + * @brief detach the thread. + * @return value of thread detaching. + */ int detach(); + + /** + * @brief return the thread id. + * @return own thread id. + */ pthread_t self() {return m_threadid; } + /** + * @brief virtul function which must be implented in derived class. + * @return void pointer. + */ virtual void* run() = 0; private: + /** own thread id */ pthread_t m_threadid; + /** true if thread is running */ bool m_running; + /** true if thread is detached */ bool m_detached; }; From b6cae7884ada8101f137dd4ee30ca3075df99e7a Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Sun, 16 Nov 2014 21:50:35 +0100 Subject: [PATCH 22/38] moving 'scan' functionality from ebusctl into ebusd continued. --- src/ebusd/baseloop.cpp | 35 +++++++++----- src/ebusd/ebusloop.cpp | 91 ++++++++++++++++++++++++++++++++++--- src/ebusd/ebusloop.h | 9 ++++ src/lib/ebus/buscommand.cpp | 4 +- src/lib/ebus/buscommand.h | 8 ++-- src/lib/ebus/commands.cpp | 47 ++++++++++++------- src/lib/ebus/commands.h | 19 +++++--- 7 files changed, 166 insertions(+), 47 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 5a6c6a0c..e799e663 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -32,7 +32,7 @@ BaseLoop::BaseLoop() L.log(bas, trace, "ebus configuration dir: %s", A.getOptVal("ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); - L.log(bas, event, " polling DB: %d ", m_commands->sizePolDB()); + L.log(bas, event, " polling DB: %d ", m_commands->sizePollDB()); // create ebusloop m_ebusloop = new EBusLoop(m_commands); @@ -131,7 +131,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) // polling data if (strcasecmp(m_commands->getCmdType(index).c_str(), "P") == 0) { // get polldata - polldata = m_commands->getPolData(index); + polldata = m_commands->getPollData(index); if (polldata != "") { // decode data Command* command = new Command(index, (*m_commands)[index], polldata); @@ -151,7 +151,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) ebusCommand += m_commands->getEbusCommand(index); std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, false); + BusCommand* busCommand = new BusCommand(ebusCommand, false, false); L.log(bas, trace, " msg: %s", ebusCommand.c_str()); // send busCommand m_ebusloop->addBusCommand(busCommand); @@ -204,7 +204,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, false); + BusCommand* busCommand = new BusCommand(ebusCommand, false, false); L.log(bas, event, " msg: %s", ebusCommand.c_str()); // send busCommand m_ebusloop->addBusCommand(busCommand); @@ -273,7 +273,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) ebusCommand += cmd[1]; std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, false); + BusCommand* busCommand = new BusCommand(ebusCommand, false, false); L.log(bas, trace, " msg: %s", ebusCommand.c_str()); // send busCommand m_ebusloop->addBusCommand(busCommand); @@ -294,15 +294,25 @@ std::string BaseLoop::decodeMessage(const std::string& data) case scan: if (cmd.size() < 1 || cmd.size() > 2) { result << "usage: 'scan'" << std::endl - << " 'scan full'"; + << " 'scan full'" << std::endl + << " 'scan result'"; break; } - //~ if (strcasecmp(cmd[1].c_str(), "FULL") == 0) - //~ // scan(FULL); - //~ else - //~ // scan(); + if (strcasecmp(cmd[1].c_str(), "RESULT") == 0) { + //~ m_ebusloop->scan(true); + result << "TODO show result"; + break; + } + if (strcasecmp(cmd[1].c_str(), "FULL") == 0) { + m_ebusloop->scan(true); + result << "done"; + break; + } + + m_ebusloop->scan(); + result << "done"; break; case log: @@ -362,7 +372,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) L.log(bas, trace, "ebus configuration dir: %s", A.getOptVal("ebusconfdir")); L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB()); L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); - L.log(bas, event, " polling DB: %d ", m_commands->sizePolDB()); + L.log(bas, event, " polling DB: %d ", m_commands->sizePollDB()); delete m_commands; m_commands = commands; @@ -379,7 +389,8 @@ std::string BaseLoop::decodeMessage(const std::string& data) << " cyc - fetch cycle data 'cyc class cmd (sub)'" << std::endl << " hex - send given hex value 'hex type value' (value: ZZPBSBNNDx)" << std::endl << std::endl << " scan - scan ebus kown addresses 'scan'" << std::endl - << " - scan ebus all addresses 'scan full'" << std::endl << std::endl + << " - scan ebus all addresses 'scan full'" << std::endl + << " - scan show results 'scan result'" << std::endl << std::endl << " log - change log areas 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl << " - change log level 'log level level' (level: error|event|trace|debug)" << std::endl << std::endl << " raw - toggle log raw data 'raw'" << std::endl diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/ebusloop.cpp index 7d248cb8..4af779c4 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/ebusloop.cpp @@ -25,7 +25,8 @@ extern LogInstance& L; extern Appl& A; EBusLoop::EBusLoop(Commands* commands) - : m_commands(commands), m_stop(false), m_lockCounter(0), m_priorRetry(false) + : m_commands(commands), m_stop(false), m_lockCounter(0), + m_priorRetry(false), m_scan(false), m_scanFull(false) { m_port = new Port(A.getOptVal("device"), A.getOptVal("nodevicecheck")); m_port->open(); @@ -72,15 +73,19 @@ void* EBusLoop::run() if (m_port->isOpen() == true) { ssize_t numBytes; - // add poll command - if (m_commands->sizePolDB() > 0) { + // add poll or scan command + if (m_commands->sizePollDB() > 0 || m_scan == true) { // check polling delta time(&pollEnd); pollDelta = difftime(pollEnd, pollStart); // add new polling command to send if (pollDelta >= m_pollInterval) { - addPollCommand(); + if (m_scan == true) + addScanCommand(); + else + addPollCommand(); + time(&pollStart); } } @@ -126,7 +131,10 @@ void* EBusLoop::run() sendRetries = 0; if (busCommand->isPoll() == true) { - m_commands->storePolData(busCommand->getMessageStr().c_str()); // TODO use getResult() + if (busCommand->isScan() == true) + m_commands->storeScanData(busCommand->getMessageStr().c_str()); + else + m_commands->storePollData(busCommand->getMessageStr().c_str()); // TODO use getResult() delete busCommand; } else @@ -255,11 +263,15 @@ void EBusLoop::analyseCycData() tmp += (*m_commands)[index][2]; L.log(bus, event, " cycle [%4d] %s", index, tmp.c_str()); } + + // collect Slave address + if (index != -3) + collectSlave(); } void EBusLoop::addPollCommand() { - int index = m_commands->nextPolCommand(); + int index = m_commands->nextPollCommand(); if (index < 0) { L.log(bus, error, "polling index out of range"); } @@ -275,7 +287,7 @@ void EBusLoop::addPollCommand() ebusCommand += m_commands->getEbusCommand(index); std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, true); + BusCommand* busCommand = new BusCommand(ebusCommand, true, false); L.log(bus, trace, " msg: %s", ebusCommand.c_str()); addBusCommand(busCommand); @@ -549,3 +561,68 @@ int EBusLoop::recvSlaveData(SymbolString& result) return RESULT_OK; } +void EBusLoop::collectSlave() +{ + std::vector::iterator it; + + for (int i = 0; i < 2; i++) { + bool found = false; + unsigned char mm = m_sstr[i]; + + if (i == 0) { + if (mm == 0xff) + mm = 0x04; + else + mm += 0x05; + } + + for (it = m_slave.begin(); it != m_slave.end(); it++) + if ((*it) == mm) + found = true; + + if (found == false && isMaster(mm) == false && mm != BROADCAST) { + m_slave.push_back(mm); + L.log(bus, event, " new slave: %d %02x", m_slave.size(), m_slave.back()); + } + } +} + +void EBusLoop::addScanCommand() +{ + //~ static int index = 0; +//~ + //~ if (m_scanFull == true) { +//~ + //~ } + //~ else { + //~ m_slave[index] + //~ } + // TODO create next scan Command + // loop over + // scan -> from collected master and slave data + // full -> loop 0x00 -> 0xff (without master and broadcast) + // after loop reset state -> m_scan == false; m_scanFull == false; + + + //~ int index = m_commands->nextPollCommand(); + //~ if (index < 0) { + //~ L.log(bus, error, "polling index out of range"); + //~ } + //~ else { + //~ // TODO: implement as methode from class commands? + //~ std::string tmp; + //~ tmp += (*m_commands)[index][1]; + //~ tmp += " "; + //~ tmp += (*m_commands)[index][2]; + //~ L.log(bus, event, " polling [%4d] %s", index, tmp.c_str()); +//~ + //~ std::string ebusCommand(A.getOptVal("address")); + //~ ebusCommand += m_commands->getEbusCommand(index); + //~ std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); +//~ + //~ BusCommand* busCommand = new BusCommand(ebusCommand, true, true); + //~ L.log(bus, trace, " msg: %s", ebusCommand.c_str()); +//~ + //~ addBusCommand(busCommand); + //~ } +} diff --git a/src/ebusd/ebusloop.h b/src/ebusd/ebusloop.h index 3d39f099..d600a0f4 100644 --- a/src/ebusd/ebusloop.h +++ b/src/ebusd/ebusloop.h @@ -50,6 +50,8 @@ public: void reload(Commands* commands) { m_commands = commands; } + void scan(const bool full=false) { m_scan = true; m_scanFull = full; } + private: Commands* m_commands; Port* m_port; @@ -73,6 +75,11 @@ private: int m_lockRetries; long m_acquireTime; + std::vector m_slave; + + bool m_scan; + bool m_scanFull; + unsigned char fetchByte(); void collectCycData(const int numRecv); void analyseCycData(); @@ -82,6 +89,8 @@ private: int sendByte(const unsigned char sendByte); int recvSlaveAck(unsigned char& recvByte); int recvSlaveData(SymbolString& result); + void collectSlave(); + void addScanCommand(); }; diff --git a/src/lib/ebus/buscommand.cpp b/src/lib/ebus/buscommand.cpp index ac822f63..61dcd702 100644 --- a/src/lib/ebus/buscommand.cpp +++ b/src/lib/ebus/buscommand.cpp @@ -22,8 +22,8 @@ namespace libebus { -BusCommand::BusCommand(const std::string commandStr, const bool isPoll) - : m_isPoll(isPoll), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) +BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool scan) + : m_poll(poll), m_scan(scan), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) { unsigned char dstAddress = m_command[1]; diff --git a/src/lib/ebus/buscommand.h b/src/lib/ebus/buscommand.h index bf06e37d..8116d00c 100644 --- a/src/lib/ebus/buscommand.h +++ b/src/lib/ebus/buscommand.h @@ -34,11 +34,12 @@ class BusCommand { public: - BusCommand(const std::string command, const bool isPoll); + BusCommand(const std::string command, const bool poll, const bool scan); ~BusCommand(); CommandType getType() const { return m_type; } - bool isPoll() const { return m_isPoll; } + bool isPoll() const { return m_poll; } + bool isScan() const { return m_scan; } SymbolString getCommand() const { return m_command; } SymbolString getResult() const { return m_result; } @@ -55,7 +56,8 @@ public: private: CommandType m_type; - bool m_isPoll; + bool m_poll; + bool m_scan; SymbolString m_command; SymbolString m_result; int m_resultCode; diff --git a/src/lib/ebus/commands.cpp b/src/lib/ebus/commands.cpp index 855d6e73..ed113de3 100644 --- a/src/lib/ebus/commands.cpp +++ b/src/lib/ebus/commands.cpp @@ -31,10 +31,10 @@ namespace libebus Commands::~Commands() { - for (mapCI_t iter = m_polDB.begin(); iter != m_polDB.end(); ++iter) + for (mapCI_t iter = m_pollDB.begin(); iter != m_pollDB.end(); ++iter) delete iter->second; - m_polDB.clear(); + m_pollDB.clear(); for (mapCI_t iter = m_cycDB.begin(); iter != m_cycDB.end(); ++iter) delete iter->second; @@ -55,7 +55,7 @@ void Commands::addCommand(const cmd_t& command) if (strcasecmp(command[0].c_str(),"P") == 0) { Command* cmd = new Command(m_cmdDB.size()-1, command); - m_polDB.insert(pair_t(m_cmdDB.size()-1, cmd)); + m_pollDB.insert(pair_t(m_cmdDB.size()-1, cmd)); } } @@ -188,33 +188,33 @@ std::string Commands::getCycData(int index) const return ""; } -int Commands::nextPolCommand() +int Commands::nextPollCommand() { size_t index = 0; - m_polIndex++; + m_pollIndex++; - if (m_polIndex == m_polDB.size()) - m_polIndex = 0; + if (m_pollIndex == m_pollDB.size()) + m_pollIndex = 0; - mapCI_t iter = m_polDB.begin(); + mapCI_t iter = m_pollDB.begin(); - for (; iter != m_polDB.end(); iter++, index++) - if (index == m_polIndex) + for (; iter != m_pollDB.end(); iter++, index++) + if (index == m_pollIndex) return iter->first; return -1; } -void Commands::storePolData(const std::string& data) const +void Commands::storePollData(const std::string& data) const { // prepare string for searching command std::string search(data.substr(2, 8 + strtol(data.substr(8,2).c_str(), NULL, 16) * 2)); - mapCI_t iter = m_polDB.begin(); + mapCI_t iter = m_pollDB.begin(); // walk through commands - for (; iter != m_polDB.end(); iter++) { + for (; iter != m_pollDB.end(); iter++) { std::string command = getEbusCommand(iter->first); @@ -228,15 +228,30 @@ void Commands::storePolData(const std::string& data) const } } -std::string Commands::getPolData(int index) const +std::string Commands::getPollData(const int index) const { - mapCI_t iter = m_polDB.find(index); - if (iter != m_polDB.end()) + mapCI_t iter = m_pollDB.find(index); + if (iter != m_pollDB.end()) return iter->second->getData(); else return ""; } +void Commands::storeScanData(const std::string& data) +{ + std::vector::const_iterator iter = m_scanDB.begin(); + bool found = false; + + // walk through scan data + for (; iter != m_scanDB.end(); iter++) + if (data == (*iter)) + found = true; + + if (found == false) + m_scanDB.push_back(data); +} + + void Commands::printCommand(const cmd_t& command) const { if (command.size() == 0) diff --git a/src/lib/ebus/commands.h b/src/lib/ebus/commands.h index c43624b1..cf97ae39 100644 --- a/src/lib/ebus/commands.h +++ b/src/lib/ebus/commands.h @@ -40,7 +40,7 @@ class Commands { public: - Commands() : m_polIndex(-1) {} + Commands() : m_pollIndex(-1) {} ~Commands(); void addCommand(const cmd_t& command); @@ -48,7 +48,8 @@ public: std::size_t sizeCmdDB() const { return m_cmdDB.size(); } std::size_t sizeCycDB() const { return m_cycDB.size(); } - std::size_t sizePolDB() const { return m_polDB.size(); } + std::size_t sizePollDB() const { return m_pollDB.size(); } + std::size_t sizeScanDB() const { return m_scanDB.size(); } cmd_t const& operator[](const std::size_t& index) const { return m_cmdDB[index]; } @@ -60,15 +61,19 @@ public: int storeCycData(const std::string& data) const; std::string getCycData(int index) const; - int nextPolCommand(); - void storePolData(const std::string& data) const; - std::string getPolData(int index) const; + int nextPollCommand(); + void storePollData(const std::string& data) const; + std::string getPollData(const int index) const; + + void storeScanData(const std::string& data); + std::string getScanData(const int index) const { return m_scanDB[index]; } private: cmdDB_t m_cmdDB; map_t m_cycDB; - map_t m_polDB; - size_t m_polIndex; + map_t m_pollDB; + size_t m_pollIndex; + std::vector m_scanDB; void printCommand(const cmd_t& command) const; From bfab16ce5d4956a8d443aa25628b7c0462e3867d Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Mon, 17 Nov 2014 16:59:45 +0100 Subject: [PATCH 23/38] EBusLoop::addScanCommand implemented. --- src/ebusd/ebusloop.cpp | 74 ++++++++++++++++++++++-------------------- src/ebusd/ebusloop.h | 3 +- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/ebusloop.cpp index 4af779c4..863d9539 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/ebusloop.cpp @@ -20,13 +20,14 @@ #include "ebusloop.h" #include "logger.h" #include "appl.h" +#include extern LogInstance& L; extern Appl& A; EBusLoop::EBusLoop(Commands* commands) : m_commands(commands), m_stop(false), m_lockCounter(0), - m_priorRetry(false), m_scan(false), m_scanFull(false) + m_priorRetry(false), m_scan(false), m_scanFull(false), m_scanIndex(0) { m_port = new Port(A.getOptVal("device"), A.getOptVal("nodevicecheck")); m_port->open(); @@ -589,40 +590,43 @@ void EBusLoop::collectSlave() void EBusLoop::addScanCommand() { - //~ static int index = 0; -//~ - //~ if (m_scanFull == true) { -//~ - //~ } - //~ else { - //~ m_slave[index] - //~ } - // TODO create next scan Command - // loop over - // scan -> from collected master and slave data - // full -> loop 0x00 -> 0xff (without master and broadcast) - // after loop reset state -> m_scan == false; m_scanFull == false; + std::string ebusCommand(A.getOptVal("address")); + std::stringstream sstr; + + if (m_scanFull == true) { + for (; m_scanIndex <= 0xff; m_scanIndex++) { + if (isMaster(m_scanIndex) == false && m_scanIndex != SYN + && m_scanIndex != ESC && m_scanIndex != BROADCAST) { + sstr << std::nouppercase << std::setw(2) << std::setfill('0') + << std::hex << m_scanIndex; + break; + } + } + + if (m_scanIndex == 0xff) + m_scan = false; + } + else { + sstr << std::nouppercase << std::setw(2) << std::setfill('0') + << std::hex << static_cast(m_slave[m_scanIndex]); + + if (m_scanIndex+1 >= m_slave.size()) + m_scan = false; + } + + m_scanIndex++; + + ebusCommand += sstr.str(); + ebusCommand += "070400"; + std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + + L.log(bus, event, " scanning address %s", sstr.str().c_str()); + + + BusCommand* busCommand = new BusCommand(ebusCommand, true, true); + L.log(bus, trace, " msg: %s", ebusCommand.c_str()); + + addBusCommand(busCommand); - //~ int index = m_commands->nextPollCommand(); - //~ if (index < 0) { - //~ L.log(bus, error, "polling index out of range"); - //~ } - //~ else { - //~ // TODO: implement as methode from class commands? - //~ std::string tmp; - //~ tmp += (*m_commands)[index][1]; - //~ tmp += " "; - //~ tmp += (*m_commands)[index][2]; - //~ L.log(bus, event, " polling [%4d] %s", index, tmp.c_str()); -//~ - //~ std::string ebusCommand(A.getOptVal("address")); - //~ ebusCommand += m_commands->getEbusCommand(index); - //~ std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); -//~ - //~ BusCommand* busCommand = new BusCommand(ebusCommand, true, true); - //~ L.log(bus, trace, " msg: %s", ebusCommand.c_str()); -//~ - //~ addBusCommand(busCommand); - //~ } } diff --git a/src/ebusd/ebusloop.h b/src/ebusd/ebusloop.h index d600a0f4..da42cb49 100644 --- a/src/ebusd/ebusloop.h +++ b/src/ebusd/ebusloop.h @@ -50,7 +50,7 @@ public: void reload(Commands* commands) { m_commands = commands; } - void scan(const bool full=false) { m_scan = true; m_scanFull = full; } + void scan(const bool full=false) { m_scan = true; m_scanFull = full; m_scanIndex = 0; } private: Commands* m_commands; @@ -79,6 +79,7 @@ private: bool m_scan; bool m_scanFull; + size_t m_scanIndex; unsigned char fetchByte(); void collectCycData(const int numRecv); From 3f7f60cc68ad5ab49accc731885f5591b129643d Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Mon, 17 Nov 2014 20:27:01 +0100 Subject: [PATCH 24/38] implementation of 'scan' functionality continued. --- src/ebusctl/ebusctl.cpp | 197 +++++----------------------------------- src/ebusd/baseloop.cpp | 2 +- src/ebusd/ebusd.cpp | 2 - src/ebusd/ebusloop.cpp | 77 ++++++++-------- src/lib/utils/appl.cpp | 2 +- 5 files changed, 65 insertions(+), 215 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index a3bea69c..a1c15ca3 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -26,11 +26,8 @@ #include "decode.h" #include "tcpsocket.h" #include -#include #include #include -#include -#include #include using namespace libebus; @@ -41,12 +38,9 @@ void define_args() { A.setVersion("ebusctl is part of """PACKAGE_STRING""); - A.addText(" local commands:\n" - " 'scan' scans the bus and identifies the participants\n\n" - " 'feed' sends a dump file to a local virtual serial device\n" - " (hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)\n\n" - " remote commands:\n" - " send 'help' to server\n\n" + A.addText(" 'help' show server commands\n\n" + " 'feed' sends a dump file to a local serial device (pts)\n" + " (hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)\n\n" " Options:\n"); A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, @@ -65,102 +59,6 @@ void define_args() "port (8888)\n"); } - -templateT * end(T (&ra)[N]) { return ra + N; } - -const char *sinit[] = {"02", "04", "05", "06", "08", "09", "0A", "0B", "0C", - "0E", "12", "14", "15", "16", "18", "19", "1A", "1B", - "1C", "1D", "1E", "20", "21", "22", "23", "24", "25", - "26", "27", "28", "29", "2A", "2B", "2C", "2D", "2E", - "2F", "32", "34", "35", "36", "38", "39", "3A", "3B", - "3C", "3D", "3E", "40", "41", "42", "43", "44", "45", - "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", - "4F", "50", "51", "52", "53", "54", "55", "56", "57", - "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", - "61", "62", "63", "64", "65", "66", "67", "68", "69", - "6A", "6B", "6C", "6D", "6E", "6F", "72", "74", "75", - "76", "78", "79", "7A", "7B", "7C", "7D", "7E", "80", - "81", "82", "83", "84", "85", "86", "87", "88", "89", - "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", - "93", "94", "95", "96", "97", "98", "99", "9A", "9B", - "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", - "A5", "A6", "A7", "A8", "AB", "AC", "AD", "AE", "AF", - "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", - "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", - "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", - "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", - "D4", "D5", "D6", "D7", "D8", "D9", "DA", "DB", "DC", - "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", - "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE", - "EF", "F2", "F4", "F5", "F6", "F8", "F9", "FA", "FB", - "FC", "FD"}; - -static std::vector s(sinit, end(sinit)); - -static std::map manufacturer; - -static void addManufacturer() -{ - manufacturer.insert(std::make_pair("06", "Karl Dungs GmbH")); - manufacturer.insert(std::make_pair("0f", "FH Braunschweig/Wolfenbüttel")); - manufacturer.insert(std::make_pair("10", "TEM AG für Elektronik Intertem Vertriebs AG")); - manufacturer.insert(std::make_pair("11", "Lamberti Elektronik")); - manufacturer.insert(std::make_pair("14", "CEB Compagnie Européenne de Brûleurs S.A.")); - manufacturer.insert(std::make_pair("15", "Landis & Staefa")); - manufacturer.insert(std::make_pair("16", "FERRO Wärmetechnik GmbH & Co.KG")); - manufacturer.insert(std::make_pair("17", "MONDIAL electronic Ges.mbH")); - manufacturer.insert(std::make_pair("18", "Wikon Kommunikationstechnik GmbH")); - manufacturer.insert(std::make_pair("19", "Wolf GmbH")); - manufacturer.insert(std::make_pair("20", "RAWE Electronic GmbH")); - manufacturer.insert(std::make_pair("30", "Satronic AG")); - manufacturer.insert(std::make_pair("40", "ENCON Electronics")); - manufacturer.insert(std::make_pair("50", "G. Kromschröder AG")); - manufacturer.insert(std::make_pair("60", "Eberle Controls GmbH")); - manufacturer.insert(std::make_pair("65", "EBV Elektronikbau")); - manufacturer.insert(std::make_pair("75", "Grässlin GmbH & Co.KG")); - manufacturer.insert(std::make_pair("85", "Motoren und Ventilatoren Landshut GmbH")); - manufacturer.insert(std::make_pair("95", "SIG Berger Lahr GmbH & Co KG")); - manufacturer.insert(std::make_pair("c0", "Toby AG")); - manufacturer.insert(std::make_pair("c5", "Max Weishaupt GmbH")); - manufacturer.insert(std::make_pair("a5", "Theben Zeitschaltautomatik")); - manufacturer.insert(std::make_pair("a7", "Thermowatt s.p.a.")); - manufacturer.insert(std::make_pair("b5", "Joh. Vaillant GmbH & Co.")); -} - -void scanVaillant(TCPSocket* socket, const std::string address) -{ - Decode* help = NULL; - std::ostringstream sstr; - - for (int i = 24; i < 28; i++) { - // build message - std::string message("hex ms "); - message += address; - message += "b50901"; - - std::stringstream ss; - ss << i; - message += ss.str(); - - socket->send(message.c_str(), message.size()); - - char data[256]; - size_t datalen; - - datalen = socket->recv(data, sizeof(data)-1); - data[datalen] = '\0'; - - std::string item(data); - - help = new DecodeSTR(item.substr(18,18)); - sstr << help->decode(); - delete help; - } - - std::cout << " s/n: '" << sstr.str().substr(1,28) << "' item: '" << sstr.str().substr(7,10) << "'"; -} - - int main(int argc, char* argv[]) { // define arguments and application variables @@ -191,100 +89,49 @@ int main(int argc, char* argv[]) } file.close(); - } else { - std::cout << "error opening file " << A.getOptVal("file") << std::endl; } + else + std::cout << "error opening file " << A.getOptVal("file") << std::endl; port.close(); if(port.isOpen() == false) std::cout << "closePort successful." << std::endl; - } else { + } else std::cout << "error opening device " << A.getOptVal("device") << std::endl; - } - - } else { + } + else { TCPClient* client = new TCPClient(); TCPSocket* socket = client->connect(A.getOptVal("server"), A.getOptVal("port")); if (socket != NULL) { -/* - if (strcasecmp(A.getArg(0).c_str(), "scan") == 0) { - addManufacturer(); + // build message + std::string message(A.getArg(0)); + for (int i = 1; i < A.numArgs(); i++) { + message += " "; + message += A.getArg(i); + } - // send command to all slaves - for (size_t i = 0; i < s.size(); i++) { - // build message - std::string message("hex "); - message += s[i]; - message += "070400"; + socket->send(message.c_str(), message.size()); - socket->send(message.c_str(), message.size()); + char data[1024]; + size_t datalen; - char data[1024]; - size_t datalen; + datalen = socket->recv(data, sizeof(data)-1); + data[datalen] = '\0'; - datalen = socket->recv(data, sizeof(data)-1); - data[datalen] = '\0'; - - // decode answer - if (strncmp(&data[0], "-", 1) != 0) { - std::string item(data); - - std::ostringstream ident; - Decode* help = NULL; - - help = new DecodeSTR(item.substr(18,10)); - ident << help->decode(); - delete help; - - std::cout << s[i] << ": '" << manufacturer.find(item.substr(16,2))->second - << "' ident: '" << std::setw(5) << std::setfill(' ') << ident.str() - << "' sw: '" << item.substr(28,2) - << "." << item.substr(30,2) - << "' hw: '" << item.substr(32,2) - << "." << item.substr(34,2) - << "'"; - - if (item.substr(16,2) == "b5") - scanVaillant(socket, s[i]); - - std::cout << std::endl; - } - - sleep(2); - } - } else { -*/ - // build message - std::string message(A.getArg(0)); - for (int i = 1; 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'; - - std::cout << data; - //~ } + std::cout << data; delete socket; - } else { + } + else { std::cout << "error connecting to " << A.getOptVal("server") << ":" << A.getOptVal("port") << std::endl; } delete client; - } return 0; - } diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index e799e663..2e778124 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -322,7 +322,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - // ToDo: check for possible areas and level + // TODO: check for possible areas and level if (strcasecmp(cmd[1].c_str(), "AREAS") == 0) { L.getSink(0)->setAreas(calcAreas(cmd[2])); result << "done"; diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 5ab43aaf..7020a50e 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -41,8 +41,6 @@ void define_args() A.setVersion(""PACKAGE_STRING""); - A.addText("test"); - A.addOption("address", "a", OptVal("FF"), dt_string, ot_mandatory, "\tebus device address (FF)"); diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/ebusloop.cpp index 863d9539..f26174da 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/ebusloop.cpp @@ -244,30 +244,36 @@ void EBusLoop::collectCycData(const int numRecv) void EBusLoop::analyseCycData() { - L.log(bus, trace, "%s", m_sstr.getDataStr().c_str()); + static bool skipfirst = false; - int index = m_commands->storeCycData(m_sstr.getDataStr()); + if (skipfirst == true) { + L.log(bus, trace, "%s", m_sstr.getDataStr().c_str()); - if (index == -1) { - L.log(bus, debug, " command not found"); - } - else if (index == -2) { - L.log(bus, debug, " no commands defined"); - } - else if (index == -3) { - L.log(bus, debug, " search skipped - string too short"); - } - else { - std::string tmp; - tmp += (*m_commands)[index][1]; - tmp += " "; - tmp += (*m_commands)[index][2]; - L.log(bus, event, " cycle [%4d] %s", index, tmp.c_str()); - } + int index = m_commands->storeCycData(m_sstr.getDataStr()); - // collect Slave address - if (index != -3) - collectSlave(); + if (index == -1) { + L.log(bus, debug, " command not found"); + } + else if (index == -2) { + L.log(bus, debug, " no commands defined"); + } + else if (index == -3) { + L.log(bus, debug, " search skipped - string too short"); + } + else { + std::string tmp; + tmp += (*m_commands)[index][1]; + tmp += " "; + tmp += (*m_commands)[index][2]; + L.log(bus, event, " cycle [%4d] %s", index, tmp.c_str()); + } + + // collect Slave address + if (index != -3) + collectSlave(); + } + else + skipfirst = true; } void EBusLoop::addPollCommand() @@ -571,7 +577,7 @@ void EBusLoop::collectSlave() unsigned char mm = m_sstr[i]; if (i == 0) { - if (mm == 0xff) + if (mm == 0xFF) mm = 0x04; else mm += 0x05; @@ -594,7 +600,7 @@ void EBusLoop::addScanCommand() std::stringstream sstr; if (m_scanFull == true) { - for (; m_scanIndex <= 0xff; m_scanIndex++) { + for (; m_scanIndex <= 0xFF; m_scanIndex++) { if (isMaster(m_scanIndex) == false && m_scanIndex != SYN && m_scanIndex != ESC && m_scanIndex != BROADCAST) { sstr << std::nouppercase << std::setw(2) << std::setfill('0') @@ -602,9 +608,6 @@ void EBusLoop::addScanCommand() break; } } - - if (m_scanIndex == 0xff) - m_scan = false; } else { sstr << std::nouppercase << std::setw(2) << std::setfill('0') @@ -614,19 +617,21 @@ void EBusLoop::addScanCommand() m_scan = false; } - m_scanIndex++; + if (m_scanIndex > 0xFF) + m_scan = false; + else { + m_scanIndex++; - ebusCommand += sstr.str(); - ebusCommand += "070400"; - std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + ebusCommand += sstr.str(); + ebusCommand += "070400"; + std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); - L.log(bus, event, " scanning address %s", sstr.str().c_str()); + L.log(bus, event, " scanning address %s", sstr.str().c_str()); - BusCommand* busCommand = new BusCommand(ebusCommand, true, true); - L.log(bus, trace, " msg: %s", ebusCommand.c_str()); - - addBusCommand(busCommand); - + BusCommand* busCommand = new BusCommand(ebusCommand, true, true); + L.log(bus, trace, " msg: %s", ebusCommand.c_str()); + addBusCommand(busCommand); + } } diff --git a/src/lib/utils/appl.cpp b/src/lib/utils/appl.cpp index 8023984e..628d84d2 100644 --- a/src/lib/utils/appl.cpp +++ b/src/lib/utils/appl.cpp @@ -198,7 +198,7 @@ void Appl::printHelp() for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) { if (strcmp(o_it->name, "__text_only__") == 0) - continue; + std::cerr << o_it->description << std::endl; else { const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " "; std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c From 46e8aa7a77067b84d8a3dbdfe429990dbd1eb679 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Mon, 17 Nov 2014 20:47:10 +0100 Subject: [PATCH 25/38] README adapted. --- README | 50 +++++++++++++++++++----------------------- src/ebusd/baseloop.cpp | 26 +++++++++++----------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/README b/README index c4bb56c2..dfc45852 100644 --- a/README +++ b/README @@ -13,6 +13,7 @@ Features * collect data of cycle messages * poll messages and collect received data * write raw data into dump files (for debugging purpose) + * scan ebus to identifies bus participants * available daemon commands: - get fetch data from ebus participant @@ -20,6 +21,10 @@ Features - cyc fetch data from collected cycle messages - hex send given hex value to ebus (ZZPBSBNNDx) + - scan scan kown slave addresses (collected) + - scan full scan all possible slave addresses + - scan result show scanning results + - log areas change log areas - log level change log level @@ -29,12 +34,29 @@ Features - reload reload ebus configuration - stop stop daemon - - quit close connection - help print help page +vendor specific configuration files for ebusd +--------------------------------------------- + +See https://github.com/yuhu-/ebus-configuration + + + +ebusctl - client program for ebusd +---------------------------------- + +server commands: + * connect to ebusd (socket) and execute server commands (commands see above) + +local commands: + * 'feed' ebusd with raw data from dump files (or telnet) to ebusd + + + Build ----- @@ -43,32 +65,6 @@ $ make $ make install -Daemon Configuration --------------------- - -See ./ebusd -h - - -Vendor specific configuration files ------------------------------------ - -See https://github.com/yuhu-/ebus-configuration - - -Tools ------ - -ebusctl - client program for ebusd. - -local commands: - * 'feed' ebusd with raw data from dump files (or telnet) to ebusd - * 'scan' ebus and identifies the participants - -remote commands: - * connect to ebusd (socket) and execute server commands (see above) - -See ./ebusctl -h - For usage and further information take a look on help page. diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 2e778124..962c7193 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -292,16 +292,9 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; case scan: - if (cmd.size() < 1 || cmd.size() > 2) { - result << "usage: 'scan'" << std::endl - << " 'scan full'" << std::endl - << " 'scan result'"; - break; - } - - if (strcasecmp(cmd[1].c_str(), "RESULT") == 0) { - //~ m_ebusloop->scan(true); - result << "TODO show result"; + if (cmd.size() == 1) { + m_ebusloop->scan(); + result << "done"; break; } @@ -311,8 +304,15 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - m_ebusloop->scan(); - result << "done"; + if (strcasecmp(cmd[1].c_str(), "RESULT") == 0) { + //~ m_ebusloop->scan(true); + result << "TODO show result"; + break; + } + + result << "usage: 'scan'" << std::endl + << " 'scan full'" << std::endl + << " 'scan result'"; break; case log: @@ -390,7 +390,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) << " hex - send given hex value 'hex type value' (value: ZZPBSBNNDx)" << std::endl << std::endl << " scan - scan ebus kown addresses 'scan'" << std::endl << " - scan ebus all addresses 'scan full'" << std::endl - << " - scan show results 'scan result'" << std::endl << std::endl + << " - show results 'scan result'" << std::endl << std::endl << " log - change log areas 'log areas area,area,..' (areas: bas|net|bus|cyc|all)" << std::endl << " - change log level 'log level level' (level: error|event|trace|debug)" << std::endl << std::endl << " raw - toggle log raw data 'raw'" << std::endl From 48fcf39d535a43e267883111d24a7d46111c6509 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Mon, 17 Nov 2014 21:06:48 +0100 Subject: [PATCH 26/38] rudimental version of 'scan result' added. --- src/ebusd/baseloop.cpp | 6 ++++-- src/lib/ebus/commands.cpp | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 962c7193..4b66a047 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -305,8 +305,10 @@ std::string BaseLoop::decodeMessage(const std::string& data) } if (strcasecmp(cmd[1].c_str(), "RESULT") == 0) { - //~ m_ebusloop->scan(true); - result << "TODO show result"; + // TODO format scan results + for (size_t i = 0; i < m_commands->sizeScanDB(); i++) + result << m_commands->getScanData(i) << std::endl; + break; } diff --git a/src/lib/ebus/commands.cpp b/src/lib/ebus/commands.cpp index ed113de3..6fdb34d0 100644 --- a/src/lib/ebus/commands.cpp +++ b/src/lib/ebus/commands.cpp @@ -251,7 +251,6 @@ void Commands::storeScanData(const std::string& data) m_scanDB.push_back(data); } - void Commands::printCommand(const cmd_t& command) const { if (command.size() == 0) From 1415b143afdfde1713956866d4215a3ec0f8b40c Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 18 Nov 2014 15:38:36 +0100 Subject: [PATCH 27/38] documentation for class TCPSocket, TCPClient and TCPServer added. --- src/ebusctl/ebusctl.cpp | 5 +-- src/ebusd/ebusd.cpp | 2 + src/lib/ebus/port.h | 2 +- src/lib/utils/appl.h | 2 +- src/lib/utils/tcpsocket.h | 86 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index a1c15ca3..0bf1596c 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -41,7 +41,7 @@ void define_args() A.addText(" 'help' show server commands\n\n" " 'feed' sends a dump file to a local serial device (pts)\n" " (hint: socat -d -d pty,raw,echo=0 pty,raw,echo=0)\n\n" - " Options:\n"); + "Options:\n"); A.addOption("device", "d", OptVal("/dev/ttyUSB60"), dt_string, ot_mandatory, "virtual serial device (/dev/ttyUSB60)"); @@ -124,10 +124,9 @@ int main(int argc, char* argv[]) delete socket; } - else { + else std::cout << "error connecting to " << A.getOptVal("server") << ":" << A.getOptVal("port") << std::endl; - } delete client; } diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 7020a50e..88e69330 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -41,6 +41,8 @@ void define_args() A.setVersion(""PACKAGE_STRING""); + A.addText("Options:\n"); + A.addOption("address", "a", OptVal("FF"), dt_string, ot_mandatory, "\tebus device address (FF)"); diff --git a/src/lib/ebus/port.h b/src/lib/ebus/port.h index 83badc92..b8097af0 100644 --- a/src/lib/ebus/port.h +++ b/src/lib/ebus/port.h @@ -74,7 +74,7 @@ public: bool isOpen(); /** - * @brief sendBytes write bytes into opened file descriptor. + * @brief sendBytes write bytes to opened file descriptor. * @param buffer data to send. * @param nbytes number of bytes to send. * @return number of written bytes or -1 if an error has occured. diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index 80894c9f..b9b79478 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -113,7 +113,7 @@ public: /** * @brief returns the value of the interested option - * @param the interested option + * @param name the interested option * @return casted value */ template diff --git a/src/lib/utils/tcpsocket.h b/src/lib/utils/tcpsocket.h index 8f86f8bb..cf9d974b 100644 --- a/src/lib/utils/tcpsocket.h +++ b/src/lib/utils/tcpsocket.h @@ -23,61 +23,141 @@ #include #include +/** + * @brief class for low level tcp socket operations. (open, close, send, receive) + */ class TCPSocket { public: + /** grant access for friend classes */ friend class TCPClient; friend class TCPServer; + /** + * @brief destructor. + */ ~TCPSocket() { close(m_sfd); } - ssize_t recv(char* buffer, size_t len) { return read(m_sfd, buffer, len); } + /** + * @brief write bytes to opened file descriptor. + * @param buffer data to send. + * @param len number of bytes to send. + * @return number of written bytes or -1 if an error has occured. + */ ssize_t send(const char* buffer, size_t len) { return write(m_sfd, buffer, len); } + /** + * @brief read bytes from opened file descriptor. + * @param buffer for received bytes. + * @param len size of the receive buffer. + * @return number of read bytes or -1 if an error has occured. + */ + ssize_t recv(char* buffer, size_t len) { return read(m_sfd, buffer, len); } + + /** + * @brief returns the tcp port. + * @return the tcp port. + */ int getPort() const { return m_port; } + + /** + * @brief returns the ip address. + * @return the ip address. + */ std::string getIP() const { return m_ip; } + /** + * @brief returns the file descriptor. + * @return the file descriptor. + */ int getFD() const { return m_sfd; } + + /** + * @brief returns status of file descriptor. + * @return true if file descriptor is valid. + */ bool isValid(); private: + /** file descriptor from tcp socket */ int m_sfd; + /** port of tcp socket */ int m_port; + /** ip address of tcp socket */ std::string m_ip; + /** + * @brief private constructor, limited access only for friend classes. + * @param sfd the file desctriptor of tcp socket. + * @param address struct which holds the ip address. + */ TCPSocket(int sfd, struct sockaddr_in* address); }; +/** + * @brief class to initiate a tcp socket connection to a listening server. + */ class TCPClient { public: + /** + * @brief initiate a tcp socket connection to a listening server. + * @param server the server name or ip address to connect. + * @param port the tcp port. + * @return pointer to an opened tcp socket. + */ TCPSocket* connect(const std::string& server, const int& port); -private: - }; +/** + * @brief class for a tcp based network server. + */ class TCPServer { public: + /** + * @brief creates a new instance of a listening tcp server. + * @param port the tcp port + * @param address the ip address + */ TCPServer(const int port, const std::string address) : m_lfd(0), m_port(port), m_address(address), m_listening(false) {} + /** + * @brief destructor. + */ ~TCPServer() { if (m_lfd > 0) {close(m_lfd);} } + /** + * @brief start listening of tcp socket. + */ int start(); + + /** + * @brief accept an incomming tcp connection and create a local tcp socket for communication. + * @return pointer to an opened tcp socket. + */ TCPSocket* newSocket(); + /** + * @brief returns the file descriptor. + * @return the file descriptor. + */ int getFD() const { return m_lfd; } private: + /** file descriptor from listening tcp socket */ int m_lfd; + /** listening tcp port */ int m_port; + /** listening tcp socket ip address */ std::string m_address; + /** true if object is already listening */ bool m_listening; }; From e2f6bbffb0f4430e79fd116980b450ca303320c4 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 18 Nov 2014 20:15:01 +0100 Subject: [PATCH 28/38] class LogInstance renamed to Logger; documentation for logging subsystem added. --- src/ebusd/Makefile.am | 4 +- src/ebusd/baseloop.cpp | 6 +- src/ebusd/baseloop.h | 4 +- src/ebusd/connection.cpp | 10 +- src/ebusd/connection.h | 10 +- src/ebusd/ebusd.cpp | 2 +- src/ebusd/ebusloop.cpp | 2 +- src/ebusd/{message.h => netmessage.h} | 13 +- src/ebusd/network.cpp | 4 +- src/ebusd/network.h | 4 +- src/lib/ebus/configfile.h | 27 +-- src/lib/ebus/dump.h | 1 + src/lib/ebus/port.h | 26 ++- src/lib/utils/appl.h | 50 ++--- src/lib/utils/daemon.h | 4 +- src/lib/utils/logger.cpp | 50 ++--- src/lib/utils/logger.h | 251 +++++++++++++++++++++----- src/lib/utils/notify.h | 11 +- src/lib/utils/tcpsocket.h | 11 +- src/lib/utils/thread.h | 4 +- src/lib/utils/wqueue.h | 10 +- 21 files changed, 347 insertions(+), 157 deletions(-) rename src/ebusd/{message.h => netmessage.h} (86%) diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index bf57211a..804fd608 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -6,8 +6,7 @@ AM_CXXFLAGS = -fpic \ bin_PROGRAMS = ebusd -ebusd_SOURCES = message.h \ - connection.cpp \ +ebusd_SOURCES = connection.cpp \ connection.h \ network.cpp \ network.h \ @@ -15,6 +14,7 @@ ebusd_SOURCES = message.h \ ebusloop.h \ baseloop.cpp \ baseloop.h \ + netmessage.h \ ebusd.cpp ebusd_LDADD = $(top_srcdir)/src/lib/utils/libutils.a \ diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 4b66a047..25a25b22 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -22,7 +22,7 @@ #include "logger.h" #include "appl.h" -extern LogInstance& L; +extern Logger& L; extern Appl& A; BaseLoop::BaseLoop() @@ -67,7 +67,7 @@ void BaseLoop::start() std::string result; // recv new message from client - Message* message = m_msgQueue.remove(); + NetMessage* message = m_msgQueue.remove(); std::string data = message->getData(); data.erase(std::remove(data.begin(), data.end(), '\r'), data.end()); @@ -86,7 +86,7 @@ void BaseLoop::start() // send result to client result += '\n'; Connection* connection = message->getConnection(); - connection->addResult(Message(result)); + connection->addResult(NetMessage(result)); delete message; diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index 1ef4426e..6dcad253 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -36,14 +36,14 @@ public: void start(); - void addMessage(Message* message) { m_msgQueue.add(message); } + void addMessage(NetMessage* message) { m_msgQueue.add(message); } private: Commands* m_commands; EBusLoop* m_ebusloop; Network* m_network; - WQueue m_msgQueue; + WQueue m_msgQueue; enum ClientCommand { get, // get ebus data diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index b82011cf..49912bbe 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -22,13 +22,13 @@ #include #include -extern LogInstance& L; +extern Logger& L; int Connection::m_sum = 0; -void Connection::addResult(Message message) +void Connection::addResult(NetMessage message) { - Message* tmp = new Message(Message(message)); + NetMessage* tmp = new NetMessage(NetMessage(message)); m_result.add(tmp); } @@ -80,11 +80,11 @@ void* Connection::run() // send data data[datalen] = '\0'; - m_data->add(new Message(data, this)); + m_data->add(new NetMessage(data, this)); // wait for result L.log(net, debug, "[%05d] wait for result", getID()); - Message* message = m_result.remove(); + NetMessage* message = m_result.remove(); L.log(net, debug, "[%05d] result added", getID()); std::string result(message->getData()); diff --git a/src/ebusd/connection.h b/src/ebusd/connection.h index 5e9b160c..b7c538f4 100644 --- a/src/ebusd/connection.h +++ b/src/ebusd/connection.h @@ -24,16 +24,16 @@ #include "wqueue.h" #include "notify.h" #include "thread.h" -#include "message.h" +#include "netmessage.h" class Connection : public Thread { public: - Connection(TCPSocket* socket, WQueue* data) + Connection(TCPSocket* socket, WQueue* data) : m_socket(socket), m_data(data), m_running(false) { m_sum++; m_id = m_sum;} - void addResult(Message message); + void addResult(NetMessage message); void* run(); void stop() const { m_notify.notify(); } @@ -43,8 +43,8 @@ public: private: TCPSocket* m_socket; - WQueue* m_data; - WQueue m_result; + WQueue* m_data; + WQueue m_result; Notify m_notify; bool m_running; int m_id; diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index 88e69330..b991cd7d 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -32,7 +32,7 @@ using namespace libebus; Appl& A = Appl::Instance(); Daemon& D = Daemon::Instance(); -LogInstance& L = LogInstance::Instance(); +Logger& L = Logger::Instance(); BaseLoop* baseloop; diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/ebusloop.cpp index f26174da..833bf47e 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/ebusloop.cpp @@ -22,7 +22,7 @@ #include "appl.h" #include -extern LogInstance& L; +extern Logger& L; extern Appl& A; EBusLoop::EBusLoop(Commands* commands) diff --git a/src/ebusd/message.h b/src/ebusd/netmessage.h similarity index 86% rename from src/ebusd/message.h rename to src/ebusd/netmessage.h index 55ee0f68..60a54053 100644 --- a/src/ebusd/message.h +++ b/src/ebusd/netmessage.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef MESSAGE_H_ -#define MESSAGE_H_ +#ifndef NETMESSAGE_H_ +#define NETMESSAGE_H_ #include @@ -28,7 +28,7 @@ class Connection; /** * @brief class for data/message transfer between connection and baseloop */ -class Message +class NetMessage { public: @@ -37,14 +37,14 @@ public: * @param data from client * @param connection to return result to correct client */ - Message(const std::string data, Connection* connection=NULL) + NetMessage(const std::string data, Connection* connection=NULL) : m_data(data), m_connection(connection) {} /** * @brief copy constructor. * @param src message object for copy */ - Message(const Message& src) : m_data(src.m_data), m_connection(src.m_connection) {} + NetMessage(const NetMessage& src) : m_data(src.m_data), m_connection(src.m_connection) {} /** * @brief data from client @@ -61,10 +61,11 @@ public: private: /** the data/message string */ std::string m_data; + /** the source connection */ Connection* m_connection; }; -#endif // MESSAGE_H_ +#endif // NETMESSAGE_H_ diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index 2762a09e..325a8934 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -22,11 +22,11 @@ #include "appl.h" #include -extern LogInstance& L; +extern Logger& L; extern Appl& A; -Network::Network(const bool localhost, WQueue* msgQueue) +Network::Network(const bool localhost, WQueue* msgQueue) : m_msgQueue(msgQueue), m_listening(false), m_running(false) { if (localhost == true) diff --git a/src/ebusd/network.h b/src/ebusd/network.h index 1bc9a26d..c62af478 100644 --- a/src/ebusd/network.h +++ b/src/ebusd/network.h @@ -26,7 +26,7 @@ class Network : public Thread { public: - Network(const bool localhost, WQueue* msgQueue); + Network(const bool localhost, WQueue* msgQueue); ~Network(); void* run(); @@ -34,7 +34,7 @@ public: private: std::list m_connections; - WQueue* m_msgQueue; + WQueue* m_msgQueue; TCPServer* m_tcpServer; Notify m_notify; bool m_listening; diff --git a/src/lib/ebus/configfile.h b/src/lib/ebus/configfile.h index 4d5183b1..9be9a622 100644 --- a/src/lib/ebus/configfile.h +++ b/src/lib/ebus/configfile.h @@ -31,14 +31,14 @@ namespace libebus enum FileType { CSV, XML }; /** - * @brief Base class for config files. + * @brief base class for config files. */ class ConfigFile { public: /** - * @brief Destructor. + * @brief destructor. */ virtual ~ConfigFile() {} @@ -52,14 +52,14 @@ public: }; /** - * @brief Class for CSV config files. + * @brief class for CSV config files. */ class ConfigFileCSV : public ConfigFile { public: /** - * @brief Destructor. + * @brief destructor. */ ~ConfigFileCSV() {} @@ -73,14 +73,14 @@ public: }; /** - * @brief Class for XML config files. + * @brief class for XML config files. */ class ConfigFileXML : public ConfigFile { public: /** - * @brief Destructor. + * @brief destructor. */ ~ConfigFileXML() {} @@ -95,32 +95,32 @@ public: /** - * @brief Class for class Device. + * @brief class for class device. */ class ConfigCommands { public: /** - * @brief Set file type and add recursive files from given path. + * @brief set file type and add recursive files from given path. * @param path to configuration files. - * @param Filetype to parse. + * @param filetype to parse. */ ConfigCommands(const std::string path, const FileType type); /** - * @brief Destructor. + * @brief destructor. */ ~ConfigCommands() { delete m_configfile; } /** * @brief setter for file type. - * @param FileType of files. + * @param filetype of files. */ void setType(const FileType type); /** - * @brief Parse files for commands and store them into commands instance. + * @brief parse files for commands and store them into commands instance. * @return a commands instance */ Commands* getCommands(); @@ -128,10 +128,13 @@ public: private: /** the configfile instance */ ConfigFile* m_configfile; + /** main path for configuration files */ std::string m_path; + /** valid file extension */ std::string m_extension; + /** vector of configuration files */ std::vector m_files; diff --git a/src/lib/ebus/dump.h b/src/lib/ebus/dump.h index 34418139..e64c6356 100644 --- a/src/lib/ebus/dump.h +++ b/src/lib/ebus/dump.h @@ -62,6 +62,7 @@ public: private: /** the name of dump file*/ std::string m_filename; + /** max. size of dump file */ long m_filesize; diff --git a/src/lib/ebus/port.h b/src/lib/ebus/port.h index b8097af0..edcf1889 100644 --- a/src/lib/ebus/port.h +++ b/src/lib/ebus/port.h @@ -39,19 +39,19 @@ enum DeviceType { SERIAL, NETWORK }; /** - * @brief Base class for input devices. + * @brief base class for input devices. */ class Device { public: /** - * @brief Constructs a new instance. + * @brief constructs a new instance. */ Device() : m_fd(-1), m_open(false), m_noDeviceCheck(false) {} /** - * @brief Destructor. + * @brief destructor. */ virtual ~Device() {} @@ -104,12 +104,16 @@ public: protected: /** if of file descriptor */ int m_fd; + /** state of device*/ bool m_open; + /** state of device check */ bool m_noDeviceCheck; + /** queue for received bytes */ std::queue m_recvBuffer; + /** receive buffer */ unsigned char m_buffer[MAX_READ_SIZE]; @@ -123,14 +127,14 @@ private: }; /** - * @brief Class for serial input device. + * @brief class for serial input device. */ class DeviceSerial : public Device { public: /** - * @brief Destructor. + * @brief destructor. */ ~DeviceSerial() { closeDevice(); } @@ -153,14 +157,14 @@ private: }; /** - * @brief Class for network input device. + * @brief class for network input device. */ class DeviceNetwork : public Device { public: /** - * @brief Destructor. + * @brief destructor. */ ~DeviceNetwork() { closeDevice(); } @@ -181,21 +185,21 @@ private: }; /** - * @brief Wrapper class for class Device. + * @brief wrapper class for class device. */ class Port { public: /** - * @brief Constructs a new instance and determine device type. + * @brief constructs a new instance and determine device type. * @param deviceName to determine device type. * @param noDeviceCheck en-/disable device check. */ Port(const std::string deviceName, const bool noDeviceCheck); /** - * @brief Destructor. + * @brief destructor. */ ~Port() { delete m_device; } @@ -248,8 +252,10 @@ public: private: /** the device name */ std::string m_deviceName; + /** the device instance */ Device* m_device; + /** true if device check is disabled */ bool m_noDeviceCheck; diff --git a/src/lib/utils/appl.h b/src/lib/utils/appl.h index b9b79478..2a04bb11 100644 --- a/src/lib/utils/appl.h +++ b/src/lib/utils/appl.h @@ -76,7 +76,7 @@ class Appl public: /** - * @brief create an appl instance and return the reference. + * @brief create an instance and return the reference. * @param command is true if an command is needed. * @return the reference to instance. */ @@ -89,32 +89,32 @@ public: /** * @brief save application version string. - * @param version string + * @param version string. */ void setVersion(const char* version) { m_version = version; } /** * @brief create new entry of application option only for help page. - * @param text string to print + * @param text string to print. */ void addText(const char* text); /** * @brief create new entry of application option. - * @param name the long name - * @param shortname optional short name - * @param optval value of option - * @param datatype data type of option value - * @param optiontype type of given option - * @param description hint text for help page + * @param name the long name. + * @param shortname optional short name. + * @param optval value of option. + * @param datatype data type of option value. + * @param optiontype type of given option. + * @param description hint text for help page. */ void addOption(const char* name, const char* shortname, OptVal optval, DataType datatype, OptionType optiontype, const char* description); /** - * @brief returns the value of the interested option - * @param name the interested option - * @return casted value + * @brief returns the value of the interested option. + * @param name the interested option. + * @return casted value. */ template T getOptVal(const char* name) @@ -125,26 +125,26 @@ public: /** * @brief parse application arguments. - * @param argc the number of options - * @param argv the given options + * @param argc the number of options. + * @param argv the given options. */ void parseArgs(int argc, char* argv[]); /** - * @brief returns the number of saved commands and arguments - * @return number of commands and arguments + * @brief returns the number of saved commands and arguments. + * @return number of commands and arguments. */ int numArgs() const { return m_arguments.size(); } /** - * @brief returns the string of an interested argument (0 = command) - * @param num number of interested argument - * @return string value + * @brief returns the string of an interested argument (0 = command). + * @param num number of interested argument. + * @return string value. */ std::string getArg(const int num) const { return m_arguments[num]; } private: - /** private constructor - singleton pattern*/ + /** private constructor - singleton pattern */ Appl(const bool command) : m_needCommand(command) {} Appl(const Appl&); Appl& operator=(const Appl&); @@ -171,16 +171,16 @@ private: /** * @brief checks the passed parameter if this is a valid option. - * @param option to check - * @param value to save if paramter is a valid option + * @param option to check. + * @param value to save if paramter is a valid option. */ bool checkOption(const std::string& option, const std::string& value); /** * @brief save the passed value to option. - * @param option name - * @param value to save - * @param datatype of given option + * @param option name. + * @param value to save. + * @param datatype of given option. */ void setOptVal(const char* option, const std::string value, DataType datatype); diff --git a/src/lib/utils/daemon.h b/src/lib/utils/daemon.h index 4ce8780a..d7f3fdbd 100644 --- a/src/lib/utils/daemon.h +++ b/src/lib/utils/daemon.h @@ -28,7 +28,7 @@ class Daemon public: /** - * @brief create an daemon instance and return the reference. + * @brief create an instance and return the reference. * @return the reference to instance. */ static Daemon& Instance(); @@ -58,8 +58,10 @@ private: /** status of process; true if we are a daemon */ bool m_status; + /** name of the pid file*/ const char* m_pidfile; + /** file descriptor of the pid file */ int m_pidfd; diff --git a/src/lib/utils/logger.cpp b/src/lib/utils/logger.cpp index 7921851e..12fb49f8 100644 --- a/src/lib/utils/logger.cpp +++ b/src/lib/utils/logger.cpp @@ -28,7 +28,10 @@ #include #include +/** */ static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus" }; + +/** */ static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" }; int calcAreas(const std::string areas) @@ -66,8 +69,8 @@ int calcLevel(const std::string level) } -LogMessage::LogMessage(const int area, const int level, const std::string text, const bool run) - : m_area(area), m_level(level), m_text(text), m_run(run) +LogMessage::LogMessage(const int area, const int level, const std::string text, const bool running) + : m_area(area), m_level(level), m_text(text), m_running(running) { char time[24]; struct timeval tv; @@ -89,17 +92,17 @@ LogMessage::LogMessage(const int area, const int level, const std::string text, void LogSink::addMessage(const LogMessage& message) { LogMessage* tmp = new LogMessage(LogMessage(message)); - m_queue.add((tmp)); + m_logMessages.add((tmp)); } void* LogSink::run() { while (1) { - LogMessage* message = m_queue.remove(); + LogMessage* message = m_logMessages.remove(); if (message->isRunning() == false) { delete message; - while (m_queue.size() == true) { - LogMessage* message = m_queue.remove(); + while (m_logMessages.size() == true) { + LogMessage* message = m_logMessages.remove(); write(*message); delete message; } @@ -114,8 +117,6 @@ void* LogSink::run() -int LogConsole::m_numInstance = 0; - void LogConsole::write(const LogMessage& message) const { std::cout << message.getTime() << " [" @@ -126,11 +127,9 @@ void LogConsole::write(const LogMessage& message) const -int LogFile::m_numInstance = 0; - void LogFile::write(const LogMessage& message) const { - std::fstream file(m_filename.c_str(), std::ios::out | std::ios::app); + std::fstream file(m_file.c_str(), std::ios::out | std::ios::app); if (file.is_open() == true) { file << message.getTime() << " [" @@ -143,19 +142,19 @@ void LogFile::write(const LogMessage& message) const -LogInstance& LogInstance::Instance() +Logger& Logger::Instance() { - static LogInstance instance; + static Logger instance; return (instance); } -LogInstance::~LogInstance() +Logger::~Logger() { while (m_sinks.empty() == false) *this -= *(m_sinks.begin()); } -LogInstance& LogInstance::operator+= (LogSink* sink) +Logger& Logger::operator+=(LogSink* sink) { sinkCI_t itEnd = m_sinks.end(); sinkCI_t it = std::find(m_sinks.begin(), itEnd, sink); @@ -166,7 +165,7 @@ LogInstance& LogInstance::operator+= (LogSink* sink) return (*this); } -LogInstance& LogInstance::operator-= (const LogSink* sink) +Logger& Logger::operator-=(const LogSink* sink) { sinkCI_t itEnd = m_sinks.end(); sinkCI_t it = std::find(m_sinks.begin(), itEnd, sink); @@ -181,7 +180,7 @@ LogInstance& LogInstance::operator-= (const LogSink* sink) return (*this); } -void LogInstance::log(const int area, const int level, const std::string& data, ...) +void Logger::log(const int area, const int level, const std::string& data, ...) { if (m_running == true) { char* tmp; @@ -190,7 +189,7 @@ void LogInstance::log(const int area, const int level, const std::string& data, if (vasprintf(&tmp, data.c_str(), ap) != -1) { std::string buffer(tmp); - m_messages.add(new LogMessage(LogMessage(area, level, buffer))); + m_logMessages.add(new LogMessage(LogMessage(area, level, buffer))); } va_end(ap); @@ -199,12 +198,12 @@ void LogInstance::log(const int area, const int level, const std::string& data, } -void* LogInstance::run() +void* Logger::run() { m_running = true; while (m_running == true) { - LogMessage* message = m_messages.remove(); + LogMessage* message = m_logMessages.remove(); sinkCI_t iter = m_sinks.begin(); @@ -213,12 +212,13 @@ void* LogInstance::run() if (((*iter)->getAreas() & message->getArea() && (*iter)->getLevel() >= message->getLevel()) - && message->isRunning() == true) { + && message->isRunning() == true) (*iter)->addMessage(*message); - } else if (message->isRunning() == false) { + + else if (message->isRunning() == false) (*iter)->addMessage(*message); m_running = false; - } + } } @@ -229,8 +229,8 @@ void* LogInstance::run() return NULL; } -void LogInstance::stop() +void Logger::stop() { - m_messages.add(new LogMessage(LogMessage(bas, error, "", false))); + m_logMessages.add(new LogMessage(LogMessage(bas, error, "", false))); usleep(100000); } diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index adae3738..10891949 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -28,142 +28,309 @@ #include #include -enum Areas { bas=1, net=2, bus=4, all=7, Size_of_Areas=3 }; -enum Level { error=0, event, trace, debug, Size_of_Level }; +/** available types for all subsystems */ +enum AreasType { + bas=1, // basis + net=2, // network + bus=4, // ebus + all=7, // type for all subsystems + Size_of_Areas=3, // number of possible areas +}; +/** available logging levels */ +enum LevelType { + error=0, // + event, // + trace, // + debug, // + Size_of_Level, // number of possible levels +}; + +/** global function to get calculate logging areas */ int calcAreas(const std::string areas); + +/** global function to get calculate logging level */ int calcLevel(const std::string level); - +/** + * @brief class which describes a logging message itself. + */ class LogMessage { public: - LogMessage(const int area, const int level, const std::string text, const bool run=true); + /** + * @brief creates a new logging message. + * @param area the logging area of the message. + * @param level the logging level of the message. + * @param text the logging message. + * @param running the status of logging subsystem. + */ + LogMessage(const int area, const int level, const std::string text, const bool running=true); + /** + * @brief copy constructor to duplicate a logging message. + * @param src a reference to logging message. + */ LogMessage(const LogMessage& src) : m_area(src.m_area), m_level(src.m_level), m_text(src.m_text), - m_run(src.m_run), m_time(src.m_time) {} + m_running(src.m_running), m_time(src.m_time) {} - void operator= (const LogMessage& src) + /** + * @brief copy operator to duplicate a logging message. + * @param src a reference to logging message. + */ + void operator=(const LogMessage& src) { m_area = src.m_area; m_level = src.m_level; m_text = src.m_text; - m_run = src.m_run; m_time = src.m_time; } + m_running = src.m_running; m_time = src.m_time; } + /** + * @brief get the logging area. + * @return the logging area. + */ int getArea() const { return (m_area); } + + /** + * @brief get the logging level. + * @return the logging level. + */ int getLevel() const { return(m_level); } + + /** + * @brief get the logging text. + * @return the logging text. + */ std::string getText() const { return (m_text.c_str()); } - bool isRunning() const { return (m_run); } + + /** + * @brief status of logging subsystem. + * @return false if logging subsystem is going down. + */ + bool isRunning() const { return m_running; } + + /** + * @brief get the logging timestamp. + * @return the logging timestamp. + */ std::string getTime() const { return (m_time.c_str()); } private: + /** the logging area */ int m_area; + + /** the logging level */ int m_level; + + /** the logging message */ std::string m_text; - bool m_run; + + /** true if this instance is running */ + bool m_running; + + /** the logging timestamp */ std::string m_time; }; -enum Type { Console, Logfile }; - +/** + * @brief base class for all type of logging sinks. + */ class LogSink : public Thread { public: - LogSink(const int areas, const int level, const Type type, const char* name) - : m_areas(areas), m_level(level), m_type(type), m_name(name) {} + /** + * @brief creates a virtual logging sink. + * @param areas the logging areas. + * @param level the logging level. + */ + LogSink(const int areas, const int level) : m_areas(areas), m_level(level) {} + /** + * @brief adds the logging message to internal message queue. + * @param message a reference to logging message. + */ void addMessage(const LogMessage& message); + /** + * @brief endless loop for logging sink instance. + * @return void pointer. + */ void* run(); + /** + * @brief get the logging areas. + * @return the logging areas. + */ int getAreas() const { return (m_areas); } + + /** + * @brief set the logging areas. + * @param areas the logging areas. + */ void setAreas(const int& areas) { m_areas = areas; } + /** + * @brief get the logging level. + * @return the logging level. + */ int getLevel() const { return (m_level); } + + /** + * @brief set the logging level. + * @param level the logging level. + */ void setLevel(const int& level) { m_level = level; } - Type getType() const { return (m_type); } - const char* getName() const { return (m_name.c_str()); } - protected: - WQueue m_queue; + /** queue for logging messages */ + WQueue m_logMessages; private: + /** the logging areas */ int m_areas; - int m_level; - Type m_type; - std::string m_name; + /** the logging level */ + int m_level; + + /** + * @brief virtual function for writing the logging message. + * @param message the logging message. + */ virtual void write(const LogMessage& message) const = 0; }; +/** + * @brief class for console logging sink type. + */ class LogConsole : public LogSink { public: + /** + * @brief creates a console logging sink. + * @param areas the logging areas. + * @param level the logging level. + * @param name the thread name for logging sink. + */ LogConsole(const int areas, const int level, const char* name) - : LogSink(areas, level, Console, name), m_instance(++m_numInstance) - { this->start(name); } + : LogSink(areas, level) { this->start(name); } private: - const int m_instance; - static int m_numInstance; - + /** + * @brief write the logging message to stdout. + * @param message the logging message. + */ void write(const LogMessage& message) const; }; +/** + * @brief class for logfile logging sink type. + */ class LogFile : public LogSink { public: - LogFile(const int areas, const int level, const char* name, const char* filename) - : LogSink(areas, level, Logfile, name), m_filename(filename), m_instance(++m_numInstance) - { this->start(name); } + /** + * @brief creates a log file logging sink. + * @param areas the logging areas. + * @param level the logging level. + * @param name the thread name for logging sink. + * @param file the log file. + */ + LogFile(const int areas, const int level, const char* name, const char* file) + : LogSink(areas, level), m_file(file) { this->start(name); } private: - std::string m_filename; - const int m_instance; - static int m_numInstance; + /** the logging file */ + std::string m_file; + /** + * @brief write the logging message to specific log file. + * @param message the logging message. + */ void write(const LogMessage& message) const; }; -class LogInstance : public Thread +/** + * @brief logger base class which provide the logging interface. + */ +class Logger : public Thread { public: - static LogInstance& Instance(); + /** + * @brief create an instance and return the reference. + * @return the reference to instance. + */ + static Logger& Instance(); - ~LogInstance(); + /** + * @brief destructor. + */ + ~Logger(); - LogInstance& operator+= (LogSink* sink); - LogInstance& operator-= (const LogSink* sink); + /** + * @brief adds a logging sink and returns the reference to logger. + * @param pointer of logging sink. + * @return the reference to logger. + */ + Logger& operator+=(LogSink* sink); + /** + * @brief removes a logging sink and returns the reference to logger. + * @param pointer of logging sink. + * @return the reference to logger. + */ + Logger& operator-=(const LogSink* sink); + + /** + * @brief creates a logging message and add them to internal message queue. + * @param area the logging area of the message. + * @param level the logging level of the message. + * @param text the logging message. + * @param ... possible 'variable argument lists'. + */ void log(const int area, const int level, const std::string& text, ...); - int getNumberOfSinks() const { return(m_sinks.size()); } - LogSink* getSink(const int Index) const { return(m_sinks[Index]); } + /** + * @brief returns the sink at the specified index. + * @param index the index of the sink to return. + * @return the sink at the specified index. + */ + LogSink* getSink(const int index) const { return(m_sinks[index]); } + /** + * @brief endless loop for logger instance. + * @return void pointer. + */ void* run(); + /** + * @brief shutdown logger subsystem. + */ void stop(); private: - LogInstance() {} - LogInstance(const LogInstance&); - LogInstance& operator=(const LogInstance&); + /** private constructor - singleton pattern */ + Logger() {} + Logger(const Logger&); + Logger& operator=(const Logger&); + /** typedefs for a vector of type LogSink* */ typedef std::vector sink_t; typedef std::vector::iterator sinkCI_t; + /** vector of available logging sinks */ sink_t m_sinks; - WQueue m_messages; + /** queue for logging messages */ + WQueue m_logMessages; + /** true if this instance is running */ bool m_running; }; diff --git a/src/lib/utils/notify.h b/src/lib/utils/notify.h index 05b1491b..188040a1 100644 --- a/src/lib/utils/notify.h +++ b/src/lib/utils/notify.h @@ -42,21 +42,22 @@ public: /** * @brief file descriptor to watch for notify event. - * @return the notification value + * @return the notification value. */ int notifyFD() { return m_recvfd; } /** * @brief write notify event to file descriptor. - * @return result of writing notification + * @return result of writing notification. */ int notify() const { return write(m_sendfd,"1",1); } private: /** file descriptor to watch */ - int m_recvfd; - /** file descriptor to notify */ - int m_sendfd; + int m_recvfd; + + /** file descriptor to notify */ + int m_sendfd; }; diff --git a/src/lib/utils/tcpsocket.h b/src/lib/utils/tcpsocket.h index cf9d974b..f7861bdb 100644 --- a/src/lib/utils/tcpsocket.h +++ b/src/lib/utils/tcpsocket.h @@ -24,7 +24,7 @@ #include /** - * @brief class for low level tcp socket operations. (open, close, send, receive) + * @brief class for low level tcp socket operations. (open, close, send, receive). */ class TCPSocket { @@ -82,8 +82,10 @@ public: private: /** file descriptor from tcp socket */ int m_sfd; + /** port of tcp socket */ int m_port; + /** ip address of tcp socket */ std::string m_ip; @@ -122,8 +124,8 @@ class TCPServer public: /** * @brief creates a new instance of a listening tcp server. - * @param port the tcp port - * @param address the ip address + * @param port the tcp port. + * @param address the ip address. */ TCPServer(const int port, const std::string address) : m_lfd(0), m_port(port), m_address(address), m_listening(false) {} @@ -153,10 +155,13 @@ public: private: /** file descriptor from listening tcp socket */ int m_lfd; + /** listening tcp port */ int m_port; + /** listening tcp socket ip address */ std::string m_address; + /** true if object is already listening */ bool m_listening; diff --git a/src/lib/utils/thread.h b/src/lib/utils/thread.h index 4d4e15dc..e18e4062 100644 --- a/src/lib/utils/thread.h +++ b/src/lib/utils/thread.h @@ -65,7 +65,7 @@ public: pthread_t self() {return m_threadid; } /** - * @brief virtul function which must be implented in derived class. + * @brief virtul function which must be implemented in derived class. * @return void pointer. */ virtual void* run() = 0; @@ -73,8 +73,10 @@ public: private: /** own thread id */ pthread_t m_threadid; + /** true if thread is running */ bool m_running; + /** true if thread is detached */ bool m_detached; diff --git a/src/lib/utils/wqueue.h b/src/lib/utils/wqueue.h index cf696370..468885a4 100644 --- a/src/lib/utils/wqueue.h +++ b/src/lib/utils/wqueue.h @@ -51,7 +51,7 @@ public: /** * @brief add a new item to the end of queue. - * @param item + * @param item to add. */ void add(T item) { @@ -65,7 +65,7 @@ public: /** * @brief remove the first item from queue. - * @return the item + * @return the item. */ T remove() { @@ -84,7 +84,7 @@ public: /** * @brief return the first item from queue without remove. - * @return the item + * @return the item. */ T next() { @@ -102,7 +102,7 @@ public: /** * @brief the number of entries inside queue. - * @return the size + * @return the size. */ int size() { @@ -118,8 +118,10 @@ public: private: /** the queue itself */ std::list m_queue; + /** mutex variable for exclusive lock */ pthread_mutex_t m_mutex; + /** condition variable for exclusive lock */ pthread_cond_t m_cond; From 8992ee4f8580efb64d904ab1cedeb0a8c675dbfa Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Tue, 18 Nov 2014 22:06:14 +0100 Subject: [PATCH 29/38] fix logger quirks; many cosmetic changes. --- src/ebusd/baseloop.cpp | 6 +++--- src/ebusd/baseloop.h | 4 ++-- src/ebusd/connection.cpp | 6 +++--- src/ebusd/connection.h | 8 ++++---- src/ebusd/netmessage.h | 18 +++++++++--------- src/ebusd/network.cpp | 6 +++--- src/ebusd/network.h | 4 ++-- src/lib/utils/logger.cpp | 25 +++++++++++++------------ src/lib/utils/logger.h | 4 ++-- 9 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 25a25b22..7e5f8a35 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -39,7 +39,7 @@ BaseLoop::BaseLoop() m_ebusloop->start("ebusloop"); // create network - m_network = new Network(A.getOptVal("localhost"), &m_msgQueue); + m_network = new Network(A.getOptVal("localhost"), &m_netQueue); m_network->start("network"); } @@ -67,7 +67,7 @@ void BaseLoop::start() std::string result; // recv new message from client - NetMessage* message = m_msgQueue.remove(); + NetMessage* message = m_netQueue.remove(); std::string data = message->getData(); data.erase(std::remove(data.begin(), data.end(), '\r'), data.end()); @@ -400,7 +400,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) << " reload - reload ebus configuration 'reload'" << std::endl << std::endl << " stop - stop daemon 'stop'" << std::endl << " quit - close connection 'quit'" << std::endl << std::endl - << " help - print this page 'help"; + << " help - print this page 'help'"; break; default: diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index 6dcad253..ec18540b 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -36,14 +36,14 @@ public: void start(); - void addMessage(NetMessage* message) { m_msgQueue.add(message); } + void addMessage(NetMessage* message) { m_netQueue.add(message); } private: Commands* m_commands; EBusLoop* m_ebusloop; Network* m_network; - WQueue m_msgQueue; + WQueue m_netQueue; enum ClientCommand { get, // get ebus data diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index 49912bbe..f591f4cf 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -29,7 +29,7 @@ int Connection::m_sum = 0; void Connection::addResult(NetMessage message) { NetMessage* tmp = new NetMessage(NetMessage(message)); - m_result.add(tmp); + m_netQueueResult.add(tmp); } void* Connection::run() @@ -80,11 +80,11 @@ void* Connection::run() // send data data[datalen] = '\0'; - m_data->add(new NetMessage(data, this)); + m_netQueueData->add(new NetMessage(data, this)); // wait for result L.log(net, debug, "[%05d] wait for result", getID()); - NetMessage* message = m_result.remove(); + NetMessage* message = m_netQueueResult.remove(); L.log(net, debug, "[%05d] result added", getID()); std::string result(message->getData()); diff --git a/src/ebusd/connection.h b/src/ebusd/connection.h index b7c538f4..bf847d32 100644 --- a/src/ebusd/connection.h +++ b/src/ebusd/connection.h @@ -30,8 +30,8 @@ class Connection : public Thread { public: - Connection(TCPSocket* socket, WQueue* data) - : m_socket(socket), m_data(data), m_running(false) { m_sum++; m_id = m_sum;} + Connection(TCPSocket* socket, WQueue* netQueue) + : m_socket(socket), m_netQueueData(netQueue), m_running(false) { m_sum++; m_id = m_sum;} void addResult(NetMessage message); @@ -43,8 +43,8 @@ public: private: TCPSocket* m_socket; - WQueue* m_data; - WQueue m_result; + WQueue* m_netQueueData; + WQueue m_netQueueResult; Notify m_notify; bool m_running; int m_id; diff --git a/src/ebusd/netmessage.h b/src/ebusd/netmessage.h index 60a54053..7b285392 100644 --- a/src/ebusd/netmessage.h +++ b/src/ebusd/netmessage.h @@ -26,35 +26,35 @@ class Connection; /** - * @brief class for data/message transfer between connection and baseloop + * @brief class for data/message transfer between connection and baseloop. */ class NetMessage { public: /** - * @brief constructs a new instance with message and source client address - * @param data from client - * @param connection to return result to correct client + * @brief constructs a new instance with message and source client address. + * @param data from client. + * @param connection to return result to correct client. */ NetMessage(const std::string data, Connection* connection=NULL) : m_data(data), m_connection(connection) {} /** * @brief copy constructor. - * @param src message object for copy + * @param src message object for copy. */ NetMessage(const NetMessage& src) : m_data(src.m_data), m_connection(src.m_connection) {} /** - * @brief data from client - * @return data string + * @brief get the data string. + * @return the data string. */ std::string getData() const { return m_data; } /** - * @brief original connection - * @return pointer to connection + * @brief original connection. + * @return pointer to connection. */ Connection* getConnection() const { return m_connection; } diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index 325a8934..92b18877 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -26,8 +26,8 @@ extern Logger& L; extern Appl& A; -Network::Network(const bool localhost, WQueue* msgQueue) - : m_msgQueue(msgQueue), m_listening(false), m_running(false) +Network::Network(const bool localhost, WQueue* netQueue) + : m_netQueue(netQueue), m_listening(false), m_running(false) { if (localhost == true) m_tcpServer = new TCPServer(A.getOptVal("port"), "127.0.0.1"); @@ -99,7 +99,7 @@ void* Network::run() if (socket == NULL) continue; - Connection* connection = new Connection(socket, m_msgQueue); + Connection* connection = new Connection(socket, m_netQueue); if (connection == NULL) continue; diff --git a/src/ebusd/network.h b/src/ebusd/network.h index c62af478..2805ec95 100644 --- a/src/ebusd/network.h +++ b/src/ebusd/network.h @@ -26,7 +26,7 @@ class Network : public Thread { public: - Network(const bool localhost, WQueue* msgQueue); + Network(const bool localhost, WQueue* netQueue); ~Network(); void* run(); @@ -34,7 +34,7 @@ public: private: std::list m_connections; - WQueue* m_msgQueue; + WQueue* m_netQueue; TCPServer* m_tcpServer; Notify m_notify; bool m_listening; diff --git a/src/lib/utils/logger.cpp b/src/lib/utils/logger.cpp index 12fb49f8..8b05a1bc 100644 --- a/src/lib/utils/logger.cpp +++ b/src/lib/utils/logger.cpp @@ -28,10 +28,10 @@ #include #include -/** */ +/** static char array with logging area names */ static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus" }; -/** */ +/** static char array with logging level names */ static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "debug" }; int calcAreas(const std::string areas) @@ -92,17 +92,17 @@ LogMessage::LogMessage(const int area, const int level, const std::string text, void LogSink::addMessage(const LogMessage& message) { LogMessage* tmp = new LogMessage(LogMessage(message)); - m_logMessages.add((tmp)); + m_logQueue.add((tmp)); } void* LogSink::run() { while (1) { - LogMessage* message = m_logMessages.remove(); + LogMessage* message = m_logQueue.remove(); if (message->isRunning() == false) { delete message; - while (m_logMessages.size() == true) { - LogMessage* message = m_logMessages.remove(); + while (m_logQueue.size() == true) { + LogMessage* message = m_logQueue.remove(); write(*message); delete message; } @@ -189,7 +189,7 @@ void Logger::log(const int area, const int level, const std::string& data, ...) if (vasprintf(&tmp, data.c_str(), ap) != -1) { std::string buffer(tmp); - m_logMessages.add(new LogMessage(LogMessage(area, level, buffer))); + m_logQueue.add(new LogMessage(LogMessage(area, level, buffer))); } va_end(ap); @@ -203,7 +203,7 @@ void* Logger::run() m_running = true; while (m_running == true) { - LogMessage* message = m_logMessages.remove(); + LogMessage* message = m_logQueue.remove(); sinkCI_t iter = m_sinks.begin(); @@ -212,12 +212,13 @@ void* Logger::run() if (((*iter)->getAreas() & message->getArea() && (*iter)->getLevel() >= message->getLevel()) - && message->isRunning() == true) + && message->isRunning() == true) { (*iter)->addMessage(*message); - - else if (message->isRunning() == false) + } + else if (message->isRunning() == false) { (*iter)->addMessage(*message); m_running = false; + } } @@ -231,6 +232,6 @@ void* Logger::run() void Logger::stop() { - m_logMessages.add(new LogMessage(LogMessage(bas, error, "", false))); + m_logQueue.add(new LogMessage(LogMessage(bas, error, "", false))); usleep(100000); } diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index 10891949..6eb953f0 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -184,7 +184,7 @@ public: protected: /** queue for logging messages */ - WQueue m_logMessages; + WQueue m_logQueue; private: /** the logging areas */ @@ -328,7 +328,7 @@ private: sink_t m_sinks; /** queue for logging messages */ - WQueue m_logMessages; + WQueue m_logQueue; /** true if this instance is running */ bool m_running; From a5896e53ca59fa766373ee3ef504cda3b411870e Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 14:09:06 +0100 Subject: [PATCH 30/38] logging subsystem: needless functions removed. --- src/lib/utils/logger.cpp | 2 +- src/lib/utils/logger.h | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/lib/utils/logger.cpp b/src/lib/utils/logger.cpp index 8b05a1bc..706cd129 100644 --- a/src/lib/utils/logger.cpp +++ b/src/lib/utils/logger.cpp @@ -189,7 +189,7 @@ void Logger::log(const int area, const int level, const std::string& data, ...) if (vasprintf(&tmp, data.c_str(), ap) != -1) { std::string buffer(tmp); - m_logQueue.add(new LogMessage(LogMessage(area, level, buffer))); + m_logQueue.add(new LogMessage(area, level, buffer)); } va_end(ap); diff --git a/src/lib/utils/logger.h b/src/lib/utils/logger.h index 6eb953f0..f8f56b6b 100644 --- a/src/lib/utils/logger.h +++ b/src/lib/utils/logger.h @@ -68,22 +68,6 @@ public: */ LogMessage(const int area, const int level, const std::string text, const bool running=true); - /** - * @brief copy constructor to duplicate a logging message. - * @param src a reference to logging message. - */ - LogMessage(const LogMessage& src) - : m_area(src.m_area), m_level(src.m_level), m_text(src.m_text), - m_running(src.m_running), m_time(src.m_time) {} - - /** - * @brief copy operator to duplicate a logging message. - * @param src a reference to logging message. - */ - void operator=(const LogMessage& src) - { m_area = src.m_area; m_level = src.m_level; m_text = src.m_text; - m_running = src.m_running; m_time = src.m_time; } - /** * @brief get the logging area. * @return the logging area. From 5b14d6dbecf08e743df9ac8f01ff09433b39bdce Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 18:55:52 +0100 Subject: [PATCH 31/38] result queue of class Connection replaced with notification functionality of class NetMessage. --- src/ebusd/baseloop.cpp | 6 ++-- src/ebusd/connection.cpp | 15 +++------ src/ebusd/connection.h | 7 ++--- src/ebusd/netmessage.h | 66 ++++++++++++++++++++++++++++++++++------ 4 files changed, 64 insertions(+), 30 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 7e5f8a35..140054bb 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -85,10 +85,8 @@ void BaseLoop::start() // send result to client result += '\n'; - Connection* connection = message->getConnection(); - connection->addResult(NetMessage(result)); - - delete message; + message->setResult(result); + message->sendSignal(); // stop daemon if (strcasecmp(data.c_str(), "STOP") == 0) diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp index f591f4cf..239bb0a1 100644 --- a/src/ebusd/connection.cpp +++ b/src/ebusd/connection.cpp @@ -26,12 +26,6 @@ extern Logger& L; int Connection::m_sum = 0; -void Connection::addResult(NetMessage message) -{ - NetMessage* tmp = new NetMessage(NetMessage(message)); - m_netQueueResult.add(tmp); -} - void* Connection::run() { m_running = true; @@ -80,22 +74,21 @@ void* Connection::run() // send data data[datalen] = '\0'; - m_netQueueData->add(new NetMessage(data, this)); + NetMessage message(data); + m_netQueue->add(&message); // wait for result L.log(net, debug, "[%05d] wait for result", getID()); - NetMessage* message = m_netQueueResult.remove(); + message.waitSignal(); L.log(net, debug, "[%05d] result added", getID()); - std::string result(message->getData()); + std::string result = message.getResult(); if (m_socket->isValid() == true) m_socket->send(result.c_str(), result.size()); else break; - delete message; - } } diff --git a/src/ebusd/connection.h b/src/ebusd/connection.h index bf847d32..e860bc8c 100644 --- a/src/ebusd/connection.h +++ b/src/ebusd/connection.h @@ -31,9 +31,7 @@ class Connection : public Thread public: Connection(TCPSocket* socket, WQueue* netQueue) - : m_socket(socket), m_netQueueData(netQueue), m_running(false) { m_sum++; m_id = m_sum;} - - void addResult(NetMessage message); + : m_socket(socket), m_netQueue(netQueue), m_running(false) { m_sum++; m_id = m_sum;} void* run(); void stop() const { m_notify.notify(); } @@ -43,8 +41,7 @@ public: private: TCPSocket* m_socket; - WQueue* m_netQueueData; - WQueue m_netQueueResult; + WQueue* m_netQueue; Notify m_notify; bool m_running; int m_id; diff --git a/src/ebusd/netmessage.h b/src/ebusd/netmessage.h index 7b285392..5c3449ae 100644 --- a/src/ebusd/netmessage.h +++ b/src/ebusd/netmessage.h @@ -35,16 +35,27 @@ public: /** * @brief constructs a new instance with message and source client address. * @param data from client. - * @param connection to return result to correct client. */ - NetMessage(const std::string data, Connection* connection=NULL) - : m_data(data), m_connection(connection) {} + NetMessage(const std::string data) : m_data(data) + { + pthread_mutex_init(&m_mutex, NULL); + pthread_cond_init(&m_cond, NULL); + } + + /** + * @brief destructor. + */ + ~NetMessage() + { + pthread_mutex_destroy(&m_mutex); + pthread_cond_destroy(&m_cond); + } /** * @brief copy constructor. * @param src message object for copy. */ - NetMessage(const NetMessage& src) : m_data(src.m_data), m_connection(src.m_connection) {} + NetMessage(const NetMessage& src) : m_data(src.m_data) {} /** * @brief get the data string. @@ -53,17 +64,52 @@ public: std::string getData() const { return m_data; } /** - * @brief original connection. - * @return pointer to connection. + * @brief get the result string. + * @return the result string. */ - Connection* getConnection() const { return m_connection; } + std::string getResult() const { return m_result; } + + /** + * @brief set the result string. + * @return the result string. + */ + void setResult(const std::string result) { m_result = result; } + + /** + * @brief wait on notification. + */ + void waitSignal() + { + pthread_mutex_lock(&m_mutex); + + while (m_result.size() == 0) + pthread_cond_wait(&m_cond, &m_mutex); + + pthread_mutex_unlock(&m_mutex); + } + + /** + * @brief send notification. + */ + void sendSignal() + { + pthread_mutex_lock(&m_mutex); + pthread_cond_signal(&m_cond); + pthread_mutex_unlock(&m_mutex); + } private: - /** the data/message string */ + /** the data string */ std::string m_data; - /** the source connection */ - Connection* m_connection; + /** the result string */ + std::string m_result; + + /** mutex variable for exclusive lock */ + pthread_mutex_t m_mutex; + + /** condition variable for exclusive lock */ + pthread_cond_t m_cond; }; From a5605c40beb6f09994263770f0b388b54746aea5 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 19:44:04 +0100 Subject: [PATCH 32/38] namespace 'libebus' removed. --- src/ebusctl/ebusctl.cpp | 2 -- src/ebusd/baseloop.h | 3 --- src/ebusd/ebusd.cpp | 2 -- src/ebusd/ebusloop.h | 3 --- src/lib/ebus/buscommand.cpp | 6 ------ src/lib/ebus/buscommand.h | 11 ++--------- src/lib/ebus/command.cpp | 7 ------- src/lib/ebus/command.h | 7 ------- src/lib/ebus/commands.cpp | 7 ------- src/lib/ebus/commands.h | 8 +------- src/lib/ebus/configfile.cpp | 7 ------- src/lib/ebus/configfile.h | 7 +------ src/lib/ebus/data.cpp | 6 ------ src/lib/ebus/data.h | 7 ------- src/lib/ebus/decode.cpp | 7 ------- src/lib/ebus/decode.h | 7 ------- src/lib/ebus/dump.cpp | 7 ------- src/lib/ebus/dump.h | 6 ------ src/lib/ebus/encode.cpp | 7 ------- src/lib/ebus/encode.h | 7 ------- src/lib/ebus/port.cpp | 10 ---------- src/lib/ebus/port.h | 6 ------ src/lib/ebus/result.cpp | 7 +------ src/lib/ebus/result.h | 9 +-------- src/lib/ebus/symbol.cpp | 4 ---- src/lib/ebus/symbol.h | 7 ------- src/lib/ebus/test/test_commands.cpp | 2 -- src/lib/ebus/test/test_configfile.cpp | 3 --- src/lib/ebus/test/test_data.cpp | 10 ++++------ src/lib/ebus/test/test_decode.cpp | 4 ---- src/lib/ebus/test/test_encode.cpp | 4 ---- src/lib/ebus/test/test_port.cpp | 2 -- src/lib/ebus/test/test_symbol.cpp | 2 -- 33 files changed, 10 insertions(+), 184 deletions(-) diff --git a/src/ebusctl/ebusctl.cpp b/src/ebusctl/ebusctl.cpp index 0bf1596c..930e6ede 100644 --- a/src/ebusctl/ebusctl.cpp +++ b/src/ebusctl/ebusctl.cpp @@ -30,8 +30,6 @@ #include #include -using namespace libebus; - Appl& A = Appl::Instance(true); void define_args() diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index ec18540b..7ef032c9 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -24,9 +24,6 @@ #include "network.h" #include "ebusloop.h" -using namespace libebus; - - class BaseLoop { diff --git a/src/ebusd/ebusd.cpp b/src/ebusd/ebusd.cpp index b991cd7d..eb98fe9c 100644 --- a/src/ebusd/ebusd.cpp +++ b/src/ebusd/ebusd.cpp @@ -28,8 +28,6 @@ #include #include -using namespace libebus; - Appl& A = Appl::Instance(); Daemon& D = Daemon::Instance(); Logger& L = Logger::Instance(); diff --git a/src/ebusd/ebusloop.h b/src/ebusd/ebusloop.h index da42cb49..dc2a17fa 100644 --- a/src/ebusd/ebusloop.h +++ b/src/ebusd/ebusloop.h @@ -30,9 +30,6 @@ /** the maximum time [us] allowed for retrieving a byte from an addressed slave */ #define RECV_TIMEOUT 10000 -using namespace libebus; - - class EBusLoop : public Thread { diff --git a/src/lib/ebus/buscommand.cpp b/src/lib/ebus/buscommand.cpp index 61dcd702..9b25dca8 100644 --- a/src/lib/ebus/buscommand.cpp +++ b/src/lib/ebus/buscommand.cpp @@ -19,9 +19,6 @@ #include "buscommand.h" -namespace libebus -{ - BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool scan) : m_poll(poll), m_scan(scan), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) { @@ -64,6 +61,3 @@ const std::string BusCommand::getMessageStr() return result; } - -} //namespace - diff --git a/src/lib/ebus/buscommand.h b/src/lib/ebus/buscommand.h index 8116d00c..4da99666 100644 --- a/src/lib/ebus/buscommand.h +++ b/src/lib/ebus/buscommand.h @@ -23,11 +23,6 @@ #include "symbol.h" #include "result.h" - -namespace libebus -{ - - enum CommandType { invalid, broadcast, masterMaster, masterSlave }; class BusCommand @@ -45,7 +40,7 @@ public: SymbolString getResult() const { return m_result; } bool isErrorResult() const { return m_resultCode < 0; } - const char* getResultCodeCStr() const { return libebus::getResultCodeCStr(m_resultCode); } + const char* getResultCodeCStr() const { return getResultCode(m_resultCode); } void setResult(const SymbolString result, const int resultCode) { m_result = result; m_resultCode = resultCode; } @@ -66,7 +61,5 @@ private: pthread_cond_t m_cond; }; - -} //namespace - #endif // LIBEBUS_BUSCOMMAND_H_ + diff --git a/src/lib/ebus/command.cpp b/src/lib/ebus/command.cpp index 8c475d34..795a3740 100644 --- a/src/lib/ebus/command.cpp +++ b/src/lib/ebus/command.cpp @@ -27,10 +27,6 @@ #include #include -namespace libebus -{ - - std::string Command::calcData() { // encode - only first entry will be encoded @@ -340,6 +336,3 @@ void Command::encode(const std::string& data, const std::string& type, delete help; } - -} //namespace - diff --git a/src/lib/ebus/command.h b/src/lib/ebus/command.h index 91bebf53..2c725ce4 100644 --- a/src/lib/ebus/command.h +++ b/src/lib/ebus/command.h @@ -23,10 +23,6 @@ #include #include -namespace libebus -{ - - typedef std::vector cmd_t; typedef cmd_t::const_iterator cmdCI_t; @@ -63,7 +59,4 @@ private: }; - -} //namespace - #endif // LIBEBUS_COMMAND_H_ diff --git a/src/lib/ebus/commands.cpp b/src/lib/ebus/commands.cpp index 6fdb34d0..dc921734 100644 --- a/src/lib/ebus/commands.cpp +++ b/src/lib/ebus/commands.cpp @@ -25,10 +25,6 @@ #include #include -namespace libebus -{ - - Commands::~Commands() { for (mapCI_t iter = m_pollDB.begin(); iter != m_pollDB.end(); ++iter) @@ -260,6 +256,3 @@ void Commands::printCommand(const cmd_t& command) const std::cout << *i << ';'; } - -} //namespace - diff --git a/src/lib/ebus/commands.h b/src/lib/ebus/commands.h index cf97ae39..dc111dc9 100644 --- a/src/lib/ebus/commands.h +++ b/src/lib/ebus/commands.h @@ -25,10 +25,6 @@ #include #include -namespace libebus -{ - - typedef std::vector cmdDB_t; typedef cmdDB_t::const_iterator cmdDBCI_t; @@ -79,7 +75,5 @@ private: }; - -} //namespace - #endif // LIBEBUS_COMMANDS_H_ + diff --git a/src/lib/ebus/configfile.cpp b/src/lib/ebus/configfile.cpp index 0520580c..f630bac5 100644 --- a/src/lib/ebus/configfile.cpp +++ b/src/lib/ebus/configfile.cpp @@ -22,10 +22,6 @@ #include #include -namespace libebus -{ - - void ConfigFileCSV::parse(std::istream& is, Commands& commands) { std::string line; @@ -132,6 +128,3 @@ void ConfigCommands::addFiles(const std::string path, const std::string extensio closedir(dir); }; - -} //namespace - diff --git a/src/lib/ebus/configfile.h b/src/lib/ebus/configfile.h index 9be9a622..d800c096 100644 --- a/src/lib/ebus/configfile.h +++ b/src/lib/ebus/configfile.h @@ -24,9 +24,6 @@ #include #include -namespace libebus -{ - /** available file endings / types. */ enum FileType { CSV, XML }; @@ -147,7 +144,5 @@ private: }; - -} //namespace - #endif // LIBEBUS_CONFIGFILE_H_ + diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index ac409c23..6127b003 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -27,9 +27,6 @@ #include #include -namespace libebus -{ - /** the known data field types. */ static const dataType_t dataTypes[] = { {"STR", 16, bt_str, ADJ, ' ', 1, 16, 0}, // >= 1 byte character string filled up with space @@ -658,6 +655,3 @@ result_t ValueListDataField::writeSymbols(std::istringstream& input, SymbolStrin return RESULT_ERR_INVALID_ARG; // value assignment not found } - -} //namespace - diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index e0e8dabe..4291997c 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -26,10 +26,6 @@ #include #include -namespace libebus -{ - - /** the message part in which a data field is stored. */ enum PartType { pt_masterData, // stored in master data @@ -315,7 +311,4 @@ protected: }; - -} //namespace - #endif // LIBEBUS_DATA_H_ diff --git a/src/lib/ebus/decode.cpp b/src/lib/ebus/decode.cpp index e8fd7a9f..49ca9e22 100644 --- a/src/lib/ebus/decode.cpp +++ b/src/lib/ebus/decode.cpp @@ -26,10 +26,6 @@ #include #include -namespace libebus -{ - - Decode::Decode(const std::string& data, const std::string& factor) : m_data(data) { @@ -347,6 +343,3 @@ std::string DecodeTTM::decode() return result.str(); } - -} //namespace - diff --git a/src/lib/ebus/decode.h b/src/lib/ebus/decode.h index d37cefe3..da1d087f 100644 --- a/src/lib/ebus/decode.h +++ b/src/lib/ebus/decode.h @@ -22,10 +22,6 @@ #include -namespace libebus -{ - - class Decode { @@ -285,7 +281,4 @@ public: }; - -} //namespace - #endif // LIBEBUS_DECODE_H_ diff --git a/src/lib/ebus/dump.cpp b/src/lib/ebus/dump.cpp index 44104f0e..258eb13f 100644 --- a/src/lib/ebus/dump.cpp +++ b/src/lib/ebus/dump.cpp @@ -21,9 +21,6 @@ #include #include -namespace libebus -{ - int Dump::write(const char* byte) { int ret = 0; @@ -47,7 +44,3 @@ int Dump::write(const char* byte) return ret; } - - -} //namespace - diff --git a/src/lib/ebus/dump.h b/src/lib/ebus/dump.h index e64c6356..af459dcf 100644 --- a/src/lib/ebus/dump.h +++ b/src/lib/ebus/dump.h @@ -22,9 +22,6 @@ #include -namespace libebus -{ - /** * @brief Class for writing raw bytes to binary file. */ @@ -68,7 +65,4 @@ private: }; - -} //namespace - #endif // LIBEBUS_DUMP_H_ diff --git a/src/lib/ebus/encode.cpp b/src/lib/ebus/encode.cpp index 76d55015..37d14eb5 100644 --- a/src/lib/ebus/encode.cpp +++ b/src/lib/ebus/encode.cpp @@ -26,10 +26,6 @@ #include #include -namespace libebus -{ - - Encode::Encode(const std::string& data, const std::string& factor) : m_data(data) { @@ -364,6 +360,3 @@ std::string EncodeTTM::encode() return result.str(); } - -} //namespace - diff --git a/src/lib/ebus/encode.h b/src/lib/ebus/encode.h index 7730054b..7d87d45d 100644 --- a/src/lib/ebus/encode.h +++ b/src/lib/ebus/encode.h @@ -22,10 +22,6 @@ #include -namespace libebus -{ - - class Encode { @@ -285,7 +281,4 @@ public: }; - -} //namespace - #endif // LIBEBUS_ENCODE_H_ diff --git a/src/lib/ebus/port.cpp b/src/lib/ebus/port.cpp index 132ee039..cf941aa8 100644 --- a/src/lib/ebus/port.cpp +++ b/src/lib/ebus/port.cpp @@ -26,11 +26,6 @@ #include #include - -namespace libebus -{ - - bool Device::isOpen() { if (isValid() == false) @@ -257,8 +252,3 @@ void Port::setType(const DeviceType type) }; }; - -} //namespace - - - diff --git a/src/lib/ebus/port.h b/src/lib/ebus/port.h index edcf1889..213bf5a4 100644 --- a/src/lib/ebus/port.h +++ b/src/lib/ebus/port.h @@ -25,9 +25,6 @@ #include #include -namespace libebus -{ - /** available device types. */ enum DeviceType { SERIAL, NETWORK }; @@ -267,7 +264,4 @@ private: }; - -} //namespace - #endif // LIBEBUS_PORT_H_ diff --git a/src/lib/ebus/result.cpp b/src/lib/ebus/result.cpp index 9fa53faf..eeb2787e 100644 --- a/src/lib/ebus/result.cpp +++ b/src/lib/ebus/result.cpp @@ -19,10 +19,7 @@ #include "result.h" -namespace libebus -{ - -const char* getResultCodeCStr(result_t resultCode) { +const char* getResultCode(result_t resultCode) { switch (resultCode) { case RESULT_ERR_SEND: return "ERR_SEND: send error"; case RESULT_ERR_EXTRA_DATA: return "ERR_EXTRA_DATA: received bytes > sent bytes"; @@ -43,5 +40,3 @@ const char* getResultCodeCStr(result_t resultCode) { } } - -} //namespace diff --git a/src/lib/ebus/result.h b/src/lib/ebus/result.h index 0298e62f..df1af2d2 100644 --- a/src/lib/ebus/result.h +++ b/src/lib/ebus/result.h @@ -20,10 +20,6 @@ #ifndef LIBEBUS_RESULT_H_ #define LIBEBUS_RESULT_H_ -namespace libebus -{ - - static const int RESULT_OK = 0; static const int RESULT_BUS_ACQUIRED = 1; // bus successfully acquired @@ -54,9 +50,6 @@ typedef int result_t; * @param resultCode the result code (see RESULT_ constants). * @return the string corresponding to the result code. */ -const char* getResultCodeCStr(result_t resultCode); - - -} //namespace +const char* getResultCode(result_t resultCode); #endif // LIBEBUS_RESULT_H_ diff --git a/src/lib/ebus/symbol.cpp b/src/lib/ebus/symbol.cpp index 05d09ab1..c98027f6 100644 --- a/src/lib/ebus/symbol.cpp +++ b/src/lib/ebus/symbol.cpp @@ -22,9 +22,6 @@ #include #include -namespace libebus -{ - /** * @brief CRC8 lookup table for the polynom 0x9b = x^8 + x^7 + x^4 + x^3 + x^1 + 1. */ @@ -190,4 +187,3 @@ bool isMaster(unsigned char addr) { && ((addrLo == 0x0) || (addrLo == 0x1) || (addrLo == 0x3) || (addrLo == 0x7) || (addrLo == 0xF)); } -} //namespace diff --git a/src/lib/ebus/symbol.h b/src/lib/ebus/symbol.h index a340b930..98b74d3d 100644 --- a/src/lib/ebus/symbol.h +++ b/src/lib/ebus/symbol.h @@ -25,10 +25,6 @@ #include #include -namespace libebus -{ - - static const unsigned char ESC = 0xA9; // escape symbol, either followed by 0x00 for the value 0xA9, or 0x01 for the value 0xAA static const unsigned char SYN = 0xAA; // synchronization symbol static const unsigned char ACK = 0x00; // positive acknowledge @@ -139,7 +135,4 @@ private: */ bool isMaster(unsigned char addr); - -} //namespace - #endif // LIBEBUS_SYMBOL_H_ diff --git a/src/lib/ebus/test/test_commands.cpp b/src/lib/ebus/test/test_commands.cpp index e51ed79b..befbd46a 100644 --- a/src/lib/ebus/test/test_commands.cpp +++ b/src/lib/ebus/test/test_commands.cpp @@ -25,8 +25,6 @@ #include #include -using namespace libebus; - // will be part of cfg csv class void readCSV(std::istream& is, Commands& commands){ std::string line; diff --git a/src/lib/ebus/test/test_configfile.cpp b/src/lib/ebus/test/test_configfile.cpp index ce46f3d5..beac4fb5 100644 --- a/src/lib/ebus/test/test_configfile.cpp +++ b/src/lib/ebus/test/test_configfile.cpp @@ -24,9 +24,6 @@ #include #include -using namespace libebus; - - int main() { std::string dir("test"); diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index c2ff3e70..7d268b00 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -21,8 +21,6 @@ #include #include -using namespace libebus; - int main () { std::string checks[][4] = { @@ -72,7 +70,7 @@ int main () result_t result = DataField::create(mstr[1], false, it, entries.end(), predefined, fields, nextPos); if (result != RESULT_OK) { - std::cout << "create \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; + std::cout << "create \"" << checks[i][0] << "\" failed: " << getResultCode(result) << std::endl; continue; } if (fields.empty() == true) { @@ -86,7 +84,7 @@ int main () std::ostringstream output; result = field->read(mstr, sstr, output); if (result != RESULT_OK) { - std::cout << "read \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; + std::cout << "read \"" << checks[i][0] << "\" failed: " << getResultCode(result) << std::endl; } else { std::string gotStr = output.str(); @@ -101,12 +99,12 @@ int main () SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false); if (result != RESULT_OK) { - std::cout << "create \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; + std::cout << "create \"" << checks[i][0] << "\" failed: " << getResultCode(result) << std::endl; return 1; } result = field->write(gotStr, writeMstr, writeSstr); if (result != RESULT_OK) { - std::cout << "write \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; + std::cout << "write \"" << checks[i][0] << "\" failed: " << getResultCode(result) << std::endl; } else { if (mstr == writeMstr && sstr == writeSstr) diff --git a/src/lib/ebus/test/test_decode.cpp b/src/lib/ebus/test/test_decode.cpp index 3da4e9ef..d9df8858 100644 --- a/src/lib/ebus/test/test_decode.cpp +++ b/src/lib/ebus/test/test_decode.cpp @@ -21,10 +21,6 @@ #include #include - -using namespace libebus; - - int main() { Decode* help_dec = NULL; diff --git a/src/lib/ebus/test/test_encode.cpp b/src/lib/ebus/test/test_encode.cpp index f7a65fd4..5cc97abe 100644 --- a/src/lib/ebus/test/test_encode.cpp +++ b/src/lib/ebus/test/test_encode.cpp @@ -21,10 +21,6 @@ #include #include - -using namespace libebus; - - int main() { Encode* help_enc = NULL; diff --git a/src/lib/ebus/test/test_port.cpp b/src/lib/ebus/test/test_port.cpp index 9bdc12cb..2aae9808 100644 --- a/src/lib/ebus/test/test_port.cpp +++ b/src/lib/ebus/test/test_port.cpp @@ -21,8 +21,6 @@ #include #include -using namespace libebus; - int main () { std::string dev("/dev/ttyUSB20"); diff --git a/src/lib/ebus/test/test_symbol.cpp b/src/lib/ebus/test/test_symbol.cpp index b68ef9df..0ca984a7 100644 --- a/src/lib/ebus/test/test_symbol.cpp +++ b/src/lib/ebus/test/test_symbol.cpp @@ -21,8 +21,6 @@ #include #include -using namespace libebus; - int main () { SymbolString sstr = SymbolString("10feb5050427a915aa"); From 31246e14312817696791cbe6c02873e2609a0bf1 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 21:04:47 +0100 Subject: [PATCH 33/38] documentation for class Network and Connection added; classes for network subsystem integrated into one network file. --- src/ebusd/Makefile.am | 5 +- src/ebusd/connection.cpp | 102 -------------------- src/ebusd/connection.h | 53 ----------- src/ebusd/netmessage.h | 117 ----------------------- src/ebusd/network.cpp | 81 +++++++++++++++- src/ebusd/network.h | 194 ++++++++++++++++++++++++++++++++++++++- 6 files changed, 272 insertions(+), 280 deletions(-) delete mode 100644 src/ebusd/connection.cpp delete mode 100644 src/ebusd/connection.h delete mode 100644 src/ebusd/netmessage.h diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index 804fd608..c1be62df 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -6,15 +6,12 @@ AM_CXXFLAGS = -fpic \ bin_PROGRAMS = ebusd -ebusd_SOURCES = connection.cpp \ - connection.h \ - network.cpp \ +ebusd_SOURCES = network.cpp \ network.h \ ebusloop.cpp \ ebusloop.h \ baseloop.cpp \ baseloop.h \ - netmessage.h \ ebusd.cpp ebusd_LDADD = $(top_srcdir)/src/lib/utils/libutils.a \ diff --git a/src/ebusd/connection.cpp b/src/ebusd/connection.cpp deleted file mode 100644 index 239bb0a1..00000000 --- a/src/ebusd/connection.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 - * - * This file is part of ebusd. - * - * ebusd is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ebusd is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with ebusd. If not, see http://www.gnu.org/licenses/. - */ - -#include "connection.h" -#include "logger.h" -#include -#include - -extern Logger& L; - -int Connection::m_sum = 0; - -void* Connection::run() -{ - m_running = true; - - int ret, nfds = 2; - struct pollfd fds[nfds]; - struct timespec tdiff; - - // set select timeout 10 secs - tdiff.tv_sec = 10; - tdiff.tv_nsec = 0; - - memset(fds, 0, sizeof(fds)); - - fds[0].fd = m_notify.notifyFD(); - fds[0].events = POLLIN; - - fds[1].fd = m_socket->getFD(); - fds[1].events = POLLIN; - - for (;;) { - // wait for new fd event - ret = ppoll(fds, nfds, &tdiff, NULL); - - if (ret == 0) { - continue; - } - - // new data from notify - if (fds[0].revents & POLLIN) - break; - - // new data from socket - if (fds[1].revents & POLLIN) { - char data[256]; - size_t datalen; - - if (m_socket->isValid() == true) - datalen = m_socket->recv(data, sizeof(data)-1); - else - break; - - // removed closed socket - if (datalen <= 0 || strcasecmp(data, "QUIT") == 0) - break; - - // send data - data[datalen] = '\0'; - NetMessage message(data); - m_netQueue->add(&message); - - // wait for result - L.log(net, debug, "[%05d] wait for result", getID()); - message.waitSignal(); - - L.log(net, debug, "[%05d] result added", getID()); - std::string result = message.getResult(); - - if (m_socket->isValid() == true) - m_socket->send(result.c_str(), result.size()); - else - break; - - } - - } - - delete m_socket; - m_running = false; - L.log(net, trace, "[%05d] connection closed", getID()); - - return NULL; -} - diff --git a/src/ebusd/connection.h b/src/ebusd/connection.h deleted file mode 100644 index e860bc8c..00000000 --- a/src/ebusd/connection.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 - * - * This file is part of ebusd. - * - * ebusd is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ebusd is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with ebusd. If not, see http://www.gnu.org/licenses/. - */ - -#ifndef CONNECTION_H_ -#define CONNECTION_H_ - -#include "tcpsocket.h" -#include "wqueue.h" -#include "notify.h" -#include "thread.h" -#include "netmessage.h" - -class Connection : public Thread -{ - -public: - Connection(TCPSocket* socket, WQueue* netQueue) - : m_socket(socket), m_netQueue(netQueue), m_running(false) { m_sum++; m_id = m_sum;} - - void* run(); - void stop() const { m_notify.notify(); } - bool isRunning() const { return m_running; } - - int getID() { return m_id; } - -private: - TCPSocket* m_socket; - WQueue* m_netQueue; - Notify m_notify; - bool m_running; - int m_id; - - static int m_sum; - -}; - -#endif // CONNECTION_H_ diff --git a/src/ebusd/netmessage.h b/src/ebusd/netmessage.h deleted file mode 100644 index 5c3449ae..00000000 --- a/src/ebusd/netmessage.h +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 - * - * This file is part of ebusd. - * - * ebusd is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ebusd is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with ebusd. If not, see http://www.gnu.org/licenses/. - */ - -#ifndef NETMESSAGE_H_ -#define NETMESSAGE_H_ - -#include - -/** forward declaration for class Connection */ -class Connection; - -/** - * @brief class for data/message transfer between connection and baseloop. - */ -class NetMessage -{ - -public: - /** - * @brief constructs a new instance with message and source client address. - * @param data from client. - */ - NetMessage(const std::string data) : m_data(data) - { - pthread_mutex_init(&m_mutex, NULL); - pthread_cond_init(&m_cond, NULL); - } - - /** - * @brief destructor. - */ - ~NetMessage() - { - pthread_mutex_destroy(&m_mutex); - pthread_cond_destroy(&m_cond); - } - - /** - * @brief copy constructor. - * @param src message object for copy. - */ - NetMessage(const NetMessage& src) : m_data(src.m_data) {} - - /** - * @brief get the data string. - * @return the data string. - */ - std::string getData() const { return m_data; } - - /** - * @brief get the result string. - * @return the result string. - */ - std::string getResult() const { return m_result; } - - /** - * @brief set the result string. - * @return the result string. - */ - void setResult(const std::string result) { m_result = result; } - - /** - * @brief wait on notification. - */ - void waitSignal() - { - pthread_mutex_lock(&m_mutex); - - while (m_result.size() == 0) - pthread_cond_wait(&m_cond, &m_mutex); - - pthread_mutex_unlock(&m_mutex); - } - - /** - * @brief send notification. - */ - void sendSignal() - { - pthread_mutex_lock(&m_mutex); - pthread_cond_signal(&m_cond); - pthread_mutex_unlock(&m_mutex); - } - -private: - /** the data string */ - std::string m_data; - - /** the result string */ - std::string m_result; - - /** mutex variable for exclusive lock */ - pthread_mutex_t m_mutex; - - /** condition variable for exclusive lock */ - pthread_cond_t m_cond; - -}; - - -#endif // NETMESSAGE_H_ diff --git a/src/ebusd/network.cpp b/src/ebusd/network.cpp index 92b18877..99dd0092 100644 --- a/src/ebusd/network.cpp +++ b/src/ebusd/network.cpp @@ -20,16 +20,93 @@ #include "network.h" #include "logger.h" #include "appl.h" +#include #include extern Logger& L; extern Appl& A; +int Connection::m_ids = 0; -Network::Network(const bool localhost, WQueue* netQueue) +void* Connection::run() +{ + m_running = true; + + int ret, nfds = 2; + struct pollfd fds[nfds]; + struct timespec tdiff; + + // set select timeout 10 secs + tdiff.tv_sec = 10; + tdiff.tv_nsec = 0; + + memset(fds, 0, sizeof(fds)); + + fds[0].fd = m_notify.notifyFD(); + fds[0].events = POLLIN; + + fds[1].fd = m_socket->getFD(); + fds[1].events = POLLIN; + + for (;;) { + // wait for new fd event + ret = ppoll(fds, nfds, &tdiff, NULL); + + if (ret == 0) { + continue; + } + + // new data from notify + if (fds[0].revents & POLLIN) + break; + + // new data from socket + if (fds[1].revents & POLLIN) { + char data[256]; + size_t datalen; + + if (m_socket->isValid() == true) + datalen = m_socket->recv(data, sizeof(data)-1); + else + break; + + // removed closed socket + if (datalen <= 0 || strcasecmp(data, "QUIT") == 0) + break; + + // send data + data[datalen] = '\0'; + NetMessage message(data); + m_netQueue->add(&message); + + // wait for result + L.log(net, debug, "[%05d] wait for result", getID()); + message.waitSignal(); + + L.log(net, debug, "[%05d] result added", getID()); + std::string result = message.getResult(); + + if (m_socket->isValid() == true) + m_socket->send(result.c_str(), result.size()); + else + break; + + } + + } + + delete m_socket; + m_running = false; + L.log(net, trace, "[%05d] connection closed", getID()); + + return NULL; +} + + +Network::Network(const bool local, WQueue* netQueue) : m_netQueue(netQueue), m_listening(false), m_running(false) { - if (localhost == true) + if (local == true) m_tcpServer = new TCPServer(A.getOptVal("port"), "127.0.0.1"); else m_tcpServer = new TCPServer(A.getOptVal("port"), "0.0.0.0"); diff --git a/src/ebusd/network.h b/src/ebusd/network.h index 2805ec95..2f25b46e 100644 --- a/src/ebusd/network.h +++ b/src/ebusd/network.h @@ -20,28 +20,218 @@ #ifndef NETWORK_H_ #define NETWORK_H_ -#include "connection.h" +#include "tcpsocket.h" +#include "wqueue.h" +#include "notify.h" +#include "thread.h" +#include +/** forward declaration for class connection */ +class Connection; + +/** + * @brief class for data/message transfer between connection and baseloop. + */ +class NetMessage +{ + +public: + /** + * @brief constructs a new instance with message and source client address. + * @param data from client. + */ + NetMessage(const std::string data) : m_data(data) + { + pthread_mutex_init(&m_mutex, NULL); + pthread_cond_init(&m_cond, NULL); + } + + /** + * @brief destructor. + */ + ~NetMessage() + { + pthread_mutex_destroy(&m_mutex); + pthread_cond_destroy(&m_cond); + } + + /** + * @brief copy constructor. + * @param src message object for copy. + */ + NetMessage(const NetMessage& src) : m_data(src.m_data) {} + + /** + * @brief get the data string. + * @return the data string. + */ + std::string getData() const { return m_data; } + + /** + * @brief get the result string. + * @return the result string. + */ + std::string getResult() const { return m_result; } + + /** + * @brief set the result string. + * @return the result string. + */ + void setResult(const std::string result) { m_result = result; } + + /** + * @brief wait on notification. + */ + void waitSignal() + { + pthread_mutex_lock(&m_mutex); + + while (m_result.size() == 0) + pthread_cond_wait(&m_cond, &m_mutex); + + pthread_mutex_unlock(&m_mutex); + } + + /** + * @brief send notification. + */ + void sendSignal() + { + pthread_mutex_lock(&m_mutex); + pthread_cond_signal(&m_cond); + pthread_mutex_unlock(&m_mutex); + } + +private: + /** the data string */ + std::string m_data; + + /** the result string */ + std::string m_result; + + /** mutex variable for exclusive lock */ + pthread_mutex_t m_mutex; + + /** condition variable for exclusive lock */ + pthread_cond_t m_cond; + +}; + +/** + * @brief class connection which handle client and baseloop communication. + */ +class Connection : public Thread +{ + +public: + /** + * @brief create a new connection instance. + * @param socket the tcp socket for communication. + * @param netQueue the remote queue for network messages. + */ + Connection(TCPSocket* socket, WQueue* netQueue) + : m_socket(socket), m_netQueue(netQueue), m_running(false) + { m_id = ++m_ids; } + + /** + * @brief endless loop for connection instance. + * @return void pointer. + */ + void* run(); + + /** + * @brief closs active connection. + */ + void stop() const { m_notify.notify(); } + + /** + * @brief status of connection instance. + * @return true if connection is running. + */ + bool isRunning() const { return m_running; } + + /** + * @brief return own connection id. + * @return id of current connection. + */ + int getID() { return m_id; } + +private: + /** the tcp socket instance */ + TCPSocket* m_socket; + + /** remote queue for network messages */ + WQueue* m_netQueue; + + /** notification object for shutdown procedure */ + Notify m_notify; + + /** true if this instance is running */ + bool m_running; + + /** id of current connection*/ + int m_id; + + /** sumary for opened connections */ + static int m_ids; + +}; + +/** + * @brief class network which listening on tcp socket for incoming connections. + */ class Network : public Thread { public: - Network(const bool localhost, WQueue* netQueue); + /** + * @brief create a new network instance and listening for incoming connections. + * @param local true to accept connections only for local host. + * @param netQueue the remote queue for network messages. + */ + Network(const bool local, WQueue* netQueue); + + /** + * @brief destructor. + */ ~Network(); + /** + * @brief endless loop for network instance. + * @return void pointer. + */ void* run(); + + /** + * @brief shutdown network subsystem. + */ void stop() const { m_notify.notify(); usleep(100000); } private: + /** container for active connections */ std::list m_connections; + + /** remote queue for network messages */ WQueue* m_netQueue; + + /** the tcp server instance */ TCPServer* m_tcpServer; + + /** notification object for shutdown procedure */ Notify m_notify; + + /** true if this instance is listening */ bool m_listening; + + /** true if this instance is running */ bool m_running; + /** + * @brief clean inactive connections from container. + */ void cleanConnections(); }; #endif // NETWORK_H_ + From 85c00c9cc73965fbfa2c76710400f2303727f431 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 21:34:52 +0100 Subject: [PATCH 34/38] files buscommand.x and dump.x moved from src/lib/ebus to src/ebusd. --- src/ebusd/Makefile.am | 4 ++++ src/{lib/ebus => ebusd}/buscommand.cpp | 0 src/{lib/ebus => ebusd}/buscommand.h | 6 +++--- src/{lib/ebus => ebusd}/dump.cpp | 0 src/{lib/ebus => ebusd}/dump.h | 6 +++--- src/lib/ebus/Makefile.am | 4 ---- 6 files changed, 10 insertions(+), 10 deletions(-) rename src/{lib/ebus => ebusd}/buscommand.cpp (100%) rename src/{lib/ebus => ebusd}/buscommand.h (95%) rename src/{lib/ebus => ebusd}/dump.cpp (100%) rename src/{lib/ebus => ebusd}/dump.h (96%) diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index c1be62df..8f1db832 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -8,6 +8,10 @@ bin_PROGRAMS = ebusd ebusd_SOURCES = network.cpp \ network.h \ + dump.cpp \ + dump.h \ + buscommand.cpp \ + buscommand.h \ ebusloop.cpp \ ebusloop.h \ baseloop.cpp \ diff --git a/src/lib/ebus/buscommand.cpp b/src/ebusd/buscommand.cpp similarity index 100% rename from src/lib/ebus/buscommand.cpp rename to src/ebusd/buscommand.cpp diff --git a/src/lib/ebus/buscommand.h b/src/ebusd/buscommand.h similarity index 95% rename from src/lib/ebus/buscommand.h rename to src/ebusd/buscommand.h index 4da99666..e454e74f 100644 --- a/src/lib/ebus/buscommand.h +++ b/src/ebusd/buscommand.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBEBUS_BUSCOMMAND_H_ -#define LIBEBUS_BUSCOMMAND_H_ +#ifndef BUSCOMMAND_H_ +#define BUSCOMMAND_H_ #include "symbol.h" #include "result.h" @@ -61,5 +61,5 @@ private: pthread_cond_t m_cond; }; -#endif // LIBEBUS_BUSCOMMAND_H_ +#endif // BUSCOMMAND_H_ diff --git a/src/lib/ebus/dump.cpp b/src/ebusd/dump.cpp similarity index 100% rename from src/lib/ebus/dump.cpp rename to src/ebusd/dump.cpp diff --git a/src/lib/ebus/dump.h b/src/ebusd/dump.h similarity index 96% rename from src/lib/ebus/dump.h rename to src/ebusd/dump.h index af459dcf..5abbe5f1 100644 --- a/src/lib/ebus/dump.h +++ b/src/ebusd/dump.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef LIBEBUS_DUMP_H_ -#define LIBEBUS_DUMP_H_ +#ifndef DUMP_H_ +#define DUMP_H_ #include @@ -65,4 +65,4 @@ private: }; -#endif // LIBEBUS_DUMP_H_ +#endif // DUMP_H_ diff --git a/src/lib/ebus/Makefile.am b/src/lib/ebus/Makefile.am index 64fa53d9..570375bf 100644 --- a/src/lib/ebus/Makefile.am +++ b/src/lib/ebus/Makefile.am @@ -12,16 +12,12 @@ libebus_a_SOURCES = result.cpp \ data.h \ port.cpp \ port.h \ - buscommand.cpp \ - buscommand.h \ command.cpp \ command.h \ commands.cpp \ commands.h \ configfile.cpp \ configfile.h \ - dump.cpp \ - dump.h \ decode.cpp \ decode.h \ encode.cpp \ From 3165342057adfd7dbc67f32954ce9c38e7a68900 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 21:53:11 +0100 Subject: [PATCH 35/38] class EBusLoop renamed to BusLoop; class Dump cleared. --- src/ebusd/Makefile.am | 10 ++++---- src/ebusd/baseloop.cpp | 32 ++++++++++++------------- src/ebusd/baseloop.h | 4 ++-- src/ebusd/{ebusloop.cpp => busloop.cpp} | 30 +++++++++++------------ src/ebusd/{ebusloop.h => busloop.h} | 12 +++++----- src/ebusd/dump.cpp | 8 +++---- src/ebusd/dump.h | 28 +++++++++++----------- 7 files changed, 62 insertions(+), 62 deletions(-) rename src/ebusd/{ebusloop.cpp => busloop.cpp} (96%) rename src/ebusd/{ebusloop.h => busloop.h} (94%) diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index 8f1db832..c39e79da 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -6,14 +6,14 @@ AM_CXXFLAGS = -fpic \ bin_PROGRAMS = ebusd -ebusd_SOURCES = network.cpp \ - network.h \ - dump.cpp \ +ebusd_SOURCES = dump.cpp \ dump.h \ buscommand.cpp \ buscommand.h \ - ebusloop.cpp \ - ebusloop.h \ + busloop.cpp \ + busloop.h \ + network.cpp \ + network.h \ baseloop.cpp \ baseloop.h \ ebusd.cpp diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 140054bb..228a92e7 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -34,9 +34,9 @@ BaseLoop::BaseLoop() L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB()); L.log(bas, event, " polling DB: %d ", m_commands->sizePollDB()); - // create ebusloop - m_ebusloop = new EBusLoop(m_commands); - m_ebusloop->start("ebusloop"); + // create busloop + m_busloop = new BusLoop(m_commands); + m_busloop->start("busloop"); // create network m_network = new Network(A.getOptVal("localhost"), &m_netQueue); @@ -49,11 +49,11 @@ BaseLoop::~BaseLoop() if (m_network != NULL) delete m_network; - // free ebusloop - if (m_ebusloop != NULL) { - m_ebusloop->stop(); - m_ebusloop->join(); - delete m_ebusloop; + // free busloop + if (m_busloop != NULL) { + m_busloop->stop(); + m_busloop->join(); + delete m_busloop; } // free commands DB @@ -152,7 +152,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) BusCommand* busCommand = new BusCommand(ebusCommand, false, false); L.log(bas, trace, " msg: %s", ebusCommand.c_str()); // send busCommand - m_ebusloop->addBusCommand(busCommand); + m_busloop->addBusCommand(busCommand); busCommand->waitSignal(); if (!busCommand->isErrorResult()) { @@ -205,7 +205,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) BusCommand* busCommand = new BusCommand(ebusCommand, false, false); L.log(bas, event, " msg: %s", ebusCommand.c_str()); // send busCommand - m_ebusloop->addBusCommand(busCommand); + m_busloop->addBusCommand(busCommand); busCommand->waitSignal(); if (!busCommand->isErrorResult()) { @@ -274,7 +274,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) BusCommand* busCommand = new BusCommand(ebusCommand, false, false); L.log(bas, trace, " msg: %s", ebusCommand.c_str()); // send busCommand - m_ebusloop->addBusCommand(busCommand); + m_busloop->addBusCommand(busCommand); busCommand->waitSignal(); if (busCommand->isErrorResult()) { @@ -291,13 +291,13 @@ std::string BaseLoop::decodeMessage(const std::string& data) case scan: if (cmd.size() == 1) { - m_ebusloop->scan(); + m_busloop->scan(); result << "done"; break; } if (strcasecmp(cmd[1].c_str(), "FULL") == 0) { - m_ebusloop->scan(true); + m_busloop->scan(true); result << "done"; break; } @@ -346,7 +346,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - m_ebusloop->raw(); + m_busloop->raw(); result << "done"; break; @@ -356,7 +356,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - m_ebusloop->dump(); + m_busloop->dump(); result << "done"; break; @@ -376,7 +376,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) delete m_commands; m_commands = commands; - m_ebusloop->reload(m_commands); + m_busloop->reload(m_commands); result << "done"; break; diff --git a/src/ebusd/baseloop.h b/src/ebusd/baseloop.h index 7ef032c9..bd0f8df6 100644 --- a/src/ebusd/baseloop.h +++ b/src/ebusd/baseloop.h @@ -22,7 +22,7 @@ #include "commands.h" #include "network.h" -#include "ebusloop.h" +#include "busloop.h" class BaseLoop { @@ -37,7 +37,7 @@ public: private: Commands* m_commands; - EBusLoop* m_ebusloop; + BusLoop* m_busloop; Network* m_network; WQueue m_netQueue; diff --git a/src/ebusd/ebusloop.cpp b/src/ebusd/busloop.cpp similarity index 96% rename from src/ebusd/ebusloop.cpp rename to src/ebusd/busloop.cpp index 833bf47e..43921488 100644 --- a/src/ebusd/ebusloop.cpp +++ b/src/ebusd/busloop.cpp @@ -17,7 +17,7 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#include "ebusloop.h" +#include "busloop.h" #include "logger.h" #include "appl.h" #include @@ -25,7 +25,7 @@ extern Logger& L; extern Appl& A; -EBusLoop::EBusLoop(Commands* commands) +BusLoop::BusLoop(Commands* commands) : m_commands(commands), m_stop(false), m_lockCounter(0), m_priorRetry(false), m_scan(false), m_scanFull(false), m_scanIndex(0) { @@ -51,7 +51,7 @@ EBusLoop::EBusLoop(Commands* commands) m_acquireTime = A.getOptVal("acquiretime"); } -EBusLoop::~EBusLoop() +BusLoop::~BusLoop() { if (m_port->isOpen() == true) m_port->close(); @@ -60,7 +60,7 @@ EBusLoop::~EBusLoop() delete m_dump; } -void* EBusLoop::run() +void* BusLoop::run() { int sendRetries = 0; int lockRetries = 0; @@ -191,7 +191,7 @@ void* EBusLoop::run() return NULL; } -unsigned char EBusLoop::fetchByte() +unsigned char BusLoop::fetchByte() { unsigned char byte; @@ -207,7 +207,7 @@ unsigned char EBusLoop::fetchByte() return byte; } -void EBusLoop::collectCycData(const int numRecv) +void BusLoop::collectCycData(const int numRecv) { // cycle bytes for (int i = 0; i < numRecv; i++) { @@ -242,7 +242,7 @@ void EBusLoop::collectCycData(const int numRecv) } } -void EBusLoop::analyseCycData() +void BusLoop::analyseCycData() { static bool skipfirst = false; @@ -276,7 +276,7 @@ void EBusLoop::analyseCycData() skipfirst = true; } -void EBusLoop::addPollCommand() +void BusLoop::addPollCommand() { int index = m_commands->nextPollCommand(); if (index < 0) { @@ -301,7 +301,7 @@ void EBusLoop::addPollCommand() } } -int EBusLoop::acquireBus() +int BusLoop::acquireBus() { unsigned char recvByte, sendByte; ssize_t numRecv, numSend; @@ -358,7 +358,7 @@ int EBusLoop::acquireBus() return RESULT_ERR_EXTRA_DATA; } -BusCommand* EBusLoop::sendCommand() +BusCommand* BusLoop::sendCommand() { unsigned char recvByte; std::string result; @@ -470,7 +470,7 @@ on_exit: } -int EBusLoop::sendByte(const unsigned char sendByte) +int BusLoop::sendByte(const unsigned char sendByte) { unsigned char recvByte; ssize_t numRecv, numSend; @@ -495,7 +495,7 @@ int EBusLoop::sendByte(const unsigned char sendByte) return RESULT_OK; } -int EBusLoop::recvSlaveAck(unsigned char& recvByte) +int BusLoop::recvSlaveAck(unsigned char& recvByte) { ssize_t numRecv; @@ -522,7 +522,7 @@ int EBusLoop::recvSlaveAck(unsigned char& recvByte) return RESULT_OK; } -int EBusLoop::recvSlaveData(SymbolString& result) +int BusLoop::recvSlaveData(SymbolString& result) { unsigned char recvByte, calcCrc = 0; ssize_t numRecv; @@ -568,7 +568,7 @@ int EBusLoop::recvSlaveData(SymbolString& result) return RESULT_OK; } -void EBusLoop::collectSlave() +void BusLoop::collectSlave() { std::vector::iterator it; @@ -594,7 +594,7 @@ void EBusLoop::collectSlave() } } -void EBusLoop::addScanCommand() +void BusLoop::addScanCommand() { std::string ebusCommand(A.getOptVal("address")); std::stringstream sstr; diff --git a/src/ebusd/ebusloop.h b/src/ebusd/busloop.h similarity index 94% rename from src/ebusd/ebusloop.h rename to src/ebusd/busloop.h index dc2a17fa..db8f6a90 100644 --- a/src/ebusd/ebusloop.h +++ b/src/ebusd/busloop.h @@ -17,8 +17,8 @@ * along with ebusd. If not, see http://www.gnu.org/licenses/. */ -#ifndef EBUSLOOP_H_ -#define EBUSLOOP_H_ +#ifndef BUSLOOP_H_ +#define BUSLOOP_H_ #include "commands.h" #include "port.h" @@ -30,12 +30,12 @@ /** the maximum time [us] allowed for retrieving a byte from an addressed slave */ #define RECV_TIMEOUT 10000 -class EBusLoop : public Thread +class BusLoop : public Thread { public: - EBusLoop(Commands* commands); - ~EBusLoop(); + BusLoop(Commands* commands); + ~BusLoop(); void* run(); void stop() { m_stop = true; } @@ -92,4 +92,4 @@ private: }; -#endif // EBUSLOOP_H_ +#endif // BUSLOOP_H_ diff --git a/src/ebusd/dump.cpp b/src/ebusd/dump.cpp index 258eb13f..5219c709 100644 --- a/src/ebusd/dump.cpp +++ b/src/ebusd/dump.cpp @@ -25,18 +25,18 @@ int Dump::write(const char* byte) { int ret = 0; - std::ofstream fs(m_filename.c_str(), std::ios::out | std::ios::binary | std::ios::app); + std::ofstream fs(m_name.c_str(), std::ios::out | std::ios::binary | std::ios::app); if (fs == 0) return -1; fs.write(byte, 1); - if (fs.tellp() >= m_filesize * 1024) { + if (fs.tellp() >= m_size * 1024) { std::string oldfile; - oldfile += m_filename; + oldfile += m_name; oldfile += ".old"; - ret = rename(m_filename.c_str(), oldfile.c_str()); + ret = rename(m_name.c_str(), oldfile.c_str()); } fs.close(); diff --git a/src/ebusd/dump.h b/src/ebusd/dump.h index 5abbe5f1..812b9002 100644 --- a/src/ebusd/dump.h +++ b/src/ebusd/dump.h @@ -23,19 +23,19 @@ #include /** - * @brief Class for writing raw bytes to binary file. + * @brief class for writing raw bytes to binary file. */ class Dump { public: /** - * @brief Create a new instance to write dump files. - * @param filename which will be used for dumping raw bytes. - * @param filesize max. Size of the dump file, before switching. + * @brief create a new instance to write dump files. + * @param name the file name of dump file. + * @param size the max. size of the dump file, before switching. */ - Dump(std::string filename, long filesize) - : m_filename(filename), m_filesize(filesize) {} + Dump(std::string name, long size) + : m_name(name), m_size(size) {} /** * @brief write byte to dump file. @@ -45,23 +45,23 @@ public: int write(const char* byte); /** - * @brief setter for dump file name. - * @param filename which will be used for dumping raw bytes. + * @brief set the name of dump file. + * @param name the file name of dump file. */ - void setFilename(const std::string& filename) { m_filename = filename; } + void setName(const std::string& name) { m_name = name; } /** - * @brief setter for max size of dump file. - * @param filesize max. Size of the dump file, before switching. + * @brief set the max size of dump file. + * @param size the max. size of the dump file, before switching. */ - void setFilesize(const long filesize) { m_filesize = filesize; } + void setSize(const long size) { m_size = size; } private: /** the name of dump file*/ - std::string m_filename; + std::string m_name; /** max. size of dump file */ - long m_filesize; + long m_size; }; From 108842b4b512410d6cd91330d2ed3f5ed54cf817 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 22:01:28 +0100 Subject: [PATCH 36/38] class BusCommand integrated into busloop file. --- src/ebusd/Makefile.am | 2 -- src/ebusd/buscommand.cpp | 63 -------------------------------------- src/ebusd/buscommand.h | 65 ---------------------------------------- src/ebusd/busloop.cpp | 43 ++++++++++++++++++++++++++ src/ebusd/busloop.h | 42 +++++++++++++++++++++++++- 5 files changed, 84 insertions(+), 131 deletions(-) delete mode 100644 src/ebusd/buscommand.cpp delete mode 100644 src/ebusd/buscommand.h diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index c39e79da..e05c74d1 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -8,8 +8,6 @@ bin_PROGRAMS = ebusd ebusd_SOURCES = dump.cpp \ dump.h \ - buscommand.cpp \ - buscommand.h \ busloop.cpp \ busloop.h \ network.cpp \ diff --git a/src/ebusd/buscommand.cpp b/src/ebusd/buscommand.cpp deleted file mode 100644 index 9b25dca8..00000000 --- a/src/ebusd/buscommand.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 - * - * This file is part of ebusd. - * - * ebusd is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ebusd is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with ebusd. If not, see http://www.gnu.org/licenses/. - */ - -#include "buscommand.h" - -BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool scan) - : m_poll(poll), m_scan(scan), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) -{ - unsigned char dstAddress = m_command[1]; - - if (dstAddress == BROADCAST) - m_type = broadcast; - else if (isMaster(dstAddress) == true) - m_type = masterMaster; - else - m_type = masterSlave; - - pthread_mutex_init(&m_mutex, NULL); - pthread_cond_init(&m_cond, NULL); -} - -BusCommand::~BusCommand() -{ - pthread_mutex_destroy(&m_mutex); - pthread_cond_destroy(&m_cond); -} - -const std::string BusCommand::getMessageStr() -{ - std::string result; - - if (m_resultCode >= 0) { - if (m_type == masterSlave) { - result = m_command.getDataStr(); - result += "00"; - result += m_result.getDataStr(); - result += "00"; - } - else - result = "success"; - } - else - result = "error: "+std::string(getResultCodeCStr()); - - return result; -} - diff --git a/src/ebusd/buscommand.h b/src/ebusd/buscommand.h deleted file mode 100644 index e454e74f..00000000 --- a/src/ebusd/buscommand.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) Roland Jax 2012-2014 - * - * This file is part of ebusd. - * - * ebusd is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * ebusd is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with ebusd. If not, see http://www.gnu.org/licenses/. - */ - -#ifndef BUSCOMMAND_H_ -#define BUSCOMMAND_H_ - -#include "symbol.h" -#include "result.h" - -enum CommandType { invalid, broadcast, masterMaster, masterSlave }; - -class BusCommand -{ - -public: - BusCommand(const std::string command, const bool poll, const bool scan); - ~BusCommand(); - - CommandType getType() const { return m_type; } - bool isPoll() const { return m_poll; } - bool isScan() const { return m_scan; } - - SymbolString getCommand() const { return m_command; } - SymbolString getResult() const { return m_result; } - - bool isErrorResult() const { return m_resultCode < 0; } - const char* getResultCodeCStr() const { return getResultCode(m_resultCode); } - void setResult(const SymbolString result, const int resultCode) - { m_result = result; m_resultCode = resultCode; } - - const std::string getMessageStr(); - - void waitSignal() { pthread_cond_wait(&m_cond, &m_mutex); } // TODO timeout - void sendSignal() { pthread_cond_signal(&m_cond); } - -private: - CommandType m_type; - bool m_poll; - bool m_scan; - SymbolString m_command; - SymbolString m_result; - int m_resultCode; - - pthread_mutex_t m_mutex; - pthread_cond_t m_cond; -}; - -#endif // BUSCOMMAND_H_ - diff --git a/src/ebusd/busloop.cpp b/src/ebusd/busloop.cpp index 43921488..f209282d 100644 --- a/src/ebusd/busloop.cpp +++ b/src/ebusd/busloop.cpp @@ -25,6 +25,49 @@ extern Logger& L; extern Appl& A; +BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool scan) + : m_poll(poll), m_scan(scan), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) +{ + unsigned char dstAddress = m_command[1]; + + if (dstAddress == BROADCAST) + m_type = broadcast; + else if (isMaster(dstAddress) == true) + m_type = masterMaster; + else + m_type = masterSlave; + + pthread_mutex_init(&m_mutex, NULL); + pthread_cond_init(&m_cond, NULL); +} + +BusCommand::~BusCommand() +{ + pthread_mutex_destroy(&m_mutex); + pthread_cond_destroy(&m_cond); +} + +const std::string BusCommand::getMessageStr() +{ + std::string result; + + if (m_resultCode >= 0) { + if (m_type == masterSlave) { + result = m_command.getDataStr(); + result += "00"; + result += m_result.getDataStr(); + result += "00"; + } + else + result = "success"; + } + else + result = "error: "+std::string(getResultCodeCStr()); + + return result; +} + + BusLoop::BusLoop(Commands* commands) : m_commands(commands), m_stop(false), m_lockCounter(0), m_priorRetry(false), m_scan(false), m_scanFull(false), m_scanIndex(0) diff --git a/src/ebusd/busloop.h b/src/ebusd/busloop.h index db8f6a90..d2242ce0 100644 --- a/src/ebusd/busloop.h +++ b/src/ebusd/busloop.h @@ -23,13 +23,53 @@ #include "commands.h" #include "port.h" #include "dump.h" -#include "buscommand.h" #include "wqueue.h" #include "thread.h" +#include "symbol.h" +#include "result.h" /** the maximum time [us] allowed for retrieving a byte from an addressed slave */ #define RECV_TIMEOUT 10000 +enum CommandType { invalid, broadcast, masterMaster, masterSlave }; + +class BusCommand +{ + +public: + BusCommand(const std::string command, const bool poll, const bool scan); + ~BusCommand(); + + CommandType getType() const { return m_type; } + bool isPoll() const { return m_poll; } + bool isScan() const { return m_scan; } + + SymbolString getCommand() const { return m_command; } + SymbolString getResult() const { return m_result; } + + bool isErrorResult() const { return m_resultCode < 0; } + const char* getResultCodeCStr() const { return getResultCode(m_resultCode); } + void setResult(const SymbolString result, const int resultCode) + { m_result = result; m_resultCode = resultCode; } + + const std::string getMessageStr(); + + void waitSignal() { pthread_cond_wait(&m_cond, &m_mutex); } // TODO timeout + void sendSignal() { pthread_cond_signal(&m_cond); } + +private: + CommandType m_type; + bool m_poll; + bool m_scan; + SymbolString m_command; + SymbolString m_result; + int m_resultCode; + + pthread_mutex_t m_mutex; + pthread_cond_t m_cond; +}; + + class BusLoop : public Thread { From 2fe5b8cdc781a9b19f2d81cb13ff41a3b4fb613d Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 22:33:59 +0100 Subject: [PATCH 37/38] class BusCommand renamed to BusMessage. --- src/ebusd/baseloop.cpp | 82 ++++++++++++++++----------------- src/ebusd/busloop.cpp | 96 +++++++++++++++++++-------------------- src/ebusd/busloop.h | 22 ++++----- src/lib/ebus/commands.cpp | 6 +-- src/lib/ebus/commands.h | 2 +- 5 files changed, 104 insertions(+), 104 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 228a92e7..20055b0b 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -145,30 +145,30 @@ std::string BaseLoop::decodeMessage(const std::string& data) break; } - std::string ebusCommand(A.getOptVal("address")); - ebusCommand += m_commands->getEbusCommand(index); - std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + std::string busCommand(A.getOptVal("address")); + busCommand += m_commands->getBusCommand(index); + std::transform(busCommand.begin(), busCommand.end(), busCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, false, false); - L.log(bas, trace, " msg: %s", ebusCommand.c_str()); - // send busCommand - m_busloop->addBusCommand(busCommand); - busCommand->waitSignal(); + BusMessage* message = new BusMessage(busCommand, false, false); + L.log(bas, trace, " msg: %s", busCommand.c_str()); + // send message + m_busloop->addBusMessage(message); + message->waitSignal(); - if (!busCommand->isErrorResult()) { + if (!message->isErrorResult()) { // decode data - Command* command = new Command(index, (*m_commands)[index], busCommand->getMessageStr()); // TODO use getCommand()+getResult() + Command* command = new Command(index, (*m_commands)[index], message->getMessageStr()); // TODO use getCommand()+getResult() // return result result << command->calcResult(cmd); delete command; } else { - L.log(bas, error, " %s", busCommand->getResultCodeCStr()); - result << busCommand->getResultCodeCStr(); + L.log(bas, error, " %s", message->getResultCodeCStr()); + result << message->getResultCodeCStr(); } - delete busCommand; + delete message; } else { result << "ebus command not found"; @@ -186,43 +186,43 @@ std::string BaseLoop::decodeMessage(const std::string& data) if (index >= 0) { - std::string ebusCommand(A.getOptVal("address")); - ebusCommand += m_commands->getEbusCommand(index); + std::string busCommand(A.getOptVal("address")); + busCommand += m_commands->getBusCommand(index); // encode data Command* command = new Command(index, (*m_commands)[index], cmd[3]); std::string value = command->calcData(); if (value[0] != '-') { - ebusCommand += value; + busCommand += value; } else { L.log(bas, error, " %s", value.c_str()); delete command; break; } - std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + std::transform(busCommand.begin(), busCommand.end(), busCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, false, false); - L.log(bas, event, " msg: %s", ebusCommand.c_str()); - // send busCommand - m_busloop->addBusCommand(busCommand); - busCommand->waitSignal(); + BusMessage* message = new BusMessage(busCommand, false, false); + L.log(bas, event, " msg: %s", busCommand.c_str()); + // send message + m_busloop->addBusMessage(message); + message->waitSignal(); - if (!busCommand->isErrorResult()) { + if (!message->isErrorResult()) { // decode result - if (busCommand->getType()==broadcast) + if (message->getType()==broadcast) result << "done"; - else if (busCommand->getMessageStr().substr(busCommand->getMessageStr().length()-8) == "00000000") // TODO use getResult() + else if (message->getMessageStr().substr(message->getMessageStr().length()-8) == "00000000") // TODO use getResult() result << "done"; else result << "error"; } else { - L.log(bas, error, " %s", busCommand->getResultCodeCStr()); - result << busCommand->getResultCodeCStr(); + L.log(bas, error, " %s", message->getResultCodeCStr()); + result << message->getResultCodeCStr(); } - delete busCommand; + delete message; delete command; } else { @@ -266,25 +266,25 @@ std::string BaseLoop::decodeMessage(const std::string& data) } { - std::string ebusCommand(A.getOptVal("address")); + std::string busCommand(A.getOptVal("address")); cmd[1].erase(std::remove_if(cmd[1].begin(), cmd[1].end(), isspace), cmd[1].end()); - ebusCommand += cmd[1]; - std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + busCommand += cmd[1]; + std::transform(busCommand.begin(), busCommand.end(), busCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, false, false); - L.log(bas, trace, " msg: %s", ebusCommand.c_str()); - // send busCommand - m_busloop->addBusCommand(busCommand); - busCommand->waitSignal(); + BusMessage* message = new BusMessage(busCommand, false, false); + L.log(bas, trace, " msg: %s", busCommand.c_str()); + // send message + m_busloop->addBusMessage(message); + message->waitSignal(); - if (busCommand->isErrorResult()) { - L.log(bas, error, " %s", busCommand->getResultCodeCStr()); - result << busCommand->getResultCodeCStr(); + if (message->isErrorResult()) { + L.log(bas, error, " %s", message->getResultCodeCStr()); + result << message->getResultCodeCStr(); } else { - result << busCommand->getMessageStr(); // TODO use getCommand()+getResult() + result << message->getMessageStr(); // TODO use getCommand()+getResult() } - delete busCommand; + delete message; } break; diff --git a/src/ebusd/busloop.cpp b/src/ebusd/busloop.cpp index f209282d..9ff3c856 100644 --- a/src/ebusd/busloop.cpp +++ b/src/ebusd/busloop.cpp @@ -25,8 +25,8 @@ extern Logger& L; extern Appl& A; -BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool scan) - : m_poll(poll), m_scan(scan), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) +BusMessage::BusMessage(const std::string command, const bool poll, const bool scan) + : m_poll(poll), m_scan(scan), m_command(command), m_result(), m_resultCode(RESULT_OK) { unsigned char dstAddress = m_command[1]; @@ -41,13 +41,13 @@ BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool pthread_cond_init(&m_cond, NULL); } -BusCommand::~BusCommand() +BusMessage::~BusMessage() { pthread_mutex_destroy(&m_mutex); pthread_cond_destroy(&m_cond); } -const std::string BusCommand::getMessageStr() +const std::string BusMessage::getMessageStr() { std::string result; @@ -126,9 +126,9 @@ void* BusLoop::run() // add new polling command to send if (pollDelta >= m_pollInterval) { if (m_scan == true) - addScanCommand(); + addScanMessage(); else - addPollCommand(); + addPollMessage(); time(&pollStart); } @@ -146,43 +146,43 @@ void* BusLoop::run() collectCycData(numBytes); // send command - if (m_sstr.size() == 0 && m_lockCounter == 0 && m_sendBuffer.size() > 0) { + if (m_sstr.size() == 0 && m_lockCounter == 0 && m_busQueue.size() > 0) { // acquire Bus int busResult = acquireBus(); // send bus command if (busResult == RESULT_BUS_ACQUIRED) { - BusCommand* busCommand = sendCommand(); - L.log(bus, trace, " %s", busCommand->getMessageStr().c_str()); + BusMessage* message = sendCommand(); + L.log(bus, trace, " %s", message->getMessageStr().c_str()); - if (busCommand->isErrorResult() == true) { + if (message->isErrorResult() == true) { if (sendRetries < m_sendRetries) { sendRetries++; L.log(bus, trace, " send retry %d", sendRetries); - busCommand->setResult(std::string(), RESULT_OK); + message->setResult(std::string(), RESULT_OK); } else { sendRetries = 0; L.log(bus, event, " send retry failed", sendRetries); - if (busCommand->isPoll() == true) - delete m_sendBuffer.remove(); + if (message->isPoll() == true) + delete m_busQueue.remove(); else - busCommand->sendSignal(); + message->sendSignal(); } } else { sendRetries = 0; - if (busCommand->isPoll() == true) { - if (busCommand->isScan() == true) - m_commands->storeScanData(busCommand->getMessageStr().c_str()); + if (message->isPoll() == true) { + if (message->isScan() == true) + m_commands->storeScanData(message->getMessageStr().c_str()); else - m_commands->storePollData(busCommand->getMessageStr().c_str()); // TODO use getResult() - delete busCommand; + m_commands->storePollData(message->getMessageStr().c_str()); // TODO use getResult() + delete message; } else - busCommand->sendSignal(); + message->sendSignal(); } lockRetries = 0; @@ -195,11 +195,11 @@ void* BusLoop::run() lockRetries = 0; L.log(bus, event, " lock bus failed"); - BusCommand* busCommand = m_sendBuffer.remove(); - if (busCommand->isPoll() == true) - delete busCommand; + BusMessage* message = m_busQueue.remove(); + if (message->isPoll() == true) + delete message; else - busCommand->sendSignal(); + message->sendSignal(); } else { lockRetries++; @@ -319,7 +319,7 @@ void BusLoop::analyseCycData() skipfirst = true; } -void BusLoop::addPollCommand() +void BusLoop::addPollMessage() { int index = m_commands->nextPollCommand(); if (index < 0) { @@ -333,14 +333,14 @@ void BusLoop::addPollCommand() tmp += (*m_commands)[index][2]; L.log(bus, event, " polling [%4d] %s", index, tmp.c_str()); - std::string ebusCommand(A.getOptVal("address")); - ebusCommand += m_commands->getEbusCommand(index); - std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + std::string busCommand(A.getOptVal("address")); + busCommand += m_commands->getBusCommand(index); + std::transform(busCommand.begin(), busCommand.end(), busCommand.begin(), tolower); - BusCommand* busCommand = new BusCommand(ebusCommand, true, false); - L.log(bus, trace, " msg: %s", ebusCommand.c_str()); + BusMessage* message = new BusMessage(busCommand, true, false); + L.log(bus, trace, " msg: %s", busCommand.c_str()); - addBusCommand(busCommand); + addBusMessage(message); } } @@ -349,7 +349,7 @@ int BusLoop::acquireBus() unsigned char recvByte, sendByte; ssize_t numRecv, numSend; - sendByte = m_sendBuffer.next()->getCommand()[0]; + sendByte = m_busQueue.next()->getCommand()[0]; // send QQ numSend = m_port->send(&sendByte); @@ -401,17 +401,17 @@ int BusLoop::acquireBus() return RESULT_ERR_EXTRA_DATA; } -BusCommand* BusLoop::sendCommand() +BusMessage* BusLoop::sendCommand() { unsigned char recvByte; std::string result; SymbolString slaveData; int retval = RESULT_OK; - BusCommand* busCommand = m_sendBuffer.next(); + BusMessage* message = m_busQueue.next(); // send ZZ PB SB NN Dx CRC - SymbolString command = busCommand->getCommand(); + SymbolString command = message->getCommand(); for (size_t i = 1; i < command.size(); i++) { retval = sendByte(command[i]); if (retval < 0) @@ -419,7 +419,7 @@ BusCommand* BusLoop::sendCommand() } // BC -> send SYN - if (busCommand->getType() == broadcast) { + if (message->getType() == broadcast) { sendByte(SYN); goto on_exit; } @@ -454,7 +454,7 @@ BusCommand* BusLoop::sendCommand() } // MM -> send SYN - if (busCommand->getType() == masterMaster) { + if (message->getType() == masterMaster) { sendByte(SYN); goto on_exit; } @@ -504,12 +504,12 @@ on_exit: while (m_port->size() != 0) recvByte = fetchByte(); - busCommand->setResult(slaveData, retval); + message->setResult(slaveData, retval); if (retval == RESULT_OK) - return m_sendBuffer.remove(); + return m_busQueue.remove(); else - return busCommand; + return message; } @@ -637,9 +637,9 @@ void BusLoop::collectSlave() } } -void BusLoop::addScanCommand() +void BusLoop::addScanMessage() { - std::string ebusCommand(A.getOptVal("address")); + std::string busCommand(A.getOptVal("address")); std::stringstream sstr; if (m_scanFull == true) { @@ -665,16 +665,16 @@ void BusLoop::addScanCommand() else { m_scanIndex++; - ebusCommand += sstr.str(); - ebusCommand += "070400"; - std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); + busCommand += sstr.str(); + busCommand += "070400"; + std::transform(busCommand.begin(), busCommand.end(), busCommand.begin(), tolower); L.log(bus, event, " scanning address %s", sstr.str().c_str()); - BusCommand* busCommand = new BusCommand(ebusCommand, true, true); - L.log(bus, trace, " msg: %s", ebusCommand.c_str()); + BusMessage* message = new BusMessage(busCommand, true, true); + L.log(bus, trace, " msg: %s", busCommand.c_str()); - addBusCommand(busCommand); + addBusMessage(message); } } diff --git a/src/ebusd/busloop.h b/src/ebusd/busloop.h index d2242ce0..a89d016d 100644 --- a/src/ebusd/busloop.h +++ b/src/ebusd/busloop.h @@ -31,16 +31,16 @@ /** the maximum time [us] allowed for retrieving a byte from an addressed slave */ #define RECV_TIMEOUT 10000 -enum CommandType { invalid, broadcast, masterMaster, masterSlave }; +enum MessageType { invalid, broadcast, masterMaster, masterSlave }; -class BusCommand +class BusMessage { public: - BusCommand(const std::string command, const bool poll, const bool scan); - ~BusCommand(); + BusMessage(const std::string command, const bool poll, const bool scan); + ~BusMessage(); - CommandType getType() const { return m_type; } + MessageType getType() const { return m_type; } bool isPoll() const { return m_poll; } bool isScan() const { return m_scan; } @@ -58,7 +58,7 @@ public: void sendSignal() { pthread_cond_signal(&m_cond); } private: - CommandType m_type; + MessageType m_type; bool m_poll; bool m_scan; SymbolString m_command; @@ -80,7 +80,7 @@ public: void* run(); void stop() { m_stop = true; } - void addBusCommand(BusCommand* busCommand) { m_sendBuffer.add(busCommand); } + void addBusMessage(BusMessage* message) { m_busQueue.add(message); } void dump() { m_dumpState == true ? m_dumpState = false : m_dumpState = true ; } void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; } @@ -103,7 +103,7 @@ private: int m_lockCounter; bool m_priorRetry; - WQueue m_sendBuffer; + WQueue m_busQueue; SymbolString m_sstr; double m_pollInterval; @@ -121,14 +121,14 @@ private: unsigned char fetchByte(); void collectCycData(const int numRecv); void analyseCycData(); - void addPollCommand(); + void addPollMessage(); int acquireBus(); - BusCommand* sendCommand(); + BusMessage* sendCommand(); int sendByte(const unsigned char sendByte); int recvSlaveAck(unsigned char& recvByte); int recvSlaveData(SymbolString& result); void collectSlave(); - void addScanCommand(); + void addScanMessage(); }; diff --git a/src/lib/ebus/commands.cpp b/src/lib/ebus/commands.cpp index dc921734..dfb0ee28 100644 --- a/src/lib/ebus/commands.cpp +++ b/src/lib/ebus/commands.cpp @@ -123,7 +123,7 @@ int Commands::findCommand(const std::string& data) const return -1; } -std::string Commands::getEbusCommand(const int index) const +std::string Commands::getBusCommand(const int index) const { cmd_t command = m_cmdDB.at(index); std::string cmd; @@ -159,7 +159,7 @@ int Commands::storeCycData(const std::string& data) const // walk through commands for (; iter != m_cycDB.end(); iter++) { - std::string command = getEbusCommand(iter->first); + std::string command = getBusCommand(iter->first); // skip wrong search string length if (command.length() > search.length()) @@ -212,7 +212,7 @@ void Commands::storePollData(const std::string& data) const // walk through commands for (; iter != m_pollDB.end(); iter++) { - std::string command = getEbusCommand(iter->first); + std::string command = getBusCommand(iter->first); // skip wrong search string length if (command.length() > search.length()) diff --git a/src/lib/ebus/commands.h b/src/lib/ebus/commands.h index dc111dc9..d927b15c 100644 --- a/src/lib/ebus/commands.h +++ b/src/lib/ebus/commands.h @@ -52,7 +52,7 @@ public: int findCommand(const std::string& data) const; std::string getCmdType(const int index) const { return std::string(m_cmdDB.at(index)[0]); } - std::string getEbusCommand(const int index) const; + std::string getBusCommand(const int index) const; int storeCycData(const std::string& data) const; std::string getCycData(int index) const; From 84bd787fbb07dd21db9f413fac271630b658f2d4 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 19 Nov 2014 22:40:45 +0100 Subject: [PATCH 38/38] formatting --- src/ebusd/baseloop.cpp | 6 +++--- src/ebusd/busloop.cpp | 4 ++-- src/ebusd/busloop.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ebusd/baseloop.cpp b/src/ebusd/baseloop.cpp index 20055b0b..8e1fffd9 100644 --- a/src/ebusd/baseloop.cpp +++ b/src/ebusd/baseloop.cpp @@ -152,7 +152,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) BusMessage* message = new BusMessage(busCommand, false, false); L.log(bas, trace, " msg: %s", busCommand.c_str()); // send message - m_busloop->addBusMessage(message); + m_busloop->addMessage(message); message->waitSignal(); if (!message->isErrorResult()) { @@ -205,7 +205,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) BusMessage* message = new BusMessage(busCommand, false, false); L.log(bas, event, " msg: %s", busCommand.c_str()); // send message - m_busloop->addBusMessage(message); + m_busloop->addMessage(message); message->waitSignal(); if (!message->isErrorResult()) { @@ -274,7 +274,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) BusMessage* message = new BusMessage(busCommand, false, false); L.log(bas, trace, " msg: %s", busCommand.c_str()); // send message - m_busloop->addBusMessage(message); + m_busloop->addMessage(message); message->waitSignal(); if (message->isErrorResult()) { diff --git a/src/ebusd/busloop.cpp b/src/ebusd/busloop.cpp index 9ff3c856..e7870858 100644 --- a/src/ebusd/busloop.cpp +++ b/src/ebusd/busloop.cpp @@ -340,7 +340,7 @@ void BusLoop::addPollMessage() BusMessage* message = new BusMessage(busCommand, true, false); L.log(bus, trace, " msg: %s", busCommand.c_str()); - addBusMessage(message); + addMessage(message); } } @@ -675,6 +675,6 @@ void BusLoop::addScanMessage() BusMessage* message = new BusMessage(busCommand, true, true); L.log(bus, trace, " msg: %s", busCommand.c_str()); - addBusMessage(message); + addMessage(message); } } diff --git a/src/ebusd/busloop.h b/src/ebusd/busloop.h index a89d016d..be0f6e04 100644 --- a/src/ebusd/busloop.h +++ b/src/ebusd/busloop.h @@ -80,7 +80,7 @@ public: void* run(); void stop() { m_stop = true; } - void addBusMessage(BusMessage* message) { m_busQueue.add(message); } + void addMessage(BusMessage* message) { m_busQueue.add(message); } void dump() { m_dumpState == true ? m_dumpState = false : m_dumpState = true ; } void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; }