cec: set a message state in CCECAdapterMessage and check this state after sending...
authorLars Op den Kamp <lars@opdenkamp.eu>
Sun, 30 Oct 2011 09:34:36 +0000 (10:34 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Sun, 30 Oct 2011 09:34:36 +0000 (10:34 +0100)
src/lib/AdapterCommunication.cpp
src/lib/AdapterCommunication.h
src/lib/CECProcessor.cpp

index 4845f57b97eed9e62edf52e3e07859cb866982cb..3b0ca015454a1250a40da236358bf2f3e0206161 100644 (file)
@@ -216,16 +216,18 @@ void CAdapterCommunication::WriteNextCommand(void)
   if (m_outBuffer.Pop(msg))
   {
     CLockObject lock(&msg->mutex);
-    if (m_port->Write(msg) != (int32_t) msg.get()->size())
+    if (m_port->Write(msg) != (int32_t) msg->size())
     {
       CStdString strError;
       strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
       m_controller->AddLog(CEC_LOG_ERROR, strError);
+      msg->state = ADAPTER_MESSAGE_STATE_ERROR;
     }
     else
     {
       m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
-      CCondition::Sleep((uint32_t) msg.get()->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
+      CCondition::Sleep((uint32_t) msg->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
+      msg->state = ADAPTER_MESSAGE_STATE_SENT;
     }
     msg->condition.Signal();
   }
@@ -233,6 +235,7 @@ void CAdapterCommunication::WriteNextCommand(void)
 
 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
 {
+  data->state = ADAPTER_MESSAGE_STATE_WAITING;
   m_outBuffer.Push(data);
   return true;
 }
@@ -285,6 +288,9 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
       msg.push_back(buf);
   }
 
+  if (bGotFullMessage)
+    msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
+
   return bGotFullMessage;
 }
 
index a82f32c673b42d5775b678bed909d3fbba432c38..9a3d1268cf008c0686ee86151632e5e90b6e52ac 100644 (file)
 
 namespace CEC
 {
+  typedef enum cec_adapter_message_state
+  {
+    ADAPTER_MESSAGE_STATE_UNKNOWN = 0,
+    ADAPTER_MESSAGE_STATE_WAITING,
+    ADAPTER_MESSAGE_STATE_SENT,
+    ADAPTER_MESSAGE_STATE_RECEIVED,
+    ADAPTER_MESSAGE_STATE_ERROR
+  } cec_adapter_message_state;
+
+
   class CCECAdapterMessage : public boost::enable_shared_from_this<CCECAdapterMessage>
   {
   public:
-    CCECAdapterMessage(void) {}
+    CCECAdapterMessage(void) { clear(); }
     CCECAdapterMessage(const cec_command &command);
 
     bool                    empty(void) const             { return packet.empty(); }
     uint8_t                 operator[](uint8_t pos) const { return packet[pos]; }
     uint8_t                 at(uint8_t pos) const         { return packet[pos]; }
     uint8_t                 size(void) const              { return packet.size; }
-    void                    clear(void)                   { packet.clear(); }
+    void                    clear(void)                   { state = ADAPTER_MESSAGE_STATE_UNKNOWN; packet.clear(); }
     void                    shift(uint8_t iShiftBy)       { packet.shift(iShiftBy); }
     void                    push_back(uint8_t add)        { packet.push_back(add); }
     cec_adapter_messagecode message(void) const           { return packet.size >= 1 ? (cec_adapter_messagecode) (packet.at(0) & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK))  : MSGCODE_NOTHING; }
@@ -60,9 +70,10 @@ namespace CEC
     cec_logical_address     destination(void) const       { return packet.size >= 2 ? (cec_logical_address) (packet.at(1) & 0xF) : CECDEVICE_UNKNOWN; };
     void                    push_escaped(int16_t byte);
 
-    cec_datapacket packet;
-    CMutex         mutex;
-    CCondition     condition;
+    cec_datapacket            packet;
+    cec_adapter_message_state state;
+    CMutex                    mutex;
+    CCondition                condition;
 
   private:
     CCECAdapterMessage &operator =(const CCECAdapterMessage &msg);
index 350958f41d1b1f9342d53731d6c1062d0bb1c934..1bc4c597854ad6475dc6557a67429445fa5a1813 100644 (file)
@@ -200,7 +200,14 @@ bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true
     if (!m_communication || !m_communication->Write(output))
       return bReturn;
     else
+    {
       output->condition.Wait(&output->mutex);
+      if (output->state != ADAPTER_MESSAGE_STATE_SENT)
+      {
+        m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
+        return bReturn;
+      }
+    }
   }
 
   if (bWaitForAck)