-bool CCECParser::ReadFromDevice(int iTimeout)
-{
- uint8_t buff[1024];
- int iBytesRead = m_serialport->Read(buff, sizeof(buff), iTimeout);
- if (iBytesRead < 0)
- {
- CStdString strError;
- strError.Format("error reading from serial port: %s", m_serialport->GetError().c_str());
- AddLog(CEC_LOG_ERROR, strError);
- return false;
- }
- else if (iBytesRead > 0)
- AddData(buff, iBytesRead);
-
- return true;
-}
-
-void CCECParser::ProcessMessages(void)
-{
- cec_frame msg;
- while (GetMessage(msg))
- ParseMessage(msg);
-}
-
-bool CCECParser::GetMessage(cec_frame &msg, bool bFromBuffer /* = true */)
-{
- if (bFromBuffer && m_frameBuffer.Pop(msg))
- return true;
-
- if (m_iInbufUsed < 1)
- return false;
-
- //search for first start of message
- int startpos = -1;
- for (int i = 0; i < m_iInbufUsed; i++)
- {
- if (m_inbuf[i] == MSGSTART)
- {
- startpos = i;
- break;
- }
- }
-
- if (startpos == -1)
- return false;
-
- //move anything from the first start of message to the beginning of the buffer
- if (startpos > 0)
- {
- memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
- m_iInbufUsed -= startpos;
- }
-
- if (m_iInbufUsed < 2)
- return false;
-
- //look for end of message
- startpos = -1;
- int endpos = -1;
- for (int i = 1; i < m_iInbufUsed; i++)
- {
- if (m_inbuf[i] == MSGEND)
- {
- endpos = i;
- break;
- }
- else if (m_inbuf[i] == MSGSTART)
- {
- startpos = i;
- break;
- }
- }
-
- if (startpos > 0) //we found a msgstart before msgend, this is not right, remove
- {
- AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
- memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos);
- m_iInbufUsed -= startpos;
- return false;
- }
-
- if (endpos > 0) //found a MSGEND
- {
- msg.clear();
- bool isesc = false;
- for (int i = 1; i < endpos; i++)
- {
- if (isesc)
- {
- msg.push_back(m_inbuf[i] + (uint8_t)ESCOFFSET);
- isesc = false;
- }
- else if (m_inbuf[i] == MSGESC)
- {
- isesc = true;
- }
- else
- {
- msg.push_back(m_inbuf[i]);
- }
- }
-
- if (endpos + 1 < m_iInbufUsed)
- memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1);
-
- m_iInbufUsed -= endpos + 1;
-
- return true;
- }
-
- return false;
-}
-