cec: simplified AddLog() method
[deb_libcec.git] / src / lib / CECProcessor.cpp
index 7f688dd134295872bc710464921f121d669e5a66..7a4e019d0b7fdfb2a48968f9ff185c54c82ce2f0 100644 (file)
@@ -32,7 +32,7 @@
 
 #include "CECProcessor.h"
 
-#include "AdapterCommunication.h"
+#include "adapter/AdapterMessage.h"
 #include "devices/CECBusDevice.h"
 #include "devices/CECAudioSystem.h"
 #include "devices/CECPlaybackDevice.h"
 #include "devices/CECTV.h"
 #include "implementations/CECCommandHandler.h"
 #include "LibCEC.h"
-#include "util/StdString.h"
-#include "platform/timeutils.h"
 
 using namespace CEC;
 using namespace std;
+using namespace PLATFORM;
 
-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_bInitialised(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_bMonitor(false),
+    m_iStandardLineTimeout(3),
+    m_iRetryLineTimeout(3),
+    m_iLastTransmission(0)
 {
+  m_communication = new CAdapterCommunication(this);
   m_logicalAddresses.Clear();
   m_logicalAddresses.Set(iLogicalAddress);
   m_types.clear();
@@ -62,15 +67,20 @@ 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_bInitialised(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_bMonitor(false),
+    m_iStandardLineTimeout(3),
+    m_iRetryLineTimeout(3),
+    m_iLastTransmission(0)
 {
+  m_communication = new CAdapterCommunication(this);
   m_logicalAddresses.Clear();
   for (int iPtr = 0; iPtr < 16; iPtr++)
   {
@@ -107,48 +117,121 @@ CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm
 
 CCECProcessor::~CCECProcessor(void)
 {
+  m_bStarted = false;
   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::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs)
 {
-  CLockObject lock(&m_mutex);
-  if (!m_communication || !m_communication->IsOpen())
+  bool bReturn(false);
+  CLockObject lock(m_mutex);
+  if (!m_communication)
   {
-    m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
-    return false;
+    CLibCEC::AddLog(CEC_LOG_ERROR, "no connection handler found");
+    return bReturn;
   }
 
-  if (CreateThread())
+  /* check for an already opened connection */
+  if (m_communication->IsOpen())
+  {
+    CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened");
+    return bReturn;
+  }
+
+  /* open a new connection */
+  if ((bReturn = m_communication->Open(strPort, iBaudRate, iTimeoutMs)) == false)
+    CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection");
+
+  /* try to ping the adapter */
+  if ((bReturn = m_communication->PingAdapter()) == false)
+    CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter does not respond correctly");
+
+  uint16_t iFirmwareVersion = m_communication->GetFirmwareVersion();
+  if ((bReturn = (iFirmwareVersion != CEC_FW_VERSION_UNKNOWN)) == false)
+    m_controller->AddLog(CEC_LOG_ERROR, "the adapter is running an unknown firmware version");
+
+  CLibCEC::AddLog(CEC_LOG_NOTICE, "CEC Adapter firmware version: %d", iFirmwareVersion);
+
+  return bReturn;
+}
+
+void CCECProcessor::SetInitialised(bool bSetTo /* = true */)
+{
+  CLockObject lock(m_mutex);
+  m_bInitialised = bSetTo;
+}
+
+bool CCECProcessor::Initialise(void)
+{
+  bool bReturn(false);
   {
-    if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
+    CLockObject lock(m_mutex);
+    if (!m_logicalAddresses.IsEmpty())
+      m_logicalAddresses.Clear();
+
+    if (!FindLogicalAddresses())
     {
-      m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
-      return false;
+      CLibCEC::AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
+      return bReturn;
     }
-    return true;
+
+    /* only set our OSD name for the primary device */
+    m_busDevices[m_logicalAddresses.primary]->m_strDeviceName = m_strDeviceName;
+  }
+
+  /* get the vendor id from the TV, so we are using the correct handler */
+  m_busDevices[CECDEVICE_TV]->RequestVendorId();
+  ReplaceHandlers();
+
+  if ((bReturn = SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true)) == false)
+    CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort, ToString(m_iBaseDevice), (uint8_t)m_iBaseDevice);
+
+  SetInitialised(bReturn);
+
+  return bReturn;
+}
+
+bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
+{
+  bool bReturn(false);
+
+  {
+    CLockObject lock(m_mutex);
+    if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
+      return bReturn;
+
+    /* create the processor thread */
+    if (!CreateThread() || !m_startCondition.Wait(m_mutex) || !m_bStarted)
+    {
+      CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+      return bReturn;
+    }
+  }
+
+  if ((bReturn = Initialise()) == false)
+  {
+    CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+    StopThread(true);
   }
   else
-    m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
+  {
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
+  }
 
-  return false;
+  return bReturn;
 }
 
 bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
 {
   if (m_busDevices[address]->TryLogicalAddress())
   {
-    /* only set our OSD name and active source for the primary device */
-    if (m_logicalAddresses.IsEmpty())
-    {
-      m_busDevices[address]->m_strDeviceName = m_strDeviceName;
-      m_busDevices[address]->m_bActiveSource = true;
-    }
     m_logicalAddresses.Set(address);
     return true;
   }
@@ -158,7 +241,7 @@ bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
 
 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
 {
-  AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
   return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
       TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
       TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
@@ -166,7 +249,7 @@ bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
 
 bool CCECProcessor::FindLogicalAddressTuner(void)
 {
-  AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
   return TryLogicalAddress(CECDEVICE_TUNER1) ||
       TryLogicalAddress(CECDEVICE_TUNER2) ||
       TryLogicalAddress(CECDEVICE_TUNER3) ||
@@ -175,7 +258,7 @@ bool CCECProcessor::FindLogicalAddressTuner(void)
 
 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
 {
-  AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
   return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
       TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
       TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
@@ -183,23 +266,98 @@ bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
 
 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
 {
-  AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
   return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
 }
 
+bool CCECProcessor::ChangeDeviceType(cec_device_type from, cec_device_type to)
+{
+  bool bChanged(false);
+
+  CLibCEC::AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
+
+  CLockObject lock(m_mutex);
+  CCECBusDevice *previousDevice = GetDeviceByType(from);
+  m_logicalAddresses.primary = CECDEVICE_UNKNOWN;
+
+  for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
+  {
+    if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
+      continue;
+
+    if (m_types.types[iPtr] == from)
+    {
+      bChanged = true;
+      m_types.types[iPtr] = to;
+    }
+    else if (m_types.types[iPtr] == to && bChanged)
+    {
+      m_types.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
+    }
+  }
+
+  if (bChanged)
+  {
+    FindLogicalAddresses();
+
+    CCECBusDevice *newDevice = GetDeviceByType(to);
+    if (previousDevice && newDevice)
+    {
+      newDevice->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
+      previousDevice->SetDeviceStatus(CEC_DEVICE_STATUS_UNKNOWN);
+
+      newDevice->SetCecVersion(previousDevice->GetCecVersion(false));
+      previousDevice->SetCecVersion(CEC_VERSION_UNKNOWN);
+
+      newDevice->SetMenuLanguage(previousDevice->GetMenuLanguage(false));
+      cec_menu_language lang;
+      lang.device = previousDevice->GetLogicalAddress();
+      for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
+        lang.language[iPtr] = '?';
+      lang.language[3] = 0;
+      previousDevice->SetMenuLanguage(lang);
+
+      newDevice->SetMenuState(previousDevice->GetMenuState());
+      previousDevice->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
+
+      newDevice->SetOSDName(previousDevice->GetOSDName(false));
+      previousDevice->SetOSDName(ToString(previousDevice->GetLogicalAddress()));
+
+      newDevice->SetPhysicalAddress(previousDevice->GetPhysicalAddress(false));
+      previousDevice->SetPhysicalAddress(0xFFFF);
+
+      newDevice->SetPowerStatus(previousDevice->GetPowerStatus(false));
+      previousDevice->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN);
+
+      newDevice->SetVendorId(previousDevice->GetVendorId(false));
+      previousDevice->SetVendorId(CEC_VENDOR_UNKNOWN);
+
+      if ((from == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || from == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
+          (to == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || to == CEC_DEVICE_TYPE_RECORDING_DEVICE))
+      {
+        ((CCECPlaybackDevice *) newDevice)->SetDeckControlMode(((CCECPlaybackDevice *) previousDevice)->GetDeckControlMode());
+        ((CCECPlaybackDevice *) previousDevice)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP);
+
+        ((CCECPlaybackDevice *) newDevice)->SetDeckStatus(((CCECPlaybackDevice *) previousDevice)->GetDeckStatus());
+        ((CCECPlaybackDevice *) previousDevice)->SetDeckStatus(CEC_DECK_INFO_STOP);
+      }
+    }
+  }
+
+  return true;
+}
+
 bool CCECProcessor::FindLogicalAddresses(void)
 {
   bool bReturn(true);
   m_logicalAddresses.Clear();
-  CStdString strLog;
 
   for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
   {
     if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
       continue;
 
-    strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
-    AddLog(CEC_LOG_DEBUG, strLog);
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
 
     if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
       bReturn &= FindLogicalAddressRecordingDevice();
@@ -211,9 +369,18 @@ bool CCECProcessor::FindLogicalAddresses(void)
       bReturn &= FindLogicalAddressAudioSystem();
   }
 
+  if (bReturn)
+    SetAckMask(m_logicalAddresses.AckMask());
+
   return bReturn;
 }
 
+void CCECProcessor::ReplaceHandlers(void)
+{
+  for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++)
+    m_busDevices[iPtr]->ReplaceHandler(m_bInitialised);
+}
+
 void *CCECProcessor::Process(void)
 {
   bool                  bParseFrame(false);
@@ -221,45 +388,26 @@ void *CCECProcessor::Process(void)
   CCECAdapterMessage    msg;
 
   {
-    if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
-    {
-      CLockObject lock(&m_mutex);
-      m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
-      m_startCondition.Signal();
-      return NULL;
-    }
-
-    SetAckMask(m_logicalAddresses.AckMask());
-
-    ScanCECBus();
-
-    {
-      CLockObject lock(&m_mutex);
-      m_bStarted = true;
-      lock.Leave();
-
-      SetHDMIPort(m_iHDMIPort);
-
-      lock.Lock();
-      m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
-      m_startCondition.Signal();
-    }
+    CLockObject lock(m_mutex);
+    m_bStarted = true;
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
+    m_startCondition.Signal();
   }
 
   while (!IsStopped())
   {
+    ReplaceHandlers();
     command.Clear();
-    msg.clear();
+    msg.Clear();
 
     {
-      CLockObject lock(&m_mutex);
+      CLockObject lock(m_mutex);
       if (m_commandBuffer.Pop(command))
       {
         bParseFrame = true;
       }
       else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
       {
-        m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
         if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
           command = m_currentframe;
       }
@@ -272,16 +420,11 @@ void *CCECProcessor::Process(void)
     Sleep(5);
 
     m_controller->CheckKeypressTimeout();
-
-    for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
-    {
-      if (!m_logicalAddresses[iDevicePtr])
-        m_busDevices[iDevicePtr]->PollVendorId();
-    }
-
-    Sleep(5);
   }
 
+  if (m_communication)
+    m_communication->Close();
+
   return NULL;
 }
 
@@ -296,7 +439,7 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE
 
   if (type != CEC_DEVICE_TYPE_RESERVED)
   {
-    for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
+    for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
     {
       if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
       {
@@ -306,13 +449,46 @@ bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RE
     }
   }
 
-  return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false)) &&
-      m_busDevices[addr]->TransmitActiveSource();
+  m_busDevices[addr]->SetActiveSource();
+  if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF)
+  {
+    bReturn = m_busDevices[addr]->TransmitActiveSource();
+
+    if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
+        m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
+        m_busDevices[addr]->GetHandler()->SendDeckStatusUpdateOnActiveSource())
+    {
+      bReturn = ((CCECPlaybackDevice *)m_busDevices[addr])->TransmitDeckStatus(CECDEVICE_TV);
+    }
+  }
+
+  return bReturn;
+}
+
+bool CCECProcessor::SetActiveSource(uint16_t iStreamPath)
+{
+  bool bReturn(false);
+
+  CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
+  if (device)
+  {
+    device->SetActiveSource();
+    bReturn = true;
+  }
+
+  return bReturn;
+}
+
+void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
+{
+  CLockObject lock(m_mutex);
+  m_iStandardLineTimeout = iTimeout;
 }
 
-bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
+void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
 {
-  return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
+  CLockObject lock(m_mutex);
+  m_iRetryLineTimeout = iTimeout;
 }
 
 bool CCECProcessor::SetActiveView(void)
@@ -352,60 +528,54 @@ bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true *
   return bReturn;
 }
 
-bool CCECProcessor::SetHDMIPort(uint8_t iPort)
+bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
 {
   bool bReturn(false);
+  CLockObject lock(m_mutex);
 
-  CStdString strLog;
-  strLog.Format("setting HDMI port to %d", iPort);
-  AddLog(CEC_LOG_DEBUG, strLog);
-
+  m_iBaseDevice = iBaseDevice;
   m_iHDMIPort = iPort;
-  if (!m_bStarted)
+  if (!m_bStarted && !bForce)
     return true;
 
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
+
   uint16_t iPhysicalAddress(0);
-  int iPos = 3;
-  while(!bReturn && iPos >= 0)
+  if (iBaseDevice > CECDEVICE_TV)
   {
-    iPhysicalAddress += ((uint16_t)iPort * (0x1 << iPos*4));
-    strLog.Format("checking physical address %4x", iPhysicalAddress);
-    AddLog(CEC_LOG_DEBUG, strLog);
-    if (CheckPhysicalAddress(iPhysicalAddress))
-    {
-      strLog.Format("physical address %4x is in use", iPhysicalAddress);
-      AddLog(CEC_LOG_DEBUG, strLog);
-      iPos--;
-    }
-    else
-    {
-      SetPhysicalAddress(iPhysicalAddress);
-      bReturn = true;
-    }
+    lock.Unlock();
+    iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
+    lock.Lock();
   }
 
-  return bReturn;
-}
+  if (iPhysicalAddress < 0xffff)
+  {
+    if (iPhysicalAddress == 0)
+      iPhysicalAddress += 0x1000 * iPort;
+    else if (iPhysicalAddress % 0x1000 == 0)
+      iPhysicalAddress += 0x100 * iPort;
+    else if (iPhysicalAddress % 0x100 == 0)
+      iPhysicalAddress += 0x10 * iPort;
+    else if (iPhysicalAddress % 0x10 == 0)
+      iPhysicalAddress += iPort;
 
-void CCECProcessor::ScanCECBus(void)
-{
-  CCECBusDevice *device(NULL);
-  for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+    bReturn = true;
+  }
+
+  if (!bReturn)
+    CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
+  else
   {
-    device = m_busDevices[iPtr];
-    if (device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
-    {
-      device->GetPhysicalAddress();
-      device->GetVendorId();
-      device->GetCecVersion();
-      device->GetPowerStatus();
-    }
+    lock.Unlock();
+    SetPhysicalAddress(iPhysicalAddress);
   }
+
+  return bReturn;
 }
 
-bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
+bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
 {
-  for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+  for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
   {
     if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
       return true;
@@ -413,27 +583,13 @@ bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress)
   return false;
 }
 
-bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
-{
-  bool bReturn(false);
-
-  CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
-  if (device)
-  {
-    device->SetActiveDevice();
-    bReturn = true;
-  }
-
-  return bReturn;
-}
-
-bool CCECProcessor::SetInactiveView(void)
+bool CCECProcessor::TransmitInactiveSource(void)
 {
   if (!IsRunning())
     return false;
 
   if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
-    return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
+    return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
   return false;
 }
 
@@ -446,16 +602,15 @@ void CCECProcessor::LogOutput(const cec_command &data)
 
   for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
     strTx.AppendFormat(":%02x", data.parameters[iPtr]);
-  m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
+  CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
 }
 
 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
 {
+  CLockObject lock(m_mutex);
   if (m_logicalAddresses.primary != iLogicalAddress)
   {
-    CStdString strLog;
-    strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
-    m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+    CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
     m_logicalAddresses.primary = iLogicalAddress;
     m_logicalAddresses.Set(iLogicalAddress);
     return SetAckMask(m_logicalAddresses.AckMask());
@@ -478,25 +633,51 @@ bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = tru
   return true;
 }
 
-bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
+bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
 {
-  if (!m_logicalAddresses.IsEmpty())
+  bool bSendActiveView(false);
+  bool bReturn(false);
+  cec_logical_addresses sendUpdatesTo;
+
   {
-    for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
-      if (m_logicalAddresses[iPtr])
-        m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
-    return SetActiveView();
+    CLockObject lock(m_mutex);
+    if (!m_logicalAddresses.IsEmpty())
+    {
+      bool bWasActiveSource(false);
+      for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
+        if (m_logicalAddresses[iPtr])
+        {
+          bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
+          m_busDevices[iPtr]->SetInactiveSource();
+          m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
+          if (bSendUpdate)
+            sendUpdatesTo.Set((cec_logical_address)iPtr);
+        }
+
+      bSendActiveView = bWasActiveSource && bSendUpdate;
+      bReturn = true;
+    }
   }
-  return false;
+
+  for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
+    if (sendUpdatesTo[iPtr])
+      m_busDevices[iPtr]->TransmitPhysicalAddress();
+
+  if (bSendActiveView)
+    SetActiveView();
+
+  return bReturn;
 }
 
 bool CCECProcessor::SwitchMonitoring(bool bEnable)
 {
-  CStdString strLog;
-  strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
-  m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
+  CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
+
+  {
+    CLockObject lock(m_mutex);
+    m_bMonitor = bEnable;
+  }
 
-  m_bMonitor = bEnable;
   if (bEnable)
     return SetAckMask(0);
   else
@@ -514,29 +695,29 @@ bool CCECProcessor::PollDevice(cec_logical_address iAddress)
   return false;
 }
 
-uint8_t CCECProcessor::VolumeUp(bool bWait /* = true */)
+uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
 {
   uint8_t status = 0;
-  if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
-    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bWait);
+  if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
+    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
 
   return status;
 }
 
-uint8_t CCECProcessor::VolumeDown(bool bWait /* = true */)
+uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
 {
   uint8_t status = 0;
-  if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
-    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bWait);
+  if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
+    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
 
   return status;
 }
 
