2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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 "AdapterCommunication.h"
35 #include "CECProcessor.h"
36 #include "platform/serialport.h"
37 #include "util/StdString.h"
38 #include "platform/timeutils.h"
43 CCECAdapterMessage::CCECAdapterMessage(const cec_command
&command
)
46 maxTries
= command
.retries
+ 1;
48 //set ack polarity to high when transmitting to the broadcast address
49 //set ack polarity low when transmitting to any other address
51 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
52 if (command
.destination
== CECDEVICE_BROADCAST
)
53 push_escaped(CEC_TRUE
);
55 push_escaped(CEC_FALSE
);
58 // add source and destination
60 push_escaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
61 push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
65 if (command
.opcode_set
== 1)
68 push_escaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
69 push_back((uint8_t) command
.opcode
);
73 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
77 if (iPtr
== command
.parameters
.size
- 1)
78 push_escaped( MSGCODE_TRANSMIT_EOM
);
80 push_escaped(MSGCODE_TRANSMIT
);
82 push_escaped(command
.parameters
[iPtr
]);
89 transmit_timeout
= command
.transmit_timeout
;
92 CCECAdapterMessage
&CCECAdapterMessage::operator =(const CCECAdapterMessage
&msg
)
99 CStdString
CCECAdapterMessage::MessageCodeAsString(void) const
104 case MSGCODE_NOTHING
:
110 case MSGCODE_TIMEOUT_ERROR
:
113 case MSGCODE_HIGH_ERROR
:
114 strMsg
= "HIGH_ERROR";
116 case MSGCODE_LOW_ERROR
:
117 strMsg
= "LOW_ERROR";
119 case MSGCODE_FRAME_START
:
120 strMsg
= "FRAME_START";
122 case MSGCODE_FRAME_DATA
:
123 strMsg
= "FRAME_DATA";
125 case MSGCODE_RECEIVE_FAILED
:
126 strMsg
= "RECEIVE_FAILED";
128 case MSGCODE_COMMAND_ACCEPTED
:
129 strMsg
= "COMMAND_ACCEPTED";
131 case MSGCODE_COMMAND_REJECTED
:
132 strMsg
= "COMMAND_REJECTED";
134 case MSGCODE_SET_ACK_MASK
:
135 strMsg
= "SET_ACK_MASK";
137 case MSGCODE_TRANSMIT
:
140 case MSGCODE_TRANSMIT_EOM
:
141 strMsg
= "TRANSMIT_EOM";
143 case MSGCODE_TRANSMIT_IDLETIME
:
144 strMsg
= "TRANSMIT_IDLETIME";
146 case MSGCODE_TRANSMIT_ACK_POLARITY
:
147 strMsg
= "TRANSMIT_ACK_POLARITY";
149 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
150 strMsg
= "TRANSMIT_LINE_TIMEOUT";
152 case MSGCODE_TRANSMIT_SUCCEEDED
:
153 strMsg
= "TRANSMIT_SUCCEEDED";
155 case MSGCODE_TRANSMIT_FAILED_LINE
:
156 strMsg
= "TRANSMIT_FAILED_LINE";
158 case MSGCODE_TRANSMIT_FAILED_ACK
:
159 strMsg
= "TRANSMIT_FAILED_ACK";
161 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
162 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
164 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
165 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
167 case MSGCODE_FIRMWARE_VERSION
:
168 strMsg
= "FIRMWARE_VERSION";
170 case MSGCODE_START_BOOTLOADER
:
171 strMsg
= "START_BOOTLOADER";
173 case MSGCODE_FRAME_EOM
:
174 strMsg
= "FRAME_EOM";
176 case MSGCODE_FRAME_ACK
:
177 strMsg
= "FRAME_ACK";
184 CStdString
CCECAdapterMessage::ToString(void) const
189 strMsg
= "empty message";
193 strMsg
= MessageCodeAsString();
197 case MSGCODE_TIMEOUT_ERROR
:
198 case MSGCODE_HIGH_ERROR
:
199 case MSGCODE_LOW_ERROR
:
201 int iLine
= (size() >= 3) ? (at(1) << 8) | at(2) : 0;
202 uint32_t iTime
= (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
203 strMsg
.AppendFormat(" line:%i", iLine
);
204 strMsg
.AppendFormat(" time:%u", iTime
);
207 case MSGCODE_FRAME_START
:
209 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
211 case MSGCODE_FRAME_DATA
:
213 strMsg
.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
223 bool CCECAdapterMessage::is_error(void) const
225 cec_adapter_messagecode code
= message();
226 return (code
== MSGCODE_HIGH_ERROR
||
227 code
== MSGCODE_LOW_ERROR
||
228 code
== MSGCODE_RECEIVE_FAILED
||
229 code
== MSGCODE_COMMAND_REJECTED
||
230 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
231 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
232 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
233 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
234 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
237 void CCECAdapterMessage::push_escaped(uint8_t byte
)
242 push_back(byte
- ESCOFFSET
);
248 CAdapterCommunication::CAdapterCommunication(CCECProcessor
*processor
) :
250 m_processor(processor
),
253 m_port
= new CSerialPort
;
256 CAdapterCommunication::~CAdapterCommunication(void)
267 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
269 CLockObject
lock(&m_mutex
);
272 m_processor
->AddLog(CEC_LOG_ERROR
, "port is NULL");
278 m_processor
->AddLog(CEC_LOG_ERROR
, "port is already open");
281 if (!m_port
->Open(strPort
, iBaudRate
))
284 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
285 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
289 m_processor
->AddLog(CEC_LOG_DEBUG
, "connection opened");
291 //clear any input bytes
293 while (m_port
->Read(buff
, sizeof(buff
), 1000) > 0) {}
297 m_startCondition
.Wait(&m_mutex
);
298 m_processor
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
303 m_processor
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
309 void CAdapterCommunication::Close(void)
311 CLockObject
lock(&m_mutex
);
312 m_startCondition
.Broadcast();
313 m_rcvCondition
.Broadcast();
317 void *CAdapterCommunication::Process(void)
320 CLockObject
lock(&m_mutex
);
321 m_startCondition
.Signal();
334 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
341 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
342 if (iBytesRead
< 0 || iBytesRead
> 256)
345 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
346 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
349 else if (iBytesRead
> 0)
350 AddData(buff
, (uint8_t) iBytesRead
);
352 return iBytesRead
> 0;
355 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
357 CLockObject
lock(&m_mutex
);
358 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
359 m_inBuffer
.Push(data
[iPtr
]);
361 m_rcvCondition
.Signal();
364 void CAdapterCommunication::WriteNextCommand(void)
366 CCECAdapterMessage
*msg
;
367 if (m_outBuffer
.Pop(msg
))
368 SendMessageToAdapter(msg
);
371 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
373 CLockObject
lock(&msg
->mutex
);
374 if (m_port
->Write(msg
) != (int32_t) msg
->size())
377 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
378 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
379 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
383 m_processor
->AddLog(CEC_LOG_DEBUG
, "command sent");
384 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
386 msg
->condition
.Signal();
389 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
391 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
392 m_outBuffer
.Push(data
);
396 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
398 CLockObject
lock(&m_mutex
);
401 uint64_t iNow
= GetTimeMs();
402 uint64_t iTarget
= iNow
+ iTimeout
;
403 bool bGotFullMessage(false);
404 bool bNextIsEscaped(false);
405 bool bGotStart(false);
407 while(!bGotFullMessage
&& iNow
< iTarget
)
410 if (!m_inBuffer
.Pop(buf
))
412 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
422 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
425 m_processor
->AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
432 bGotFullMessage
= true;
434 else if (bNextIsEscaped
)
436 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
437 bNextIsEscaped
= false;
439 else if (buf
== MSGESC
)
440 bNextIsEscaped
= true;
446 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
448 return bGotFullMessage
;
451 std::string
CAdapterCommunication::GetError(void) const
453 return m_port
->GetError();
456 bool CAdapterCommunication::StartBootloader(void)
462 m_processor
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
463 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
465 output
->push_back(MSGSTART
);
466 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
467 output
->push_back(MSGEND
);
469 CLockObject
lock(&output
->mutex
);
471 output
->condition
.Wait(&output
->mutex
);
472 bReturn
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
478 bool CAdapterCommunication::PingAdapter(void)
484 m_processor
->AddLog(CEC_LOG_DEBUG
, "sending ping");
485 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
487 output
->push_back(MSGSTART
);
488 output
->push_escaped(MSGCODE_PING
);
489 output
->push_back(MSGEND
);
491 CLockObject
lock(&output
->mutex
);
493 output
->condition
.Wait(&output
->mutex
);
494 bReturn
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
500 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
502 bool bReturn(m_iLineTimeout
!= iTimeout
);
506 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
508 output
->push_back(MSGSTART
);
509 output
->push_escaped(MSGCODE_TRANSMIT_IDLETIME
);
510 output
->push_escaped(iTimeout
);
511 output
->push_back(MSGEND
);
513 if ((bReturn
= Write(output
)) == false)
514 m_processor
->AddLog(CEC_LOG_ERROR
, "could not set the idletime");
521 bool CAdapterCommunication::IsOpen(void) const
523 return !IsStopped() && m_port
->IsOpen() && IsRunning();