cec: check whether the adapter responds when connecting to it and report the firmware...
authorLars Op den Kamp <lars@opdenkamp.eu>
Wed, 25 Jan 2012 10:41:26 +0000 (11:41 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Wed, 25 Jan 2012 10:41:26 +0000 (11:41 +0100)
include/cectypes.h
src/lib/CECProcessor.cpp
src/lib/adapter/AdapterCommunication.cpp
src/lib/adapter/AdapterCommunication.h

index ba21200fece893f93d0c13cde4be1f56d6ca5828..86be2a258e2969fe80e7ff788933e7810a2a8f72 100644 (file)
@@ -71,6 +71,7 @@ namespace CEC {
 #define ESCOFFSET                    3
 #define CEC_BUTTON_TIMEOUT           500
 #define CEC_POWER_STATE_REFRESH_TIME 30000
+#define CEC_FW_VERSION_UNKNOWN       0xFFFF
 
 #define CEC_DEFAULT_TRANSMIT_TIMEOUT 1000
 #define CEC_DEFAULT_TRANSMIT_WAIT    2000
index cb4fe8101e31adb144515dab7009830ea71f3c0c..659eae3ecf88376ef158bd01ef28ec13b89ce25d 100644 (file)
@@ -149,6 +149,18 @@ bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint
   if ((bReturn = m_communication->Open(strPort, iBaudRate, iTimeoutMs)) == false)
     m_controller->AddLog(CEC_LOG_ERROR, "could not open a connection");
 
+  /* try to ping the adapter */
+  if ((bReturn = m_communication->PingAdapter()) == false)
+    m_controller->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");
+
+  CStdString strLog;
+  strLog.Format("CEC Adapter firmware version: %d", iFirmwareVersion);
+  m_controller->AddLog(CEC_LOG_NOTICE, strLog);
+
   return bReturn;
 }
 
index 44b24e1a7c28e9672f3926fd21aea683355c5f73..cc8507fe849290bfb60fade2a1d0aa528031b7a8 100644 (file)
@@ -43,7 +43,8 @@ using namespace PLATFORM;
 CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) :
     m_port(NULL),
     m_processor(processor),
-    m_iLineTimeout(0)
+    m_iLineTimeout(0),
+    m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN)
 {
   m_port = new PLATFORM::CSerialPort;
 }
@@ -269,6 +270,39 @@ bool CAdapterCommunication::PingAdapter(void)
   return bReturn;
 }
 
+uint16_t CAdapterCommunication::GetFirmwareVersion(void)
+{
+  uint16_t iReturn(m_iFirmwareVersion);
+  if (!IsRunning())
+    return iReturn;
+
+  if (iReturn == CEC_FW_VERSION_UNKNOWN)
+  {
+    m_processor->AddLog(CEC_LOG_DEBUG, "requesting the firmware version");
+    CCECAdapterMessage *output = new CCECAdapterMessage;
+
+    output->PushBack(MSGSTART);
+    output->PushEscaped(MSGCODE_FIRMWARE_VERSION);
+    output->PushBack(MSGEND);
+    output->isTransmission = false;
+    output->expectControllerAck = false;
+
+    SendMessageToAdapter(output);
+    delete output;
+
+    CCECAdapterMessage input;
+    if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
+      m_processor->AddLog(CEC_LOG_ERROR, "no or invalid firmware version");
+    else
+    {
+      m_iFirmwareVersion = (input[1] << 8 | input[2]);
+      iReturn = m_iFirmwareVersion;
+    }
+  }
+
+  return iReturn;
+}
+
 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
 {
   bool bReturn(m_iLineTimeout != iTimeout);
@@ -403,6 +437,7 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
   if (!m_port)
     return false;
 
+  CLockObject lock(m_mutex);
   iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
   if (iBytesRead < 0 || iBytesRead > 256)
   {
@@ -419,6 +454,7 @@ bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
 
 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
 {
+  CLockObject adapterLock(m_mutex);
   CLockObject lock(msg->mutex);
   if (m_port->Write(msg->packet.data, msg->Size()) != (int32_t) msg->Size())
   {
index bdc3de30f3f543e8bfd30d46528ef1a1d01e973f..3d74546d5e5b970a0ab93204fdea01591ac4f77e 100644 (file)
@@ -53,7 +53,6 @@ namespace CEC
     bool Open(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
     bool Read(CCECAdapterMessage &msg, uint32_t iTimeout = 1000);
     bool Write(CCECAdapterMessage *data);
-    bool PingAdapter(void);
     void Close(void);
     bool IsOpen(void);
     std::string GetError(void) const;
@@ -63,6 +62,8 @@ namespace CEC
     bool SetLineTimeout(uint8_t iTimeout);
     bool StartBootloader(void);
     bool SetAckMask(uint16_t iMask);
+    bool PingAdapter(void);
+    uint16_t GetFirmwareVersion(void);
 
     bool WaitForTransmitSucceeded(CCECAdapterMessage *message);
 
@@ -79,5 +80,6 @@ namespace CEC
     PLATFORM::CMutex                             m_mutex;
     PLATFORM::CCondition                         m_rcvCondition;
     uint8_t                                      m_iLineTimeout;
+    uint16_t                                     m_iFirmwareVersion;
   };
 };