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"
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(MSGCODE_TRANSMIT
);
60 push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
65 push_escaped(command
.parameters
.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
66 push_back((uint8_t) command
.opcode
);
70 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
74 if (iPtr
== command
.parameters
.size
- 1)
75 push_escaped( MSGCODE_TRANSMIT_EOM
);
77 push_escaped(MSGCODE_TRANSMIT
);
79 push_escaped(command
.parameters
[iPtr
]);
85 transmit_timeout
= command
.transmit_timeout
;
88 CCECAdapterMessage
&CCECAdapterMessage::operator =(const CCECAdapterMessage
&msg
)
95 CStdString
CCECAdapterMessage::MessageCodeAsString(void) const
100 case MSGCODE_NOTHING
:
106 case MSGCODE_TIMEOUT_ERROR
:
109 case MSGCODE_HIGH_ERROR
:
110 strMsg
= "HIGH_ERROR";
112 case MSGCODE_LOW_ERROR
:
113 strMsg
= "LOW_ERROR";
115 case MSGCODE_FRAME_START
:
116 strMsg
= "FRAME_START";
118 case MSGCODE_FRAME_DATA
:
119 strMsg
= "FRAME_DATA";
121 case MSGCODE_RECEIVE_FAILED
:
122 strMsg
= "RECEIVE_FAILED";
124 case MSGCODE_COMMAND_ACCEPTED
:
125 strMsg
= "COMMAND_ACCEPTED";
127 case MSGCODE_COMMAND_REJECTED
:
128 strMsg
= "COMMAND_REJECTED";
130 case MSGCODE_SET_ACK_MASK
:
131 strMsg
= "SET_ACK_MASK";
133 case MSGCODE_TRANSMIT
:
136 case MSGCODE_TRANSMIT_EOM
:
137 strMsg
= "TRANSMIT_EOM";
139 case MSGCODE_TRANSMIT_IDLETIME
:
140 strMsg
= "TRANSMIT_IDLETIME";
142 case MSGCODE_TRANSMIT_ACK_POLARITY
:
143 strMsg
= "TRANSMIT_ACK_POLARITY";
145 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
146 strMsg
= "TRANSMIT_LINE_TIMEOUT";
148 case MSGCODE_TRANSMIT_SUCCEEDED
:
149 strMsg
= "TRANSMIT_SUCCEEDED";
151 case MSGCODE_TRANSMIT_FAILED_LINE
:
152 strMsg
= "TRANSMIT_FAILED_LINE";
154 case MSGCODE_TRANSMIT_FAILED_ACK
:
155 strMsg
= "TRANSMIT_FAILED_ACK";
157 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
158 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
160 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
161 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
163 case MSGCODE_FIRMWARE_VERSION
:
164 strMsg
= "FIRMWARE_VERSION";
166 case MSGCODE_START_BOOTLOADER
:
167 strMsg
= "START_BOOTLOADER";
169 case MSGCODE_FRAME_EOM
:
170 strMsg
= "FRAME_EOM";
172 case MSGCODE_FRAME_ACK
:
173 strMsg
= "FRAME_ACK";
180 CStdString
CCECAdapterMessage::ToString(void) const
183 strMsg
= MessageCodeAsString();
187 case MSGCODE_TIMEOUT_ERROR
:
188 case MSGCODE_HIGH_ERROR
:
189 case MSGCODE_LOW_ERROR
:
191 int iLine
= (size() >= 3) ? (at(1) << 8) | at(2) : 0;
192 uint32_t iTime
= (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
193 strMsg
.AppendFormat(" line:%i", iLine
);
194 strMsg
.AppendFormat(" time:%u", iTime
);
197 case MSGCODE_FRAME_START
:
199 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
201 case MSGCODE_FRAME_DATA
:
203 strMsg
.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
212 bool CCECAdapterMessage::is_error(void) const
214 cec_adapter_messagecode code
= message();
215 return (code
== MSGCODE_TIMEOUT_ERROR
||
216 code
== MSGCODE_HIGH_ERROR
||
217 code
== MSGCODE_LOW_ERROR
||
218 code
== MSGCODE_RECEIVE_FAILED
||
219 code
== MSGCODE_COMMAND_REJECTED
||
220 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
221 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
222 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
223 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
224 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
227 void CCECAdapterMessage::push_escaped(int16_t byte
)
229 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
232 push_back((uint8_t) (byte
- ESCOFFSET
));
235 push_back((uint8_t) byte
);
238 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
240 m_controller(controller
)
242 m_port
= new CSerialPort
;
245 CAdapterCommunication::~CAdapterCommunication(void)
256 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
258 CLockObject
lock(&m_mutex
);
261 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
267 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
270 if (!m_port
->Open(strPort
, iBaudRate
))
273 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
274 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
278 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
280 //clear any input bytes
282 m_port
->Read(buff
, sizeof(buff
), 500);
286 m_startCondition
.Wait(&m_mutex
);
287 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
292 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
298 void CAdapterCommunication::Close(void)
300 CLockObject
lock(&m_mutex
);
301 m_startCondition
.Broadcast();
302 m_rcvCondition
.Broadcast();
306 void *CAdapterCommunication::Process(void)
309 CLockObject
lock(&m_mutex
);
310 m_startCondition
.Signal();
323 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
330 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
331 if (iBytesRead
< 0 || iBytesRead
> 256)
334 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
335 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
338 else if (iBytesRead
> 0)
339 AddData(buff
, (uint8_t) iBytesRead
);
341 return iBytesRead
> 0;
344 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
346 CLockObject
lock(&m_mutex
);
347 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
348 m_inBuffer
.Push(data
[iPtr
]);
350 m_rcvCondition
.Signal();
353 void CAdapterCommunication::WriteNextCommand(void)
355 CCECAdapterMessage
*msg
;
356 if (m_outBuffer
.Pop(msg
))
358 CLockObject
lock(&msg
->mutex
);
359 if (m_port
->Write(msg
) != (int32_t) msg
->size())
362 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
363 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
364 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
368 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
369 CCondition::Sleep((uint32_t) msg
->size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 10);
370 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
372 msg
->condition
.Signal();
376 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
378 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
379 m_outBuffer
.Push(data
);
383 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
385 CLockObject
lock(&m_mutex
);
388 uint64_t iNow
= GetTimeMs();
389 uint64_t iTarget
= iNow
+ iTimeout
;
390 bool bGotFullMessage(false);
391 bool bNextIsEscaped(false);
392 bool bGotStart(false);
394 while(!bGotFullMessage
&& iNow
< iTarget
)
397 if (!m_inBuffer
.Pop(buf
))
399 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
409 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
411 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
418 bGotFullMessage
= true;
420 else if (bNextIsEscaped
)
422 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
423 bNextIsEscaped
= false;
425 else if (buf
== MSGESC
)
426 bNextIsEscaped
= true;
432 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
434 return bGotFullMessage
;
437 std::string
CAdapterCommunication::GetError(void) const
439 return m_port
->GetError();
442 bool CAdapterCommunication::StartBootloader(void)
448 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
449 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
451 output
->push_back(MSGSTART
);
452 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
453 output
->push_back(MSGEND
);
455 if ((bReturn
= Write(output
)) == false)
456 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
463 bool CAdapterCommunication::PingAdapter(void)
469 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
470 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
472 output
->push_back(MSGSTART
);
473 output
->push_escaped(MSGCODE_PING
);
474 output
->push_back(MSGEND
);
476 if ((bReturn
= Write(output
)) == false)
477 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
479 // TODO check for pong
485 bool CAdapterCommunication::IsOpen(void) const
487 return !IsStopped() && m_port
->IsOpen() && IsRunning();