From 1113cb7d55328a6c15846955626187615a9ad02e Mon Sep 17 00:00:00 2001 From: Lars Op den Kamp Date: Sat, 3 Dec 2011 22:32:08 +0100 Subject: [PATCH] cec: create the CAdapterCommunication instance in CCECProcessor --- src/lib/AdapterCommunication.cpp | 30 ++++++++++++------------ src/lib/AdapterCommunication.h | 6 ++--- src/lib/CECProcessor.cpp | 34 ++++++++++++++++++++++------ src/lib/CECProcessor.h | 9 +++++--- src/lib/LibCEC.cpp | 39 +++++--------------------------- src/lib/LibCEC.h | 1 - 6 files changed, 57 insertions(+), 62 deletions(-) diff --git a/src/lib/AdapterCommunication.cpp b/src/lib/AdapterCommunication.cpp index 32ceeaf..e6afcf4 100644 --- a/src/lib/AdapterCommunication.cpp +++ b/src/lib/AdapterCommunication.cpp @@ -32,7 +32,7 @@ #include "AdapterCommunication.h" -#include "LibCEC.h" +#include "CECProcessor.h" #include "platform/serialport.h" #include "util/StdString.h" #include "platform/timeutils.h" @@ -237,9 +237,9 @@ void CCECAdapterMessage::push_escaped(uint8_t byte) push_back(byte); } -CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : +CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) : m_port(NULL), - m_controller(controller) + m_processor(processor) { m_port = new CSerialPort; } @@ -260,24 +260,24 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 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]; @@ -286,12 +286,12 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38 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; @@ -334,7 +334,7 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) { 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) @@ -366,12 +366,12 @@ void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) { 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; } @@ -414,7 +414,7 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) 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; } @@ -451,7 +451,7 @@ bool CAdapterCommunication::StartBootloader(void) 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); @@ -471,7 +471,7 @@ bool CAdapterCommunication::PingAdapter(void) 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); diff --git a/src/lib/AdapterCommunication.h b/src/lib/AdapterCommunication.h index aaa0747..2ebffed 100644 --- a/src/lib/AdapterCommunication.h +++ b/src/lib/AdapterCommunication.h @@ -81,12 +81,12 @@ namespace CEC }; 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); @@ -108,7 +108,7 @@ namespace CEC bool ReadFromDevice(uint32_t iTimeout); CSerialPort * m_port; - CLibCEC * m_controller; + CCECProcessor * m_processor; CecBuffer m_inBuffer; CecBuffer m_outBuffer; CMutex m_mutex; diff --git a/src/lib/CECProcessor.cpp b/src/lib/CECProcessor.cpp index 406ef32..3eb4734 100644 --- a/src/lib/CECProcessor.cpp +++ b/src/lib/CECProcessor.cpp @@ -47,17 +47,17 @@ 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(); @@ -65,16 +65,16 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm 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++) { @@ -120,18 +120,25 @@ CCECProcessor::~CCECProcessor(void) 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; } @@ -297,6 +304,9 @@ void *CCECProcessor::Process(void) m_controller->CheckKeypressTimeout(); } + if (m_communication) + m_communication->Close(); + return NULL; } @@ -1268,3 +1278,13 @@ void *CCECBusScan::Process(void) } return NULL; } + +bool CCECProcessor::StartBootloader(void) +{ + return m_communication->StartBootloader(); +} + +bool CCECProcessor::PingAdapter(void) +{ + return m_communication->PingAdapter(); +} diff --git a/src/lib/CECProcessor.h b/src/lib/CECProcessor.h index 7ec9d81..c8c8262 100644 --- a/src/lib/CECProcessor.h +++ b/src/lib/CECProcessor.h @@ -49,11 +49,11 @@ namespace CEC 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; } @@ -119,6 +119,9 @@ namespace CEC virtual bool FindLogicalAddresses(void); virtual bool SetAckMask(uint16_t iMask); + virtual bool StartBootloader(void); + virtual bool PingAdapter(void); + CCECBusDevice *m_busDevices[16]; private: diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index 445798b..4a63051 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -47,8 +47,7 @@ CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) : 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 */) : @@ -56,48 +55,24 @@ CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress 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; @@ -110,8 +85,6 @@ void CLibCEC::Close(void) { 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 */) @@ -128,12 +101,12 @@ int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const ch 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) diff --git a/src/lib/LibCEC.h b/src/lib/LibCEC.h index e1b991b..a0a4ddc 100644 --- a/src/lib/LibCEC.h +++ b/src/lib/LibCEC.h @@ -120,7 +120,6 @@ namespace CEC cec_user_control_code m_iCurrentButton; int64_t m_buttontime; CCECProcessor *m_cec; - CAdapterCommunication *m_comm; CecBuffer m_logBuffer; CecBuffer m_keyBuffer; CecBuffer m_commandBuffer; -- 2.34.1