class BusCommand renamed to BusMessage.

This commit is contained in:
Roland Jax
2014-11-19 22:33:59 +01:00
parent 108842b4b5
commit 2fe5b8cdc7
5 changed files with 104 additions and 104 deletions
+41 -41
View File
@@ -145,30 +145,30 @@ std::string BaseLoop::decodeMessage(const std::string& data)
break; break;
} }
std::string ebusCommand(A.getOptVal<const char*>("address")); std::string busCommand(A.getOptVal<const char*>("address"));
ebusCommand += m_commands->getEbusCommand(index); busCommand += m_commands->getBusCommand(index);
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); BusMessage* message = new BusMessage(busCommand, false, false);
L.log(bas, trace, " msg: %s", ebusCommand.c_str()); L.log(bas, trace, " msg: %s", busCommand.c_str());
// send busCommand // send message
m_busloop->addBusCommand(busCommand); m_busloop->addBusMessage(message);
busCommand->waitSignal(); message->waitSignal();
if (!busCommand->isErrorResult()) { if (!message->isErrorResult()) {
// decode data // 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 // return result
result << command->calcResult(cmd); result << command->calcResult(cmd);
delete command; delete command;
} else { } else {
L.log(bas, error, " %s", busCommand->getResultCodeCStr()); L.log(bas, error, " %s", message->getResultCodeCStr());
result << busCommand->getResultCodeCStr(); result << message->getResultCodeCStr();
} }
delete busCommand; delete message;
} else { } else {
result << "ebus command not found"; result << "ebus command not found";
@@ -186,43 +186,43 @@ std::string BaseLoop::decodeMessage(const std::string& data)
if (index >= 0) { if (index >= 0) {
std::string ebusCommand(A.getOptVal<const char*>("address")); std::string busCommand(A.getOptVal<const char*>("address"));
ebusCommand += m_commands->getEbusCommand(index); busCommand += m_commands->getBusCommand(index);
// encode data // encode data
Command* command = new Command(index, (*m_commands)[index], cmd[3]); Command* command = new Command(index, (*m_commands)[index], cmd[3]);
std::string value = command->calcData(); std::string value = command->calcData();
if (value[0] != '-') { if (value[0] != '-') {
ebusCommand += value; busCommand += value;
} else { } else {
L.log(bas, error, " %s", value.c_str()); L.log(bas, error, " %s", value.c_str());
delete command; delete command;
break; 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); BusMessage* message = new BusMessage(busCommand, false, false);
L.log(bas, event, " msg: %s", ebusCommand.c_str()); L.log(bas, event, " msg: %s", busCommand.c_str());
// send busCommand // send message
m_busloop->addBusCommand(busCommand); m_busloop->addBusMessage(message);
busCommand->waitSignal(); message->waitSignal();
if (!busCommand->isErrorResult()) { if (!message->isErrorResult()) {
// decode result // decode result
if (busCommand->getType()==broadcast) if (message->getType()==broadcast)
result << "done"; 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"; result << "done";
else else
result << "error"; result << "error";
} else { } else {
L.log(bas, error, " %s", busCommand->getResultCodeCStr()); L.log(bas, error, " %s", message->getResultCodeCStr());
result << busCommand->getResultCodeCStr(); result << message->getResultCodeCStr();
} }
delete busCommand; delete message;
delete command; delete command;
} else { } else {
@@ -266,25 +266,25 @@ std::string BaseLoop::decodeMessage(const std::string& data)
} }
{ {
std::string ebusCommand(A.getOptVal<const char*>("address")); std::string busCommand(A.getOptVal<const char*>("address"));
cmd[1].erase(std::remove_if(cmd[1].begin(), cmd[1].end(), isspace), cmd[1].end()); cmd[1].erase(std::remove_if(cmd[1].begin(), cmd[1].end(), isspace), cmd[1].end());
ebusCommand += cmd[1]; busCommand += cmd[1];
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); BusMessage* message = new BusMessage(busCommand, false, false);
L.log(bas, trace, " msg: %s", ebusCommand.c_str()); L.log(bas, trace, " msg: %s", busCommand.c_str());
// send busCommand // send message
m_busloop->addBusCommand(busCommand); m_busloop->addBusMessage(message);
busCommand->waitSignal(); message->waitSignal();
if (busCommand->isErrorResult()) { if (message->isErrorResult()) {
L.log(bas, error, " %s", busCommand->getResultCodeCStr()); L.log(bas, error, " %s", message->getResultCodeCStr());
result << busCommand->getResultCodeCStr(); result << message->getResultCodeCStr();
} else { } else {
result << busCommand->getMessageStr(); // TODO use getCommand()+getResult() result << message->getMessageStr(); // TODO use getCommand()+getResult()
} }
delete busCommand; delete message;
} }
break; break;
+48 -48
View File
@@ -25,8 +25,8 @@
extern Logger& L; extern Logger& L;
extern Appl& A; extern Appl& A;
BusCommand::BusCommand(const std::string commandStr, const bool poll, const bool scan) BusMessage::BusMessage(const std::string command, const bool poll, const bool scan)
: m_poll(poll), m_scan(scan), m_command(commandStr), m_result(), m_resultCode(RESULT_OK) : m_poll(poll), m_scan(scan), m_command(command), m_result(), m_resultCode(RESULT_OK)
{ {
unsigned char dstAddress = m_command[1]; 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); pthread_cond_init(&m_cond, NULL);
} }
BusCommand::~BusCommand() BusMessage::~BusMessage()
{ {
pthread_mutex_destroy(&m_mutex); pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond); pthread_cond_destroy(&m_cond);
} }
const std::string BusCommand::getMessageStr() const std::string BusMessage::getMessageStr()
{ {
std::string result; std::string result;
@@ -126,9 +126,9 @@ void* BusLoop::run()
// add new polling command to send // add new polling command to send
if (pollDelta >= m_pollInterval) { if (pollDelta >= m_pollInterval) {
if (m_scan == true) if (m_scan == true)
addScanCommand(); addScanMessage();
else else
addPollCommand(); addPollMessage();
time(&pollStart); time(&pollStart);
} }
@@ -146,43 +146,43 @@ void* BusLoop::run()
collectCycData(numBytes); collectCycData(numBytes);
// send command // 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 // acquire Bus
int busResult = acquireBus(); int busResult = acquireBus();
// send bus command // send bus command
if (busResult == RESULT_BUS_ACQUIRED) { if (busResult == RESULT_BUS_ACQUIRED) {
BusCommand* busCommand = sendCommand(); BusMessage* message = sendCommand();
L.log(bus, trace, " %s", busCommand->getMessageStr().c_str()); L.log(bus, trace, " %s", message->getMessageStr().c_str());
if (busCommand->isErrorResult() == true) { if (message->isErrorResult() == true) {
if (sendRetries < m_sendRetries) { if (sendRetries < m_sendRetries) {
sendRetries++; sendRetries++;
L.log(bus, trace, " send retry %d", sendRetries); L.log(bus, trace, " send retry %d", sendRetries);
busCommand->setResult(std::string(), RESULT_OK); message->setResult(std::string(), RESULT_OK);
} }
else { else {
sendRetries = 0; sendRetries = 0;
L.log(bus, event, " send retry failed", sendRetries); L.log(bus, event, " send retry failed", sendRetries);
if (busCommand->isPoll() == true) if (message->isPoll() == true)
delete m_sendBuffer.remove(); delete m_busQueue.remove();
else else
busCommand->sendSignal(); message->sendSignal();
} }
} }
else { else {
sendRetries = 0; sendRetries = 0;
if (busCommand->isPoll() == true) { if (message->isPoll() == true) {
if (busCommand->isScan() == true) if (message->isScan() == true)
m_commands->storeScanData(busCommand->getMessageStr().c_str()); m_commands->storeScanData(message->getMessageStr().c_str());
else else
m_commands->storePollData(busCommand->getMessageStr().c_str()); // TODO use getResult() m_commands->storePollData(message->getMessageStr().c_str()); // TODO use getResult()
delete busCommand; delete message;
} }
else else
busCommand->sendSignal(); message->sendSignal();
} }
lockRetries = 0; lockRetries = 0;
@@ -195,11 +195,11 @@ void* BusLoop::run()
lockRetries = 0; lockRetries = 0;
L.log(bus, event, " lock bus failed"); L.log(bus, event, " lock bus failed");
BusCommand* busCommand = m_sendBuffer.remove(); BusMessage* message = m_busQueue.remove();
if (busCommand->isPoll() == true) if (message->isPoll() == true)
delete busCommand; delete message;
else else
busCommand->sendSignal(); message->sendSignal();
} }
else { else {
lockRetries++; lockRetries++;
@@ -319,7 +319,7 @@ void BusLoop::analyseCycData()
skipfirst = true; skipfirst = true;
} }
void BusLoop::addPollCommand() void BusLoop::addPollMessage()
{ {
int index = m_commands->nextPollCommand(); int index = m_commands->nextPollCommand();
if (index < 0) { if (index < 0) {
@@ -333,14 +333,14 @@ void BusLoop::addPollCommand()
tmp += (*m_commands)[index][2]; tmp += (*m_commands)[index][2];
L.log(bus, event, " polling [%4d] %s", index, tmp.c_str()); L.log(bus, event, " polling [%4d] %s", index, tmp.c_str());
std::string ebusCommand(A.getOptVal<const char*>("address")); std::string busCommand(A.getOptVal<const char*>("address"));
ebusCommand += m_commands->getEbusCommand(index); busCommand += m_commands->getBusCommand(index);
std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); std::transform(busCommand.begin(), busCommand.end(), busCommand.begin(), tolower);
BusCommand* busCommand = new BusCommand(ebusCommand, true, false); BusMessage* message = new BusMessage(busCommand, true, false);
L.log(bus, trace, " msg: %s", ebusCommand.c_str()); L.log(bus, trace, " msg: %s", busCommand.c_str());
addBusCommand(busCommand); addBusMessage(message);
} }
} }
@@ -349,7 +349,7 @@ int BusLoop::acquireBus()
unsigned char recvByte, sendByte; unsigned char recvByte, sendByte;
ssize_t numRecv, numSend; ssize_t numRecv, numSend;
sendByte = m_sendBuffer.next()->getCommand()[0]; sendByte = m_busQueue.next()->getCommand()[0];
// send QQ // send QQ
numSend = m_port->send(&sendByte); numSend = m_port->send(&sendByte);
@@ -401,17 +401,17 @@ int BusLoop::acquireBus()
return RESULT_ERR_EXTRA_DATA; return RESULT_ERR_EXTRA_DATA;
} }
BusCommand* BusLoop::sendCommand() BusMessage* BusLoop::sendCommand()
{ {
unsigned char recvByte; unsigned char recvByte;
std::string result; std::string result;
SymbolString slaveData; SymbolString slaveData;
int retval = RESULT_OK; int retval = RESULT_OK;
BusCommand* busCommand = m_sendBuffer.next(); BusMessage* message = m_busQueue.next();
// send ZZ PB SB NN Dx CRC // send ZZ PB SB NN Dx CRC
SymbolString command = busCommand->getCommand(); SymbolString command = message->getCommand();
for (size_t i = 1; i < command.size(); i++) { for (size_t i = 1; i < command.size(); i++) {
retval = sendByte(command[i]); retval = sendByte(command[i]);
if (retval < 0) if (retval < 0)
@@ -419,7 +419,7 @@ BusCommand* BusLoop::sendCommand()
} }
// BC -> send SYN // BC -> send SYN
if (busCommand->getType() == broadcast) { if (message->getType() == broadcast) {
sendByte(SYN); sendByte(SYN);
goto on_exit; goto on_exit;
} }
@@ -454,7 +454,7 @@ BusCommand* BusLoop::sendCommand()
} }
// MM -> send SYN // MM -> send SYN
if (busCommand->getType() == masterMaster) { if (message->getType() == masterMaster) {
sendByte(SYN); sendByte(SYN);
goto on_exit; goto on_exit;
} }
@@ -504,12 +504,12 @@ on_exit:
while (m_port->size() != 0) while (m_port->size() != 0)
recvByte = fetchByte(); recvByte = fetchByte();
busCommand->setResult(slaveData, retval); message->setResult(slaveData, retval);
if (retval == RESULT_OK) if (retval == RESULT_OK)
return m_sendBuffer.remove(); return m_busQueue.remove();
else else
return busCommand; return message;
} }
@@ -637,9 +637,9 @@ void BusLoop::collectSlave()
} }
} }
void BusLoop::addScanCommand() void BusLoop::addScanMessage()
{ {
std::string ebusCommand(A.getOptVal<const char*>("address")); std::string busCommand(A.getOptVal<const char*>("address"));
std::stringstream sstr; std::stringstream sstr;
if (m_scanFull == true) { if (m_scanFull == true) {
@@ -665,16 +665,16 @@ void BusLoop::addScanCommand()
else { else {
m_scanIndex++; m_scanIndex++;
ebusCommand += sstr.str(); busCommand += sstr.str();
ebusCommand += "070400"; busCommand += "070400";
std::transform(ebusCommand.begin(), ebusCommand.end(), ebusCommand.begin(), tolower); std::transform(busCommand.begin(), busCommand.end(), busCommand.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); BusMessage* message = new BusMessage(busCommand, true, true);
L.log(bus, trace, " msg: %s", ebusCommand.c_str()); L.log(bus, trace, " msg: %s", busCommand.c_str());
addBusCommand(busCommand); addBusMessage(message);
} }
} }
+11 -11
View File
@@ -31,16 +31,16 @@
/** the maximum time [us] allowed for retrieving a byte from an addressed slave */ /** the maximum time [us] allowed for retrieving a byte from an addressed slave */
#define RECV_TIMEOUT 10000 #define RECV_TIMEOUT 10000
enum CommandType { invalid, broadcast, masterMaster, masterSlave }; enum MessageType { invalid, broadcast, masterMaster, masterSlave };
class BusCommand class BusMessage
{ {
public: public:
BusCommand(const std::string command, const bool poll, const bool scan); BusMessage(const std::string command, const bool poll, const bool scan);
~BusCommand(); ~BusMessage();
CommandType getType() const { return m_type; } MessageType getType() const { return m_type; }
bool isPoll() const { return m_poll; } bool isPoll() const { return m_poll; }
bool isScan() const { return m_scan; } bool isScan() const { return m_scan; }
@@ -58,7 +58,7 @@ public:
void sendSignal() { pthread_cond_signal(&m_cond); } void sendSignal() { pthread_cond_signal(&m_cond); }
private: private:
CommandType m_type; MessageType m_type;
bool m_poll; bool m_poll;
bool m_scan; bool m_scan;
SymbolString m_command; SymbolString m_command;
@@ -80,7 +80,7 @@ public:
void* run(); void* run();
void stop() { m_stop = true; } 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 dump() { m_dumpState == true ? m_dumpState = false : m_dumpState = true ; }
void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; } void raw() { m_logRawData == true ? m_logRawData = false : m_logRawData = true ; }
@@ -103,7 +103,7 @@ private:
int m_lockCounter; int m_lockCounter;
bool m_priorRetry; bool m_priorRetry;
WQueue<BusCommand*> m_sendBuffer; WQueue<BusMessage*> m_busQueue;
SymbolString m_sstr; SymbolString m_sstr;
double m_pollInterval; double m_pollInterval;
@@ -121,14 +121,14 @@ private:
unsigned char fetchByte(); unsigned char fetchByte();
void collectCycData(const int numRecv); void collectCycData(const int numRecv);
void analyseCycData(); void analyseCycData();
void addPollCommand(); void addPollMessage();
int acquireBus(); int acquireBus();
BusCommand* sendCommand(); BusMessage* sendCommand();
int sendByte(const unsigned char sendByte); int sendByte(const unsigned char sendByte);
int recvSlaveAck(unsigned char& recvByte); int recvSlaveAck(unsigned char& recvByte);
int recvSlaveData(SymbolString& result); int recvSlaveData(SymbolString& result);
void collectSlave(); void collectSlave();
void addScanCommand(); void addScanMessage();
}; };
+3 -3
View File
@@ -123,7 +123,7 @@ int Commands::findCommand(const std::string& data) const
return -1; 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); cmd_t command = m_cmdDB.at(index);
std::string cmd; std::string cmd;
@@ -159,7 +159,7 @@ int Commands::storeCycData(const std::string& data) const
// walk through commands // walk through commands
for (; iter != m_cycDB.end(); iter++) { for (; iter != m_cycDB.end(); iter++) {
std::string command = getEbusCommand(iter->first); std::string command = getBusCommand(iter->first);
// skip wrong search string length // skip wrong search string length
if (command.length() > search.length()) if (command.length() > search.length())
@@ -212,7 +212,7 @@ void Commands::storePollData(const std::string& data) const
// walk through commands // walk through commands
for (; iter != m_pollDB.end(); iter++) { for (; iter != m_pollDB.end(); iter++) {
std::string command = getEbusCommand(iter->first); std::string command = getBusCommand(iter->first);
// skip wrong search string length // skip wrong search string length
if (command.length() > search.length()) if (command.length() > search.length())
+1 -1
View File
@@ -52,7 +52,7 @@ public:
int findCommand(const std::string& data) const; int findCommand(const std::string& data) const;
std::string getCmdType(const int index) const { return std::string(m_cmdDB.at(index)[0]); } 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; int storeCycData(const std::string& data) const;
std::string getCycData(int index) const; std::string getCycData(int index) const;