#define CEC_ADAPTER_PING_TIMEOUT 15000
-CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate /* = 38400 */) :
+CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */) :
IAdapterCommunication(callback),
m_port(NULL),
m_iLineTimeout(0),
m_commands(NULL),
m_adapterMessageQueue(NULL)
{
- for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
+ for (unsigned int iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
m_bWaitingForAck[iPtr] = false;
m_port = new CSerialPort(strPort, iBaudRate);
}
delete m_port;
}
-bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = 10000 */, bool bSkipChecks /* = false */, bool bStartListening /* = true */)
+bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool bSkipChecks /* = false */, bool bStartListening /* = true */)
{
bool bConnectionOpened(false);
{
m_commands = new CUSBCECAdapterCommands(this);
if (!m_adapterMessageQueue)
+ {
m_adapterMessageQueue = new CCECAdapterMessageQueue(this);
+ m_adapterMessageQueue->CreateThread();
+ }
/* try to open the connection */
CStdString strError;
SetControlledMode(false);
}
+ m_adapterMessageQueue->Clear();
+
/* stop and delete the ping thread */
if (m_pingThread)
m_pingThread->StopThread(0);
/* send the message */
bRetry = (!m_adapterMessageQueue->Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0;
- if (output->state == ADAPTER_MESSAGE_STATE_ERROR)
- Close();
- else if (bRetry)
+ if (bRetry)
Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
retVal = output->state;
}
}
-void CUSBCECAdapterCommunication::ClearInputBytes(uint32_t iTimeout /* = 1000 */)
+void CUSBCECAdapterCommunication::ClearInputBytes(uint32_t iTimeout /* = CEC_CLEAR_INPUT_DEFAULT_WAIT */)
{
CTimeout timeout(iTimeout);
uint8_t buff[1024];
{
CLibCEC::AddLog(CEC_LOG_DEBUG, "error writing command '%s' to serial port '%s': %s", CCECAdapterMessage::ToString(message->Message()), m_port->GetName().c_str(), m_port->GetError().c_str());
message->state = ADAPTER_MESSAGE_STATE_ERROR;
+ Close();
return false;
}
CLockObject lock(m_mutex);
if (!m_port || !m_port->IsOpen())
return false;
+
iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
+
+ if (m_port->GetErrorNumber())
+ {
+ CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
+ m_port->Close();
+ return false;
+ }
}
if (iBytesRead < 0 || iBytesRead > 256)
- {
- CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
- Close();
return false;
- }
else if (iBytesRead > 0)
{
/* add the data to the current frame */
}
else
{
- if (!bIsRetry && output->Reply() == MSGCODE_COMMAND_REJECTED && msgCode != MSGCODE_SET_CONTROLLED)
+ if (!bIsRetry && output->Reply() == MSGCODE_COMMAND_REJECTED && msgCode != MSGCODE_SET_CONTROLLED &&
+ msgCode != MSGCODE_GET_BUILDDATE /* same messagecode value had a different meaning in older fw builds */)
{
/* if the controller reported that the command was rejected, and we didn't send the command
to set controlled mode, then the controller probably switched to auto mode. set controlled
return output;
}
-bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */)
+bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
{
bool bReturn(false);
CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
else
bReturn = true;
+ /* try to read the build date */
+ m_commands->RequestBuildDate();
+
SetInitialised(bReturn);
return bReturn;
}
bool CUSBCECAdapterCommunication::StartBootloader(void)
{
- return m_port->IsOpen() ? m_commands->StartBootloader() : false;
+ if (m_port->IsOpen() && m_commands->StartBootloader())
+ {
+ Close();
+ return true;
+ }
+ return false;
}
bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
return m_commands->GetFirmwareVersion();
}
+uint32_t CUSBCECAdapterCommunication::GetFirmwareBuildDate(void)
+{
+ return m_commands->RequestBuildDate();
+}
+
bool CUSBCECAdapterCommunication::PersistConfiguration(libcec_configuration *configuration)
{
return m_port->IsOpen() ? m_commands->PersistConfiguration(configuration) : false;
{
if (!m_com->PingAdapter())
{
- /* sleep 1 second and retry */
- Sleep(1000);
+ /* sleep and retry */
+ Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
++iFailedCounter;
}
else