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;