changed the year in copyright notices to 2011-2012
[deb_libcec.git] / src / lib / adapter / AdapterCommunication.cpp
index 77c90326377b4832d5b40ca0c80f0a8f9fb0a1cb..cd112c21e0f2263175a33d098d7c862610998c3f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
@@ -34,7 +34,8 @@
 
 #include "AdapterMessage.h"
 #include "../CECProcessor.h"
-#include "../platform/serialport/serialport.h"
+#include "../platform/sockets/serialport.h"
+#include "../platform/util/timeutils.h"
 #include "../LibCEC.h"
 
 using namespace std;
@@ -98,11 +99,15 @@ bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38
     return false;
   }
 
-  CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened");
+  CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
 
   //clear any input bytes
-  uint8_t buff[1];
-  while (m_port->Read(buff, 1, 5) == 1) {}
+  uint8_t buff[1024];
+  while (m_port->Read(buff, 1024, 100) > 0)
+  {
+    CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
+    Sleep(250);
+  }
 
   if (CreateThread())
   {
@@ -153,10 +158,9 @@ bool CAdapterCommunication::Write(CCECAdapterMessage *data)
   {
     CLibCEC::AddLog(CEC_LOG_ERROR, "command was not sent");
   }
-
-  if (data->expectControllerAck)
+  else if (data->expectControllerAck)
   {
-    bReturn = WaitForTransmitSucceeded(data);
+    bReturn = WaitForAck(*data);
     if (bReturn)
     {
       if (data->isTransmission)
@@ -231,9 +235,11 @@ bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
   return bGotFullMessage;
 }
 
-std::string CAdapterCommunication::GetError(void) const
+CStdString CAdapterCommunication::GetError(void) const
 {
-  return m_port->GetError();
+  CStdString strError;
+  strError = m_port->GetError();
+  return strError;
 }
 
 bool CAdapterCommunication::StartBootloader(void)
@@ -301,7 +307,7 @@ uint16_t CAdapterCommunication::GetFirmwareVersion(void)
 
     CCECAdapterMessage input;
     if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
-      CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version");
+      CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version (size = %d, message = %d)", input.Size(), input.Message());
     else
     {
       m_iFirmwareVersion = (input[1] << 8 | input[2]);
@@ -362,20 +368,20 @@ bool CAdapterCommunication::IsOpen(void)
   return !IsStopped() && m_port->IsOpen() && IsRunning();
 }
 
-bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message)
+bool CAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
 {
   bool bError(false);
   bool bTransmitSucceeded(false);
-  uint8_t iPacketsLeft(message->Size() / 4);
+  uint8_t iPacketsLeft(message.Size() / 4);
 
   int64_t iNow = GetTimeMs();
-  int64_t iTargetTime = iNow + message->transmit_timeout;
+  int64_t iTargetTime = iNow + message.transmit_timeout;
 
-  while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime))
+  while (!bTransmitSucceeded && !bError && (message.transmit_timeout == 0 || iNow < iTargetTime))
   {
     CCECAdapterMessage msg;
     int32_t iWait = (int32_t)(iTargetTime - iNow);
-    if (iWait <= 5 || message->transmit_timeout <= 5)
+    if (iWait <= 5 || message.transmit_timeout <= 5)
       iWait = CEC_DEFAULT_TRANSMIT_WAIT;
 
     if (!Read(msg, iWait))
@@ -401,7 +407,7 @@ bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message
     bError = msg.IsError();
     if (bError)
     {
-      message->reply = msg.Message();
+      message.reply = msg.Message();
       CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
     }
     else
@@ -412,14 +418,14 @@ bool CAdapterCommunication::WaitForTransmitSucceeded(CCECAdapterMessage *message
         CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
         if (iPacketsLeft > 0)
           iPacketsLeft--;
-        if (!message->isTransmission && iPacketsLeft == 0)
+        if (!message.isTransmission && iPacketsLeft == 0)
           bTransmitSucceeded = true;
         break;
       case MSGCODE_TRANSMIT_SUCCEEDED:
         CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
         bTransmitSucceeded = (iPacketsLeft == 0);
         bError = !bTransmitSucceeded;
-        message->reply = MSGCODE_TRANSMIT_SUCCEEDED;
+        message.reply = MSGCODE_TRANSMIT_SUCCEEDED;
         break;
       default:
         // ignore other data while waiting