X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2FAdapterCommunication.cpp;h=d3eecf45ea7f1045ef466ba38e1291f71f0186ca;hb=b9eea66dd7e19a4baf9e89f4fd6b3c2f281ba7a7;hp=4845f57b97eed9e62edf52e3e07859cb866982cb;hpb=eb35e4cac7a6a239392970741bf255011a13ec0e;p=deb_libcec.git diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 4845f57..d3eecf4 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -147,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 @@ -161,14 +162,17 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 void CAdapterCommunication::Close(void) { CLockObject lock(&m_mutex); - StopThread(); - + 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()) { @@ -216,16 +220,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 +239,7 @@ void CAdapterCommunication::WriteNextCommand(void) bool CAdapterCommunication::Write(CCECAdapterMessagePtr data) { + data->state = ADAPTER_MESSAGE_STATE_WAITING; m_outBuffer.Push(data); return true; } @@ -285,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; } @@ -366,5 +376,5 @@ bool CAdapterCommunication::PingAdapter(void) bool CAdapterCommunication::IsOpen(void) const { - return !IsStopped() && m_port->IsOpen(); + return !IsStopped() && m_port->IsOpen() && IsRunning(); }