cec: fix GetTimeMs() on linux. add time in ms since startup to log messages
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 74f03b379698d3965d48e6cdef662358d1b1d70f..1946556a6f4edcd9f027094ada1c97d462808819 100644 (file)
@@ -59,7 +59,10 @@ CCECProcessor::~CCECProcessor(void)
 bool CCECProcessor::Start(void)
 {
   if (!m_communication || !m_communication->IsOpen())
+  {
+    m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
     return false;
+  }
 
   if (!SetLogicalAddress(m_iLogicalAddress))
   {
@@ -79,26 +82,31 @@ void *CCECProcessor::Process(void)
 {
   m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
 
-  while (!m_bStop)
+  while (!IsStopped())
   {
     bool bParseFrame(false);
+    cec_frame msg;
+    msg.clear();
+
     {
       CLockObject lock(&m_mutex);
-      cec_frame msg;
-      msg.clear();
+      if (m_communication->IsOpen() && m_communication->Read(msg, 50))
+        bParseFrame = ParseMessage(msg) && !IsStopped();
 
-      if (!m_bStop && m_communication->IsOpen() && m_communication->Read(msg, CEC_BUTTON_TIMEOUT))
-        bParseFrame = ParseMessage(msg);
+      if (bParseFrame)
+      {
+        msg.clear();
+        msg = m_currentframe;
+      }
     }
 
-    if (!m_bStop && bParseFrame)
-      ParseCurrentFrame();
+    if (bParseFrame)
+      ParseCurrentFrame(msg);
 
-    if (!m_bStop)
-    {
-      m_controller->CheckKeypressTimeout();
+    m_controller->CheckKeypressTimeout();
+
+    if (!IsStopped())
       Sleep(50);
-    }
   }
 
   return NULL;
@@ -116,7 +124,7 @@ bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV
   frame.clear();
 
   frame.push_back(GetSourceDestination(address));
-  frame.push_back((uint8_t) CEC_OPCODE_TEXT_VIEW_ON);
+  frame.push_back((uint8_t) CEC_OPCODE_IMAGE_VIEW_ON);
   return Transmit(frame);
 }
 
@@ -406,7 +414,6 @@ bool CCECProcessor::WaitForAck(uint32_t iTimeout /* = 1000 */)
         break;
       default:
         m_frameBuffer.Push(msg);
-        bGotAck = (msg.data[0] & MSGCODE_FRAME_ACK) != 0;
         break;
       }
       iNow = GetTimeMs();
@@ -487,26 +494,26 @@ bool CCECProcessor::ParseMessage(cec_frame &msg)
   return bReturn;
 }
 
-void CCECProcessor::ParseCurrentFrame(void)
+void CCECProcessor::ParseCurrentFrame(cec_frame &frame)
 {
-  uint8_t initiator = m_currentframe.data[0] >> 4;
-  uint8_t destination = m_currentframe.data[0] & 0xF;
+  uint8_t initiator = frame.data[0] >> 4;
+  uint8_t destination = frame.data[0] & 0xF;
 
   CStdString dataStr;
   dataStr.Format("received frame: initiator: %u destination: %u", initiator, destination);
 
-  if (m_currentframe.size > 1)
+  if (frame.size > 1)
   {
     dataStr += " data:";
-    for (unsigned int i = 1; i < m_currentframe.size; i++)
-      dataStr.AppendFormat(" %02x", m_currentframe.data[i]);
+    for (unsigned int i = 1; i < frame.size; i++)
+      dataStr.AppendFormat(" %02x", frame.data[i]);
   }
   m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str());
 
-  if (m_currentframe.size <= 1)
+  if (frame.size <= 1)
     return;
 
-  cec_opcode opCode = (cec_opcode) m_currentframe.data[1];
+  cec_opcode opCode = (cec_opcode) frame.data[1];
   if (destination == (uint16_t) m_iLogicalAddress)
   {
     switch(opCode)
@@ -531,19 +538,19 @@ void CCECProcessor::ParseCurrentFrame(void)
       ReportCECVersion((cec_logical_address)initiator);
       break;
     case CEC_OPCODE_USER_CONTROL_PRESSED:
-      if (m_currentframe.size > 2)
+      if (frame.size > 2)
       {
         m_controller->AddKey();
 
-        if (m_currentframe.data[2] <= CEC_USER_CONTROL_CODE_MAX)
-          m_controller->SetCurrentButton((cec_user_control_code) m_currentframe.data[2]);
+        if (frame.data[2] <= CEC_USER_CONTROL_CODE_MAX)
+          m_controller->SetCurrentButton((cec_user_control_code) frame.data[2]);
       }
       break;
     case CEC_OPCODE_USER_CONTROL_RELEASE:
       m_controller->AddKey();
       break;
     default:
-      cec_frame params = m_currentframe;
+      cec_frame params = frame;
       params.shift(2);
       m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &params);
       break;
@@ -551,16 +558,18 @@ void CCECProcessor::ParseCurrentFrame(void)
   }
   else if (destination == (uint8_t) CECDEVICE_BROADCAST)
   {
+    CStdString strLog;
     if (opCode == CEC_OPCODE_REQUEST_ACTIVE_SOURCE)
     {
+      strLog.Format("%i requests active source", initiator);
+      m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
       BroadcastActiveSource();
     }
     else if (opCode == CEC_OPCODE_SET_STREAM_PATH)
     {
-      if (m_currentframe.size >= 4)
+      if (frame.size >= 4)
       {
-        int streamaddr = ((int)m_currentframe.data[2] << 8) | ((int)m_currentframe.data[3]);
-        CStdString strLog;
+        int streamaddr = ((int)frame.data[2] << 8) | ((int)frame.data[3]);
         strLog.Format("%i requests stream path from physical address %04x", initiator, streamaddr);
         m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
         if (streamaddr == m_physicaladdress)
@@ -569,7 +578,7 @@ void CCECProcessor::ParseCurrentFrame(void)
     }
     else
     {
-      cec_frame params = m_currentframe;
+      cec_frame params = frame;
       params.shift(2);
       m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &params);
     }