Merge branch 'master' of github.com:Pulse-Eight/libcec
[deb_libcec.git] / src / lib / CECProcessor.cpp
index b07f2ddc72bbfcc6c61d85c440b814bc4707ca20..e1e03b868d1ee6925bf319fd26c6f2b29fb801a8 100644 (file)
@@ -152,8 +152,10 @@ bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */,
     }
 
     lock.Leave();
-    if (SetAckMask(m_logicalAddresses.AckMask()) &&
-        SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true))
+
+    m_busDevices[CECDEVICE_TV]->GetVendorId();
+
+    if (SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true))
     {
       m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
       m_busScan = new CCECBusScan(this);
@@ -260,8 +262,7 @@ void *CCECProcessor::Process(void)
   }
   else
   {
-    m_busDevices[CECDEVICE_TV]->GetVendorId();
-    m_busDevices[m_logicalAddresses.primary]->TransmitVendorID(CECDEVICE_TV, false);
+    SetAckMask(m_logicalAddresses.AckMask());
 
     CLockObject lock(&m_mutex);
     m_bStarted = true;
@@ -395,44 +396,54 @@ bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort,
 {
   bool bReturn(false);
 
-  CStdString strLog;
-  strLog.Format("setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
-  AddLog(CEC_LOG_DEBUG, strLog);
-
   m_iBaseDevice = iBaseDevice;
   m_iHDMIPort = iPort;
   if (!m_bStarted && !bForce)
     return true;
 
+  CStdString strLog;
+  strLog.Format("setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
+  AddLog(CEC_LOG_DEBUG, strLog);
+
   uint16_t iPhysicalAddress(0);
-  iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
-  uint16_t iPos = 0;
-  if (iPhysicalAddress == 0)
-    iPos = 0x1000;
-  else if (iPhysicalAddress % 0x1000 == 0)
-    iPos = 0x100;
-  else if (iPhysicalAddress % 0x100 == 0)
-    iPos = 0x10;
-  else if (iPhysicalAddress % 0x10 == 0)
-    iPos = 0x1;
-
-  while(!bReturn && iPos > 0)
+  if (iBaseDevice > CECDEVICE_TV)
+    iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
+
+  if (iPhysicalAddress == 0xffff)
   {
-    iPhysicalAddress += (uint16_t)(iPort * iPos);
-    strLog.Format("checking physical address %4x", iPhysicalAddress);
-    AddLog(CEC_LOG_DEBUG, strLog);
-    if (PhysicalAddressInUse(iPhysicalAddress))
-    {
-      strLog.Format("physical address %4x is in use", iPhysicalAddress);
-      AddLog(CEC_LOG_DEBUG, strLog);
-      iPos = (iPos == 1) ? 0 : iPos / 0x10;
-    }
-    else
+    SetPhysicalAddress((uint16_t)iPort * 0x1000);
+    bReturn = false;
+  }
+  else
+  {
+    uint16_t iPos = 0;
+    if (iPhysicalAddress == 0)
+      iPos = 0x1000;
+    else if (iPhysicalAddress % 0x1000 == 0)
+      iPos = 0x100;
+    else if (iPhysicalAddress % 0x100 == 0)
+      iPos = 0x10;
+    else if (iPhysicalAddress % 0x10 == 0)
+      iPos = 0x1;
+
+    while(!bReturn && iPos > 0)
     {
-      strLog.Format("physical address %4x is free", iPhysicalAddress);
+      iPhysicalAddress += (uint16_t)(iPort * iPos);
+      strLog.Format("checking physical address %4x", iPhysicalAddress);
       AddLog(CEC_LOG_DEBUG, strLog);
-      SetPhysicalAddress(iPhysicalAddress);
-      bReturn = true;
+      if (PhysicalAddressInUse(iPhysicalAddress))
+      {
+        strLog.Format("physical address %4x is in use", iPhysicalAddress);
+        AddLog(CEC_LOG_DEBUG, strLog);
+        iPos = (iPos == 1) ? 0 : iPos / 0x10;
+      }
+      else
+      {
+        strLog.Format("physical address %4x is free", iPhysicalAddress);
+        AddLog(CEC_LOG_DEBUG, strLog);
+        SetPhysicalAddress(iPhysicalAddress);
+        bReturn = true;
+      }
     }
   }
 
@@ -1324,7 +1335,7 @@ void *CCECBusScan::Process(void)
 
   while (!IsStopped())
   {
-    if (++iCounter < 30)
+    if (++iCounter < 10)
     {
       Sleep(1000);
       continue;
@@ -1354,11 +1365,11 @@ void CCECBusScan::WaitUntilIdle(void)
   if (IsStopped())
     return;
 
-  int64_t iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission());
+  int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
   while (iWaitTime > 0)
   {
     Sleep(iWaitTime);
-    iWaitTime = 3000 - (GetTimeMs() - m_processor->GetLastTransmission());
+    iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
   }
 }