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/os-dependent.h"
37 #include "platform/serialport.h"
42 CCECAdapterMessage::CCECAdapterMessage(const cec_command
&command
)
46 //set ack polarity to high when transmitting to the broadcast address
47 //set ack polarity low when transmitting to any other address
49 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY
);
50 if (command
.destination
== CECDEVICE_BROADCAST
)
51 push_escaped(CEC_TRUE
);
53 push_escaped(CEC_FALSE
);
56 // add source and destination
58 push_escaped(command
.opcode_set
== 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
59 push_back(((uint8_t)command
.initiator
<< 4) + (uint8_t)command
.destination
);
63 if (command
.opcode_set
== 1)
66 push_escaped(command
.parameters
.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM
: (uint8_t)MSGCODE_TRANSMIT
);
67 push_back((uint8_t) command
.opcode
);
71 for (int8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
75 if (iPtr
== command
.parameters
.size
- 1)
76 push_escaped( MSGCODE_TRANSMIT_EOM
);
78 push_escaped(MSGCODE_TRANSMIT
);
80 push_escaped(command
.parameters
[iPtr
]);
87 transmit_timeout
= command
.transmit_timeout
;
90 CCECAdapterMessage
&CCECAdapterMessage::operator =(const CCECAdapterMessage
&msg
)
97 CStdString
CCECAdapterMessage::MessageCodeAsString(void) const
102 case MSGCODE_NOTHING
:
108 case MSGCODE_TIMEOUT_ERROR
:
111 case MSGCODE_HIGH_ERROR
:
112 strMsg
= "HIGH_ERROR";
114 case MSGCODE_LOW_ERROR
:
115 strMsg
= "LOW_ERROR";
117 case MSGCODE_FRAME_START
:
118 strMsg
= "FRAME_START";
120 case MSGCODE_FRAME_DATA
:
121 strMsg
= "FRAME_DATA";
123 case MSGCODE_RECEIVE_FAILED
:
124 strMsg
= "RECEIVE_FAILED";
126 case MSGCODE_COMMAND_ACCEPTED
:
127 strMsg
= "COMMAND_ACCEPTED";
129 case MSGCODE_COMMAND_REJECTED
:
130 strMsg
= "COMMAND_REJECTED";
132 case MSGCODE_SET_ACK_MASK
:
133 strMsg
= "SET_ACK_MASK";
135 case MSGCODE_TRANSMIT
:
138 case MSGCODE_TRANSMIT_EOM
:
139 strMsg
= "TRANSMIT_EOM";
141 case MSGCODE_TRANSMIT_IDLETIME
:
142 strMsg
= "TRANSMIT_IDLETIME";
144 case MSGCODE_TRANSMIT_ACK_POLARITY
:
145 strMsg
= "TRANSMIT_ACK_POLARITY";
147 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
148 strMsg
= "TRANSMIT_LINE_TIMEOUT";
150 case MSGCODE_TRANSMIT_SUCCEEDED
:
151 strMsg
= "TRANSMIT_SUCCEEDED";
153 case MSGCODE_TRANSMIT_FAILED_LINE
:
154 strMsg
= "TRANSMIT_FAILED_LINE";
156 case MSGCODE_TRANSMIT_FAILED_ACK
:
157 strMsg
= "TRANSMIT_FAILED_ACK";
159 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
160 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
162 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
163 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
165 case MSGCODE_FIRMWARE_VERSION
:
166 strMsg
= "FIRMWARE_VERSION";
168 case MSGCODE_START_BOOTLOADER
:
169 strMsg
= "START_BOOTLOADER";
171 case MSGCODE_FRAME_EOM
:
172 strMsg
= "FRAME_EOM";
174 case MSGCODE_FRAME_ACK
:
175 strMsg
= "FRAME_ACK";
182 CStdString
CCECAdapterMessage::ToString(void) const
187 strMsg
= "empty message";
191 strMsg
= MessageCodeAsString();
195 case MSGCODE_TIMEOUT_ERROR
:
196 case MSGCODE_HIGH_ERROR
:
197 case MSGCODE_LOW_ERROR
:
199 uint32_t iLine
= (size() >= 3) ? (at(1) << 8) | at(2) : 0;
200 uint32_t iTime
= (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
201 strMsg
.AppendFormat(" line:%u", iLine
);
202 strMsg
.AppendFormat(" time:%u", iTime
);
205 case MSGCODE_FRAME_START
:
207 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
209 case MSGCODE_FRAME_DATA
:
211 strMsg
.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
221 bool CCECAdapterMessage::is_error(void) const
223 cec_adapter_messagecode code
= message();
224 return (code
== MSGCODE_HIGH_ERROR
||
225 code
== MSGCODE_LOW_ERROR
||
226 code
== MSGCODE_RECEIVE_FAILED
||
227 code
== MSGCODE_COMMAND_REJECTED
||
228 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
229 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
230 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
231 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
232 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
235 void CCECAdapterMessage::push_escaped(uint8_t byte
)
240 push_back(byte
- ESCOFFSET
);
246 CAdapterCommunication::CAdapterCommunication(CCECProcessor
*processor
) :
248 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 uint64_t iNow
= GetTimeMs();
268 uint64_t iTimeout
= iNow
+ iTimeoutMs
;
270 CLockObject
lock(&m_mutex
);
274 m_processor
->AddLog(CEC_LOG_ERROR
, "port is NULL");
280 m_processor
->AddLog(CEC_LOG_ERROR
, "port is already open");
285 bool bConnected(false);
286 while (!bConnected
&& iNow
< iTimeout
)
288 if ((bConnected
= m_port
->Open(strPort
, iBaudRate
)) == false)
290 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
298 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
302 m_processor
->AddLog(CEC_LOG_DEBUG
, "connection opened");
304 //clear any input bytes
306 while (m_port
->Read(buff
, 1, 5) == 1) {}
310 m_startCondition
.Wait(&m_mutex
);
311 m_processor
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
316 m_processor
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
322 void CAdapterCommunication::Close(void)
324 CLockObject
lock(&m_mutex
);
325 m_startCondition
.Broadcast();
326 m_rcvCondition
.Broadcast();
330 void *CAdapterCommunication::Process(void)
333 CLockObject
lock(&m_mutex
);
334 m_startCondition
.Signal();
344 CCECAdapterMessage
*msg
;
345 if (m_outBuffer
.Pop(msg
))
346 msg
->condition
.Broadcast();
351 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
358 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
359 if (iBytesRead
< 0 || iBytesRead
> 256)
362 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
363 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
366 else if (iBytesRead
> 0)
367 AddData(buff
, (uint8_t) iBytesRead
);
369 return iBytesRead
> 0;
372 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
374 CLockObject
lock(&m_mutex
);
375 for (uint8_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
376 m_inBuffer
.Push(data
[iPtr
]);
378 m_rcvCondition
.Signal();
381 void CAdapterCommunication::WriteNextCommand(void)
383 CCECAdapterMessage
*msg
;
384 if (m_outBuffer
.Pop(msg
))
385 SendMessageToAdapter(msg
);
388 void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage
*msg
)
390 CLockObject
lock(&msg
->mutex
);
391 if (m_port
->Write(msg
) != (int32_t) msg
->size())
394 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
395 m_processor
->AddLog(CEC_LOG_ERROR
, strError
);
396 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
400 m_processor
->AddLog(CEC_LOG_DEBUG
, "command sent");
401 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
403 msg
->condition
.Signal();
406 bool CAdapterCommunication::Write(CCECAdapterMessage
*data
)
408 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
409 m_outBuffer
.Push(data
);
413 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
415 CLockObject
lock(&m_mutex
);
418 uint64_t iNow
= GetTimeMs();
419 uint64_t iTarget
= iNow
+ iTimeout
;
420 bool bGotFullMessage(false);
421 bool bNextIsEscaped(false);
422 bool bGotStart(false);
424 while(!bGotFullMessage
&& iNow
< iTarget
)
427 if (!m_inBuffer
.Pop(buf
))
429 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
439 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
442 m_processor
->AddLog(CEC_LOG_WARNING
, "received MSGSTART before MSGEND, removing previous buffer contents");
449 bGotFullMessage
= true;
451 else if (bNextIsEscaped
)
453 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
454 bNextIsEscaped
= false;
456 else if (buf
== MSGESC
)
457 bNextIsEscaped
= true;
463 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
465 return bGotFullMessage
;
468 std::string
CAdapterCommunication::GetError(void) const
470 return m_port
->GetError();
473 bool CAdapterCommunication::StartBootloader(void)
479 m_processor
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
480 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
482 output
->push_back(MSGSTART
);
483 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
484 output
->push_back(MSGEND
);
486 CLockObject
lock(&output
->mutex
);
488 output
->condition
.Wait(&output
->mutex
);
489 bReturn
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
495 bool CAdapterCommunication::PingAdapter(void)
501 m_processor
->AddLog(CEC_LOG_DEBUG
, "sending ping");
502 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
504 output
->push_back(MSGSTART
);
505 output
->push_escaped(MSGCODE_PING
);
506 output
->push_back(MSGEND
);
508 CLockObject
lock(&output
->mutex
);
510 output
->condition
.Wait(&output
->mutex
);
511 bReturn
= output
->state
== ADAPTER_MESSAGE_STATE_SENT
;
517 bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout
)
519 bool bReturn(m_iLineTimeout
!= iTimeout
);
523 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
525 output
->push_back(MSGSTART
);
526 output
->push_escaped(MSGCODE_TRANSMIT_IDLETIME
);
527 output
->push_escaped(iTimeout
);
528 output
->push_back(MSGEND
);
530 if ((bReturn
= Write(output
)) == false)
531 m_processor
->AddLog(CEC_LOG_ERROR
, "could not set the idletime");
538 bool CAdapterCommunication::IsOpen(void) const
540 return !IsStopped() && m_port
->IsOpen() && IsRunning();