#include "USBCECAdapterMessageQueue.h"
#include "../platform/sockets/serialport.h"
#include "../platform/util/timeutils.h"
+#include "../platform/util/util.h"
#include "../LibCEC.h"
#include "../CECProcessor.h"
CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void)
{
Close();
- delete m_commands;
- delete m_adapterMessageQueue;
- delete m_port;
+ DELETE_AND_NULL(m_commands);
+ DELETE_AND_NULL(m_adapterMessageQueue);
+ DELETE_AND_NULL(m_port);
}
bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool bSkipChecks /* = false */, bool bStartListening /* = true */)
if (!bConnectionOpened)
{
LIB_CEC->AddLog(CEC_LOG_ERROR, strError);
+
+ if (m_port->GetErrorNumber() == EACCES)
+ {
+ libcec_parameter param;
+ param.paramType = CEC_PARAMETER_TYPE_STRING;
+ param.paramData = (void*)"No permission to open the device";
+ LIB_CEC->Alert(CEC_ALERT_PERMISSION_ERROR, param);
+ }
+ else if (m_port->GetErrorNumber() == EBUSY)
+ {
+ libcec_parameter param;
+ param.paramType = CEC_PARAMETER_TYPE_STRING;
+ param.paramData = (void*)"The serial port is busy. Only one program can access the device directly.";
+ LIB_CEC->Alert(CEC_ALERT_PORT_BUSY, param);
+ }
return false;
}
m_adapterMessageQueue->Clear();
/* stop and delete the ping thread */
- if (m_pingThread)
- m_pingThread->StopThread(0);
- delete m_pingThread;
- m_pingThread = NULL;
+ DELETE_AND_NULL(m_pingThread);
/* close and delete the com port connection */
if (m_port)
{
LIB_CEC->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();
+ // this will trigger an alert in the reader thread
+ m_port->Close();
return false;
}
CCECAdapterMessage *CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage ¶ms, bool bIsRetry /* = false */)
{
- if (!m_port->IsOpen() || !m_adapterMessageQueue)
+ if (!IsOpen() || !m_adapterMessageQueue)
return NULL;
/* create the adapter message for this command */
/* write the command */
if (!m_adapterMessageQueue->Write(output))
{
+ // this will trigger an alert in the reader thread
if (output->state == ADAPTER_MESSAGE_STATE_ERROR)
- {
- libcec_parameter param;
- param.paramData = NULL; param.paramType = CEC_PARAMETER_TYPE_UNKOWN;
- LIB_CEC->Alert(CEC_ALERT_CONNECTION_LOST, param);
-
- Close();
- }
+ m_port->Close();
return output;
}
else
{
if (m_port->IsOpen() && m_commands->StartBootloader())
{
- Close();
+ m_port->Close();
return true;
}
return false;