X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fadapter%2FUSBCECAdapterCommunication.cpp;h=99086ce94f54dbbc4e88cc692437e2e210fca0dd;hb=e69d8f191784c77c156ffec90db50767f91421e3;hp=82c4252d27eb314d993c3178357cc18dfddc026e;hpb=d28b4393c62e4ae4d030251cb7838f9a02a1492e;p=deb_libcec.git diff --git a/src/lib/adapter/USBCECAdapterCommunication.cpp b/src/lib/adapter/USBCECAdapterCommunication.cpp index 82c4252..99086ce 100644 --- a/src/lib/adapter/USBCECAdapterCommunication.cpp +++ b/src/lib/adapter/USBCECAdapterCommunication.cpp @@ -66,20 +66,17 @@ CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processo m_bHasData(false), m_iLineTimeout(0), m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN), - m_lastInitiator(CECDEVICE_UNKNOWN), + m_lastDestination(CECDEVICE_UNKNOWN), m_bNextIsEscaped(false), m_bGotStart(false), m_messageProcessor(NULL), m_bInitialised(false) { + for (unsigned int iPtr = 0; iPtr < 15; iPtr++) + m_bWaitingForAck[iPtr] = false; m_port = new CSerialPort(strPort, iBaudRate); } -CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void) -{ - Close(); -} - bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */) { bool bReturn(false); @@ -181,9 +178,20 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32 { //clear any input bytes uint8_t buff[1024]; - while (m_port->Read(buff, 1024, 100) > 0) + ssize_t iBytesRead(0); + bool bGotMsgStart(false), bGotMsgEnd(false); + while ((iBytesRead = m_port->Read(buff, 1024, 100)) > 0 || (bGotMsgStart && !bGotMsgEnd)) { - CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); + if (!bGotMsgStart) + CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it"); + // if something was received, wait for MSGEND + for (ssize_t iPtr = 0; iPtr < iBytesRead; iPtr++) + { + if (buff[iPtr] == MSGSTART) + bGotMsgStart = true; + else if (buff[iPtr] == MSGEND) + bGotMsgEnd = true; + } Sleep(250); } } @@ -192,6 +200,7 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32 if (!bSkipChecks && !CheckAdapter()) { CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks"); + Close(); return false; } else @@ -203,6 +212,7 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32 } else { + Close(); CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread"); } } @@ -212,7 +222,7 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32 void CUSBCECAdapterCommunication::Close(void) { - StopThread(); + StopThread(0); } void *CUSBCECAdapterCommunication::Process(void) @@ -292,6 +302,11 @@ cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command & output->retryTimeout = iRetryLineTimeout; output->tries = 0; + { + CLockObject lock(m_mutex); + m_bWaitingForAck[data.destination] = true; + } + bool bRetry(true); while (bRetry && ++output->tries < output->maxTries) { @@ -415,16 +430,19 @@ bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg) } if (m_currentframe.ack == 0x1) { - m_lastInitiator = m_currentframe.initiator; - m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination); + m_lastDestination = m_currentframe.destination; + if (!m_bWaitingForAck[m_currentframe.destination]) + m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination); + else + m_bWaitingForAck[m_currentframe.destination] = false; } } break; case MSGCODE_RECEIVE_FAILED: { m_currentframe.Clear(); - if (m_lastInitiator != CECDEVICE_UNKNOWN) - bIsError = m_processor->HandleReceiveFailed(m_lastInitiator); + if (m_lastDestination != CECDEVICE_UNKNOWN) + bIsError = m_processor->HandleReceiveFailed(m_lastDestination); } break; case MSGCODE_FRAME_DATA: @@ -466,15 +484,16 @@ uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void) bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout) { - m_iLineTimeout = iTimeout; - bool bReturn(m_iLineTimeout != iTimeout); + bool bReturn(true); - if (!bReturn) + if (m_iLineTimeout != iTimeout) { CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the line timeout to %d", iTimeout); CCECAdapterMessage params; params.PushEscaped(iTimeout); bReturn = SendCommand(MSGCODE_TRANSMIT_IDLETIME, params); + if (bReturn) + m_iLineTimeout = iTimeout; } return bReturn; @@ -684,7 +703,7 @@ bool CUSBCECAdapterCommunication::GetSettingLogicalAddressMask(uint16_t &iMask) bool CUSBCECAdapterCommunication::SetSettingPhysicalAddress(uint16_t iPhysicalAddress) { CLockObject lock(m_mutex); - CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the physical address to %2X", iPhysicalAddress); + CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the physical address to %04X", iPhysicalAddress); CCECAdapterMessage params; params.PushEscaped(iPhysicalAddress >> 8); @@ -794,15 +813,20 @@ bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message) if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK()) { - m_processor->HandlePoll(msg.Initiator(), msg.Destination()); - m_lastInitiator = msg.Initiator(); + if (m_bWaitingForAck[msg.Initiator()]) + m_bWaitingForAck[msg.Initiator()] = false; + else + { + m_processor->HandlePoll(msg.Initiator(), msg.Destination()); + m_lastDestination = msg.Initiator(); + } iNow = GetTimeMs(); continue; } if (msg.Message() == MSGCODE_RECEIVE_FAILED && - m_lastInitiator != CECDEVICE_UNKNOWN && - m_processor->HandleReceiveFailed(m_lastInitiator)) + m_lastDestination != CECDEVICE_UNKNOWN && + m_processor->HandleReceiveFailed(m_lastDestination)) { iNow = GetTimeMs(); continue; @@ -967,14 +991,14 @@ CStdString CUSBCECAdapterCommunication::GetPortName(void) return strName; } -bool CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage ¶ms, bool bExpectAck /* = true */, bool bIsTransmission /* = false */, bool bSendDirectly /* = true */) +bool CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage ¶ms, bool bExpectAck /* = true */, bool bIsTransmission /* = false */, bool bSendDirectly /* = true */, bool bIsRetry /* = false */) { CLockObject lock(m_mutex); CCECAdapterMessage *output = new CCECAdapterMessage; output->PushBack(MSGSTART); - output->PushEscaped(msgCode); + output->PushEscaped((uint8_t)msgCode); output->Append(params); output->PushBack(MSGEND); output->isTransmission = bIsTransmission; @@ -990,6 +1014,13 @@ bool CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode, C { CLibCEC::AddLog(CEC_LOG_ERROR, "'%s' failed", CCECAdapterMessage::ToString(msgCode)); delete output; + + if (!bIsRetry && output->reply == MSGCODE_COMMAND_REJECTED && msgCode != MSGCODE_SET_CONTROLLED) + { + CLibCEC::AddLog(CEC_LOG_DEBUG, "setting controlled mode and retrying"); + if (SetControlledMode(true)) + return SendCommand(msgCode, params, bExpectAck, bIsTransmission, bSendDirectly, true); + } return false; }