cec: ensure that the ackmask is always set to 0 when closing the connection and that...
authorLars Op den Kamp <lars@opdenkamp.eu>
Thu, 16 Feb 2012 09:44:27 +0000 (10:44 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Thu, 16 Feb 2012 09:44:27 +0000 (10:44 +0100)
src/lib/CECProcessor.cpp
src/lib/adapter/USBCECAdapterCommunication.cpp
src/lib/adapter/USBCECAdapterCommunication.h

index 98be0c49229593c515ba451718763d5c20b95c96..f9d21346f2dbceaec74dfa2862df5808fbb6cd97 100644 (file)
@@ -136,6 +136,8 @@ CCECProcessor::~CCECProcessor(void)
 
 void CCECProcessor::Close(void)
 {
+  StopThread(false);
+  SetInitialised(false);
   StopThread();
 
   CLockObject lock(m_mutex);
@@ -867,8 +869,11 @@ bool CCECProcessor::Transmit(const cec_command &data)
     CLockObject lock(m_mutex);
     LogOutput(data);
     m_iLastTransmission = GetTimeMs();
-    if (!m_communication)
+    if (!m_communication || !IsInitialised())
+    {
+      CLibCEC::AddLog(CEC_LOG_ERROR, "cannot transmit command: connection closed");
       return false;
+    }
     uint8_t iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
     retVal = m_communication->Write(data, iMaxTries, m_iLineTimeout, m_iRetryLineTimeout);
   }
index 96571cc3dc52bcb5fb4df06ea55e7fdfcae4d17c..2d8597205214408c5f9f8ba72aa6255dc550c3c3 100644 (file)
@@ -173,12 +173,6 @@ bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32
 
 void CUSBCECAdapterCommunication::Close(void)
 {
-  {
-    CLockObject lock(m_mutex);
-    m_bHasData = true;
-    m_rcvCondition.Broadcast();
-  }
-  SetAckMask(0);
   StopThread();
 }
 
@@ -205,10 +199,14 @@ void *CUSBCECAdapterCommunication::Process(void)
     }
   }
 
+  /* notify all threads that are waiting on messages to be sent */
   CCECAdapterMessage *msg(NULL);
-  if (m_outBuffer.Pop(msg))
+  while (m_outBuffer.Pop(msg))
     msg->event.Broadcast();
 
+  /* set the ackmask to 0 before closing the connection */
+  SetAckMaskInternal(0, true);
+
   if (m_port)
   {
     delete m_port;
@@ -253,7 +251,7 @@ bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data)
 {
   data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
   m_outBuffer.Push(data);
-  data->event.Wait();
+  data->event.Wait(5000);
 
   if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) ||
       (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT))
@@ -485,6 +483,11 @@ bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
 }
 
 bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
+{
+  return SetAckMaskInternal(iMask, false);
+}
+
+bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask, bool bWriteDirectly /* = false */)
 {
   bool bReturn(false);
   CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask);
@@ -498,7 +501,9 @@ bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
   output->PushBack(MSGEND);
   output->isTransmission = false;
 
-  if ((bReturn = Write(output)) == false)
+  if (bWriteDirectly)
+    SendMessageToAdapter(output);
+  else if ((bReturn = Write(output)) == false)
     CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask");
   delete output;
 
@@ -677,6 +682,13 @@ bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize
 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->tries == 1)
     SetLineTimeout(msg->lineTimeout);
   else
index fc9e16ce044d30da4357288e88ed3b0ac1fd306e..22c7db9738ee459bcd0b239f7f06165091c03eef 100644 (file)
@@ -72,6 +72,8 @@ namespace CEC
 
     void *Process(void);
   private:
+    bool SetAckMaskInternal(uint16_t iMask, bool bWriteDirectly = false);
+
     bool CheckAdapter(uint32_t iTimeoutMs = 10000);
     bool Write(CCECAdapterMessage *data);
     bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);