-void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen)
-{
- CLockObject lock(m_mutex);
- for (size_t iPtr = 0; iPtr < iLen; iPtr++)
- {
- if (!m_bGotStart)
- {
- if (data[iPtr] == MSGSTART)
- m_bGotStart = true;
- }
- else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove
- {
- if (m_currentAdapterMessage.Size() > 0)
- CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
- m_currentAdapterMessage.Clear();
- m_bGotStart = true;
- }
- else if (data[iPtr] == MSGEND)
- {
- CCECAdapterMessage *newMessage = new CCECAdapterMessage;
- newMessage->packet = m_currentAdapterMessage.packet;
- m_inBuffer.Push(newMessage);
- m_currentAdapterMessage.Clear();
- m_bGotStart = false;
- m_bNextIsEscaped = false;
- m_bHasData = true;
- m_rcvCondition.Broadcast();
- }
- else if (m_bNextIsEscaped)
- {
- m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET);
- m_bNextIsEscaped = false;
- }
- else if (data[iPtr] == MSGESC)
- {
- m_bNextIsEscaped = true;
- }
- else
- {
- m_currentAdapterMessage.PushBack(data[iPtr]);
- }
- }
-}
-
-bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */)
-{
- ssize_t iBytesRead;
- uint8_t buff[256];
- if (!m_port)
- return false;
- if (iSize > 256)
- iSize = 256;
-
- CLockObject lock(m_mutex);
- iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
- if (iBytesRead < 0 || iBytesRead > 256)
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
- StopThread(false);
- return false;
- }
- else if (iBytesRead > 0)
- {
- AddData(buff, iBytesRead);
- }
-
- return iBytesRead > 0;
-}
-
-void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
-{
- CLockObject adapterLock(m_mutex);
- if (!m_port->IsOpen())
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: the connection is closed");
- msg->state = ADAPTER_MESSAGE_STATE_ERROR;
- return;
- }
-
- if (msg->isTransmission && (msg->Size() < 2 || msg->At(1) != MSGCODE_TRANSMIT_IDLETIME))
- {
- if (msg->tries == 1)
- SetLineTimeout(msg->lineTimeout);
- else
- SetLineTimeout(msg->retryTimeout);
- }
-
- if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size())
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str());
- msg->state = ADAPTER_MESSAGE_STATE_ERROR;
- }
- else
- {
- CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent");
- msg->state = ADAPTER_MESSAGE_STATE_SENT;
-
- if (msg->expectControllerAck)
- {
- if (!WaitForAck(*msg))
- CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack");
- }