-uint8_t CCECProcessor::MuteAudio(bool bWait /* = true */)
+uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
 {
   uint8_t status = 0;
-  if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
-    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bWait);
+  if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
+    status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
 
   return status;
 }
@@ -563,9 +744,9 @@ CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
 {
   CCECBusDevice *device = NULL;
 
-  for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
+  for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
   {
-    if (m_busDevices[iPtr]->m_type == type)
+    if (m_busDevices[iPtr]->m_type == type && m_logicalAddresses[iPtr])
     {
       device = m_busDevices[iPtr];
       break;
@@ -575,11 +756,31 @@ CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
   return device;
 }
 
+CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
+{
+  CCECBusDevice *device(NULL);
+  cec_logical_address primary = m_logicalAddresses.primary;
+  if (primary != CECDEVICE_UNKNOWN)
+    device = m_busDevices[primary];
+  return device;
+}
+
 cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
 {
   return m_busDevices[iAddress]->GetCecVersion();
 }
 
+cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
+{
+  CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
+  cec_osd_name retVal;
+
+  snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
+  retVal.device = iAddress;
+
+  return retVal;
+}
+
 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
 {
   if (m_busDevices[iAddress])
@@ -597,6 +798,13 @@ uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
   return false;
 }
 
+uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
+{
+  if (m_busDevices[iAddress])
+    return m_busDevices[iAddress]->GetPhysicalAddress(false);
+  return false;
+}
+
 cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
 {
   if (m_busDevices[iAddress])
@@ -604,51 +812,74 @@ cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddres
   return CEC_POWER_STATUS_UNKNOWN;
 }
 
+cec_logical_address CCECProcessor::GetActiveSource(void)
+{
+  for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
+  {
+    if (m_busDevices[iPtr]->IsActiveSource())
+      return (cec_logical_address)iPtr;
+  }
+
+  return CECDEVICE_UNKNOWN;
+}
+
+bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
+{
+  return m_busDevices[iAddress]->IsActiveSource();
+}
+
 bool CCECProcessor::Transmit(const cec_command &data)
 {
   bool bReturn(false);
   LogOutput(data);
 
   CCECAdapterMessage *output = new CCECAdapterMessage(data);
+
+  /* set the number of retries */
+  if (data.opcode == CEC_OPCODE_NONE)
+    output->maxTries = 1;
+  else if (data.initiator != CECDEVICE_BROADCAST)
+    output->maxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
+
   bReturn = Transmit(output);
-  delete output;
 
+  /* set to "not present" on failed ack */
+  if (output->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED &&
+      output->Destination() != CECDEVICE_BROADCAST)
+    m_busDevices[output->Destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
+
+  delete output;
   return bReturn;
 }
 
 bool CCECProcessor::Transmit(CCECAdapterMessage *output)
 {
   bool bReturn(false);
-  CLockObject lock(&m_mutex);
+  CLockObject lock(m_mutex);
   {
-    CLockObject msgLock(&output->mutex);
-    if (!m_communication || !m_communication->Write(output))
+    if (!m_communication)
       return bReturn;
-    else
-    {
-      output->condition.Wait(&output->mutex);
-      if (output->state != ADAPTER_MESSAGE_STATE_SENT)
-      {
-        m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
-        return bReturn;
-      }
-    }
 
-    if (output->transmit_timeout > 0)
+    m_iLastTransmission = GetTimeMs();
+    m_communication->SetLineTimeout(m_iStandardLineTimeout);
+    output->tries = 0;
+
+    do
     {
-      if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
-        m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
-    }
-    else
-      bReturn = true;
+      if (output->tries > 0)
+        m_communication->SetLineTimeout(m_iRetryLineTimeout);
+      bReturn = m_communication->Write(output);
+    }while (!bReturn && output->transmit_timeout > 0 && output->NeedsRetry() && ++output->tries < output->maxTries);
   }
 
+  m_communication->SetLineTimeout(m_iStandardLineTimeout);
+
   return bReturn;
 }
 
 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
 {
-  m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
 
   cec_command command;
   // TODO
@@ -659,86 +890,54 @@ void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode
   Transmit(command);
 }
 
-bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
-{
-  bool bError(false);
-  bool bTransmitSucceeded(false);
-  uint8_t iPacketsLeft(iLength / 4);
-
-  int64_t iNow = GetTimeMs();
-  int64_t iTargetTime = iNow + (uint64_t) iTimeout;
-
-  while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
-  {
-    CCECAdapterMessage msg;
-
-    if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
-    {
-      iNow = GetTimeMs();
-      continue;
-    }
-
-    if ((bError = msg.is_error()) == false)
-    {
-      m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
-
-      switch(msg.message())
-      {
-      case MSGCODE_COMMAND_ACCEPTED:
-        if (iPacketsLeft > 0)
-          iPacketsLeft--;
-        break;
-      case MSGCODE_TRANSMIT_SUCCEEDED:
-        bTransmitSucceeded = (iPacketsLeft == 0);
-        bError = !bTransmitSucceeded;
-        break;
-      default:
-        if (ParseMessage(msg))
-          m_commandBuffer.Push(m_currentframe);
-      }
-
-      iNow = GetTimeMs();
-    }
-  }
-
-  return bTransmitSucceeded && !bError;
-}
-
 bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
 {
-  bool bEom = false;
+  bool bEom(false);
+  bool bIsError(msg.IsError());
 
-  if (msg.empty())
+  if (msg.IsEmpty())
     return bEom;
 
-  switch(msg.message())
+  switch(msg.Message())
   {
   case MSGCODE_FRAME_START:
     {
       m_currentframe.Clear();
-      if (msg.size() >= 2)
+      if (msg.Size() >= 2)
+      {
+        m_currentframe.initiator   = msg.Initiator();
+        m_currentframe.destination = msg.Destination();
+        m_currentframe.ack         = msg.IsACK();
+        m_currentframe.eom         = msg.IsEOM();
+      }
+      if (m_currentframe.ack == 0x1)
       {
-        m_currentframe.initiator   = msg.initiator();
-        m_currentframe.destination = msg.destination();
-        m_currentframe.ack         = msg.ack();
-        m_currentframe.eom         = msg.eom();
+        m_lastInitiator = m_currentframe.initiator;
+        m_busDevices[m_lastInitiator]->GetHandler()->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
       }
     }
     break;
+  case MSGCODE_RECEIVE_FAILED:
+    {
+      if (m_lastInitiator != CECDEVICE_UNKNOWN)
+        bIsError = m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed();
+    }
+    break;
   case MSGCODE_FRAME_DATA:
     {
-      if (msg.size() >= 2)
+      if (msg.Size() >= 2)
       {
         m_currentframe.PushBack(msg[1]);
-        m_currentframe.eom = msg.eom();
+        m_currentframe.eom = msg.IsEOM();
       }
-      bEom = msg.eom();
+      bEom = msg.IsEOM();
     }
     break;
   default:
     break;
   }
 
+  CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
   return bEom;
 }
 
@@ -748,7 +947,7 @@ void CCECProcessor::ParseCommand(cec_command &command)
   dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
   for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
     dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
-  m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
+  CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
 
   if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
     m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
@@ -757,6 +956,7 @@ void CCECProcessor::ParseCommand(cec_command &command)
 cec_logical_addresses CCECProcessor::GetActiveDevices(void)
 {
   cec_logical_addresses addresses;
+  addresses.Clear();
   for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
   {
     if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
@@ -765,12 +965,12 @@ cec_logical_addresses CCECProcessor::GetActiveDevices(void)
   return addresses;
 }
 
-bool CCECProcessor::IsActiveDevice(cec_logical_address address)
+bool CCECProcessor::IsPresentDevice(cec_logical_address address)
 {
   return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
 }
 
-bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
+bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
 {
   for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
   {
@@ -808,40 +1008,428 @@ void CCECProcessor::AddKey(void)
   m_controller->AddKey();
 }
 
-void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
+bool CCECProcessor::SetAckMask(uint16_t iMask)
 {
-  m_controller->AddLog(level, strMessage);
+  return m_communication->SetAckMask(iMask);
 }
 
-bool CCECProcessor::SetAckMask(uint16_t iMask)
+bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
 {
-  bool bReturn(false);
-  CStdString strLog;
-  strLog.Format("setting ackmask to %2x", iMask);
-  m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
+  return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
+}
+
+bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
+{
+  return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
+}
+
+const char *CCECProcessor::ToString(const cec_device_type type)
+{
+  switch (type)
+  {
+  case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
+    return "audio system";
+  case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
+    return "playback device";
+  case CEC_DEVICE_TYPE_RECORDING_DEVICE:
+      return "recording device";
+  case CEC_DEVICE_TYPE_RESERVED:
+      return "reserved";
+  case CEC_DEVICE_TYPE_TUNER:
+      return "tuner";
+  case CEC_DEVICE_TYPE_TV:
+      return "TV";
+  default:
+    return "unknown";
+  }
+}
 
-  CCECAdapterMessage *output = new CCECAdapterMessage;
+const char *CCECProcessor::ToString(const cec_menu_state state)
+{
+  switch (state)
+  {
+  case CEC_MENU_STATE_ACTIVATED:
+    return "activated";
+  case CEC_MENU_STATE_DEACTIVATED:
+    return "deactivated";
+  default:
+    return "unknown";
+  }
+}
 
-  output->push_back(MSGSTART);
-  output->push_escaped(MSGCODE_SET_ACK_MASK);
-  output->push_escaped(iMask >> 8);
-  output->push_escaped((uint8_t)iMask);
-  output->push_back(MSGEND);
+const char *CCECProcessor::ToString(const cec_version version)
+{
+  switch (version)
+  {
+  case CEC_VERSION_1_2:
+    return "1.2";
+  case CEC_VERSION_1_2A:
+    return "1.2a";
+  case CEC_VERSION_1_3:
+    return "1.3";
+  case CEC_VERSION_1_3A:
+    return "1.3a";
+  case CEC_VERSION_1_4:
+    return "1.4";
+  default:
+    return "unknown";
+  }
+}
 
-  if ((bReturn = Transmit(output)) == false)
-    m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
+const char *CCECProcessor::ToString(const cec_power_status status)
+{
+  switch (status)
+  {
+  case CEC_POWER_STATUS_ON:
+    return "on";
+  case CEC_POWER_STATUS_STANDBY:
+    return "standby";
+  case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
+    return "in transition from on to standby";
+  case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
+    return "in transition from standby to on";
+  default:
+    return "unknown";
+  }
+}
 
-  delete output;
+const char *CCECProcessor::ToString(const cec_logical_address address)
+{
+  switch(address)
+  {
+  case CECDEVICE_AUDIOSYSTEM:
+    return "Audio";
+  case CECDEVICE_BROADCAST:
+    return "Broadcast";
+  case CECDEVICE_FREEUSE:
+    return "Free use";
+  case CECDEVICE_PLAYBACKDEVICE1:
+    return "Playback 1";
+  case CECDEVICE_PLAYBACKDEVICE2:
+    return "Playback 2";
+  case CECDEVICE_PLAYBACKDEVICE3:
+    return "Playback 3";
+  case CECDEVICE_RECORDINGDEVICE1:
+    return "Recorder 1";
+  case CECDEVICE_RECORDINGDEVICE2:
+    return "Recorder 2";
+  case CECDEVICE_RECORDINGDEVICE3:
+    return "Recorder 3";
+  case CECDEVICE_RESERVED1:
+    return "Reserved 1";
+  case CECDEVICE_RESERVED2:
+    return "Reserved 2";
+  case CECDEVICE_TUNER1:
+    return "Tuner 1";
+  case CECDEVICE_TUNER2:
+    return "Tuner 2";
+  case CECDEVICE_TUNER3:
+    return "Tuner 3";
+  case CECDEVICE_TUNER4:
+    return "Tuner 4";
+  case CECDEVICE_TV:
+    return "TV";
+  default:
+    return "unknown";
+  }
+}
 
-  return bReturn;
+const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
+{
+  switch (mode)
+  {
+  case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
+    return "skip forward wind";
+  case CEC_DECK_CONTROL_MODE_EJECT:
+    return "eject";
+  case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
+    return "reverse rewind";
+  case CEC_DECK_CONTROL_MODE_STOP:
+    return "stop";
+  default:
+    return "unknown";
+  }
+}
+
+const char *CCECProcessor::ToString(const cec_deck_info status)
+{
+  switch (status)
+  {
+  case CEC_DECK_INFO_PLAY:
+    return "play";
+  case CEC_DECK_INFO_RECORD:
+    return "record";
+  case CEC_DECK_INFO_PLAY_REVERSE:
+    return "play reverse";
+  case CEC_DECK_INFO_STILL:
+    return "still";
+  case CEC_DECK_INFO_SLOW:
+    return "slow";
+  case CEC_DECK_INFO_SLOW_REVERSE:
+    return "slow reverse";
+  case CEC_DECK_INFO_FAST_FORWARD:
+    return "fast forward";
+  case CEC_DECK_INFO_FAST_REVERSE:
+    return "fast reverse";
+  case CEC_DECK_INFO_NO_MEDIA:
+    return "no media";
+  case CEC_DECK_INFO_STOP:
+    return "stop";
+  case CEC_DECK_INFO_SKIP_FORWARD_WIND:
+    return "info skip forward wind";
+  case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
+    return "info skip reverse rewind";
+  case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
+    return "info index search forward";
+  case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
+    return "info index search reverse";
+  case CEC_DECK_INFO_OTHER_STATUS:
+    return "other";
+  default:
+    return "unknown";
+  }
+}
+
+const char *CCECProcessor::ToString(const cec_opcode opcode)
+{
+  switch (opcode)
+  {
+  case CEC_OPCODE_ACTIVE_SOURCE:
+    return "active source";
+  case CEC_OPCODE_IMAGE_VIEW_ON:
+    return "image view on";
+  case CEC_OPCODE_TEXT_VIEW_ON:
+    return "text view on";
+  case CEC_OPCODE_INACTIVE_SOURCE:
+    return "inactive source";
+  case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
+    return "request active source";
+  case CEC_OPCODE_ROUTING_CHANGE:
+    return "routing change";
+  case CEC_OPCODE_ROUTING_INFORMATION:
+    return "routing information";
+  case CEC_OPCODE_SET_STREAM_PATH:
+    return "set stream path";
+  case CEC_OPCODE_STANDBY:
+    return "standby";
+  case CEC_OPCODE_RECORD_OFF:
+    return "record off";
+  case CEC_OPCODE_RECORD_ON:
+    return "record on";
+  case CEC_OPCODE_RECORD_STATUS:
+    return "record status";
+  case CEC_OPCODE_RECORD_TV_SCREEN:
+    return "record tv screen";
+  case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
+    return "clear analogue timer";
+  case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
+    return "clear digital timer";
+  case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
+    return "clear external timer";
+  case CEC_OPCODE_SET_ANALOGUE_TIMER:
+    return "set analogue timer";
+  case CEC_OPCODE_SET_DIGITAL_TIMER:
+    return "set digital timer";
+  case CEC_OPCODE_SET_EXTERNAL_TIMER:
+    return "set external timer";
+  case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
+    return "set timer program title";
+  case CEC_OPCODE_TIMER_CLEARED_STATUS:
+    return "timer cleared status";
+  case CEC_OPCODE_TIMER_STATUS:
+    return "timer status";
+  case CEC_OPCODE_CEC_VERSION:
+    return "cec version";
+  case CEC_OPCODE_GET_CEC_VERSION:
+    return "get cec version";
+  case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
+    return "give physical address";
+  case CEC_OPCODE_GET_MENU_LANGUAGE:
+    return "get menu language";
+  case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
+    return "report physical address";
+  case CEC_OPCODE_SET_MENU_LANGUAGE:
+    return "set menu language";
+  case CEC_OPCODE_DECK_CONTROL:
+    return "deck control";
+  case CEC_OPCODE_DECK_STATUS:
+    return "deck status";
+  case CEC_OPCODE_GIVE_DECK_STATUS:
+    return "give deck status";
+  case CEC_OPCODE_PLAY:
+    return "play";
+  case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
+    return "give tuner status";
+  case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
+    return "select analogue service";
+  case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
+    return "set digital service";
+  case CEC_OPCODE_TUNER_DEVICE_STATUS:
+    return "tuner device status";
+  case CEC_OPCODE_TUNER_STEP_DECREMENT:
+    return "tuner step decrement";
+  case CEC_OPCODE_TUNER_STEP_INCREMENT:
+    return "tuner step increment";
+  case CEC_OPCODE_DEVICE_VENDOR_ID:
+    return "device vendor id";
+  case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
+    return "give device vendor id";
+  case CEC_OPCODE_VENDOR_COMMAND:
+    return "vendor command";
+  case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
+    return "vendor command with id";
+  case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
+    return "vendor remote button down";
+  case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
+    return "vendor remote button up";
+  case CEC_OPCODE_SET_OSD_STRING:
+    return "set osd string";
+  case CEC_OPCODE_GIVE_OSD_NAME:
+    return "give osd name";
+  case CEC_OPCODE_SET_OSD_NAME:
+    return "set osd name";
+  case CEC_OPCODE_MENU_REQUEST:
+    return "menu request";
+  case CEC_OPCODE_MENU_STATUS:
+    return "menu status";
+  case CEC_OPCODE_USER_CONTROL_PRESSED:
+    return "user control pressed";
+  case CEC_OPCODE_USER_CONTROL_RELEASE:
+    return "user control release";
+  case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
+    return "give device power status";
+  case CEC_OPCODE_REPORT_POWER_STATUS:
+    return "report power status";
+  case CEC_OPCODE_FEATURE_ABORT:
+    return "feature abort";
+  case CEC_OPCODE_ABORT:
+    return "abort";
+  case CEC_OPCODE_GIVE_AUDIO_STATUS:
+    return "give audio status";
+  case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
+    return "give audio mode status";
+  case CEC_OPCODE_REPORT_AUDIO_STATUS:
+    return "report audio status";
+  case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
+    return "set system audio mode";
+  case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
+    return "system audio mode request";
+  case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
+    return "system audio mode status";
+  case CEC_OPCODE_SET_AUDIO_RATE:
+    return "set audio rate";
+  default:
+    return "UNKNOWN";
+  }
+}
+
+const char *CCECProcessor::ToString(const cec_system_audio_status mode)
+{
+  switch(mode)
+  {
+  case CEC_SYSTEM_AUDIO_STATUS_ON:
+    return "on";
+  case CEC_SYSTEM_AUDIO_STATUS_OFF:
+    return "off";
+  default:
+    return "unknown";
+  }
+}
+
+const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
+{
+  // TODO this is a mask
+  return "TODO";
+}
+
+const char *CCECProcessor::ToString(const cec_vendor_id vendor)
+{
+  switch (vendor)
+  {
+  case CEC_VENDOR_SAMSUNG:
+    return "Samsung";
+  case CEC_VENDOR_LG:
+    return "LG";
+  case CEC_VENDOR_PANASONIC:
+    return "Panasonic";
+  case CEC_VENDOR_PIONEER:
+    return "Pioneer";
+  case CEC_VENDOR_ONKYO:
+    return "Onkyo";
+  case CEC_VENDOR_YAMAHA:
+    return "Yamaha";
+  case CEC_VENDOR_PHILIPS:
+    return "Philips";
+  case CEC_VENDOR_SONY:
+    return "Sony";
+  default:
+    return "Unknown";
+  }
+}
+
+void *CCECBusScan::Process(void)
+{
+  CCECBusDevice *device(NULL);
+  uint8_t iCounter(0);
+
+  while (!IsStopped())
+  {
+    if (++iCounter < 10)
+    {
+      Sleep(1000);
+      continue;
+    }
+    for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
+    {
+      device = m_processor->m_busDevices[iPtr];
+      WaitUntilIdle();
+      if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
+      {
+        WaitUntilIdle();
+        if (!IsStopped())
+          device->GetVendorId();
+
+        WaitUntilIdle();
+        if (!IsStopped())
+          device->GetPowerStatus(true);
+      }
+    }
+  }
+
+  return NULL;
+}
+
+void CCECBusScan::WaitUntilIdle(void)
+{
+  if (IsStopped())
+    return;
+
+  int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
+  while (iWaitTime > 0)
+  {
+    Sleep(iWaitTime);
+    iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
+  }
+}
+
+bool CCECProcessor::StartBootloader(void)
+{
+  return m_communication->StartBootloader();
+}
+
+bool CCECProcessor::PingAdapter(void)
+{
+  return m_communication->PingAdapter();
 }
 
-bool CCECProcessor::SendKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = false */)
+void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
 {
-  return m_busDevices[iDestination]->SendKeypress(key, bWait);
+  m_busDevices[initiator]->GetHandler()->HandlePoll(initiator, destination);
+  m_lastInitiator = initiator;
 }
 
-bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination, bool bWait /* = false */)
+bool CCECProcessor::HandleReceiveFailed(void)
 {
-  return m_busDevices[iDestination]->SendKeyRelease(bWait);
+  return m_lastInitiator != CECDEVICE_UNKNOWN &&
+      !m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed();
 }