cec: fixed - old client versions didn't always provide a valid physical address in...
[deb_libcec.git] / src / lib / LibCEC.cpp
index a226d41e1ddf8263feac02dc9c406dba9a601cbb..effade5bdd02a04846fba055ba3d0e706c78a13a 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
 
 #include "LibCEC.h"
 
-#include "AdapterCommunication.h"
-#include "AdapterDetection.h"
+#include "adapter/USBCECAdapterDetection.h"
+#include "adapter/USBCECAdapterCommunication.h"
 #include "CECProcessor.h"
+#include "CECTypeUtils.h"
+#include "devices/CECAudioSystem.h"
 #include "devices/CECBusDevice.h"
-#include "util/StdString.h"
-#include "platform/timeutils.h"
+#include "devices/CECPlaybackDevice.h"
+#include "devices/CECTV.h"
+#include "platform/util/timeutils.h"
+#include "platform/util/StdString.h"
+#include "platform/util/util.h"
+
+#include "CECClient.h"
 
 using namespace std;
 using namespace CEC;
+using namespace PLATFORM;
 
-CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) :
+//TODO replace deprecated constructor in 2.0
+CLibCEC::CLibCEC(const char *UNUSED(strDeviceName), cec_device_type_list UNUSED(types), uint16_t UNUSED(iPhysicalAddress) /* = 0 */) :
     m_iStartTime(GetTimeMs()),
-    m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
-    m_buttontime(0)
+    m_client(NULL)
 {
-  m_comm = new CAdapterCommunication(this);
-  m_cec = new CCECProcessor(this, m_comm, strDeviceName, types);
+  m_cec = new CCECProcessor(this);
 }
 
-CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
+//TODO replace deprecated constructor in 2.0
+CLibCEC::CLibCEC(libcec_configuration *UNUSED(configuration)) :
     m_iStartTime(GetTimeMs()),
-    m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
-    m_buttontime(0)
+    m_client(NULL)
 {
-  m_comm = new CAdapterCommunication(this);
-  m_cec = new CCECProcessor(this, m_comm, strDeviceName, iLogicalAddress, iPhysicalAddress);
+  m_cec = new CCECProcessor(this);
 }
 
 CLibCEC::~CLibCEC(void)
 {
-  Close();
-  delete m_cec;
-  delete m_comm;
+  // unregister all clients client
+  UnregisterClients();
+
+  // delete the adapter connection
+  DELETE_AND_NULL(m_cec);
 }
 
-bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
+bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
 {
-  if (!m_comm)
-  {
-    AddLog(CEC_LOG_ERROR, "no comm port");
+  if (!m_cec || !strPort)
     return false;
-  }
 
-  if (m_comm->IsOpen())
+  // open a new connection
+  if (!m_cec->Start(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs))
   {
-    AddLog(CEC_LOG_ERROR, "connection already open");
+    AddLog(CEC_LOG_ERROR, "could not start CEC communications");
     return false;
   }
 
-  int64_t iNow = GetTimeMs();
-  int64_t iTarget = iNow + iTimeoutMs;
-
-  bool bOpened(false);
-  while (!bOpened && iNow < iTarget)
+  // register all clients
+  for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
   {
-    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())
-  {
-    AddLog(CEC_LOG_ERROR, "could not start CEC communications");
-    return false;
+    if (!m_cec->RegisterClient(*it))
+    {
+      AddLog(CEC_LOG_ERROR, "failed to register a CEC client");
+      return false;
+    }
   }
 
   return true;
@@ -108,228 +102,511 @@ bool CLibCEC::Open(const char *strPort, uint32_t iTimeoutMs /* = 10000 */)
 
 void CLibCEC::Close(void)
 {
-  if (m_cec)
-    m_cec->StopThread();
-  if (m_comm)
-    m_comm->Close();
+  if (!m_cec)
+    return;
+
+  // unregister all clients
+  m_cec->UnregisterClients();
+
+  // close the connection
+  m_cec->Close();
 }
 
 int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
 {
-  CStdString strDebug;
-  if (strDevicePath)
-    strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath);
-  else
-    strDebug.Format("trying to autodetect all CEC adapters");
-  AddLog(CEC_LOG_DEBUG, strDebug);
+  if (!CUSBCECAdapterDetection::CanAutodetect())
+  {
+    AddLog(CEC_LOG_WARNING, "libCEC has not been compiled with adapter detection code for this target, so the path to the COM port has to be provided to libCEC");
+    return 0;
+  }
+
+  return CUSBCECAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
+}
 
