- bool bError(false);
- bool bTransmitSucceeded(false);
- uint8_t iPacketsLeft(message.Size() / 4);
-
- int64_t iNow = GetTimeMs();
- int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout);
-
- while (!bTransmitSucceeded && !bError && iNow < iTargetTime)
- {
- ReadFromDevice(50);
- CCECAdapterMessage msg;
- if (!Read(msg, 0))
- {
- iNow = GetTimeMs();
- continue;
- }
-
- if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
- {
- m_processor->HandlePoll(msg.Initiator(), msg.Destination());
- m_lastInitiator = msg.Initiator();
- iNow = GetTimeMs();
- continue;
- }
-
- if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
- m_lastInitiator != CECDEVICE_UNKNOWN &&
- m_processor->HandleReceiveFailed(m_lastInitiator))
- {
- 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:
- if (iPacketsLeft > 0)
- iPacketsLeft--;
- if (!message.isTransmission && iPacketsLeft == 0)
- bTransmitSucceeded = true;
- CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - waiting for %d more", msg.ToString().c_str(), iPacketsLeft);
- 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();
- }
- }