using namespace CEC;
CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
+ m_port(NULL),
m_controller(controller),
m_inbuf(NULL),
m_iInbufSize(0),
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))
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;
}
if (!m_bStop)
- CCondition::Sleep(50);
+ {
+ lock.Leave();
+ Sleep(50);
+ }
}
- CLockObject lock(&m_commMutex);
m_bStarted = false;
return NULL;
}
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;
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;
}
CLibCEC::~CLibCEC(void)
{
+ Close();
delete m_cec;
m_cec = NULL;
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())
{
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<cec_adapter> &deviceList, const char *strDevicePath /* = NULL */)
}
cout << "cec device opened" << endl;
- usleep(CEC_SETTLE_DOWN_TIME);
parser->PowerOnDevices(CECDEVICE_TV);
flush_log(parser);
{
parser->StartBootloader();
}
+ else if (command == "r")
+ {
+ parser->Close();
+ parser->Open(strPort.c_str());
+ parser->SetActiveView();
+ }
else if (command == "h" || command == "help")
{
show_console_help();