X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fadapter%2FAdapterCommunication.cpp;h=ff69a7f4a73f2aeb6a10cf43bb9a888e21190330;hb=ba65909d0a9c43a1bac71c6182c53f202285cec5;hp=e143b08c3f9054e2a0e5b307df9e1600c4331c59;hpb=b5f787b969b7e0517321ebbfe069ac65c330982e;p=deb_libcec.git diff --git a/src/lib/adapter/AdapterCommunication.cpp b/src/lib/adapter/AdapterCommunication.cpp index e143b08..ff69a7f 100644 --- a/src/lib/adapter/AdapterCommunication.cpp +++ b/src/lib/adapter/AdapterCommunication.cpp @@ -34,7 +34,8 @@ #include "AdapterMessage.h" #include "../CECProcessor.h" -#include "../platform/serialport/serialport.h" +#include "../platform/sockets/serialport.h" +#include "../platform/util/timeutils.h" #include "../LibCEC.h" using namespace std; @@ -98,11 +99,15 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 return false; } - CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened"); + CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting"); //clear any input bytes - uint8_t buff[1]; - while (m_port->Read(buff, 1, 5) == 1) {} + uint8_t buff[1024]; + while (m_port->Read(buff, 1024, 100) > 0) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); + Sleep(250); + } if (CreateThread()) { @@ -153,10 +158,9 @@ bool CAdapterCommunication::Write(CCECAdapterMessage *data) { CLibCEC::AddLog(CEC_LOG_ERROR, "command was not sent"); } - - if (data->expectControllerAck) + else if (data->expectControllerAck) { - bReturn = WaitForTransmitSucceeded(data); + bReturn = WaitForAck(*data); if (bReturn) { if (data->isTransmission) @@ -231,9 +235,11 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) return bGotFullMessage; } -std::string CAdapterCommunication::GetError(void) const +CStdString CAdapterCommunication::GetError(void) const { - return m_port->GetError(); + CStdString strError; + strError = m_port->GetError(); + return strError; } bool CAdapterCommunication::StartBootloader(void) @@ -301,7 +307,7 @@ uint16_t CAdapterCommunication::GetFirmwareVersion(void) CCECAdapterMessage input; if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3) - CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version"); + CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version (size = %d, message = %d)", input.Size(), input.Message()); else { m_iFirmwareVersion = (input[1] << 8 | input[2]); @@ -362,20 +368,23 @@ bool CAdapterCommunication::IsOpen(void) return !IsStopped() && m_port->IsOpen() && IsRunning(); } -bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message) +bool CAdapterCommunication::WaitForAck(CCECAdapterMessage &message) { bool bError(false); bool bTransmitSucceeded(false); - uint8_t iPacketsLeft(message->Size() / 4); + uint8_t iPacketsLeft(message.Size() / 4); int64_t iNow = GetTimeMs(); - int64_t iTargetTime = iNow + message->transmit_timeout; + int64_t iTargetTime = iNow + message.transmit_timeout; - while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime)) + while (!bTransmitSucceeded && !bError && (message.transmit_timeout == 0 || iNow < iTargetTime)) { CCECAdapterMessage msg; + int32_t iWait = (int32_t)(iTargetTime - iNow); + if (iWait <= 5 || message.transmit_timeout <= 5) + iWait = CEC_DEFAULT_TRANSMIT_WAIT; - if (!Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) + if (!Read(msg, iWait)) { iNow = GetTimeMs(); continue; @@ -398,7 +407,7 @@ bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message bError = msg.IsError(); if (bError) { - message->reply = msg.Message(); + message.reply = msg.Message(); CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); } else @@ -409,14 +418,14 @@ bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); if (iPacketsLeft > 0) iPacketsLeft--; - if (!message->isTransmission && iPacketsLeft == 0) + if (!message.isTransmission && iPacketsLeft == 0) bTransmitSucceeded = true; break; case MSGCODE_TRANSMIT_SUCCEEDED: CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); bTransmitSucceeded = (iPacketsLeft == 0); bError = !bTransmitSucceeded; - message->reply = MSGCODE_TRANSMIT_SUCCEEDED; + message.reply = MSGCODE_TRANSMIT_SUCCEEDED; break; default: // ignore other data while waiting