+ ResetMessageQueue();
+
+ /* try to open the connection */
+ CStdString strError;
+ CTimeout timeout(iTimeoutMs);
+ while (!bConnectionOpened && timeout.TimeLeft() > 0)
+ {
+ if ((bConnectionOpened = m_port->Open(timeout.TimeLeft())) == false)
+ {
+ strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str());
+ Sleep(250);
+ }
+ /* and retry every 250ms until the timeout passed */
+ }
+
+ /* return false when we couldn't connect */
+ 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;
+ }
+
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
+ ClearInputBytes();