From: Lars Op den Kamp Date: Wed, 5 Oct 2011 21:14:12 +0000 (+0200) Subject: cec: fix reconnect X-Git-Tag: upstream/2.2.0~1^2~285 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=12027dbe8463aa8dbc3a7b3df60862a867cf94d9;p=deb_libcec.git cec: fix reconnect --- diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 4b0bcdd..ae1a4e4 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -40,6 +40,7 @@ using namespace std; using namespace CEC; CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : + m_port(NULL), m_controller(controller), m_inbuf(NULL), m_iInbufSize(0), @@ -52,16 +53,19 @@ CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : CAdapterCommunication::~CAdapterCommunication(void) { - StopThread(); - m_port->Close(); - delete m_port; - m_port = NULL; + Close(); + + if (m_port) + { + delete m_port; + m_port = NULL; + } } 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)) @@ -78,14 +82,12 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 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 @@ -98,14 +100,20 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 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; @@ -113,10 +121,12 @@ void *CAdapterCommunication::Process(void) } if (!m_bStop) - CCondition::Sleep(50); + { + lock.Leave(); + Sleep(50); + } } - CLockObject lock(&m_commMutex); m_bStarted = false; return NULL; } @@ -124,12 +134,10 @@ void *CAdapterCommunication::Process(void) bool CAdapterCommunication::ReadFromDevice(uint64_t iTimeout) { uint8_t buff[1024]; - CLockObject lock(&m_commMutex); if (!m_port) return false; int32_t iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); - lock.Leave(); if (iBytesRead < 0 || iBytesRead > 256) { CStdString strError; @@ -172,7 +180,7 @@ bool CAdapterCommunication::Write(const cec_frame &data) 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; } diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 2133080..6750c87 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -95,7 +95,7 @@ void *CCECProcessor::Process(void) if (!m_bStop) { m_controller->CheckKeypressTimeout(); - CCondition::Sleep(50); + Sleep(50); } } diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index 47554d6..abc800c 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -51,6 +51,7 @@ CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress CLibCEC::~CLibCEC(void) { + Close(); delete m_cec; m_cec = NULL; @@ -61,7 +62,10 @@ CLibCEC::~CLibCEC(void) bool CLibCEC::Open(const char *strPort, uint64_t iTimeoutMs /* = 10000 */) { if (!m_comm) + { + AddLog(CEC_LOG_ERROR, "no comm port"); return false; + } if (m_comm->IsOpen()) { @@ -87,17 +91,9 @@ bool CLibCEC::Open(const char *strPort, uint64_t iTimeoutMs /* = 10000 */) void CLibCEC::Close(void) { if (m_cec) - { m_cec->StopThread(); - delete m_cec; - m_cec = NULL; - } if (m_comm) - { m_comm->Close(); - delete m_comm; - m_comm = NULL; - } } int CLibCEC::FindAdapters(std::vector &deviceList, const char *strDevicePath /* = NULL */) diff --git a/src/testclient/main.cpp b/src/testclient/main.cpp index 983ba13..813e22b 100644 --- a/src/testclient/main.cpp +++ b/src/testclient/main.cpp @@ -240,7 +240,6 @@ int main (int argc, char *argv[]) } cout << "cec device opened" << endl; - usleep(CEC_SETTLE_DOWN_TIME); parser->PowerOnDevices(CECDEVICE_TV); flush_log(parser); @@ -289,6 +288,12 @@ int main (int argc, char *argv[]) { parser->StartBootloader(); } + else if (command == "r") + { + parser->Close(); + parser->Open(strPort.c_str()); + parser->SetActiveView(); + } else if (command == "h" || command == "help") { show_console_help();