cec: added bWait parameter to volume change methods.
[deb_libcec.git] / src / lib / LibCEC.cpp
index 652d732087b62ca5bf85fe8413fe250a0ccbe1d3..26fd7b0bdb68b26402b6e3ca1588cd938f124ed3 100644 (file)
 #include "AdapterCommunication.h"
 #include "AdapterDetection.h"
 #include "CECProcessor.h"
+#include "devices/CECBusDevice.h"
 #include "util/StdString.h"
 #include "platform/timeutils.h"
 
 using namespace std;
 using namespace CEC;
 
+CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) :
+    m_iStartTime(GetTimeMs()),
+    m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
+    m_buttontime(0)
+{
+  m_comm = new CAdapterCommunication(this);
+  m_cec = new CCECProcessor(this, m_comm, strDeviceName, types);
+}
+
 CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
-    m_bStarted(false),
+    m_iStartTime(GetTimeMs()),
     m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
     m_buttontime(0)
 {
@@ -54,14 +64,7 @@ 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, uint32_t iTimeoutMs /* = 10000 */)
@@ -78,7 +81,17 @@ bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
     return false;
   }
 
-  if (!m_comm->Open(strPort, 38400, iTimeoutMs))
+  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;
@@ -90,17 +103,15 @@ bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
     return false;
   }
 
-  m_bStarted = true;
   return true;
 }
 
 void CLibCEC::Close(void)
 {
-  if (m_comm)
-    m_comm->Close();
   if (m_cec)
     m_cec->StopThread();
-  m_bStarted = false;
+  if (m_comm)
+    m_comm->Close();
 }
 
 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
@@ -125,16 +136,6 @@ bool CLibCEC::StartBootloader(void)
   return m_comm ? m_comm->StartBootloader() : false;
 }
 
-int8_t CLibCEC::GetMinVersion(void)
-{
-  return CEC_MIN_VERSION;
-}
-
-int8_t CLibCEC::GetLibVersion(void)
-{
-  return CEC_LIB_VERSION;
-}
-
 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
 {
   return (m_logBuffer.Pop(*message));
@@ -150,9 +151,9 @@ bool CLibCEC::GetNextCommand(cec_command *command)
   return m_commandBuffer.Pop(*command);
 }
 
-bool CLibCEC::Transmit(const cec_frame &data, bool bWaitForAck /* = true */)
+bool CLibCEC::Transmit(const cec_command &data)
 {
-  return m_cec ? m_cec->Transmit(data, bWaitForAck) : false;
+  return m_cec ? m_cec->Transmit(data) : false;
 }
 
 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
@@ -160,14 +161,29 @@ bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
   return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
 }
 
+bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
+{
+  return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false;
+}
+
+bool CLibCEC::SetHDMIPort(uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
+{
+  return m_cec ? m_cec->SetHDMIPort(iPort) : false;
+}
+
 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
 {
-  return m_cec ? m_cec->PowerOnDevices(address) : false;
+  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->PowerOn() : false;
 }
 
 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
 {
-  return m_cec ? m_cec->StandbyDevices(address) : false;
+  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->Standby() : false;
+}
+
+bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
+{
+  return m_cec ? m_cec->SetActiveSource(type) : false;
 }
 
 bool CLibCEC::SetActiveView(void)
@@ -175,25 +191,139 @@ bool CLibCEC::SetActiveView(void)
   return m_cec ? m_cec->SetActiveView() : false;
 }
 
+bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
+{
+  return m_cec ? m_cec->SetDeckControlMode(mode, bSendUpdate) : false;
+}
+
+bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
+{
+  return m_cec ? m_cec->SetDeckInfo(info, bSendUpdate) : false;
+}
+
 bool CLibCEC::SetInactiveView(void)
 {
   return m_cec ? m_cec->SetInactiveView() : false;
 }
 
+bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
+{
+  return m_cec ? m_cec->SetMenuState(state, bSendUpdate) : false;
+}
+
+bool CLibCEC::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
+{
+  return m_cec && iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST ?
+      m_cec->m_busDevices[m_cec->GetLogicalAddress()]->TransmitOSDString(iLogicalAddress, duration, strMessage) :
+      false;
+}
+
+bool CLibCEC::SwitchMonitoring(bool bEnable)
+{
+  return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
+}
+
+cec_version CLibCEC::GetDeviceCecVersion(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->GetDeviceCecVersion(iAddress);
+  return CEC_VERSION_UNKNOWN;
+}
+
+bool CLibCEC::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->GetDeviceMenuLanguage(iAddress, language);
+  return false;
+}
+
+uint64_t CLibCEC::GetDeviceVendorId(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->GetDeviceVendorId(iAddress);
+  return 0;
+}
+
+cec_power_status CLibCEC::GetDevicePowerStatus(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->GetDevicePowerStatus(iAddress);
+  return CEC_POWER_STATUS_UNKNOWN;
+}
+
+bool CLibCEC::PollDevice(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->PollDevice(iAddress);
+  return false;
+}
+
+cec_logical_addresses CLibCEC::GetActiveDevices(void)
+{
+  cec_logical_addresses addresses;
+  if (m_cec)
+    addresses = m_cec->GetActiveDevices();
+  return addresses;
+}
+
+bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
+{
+  if (m_cec && iAddress >= CECDEVICE_TV && iAddress < CECDEVICE_BROADCAST)
+    return m_cec->IsActiveDevice(iAddress);
+  return false;
+}
+
+bool CLibCEC::IsActiveDeviceType(cec_device_type type)
+{
+  if (m_cec && type >= CEC_DEVICE_TYPE_TV && type <= CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+    return m_cec->IsActiveDeviceType(type);
+  return false;
+}
+
+uint8_t CLibCEC::VolumeUp(bool bWait /* = true */)
+{
+  if (m_cec)
+    return m_cec->VolumeUp(bWait);
+  return 0;
+}
+
+uint8_t CLibCEC::VolumeDown(bool bWait /* = true */)
+{
+  if (m_cec)
+    return m_cec->VolumeDown(bWait);
+  return 0;
+}
+
+
+uint8_t CLibCEC::MuteAudio(bool bWait /* = true */)
+{
+  if (m_cec)
+    return m_cec->MuteAudio(bWait);
+  return 0;
+}
+
 void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
 {
-  if (m_bStarted)
+  if (m_cec)
   {
     cec_log_message message;
     message.level = level;
+    message.time = GetTimeMs() - m_iStartTime;
     snprintf(message.message, sizeof(message.message), "%s", strMessage.c_str());
     m_logBuffer.Push(message);
   }
 }
 
+void CLibCEC::AddKey(cec_keypress &key)
+{
+  m_keyBuffer.Push(key);
+  m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
+  m_buttontime = 0;
+}
+
 void CLibCEC::AddKey(void)
 {
-  if (m_bStarted && m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
+  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
   {
     cec_keypress key;
 
@@ -201,27 +331,16 @@ void CLibCEC::AddKey(void)
     key.keycode = m_iCurrentButton;
     m_keyBuffer.Push(key);
     m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
-    m_buttontime = 0;
   }
+  m_buttontime = 0;
 }
 
-void CLibCEC::AddCommand(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_frame *parameters)
+void CLibCEC::AddCommand(const cec_command &command)
 {
-  if (!m_bStarted)
-    return;
-
-  cec_command command;
-  command.clear();
-
-  command.source       = source;
-  command.destination  = destination;
-  command.opcode       = opcode;
-  if (parameters)
-    command.parameters = *parameters;
   if (m_commandBuffer.Push(command))
   {
     CStdString strDebug;
-    strDebug.Format("stored command '%d' in the command buffer. buffer size = %d", opcode, m_commandBuffer.Size());
+    strDebug.Format("stored command '%2x' in the command buffer. buffer size = %d", command.opcode, m_commandBuffer.Size());
     AddLog(CEC_LOG_DEBUG, strDebug);
   }
   else
@@ -243,6 +362,13 @@ void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
 {
   m_iCurrentButton = iButtonCode;
   m_buttontime = GetTimeMs();
+
+  /* push keypress to the keybuffer with 0 duration.
+     push another press to the keybuffer with the duration set when the button is released */
+  cec_keypress key;
+  key.duration = 0;
+  key.keycode = m_iCurrentButton;
+  m_keyBuffer.Push(key);
 }
 
 void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
@@ -250,6 +376,11 @@ void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAdd
   return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
 }
 
+void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types)
+{
+  return static_cast< void* > (new CLibCEC(strDeviceName, types));
+}
+
 void CECDestroy(CEC::ICECAdapter *instance)
 {
   CLibCEC *lib = static_cast< CLibCEC* > (instance);