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_lastInitiator(CECDEVICE_UNKNOWN
),
70 m_bNextIsEscaped(false),
72 m_messageProcessor(NULL
),
75 m_port
= new PLATFORM::CSerialPort(strPort
, iBaudRate
);
78 CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void)
83 bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs
/* = 10000 */)
86 uint64_t iNow
= GetTimeMs();
87 uint64_t iTarget
= iTimeoutMs
> 0 ? iNow
+ iTimeoutMs
: iNow
+ CEC_DEFAULT_TRANSMIT_WAIT
;
89 /* try to ping the adapter */
92 while (iNow
< iTarget
&& (bPinged
= PingAdapter()) == false)
94 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry
);
99 /* try to read the firmware version */
100 m_iFirmwareVersion
= CEC_FW_VERSION_UNKNOWN
;
101 unsigned iFwVersionTry(0);
102 while (bPinged
&& iNow
< iTarget
&& (m_iFirmwareVersion
= GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN
&& iFwVersionTry
< 3)
104 CLibCEC::AddLog(CEC_LOG_WARNING
, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry
);
109 if (m_iFirmwareVersion
== CEC_FW_VERSION_UNKNOWN
)
111 CLibCEC::AddLog(CEC_LOG_DEBUG
, "defaulting to firmware version 1");
112 m_iFirmwareVersion
= 1;
115 if (m_iFirmwareVersion
>= 2)
117 /* try to set controlled mode */
118 unsigned iControlledTry(0);
119 bool bControlled(false);
120 while (iNow
< iTarget
&& (bControlled
= SetControlledMode(true)) == false)
122 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry
);
126 bReturn
= bControlled
;
132 CLockObject
lock(m_mutex
);
133 m_bInitialised
= bReturn
;
139 bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback
*cb
, uint32_t iTimeoutMs
/* = 10000 */, bool bSkipChecks
/* = false */)
141 uint64_t iNow
= GetTimeMs();
142 uint64_t iTimeout
= iNow
+ iTimeoutMs
;
145 CLockObject
lock(m_mutex
);
149 CLibCEC::AddLog(CEC_LOG_ERROR
, "port is NULL");
155 CLibCEC::AddLog(CEC_LOG_ERROR
, "port is already open");
161 bool bConnected(false);
162 while (!bConnected
&& iNow
< iTimeout
)
164 if ((bConnected
= m_port
->Open(iTimeout
)) == false)
166 strError
.Format("error opening serial port '%s': %s", m_port
->GetName().c_str(), m_port
->GetError().c_str());
174 CLibCEC::AddLog(CEC_LOG_ERROR
, strError
);
178 CLibCEC::AddLog(CEC_LOG_DEBUG
, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
182 //clear any input bytes
184 while (m_port
->Read(buff
, 1024, 100) > 0)
186 CLibCEC::AddLog(CEC_LOG_DEBUG
, "data received, clearing it");
192 if (!bSkipChecks
&& !CheckAdapter())
194 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter failed to pass basic checks");
201 CLibCEC::AddLog(CEC_LOG_DEBUG
, "communication thread started");
206 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a communication thread");
213 void CUSBCECAdapterCommunication::Close(void)
218 void *CUSBCECAdapterCommunication::Process(void)
220 m_messageProcessor
= new CUSBCECAdapterProcessor(m_callback
);
221 m_messageProcessor
->CreateThread();
225 bool bCommandReceived(false);
226 CTimeout
pingTimeout(CEC_ADAPTER_PING_TIMEOUT
);
230 CLockObject
lock(m_mutex
);
232 bCommandReceived
= m_callback
&& Read(command
, 0) && m_bInitialised
;
235 /* push the next command to the callback method if there is one */
236 if (!IsStopped() && bCommandReceived
)
237 m_messageProcessor
->AddCommand(command
);
239 /* ping the adapter every 15 seconds */
240 if (pingTimeout
.TimeLeft() == 0)
242 pingTimeout
.Init(CEC_ADAPTER_PING_TIMEOUT
);
253 /* stop the message processor */
254 m_messageProcessor
->StopThread();
255 delete m_messageProcessor
;
257 /* notify all threads that are waiting on messages to be sent */
258 CCECAdapterMessage
*msg(NULL
);
259 while (m_outBuffer
.Pop(msg
))
260 msg
->event
.Broadcast();
262 /* set the ackmask to 0 before closing the connection */
263 SetAckMaskInternal(0, true);
265 if (m_iFirmwareVersion
>= 2)
266 SetControlledMode(false);
277 cec_adapter_message_state
CUSBCECAdapterCommunication::Write(const cec_command
&data
, uint8_t iMaxTries
, uint8_t iLineTimeout
/* = 3 */, uint8_t iRetryLineTimeout
/* = 3 */)
279 cec_adapter_message_state
retVal(ADAPTER_MESSAGE_STATE_UNKNOWN
);
283 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
285 /* set the number of retries */
286 if (data
.opcode
== CEC_OPCODE_NONE
) //TODO
287 output
->maxTries
= 1;
288 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
289 output
->maxTries
= iMaxTries
;
291 output
->lineTimeout
= iLineTimeout
;
292 output
->retryTimeout
= iRetryLineTimeout
;
296 while (bRetry
&& ++output
->tries
< output
->maxTries
)
298 bRetry
= (!Write(output
) || output
->NeedsRetry()) && output
->transmit_timeout
> 0;
300 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
302 retVal
= output
->state
;
308 bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage
*data
)
310 data
->state
= ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
;
311 m_outBuffer
.Push(data
);
312 data
->event
.Wait(5000);
314 if ((data
->expectControllerAck
&& data
->state
!= ADAPTER_MESSAGE_STATE_SENT_ACKED
) ||
315 (!data
->expectControllerAck
&& data
->state
!= ADAPTER_MESSAGE_STATE_SENT
))
317 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command was not %s", data
->state
== ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
? "acked" : "sent");
324 bool CUSBCECAdapterCommunication::Read(cec_command
&command
, uint32_t iTimeout
)
329 CCECAdapterMessage msg
;
330 if (Read(msg
, iTimeout
))
332 if (ParseMessage(msg
))
334 command
= m_currentframe
;
335 m_currentframe
.Clear();
342 bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
344 CLockObject
lock(m_mutex
);
347 CCECAdapterMessage
*buf(NULL
);
349 if (!m_inBuffer
.Pop(buf
))
351 if (iTimeout
== 0 || !m_rcvCondition
.Wait(m_mutex
, m_bHasData
, iTimeout
))
354 m_bHasData
= !m_inBuffer
.IsEmpty();
359 msg
.packet
= buf
->packet
;
360 msg
.state
= ADAPTER_MESSAGE_STATE_INCOMING
;
367 CStdString
CUSBCECAdapterCommunication::GetError(void) const
370 strError
= m_port
->GetError();
374 bool CUSBCECAdapterCommunication::StartBootloader(void)
380 CLibCEC::AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
381 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
383 output
->PushBack(MSGSTART
);
384 output
->PushEscaped(MSGCODE_START_BOOTLOADER
);
385 output
->PushBack(MSGEND
);
386 output
->isTransmission
= false;
387 output
->expectControllerAck
= false;
389 if ((bReturn
= Write(output
)) == false)
390 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
396 bool CUSBCECAdapterCommunication::PingAdapter(void)
398 CLockObject
lock(m_mutex
);
399 CLibCEC::AddLog(CEC_LOG_DEBUG
, "sending ping");
401 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
403 output
->PushBack(MSGSTART
);
404 output
->PushEscaped(MSGCODE_PING
);
405 output
->PushBack(MSGEND
);
406 output
->isTransmission
= false;
408 SendMessageToAdapter(output
);
409 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
413 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not ping the adapter");
420 bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage
&msg
)
423 bool bIsError(msg
.IsError());
428 CLockObject
adapterLock(m_mutex
);
429 switch(msg
.Message())
431 case MSGCODE_FRAME_START
:
433 m_currentframe
.Clear();
436 m_currentframe
.initiator
= msg
.Initiator();
437 m_currentframe
.destination
= msg
.Destination();
438 m_currentframe
.ack
= msg
.IsACK();
439 m_currentframe
.eom
= msg
.IsEOM();
441 if (m_currentframe
.ack
== 0x1)
443 m_lastInitiator
= m_currentframe
.initiator
;
444 m_processor
->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
448 case MSGCODE_RECEIVE_FAILED
:
450 m_currentframe
.Clear();
451 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
452 bIsError
= m_processor
->HandleReceiveFailed(m_lastInitiator
);
455 case MSGCODE_FRAME_DATA
:
459 m_currentframe
.PushBack(msg
[1]);
460 m_currentframe
.eom
= msg
.IsEOM();
468 CLibCEC::AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
472 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
474 uint16_t iReturn(m_iFirmwareVersion
);
476 if (iReturn
== CEC_FW_VERSION_UNKNOWN
)
478 CLockObject
lock(m_mutex
);
479 CLibCEC::AddLog(CEC_LOG_DEBUG
, "requesting the firmware version");
480 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
482 output
->PushBack(MSGSTART
);
483 output
->PushEscaped(MSGCODE_FIRMWARE_VERSION
);
484 output
->PushBack(MSGEND
);
485 output
->isTransmission
= false;
486 output
->expectControllerAck
= false;
488 SendMessageToAdapter(output
);
489 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
493 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not request the firmware version");
497 Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32
498 ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT
, 5 /* start + msgcode + 2 bytes for fw version + end */);
499 CCECAdapterMessage input
;
502 if (input
.Message() != MSGCODE_FIRMWARE_VERSION
|| input
.Size() != 3)
503 CLibCEC::AddLog(CEC_LOG_ERROR
, "invalid firmware version (size = %d, message = %d)", input
.Size(), input
.Message());
506 m_iFirmwareVersion
= (input
[1] << 8 | input
[2]);
507 iReturn
= m_iFirmwareVersion
;
512 CLibCEC::AddLog(CEC_LOG_ERROR
, "no firmware version received");
519 bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
521 m_iLineTimeout
= iTimeout
;
524 // bool bReturn(m_iLineTimeout != iTimeout);
528 // CCECAdapterMessage *output = new CCECAdapterMessage;
530 // output->PushBack(MSGSTART);
531 // output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
532 // output->PushEscaped(iTimeout);
533 // output->PushBack(MSGEND);
534 // output->isTransmission = false;
536 // if ((bReturn = Write(output)) == false)
537 // CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime");
544 bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask
)
546 return SetAckMaskInternal(iMask
, false);
549 bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask
, bool bWriteDirectly
/* = false */)
552 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting ackmask to %2x", iMask
);
554 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
556 output
->PushBack(MSGSTART
);
557 output
->PushEscaped(MSGCODE_SET_ACK_MASK
);
558 output
->PushEscaped(iMask
>> 8);
559 output
->PushEscaped((uint8_t)iMask
);
560 output
->PushBack(MSGEND
);
561 output
->isTransmission
= false;
564 SendMessageToAdapter(output
);
565 else if ((bReturn
= Write(output
)) == false)
566 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
572 bool CUSBCECAdapterCommunication::PersistConfiguration(libcec_configuration
*configuration
)
574 return SetAutoEnabled(true) &&
575 //SetDefaultLogicalAddress() TODO
576 //SetLogicalAddressMask() TODO
577 SetPhysicalAddress(configuration
->iPhysicalAddress
) &&
578 SetCECVersion(CEC_VERSION_1_3A
) &&
579 SetOSDName(configuration
->strDeviceName
) &&
583 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled
)
585 CLockObject
lock(m_mutex
);
586 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning controlled mode %s", controlled
? "on" : "off");
588 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
590 output
->PushBack(MSGSTART
);
591 output
->PushEscaped(MSGCODE_SET_CONTROLLED
);
592 output
->PushEscaped(controlled
);
593 output
->PushBack(MSGEND
);
594 output
->isTransmission
= false;
596 SendMessageToAdapter(output
);
597 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
601 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set controlled mode");
608 bool CUSBCECAdapterCommunication::SetAutoEnabled(bool enabled
)
610 CLockObject
lock(m_mutex
);
611 CLibCEC::AddLog(CEC_LOG_DEBUG
, "turning autonomous mode %s", enabled
? "on" : "off");
613 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
615 output
->PushBack(MSGSTART
);
616 output
->PushEscaped(MSGCODE_SET_AUTO_ENABLED
);
617 output
->PushEscaped(enabled
);
618 output
->PushBack(MSGEND
);
619 output
->isTransmission
= false;
621 SendMessageToAdapter(output
);
622 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
626 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set autonomous mode");
633 bool CUSBCECAdapterCommunication::SetDefaultLogicalAddress(cec_logical_address address
)
635 CLockObject
lock(m_mutex
);
636 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the default logical address to %1X", address
);
638 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
640 output
->PushBack(MSGSTART
);
641 output
->PushEscaped(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS
);
642 output
->PushEscaped((uint8_t) address
);
643 output
->PushBack(MSGEND
);
644 output
->isTransmission
= false;
646 SendMessageToAdapter(output
);
647 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
651 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the default logical address");
658 bool CUSBCECAdapterCommunication::SetLogicalAddressMask(uint16_t iMask
)
660 CLockObject
lock(m_mutex
);
661 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the logical address mask to %2X", iMask
);
663 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
665 output
->PushBack(MSGSTART
);
666 output
->PushEscaped(MSGCODE_SET_LOGICAL_ADDRESS_MASK
);
667 output
->PushEscaped(iMask
>> 8);
668 output
->PushEscaped((uint8_t)iMask
);
669 output
->PushBack(MSGEND
);
670 output
->isTransmission
= false;
672 SendMessageToAdapter(output
);
673 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
677 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the logical address mask");
684 bool CUSBCECAdapterCommunication::SetPhysicalAddress(uint16_t iPhysicalAddress
)
686 CLockObject
lock(m_mutex
);
687 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the physical address to %2X", iPhysicalAddress
);
689 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
691 output
->PushBack(MSGSTART
);
692 output
->PushEscaped(MSGCODE_SET_PHYSICAL_ADDRESS
);
693 output
->PushEscaped(iPhysicalAddress
>> 8);
694 output
->PushEscaped((uint8_t)iPhysicalAddress
);
695 output
->PushBack(MSGEND
);
696 output
->isTransmission
= false;
698 SendMessageToAdapter(output
);
699 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
703 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the physical address");
710 bool CUSBCECAdapterCommunication::SetCECVersion(cec_version version
)
712 CLockObject
lock(m_mutex
);
713 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the CEC version to %s", CLibCEC::GetInstance()->ToString(version
));
715 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
717 output
->PushBack(MSGSTART
);
718 output
->PushEscaped(MSGCODE_SET_HDMI_VERSION
);
719 output
->PushEscaped((uint8_t)version
);
720 output
->PushBack(MSGEND
);
721 output
->isTransmission
= false;
723 SendMessageToAdapter(output
);
724 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
728 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the CEC version");
735 bool CUSBCECAdapterCommunication::SetOSDName(const char *strOSDName
)
737 CLockObject
lock(m_mutex
);
738 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting the OSD name to %s", strOSDName
);
740 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
742 output
->PushBack(MSGSTART
);
743 output
->PushEscaped(MSGCODE_SET_OSD_NAME
);
744 for (size_t iPtr
= 0; iPtr
< strlen(strOSDName
); iPtr
++)
745 output
->PushEscaped(strOSDName
[iPtr
]);
746 output
->PushBack(MSGEND
);
747 output
->isTransmission
= false;
749 SendMessageToAdapter(output
);
750 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
754 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not set the OSD name");
761 bool CUSBCECAdapterCommunication::WriteEEPROM(void)
763 CLockObject
lock(m_mutex
);
764 CLibCEC::AddLog(CEC_LOG_DEBUG
, "writing settings in the EEPROM");
766 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
768 output
->PushBack(MSGSTART
);
769 output
->PushEscaped(MSGCODE_WRITE_EEPROM
);
770 output
->PushBack(MSGEND
);
771 output
->isTransmission
= false;
773 SendMessageToAdapter(output
);
774 bool bWriteOk
= output
->state
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
778 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not write the settings in the EEPROM");
785 bool CUSBCECAdapterCommunication::IsOpen(void)
787 return !IsStopped() && m_port
->IsOpen() && IsRunning();
790 bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage
&message
)
793 bool bTransmitSucceeded(false);
794 uint8_t iPacketsLeft(message
.Size() / 4);
796 int64_t iNow
= GetTimeMs();
797 int64_t iTargetTime
= iNow
+ (message
.transmit_timeout
<= 5 ? CEC_DEFAULT_TRANSMIT_WAIT
: message
.transmit_timeout
);
799 while (!bTransmitSucceeded
&& !bError
&& iNow
< iTargetTime
)
802 CCECAdapterMessage msg
;
809 if (msg
.Message() == MSGCODE_FRAME_START
&& msg
.IsACK())
811 m_processor
->HandlePoll(msg
.Initiator(), msg
.Destination());
812 m_lastInitiator
= msg
.Initiator();
817 if (msg
.Message() == MSGCODE_RECEIVE_FAILED
&&
818 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
819 m_processor
->HandleReceiveFailed(m_lastInitiator
))
825 bError
= msg
.IsError();
828 message
.reply
= msg
.Message();
829 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
833 switch(msg
.Message())
835 case MSGCODE_COMMAND_ACCEPTED
:
836 if (iPacketsLeft
> 0)
838 if (!message
.isTransmission
&& iPacketsLeft
== 0)
839 bTransmitSucceeded
= true;
840 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - waiting for %d more", msg
.ToString().c_str(), iPacketsLeft
);
842 case MSGCODE_TRANSMIT_SUCCEEDED
:
843 CLibCEC::AddLog(CEC_LOG_DEBUG
, msg
.ToString());
844 bTransmitSucceeded
= (iPacketsLeft
== 0);
845 bError
= !bTransmitSucceeded
;
846 message
.reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
849 // ignore other data while waiting
857 message
.state
= bTransmitSucceeded
&& !bError
?
858 ADAPTER_MESSAGE_STATE_SENT_ACKED
:
859 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
;
861 return bTransmitSucceeded
&& !bError
;
864 void CUSBCECAdapterCommunication::AddData(uint8_t *data
, size_t iLen
)
866 CLockObject
lock(m_mutex
);
867 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
871 if (data
[iPtr
] == MSGSTART
)
874 else if (data
[iPtr
] == MSGSTART
) //we found a msgstart before msgend, this is not right, remove
876 if (m_currentAdapterMessage
.Size() > 0)
877 CLibCEC::AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
878 m_currentAdapterMessage
.Clear();
881 else if (data
[iPtr
] == MSGEND
)
883 CCECAdapterMessage
*newMessage
= new CCECAdapterMessage
;
884 newMessage
->packet
= m_currentAdapterMessage
.packet
;
885 m_inBuffer
.Push(newMessage
);
886 m_currentAdapterMessage
.Clear();
888 m_bNextIsEscaped
= false;
890 m_rcvCondition
.Broadcast();
892 else if (m_bNextIsEscaped
)
894 m_currentAdapterMessage
.PushBack(data
[iPtr
] + (uint8_t)ESCOFFSET
);
895 m_bNextIsEscaped
= false;
897 else if (data
[iPtr
] == MSGESC
)
899 m_bNextIsEscaped
= true;
903 m_currentAdapterMessage
.PushBack(data
[iPtr
]);
908 bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout
, size_t iSize
/* = 256 */)
917 CLockObject
lock(m_mutex
);
918 iBytesRead
= m_port
->Read(buff
, sizeof(uint8_t) * iSize
, iTimeout
);
919 if (iBytesRead
< 0 || iBytesRead
> 256)
921 CLibCEC::AddLog(CEC_LOG_ERROR
, "error reading from serial port: %s", m_port
->GetError().c_str());
925 else if (iBytesRead
> 0)
927 AddData(buff
, iBytesRead
);
930 return iBytesRead
> 0;
933 void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
935 CLockObject
adapterLock(m_mutex
);
936 if (!m_port
->IsOpen())
938 CLibCEC::AddLog(CEC_LOG_ERROR
, "error writing to serial port: the connection is closed");
939 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
944 SetLineTimeout(msg
->lineTimeout
);
946 SetLineTimeout(msg
->retryTimeout
);
948 if (m_port
->Write(msg
->packet
.data
, msg
->Size()) != (ssize_t
) msg
->Size())
950 CLibCEC::AddLog(CEC_LOG_ERROR
, "error writing to serial port: %s", m_port
->GetError().c_str());
951 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
955 CLibCEC::AddLog(CEC_LOG_DEBUG
, "command sent");
956 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
958 if (msg
->expectControllerAck
)
960 if (!WaitForAck(*msg
))
961 CLibCEC::AddLog(CEC_LOG_DEBUG
, "did not receive ack");
967 void CUSBCECAdapterCommunication::WriteNextCommand(void)
969 CCECAdapterMessage
*msg(NULL
);
970 if (m_outBuffer
.Pop(msg
))
971 SendMessageToAdapter(msg
);
974 CStdString
CUSBCECAdapterCommunication::GetPortName(void)
977 strName
= m_port
->GetName();