-  return CAdapterDetection::FindAdapters(deviceList, iBufSize, strDevicePath);
+bool CLibCEC::StartBootloader(void)
+{
+  return m_cec ? m_cec->StartBootloader() : false;
 }
 
 bool CLibCEC::PingAdapter(void)
 {
-  return m_comm ? m_comm->PingAdapter() : false;
+  return m_client ? m_client->PingAdapter() : false;
 }
 
-bool CLibCEC::StartBootloader(void)
+bool CLibCEC::EnableCallbacks(void *cbParam, ICECCallbacks *callbacks)
 {
-  return m_comm ? m_comm->StartBootloader() : false;
+  return m_client ? m_client->EnableCallbacks(cbParam, callbacks) : false;
 }
 
-bool CLibCEC::GetNextLogMessage(cec_log_message *message)
+bool CLibCEC::GetCurrentConfiguration(libcec_configuration *configuration)
 {
-  return (m_logBuffer.Pop(*message));
+  return m_client ? m_client->GetCurrentConfiguration(*configuration) : false;
 }
 
-bool CLibCEC::GetNextKeypress(cec_keypress *key)
+bool CLibCEC::SetConfiguration(const libcec_configuration *configuration)
 {
-  return m_keyBuffer.Pop(*key);
+  return m_client ? m_client->SetConfiguration(*configuration) : false;
 }
 
-bool CLibCEC::GetNextCommand(cec_command *command)
+bool CLibCEC::CanPersistConfiguration(void)
 {
-  return m_commandBuffer.Pop(*command);
+  return m_client ? m_client->CanPersistConfiguration() : false;
+}
+
+bool CLibCEC::PersistConfiguration(libcec_configuration *configuration)
+{
+  return m_client ? m_client->PersistConfiguration(*configuration) : false;
+}
+
+void CLibCEC::RescanActiveDevices(void)
+{
+  if (m_client)
+    m_client->RescanActiveDevices();
+}
+
+bool CLibCEC::IsLibCECActiveSource(void)
+{
+  return m_client ? m_client->IsLibCECActiveSource() : false;
 }
 
 bool CLibCEC::Transmit(const cec_command &data)
 {
-  return m_cec ? m_cec->Transmit(data) : false;
+  return m_client ? m_client->Transmit(data) : false;
 }
 
 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
 {
-  return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
+  return m_client ? m_client->SetLogicalAddress(iLogicalAddress) : false;
 }
 
-bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress)
+bool CLibCEC::SetPhysicalAddress(uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
 {
-  return m_cec ? m_cec->SetPhysicalAddress(iPhysicalAddress) : false;
+  return m_client ? m_client->SetPhysicalAddress(iPhysicalAddress) : false;
+}
+
+bool CLibCEC::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort /* = CEC_DEFAULT_HDMI_PORT */)
+{
+  return m_client ? m_client->SetHDMIPort(iBaseDevice, iPort) : false;
 }
 
 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
 {
-  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->PowerOn() : false;
+  return m_client ? m_client->SendPowerOnDevices(address) : false;
 }
 
 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
 {
-  return m_cec && address >= CECDEVICE_TV && address <= CECDEVICE_BROADCAST ? m_cec->m_busDevices[(uint8_t)address]->Standby() : false;
+  return m_client ? m_client->SendStandbyDevices(address) : false;
 }
 
 bool CLibCEC::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
 {
-  return m_cec ? m_cec->SetActiveSource(type) : false;
+  return m_client ? m_client->SendSetActiveSource(type) : false;
 }
 
-bool CLibCEC::SetActiveView(void)
+bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
 {
-  return m_cec ? m_cec->SetActiveView() : false;
+  return m_client ? m_client->SendSetDeckControlMode(mode, bSendUpdate) : false;
 }
 
-bool CLibCEC::SetDeckControlMode(cec_deck_control_mode mode)
+bool CLibCEC::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
 {
-  return m_cec ? m_cec->SetDeckControlMode(mode) : false;
+  return m_client ? m_client->SendSetDeckInfo(info, bSendUpdate) : false;
 }
 
-bool CLibCEC::SetDeckInfo(cec_deck_info info)
+bool CLibCEC::SetInactiveView(void)
 {
-  return m_cec ? m_cec->SetDeckInfo(info) : false;
+  return m_client ? m_client->SendSetInactiveView() : false;
 }
 
