cec: only wait for multiple 'command accepted' response when sending a transmit comma...
[deb_libcec.git] / src / lib / adapter / USBCECAdapterCommunication.cpp
index d2d4e6de8663c6e615938a68ed9f64b2e189383a..20f480b09b97310728c0f1322a9757b57bcfc9a9 100644 (file)
@@ -571,13 +571,20 @@ bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask, bool bWrite
 
 bool CUSBCECAdapterCommunication::PersistConfiguration(libcec_configuration *configuration)
 {
-  return SetAutoEnabled(true) &&
-      //SetDefaultLogicalAddress() TODO
-      //SetLogicalAddressMask() TODO
-      SetPhysicalAddress(configuration->iPhysicalAddress) &&
-      SetCECVersion(CEC_VERSION_1_3A) &&
-      SetOSDName(configuration->strDeviceName) &&
-      WriteEEPROM();
+  if (m_iFirmwareVersion < 2)
+    return false;
+
+  bool bReturn(true);
+  bReturn &= SetAutoEnabled(true);
+  bReturn &= SetDeviceType(CLibCEC::GetType(configuration->logicalAddresses.primary));
+  bReturn &= SetDefaultLogicalAddress(configuration->logicalAddresses.primary);
+  bReturn &= SetLogicalAddressMask(configuration->logicalAddresses.AckMask());
+  bReturn &= SetPhysicalAddress(configuration->iPhysicalAddress);
+  bReturn &= SetCECVersion(CEC_VERSION_1_3A);
+  bReturn &= SetOSDName(configuration->strDeviceName);
+  if (bReturn)
+    bReturn = WriteEEPROM();
+  return bReturn;
 }
 
 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
@@ -630,6 +637,31 @@ bool CUSBCECAdapterCommunication::SetAutoEnabled(bool enabled)
   return true;
 }
 
+bool CUSBCECAdapterCommunication::SetDeviceType(cec_device_type type)
+{
+  CLockObject lock(m_mutex);
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "setting the device type to %1X", (uint8_t)type);
+
+  CCECAdapterMessage *output = new CCECAdapterMessage;
+
+  output->PushBack(MSGSTART);
+  output->PushEscaped(MSGCODE_SET_DEVICE_TYPE);
+  output->PushEscaped((uint8_t)type);
+  output->PushBack(MSGEND);
+  output->isTransmission = false;
+
+  SendMessageToAdapter(output);
+  bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
+  delete output;
+  if (!bWriteOk)
+  {
+    CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the device type");
+    return false;
+  }
+
+  return true;
+}
+
 bool CUSBCECAdapterCommunication::SetDefaultLogicalAddress(cec_logical_address address)
 {
   CLockObject lock(m_mutex);
@@ -791,7 +823,7 @@ bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
 {
   bool bError(false);
   bool bTransmitSucceeded(false);
-  uint8_t iPacketsLeft(message.Size() / 4);
+  uint8_t iPacketsLeft(message.isTransmission ? message.Size() / 4 : 1);
 
   int64_t iNow = GetTimeMs();
   int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout);