added: set controlled mode on after opening a connection to the adapter
authorBob van Loosen <bob.loosen@gmail.com>
Wed, 1 Feb 2012 23:55:40 +0000 (00:55 +0100)
committerBob van Loosen <bob.loosen@gmail.com>
Wed, 1 Feb 2012 23:58:01 +0000 (00:58 +0100)
src/lib/CECProcessor.cpp
src/lib/adapter/AdapterCommunication.h
src/lib/adapter/USBCECAdapterCommunication.cpp
src/lib/adapter/USBCECAdapterCommunication.h

index 57704bdba531e571f8db1babbede3680ef167834..7041b78f79acf82075b29d0eff9155e24448c556 100644 (file)
@@ -149,8 +149,8 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
 
   uint64_t iNow = GetTimeMs();
   uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
-  unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0);
-  bool bConnected(false), bPinged(false);
+  unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0), iControlledTry(0);
+  bool bConnected(false), bPinged(false), bControlled(false);
 
   /* open a new connection */
   while (iNow < iTarget && (bConnected = m_communication->Open(iTimeoutMs)) == false)
@@ -177,6 +177,17 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
     iNow = GetTimeMs();
   }
 
+  if (iFirmwareVersion >= 2)
+  {
+    /* try to set controlled mode */
+    while (bConnected && iNow < iTarget && (bControlled = m_communication->SetControlledMode(true)) == false)
+    {
+      CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry);
+      Sleep(500);
+      iNow = GetTimeMs();
+    }
+  }
+
   if ((bReturn = iFirmwareVersion != CEC_FW_VERSION_UNKNOWN) == true)
     CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d", iFirmwareVersion);
 
index cec8501a0c3e22a051fee4e3709dd758281577bf..36a9bb1d434356bd304afc86119f35347e6af0c4 100644 (file)
@@ -119,5 +119,10 @@ namespace CEC
      * @return The firmware version of this CEC adapter.
      */
     virtual uint16_t GetFirmwareVersion(void) = 0;
+
+    /*!
+     * @return True when the control mode has been set, false otherwise.
+     */
+    virtual bool SetControlledMode(bool controlled) = 0;
   };
 };
index 0134751d31b2b05f8eb4f88f3320ff9b09fba686..e2b03862d437cf75da03dda403a94e3da4e8f600 100644 (file)
@@ -432,6 +432,29 @@ bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
   return bReturn;
 }
 
+
+bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
+{
+  bool bReturn(false);
+  CStdString strLog;
+  strLog.Format("turning controlled mode %s", controlled ? "on" : "off");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, strLog.c_str());
+
+  CCECAdapterMessage *output = new CCECAdapterMessage;
+
+  output->PushBack(MSGSTART);
+  output->PushEscaped(MSGCODE_SET_CONTROLLED);
+  output->PushEscaped(controlled);
+  output->PushBack(MSGEND);
+  output->isTransmission = false;
+
+  if ((bReturn = Write(output)) == false)
+    CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode");
+  delete output;
+
+  return bReturn;
+}
+
 bool CUSBCECAdapterCommunication::IsOpen(void)
 {
   return !IsStopped() && m_port->IsOpen() && IsRunning();
index 7aa03507d57b91734887dc3d72dfdc7b2086506a..8cada4dbe0fd1259b13bf68cf6044f7a97958c61 100644 (file)
@@ -65,6 +65,7 @@ namespace CEC
     virtual bool SetAckMask(uint16_t iMask);
     virtual bool PingAdapter(void);
     virtual uint16_t GetFirmwareVersion(void);
+    virtual bool SetControlledMode(bool controlled);
 
     void *Process(void);
   private: