From 6eab3adc4260e6251f6526e3f86819d1ed4c9a04 Mon Sep 17 00:00:00 2001 From: Roland Jax Date: Wed, 29 Oct 2014 18:20:40 +0100 Subject: [PATCH] class BusCommand separated from class Bus. --- src/ebusd/Makefile.am | 7 +++- src/libcore/Makefile.am | 11 ++++-- src/libebus/Makefile.am | 14 +++++++- src/libebus/bus.cpp | 47 ------------------------ src/libebus/bus.h | 34 +----------------- src/libebus/buscommand.cpp | 74 ++++++++++++++++++++++++++++++++++++++ src/libebus/buscommand.h | 65 +++++++++++++++++++++++++++++++++ 7 files changed, 168 insertions(+), 84 deletions(-) create mode 100644 src/libebus/buscommand.cpp create mode 100644 src/libebus/buscommand.h diff --git a/src/ebusd/Makefile.am b/src/ebusd/Makefile.am index 30b115e5..a0990baf 100644 --- a/src/ebusd/Makefile.am +++ b/src/ebusd/Makefile.am @@ -6,10 +6,15 @@ AM_CXXFLAGS = -fpic \ bin_PROGRAMS = ebusd -ebusd_SOURCES = connection.cpp \ +ebusd_SOURCES = message.h \ + connection.cpp \ + connection.h \ network.cpp \ + network.h \ ebusloop.cpp \ + ebusloop.h \ baseloop.cpp \ + baseloop.h \ main.cpp ebusd_LDADD = $(top_srcdir)/src/libcore/libcore.a \ diff --git a/src/libcore/Makefile.am b/src/libcore/Makefile.am index f7497681..d1b3b997 100644 --- a/src/libcore/Makefile.am +++ b/src/libcore/Makefile.am @@ -4,11 +4,18 @@ AM_CXXFLAGS = -fpic \ noinst_LIBRARIES = libcore.a -libcore_a_SOURCES = appl.cpp \ +libcore_a_SOURCES = wqueue.h \ + notify.h \ + appl.cpp \ + appl.h \ daemon.cpp \ + daemon.h \ logger.cpp \ + logger.h \ thread.cpp \ - tcpsocket.cpp + thread.h \ + tcpsocket.cpp \ + tcpsocket.h distclean-local: -rm -f Makefile.in diff --git a/src/libebus/Makefile.am b/src/libebus/Makefile.am index 7cf2761b..e5451b7b 100644 --- a/src/libebus/Makefile.am +++ b/src/libebus/Makefile.am @@ -5,15 +5,27 @@ AM_CXXFLAGS = -fpic \ noinst_LIBRARIES = libebus.a libebus_a_SOURCES = result.cpp \ + result.h \ symbol.cpp \ + symbol.h \ port.cpp \ + port.h \ + buscommand.cpp \ + buscommand.h \ bus.cpp \ + bus.h \ command.cpp \ + command.h \ commands.cpp \ + commands.h \ configfile.cpp \ + configfile.h \ dump.cpp \ + dump.h \ decode.cpp \ - encode.cpp + decode.h \ + encode.cpp \ + encode.h distclean-local: -rm -f Makefile.in diff --git a/src/libebus/bus.cpp b/src/libebus/bus.cpp index 99f337ae..6748371c 100644 --- a/src/libebus/bus.cpp +++ b/src/libebus/bus.cpp @@ -25,53 +25,6 @@ namespace libebus { -BusCommand::BusCommand(const std::string commandStr, const bool isPoll) - : m_isPoll(isPoll), m_command(commandStr), 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 char* BusCommand::getResultCodeCStr() -{ - return libebus::getResultCodeCStr(m_resultCode); -} - -const std::string BusCommand::getMessageStr() -{ - std::string result; - - if (m_resultCode >= 0) { - if (m_type == masterSlave) { - result = m_command.getDataStr(true); - result += "00"; - result += m_result.getDataStr(); - result += "00"; - } else { - result = "success"; - } - } - else - result = "error"; - - return result; -} - - Bus::Bus(const std::string deviceName, const bool noDeviceCheck, const long recvTimeout, const std::string dumpFile, const long dumpSize, const bool dumpState) : m_previousEscape(false), m_recvTimeout(recvTimeout), m_dumpState(dumpState), diff --git a/src/libebus/bus.h b/src/libebus/bus.h index 628c367a..be07313c 100644 --- a/src/libebus/bus.h +++ b/src/libebus/bus.h @@ -24,6 +24,7 @@ #include "result.h" #include "port.h" #include "dump.h" +#include "buscommand.h" #include #include #include @@ -36,39 +37,6 @@ namespace libebus // the maximum time 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 commandStr, const bool isPoll); - ~BusCommand(); - - CommandType getType() const { return m_type; } - bool isPoll() const { return m_isPoll; } - SymbolString getCommand() const { return m_command; } - bool isErrorResult() const { return m_resultCode < 0; } - const char* getResultCodeCStr(); - SymbolString getResult() const { return m_result; } - 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_isPoll; - SymbolString m_command; - SymbolString m_result; - int m_resultCode; - - pthread_mutex_t m_mutex; - pthread_cond_t m_cond; -}; - class Bus { diff --git a/src/libebus/buscommand.cpp b/src/libebus/buscommand.cpp new file mode 100644 index 00000000..59a6d891 --- /dev/null +++ b/src/libebus/buscommand.cpp @@ -0,0 +1,74 @@ +/* + * 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" + +namespace libebus +{ + + +BusCommand::BusCommand(const std::string commandStr, const bool isPoll) + : m_isPoll(isPoll), m_command(commandStr), 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 char* BusCommand::getResultCodeCStr() +{ + return libebus::getResultCodeCStr(m_resultCode); +} + +const std::string BusCommand::getMessageStr() +{ + std::string result; + + if (m_resultCode >= 0) { + if (m_type == masterSlave) { + result = m_command.getDataStr(true); + result += "00"; + result += m_result.getDataStr(); + result += "00"; + } else { + result = "success"; + } + } + else + result = "error"; + + return result; +} + + +} //namespace + diff --git a/src/libebus/buscommand.h b/src/libebus/buscommand.h new file mode 100644 index 00000000..b469ae3c --- /dev/null +++ b/src/libebus/buscommand.h @@ -0,0 +1,65 @@ +/* + * 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 LIBEBUS_BUSCOMMAND_H_ +#define LIBEBUS_BUSCOMMAND_H_ + +#include "symbol.h" +#include "result.h" + + +namespace libebus +{ + + +enum CommandType { invalid, broadcast, masterMaster, masterSlave }; + +class BusCommand +{ + +public: + BusCommand(const std::string commandStr, const bool isPoll); + ~BusCommand(); + + CommandType getType() const { return m_type; } + bool isPoll() const { return m_isPoll; } + SymbolString getCommand() const { return m_command; } + bool isErrorResult() const { return m_resultCode < 0; } + const char* getResultCodeCStr(); + SymbolString getResult() const { return m_result; } + 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_isPoll; + SymbolString m_command; + SymbolString m_result; + int m_resultCode; + + pthread_mutex_t m_mutex; + pthread_cond_t m_cond; +}; + + +} //namespace + +#endif // LIBEBUS_BUSCOMMAND_H_