class BusCommand separated from class Bus.
This commit is contained in:
@@ -6,10 +6,15 @@ AM_CXXFLAGS = -fpic \
|
|||||||
|
|
||||||
bin_PROGRAMS = ebusd
|
bin_PROGRAMS = ebusd
|
||||||
|
|
||||||
ebusd_SOURCES = connection.cpp \
|
ebusd_SOURCES = message.h \
|
||||||
|
connection.cpp \
|
||||||
|
connection.h \
|
||||||
network.cpp \
|
network.cpp \
|
||||||
|
network.h \
|
||||||
ebusloop.cpp \
|
ebusloop.cpp \
|
||||||
|
ebusloop.h \
|
||||||
baseloop.cpp \
|
baseloop.cpp \
|
||||||
|
baseloop.h \
|
||||||
main.cpp
|
main.cpp
|
||||||
|
|
||||||
ebusd_LDADD = $(top_srcdir)/src/libcore/libcore.a \
|
ebusd_LDADD = $(top_srcdir)/src/libcore/libcore.a \
|
||||||
|
|||||||
@@ -4,11 +4,18 @@ AM_CXXFLAGS = -fpic \
|
|||||||
|
|
||||||
noinst_LIBRARIES = libcore.a
|
noinst_LIBRARIES = libcore.a
|
||||||
|
|
||||||
libcore_a_SOURCES = appl.cpp \
|
libcore_a_SOURCES = wqueue.h \
|
||||||
|
notify.h \
|
||||||
|
appl.cpp \
|
||||||
|
appl.h \
|
||||||
daemon.cpp \
|
daemon.cpp \
|
||||||
|
daemon.h \
|
||||||
logger.cpp \
|
logger.cpp \
|
||||||
|
logger.h \
|
||||||
thread.cpp \
|
thread.cpp \
|
||||||
tcpsocket.cpp
|
thread.h \
|
||||||
|
tcpsocket.cpp \
|
||||||
|
tcpsocket.h
|
||||||
|
|
||||||
distclean-local:
|
distclean-local:
|
||||||
-rm -f Makefile.in
|
-rm -f Makefile.in
|
||||||
|
|||||||
+13
-1
@@ -5,15 +5,27 @@ AM_CXXFLAGS = -fpic \
|
|||||||
noinst_LIBRARIES = libebus.a
|
noinst_LIBRARIES = libebus.a
|
||||||
|
|
||||||
libebus_a_SOURCES = result.cpp \
|
libebus_a_SOURCES = result.cpp \
|
||||||
|
result.h \
|
||||||
symbol.cpp \
|
symbol.cpp \
|
||||||
|
symbol.h \
|
||||||
port.cpp \
|
port.cpp \
|
||||||
|
port.h \
|
||||||
|
buscommand.cpp \
|
||||||
|
buscommand.h \
|
||||||
bus.cpp \
|
bus.cpp \
|
||||||
|
bus.h \
|
||||||
command.cpp \
|
command.cpp \
|
||||||
|
command.h \
|
||||||
commands.cpp \
|
commands.cpp \
|
||||||
|
commands.h \
|
||||||
configfile.cpp \
|
configfile.cpp \
|
||||||
|
configfile.h \
|
||||||
dump.cpp \
|
dump.cpp \
|
||||||
|
dump.h \
|
||||||
decode.cpp \
|
decode.cpp \
|
||||||
encode.cpp
|
decode.h \
|
||||||
|
encode.cpp \
|
||||||
|
encode.h
|
||||||
|
|
||||||
distclean-local:
|
distclean-local:
|
||||||
-rm -f Makefile.in
|
-rm -f Makefile.in
|
||||||
|
|||||||
@@ -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,
|
Bus::Bus(const std::string deviceName, const bool noDeviceCheck, const long recvTimeout,
|
||||||
const std::string dumpFile, const long dumpSize, const bool dumpState)
|
const std::string dumpFile, const long dumpSize, const bool dumpState)
|
||||||
: m_previousEscape(false), m_recvTimeout(recvTimeout), m_dumpState(dumpState),
|
: m_previousEscape(false), m_recvTimeout(recvTimeout), m_dumpState(dumpState),
|
||||||
|
|||||||
+1
-33
@@ -24,6 +24,7 @@
|
|||||||
#include "result.h"
|
#include "result.h"
|
||||||
#include "port.h"
|
#include "port.h"
|
||||||
#include "dump.h"
|
#include "dump.h"
|
||||||
|
#include "buscommand.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -36,39 +37,6 @@ namespace libebus
|
|||||||
// the maximum time allowed for retrieving a byte from an addressed slave
|
// the maximum time allowed for retrieving a byte from an addressed slave
|
||||||
#define RECV_TIMEOUT 10000
|
#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
|
class Bus
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
|
||||||
|
*
|
||||||
|
* 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_
|
||||||
Reference in New Issue
Block a user