-bool CLibCEC::SetInactiveView(void)
+bool CLibCEC::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
 {
-  return m_cec ? m_cec->SetInactiveView() : false;
+  return m_client ? m_client->SendSetMenuState(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;
+  return m_client ? m_client->SendSetOSDString(iLogicalAddress, duration, strMessage) : false;
 }
 
 bool CLibCEC::SwitchMonitoring(bool bEnable)
 {
-  return m_cec ? m_cec->SwitchMonitoring(bEnable) : false;
+  return m_client ? m_client->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;
+  return m_client ? m_client->GetDeviceCecVersion(iAddress) : 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;
+  return m_client ? m_client->GetDeviceMenuLanguage(iAddress, *language) : 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;
+  return m_client ? m_client->GetDeviceVendorId(iAddress) : (uint64_t)CEC_VENDOR_UNKNOWN;
+}
+
+uint16_t CLibCEC::GetDevicePhysicalAddress(cec_logical_address iAddress)
+{
+  return m_client ? m_client->GetDevicePhysicalAddress(iAddress) : CEC_INVALID_PHYSICAL_ADDRESS;
 }
 
 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;
+  return m_client ? m_client->GetDevicePowerStatus(iAddress) : 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;
+  return m_client ? m_client->PollDevice(iAddress) : false;
 }
 
-void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
+cec_logical_addresses CLibCEC::GetActiveDevices(void)
 {
-  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);
-  }
+  cec_logical_addresses addresses;
+  addresses.Clear();
+  if (m_client)
+    addresses = m_client->GetActiveDevices();
+  return addresses;
 }
 
-void CLibCEC::AddKey(cec_keypress &key)
+bool CLibCEC::IsActiveDevice(cec_logical_address iAddress)
 {
-  m_keyBuffer.Push(key);
-  m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
-  m_buttontime = 0;
+  return m_client ? m_client->IsActiveDevice(iAddress) : false;
 }
 
