added the type of adapter to libcec_configuration, and display the type in cec-client...
[deb_libcec.git] / src / lib / adapter / Pulse-Eight / USBCECAdapterCommands.cpp
index bbd15d4b0b230edf42b76e0042396f35489f6ffe..ab0fa392e12633c54a0c237861bbae481b1fbfcb 100644 (file)
@@ -53,7 +53,9 @@ CUSBCECAdapterCommands::CUSBCECAdapterCommands(CUSBCECAdapterCommunication *comm
     m_settingCecVersion(CEC_VERSION_UNKNOWN),
     m_iSettingLAMask(0),
     m_bNeedsWrite(false),
-    m_iBuildDate(CEC_FW_BUILD_UNKNOWN)
+    m_iBuildDate(CEC_FW_BUILD_UNKNOWN),
+    m_bControlledMode(false),
+    m_adapterType(P8_ADAPTERTYPE_UNKNOWN)
 {
   m_persistedConfiguration.Clear();
 }
@@ -131,6 +133,19 @@ bool CUSBCECAdapterCommands::RequestSettingCECVersion(void)
   return false;
 }
 
+p8_cec_adapter_type CUSBCECAdapterCommands::RequestAdapterType(void)
+{
+  if (m_adapterType == P8_ADAPTERTYPE_UNKNOWN)
+  {
+    LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting adapter type");
+
+    cec_datapacket response = RequestSetting(MSGCODE_GET_ADAPTER_TYPE);
+    if (response.size == 1)
+      m_adapterType = (p8_cec_adapter_type)response[0];
+  }
+  return m_adapterType;
+}
+
 uint32_t CUSBCECAdapterCommands::RequestBuildDate(void)
 {
   if (m_iBuildDate == CEC_FW_BUILD_UNKNOWN)
@@ -230,10 +245,13 @@ bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled)
   bool bReturn(false);
 
   /* check whether this value was changed */
-  if (m_bSettingAutoEnabled == enabled)
-    return bReturn;
+  {
+    CLockObject lock(m_mutex);
+    if (m_bSettingAutoEnabled == enabled)
+      return bReturn;
+    m_bNeedsWrite = true;
+  }
 
-  m_bNeedsWrite = true;
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "turning autonomous mode %s", enabled ? "on" : "off");
 
   CCECAdapterMessage params;
@@ -243,7 +261,10 @@ bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled)
   delete message;
 
   if (bReturn)
+  {
+    CLockObject lock(m_mutex);
     m_bSettingAutoEnabled = enabled;
+  }
 
   return bReturn;
 }
@@ -253,10 +274,13 @@ bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type)
   bool bReturn(false);
 
   /* check whether this value was changed */
-  if (m_persistedConfiguration.deviceTypes.types[0] == type)
-    return bReturn;
+  {
+    CLockObject lock(m_mutex);
+    if (m_persistedConfiguration.deviceTypes.types[0] == type)
+      return bReturn;
+    m_bNeedsWrite = true;
+  }
 
-  m_bNeedsWrite = true;
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the device type to %X (previous: %X)", (uint8_t)type, (uint8_t)m_persistedConfiguration.deviceTypes.types[0]);
 
   CCECAdapterMessage params;
@@ -265,6 +289,12 @@ bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type)
   bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
   delete message;
 
+  if (bReturn)
+  {
+    CLockObject lock(m_mutex);
+    m_persistedConfiguration.deviceTypes.types[0] = type;
+  }
+
   return bReturn;
 }
 
@@ -273,10 +303,13 @@ bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address
   bool bReturn(false);
 
   /* check whether this value was changed */
-  if (m_persistedConfiguration.logicalAddresses.primary == address)
-    return bReturn;
+  {
+    CLockObject lock(m_mutex);
+    if (m_persistedConfiguration.logicalAddresses.primary == address)
+      return bReturn;
+    m_bNeedsWrite = true;
+  }
 
-  m_bNeedsWrite = true;
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the default logical address to %X (previous: %X)", (uint8_t)address, (uint8_t)m_persistedConfiguration.logicalAddresses.primary);
 
   CCECAdapterMessage params;
