X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fadapter%2FAdapterCommunication.cpp;h=ff69a7f4a73f2aeb6a10cf43bb9a888e21190330;hb=ba65909d0a9c43a1bac71c6182c53f202285cec5;hp=4a136ab113f4bef0a8e1fcdd83b19506d49f7eb1;hpb=ef7696f555d4051a4412346939f3da4c649fb128;p=deb_libcec.git diff --git a/src/lib/adapter/AdapterCommunication.cpp b/src/lib/adapter/AdapterCommunication.cpp index 4a136ab..ff69a7f 100644 --- a/src/lib/adapter/AdapterCommunication.cpp +++ b/src/lib/adapter/AdapterCommunication.cpp @@ -33,8 +33,10 @@ #include "AdapterCommunication.h" #include "AdapterMessage.h" -#include "CECProcessor.h" -#include "platform/serialport/serialport.h" +#include "../CECProcessor.h" +#include "../platform/sockets/serialport.h" +#include "../platform/util/timeutils.h" +#include "../LibCEC.h" using namespace std; using namespace CEC; @@ -43,7 +45,8 @@ using namespace PLATFORM; CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) : m_port(NULL), m_processor(processor), - m_iLineTimeout(0) + m_iLineTimeout(0), + m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN) { m_port = new PLATFORM::CSerialPort; } @@ -68,13 +71,13 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 if (!m_port) { - m_processor->AddLog(CEC_LOG_ERROR, "port is NULL"); + CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL"); return false; } if (IsOpen()) { - m_processor->AddLog(CEC_LOG_ERROR, "port is already open"); + CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open"); return true; } @@ -92,24 +95,28 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 if (!bConnected) { - m_processor->AddLog(CEC_LOG_ERROR, strError); + CLibCEC::AddLog(CEC_LOG_ERROR, strError); return false; } - m_processor->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()) { - m_processor->AddLog(CEC_LOG_DEBUG, "communication thread started"); + CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started"); return true; } else { - m_processor->AddLog(CEC_LOG_ERROR, "could not create a communication thread"); + CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread"); } return false; @@ -140,9 +147,37 @@ void *CAdapterCommunication::Process(void) bool CAdapterCommunication::Write(CCECAdapterMessage *data) { - data->state = ADAPTER_MESSAGE_STATE_WAITING; + bool bReturn(false); + + CLockObject lock(data->mutex); + data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT; m_outBuffer.Push(data); - return true; + data->condition.Wait(data->mutex); + + if (data->state != ADAPTER_MESSAGE_STATE_SENT) + { + CLibCEC::AddLog(CEC_LOG_ERROR, "command was not sent"); + } + else if (data->expectControllerAck) + { + bReturn = WaitForAck(*data); + if (bReturn) + { + if (data->isTransmission) + data->state = ADAPTER_MESSAGE_STATE_SENT_ACKED; + } + else + { + data->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED; + CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack"); + } + } + else + { + bReturn = true; + } + + return bReturn; } bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) @@ -174,7 +209,7 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove { if (msg.Size() > 0) - m_processor->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); + CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); msg.Clear(); bGotStart = true; } @@ -195,14 +230,16 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) } if (bGotFullMessage) - msg.state = ADAPTER_MESSAGE_STATE_RECEIVED; + msg.state = ADAPTER_MESSAGE_STATE_INCOMING; 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) @@ -211,17 +248,17 @@ bool CAdapterCommunication::StartBootloader(void) if (!IsRunning()) return bReturn; - m_processor->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); + CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader"); CCECAdapterMessage *output = new CCECAdapterMessage; output->PushBack(MSGSTART); output->PushEscaped(MSGCODE_START_BOOTLOADER); output->PushBack(MSGEND); + output->isTransmission = false; + output->expectControllerAck = false; - CLockObject lock(output->mutex); - if (Write(output)) - output->condition.Wait(output->mutex); - bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; + if ((bReturn = Write(output)) == false) + CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader"); delete output; return bReturn; @@ -233,22 +270,54 @@ bool CAdapterCommunication::PingAdapter(void) if (!IsRunning()) return bReturn; - m_processor->AddLog(CEC_LOG_DEBUG, "sending ping"); + CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping"); CCECAdapterMessage *output = new CCECAdapterMessage; output->PushBack(MSGSTART); output->PushEscaped(MSGCODE_PING); output->PushBack(MSGEND); + output->isTransmission = false; - CLockObject lock(output->mutex); - if (Write(output)) - output->condition.Wait(output->mutex); - bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; + if ((bReturn = Write(output)) == false) + CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter"); delete output; return bReturn; } +uint16_t CAdapterCommunication::GetFirmwareVersion(void) +{ + uint16_t iReturn(m_iFirmwareVersion); + if (!IsRunning()) + return iReturn; + + if (iReturn == CEC_FW_VERSION_UNKNOWN) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); + CCECAdapterMessage *output = new CCECAdapterMessage; + + output->PushBack(MSGSTART); + output->PushEscaped(MSGCODE_FIRMWARE_VERSION); + output->PushBack(MSGEND); + output->isTransmission = false; + output->expectControllerAck = false; + + SendMessageToAdapter(output); + delete output; + + 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 (size = %d, message = %d)", input.Size(), input.Message()); + else + { + m_iFirmwareVersion = (input[1] << 8 | input[2]); + iReturn = m_iFirmwareVersion; + } + } + + return iReturn; +} + bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout) { bool bReturn(m_iLineTimeout != iTimeout); @@ -261,20 +330,115 @@ bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout) output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); output->PushEscaped(iTimeout); output->PushBack(MSGEND); + output->isTransmission = false; if ((bReturn = Write(output)) == false) - m_processor->AddLog(CEC_LOG_ERROR, "could not set the idletime"); + CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime"); delete output; } return bReturn; } +bool CAdapterCommunication::SetAckMask(uint16_t iMask) +{ + bool bReturn(false); + CStdString strLog; + strLog.Format("setting ackmask to %2x", iMask); + CLibCEC::AddLog(CEC_LOG_DEBUG, strLog.c_str()); + + CCECAdapterMessage *output = new CCECAdapterMessage; + + output->PushBack(MSGSTART); + output->PushEscaped(MSGCODE_SET_ACK_MASK); + output->PushEscaped(iMask >> 8); + output->PushEscaped((uint8_t)iMask); + output->PushBack(MSGEND); + output->isTransmission = false; + + if ((bReturn = Write(output)) == false) + CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask"); + delete output; + + return bReturn; +} + bool CAdapterCommunication::IsOpen(void) { return !IsStopped() && m_port->IsOpen() && IsRunning(); } +bool CAdapterCommunication::WaitForAck(CCECAdapterMessage &message) +{ + bool bError(false); + bool bTransmitSucceeded(false); + uint8_t iPacketsLeft(message.Size() / 4); + + int64_t iNow = GetTimeMs(); + int64_t iTargetTime = iNow + message.transmit_timeout; + + 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, iWait)) + { + iNow = GetTimeMs(); + continue; + } + + if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) + { + m_processor->HandlePoll(msg.Initiator(), msg.Destination()); + iNow = GetTimeMs(); + continue; + } + + if (msg.Message() == MSGCODE_RECEIVE_FAILED && + m_processor->HandleReceiveFailed()) + { + iNow = GetTimeMs(); + continue; + } + + bError = msg.IsError(); + if (bError) + { + message.reply = msg.Message(); + CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); + } + else + { + switch(msg.Message()) + { + case MSGCODE_COMMAND_ACCEPTED: + CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString()); + if (iPacketsLeft > 0) + iPacketsLeft--; + 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; + break; + default: + // ignore other data while waiting + break; + } + + iNow = GetTimeMs(); + } + } + + return bTransmitSucceeded && !bError; +} + void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) { CLockObject lock(m_mutex); @@ -291,12 +455,13 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) if (!m_port) return false; + CLockObject lock(m_mutex); iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); if (iBytesRead < 0 || iBytesRead > 256) { CStdString strError; strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); - m_processor->AddLog(CEC_LOG_ERROR, strError); + CLibCEC::AddLog(CEC_LOG_ERROR, strError); return false; } else if (iBytesRead > 0) @@ -307,17 +472,18 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) { + CLockObject adapterLock(m_mutex); CLockObject lock(msg->mutex); if (m_port->Write(msg->packet.data, msg->Size()) != (int32_t) msg->Size()) { CStdString strError; strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); - m_processor->AddLog(CEC_LOG_ERROR, strError); + CLibCEC::AddLog(CEC_LOG_ERROR, strError); msg->state = ADAPTER_MESSAGE_STATE_ERROR; } else { - m_processor->AddLog(CEC_LOG_DEBUG, "command sent"); + CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent"); msg->state = ADAPTER_MESSAGE_STATE_SENT; } msg->condition.Signal();