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
)
47 //set ack polarity to high when transmitting to the broadcast address
48 //set ack polarity low when transmitting to any other address
50 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
51 if (command
.destination
== CECDEVICE_BROADCAST
)
52 push_escaped(CEC_TRUE
);
54 push_escaped(CEC_FALSE
);
57 // add source and destination
59 push_escaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
60 push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
64 if (command
.opcode_set
== 1)
67 push_escaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
68 push_back((uint8_t) command
.opcode
);
72 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
76 if (iPtr
== command
.parameters
.size
- 1)
77 push_escaped( MSGCODE_TRANSMIT_EOM
);
79 push_escaped(MSGCODE_TRANSMIT
);
81 push_escaped(command
.parameters
[iPtr
]);
88 transmit_timeout
= command
.transmit_timeout
;
91 CCECAdapterMessage
&CCECAdapterMessage::operator =(const CCECAdapterMessage
&msg
)
98 CStdString
CCECAdapterMessage::MessageCodeAsString(void) const
103 case MSGCODE_NOTHING
:
109 case MSGCODE_TIMEOUT_ERROR
:
112 case MSGCODE_HIGH_ERROR
:
113 strMsg
= "HIGH_ERROR";
115 case MSGCODE_LOW_ERROR
:
116 strMsg
= "LOW_ERROR";
118 case MSGCODE_FRAME_START
:
119 strMsg
= "FRAME_START";
121 case MSGCODE_FRAME_DATA
:
122 strMsg
= "FRAME_DATA";
124 case MSGCODE_RECEIVE_FAILED
:
125 strMsg
= "RECEIVE_FAILED";
127 case MSGCODE_COMMAND_ACCEPTED
:
128 strMsg
= "COMMAND_ACCEPTED";
130 case MSGCODE_COMMAND_REJECTED
:
131 strMsg
= "COMMAND_REJECTED";
133 case MSGCODE_SET_ACK_MASK
:
134 strMsg
= "SET_ACK_MASK";
136 case MSGCODE_TRANSMIT
:
139 case MSGCODE_TRANSMIT_EOM
:
140 strMsg
= "TRANSMIT_EOM";
142 case MSGCODE_TRANSMIT_IDLETIME
:
143 strMsg
= "TRANSMIT_IDLETIME";
145 case MSGCODE_TRANSMIT_ACK_POLARITY
:
146 strMsg
= "TRANSMIT_ACK_POLARITY";
148 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
149 strMsg
= "TRANSMIT_LINE_TIMEOUT";
151 case MSGCODE_TRANSMIT_SUCCEEDED
:
152 strMsg
= "TRANSMIT_SUCCEEDED";
154 case MSGCODE_TRANSMIT_FAILED_LINE
:
155 strMsg
= "TRANSMIT_FAILED_LINE";
157 case MSGCODE_TRANSMIT_FAILED_ACK
:
158 strMsg
= "TRANSMIT_FAILED_ACK";
160 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
161 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
163 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
164 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
166 case MSGCODE_FIRMWARE_VERSION
:
167 strMsg
= "FIRMWARE_VERSION";
169 case MSGCODE_START_BOOTLOADER
:
170 strMsg
= "START_BOOTLOADER";
172 case MSGCODE_FRAME_EOM
:
173 strMsg
= "FRAME_EOM";
175 case MSGCODE_FRAME_ACK
:
176 strMsg
= "FRAME_ACK";
183 CStdString
CCECAdapterMessage::ToString(void) const
188 strMsg
= "empty message";
192 strMsg
= MessageCodeAsString();
196 case MSGCODE_TIMEOUT_ERROR
:
197 case MSGCODE_HIGH_ERROR
:
198 case MSGCODE_LOW_ERROR
:
200 int iLine
= (size() >= 3) ? (at(1) << 8) | at(2) : 0;
201 uint32_t iTime
= (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
202 strMsg
.AppendFormat(" line:%i", iLine
);
203 strMsg
.AppendFormat(" time:%u", iTime
);
206 case MSGCODE_FRAME_START
:
208 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
210 case MSGCODE_FRAME_DATA
:
212 strMsg
.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
222 bool CCECAdapterMessage::is_error(void) const
224 cec_adapter_messagecode code
= message();
225 return (code
== MSGCODE_HIGH_ERROR
||
226 code
== MSGCODE_LOW_ERROR
||
227 code
== MSGCODE_RECEIVE_FAILED
||
228 code
== MSGCODE_COMMAND_REJECTED
||
229 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
230 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
231 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
232 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
233 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
236 void CCECAdapterMessage::push_escaped(uint8_t byte
)
241 push_back(byte
- ESCOFFSET
);
247 CAdapterCommunication::CAdapterCommunication(CCECProcessor
*processor
) :
249 m_processor(processor
)
251 m_port
= new CSerialPort
;
254 CAdapterCommunication::~CAdapterCommunication(void)
265 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
267 CLockObject
lock(&m_mutex
);
270 m_processor
->AddLog(CEC_LOG_ERROR
, "port is NULL");
276 m_processor
->AddLog(CEC_LOG_ERROR
, "port is already open");
279 if (!m_port
->Open(strPort
, iBaudRate
))
282 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
283 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
287 m_processor
->AddLog(CEC_LOG_DEBUG
, "connection opened");
289 //clear any input bytes
291 while (m_port
->Read(buff
, sizeof(buff
), 500) > 0) {}
295 m_startCondition
.Wait(&m_mutex
);
296 m_processor
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
301 m_processor
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
307 void CAdapterCommunication::Close(void)
309 CLockObject
lock(&m_mutex
);
310 m_startCondition
.Broadcast();
311 m_rcvCondition
.Broadcast();
315 void *CAdapterCommunication::Process(void)
318 CLockObject
lock(&m_mutex
);
319 m_startCondition
.Signal();
332 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
339 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
340 if (iBytesRead
< 0 || iBytesRead
> 256)
343 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
344 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
347 else if (iBytesRead
> 0)
348 AddData(buff
, (uint8_t) iBytesRead
);
350 return iBytesRead
> 0;
353 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
355 CLockObject
lock(&m_mutex
);
356 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
357 m_inBuffer
.Push(data
[iPtr
]);
359 m_rcvCondition
.Signal();
362 void CAdapterCommunication::WriteNextCommand(void)
364 CCECAdapterMessage
*msg
;
365 if (m_outBuffer
.Pop(msg
))
366 SendMessageToAdapter(msg
);
369 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
371 CLockObject
lock(&msg
->mutex
);
372 if (m_port
->Write(msg
) != (int32_t) msg
->size())
375 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
376 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
377 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
381 m_processor
->AddLog(CEC_LOG_DEBUG
, "command sent");
382 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
384 msg
->condition
.Signal();
387 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
389 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
390 m_outBuffer
.Push(data
);
394 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
396 CLockObject
lock(&m_mutex
);
399 uint64_t iNow
= GetTimeMs();
400 uint64_t iTarget
= iNow
+ iTimeout
;
401 bool bGotFullMessage(false);
402 bool bNextIsEscaped(false);
403 bool bGotStart(false);
405 while(!bGotFullMessage
&& iNow
< iTarget
)
408 if (!m_inBuffer
.Pop(buf
))
410 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
420 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
423 m_processor
->AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
430 bGotFullMessage
= true;
432 else if (bNextIsEscaped
)
434 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
435 bNextIsEscaped
= false;
437 else if (buf
== MSGESC
)
438 bNextIsEscaped
= true;
444 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
446 return bGotFullMessage
;
449 std::string
CAdapterCommunication::GetError(void) const
451 return m_port
->GetError();
454 bool CAdapterCommunication::StartBootloader(void)
460 m_processor
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
461 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
463 output
->push_back(MSGSTART
);
464 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
465 output
->push_back(MSGEND
);
467 CLockObject
lock(&output
->mutex
);
469 output
->condition
.Wait(&output
->mutex
);
470 bReturn
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
476 bool CAdapterCommunication::PingAdapter(void)
482 m_processor
->AddLog(CEC_LOG_DEBUG
, "sending ping");
483 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
485 output
->push_back(MSGSTART
);
486 output
->push_escaped(MSGCODE_PING
);
487 output
->push_back(MSGEND
);
489 CLockObject
lock(&output
->mutex
);
491 output
->condition
.Wait(&output
->mutex
);
492 bReturn
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
498 bool CAdapterCommunication::IsOpen(void) const
500 return !IsStopped() && m_port
->IsOpen() && IsRunning();