X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2FAdapterCommunication.cpp;h=440226b3e84eb9f12aa357cbf68f472ff4549dba;hb=61742d42b79639e3fbf0aafa43f437d9a1c149e9;hp=1aaf3c8573c4efac9784650b28f939006036177b;hpb=c7903c025a521e18ce9e33b17679d14640365eaa;p=deb_libcec.git diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 1aaf3c8..440226b 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -64,7 +64,7 @@ CCECAdapterMessage::CCECAdapterMessage(const cec_command &command) if (command.opcode_set == 1) { push_back(MSGSTART); - push_escaped(command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT); + push_escaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT); push_back((uint8_t) command.opcode); push_back(MSGEND); @@ -229,7 +229,7 @@ bool CCECAdapterMessage::is_error(void) const void CCECAdapterMessage::push_escaped(uint8_t byte) { - if (byte >= MSGESC && byte != MSGSTART) + if (byte >= MSGESC) { push_back(MSGESC); push_back(byte - ESCOFFSET); @@ -282,7 +282,7 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 //clear any input bytes uint8_t buff[1024]; - m_port->Read(buff, sizeof(buff), 500); + while (m_port->Read(buff, sizeof(buff), 500) > 0) {} if (CreateThread()) { @@ -357,23 +357,26 @@ void CAdapterCommunication::WriteNextCommand(void) { CCECAdapterMessage *msg; if (m_outBuffer.Pop(msg)) + SendMessageToAdapter(msg); +} + +void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) +{ + CLockObject lock(&msg->mutex); + if (m_port->Write(msg) != (int32_t) msg->size()) { - CLockObject lock(&msg->mutex); - if (m_port->Write(msg) != (int32_t) msg->size()) - { - CStdString strError; - strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); - m_controller->AddLog(CEC_LOG_ERROR, strError); - msg->state = ADAPTER_MESSAGE_STATE_ERROR; - } - else - { - m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); - CCondition::Sleep((uint32_t) msg->size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 10); - msg->state = ADAPTER_MESSAGE_STATE_SENT; - } - msg->condition.Signal(); + CStdString strError; + strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); + m_controller->AddLog(CEC_LOG_ERROR, strError); + msg->state = ADAPTER_MESSAGE_STATE_ERROR; } + else + { + m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); + CCondition::Sleep((uint32_t) msg->size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 10); + msg->state = ADAPTER_MESSAGE_STATE_SENT; + } + msg->condition.Signal(); } bool CAdapterCommunication::Write(CCECAdapterMessage *data) @@ -455,9 +458,8 @@ bool CAdapterCommunication::StartBootloader(void) output->push_escaped(MSGCODE_START_BOOTLOADER); output->push_back(MSGEND); - if ((bReturn = Write(output)) == false) - m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); - + SendMessageToAdapter(output); + bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; delete output; return bReturn; @@ -476,10 +478,8 @@ bool CAdapterCommunication::PingAdapter(void) output->push_escaped(MSGCODE_PING); output->push_back(MSGEND); - if ((bReturn = Write(output)) == false) - m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); - - // TODO check for pong + SendMessageToAdapter(output); + bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; delete output; return bReturn;