win32: fix build
[deb_libcec.git] / src / lib / AdapterCommunication.cpp
index 743f00d66e6caefe6bf0d079248a641e7d691bd3..74597665cfb98910423ea35f01562adce47c2dc6 100644 (file)
@@ -93,10 +93,10 @@ void CCECAdapterMessage::push_escaped(int16_t byte)
   if (byte >= MSGESC && byte != MSGSTART)
   {
     push_back(MSGESC);
-    push_back(byte - ESCOFFSET);
+    push_back((uint8_t) (byte - ESCOFFSET));
   }
   else
-    push_back(byte);
+    push_back((uint8_t) byte);
 }
 
 CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
@@ -119,6 +119,7 @@ CAdapterCommunication::~CAdapterCommunication(void)
 
 bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
 {
+  CLockObject lock(&m_mutex);
   if (!m_port)
   {
     m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
@@ -146,7 +147,8 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38
 
   if (CreateThread())
   {
-    m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
+    m_startCondition.Wait(&m_mutex);
+    m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
     return true;
   }
   else
@@ -159,14 +161,18 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38
 
 void CAdapterCommunication::Close(void)
 {
-  StopThread();
-
+  CLockObject lock(&m_mutex);
+  m_startCondition.Broadcast();
   m_rcvCondition.Broadcast();
+  StopThread();
 }
 
 void *CAdapterCommunication::Process(void)
 {
-  m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
+  {
+    CLockObject lock(&m_mutex);
+    m_startCondition.Signal();
+  }
 
   while (!IsStopped())
   {
@@ -201,7 +207,7 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
 
 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
 {
-  CLockObject lock(&m_bufferMutex);
+  CLockObject lock(&m_mutex);
   for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
     m_inBuffer.Push(data[iPtr]);
 
@@ -213,29 +219,34 @@ void CAdapterCommunication::WriteNextCommand(void)
   CCECAdapterMessagePtr msg;
   if (m_outBuffer.Pop(msg))
   {
-    if (m_port->Write(msg) != (int32_t) msg.get()->size())
+    CLockObject lock(&msg->mutex);
+    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();
   }
 }
 
 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
 {
+  data->state = ADAPTER_MESSAGE_STATE_WAITING;
   m_outBuffer.Push(data);
   return true;
 }
 
 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
 {
-  CLockObject lock(&m_bufferMutex);
+  CLockObject lock(&m_mutex);
 
   msg.clear();
   uint64_t iNow = GetTimeMs();
@@ -249,7 +260,7 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
     uint8_t buf = 0;
     if (!m_inBuffer.Pop(buf))
     {
-      if (!m_rcvCondition.Wait(&m_bufferMutex, iTarget - iNow))
+      if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow)))
         return false;
     }
 
@@ -281,6 +292,9 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
       msg.push_back(buf);
   }
 
+  if (bGotFullMessage)
+    msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
+
   return bGotFullMessage;
 }
 
@@ -362,5 +376,5 @@ bool CAdapterCommunication::PingAdapter(void)
 
 bool CAdapterCommunication::IsOpen(void) const
 {
-  return !IsStopped() && m_port->IsOpen();
+  return !IsStopped() && m_port->IsOpen() && IsRunning();
 }