@@ -286,7 +319,10 @@ bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address
   delete message;
 
   if (bReturn)
+  {
+    CLockObject lock(m_mutex);
     m_persistedConfiguration.logicalAddresses.primary = address;
+  }
 
   return bReturn;
 }
@@ -296,10 +332,13 @@ bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask)
   bool bReturn(false);
 
   /* check whether this value was changed */
-  if (m_iSettingLAMask == iMask)
-    return bReturn;
+  {
+    CLockObject lock(m_mutex);
+    if (m_iSettingLAMask == iMask)
+      return bReturn;
+    m_bNeedsWrite = true;
+  }
 
-  m_bNeedsWrite = true;
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the logical address mask to %2X (previous: %2X)", iMask, m_iSettingLAMask);
 
   CCECAdapterMessage params;
@@ -310,7 +349,10 @@ bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask)
   delete message;
 
   if (bReturn)
+  {
+    CLockObject lock(m_mutex);
     m_iSettingLAMask = iMask;
+  }
 
   return bReturn;
 }
@@ -320,10 +362,13 @@ bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
   bool bReturn(false);
 
   /* check whether this value was changed */
-  if (m_persistedConfiguration.iPhysicalAddress == iPhysicalAddress)
-    return bReturn;
+  {
+    CLockObject lock(m_mutex);
+    if (m_persistedConfiguration.iPhysicalAddress == iPhysicalAddress)
+      return bReturn;
+    m_bNeedsWrite = true;
+  }
 
-  m_bNeedsWrite = true;
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress, m_persistedConfiguration.iPhysicalAddress);
 
   CCECAdapterMessage params;
@@ -334,7 +379,10 @@ bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
   delete message;
 
   if (bReturn)
+  {
+    CLockObject lock(m_mutex);
     m_persistedConfiguration.iPhysicalAddress = iPhysicalAddress;
+  }
 
   return bReturn;
 }
@@ -344,10 +392,13 @@ bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version)
   bool bReturn(false);
 
   /* check whether this value was changed */
-  if (m_settingCecVersion == version)
-    return bReturn;
+  {
+    CLockObject lock(m_mutex);
+    if (m_settingCecVersion == version)
+      return bReturn;
+    m_bNeedsWrite = true;
+  }
 
-  m_bNeedsWrite = true;
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the CEC version to %s (previous: %s)", ToString(version), ToString(m_settingCecVersion));
 
   CCECAdapterMessage params;
@@ -357,7 +408,10 @@ bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version)
   delete message;
 
   if (bReturn)
+  {
+    CLockObject lock(m_mutex);
     m_settingCecVersion = version;
+  }
 
   return bReturn;
 }
@@ -387,16 +441,26 @@ bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName)
 
 bool CUSBCECAdapterCommands::WriteEEPROM(void)
 {
-  if (!m_bNeedsWrite)
-    return true;
+  {
+    CLockObject lock(m_mutex);
+    if (!m_bNeedsWrite)
+      return true;
+  }
 
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "writing settings in the EEPROM");
 
   CCECAdapterMessage params;
   CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_WRITE_EEPROM, params);
-  m_bNeedsWrite = !(message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED);
+  bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
   delete message;
-  return m_bNeedsWrite;
+
+  if (bReturn)
+  {
+    CLockObject lock(m_mutex);
+    m_bNeedsWrite = false;
+  }
+
+  return bReturn;
 }
 
 bool CUSBCECAdapterCommands::PersistConfiguration(const libcec_configuration &configuration)
@@ -526,6 +590,12 @@ bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout)
 
 bool CUSBCECAdapterCommands::SetControlledMode(bool controlled)
 {
+  {
+    CLockObject lock(m_mutex);
+    if (m_bControlledMode == controlled)
+      return true;
+  }
+
   LIB_CEC->AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off");
 
   CCECAdapterMessage params;
@@ -533,5 +603,12 @@ bool CUSBCECAdapterCommands::SetControlledMode(bool controlled)
   CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_CONTROLLED, params);
   bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
   delete message;
+
+  if (bReturn)
+  {
+    CLockObject lock(m_mutex);
+    m_bControlledMode = controlled;
+  }
+
   return bReturn;
 }