using namespace CEC;
CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
+ m_port(NULL),
m_controller(controller),
m_inbuf(NULL),
m_iInbufSize(0),
CAdapterCommunication::~CAdapterCommunication(void)
{
- m_port->Close();
- m_port = NULL;
+ Close();
+
+ if (m_port)
+ {
+ delete m_port;
+ m_port = NULL;
+ }
}
-bool CAdapterCommunication::Open(const char *strPort, int iBaudRate /* = 38400 */, int iTimeoutMs /* = 10000 */)
+bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint64_t iTimeoutMs /* = 10000 */)
{
CLockObject lock(&m_commMutex);
- if (m_bStarted)
+ if (m_bStarted || !m_port)
return false;
if (!m_port->Open(strPort, iBaudRate))
uint8_t buff[1024];
m_port->Read(buff, sizeof(buff), 50);
- CCondition::Sleep(CEC_SETTLE_DOWN_TIME);
-
- m_bStop = false;
- m_bStarted = true;
+ Sleep(CEC_SETTLE_DOWN_TIME);
if (CreateThread())
{
m_controller->AddLog(CEC_LOG_DEBUG, "reader thread created");
+ m_bStarted = true;
return true;
}
else
void CAdapterCommunication::Close(void)
{
+ CLockObject lock(&m_commMutex);
+ if (m_port)
+ m_port->Close();
+
StopThread();
- m_port->Close();
}
void *CAdapterCommunication::Process(void)
{
+ m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
+
while (!m_bStop)
{
+ CLockObject lock(&m_commMutex);
if (!ReadFromDevice(250))
{
m_bStarted = false;
break;
}
- CCondition::Sleep(50);
+ if (!m_bStop)
+ {
+ lock.Leave();
+ Sleep(50);
+ }
}
- m_controller->AddLog(CEC_LOG_DEBUG, "reader thread terminated");
-
- CLockObject lock(&m_commMutex);
m_bStarted = false;
return NULL;
}
-bool CAdapterCommunication::ReadFromDevice(int iTimeout)
+bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout)
{
uint8_t buff[1024];
- CLockObject lock(&m_commMutex);
- int iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
- lock.Leave();
- if (iBytesRead < 0)
+ if (!m_port)
+ return false;
+
+ int32_t 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());
return false;
}
else if (iBytesRead > 0)
- AddData(buff, iBytesRead);
+ AddData(buff, (uint8_t) iBytesRead);
return true;
}
-void CAdapterCommunication::AddData(uint8_t *data, int iLen)
+void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
{
CLockObject lock(&m_bufferMutex);
if (iLen + m_iInbufUsed > m_iInbufSize)
{
CLockObject lock(&m_commMutex);
- if (m_port->Write(data) != data.size())
+ if (m_port->Write(data) != (int) data.size())
{
CStdString strError;
strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
- CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
+ Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
return true;
}
-bool CAdapterCommunication::Read(cec_frame &msg, int iTimeout)
+bool CAdapterCommunication::Read(cec_frame &msg, uint64_t iTimeout)
{
CLockObject lock(&m_bufferMutex);