diff --git a/src/baseloop.cpp b/src/baseloop.cpp index 8bc88b42..53762b98 100644 --- a/src/baseloop.cpp +++ b/src/baseloop.cpp @@ -91,7 +91,7 @@ std::string BaseLoop::decodeMessage(const std::string& data) std::string type = m_commands->getType(index); std::string cmd(A.getParam("p_address")); - cmd += m_commands->getCommand(index); + cmd += m_commands->getEbusCommand(index); std::transform(cmd.begin(), cmd.end(), cmd.begin(), tolower); L.log(bas, trace, " type: %s msg: %s", type.c_str(), cmd.c_str()); diff --git a/src/cycdata.cpp b/src/cycdata.cpp index 25a57d7d..e240f29d 100644 --- a/src/cycdata.cpp +++ b/src/cycdata.cpp @@ -19,9 +19,23 @@ #include "cycdata.h" #include "logger.h" +#include +#include extern LogInstance& L; +CYCData::CYCData(EBusLoop* ebusloop, Commands* commands) + : m_ebusloop(ebusloop), m_commands(commands), m_stop(false) +{ + for (size_t index = 0; index < commands->size(); index++) { + if (strcasecmp((*m_commands)[index][0].c_str(),"cyc") == 0) { + Command* cmd = new Command(index, (*m_commands)[index]); + m_cycDB.insert(pair_t(index, cmd)); + } + } + +} + CYCData::~CYCData() { for (mapCI_t iter = m_cycDB.begin(); iter != m_cycDB.end(); ++iter) @@ -37,8 +51,8 @@ void* CYCData::run() if (skipfirst == true) { L.log(cyc, trace, "%s", data.c_str()); - - int index = m_commands->findData(data); + + int index = findData(data); if (index >= 0) { std::string tmp; @@ -61,22 +75,6 @@ void* CYCData::run() return NULL; } -void CYCData::storeData(int index, std::string data) -{ - mapCI_t iter = m_cycDB.find(index); - - if (iter != m_cycDB.end()) { - iter->second->setData(data); - L.log(cyc, debug, " [%d] %s -> replaced", index, data.c_str()); - } else { - Command* cmd = new Command(index, (*m_commands)[index], data); - m_cycDB.insert(pair_t(index, cmd)); - L.log(cyc, debug, " [%d] %s -> inserted", index, data.c_str()); - } - - L.log(cyc, debug, " cycDB entries: %d", m_cycDB.size()); -} - std::string CYCData::getData(int index) { mapCI_t iter = m_cycDB.find(index); @@ -86,3 +84,53 @@ std::string CYCData::getData(int index) return ""; } +int CYCData::findData(const std::string& data) const +{ + // no commands definend + if (m_cycDB.size() == 0) + return -2; + + // skip to small search string length + if (data.length() < 10) + return -3; + + // preapre string for searching command + std::string search(data.substr(2, 8 + atoi(data.substr(8,2).c_str()) * 2)); + + std::size_t index; + mapCI_t i = m_cycDB.begin(); + + // walk through commands + for (index = 0; i != m_cycDB.end(); i++, index++) { + cmd_t cmd = i->second->getCommand(); + // prepare string for defined command + std::string command(cmd[5]); + command += cmd[6]; + std::stringstream sstr; + sstr << std::setw(2) << std::hex << std::setfill('0') << cmd[7]; + command += sstr.str(); + command += cmd[8]; + + // skip wrong search string length + if (command.length() > search.length()) + continue; + + if (strcasecmp(command.c_str(), search.substr(0,command.length()).c_str()) == 0) + return i->first; + + } + + // command not found + return -1; +} + +void CYCData::storeData(int index, std::string data) +{ + mapCI_t iter = m_cycDB.find(index); + + if (iter != m_cycDB.end()) { + iter->second->setData(data); + L.log(cyc, debug, " [%d] data saved", index); + } +} + diff --git a/src/cycdata.h b/src/cycdata.h index 2596b7e2..98e9d1b0 100644 --- a/src/cycdata.h +++ b/src/cycdata.h @@ -35,8 +35,7 @@ class CYCData : public Thread { public: - CYCData(EBusLoop* ebusloop, Commands* commands) - : m_ebusloop(ebusloop), m_commands(commands), m_stop(false) {} + CYCData(EBusLoop* ebusloop, Commands* commands); ~CYCData(); void* run(); @@ -50,7 +49,9 @@ private: map_t m_cycDB; bool m_stop; + int findData(const std::string& data) const; void storeData(int index, std::string data); + }; #endif // CYCDATA_H_ diff --git a/src/ebusloop.cpp b/src/ebusloop.cpp index e54b2282..323f3fd3 100644 --- a/src/ebusloop.cpp +++ b/src/ebusloop.cpp @@ -65,7 +65,7 @@ void* EBusLoop::run() // new cyc message arrived if (busResult == 2) { std::string data = m_bus->getCycData(); - //~ L.log(bus, trace, "%s", data.c_str()); + L.log(bus, debug, "%s", data.c_str()); m_cycBuffer.add(data); } diff --git a/src/main.cpp b/src/main.cpp index eb6e6874..db87c3b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,7 +82,7 @@ void define_args() A.addItem("p_dump", Appl::Param(false), "D", "dump", "\tenable dump", Appl::type_bool, Appl::opt_none); - A.addItem("p_dumpfile", Appl::Param("/tmp/dump_ebusd.bin"), "", "dumpfile", + A.addItem("p_dumpfile", Appl::Param("/tmp/dump_ebus.bin"), "", "dumpfile", "\tdump file name (/tmp/dump_ebusd.bin)", Appl::type_string, Appl::opt_mandatory); A.addItem("p_dumpsize", Appl::Param(100), "", "dumpsize", diff --git a/utils/thread.cpp b/utils/thread.cpp index b1a2c908..34a02d98 100644 --- a/utils/thread.cpp +++ b/utils/thread.cpp @@ -24,8 +24,6 @@ static void* runThread(void* arg) return ((Thread *)arg)->run(); } -Thread::Thread() : m_threadid(0), m_running(false), m_detached(false) {} - Thread::~Thread() { if (m_running == true && m_detached == false) @@ -78,7 +76,3 @@ int Thread::detach() return result; } -pthread_t Thread::self() -{ - return m_threadid; -} diff --git a/utils/thread.h b/utils/thread.h index 0abc32da..3ec70d65 100644 --- a/utils/thread.h +++ b/utils/thread.h @@ -26,13 +26,13 @@ class Thread { public: - Thread(); + Thread() : m_threadid(0), m_running(false), m_detached(false) {} virtual ~Thread(); int start(const char* name); int join(); int detach(); - pthread_t self(); + pthread_t self() {return m_threadid; } virtual void* run() = 0;