cec: removed dupe m_bRunning properties. wait until a thread is started before return...
[deb_libcec.git] / src / lib / LibCEC.cpp
index 23c4b18b544cd9be3886388389ab2b02b9e2c5a9..0f88470554dec9957713e9d4e6d318e10634af5f 100644 (file)
 #include "AdapterDetection.h"
 #include "CECProcessor.h"
 #include "util/StdString.h"
-#include "util/timeutils.h"
+#include "platform/timeutils.h"
 
 using namespace std;
 using namespace CEC;
 
-CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
+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)
 {
@@ -51,17 +51,25 @@ CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress
 
 CLibCEC::~CLibCEC(void)
 {
+  Close();
   delete m_cec;
   m_cec = NULL;
 
   delete m_comm;
   m_comm = NULL;
+
+  m_logBuffer.Clear();
+  m_keyBuffer.Clear();
+  m_commandBuffer.Clear();
 }
 
-bool CLibCEC::Open(const char *strPort, int iTimeoutMs /* = 10000 */)
+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())
   {
@@ -92,7 +100,7 @@ void CLibCEC::Close(void)
     m_comm->Close();
 }
 
-int CLibCEC::FindAdapters(std::vector<cec_adapter> &deviceList, const char *strDevicePath /* = NULL */)
+int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
 {
   CStdString strDebug;
   if (strDevicePath)
@@ -101,7 +109,7 @@ int CLibCEC::FindAdapters(std::vector<cec_adapter> &deviceList, const char *strD
     strDebug.Format("trying to autodetect all CEC adapters");
   AddLog(CEC_LOG_DEBUG, strDebug);
 
-  return CAdapterDetection::FindAdapters(deviceList, strDevicePath);
+  return CAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
 }
 
 bool CLibCEC::PingAdapter(void)
@@ -114,19 +122,19 @@ bool CLibCEC::StartBootloader(void)
   return m_comm ? m_comm->StartBootloader() : false;
 }
 
-int CLibCEC::GetMinVersion(void)
+int8_t CLibCEC::GetMinVersion(void)
 {
   return CEC_MIN_VERSION;
 }
 
-int CLibCEC::GetLibVersion(void)
+int8_t CLibCEC::GetLibVersion(void)
 {
   return CEC_LIB_VERSION;
 }
 
 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
 {
-  return m_logBuffer.Pop(*message);
+  return (m_logBuffer.Pop(*message));
 }
 
 bool CLibCEC::GetNextKeypress(cec_keypress *key)
@@ -171,10 +179,13 @@ bool CLibCEC::SetInactiveView(void)
 
 void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
 {
-  cec_log_message message;
-  message.level = level;
-  message.message.assign(strMessage.c_str());
-  m_logBuffer.Push(message);
+  if (m_cec)
+  {
+    cec_log_message message;
+    message.level = level;
+    snprintf(message.message, sizeof(message.message), "%s", strMessage.c_str());
+    m_logBuffer.Push(message);
+  }
 }
 
 void CLibCEC::AddKey(void)
@@ -182,6 +193,7 @@ void CLibCEC::AddKey(void)
   if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
   {
     cec_keypress key;
+
     key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
     key.keycode = m_iCurrentButton;
     m_keyBuffer.Push(key);
@@ -193,6 +205,8 @@ void CLibCEC::AddKey(void)
 void CLibCEC::AddCommand(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_frame *parameters)
 {
   cec_command command;
+  command.clear();
+
   command.source       = source;
   command.destination  = destination;
   command.opcode       = opcode;
@@ -225,7 +239,14 @@ void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
   m_buttontime = GetTimeMs();
 }
 
-DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
+void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
 {
   return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
 }
+
+void CECDestroy(CEC::ICECAdapter *instance)
+{
+  CLibCEC *lib = static_cast< CLibCEC* > (instance);
+  if (lib)
+    delete lib;
+}