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/
33 #include "USBCECAdapterCommunication.h"
34 #include "../platform/sockets/serialport.h"
35 #include "../platform/util/timeutils.h"
36 #include "../LibCEC.h"
37 #include "../CECProcessor.h"
41 using namespace PLATFORM
;
43 #define CEC_ADAPTER_PING_TIMEOUT 15000
45 void *CUSBCECAdapterProcessor::Process(void)
50 if (m_inBuffer
.Pop(command
))
51 m_callback
->OnCommandReceived(command
);
58 void CUSBCECAdapterProcessor::AddCommand(cec_command command
)
60 m_inBuffer
.Push(command
);
63 CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor
*processor
, const char *strPort
, uint16_t iBaudRate
/* = 38400 */) :
65 m_processor(processor
),
68 m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN
),
69 m_lastDestination(CECDEVICE_UNKNOWN
),
70 m_bNextIsEscaped(false),
72 m_messageProcessor(NULL
),
75 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
76 m_bWaitingForAck
[iPtr
] = false;
77 m_port
= new CSerialPort(strPort
, iBaudRate
);
80 bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs
/* = 10000 */)
83 uint64_t iNow
= GetTimeMs();
84 uint64_t iTarget
= iTimeoutMs
> 0 ? iNow
+ iTimeoutMs
: iNow
+ CEC_DEFAULT_TRANSMIT_WAIT
;
86 /* try to ping the adapter */
89 while (iNow
< iTarget
&& (bPinged
= PingAdapter()) == false)
91 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry
);
96 /* try to read the firmware version */
97 m_iFirmwareVersion
= CEC_FW_VERSION_UNKNOWN
;
98 unsigned iFwVersionTry(0);
99 while (bPinged
&& iNow
< iTarget
&& (m_iFirmwareVersion
= GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN
&& iFwVersionTry
< 3)
101 CLibCEC::AddLog(CEC_LOG_WARNING
, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry
);
106 if (m_iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
)
108 CLibCEC::AddLog(CEC_LOG_DEBUG
, "defaulting to firmware version 1");
109 m_iFirmwareVersion
= 1;
112 if (m_iFirmwareVersion
>= 2)
114 /* try to set controlled mode */
115 unsigned iControlledTry(0);
116 bool bControlled(false);
117 while (iNow
< iTarget
&& (bControlled
= SetControlledMode(true)) == false)
119 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry
);
123 bReturn
= bControlled
;
129 CLockObject
lock(m_mutex
);
130 m_bInitialised
= bReturn
;
136 bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback
*cb
, uint32_t iTimeoutMs
/* = 10000 */, bool bSkipChecks
/* = false */, bool bStartListening
/* = true */)
138 uint64_t iNow
= GetTimeMs();
139 uint64_t iTimeout
= iNow
+ iTimeoutMs
;
142 CLockObject
lock(m_mutex
);
146 CLibCEC::AddLog(CEC_LOG_ERROR
, "port is NULL");
152 CLibCEC::AddLog(CEC_LOG_ERROR
, "port is already open");
158 bool bConnected(false);
159 while (!bConnected
&& iNow
< iTimeout
)
161 if ((bConnected
= m_port
->Open(iTimeout
)) == false)
163 strError
.Format("error opening serial port '%s': %s", m_port
->GetName().c_str(), m_port
->GetError().c_str());
171 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
175 CLibCEC::AddLog(CEC_LOG_DEBUG
, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
179 //clear any input bytes
181 ssize_t
iBytesRead(0);
182 bool bGotMsgStart(false), bGotMsgEnd(false);
183 while ((iBytesRead
= m_port
->Read(buff
, 1024, 100)) > 0 || (bGotMsgStart
&& !bGotMsgEnd
))
186 CLibCEC::AddLog(CEC_LOG_DEBUG
, "data received, clearing it");
187 // if something was received, wait for MSGEND
188 for (ssize_t iPtr
= 0; iPtr
< iBytesRead
; iPtr
++)
190 if (buff
[iPtr
] == MSGSTART
)
192 else if (buff
[iPtr
] == MSGEND
)
200 if (!bSkipChecks
&& !CheckAdapter())
202 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter failed to pass basic checks");
207 else if (bStartListening
)
211 CLibCEC::AddLog(CEC_LOG_DEBUG
, "communication thread started");
218 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
230 void CUSBCECAdapterCommunication::Close(void)
235 void *CUSBCECAdapterCommunication::Process(void)
237 m_messageProcessor
= new CUSBCECAdapterProcessor(m_callback
);
238 m_messageProcessor
->CreateThread();
242 bool bCommandReceived(false);
243 CTimeout
pingTimeout(CEC_ADAPTER_PING_TIMEOUT
);
247 CLockObject
lock(m_mutex
);
249 bCommandReceived
= m_callback
&& Read(command
, 0) && m_bInitialised
;
252 /* push the next command to the callback method if there is one */
253 if (!IsStopped() && bCommandReceived
)
254 m_messageProcessor
->AddCommand(command
);
256 /* ping the adapter every 15 seconds */
257 if (pingTimeout
.TimeLeft() == 0)
259 pingTimeout
.Init(CEC_ADAPTER_PING_TIMEOUT
);
270 /* stop the message processor */
271 m_messageProcessor
->StopThread();
272 delete m_messageProcessor
;
273 m_messageProcessor
= NULL
;
275 /* notify all threads that are waiting on messages to be sent */
276 CCECAdapterMessage
*msg(NULL
);
277 while (m_outBuffer
.Pop(msg
))
278 msg
->event
.Broadcast();
280 /* set the ackmask to 0 before closing the connection */
281 SetAckMaskInternal(0, true);
283 if (m_iFirmwareVersion
>= 2)
284 SetControlledMode(false);
295 cec_adapter_message_state
CUSBCECAdapterCommunication::Write(const cec_command
&data
, uint8_t iMaxTries
, uint8_t iLineTimeout
/* = 3 */, uint8_t iRetryLineTimeout
/* = 3 */)
297 cec_adapter_message_state
retVal(ADAPTER_MESSAGE_STATE_UNKNOWN
);
301 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
303 /* set the number of retries */
304 if (data
.opcode
== CEC_OPCODE_NONE
) //TODO
305 output
->maxTries
= 1;
306 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
307 output
->maxTries
= iMaxTries
;
309 output
->lineTimeout
= iLineTimeout
;
310 output
->retryTimeout
= iRetryLineTimeout
;
314 CLockObject
lock(m_mutex
);
315 m_bWaitingForAck
[data
.destination
] = true;
319 while (bRetry
&& ++output
->tries
< output
->maxTries
)
321 bRetry
= (!Write(output
) || output
->NeedsRetry()) && output
->transmit_timeout
> 0;
323 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
325 retVal
= output
->state
;
331 bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage
*data
)
333 data
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
334 m_outBuffer
.Push(data
);
335 data
->event
.Wait(5000);
337 if ((data
->expectControllerAck
&& data
->state
!= ADAPTER_MESSAGE_STATE_SENT_ACKED
) ||
338 (!data
->expectControllerAck
&& data
->state
!= ADAPTER_MESSAGE_STATE_SENT
))
340 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command was not %s", data
->state
== ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
? "acked" : "sent");
347 bool CUSBCECAdapterCommunication::Read(cec_command
&command
, uint32_t iTimeout
)
352 CCECAdapterMessage msg
;
353 if (Read(msg
, iTimeout
))
355 if (ParseMessage(msg
))
357 command
= m_currentframe
;
358 m_currentframe
.Clear();
365 bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
367 CLockObject
lock(m_mutex
);
370 CCECAdapterMessage
*buf(NULL
);
372 if (!m_inBuffer
.Pop(buf
))
374 if (iTimeout
== 0 || !m_rcvCondition
.Wait(m_mutex
, m_bHasData
, iTimeout
))
377 m_bHasData
= !m_inBuffer
.IsEmpty();
382 msg
.packet
= buf
->packet
;
383 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
390 CStdString
CUSBCECAdapterCommunication::GetError(void) const
393 strError
= m_port
->GetError();
397 bool CUSBCECAdapterCommunication::StartBootloader(void)
403 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
405 CCECAdapterMessage params
;
406 return SendCommand(MSGCODE_START_BOOTLOADER
, params
, false);
409 bool CUSBCECAdapterCommunication::PingAdapter(void)
411 CLockObject
lock(m_mutex
);
412 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
414 CCECAdapterMessage params
;
415 return SendCommand(MSGCODE_PING
, params
);
418 bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage
&msg
)
421 bool bIsError(msg
.IsError());
426 CLockObject
adapterLock(m_mutex
);
427 switch(msg
.Message())
429 case MSGCODE_FRAME_START
:
431 m_currentframe
.Clear();
434 m_currentframe
.initiator
= msg
.Initiator();
435 m_currentframe
.destination
= msg
.Destination();
436 m_currentframe
.ack
= msg
.IsACK();
437 m_currentframe
.eom
= msg
.IsEOM();
439 if (m_currentframe
.ack
== 0x1)
441 m_lastDestination
= m_currentframe
.destination
;
442 if (!m_bWaitingForAck
[m_currentframe
.destination
])
443 m_processor
->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
445 m_bWaitingForAck
[m_currentframe
.destination
] = false;
449 case MSGCODE_RECEIVE_FAILED
:
451 m_currentframe
.Clear();
452 if (m_lastDestination
!= CECDEVICE_UNKNOWN
)
453 bIsError
= m_processor
->HandleReceiveFailed(m_lastDestination
);
456 case MSGCODE_FRAME_DATA
:
460 m_currentframe
.PushBack(msg
[1]);
461 m_currentframe
.eom
= msg
.IsEOM();
469 CLibCEC::AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
473 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
475 uint16_t iReturn(m_iFirmwareVersion
);
477 if (iReturn
== CEC_FW_VERSION_UNKNOWN
)
479 CLockObject
lock(m_mutex
);
480 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
481 cec_datapacket response
= GetSetting(MSGCODE_FIRMWARE_VERSION
, 2);
482 if (response
.size
== 2)
484 m_iFirmwareVersion
= (response
[0] << 8 | response
[1]);
485 iReturn
= m_iFirmwareVersion
;
486 CLibCEC::AddLog(CEC_LOG_DEBUG
, "firmware version %d", m_iFirmwareVersion
);
493 bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
497 if (m_iLineTimeout
!= iTimeout
)
499 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the line timeout to %d", iTimeout
);
500 CCECAdapterMessage params
;
501 params
.PushEscaped(iTimeout
);
502 bReturn
= SendCommand(MSGCODE_TRANSMIT_IDLETIME
, params
);
504 m_iLineTimeout
= iTimeout
;
510 bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask
)
512 return SetAckMaskInternal(iMask
, IsRunning());
515 bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask
, bool bWriteDirectly
/* = false */)
517 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
519 CCECAdapterMessage params
;
520 params
.PushEscaped(iMask
>> 8);
521 params
.PushEscaped((uint8_t)iMask
);
522 return SendCommand(MSGCODE_SET_ACK_MASK
, params
, true, false, bWriteDirectly
);
525 bool CUSBCECAdapterCommunication::PersistConfiguration(libcec_configuration
*configuration
)
527 if (m_iFirmwareVersion
< 2)
531 bReturn
&= SetSettingAutoEnabled(true);
532 bReturn
&= SetSettingDeviceType(CLibCEC::GetType(configuration
->logicalAddresses
.primary
));
533 bReturn
&= SetSettingDefaultLogicalAddress(configuration
->logicalAddresses
.primary
);
534 bReturn
&= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration
->logicalAddresses
.primary
));
535 bReturn
&= SetSettingPhysicalAddress(configuration
->iPhysicalAddress
);
536 bReturn
&= SetSettingCECVersion(CEC_VERSION_1_3A
);
537 bReturn
&= SetSettingOSDName(configuration
->strDeviceName
);
539 bReturn
= WriteEEPROM();
543 bool CUSBCECAdapterCommunication::GetConfiguration(libcec_configuration
*configuration
)
545 configuration
->iFirmwareVersion
= m_iFirmwareVersion
;
546 if (m_iFirmwareVersion
< 2)
550 cec_device_type type
;
551 if (GetSettingDeviceType(type
))
553 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device type setting %s", m_processor
->ToString(type
));
554 configuration
->deviceTypes
.Clear();
555 configuration
->deviceTypes
.Add(type
);
559 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device type setting");
563 if (GetSettingPhysicalAddress(configuration
->iPhysicalAddress
))
565 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted physical address setting %4x", configuration
->iPhysicalAddress
);
569 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted physical address setting");
573 CStdString strDeviceName
;
574 if (GetSettingOSDName(strDeviceName
))
576 snprintf(configuration
->strDeviceName
, 13, "%s", strDeviceName
.c_str());
577 CLibCEC::AddLog(CEC_LOG_DEBUG
, "using persisted device name setting %s", configuration
->strDeviceName
);
581 CLibCEC::AddLog(CEC_LOG_DEBUG
, "no persisted device name setting");
585 // don't read the following settings:
586 // - auto enabled (always enabled)
587 // - default logical address (autodetected)
588 // - logical address mask (autodetected)
589 // - CEC version (1.3a)
591 // TODO to be added to the firmware:
592 // - base device (4 bits)
593 // - HDMI port number (4 bits)
594 // - TV vendor id (12 bits)
595 // - wake devices (8 bits)
596 // - standby devices (8 bits)
597 // - use TV menu language (1 bit)
598 // - activate source (1 bit)
599 // - power off screensaver (1 bit)
600 // - power off on standby (1 bit)
601 // - send inactive source (1 bit)
605 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled
)
607 CLockObject
lock(m_mutex
);
608 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
610 CCECAdapterMessage params
;
611 params
.PushEscaped(controlled
? 1 : 0);
612 return SendCommand(MSGCODE_SET_CONTROLLED
, params
);
615 bool CUSBCECAdapterCommunication::SetSettingAutoEnabled(bool enabled
)
617 CLockObject
lock(m_mutex
);
618 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
620 CCECAdapterMessage params
;
621 params
.PushEscaped(enabled
? 1 : 0);
622 return SendCommand(MSGCODE_SET_AUTO_ENABLED
, params
);
625 bool CUSBCECAdapterCommunication::GetSettingAutoEnabled(bool &enabled
)
627 CLockObject
lock(m_mutex
);
628 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting autonomous mode setting");
630 cec_datapacket response
= GetSetting(MSGCODE_GET_AUTO_ENABLED
, 1);
631 if (response
.size
== 1)
633 enabled
= response
[0] == 1;
639 bool CUSBCECAdapterCommunication::SetSettingDeviceType(cec_device_type type
)
641 CLockObject
lock(m_mutex
);
642 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the device type to %1X", (uint8_t)type
);
644 CCECAdapterMessage params
;
645 params
.PushEscaped((uint8_t)type
);
646 return SendCommand(MSGCODE_SET_DEVICE_TYPE
, params
);
649 bool CUSBCECAdapterCommunication::GetSettingDeviceType(cec_device_type
&value
)
651 CLockObject
lock(m_mutex
);
652 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting device type setting");
654 cec_datapacket response
= GetSetting(MSGCODE_GET_DEVICE_TYPE
, 1);
655 if (response
.size
== 1)
657 value
= (cec_device_type
)response
[0];
663 bool CUSBCECAdapterCommunication::SetSettingDefaultLogicalAddress(cec_logical_address address
)
665 CLockObject
lock(m_mutex
);
666 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %1X", address
);
668 CCECAdapterMessage params
;
669 params
.PushEscaped((uint8_t)address
);
670 return SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
, params
);
673 bool CUSBCECAdapterCommunication::GetSettingDefaultLogicalAddress(cec_logical_address
&address
)
675 CLockObject
lock(m_mutex
);
676 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting default logical address setting");
678 cec_datapacket response
= GetSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS
, 1);
679 if (response
.size
== 1)
681 address
= (cec_logical_address
)response
[0];
687 bool CUSBCECAdapterCommunication::SetSettingLogicalAddressMask(uint16_t iMask
)
689 CLockObject
lock(m_mutex
);
690 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X", iMask
);
692 CCECAdapterMessage params
;
693 params
.PushEscaped(iMask
>> 8);
694 params
.PushEscaped((uint8_t)iMask
);
695 return SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK
, params
);
698 bool CUSBCECAdapterCommunication::GetSettingLogicalAddressMask(uint16_t &iMask
)
700 CLockObject
lock(m_mutex
);
701 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting logical address mask setting");
703 cec_datapacket response
= GetSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK
, 2);
704 if (response
.size
== 2)
706 iMask
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
712 bool CUSBCECAdapterCommunication::SetSettingPhysicalAddress(uint16_t iPhysicalAddress
)
714 CLockObject
lock(m_mutex
);
715 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the physical address to %04X", iPhysicalAddress
);
717 CCECAdapterMessage params
;
718 params
.PushEscaped(iPhysicalAddress
>> 8);
719 params
.PushEscaped((uint8_t)iPhysicalAddress
);
720 return SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS
, params
);
723 bool CUSBCECAdapterCommunication::GetSettingPhysicalAddress(uint16_t &iPhysicalAddress
)
725 CLockObject
lock(m_mutex
);
726 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting physical address setting");
728 cec_datapacket response
= GetSetting(MSGCODE_GET_PHYSICAL_ADDRESS
, 2);
729 if (response
.size
== 2)
731 iPhysicalAddress
= ((uint16_t)response
[0] << 8) | ((uint16_t)response
[1]);
737 bool CUSBCECAdapterCommunication::SetSettingCECVersion(cec_version version
)
739 CLockObject
lock(m_mutex
);
740 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s", CLibCEC::GetInstance()->ToString(version
));
742 CCECAdapterMessage params
;
743 params
.PushEscaped((uint8_t)version
);
744 return SendCommand(MSGCODE_SET_HDMI_VERSION
, params
);
747 bool CUSBCECAdapterCommunication::GetSettingCECVersion(cec_version
&version
)
749 CLockObject
lock(m_mutex
);
750 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting CEC version setting");
752 cec_datapacket response
= GetSetting(MSGCODE_GET_HDMI_VERSION
, 1);
753 if (response
.size
== 1)
755 version
= (cec_version
)response
[0];
761 bool CUSBCECAdapterCommunication::SetSettingOSDName(const char *strOSDName
)
763 CLockObject
lock(m_mutex
);
764 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s", strOSDName
);
766 CCECAdapterMessage params
;
767 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
768 params
.PushEscaped(strOSDName
[iPtr
]);
769 return SendCommand(MSGCODE_SET_OSD_NAME
, params
);
772 bool CUSBCECAdapterCommunication::GetSettingOSDName(CStdString
&strOSDName
)
774 CLockObject
lock(m_mutex
);
775 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting OSD name setting");
777 cec_datapacket response
= GetSetting(MSGCODE_GET_OSD_NAME
, 13);
778 if (response
.size
== 0)
782 for (uint8_t iPtr
= 0; iPtr
< response
.size
&& iPtr
< 13; iPtr
++)
783 buf
[iPtr
] = (char)response
[iPtr
];
784 buf
[response
.size
] = 0;
786 strOSDName
.Format("%s", buf
);
790 bool CUSBCECAdapterCommunication::WriteEEPROM(void)
792 CLockObject
lock(m_mutex
);
793 CLibCEC::AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
795 CCECAdapterMessage params
;
796 return SendCommand(MSGCODE_WRITE_EEPROM
, params
);
799 bool CUSBCECAdapterCommunication::IsOpen(void)
801 return !IsStopped() && m_port
->IsOpen() && IsRunning();
804 bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage
&message
)
807 bool bTransmitSucceeded(false);
808 uint8_t iPacketsLeft(message
.isTransmission
? message
.Size() / 4 : 1);
810 int64_t iNow
= GetTimeMs();
811 int64_t iTargetTime
= iNow
+ (message
.transmit_timeout
<= 5 ? CEC_DEFAULT_TRANSMIT_WAIT
: message
.transmit_timeout
);
813 while (!bTransmitSucceeded
&& !bError
&& iNow
< iTargetTime
)
816 CCECAdapterMessage msg
;
823 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
825 if (m_bWaitingForAck
[msg
.Initiator()])
826 m_bWaitingForAck
[msg
.Initiator()] = false;
829 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
830 m_lastDestination
= msg
.Initiator();
836 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
837 m_lastDestination
!= CECDEVICE_UNKNOWN
&&
838 m_processor
->HandleReceiveFailed(m_lastDestination
))
844 bError
= msg
.IsError();
847 message
.reply
= msg
.Message();
848 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
852 switch(msg
.Message())
854 case MSGCODE_COMMAND_ACCEPTED
:
855 if (iPacketsLeft
> 0)
857 if (!message
.isTransmission
&& iPacketsLeft
== 0)
858 bTransmitSucceeded
= true;
859 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - waiting for %d more", msg
.ToString().c_str(), iPacketsLeft
);
861 case MSGCODE_TRANSMIT_SUCCEEDED
:
862 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
863 bTransmitSucceeded
= (iPacketsLeft
== 0);
864 bError
= !bTransmitSucceeded
;
865 message
.reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
868 // ignore other data while waiting
876 message
.state
= bTransmitSucceeded
&& !bError
?
877 ADAPTER_MESSAGE_STATE_SENT_ACKED
:
878 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
880 return bTransmitSucceeded
&& !bError
;
883 void CUSBCECAdapterCommunication::AddData(uint8_t *data
, size_t iLen
)
885 CLockObject
lock(m_mutex
);
886 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
890 if (data
[iPtr
] == MSGSTART
)
893 else if (data
[iPtr
] == MSGSTART
) //we found a msgstart before msgend, this is not right, remove
895 if (m_currentAdapterMessage
.Size() > 0)
896 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
897 m_currentAdapterMessage
.Clear();
900 else if (data
[iPtr
] == MSGEND
)
902 CCECAdapterMessage
*newMessage
= new CCECAdapterMessage
;
903 newMessage
->packet
= m_currentAdapterMessage
.packet
;
904 m_inBuffer
.Push(newMessage
);
905 m_currentAdapterMessage
.Clear();
907 m_bNextIsEscaped
= false;
909 m_rcvCondition
.Broadcast();
911 else if (m_bNextIsEscaped
)
913 m_currentAdapterMessage
.PushBack(data
[iPtr
] + (uint8_t)ESCOFFSET
);
914 m_bNextIsEscaped
= false;
916 else if (data
[iPtr
] == MSGESC
)
918 m_bNextIsEscaped
= true;
922 m_currentAdapterMessage
.PushBack(data
[iPtr
]);
927 bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout
, size_t iSize
/* = 256 */)
936 CLockObject
lock(m_mutex
);
937 iBytesRead
= m_port
->Read(buff
, sizeof(uint8_t) * iSize
, iTimeout
);
938 if (iBytesRead
< 0 || iBytesRead
> 256)
940 CLibCEC::AddLog(CEC_LOG_ERROR
, "error reading from serial port: %s", m_port
->GetError().c_str());
944 else if (iBytesRead
> 0)
946 AddData(buff
, iBytesRead
);
949 return iBytesRead
> 0;
952 void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
954 CLockObject
adapterLock(m_mutex
);
955 if (!m_port
->IsOpen())
957 CLibCEC::AddLog(CEC_LOG_ERROR
, "error writing to serial port: the connection is closed");
958 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
962 if (msg
->isTransmission
&& (msg
->Size() < 2 || msg
->At(1) != MSGCODE_TRANSMIT_IDLETIME
))
965 SetLineTimeout(msg
->lineTimeout
);
967 SetLineTimeout(msg
->retryTimeout
);
970 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (ssize_t
) msg
->Size())
972 CLibCEC::AddLog(CEC_LOG_ERROR
, "error writing to serial port: %s", m_port
->GetError().c_str());
973 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
977 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command sent");
978 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
980 if (msg
->expectControllerAck
)
982 if (!WaitForAck(*msg
))
983 CLibCEC::AddLog(CEC_LOG_DEBUG
, "did not receive ack");
989 void CUSBCECAdapterCommunication::WriteNextCommand(void)
991 CCECAdapterMessage
*msg(NULL
);
992 if (m_outBuffer
.Pop(msg
))
993 SendMessageToAdapter(msg
);
996 CStdString
CUSBCECAdapterCommunication::GetPortName(void)
999 strName
= m_port
->GetName();
1003 bool CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode
, CCECAdapterMessage
¶ms
, bool bExpectAck
/* = true */, bool bIsTransmission
/* = false */, bool bSendDirectly
/* = true */, bool bIsRetry
/* = false */)
1005 CLockObject
lock(m_mutex
);
1007 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
1009 output
->PushBack(MSGSTART
);
1010 output
->PushEscaped((uint8_t)msgCode
);
1011 output
->Append(params
);
1012 output
->PushBack(MSGEND
);
1013 output
->isTransmission
= bIsTransmission
;
1014 output
->expectControllerAck
= bExpectAck
;
1017 SendMessageToAdapter(output
);
1021 bool bWriteOk
= output
->state
== (output
->expectControllerAck
? ADAPTER_MESSAGE_STATE_SENT_ACKED
: ADAPTER_MESSAGE_STATE_SENT
);
1022 cec_adapter_messagecode reply
= output
->reply
;
1027 CLibCEC::AddLog(CEC_LOG_ERROR
, "'%s' failed", CCECAdapterMessage::ToString(msgCode
));
1029 if (!bIsRetry
&& reply
== MSGCODE_COMMAND_REJECTED
&& msgCode
!= MSGCODE_SET_CONTROLLED
)
1031 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting controlled mode and retrying");
1032 if (SetControlledMode(true))
1033 return SendCommand(msgCode
, params
, bExpectAck
, bIsTransmission
, bSendDirectly
, true);
1041 cec_datapacket
CUSBCECAdapterCommunication::GetSetting(cec_adapter_messagecode msgCode
, uint8_t iResponseLength
)
1043 cec_datapacket retVal
;
1046 CCECAdapterMessage params
;
1047 if (!SendCommand(msgCode
, params
, false))
1049 CLibCEC::AddLog(CEC_LOG_ERROR
, "%s failed", CCECAdapterMessage::ToString(msgCode
));
1053 Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32
1054 ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT
, iResponseLength
+ 3 /* start + msgcode + iResponseLength + end */);
1055 CCECAdapterMessage input
;
1058 if (input
.Message() != msgCode
)
1059 CLibCEC::AddLog(CEC_LOG_ERROR
, "invalid response to %s received (%s)", CCECAdapterMessage::ToString(msgCode
), CCECAdapterMessage::ToString(input
.Message()));
1062 for (uint8_t iPtr
= 1; iPtr
< input
.Size(); iPtr
++)
1063 retVal
.PushBack(input
[iPtr
]);
1068 CLibCEC::AddLog(CEC_LOG_ERROR
, "no response to %s received", CCECAdapterMessage::ToString(msgCode
));