m_controller(controller),
m_inbuf(NULL),
m_iInbufSize(0),
- m_iInbufUsed(0),
- m_bStarted(false),
- m_bStop(false)
+ m_iInbufUsed(0)
{
m_port = new CSerialPort;
}
bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
{
- if (m_bStarted || !m_port)
+ CLockObject lock(&m_commMutex);
+ if (!m_port)
+ {
+ m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
return false;
+ }
+
+ if (IsOpen())
+ {
+ m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
+ }
if (!m_port->Open(strPort, iBaudRate))
{
if (CreateThread())
{
- m_controller->AddLog(CEC_LOG_DEBUG, "reader thread created");
- m_bStarted = true;
+ m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
return true;
}
else
{
- m_controller->AddLog(CEC_LOG_DEBUG, "could not create a reader thread");
+ m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
}
return false;
void CAdapterCommunication::Close(void)
{
- m_bStop = true;
- m_rcvCondition.Broadcast();
-
+ CLockObject lock(&m_commMutex);
StopThread();
- if (m_port)
- m_port->Close();
+ if (m_inbuf)
+ {
+ free(m_inbuf);
+ m_inbuf = NULL;
+ m_iInbufSize = 0;
+ m_iInbufUsed = 0;
+ }
+
+ m_rcvCondition.Broadcast();
}
void *CAdapterCommunication::Process(void)
{
m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
- while (!m_bStop)
+ while (!IsStopped())
{
- if (!ReadFromDevice(1000))
{
- m_bStarted = false;
- break;
+ CLockObject lock(&m_commMutex);
+ ReadFromDevice(100);
}
- if (!m_bStop)
- Sleep(50);
+ if (!IsStopped())
+ Sleep(5);
}
- m_bStarted = false;
return NULL;
}
bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
{
int32_t iBytesRead;
+ uint8_t buff[1024];
+ if (!m_port)
+ return false;
+ iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
+ if (iBytesRead < 0 || iBytesRead > 256)
{
- CLockObject lock(&m_mutex);
-
- uint8_t buff[1024];
- if (!m_port)
- return false;
-
- iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
- if (iBytesRead < 0 || iBytesRead > 256)
- {
- CStdString strError;
- strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
- m_controller->AddLog(CEC_LOG_ERROR, strError);
- return false;
- }
- else if (iBytesRead > 0)
- AddData(buff, (uint8_t) iBytesRead);
+ CStdString strError;
+ strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
+ m_controller->AddLog(CEC_LOG_ERROR, strError);
+ return false;
}
+ else if (iBytesRead > 0)
+ AddData(buff, (uint8_t) iBytesRead);
- if (iBytesRead > 0)
- m_rcvCondition.Signal();
-
- return true;
+ return iBytesRead > 0;
}
void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
{
+ CLockObject lock(&m_bufferMutex);
if (m_iInbufUsed + iLen > m_iInbufSize)
{
m_iInbufSize = m_iInbufUsed + iLen;
memcpy(m_inbuf + m_iInbufUsed, data, iLen);
m_iInbufUsed += iLen;
+
+ m_rcvCondition.Signal();
}
bool CAdapterCommunication::Write(const cec_frame &data)
{
+ CLockObject lock(&m_commMutex);
+ if (m_port->Write(data) != (int32_t) data.size())
{
- CLockObject lock(&m_mutex);
- if (m_port->Write(data) != (int32_t) data.size)
- {
- CStdString strError;
- strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
- m_controller->AddLog(CEC_LOG_ERROR, strError);
- return false;
- }
-
- m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
-
- CCondition::Sleep((uint32_t) data.size * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/ + (uint32_t)50 /* to be on the safe side */);
+ CStdString strError;
+ strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
+ m_controller->AddLog(CEC_LOG_ERROR, strError);
+ return false;
}
+ m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
+ CCondition::Sleep((uint32_t) data.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
+
return true;
}
bool CAdapterCommunication::Read(cec_frame &msg, uint32_t iTimeout)
{
- CLockObject lock(&m_mutex);
+ CLockObject lock(&m_bufferMutex);
if (m_iInbufUsed < 1)
- m_rcvCondition.Wait(&m_mutex, iTimeout);
+ {
+ if (!m_rcvCondition.Wait(&m_bufferMutex, iTimeout))
+ return false;
+ }
- if (m_iInbufUsed < 1 || m_bStop)
+ if (m_iInbufUsed < 1 || IsStopped())
return false;
//search for first start of message
// TODO check for pong
return true;
}
+
+bool CAdapterCommunication::IsOpen(void) const
+{
+ return !IsStopped() && m_port->IsOpen();
+}