-void CLibCEC::AddKey(void)
+bool CLibCEC::IsActiveDeviceType(cec_device_type type)
 {
-  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
-  {
-    cec_keypress key;
+  return m_client ? m_client->IsActiveDeviceType(type) : false;
+}
 
-    key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
-    key.keycode = m_iCurrentButton;
-    m_keyBuffer.Push(key);
-    m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
-  }
-  m_buttontime = 0;
+uint8_t CLibCEC::VolumeUp(bool bSendRelease /* = true */)
+{
+  return m_client ? m_client->SendVolumeUp(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
 }
 
-void CLibCEC::AddCommand(const cec_command &command)
+uint8_t CLibCEC::VolumeDown(bool bSendRelease /* = true */)
 {
-  if (m_commandBuffer.Push(command))
-  {
-    CStdString strDebug;
-    strDebug.Format("stored command '%2x' in the command buffer. buffer size = %d", command.opcode, m_commandBuffer.Size());
-    AddLog(CEC_LOG_DEBUG, strDebug);
-  }
-  else
-  {
-    AddLog(CEC_LOG_WARNING, "command buffer is full");
-  }
+  return m_client ? m_client->SendVolumeDown(bSendRelease) : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
+}
+
+uint8_t CLibCEC::MuteAudio(bool UNUSED(bSendRelease) /* = true */)
+{
+  return m_client ? m_client->SendMuteAudio() : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
+}
+
+bool CLibCEC::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
+{
+  return m_client ? m_client->SendKeypress(iDestination, key, bWait) : false;
+}
+
+bool CLibCEC::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
+{
+  return m_client ? m_client->SendKeyRelease(iDestination, bWait) : false;
+}
+
+cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress)
+{
+  cec_osd_name retVal;
+  retVal.device = CECDEVICE_UNKNOWN;
+  memset(retVal.name, 0, 14);
+
+  if (m_client)
+    retVal = m_client->GetDeviceOSDName(iAddress);
+  return retVal;
+}
+
+cec_logical_address CLibCEC::GetActiveSource(void)
+{
+  return m_client ? m_client->GetActiveSource() : CECDEVICE_UNKNOWN;
+}
+
+bool CLibCEC::IsActiveSource(cec_logical_address iAddress)
+{
+  return m_client ? m_client->IsActiveSource(iAddress) : false;
+}
+bool CLibCEC::SetStreamPath(cec_logical_address iAddress)
+{
+  return m_client ? m_client->SetStreamPath(iAddress) : false;
+}
+
+bool CLibCEC::SetStreamPath(uint16_t iPhysicalAddress)
+{
+  return m_client ? m_client->SetStreamPath(iPhysicalAddress) : false;
+}
+
+cec_logical_addresses CLibCEC::GetLogicalAddresses(void)
+{
+  cec_logical_addresses addresses;
+  addresses.Clear();
+  if (m_client)
+    m_client->GetLogicalAddresses();
+  return addresses;
+}
+
+bool CLibCEC::GetNextLogMessage(cec_log_message *message)
+{
+  return m_client ? m_client->GetNextLogMessage(message) : false;
+}
+
+bool CLibCEC::GetNextKeypress(cec_keypress *key)
+{
+  return m_client ? m_client->GetNextKeypress(key) : false;
+}
+
+bool CLibCEC::GetNextCommand(cec_command *command)
+{
+  return m_client ? m_client->GetNextCommand(command) : false;
+}
+
+cec_device_type CLibCEC::GetType(cec_logical_address address)
+{
+  return CCECTypeUtils::GetType(address);
+}
+
+uint16_t CLibCEC::GetMaskForType(cec_logical_address address)
+{
+  return CCECTypeUtils::GetMaskForType(address);
+}
+
+uint16_t CLibCEC::GetMaskForType(cec_device_type type)
+{
+  return CCECTypeUtils::GetMaskForType(type);
+}
+
+bool CLibCEC::IsValidPhysicalAddress(uint16_t iPhysicalAddress)
+{
+  return iPhysicalAddress >= CEC_MIN_PHYSICAL_ADDRESS &&
+         iPhysicalAddress <= CEC_MAX_PHYSICAL_ADDRESS;
+}
+
+const char *CLibCEC::ToString(const cec_device_type type)
+{
+  return CCECTypeUtils::ToString(type);
+}
+
+const char *CLibCEC::ToString(const cec_menu_state state)
+{
+  return CCECTypeUtils::ToString(state);
+}
+
+const char *CLibCEC::ToString(const cec_version version)
+{
+  return CCECTypeUtils::ToString(version);
+}
+
+const char *CLibCEC::ToString(const cec_power_status status)
+{
+  return CCECTypeUtils::ToString(status);
+}
+
+const char *CLibCEC::ToString(const cec_logical_address address)
+{
+  return CCECTypeUtils::ToString(address);
+}
+
+const char *CLibCEC::ToString(const cec_deck_control_mode mode)
+{
+  return CCECTypeUtils::ToString(mode);
+}
+
+const char *CLibCEC::ToString(const cec_deck_info status)
+{
+  return CCECTypeUtils::ToString(status);
+}
+
+const char *CLibCEC::ToString(const cec_opcode opcode)
+{
+  return CCECTypeUtils::ToString(opcode);
+}
+
+const char *CLibCEC::ToString(const cec_system_audio_status mode)
+{
+  return CCECTypeUtils::ToString(mode);
+}
+
+const char *CLibCEC::ToString(const cec_audio_status status)
+{
+  return CCECTypeUtils::ToString(status);
+}
+
+const char *CLibCEC::ToString(const cec_vendor_id vendor)
+{
+  return CCECTypeUtils::ToString(vendor);
+}
+
+const char *CLibCEC::ToString(const cec_client_version version)
+{
+  return CCECTypeUtils::ToString(version);
+}
+
+const char *CLibCEC::ToString(const cec_server_version version)
+{
+  return CCECTypeUtils::ToString(version);
 }
 
 void CLibCEC::CheckKeypressTimeout(void)
 {
-  if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
-  {
-    AddKey();
-    m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
-  }
+  // check all clients
+  for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
+    (*it)->CheckKeypressTimeout();
 }
 
