2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
34 #include "USBCECAdapterCommunication.h"
36 #include "USBCECAdapterCommands.h"
37 #include "USBCECAdapterMessageQueue.h"
38 #include "USBCECAdapterMessage.h"
39 #include "lib/platform/sockets/serialport.h"
40 #include "lib/platform/util/timeutils.h"
41 #include "lib/platform/util/util.h"
42 #include "lib/platform/util/edid.h"
43 #include "lib/platform/adl/adl-edid.h"
44 #include "lib/platform/nvidia/nv-edid.h"
45 #include "lib/LibCEC.h"
46 #include "lib/CECProcessor.h"
50 using namespace PLATFORM
;
52 #define CEC_ADAPTER_PING_TIMEOUT 15000
53 #define CEC_ADAPTER_EEPROM_WRITE_INTERVAL 30000
54 #define CEC_ADAPTER_EEPROM_WRITE_RETRY 5000
57 #define CEC_LATEST_ADAPTER_FW_VERSION 2
58 // firmware date Thu Apr 26 20:14:49 2012 +0000
59 #define CEC_LATEST_ADAPTER_FW_DATE 0x5009F0A3
61 #define LIB_CEC m_callback->GetLib()
63 CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCallback
*callback
, const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */) :
64 IAdapterCommunication(callback
),
67 m_lastPollDestination(CECDEVICE_UNKNOWN
),
68 m_bInitialised(false),
71 m_adapterMessageQueue(NULL
),
72 m_iLastEepromWrite(0),
73 m_iScheduleEepromWrite(0)
75 m_logicalAddresses
.Clear();
76 for (unsigned int iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
77 m_bWaitingForAck
[iPtr
] = false;
78 m_port
= new CSerialPort(strPort
, iBaudRate
);
79 m_commands
= new CUSBCECAdapterCommands(this);
82 CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void)
85 DELETE_AND_NULL(m_commands
);
86 DELETE_AND_NULL(m_adapterMessageQueue
);
87 DELETE_AND_NULL(m_port
);
90 void CUSBCECAdapterCommunication::ResetMessageQueue(void)
92 DELETE_AND_NULL(m_adapterMessageQueue
);
93 m_adapterMessageQueue
= new CCECAdapterMessageQueue(this);
94 m_adapterMessageQueue
->CreateThread();
97 bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool bSkipChecks
/* = false */, bool bStartListening
/* = true */)
99 bool bConnectionOpened(false);
101 CLockObject
lock(m_mutex
);
103 /* we need the port settings here */
106 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "port is NULL");
107 return bConnectionOpened
;
110 /* return true when the port is already open */
113 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "port is already open");
119 /* try to open the connection */
121 CTimeout
timeout(iTimeoutMs
);
122 while (!bConnectionOpened
&& timeout
.TimeLeft() > 0)
124 if ((bConnectionOpened
= m_port
->Open(timeout
.TimeLeft())) == false)
126 strError
.Format("error opening serial port '%s': %s", m_port
->GetName().c_str(), m_port
->GetError().c_str());
129 /* and retry every 250ms until the timeout passed */
132 /* return false when we couldn't connect */
133 if (!bConnectionOpened
)
135 LIB_CEC
->AddLog(CEC_LOG_ERROR
, strError
);
137 if (m_port
->GetErrorNumber() == EACCES
)
139 libcec_parameter param
;
140 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
141 param
.paramData
= (void*)"No permission to open the device";
142 LIB_CEC
->Alert(CEC_ALERT_PERMISSION_ERROR
, param
);
144 else if (m_port
->GetErrorNumber() == EBUSY
)
146 libcec_parameter param
;
147 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
148 param
.paramData
= (void*)"The serial port is busy. Only one program can access the device directly.";
149 LIB_CEC
->Alert(CEC_ALERT_PORT_BUSY
, param
);
154 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
158 // always start by setting the ackmask to 0, to clear previous values
159 cec_logical_addresses addresses
; addresses
.Clear();
160 SetLogicalAddresses(addresses
);
164 bConnectionOpened
= false;
165 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
167 else if (!bSkipChecks
&& !CheckAdapter())
169 bConnectionOpened
= false;
170 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "the adapter failed to pass basic checks");
172 else if (bStartListening
)
174 /* start a ping thread, that will ping the adapter every 15 seconds
175 if it doesn't receive any ping for 30 seconds, it'll switch to auto mode */
176 m_pingThread
= new CAdapterPingThread(this, CEC_ADAPTER_PING_TIMEOUT
);
177 if (m_pingThread
->CreateThread())
179 bConnectionOpened
= true;
183 bConnectionOpened
= false;
184 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "could not create a ping thread");
188 if (!bConnectionOpened
|| !bStartListening
)
191 return bConnectionOpened
;
194 void CUSBCECAdapterCommunication::Close(void)
196 /* stop the reader thread */
199 CLockObject
lock(m_mutex
);
201 /* set the ackmask to 0 before closing the connection */
202 if (IsOpen() && m_port
->GetErrorNumber() == 0)
204 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - closing the connection", __FUNCTION__
);
205 cec_logical_addresses addresses
; addresses
.Clear();
206 SetLogicalAddresses(addresses
);
207 if (m_commands
->GetFirmwareVersion() >= 2)
208 SetControlledMode(false);
211 m_adapterMessageQueue
->Clear();
213 /* stop and delete the ping thread */
214 DELETE_AND_NULL(m_pingThread
);
216 /* close and delete the com port connection */
221 cec_adapter_message_state
CUSBCECAdapterCommunication::Write(const cec_command
&data
, bool &bRetry
, uint8_t iLineTimeout
, bool UNUSED(bIsReply
))
223 cec_adapter_message_state
retVal(ADAPTER_MESSAGE_STATE_UNKNOWN
);
227 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
, iLineTimeout
);
229 /* mark as waiting for an ack from the destination */
230 MarkAsWaiting(data
.destination
);
232 /* send the message */
233 bRetry
= (!m_adapterMessageQueue
->Write(output
) || output
->NeedsRetry()) && output
->transmit_timeout
> 0;
235 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
236 retVal
= output
->state
;
242 void *CUSBCECAdapterCommunication::Process(void)
244 CCECAdapterMessage msg
;
245 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
247 bool bWriteEeprom(false);
250 /* read from the serial port */
251 if (!ReadFromDevice(50, 5))
253 libcec_parameter param
;
254 param
.paramData
= NULL
; param
.paramType
= CEC_PARAMETER_TYPE_UNKOWN
;
255 LIB_CEC
->Alert(CEC_ALERT_CONNECTION_LOST
, param
);
260 // check if we need to do another eeprom write
262 CLockObject
lock(m_mutex
);
263 int64_t iNow
= GetTimeMs();
264 if (m_iScheduleEepromWrite
> 0 &&
265 m_iScheduleEepromWrite
>= iNow
)
267 m_iScheduleEepromWrite
= 0;
268 m_iLastEepromWrite
= iNow
;
275 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "updating the eeprom (scheduled)");
276 bWriteEeprom
= false;
277 if (!m_commands
->WriteEEPROM())
279 // failed, retry later
280 CLockObject
lock(m_mutex
);
281 m_iScheduleEepromWrite
= GetTimeMs() + CEC_ADAPTER_EEPROM_WRITE_RETRY
;
285 /* TODO sleep 5 ms so other threads can get a lock */
289 m_adapterMessageQueue
->Clear();
290 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "communication thread ended");
294 bool CUSBCECAdapterCommunication::HandlePoll(const CCECAdapterMessage
&msg
)
296 bool bIsError(msg
.IsError());
297 cec_adapter_messagecode
messageCode(msg
.Message());
298 CLockObject
lock(m_mutex
);
300 if (messageCode
== MSGCODE_FRAME_START
&& msg
.IsACK())
302 m_lastPollDestination
= msg
.Destination();
303 if (msg
.Destination() < CECDEVICE_BROADCAST
)
305 if (!m_bWaitingForAck
[msg
.Destination()] && !msg
.IsEOM())
308 m_callback
->HandlePoll(msg
.Initiator(), msg
.Destination());
311 m_bWaitingForAck
[msg
.Destination()] = false;
314 else if (messageCode
== MSGCODE_RECEIVE_FAILED
)
316 /* hack to suppress warnings when an LG is polling */
317 if (m_lastPollDestination
!= CECDEVICE_UNKNOWN
)
318 bIsError
= m_callback
->HandleReceiveFailed(m_lastPollDestination
);
324 void CUSBCECAdapterCommunication::MarkAsWaiting(const cec_logical_address dest
)
326 /* mark as waiting for an ack from the destination */
327 if (dest
< CECDEVICE_BROADCAST
)
329 CLockObject
lock(m_mutex
);
330 m_bWaitingForAck
[dest
] = true;
334 void CUSBCECAdapterCommunication::ClearInputBytes(uint32_t iTimeout
/* = CEC_CLEAR_INPUT_DEFAULT_WAIT */)
336 CTimeout
timeout(iTimeout
);
338 ssize_t
iBytesRead(0);
339 bool bGotMsgEnd(true);
341 while (timeout
.TimeLeft() > 0 && ((iBytesRead
= m_port
->Read(buff
, 1024, 5)) > 0 || !bGotMsgEnd
))
344 /* if something was received, wait for MSGEND */
345 for (ssize_t iPtr
= 0; iPtr
< iBytesRead
; iPtr
++)
346 bGotMsgEnd
= buff
[iPtr
] == MSGEND
;
350 bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
353 bool bChanged(false);
355 /* only send the command if the timeout changed */
357 CLockObject
lock(m_mutex
);
358 bChanged
= (m_iLineTimeout
!= iTimeout
);
359 m_iLineTimeout
= iTimeout
;
363 bReturn
= m_commands
->SetLineTimeout(iTimeout
);
368 bool CUSBCECAdapterCommunication::WriteToDevice(CCECAdapterMessage
*message
)
370 CLockObject
adapterLock(m_mutex
);
373 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "error writing command '%s' to serial port '%s': the connection is closed", CCECAdapterMessage::ToString(message
->Message()), m_port
->GetName().c_str());
374 message
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
378 /* write the message */
379 if (m_port
->Write(message
->packet
.data
, message
->Size()) != (ssize_t
) message
->Size())
381 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());
382 message
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
383 // this will trigger an alert in the reader thread
388 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command '%s' sent", message
->IsTranmission() ? "CEC transmission" : CCECAdapterMessage::ToString(message
->Message()));
389 message
->state
= ADAPTER_MESSAGE_STATE_SENT
;
393 bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout
, size_t iSize
/* = 256 */)
395 ssize_t
iBytesRead(0);
400 /* read from the serial port */
402 CLockObject
lock(m_mutex
);
406 iBytesRead
= m_port
->Read(buff
, sizeof(uint8_t) * iSize
, iTimeout
);
408 if (m_port
->GetErrorNumber())
410 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "error reading from serial port: %s", m_port
->GetError().c_str());
416 if (iBytesRead
< 0 || iBytesRead
> 256)
418 else if (iBytesRead
> 0)
420 /* add the data to the current frame */
421 m_adapterMessageQueue
->AddData(buff
, iBytesRead
);
427 CCECAdapterMessage
*CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode
, CCECAdapterMessage
¶ms
, bool bIsRetry
/* = false */)
429 if (!IsOpen() || !m_adapterMessageQueue
)
432 /* create the adapter message for this command */
433 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
434 output
->PushBack(MSGSTART
);
435 output
->PushEscaped((uint8_t)msgCode
);
436 output
->Append(params
);
437 output
->PushBack(MSGEND
);
439 /* write the command */
440 if (!m_adapterMessageQueue
->Write(output
))
442 // this will trigger an alert in the reader thread
443 if (output
->state
== ADAPTER_MESSAGE_STATE_ERROR
)
449 if (!bIsRetry
&& output
->Reply() == MSGCODE_COMMAND_REJECTED
&& msgCode
!= MSGCODE_SET_CONTROLLED
&&
450 msgCode
!= MSGCODE_GET_BUILDDATE
/* same messagecode value had a different meaning in older fw builds */)
452 /* if the controller reported that the command was rejected, and we didn't send the command
453 to set controlled mode, then the controller probably switched to auto mode. set controlled
455 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "setting controlled mode and retrying");
457 if (SetControlledMode(true))
458 return SendCommand(msgCode
, params
, true);
465 bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
468 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
470 /* try to ping the adapter */
472 unsigned iPingTry(0);
473 while (timeout
.TimeLeft() > 0 && (bPinged
= PingAdapter()) == false)
475 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry
);
479 /* try to read the firmware version */
480 if (bPinged
&& timeout
.TimeLeft() > 0 && m_commands
->RequestFirmwareVersion() >= 2)
482 /* try to set controlled mode for v2+ firmwares */
483 unsigned iControlledTry(0);
484 bool bControlled(false);
485 while (timeout
.TimeLeft() > 0 && (bControlled
= SetControlledMode(true)) == false)
487 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry
);
490 bReturn
= bControlled
;
495 /* try to read the build date */
496 m_commands
->RequestBuildDate();
498 SetInitialised(bReturn
);
502 bool CUSBCECAdapterCommunication::IsOpen(void)
504 /* thread is not being stopped, the port is open and the thread is running */
505 return !IsStopped() && m_port
->IsOpen() && IsRunning();
508 std::string
CUSBCECAdapterCommunication::GetError(void) const
510 return m_port
->GetError();
513 void CUSBCECAdapterCommunication::SetInitialised(bool bSetTo
/* = true */)
515 CLockObject
lock(m_mutex
);
516 m_bInitialised
= bSetTo
;
519 bool CUSBCECAdapterCommunication::IsInitialised(void)
521 CLockObject
lock(m_mutex
);
522 return m_bInitialised
;
525 bool CUSBCECAdapterCommunication::StartBootloader(void)
527 if (m_port
->IsOpen() && m_commands
->StartBootloader())
535 bool CUSBCECAdapterCommunication::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
538 CLockObject
lock(m_mutex
);
539 if (m_logicalAddresses
== addresses
)
543 if (IsOpen() && m_commands
->SetAckMask(addresses
.AckMask()))
545 CLockObject
lock(m_mutex
);
546 m_logicalAddresses
= addresses
;
550 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "couldn't change the ackmask: the connection is closed");
554 cec_logical_addresses
CUSBCECAdapterCommunication::GetLogicalAddresses(void)
556 cec_logical_addresses addresses
;
557 CLockObject
lock(m_mutex
);
558 addresses
= m_logicalAddresses
;
562 bool CUSBCECAdapterCommunication::PingAdapter(void)
564 return IsOpen() ? m_commands
->PingAdapter() : false;
567 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
569 return m_commands
? m_commands
->GetFirmwareVersion() : CEC_FW_VERSION_UNKNOWN
;
572 uint32_t CUSBCECAdapterCommunication::GetFirmwareBuildDate(void)
574 return IsOpen() ? m_commands
->RequestBuildDate() : m_commands
? m_commands
->GetPersistedBuildDate() : 0;
577 bool CUSBCECAdapterCommunication::IsRunningLatestFirmware(void)
579 return GetFirmwareVersion() >= CEC_LATEST_ADAPTER_FW_VERSION
&&
580 GetFirmwareBuildDate() >= CEC_LATEST_ADAPTER_FW_DATE
;
583 bool CUSBCECAdapterCommunication::PersistConfiguration(const libcec_configuration
&configuration
)
587 // returns true when something changed
588 if (m_commands
->PersistConfiguration(configuration
))
591 CLockObject
lock(m_mutex
);
592 uint64_t iNow
= GetTimeMs();
593 if (iNow
- m_iLastEepromWrite
< CEC_ADAPTER_EEPROM_WRITE_INTERVAL
)
595 // if there was more than 1 write within the last 30 seconds, schedule another one
596 if (m_iScheduleEepromWrite
== 0)
597 m_iScheduleEepromWrite
= m_iLastEepromWrite
+ CEC_ADAPTER_EEPROM_WRITE_INTERVAL
;
602 m_iLastEepromWrite
= iNow
;
606 if (!m_commands
->WriteEEPROM())
608 // write failed, retry later
609 CLockObject
lock(m_mutex
);
610 m_iScheduleEepromWrite
= GetTimeMs() + CEC_ADAPTER_EEPROM_WRITE_RETRY
;
614 return IsOpen() ? m_commands
->PersistConfiguration(configuration
) : false;
617 bool CUSBCECAdapterCommunication::GetConfiguration(libcec_configuration
&configuration
)
619 return IsOpen() ? m_commands
->GetConfiguration(configuration
) : false;
622 std::string
CUSBCECAdapterCommunication::GetPortName(void)
624 return m_port
->GetName();
627 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled
)
629 return IsOpen() ? m_commands
->SetControlledMode(controlled
) : false;
632 uint16_t CUSBCECAdapterCommunication::GetPhysicalAddress(void)
636 // try to get the PA from ADL
637 #if defined(HAS_ADL_EDID_PARSER)
639 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - trying to get the physical address via ADL", __FUNCTION__
);
641 iPA
= adl
.GetPhysicalAddress();
642 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - ADL returned physical address %04x", __FUNCTION__
, iPA
);
646 // try to get the PA from the nvidia driver
647 #if defined(HAS_NVIDIA_EDID_PARSER)
650 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - trying to get the physical address via nvidia driver", __FUNCTION__
);
652 iPA
= nv
.GetPhysicalAddress();
653 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - nvidia driver returned physical address %04x", __FUNCTION__
, iPA
);
657 // try to get the PA from the OS
660 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - trying to get the physical address from the OS", __FUNCTION__
);
661 iPA
= CEDIDParser::GetPhysicalAddress();
662 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "%s - OS returned physical address %04x", __FUNCTION__
, iPA
);
668 void *CAdapterPingThread::Process(void)
672 if (m_timeout
.TimeLeft() == 0)
674 /* reinit the timeout */
675 m_timeout
.Init(CEC_ADAPTER_PING_TIMEOUT
);
677 /* send a ping to the adapter */
679 int iFailedCounter(0);
680 while (!bPinged
&& iFailedCounter
< 3)
682 if (!m_com
->PingAdapter())
684 /* sleep and retry */
685 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
694 if (iFailedCounter
== 3)
696 /* failed to ping the adapter 3 times in a row. something must be wrong with the connection */
697 m_com
->LIB_CEC
->AddLog(CEC_LOG_ERROR
, "failed to ping the adapter 3 times in a row. closing the connection.");
698 m_com
->StopThread(false);