#include "AdapterCommunication.h"
-#include "LibCEC.h"
+#include "CECProcessor.h"
#include "platform/serialport.h"
#include "util/StdString.h"
#include "platform/timeutils.h"
push_back(byte);
}
-CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
+CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) :
m_port(NULL),
- m_controller(controller)
+ m_processor(processor)
{
m_port = new CSerialPort;
}
CLockObject lock(&m_mutex);
if (!m_port)
{
- m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
+ m_processor->AddLog(CEC_LOG_ERROR, "port is NULL");
return false;
}
if (IsOpen())
{
- m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
+ m_processor->AddLog(CEC_LOG_ERROR, "port is already open");
}
if (!m_port->Open(strPort, iBaudRate))
{
CStdString strError;
strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
- m_controller->AddLog(CEC_LOG_ERROR, strError);
+ m_processor->AddLog(CEC_LOG_ERROR, strError);
return false;
}
- m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
+ m_processor->AddLog(CEC_LOG_DEBUG, "connection opened");
//clear any input bytes
uint8_t buff[1024];
if (CreateThread())
{
m_startCondition.Wait(&m_mutex);
- m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
+ m_processor->AddLog(CEC_LOG_DEBUG, "communication thread started");
return true;
}
else
{
- m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
+ m_processor->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
}
return false;
{
CStdString strError;
strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
- m_controller->AddLog(CEC_LOG_ERROR, strError);
+ m_processor->AddLog(CEC_LOG_ERROR, strError);
return false;
}
else if (iBytesRead > 0)
{
CStdString strError;
strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
- m_controller->AddLog(CEC_LOG_ERROR, strError);
+ m_processor->AddLog(CEC_LOG_ERROR, strError);
msg->state = ADAPTER_MESSAGE_STATE_ERROR;
}
else
{
- m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
+ m_processor->AddLog(CEC_LOG_DEBUG, "command sent");
CCondition::Sleep((uint32_t) msg->size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/);
msg->state = ADAPTER_MESSAGE_STATE_SENT;
}
else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
{
if (msg.size() > 0)
- m_controller->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
+ m_processor->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
msg.clear();
bGotStart = true;
}
if (!IsRunning())
return bReturn;
- m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
+ m_processor->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
CCECAdapterMessage *output = new CCECAdapterMessage;
output->push_back(MSGSTART);
if (!IsRunning())
return bReturn;
- m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
+ m_processor->AddLog(CEC_LOG_DEBUG, "sending ping");
CCECAdapterMessage *output = new CCECAdapterMessage;
output->push_back(MSGSTART);
};
class CSerialPort;
- class CLibCEC;
+ class CCECProcessor;
class CAdapterCommunication : private CThread
{
public:
- CAdapterCommunication(CLibCEC *controller);
+ CAdapterCommunication(CCECProcessor *processor);
virtual ~CAdapterCommunication();
bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
bool ReadFromDevice(uint32_t iTimeout);
CSerialPort * m_port;
- CLibCEC * m_controller;
+ CCECProcessor * m_processor;
CecBuffer<uint8_t> m_inBuffer;
CecBuffer<CCECAdapterMessage *> m_outBuffer;
CMutex m_mutex;
using namespace CEC;
using namespace std;
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
+CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
m_bStarted(false),
m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
m_lastInitiator(CECDEVICE_UNKNOWN),
m_strDeviceName(strDeviceName),
- m_communication(serComm),
m_controller(controller),
m_bMonitor(false),
m_busScan(NULL)
{
+ m_communication = new CAdapterCommunication(this);
m_logicalAddresses.Clear();
m_logicalAddresses.Set(iLogicalAddress);
m_types.clear();
m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
}
-CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
+CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types) :
m_bStarted(false),
m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
m_strDeviceName(strDeviceName),
m_types(types),
- m_communication(serComm),
m_controller(controller),
m_bMonitor(false)
{
+ m_communication = new CAdapterCommunication(this);
m_logicalAddresses.Clear();
for (int iPtr = 0; iPtr < 16; iPtr++)
{
m_startCondition.Broadcast();
StopThread();
+ delete m_communication;
m_communication = NULL;
m_controller = NULL;
for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
delete m_busDevices[iPtr];
}
-bool CCECProcessor::Start(void)
+bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
{
CLockObject lock(&m_mutex);
- if (!m_communication || !m_communication->IsOpen())
+ if (!m_communication || m_communication->IsOpen())
{
- m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
+ m_controller->AddLog(CEC_LOG_ERROR, "connection already opened");
+ return false;
+ }
+
+ if (!m_communication->Open(strPort, iBaudRate, iTimeoutMs))
+ {
+ m_controller->AddLog(CEC_LOG_ERROR, "could not open a connection");
return false;
}
m_controller->CheckKeypressTimeout();
}
+ if (m_communication)
+ m_communication->Close();
+
return NULL;
}
}
return NULL;
}
+
+bool CCECProcessor::StartBootloader(void)
+{
+ return m_communication->StartBootloader();
+}
+
+bool CCECProcessor::PingAdapter(void)
+{
+ return m_communication->PingAdapter();
+}
class CCECProcessor : public CThread
{
public:
- CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
- CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types);
+ CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS);
+ CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types);
virtual ~CCECProcessor(void);
- virtual bool Start(void);
+ virtual bool Start(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
virtual void *Process(void);
virtual bool IsMonitoring(void) const { return m_bMonitor; }
virtual bool FindLogicalAddresses(void);
virtual bool SetAckMask(uint16_t iMask);
+ virtual bool StartBootloader(void);
+ virtual bool PingAdapter(void);
+
CCECBusDevice *m_busDevices[16];
private:
m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
m_buttontime(0)
{
- m_comm = new CAdapterCommunication(this);
- m_cec = new CCECProcessor(this, m_comm, strDeviceName, types);
+ m_cec = new CCECProcessor(this, strDeviceName, types);
}
CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
m_buttontime(0)
{
- m_comm = new CAdapterCommunication(this);
- m_cec = new CCECProcessor(this, m_comm, strDeviceName, iLogicalAddress, iPhysicalAddress);
+ m_cec = new CCECProcessor(this, strDeviceName, iLogicalAddress, iPhysicalAddress);
}
CLibCEC::~CLibCEC(void)
{
Close();
delete m_cec;
- delete m_comm;
}
bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
{
- if (!m_comm)
- {
- AddLog(CEC_LOG_ERROR, "no comm port");
- return false;
- }
-
- if (m_comm->IsOpen())
+ if (m_cec->IsRunning())
{
AddLog(CEC_LOG_ERROR, "connection already open");
return false;
}
- int64_t iNow = GetTimeMs();
- int64_t iTarget = iNow + iTimeoutMs;
-
- bool bOpened(false);
- while (!bOpened && iNow < iTarget)
- {
- bOpened = m_comm->Open(strPort, 38400, iTimeoutMs);
- iNow = GetTimeMs();
- }
-
- if (!bOpened)
- {
- AddLog(CEC_LOG_ERROR, "could not open a connection");
- return false;
- }
-
- if (!m_cec->Start())
+ if (!m_cec->Start(strPort, 38400, iTimeoutMs))
{
AddLog(CEC_LOG_ERROR, "could not start CEC communications");
return false;
{
if (m_cec)
m_cec->StopThread();
- if (m_comm)
- m_comm->Close();
}
int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
bool CLibCEC::PingAdapter(void)
{
- return m_comm ? m_comm->PingAdapter() : false;
+ return m_cec ? m_cec->PingAdapter() : false;
}
bool CLibCEC::StartBootloader(void)
{
- return m_comm ? m_comm->StartBootloader() : false;
+ return m_cec ? m_cec->StartBootloader() : false;
}
bool CLibCEC::GetNextLogMessage(cec_log_message *message)
cec_user_control_code m_iCurrentButton;
int64_t m_buttontime;
CCECProcessor *m_cec;
- CAdapterCommunication *m_comm;
CecBuffer<cec_log_message> m_logBuffer;
CecBuffer<cec_keypress> m_keyBuffer;
CecBuffer<cec_command> m_commandBuffer;