-void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
+void CLibCEC::AddLog(const cec_log_level level, const char *strFormat, ...)
 {
-  m_iCurrentButton = iButtonCode;
-  m_buttontime = GetTimeMs();
+  CStdString strLog;
 
-  /* 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);
+  // format the message
+  va_list argList;
+  va_start(argList, strFormat);
+  strLog.FormatV(strFormat, argList);
+  va_end(argList);
+
+  cec_log_message message;
+  message.level = level;
+  message.time = GetTimeMs() - m_iStartTime;
+  snprintf(message.message, sizeof(message.message), "%s", strLog.c_str());
+
+  // send the message to all clients
+  for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
+    (*it)->AddLog(message);
+}
+
+void CLibCEC::AddCommand(const cec_command &command)
+{
+  // send the command to all clients
+  for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
+    (*it)->AddCommand(command);
+}
+
+void CLibCEC::Alert(const libcec_alert type, const libcec_parameter &param)
+{
+  // send the alert to all clients
+  for (vector<CCECClient *>::iterator it = m_clients.begin(); it != m_clients.end(); it++)
+    (*it)->Alert(type, param);
+}
+
+bool CLibCEC::SetActiveView(void)
+{
+  AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
+  return SetActiveSource();
+}
+
+bool CLibCEC::EnablePhysicalAddressDetection(void)
+{
+  AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
+  return true;
+}
+
+CCECClient *CLibCEC::RegisterClient(libcec_configuration &configuration)
+{
+  if (!m_cec)
+    return NULL;
+
+  // create a new client instance
+  CCECClient *newClient = new CCECClient(m_cec, configuration);
+  if (!newClient)
+    return NULL;
+  m_clients.push_back(newClient);
+
+  // if the default client isn't set, set it
+  if (!m_client)
+    m_client = newClient;
+
+  // register the new client
+  if (m_cec->CECInitialised())
+    m_cec->RegisterClient(newClient);
+
+  return newClient;
+}
+
+void CLibCEC::UnregisterClients(void)
+{
+  if (m_cec)
+    m_cec->UnregisterClients();
+
+  m_clients.clear();
+
+  DELETE_AND_NULL(m_client);
 }
 
-void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
+void * CECInitialise(libcec_configuration *configuration)
 {
-  return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
+  if (!configuration)
+    return NULL;
+
+  // create a new libCEC instance
+  CLibCEC *lib = new CLibCEC(NULL);
+
+  // register a new client
+  CCECClient *client(NULL);
+  if (lib && configuration)
+    client = lib->RegisterClient(*configuration);
+
+  // update the current configuration
+  if (client)
+    client->GetCurrentConfiguration(*configuration);
+
+  // ensure that the correct server version is set
+  configuration->serverVersion = LIBCEC_VERSION_CURRENT;
+
+  return static_cast< void* > (lib);
 }
 
-void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types)
+void * CECInit(const char *strDeviceName, CEC::cec_device_type_list types, uint16_t UNUSED(iPhysicalAddress) /* = 0 */)
 {
-  return static_cast< void* > (new CLibCEC(strDeviceName, types));
+  libcec_configuration configuration;
+
+  // client version < 1.5.0
+  snprintf(configuration.strDeviceName, 13, "%s", strDeviceName);
+  configuration.deviceTypes      = types;
+  configuration.iPhysicalAddress = CEC_INVALID_PHYSICAL_ADDRESS;
+
+  if (configuration.deviceTypes.IsEmpty())
+    configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
+
+  return CECInitialise(&configuration);
+}
+
+bool CECStartBootloader(void)
+{
+  bool bReturn(false);
+  cec_adapter deviceList[1];
+  if (CUSBCECAdapterDetection::FindAdapters(deviceList, 1) > 0)
+  {
+    CUSBCECAdapterCommunication comm(NULL, deviceList[0].comm);
+    CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
+    while (timeout.TimeLeft() > 0 && (bReturn = comm.Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
+    {
+      comm.Close();
+      CEvent::Sleep(500);
+    }
+    if (comm.IsOpen())
+      bReturn = comm.StartBootloader();
+  }
+
+  return bReturn;
 }
 
 void CECDestroy(CEC::ICECAdapter *instance)
 {
-  CLibCEC *lib = static_cast< CLibCEC* > (instance);
-  if (lib)
-    delete lib;
+  DELETE_AND_NULL(instance);
 }
+
+bool CLibCEC::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
+{
+  if (m_cec->IsRunning())
+    return false;
+
+  return m_cec->GetDeviceInformation(strPort, config, iTimeoutMs);
+}
+
+// no longer being used
+void CLibCEC::AddKey(const cec_keypress &UNUSED(key)) {}
+void CLibCEC::ConfigurationChanged(const libcec_configuration &UNUSED(config)) {}
+void CLibCEC::SetCurrentButton(cec_user_control_code UNUSED(iButtonCode)) {}
+CLibCEC *CLibCEC::GetInstance(void) { return NULL; }
+void CLibCEC::SetInstance(CLibCEC *UNUSED(instance)) {}