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 CCECAdapterMessage
&CCECAdapterMessage::operator =(const CCECAdapterMessage
&msg
)
92 CStdString
CCECAdapterMessage::MessageCodeAsString(void) const
103 case MSGCODE_TIMEOUT_ERROR
:
106 case MSGCODE_HIGH_ERROR
:
107 strMsg
= "HIGH_ERROR";
109 case MSGCODE_LOW_ERROR
:
110 strMsg
= "LOW_ERROR";
112 case MSGCODE_FRAME_START
:
113 strMsg
= "FRAME_START";
115 case MSGCODE_FRAME_DATA
:
116 strMsg
= "FRAME_DATA";
118 case MSGCODE_RECEIVE_FAILED
:
119 strMsg
= "RECEIVE_FAILED";
121 case MSGCODE_COMMAND_ACCEPTED
:
122 strMsg
= "COMMAND_ACCEPTED";
124 case MSGCODE_COMMAND_REJECTED
:
125 strMsg
= "COMMAND_REJECTED";
127 case MSGCODE_SET_ACK_MASK
:
128 strMsg
= "SET_ACK_MASK";
130 case MSGCODE_TRANSMIT
:
133 case MSGCODE_TRANSMIT_EOM
:
134 strMsg
= "TRANSMIT_EOM";
136 case MSGCODE_TRANSMIT_IDLETIME
:
137 strMsg
= "TRANSMIT_IDLETIME";
139 case MSGCODE_TRANSMIT_ACK_POLARITY
:
140 strMsg
= "TRANSMIT_ACK_POLARITY";
142 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
143 strMsg
= "TRANSMIT_LINE_TIMEOUT";
145 case MSGCODE_TRANSMIT_SUCCEEDED
:
146 strMsg
= "TRANSMIT_SUCCEEDED";
148 case MSGCODE_TRANSMIT_FAILED_LINE
:
149 strMsg
= "TRANSMIT_FAILED_LINE";
151 case MSGCODE_TRANSMIT_FAILED_ACK
:
152 strMsg
= "TRANSMIT_FAILED_ACK";
154 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
155 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
157 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
158 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
160 case MSGCODE_FIRMWARE_VERSION
:
161 strMsg
= "FIRMWARE_VERSION";
163 case MSGCODE_START_BOOTLOADER
:
164 strMsg
= "START_BOOTLOADER";
166 case MSGCODE_FRAME_EOM
:
167 strMsg
= "FRAME_EOM";
169 case MSGCODE_FRAME_ACK
:
170 strMsg
= "FRAME_ACK";
177 CStdString
CCECAdapterMessage::ToString(void) const
180 strMsg
= MessageCodeAsString();
184 case MSGCODE_TIMEOUT_ERROR
:
185 case MSGCODE_HIGH_ERROR
:
186 case MSGCODE_LOW_ERROR
:
188 int iLine
= (size() >= 3) ? (at(1) << 8) | at(2) : 0;
189 uint32_t iTime
= (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
190 strMsg
.AppendFormat(" line:%i", iLine
);
191 strMsg
.AppendFormat(" time:%u", iTime
);
194 case MSGCODE_FRAME_START
:
196 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
198 case MSGCODE_FRAME_DATA
:
200 strMsg
.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
209 bool CCECAdapterMessage::is_error(void) const
211 cec_adapter_messagecode code
= message();
212 return (code
== MSGCODE_TIMEOUT_ERROR
||
213 code
== MSGCODE_HIGH_ERROR
||
214 code
== MSGCODE_LOW_ERROR
||
215 code
== MSGCODE_RECEIVE_FAILED
||
216 code
== MSGCODE_COMMAND_REJECTED
||
217 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
218 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
219 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
220 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
221 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
224 void CCECAdapterMessage::push_escaped(int16_t byte
)
226 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
229 push_back((uint8_t) (byte
- ESCOFFSET
));
232 push_back((uint8_t) byte
);
235 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
237 m_controller(controller
)
239 m_port
= new CSerialPort
;
242 CAdapterCommunication::~CAdapterCommunication(void)
253 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
255 CLockObject
lock(&m_mutex
);
258 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
264 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
267 if (!m_port
->Open(strPort
, iBaudRate
))
270 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
271 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
275 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
277 //clear any input bytes
279 m_port
->Read(buff
, sizeof(buff
), 500);
283 m_startCondition
.Wait(&m_mutex
);
284 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
289 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
295 void CAdapterCommunication::Close(void)
297 CLockObject
lock(&m_mutex
);
298 m_startCondition
.Broadcast();
299 m_rcvCondition
.Broadcast();
303 void *CAdapterCommunication::Process(void)
306 CLockObject
lock(&m_mutex
);
307 m_startCondition
.Signal();
320 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
327 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
328 if (iBytesRead
< 0 || iBytesRead
> 256)
331 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
332 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
335 else if (iBytesRead
> 0)
336 AddData(buff
, (uint8_t) iBytesRead
);
338 return iBytesRead
> 0;
341 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
343 CLockObject
lock(&m_mutex
);
344 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
345 m_inBuffer
.Push(data
[iPtr
]);
347 m_rcvCondition
.Signal();
350 void CAdapterCommunication::WriteNextCommand(void)
352 CCECAdapterMessagePtr msg
;
353 if (m_outBuffer
.Pop(msg
))
355 CLockObject
lock(&msg
->mutex
);
356 if (m_port
->Write(msg
) != (int32_t) msg
->size())
359 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
360 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
361 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
365 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
366 CCondition::Sleep((uint32_t) msg
->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
367 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
369 msg
->condition
.Signal();
373 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data
)
375 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
376 m_outBuffer
.Push(data
);
380 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
382 CLockObject
lock(&m_mutex
);
385 uint64_t iNow
= GetTimeMs();
386 uint64_t iTarget
= iNow
+ iTimeout
;
387 bool bGotFullMessage(false);
388 bool bNextIsEscaped(false);
389 bool bGotStart(false);
391 while(!bGotFullMessage
&& iNow
< iTarget
)
394 if (!m_inBuffer
.Pop(buf
))
396 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
406 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
408 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
415 bGotFullMessage
= true;
417 else if (bNextIsEscaped
)
419 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
420 bNextIsEscaped
= false;
422 else if (buf
== MSGESC
)
423 bNextIsEscaped
= true;
429 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
431 return bGotFullMessage
;
434 std::string
CAdapterCommunication::GetError(void) const
436 return m_port
->GetError();
439 bool CAdapterCommunication::StartBootloader(void)
444 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
445 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
447 output
->push_back(MSGSTART
);
448 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
449 output
->push_back(MSGEND
);
453 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
456 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
460 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
466 strLog
.Format("setting ackmask to %2x", iMask
);
467 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
469 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
471 output
->push_back(MSGSTART
);
472 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
473 output
->push_escaped(iMask
>> 8);
474 output
->push_escaped((uint8_t)iMask
);
475 output
->push_back(MSGEND
);
479 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
486 bool CAdapterCommunication::PingAdapter(void)
491 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
492 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
494 output
->push_back(MSGSTART
);
495 output
->push_escaped(MSGCODE_PING
);
496 output
->push_back(MSGEND
);
500 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
504 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
506 // TODO check for pong
510 bool CAdapterCommunication::IsOpen(void) const
512 return !IsStopped() && m_port
->IsOpen() && IsRunning();