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::ToString(void) const
103 case MSGCODE_TIMEOUT_ERROR
:
104 case MSGCODE_HIGH_ERROR
:
105 case MSGCODE_LOW_ERROR
:
107 if (message() == MSGCODE_TIMEOUT_ERROR
)
109 else if (message() == MSGCODE_HIGH_ERROR
)
110 strMsg
= "HIGH_ERROR";
112 strMsg
= "LOW_ERROR";
114 int iLine
= (size() >= 3) ? (at(1) << 8) | at(2) : 0;
115 uint32_t iTime
= (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
116 strMsg
.AppendFormat(" line:%i", iLine
);
117 strMsg
.AppendFormat(" time:%u", iTime
);
120 case MSGCODE_FRAME_START
:
121 strMsg
= "FRAME_START";
123 strMsg
.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
125 case MSGCODE_FRAME_DATA
:
126 strMsg
= "FRAME_DATA";
128 strMsg
.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
130 case MSGCODE_RECEIVE_FAILED
:
131 strMsg
= "RECEIVE_FAILED";
133 case MSGCODE_COMMAND_ACCEPTED
:
134 strMsg
= "COMMAND_ACCEPTED";
136 case MSGCODE_COMMAND_REJECTED
:
137 strMsg
= "COMMAND_REJECTED";
139 case MSGCODE_SET_ACK_MASK
:
140 strMsg
= "SET_ACK_MASK";
142 case MSGCODE_TRANSMIT
:
145 case MSGCODE_TRANSMIT_EOM
:
146 strMsg
= "TRANSMIT_EOM";
148 case MSGCODE_TRANSMIT_IDLETIME
:
149 strMsg
= "TRANSMIT_IDLETIME";
151 case MSGCODE_TRANSMIT_ACK_POLARITY
:
152 strMsg
= "TRANSMIT_ACK_POLARITY";
154 case MSGCODE_TRANSMIT_LINE_TIMEOUT
:
155 strMsg
= "TRANSMIT_LINE_TIMEOUT";
157 case MSGCODE_TRANSMIT_SUCCEEDED
:
158 strMsg
= "TRANSMIT_SUCCEEDED";
160 case MSGCODE_TRANSMIT_FAILED_LINE
:
161 strMsg
= "TRANSMIT_FAILED_LINE";
163 case MSGCODE_TRANSMIT_FAILED_ACK
:
164 strMsg
= "TRANSMIT_FAILED_ACK";
166 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
:
167 strMsg
= "TRANSMIT_FAILED_TIMEOUT_DATA";
169 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
:
170 strMsg
= "TRANSMIT_FAILED_TIMEOUT_LINE";
172 case MSGCODE_FIRMWARE_VERSION
:
173 strMsg
= "FIRMWARE_VERSION";
175 case MSGCODE_START_BOOTLOADER
:
176 strMsg
= "START_BOOTLOADER";
178 case MSGCODE_FRAME_EOM
:
179 strMsg
= "FRAME_EOM";
181 case MSGCODE_FRAME_ACK
:
182 strMsg
= "FRAME_ACK";
189 bool CCECAdapterMessage::is_error(void) const
191 cec_adapter_messagecode code
= message();
192 return (code
== MSGCODE_TIMEOUT_ERROR
||
193 code
== MSGCODE_HIGH_ERROR
||
194 code
== MSGCODE_LOW_ERROR
||
195 code
== MSGCODE_RECEIVE_FAILED
||
196 code
== MSGCODE_COMMAND_REJECTED
||
197 code
== MSGCODE_TRANSMIT_LINE_TIMEOUT
||
198 code
== MSGCODE_TRANSMIT_FAILED_LINE
||
199 code
== MSGCODE_TRANSMIT_FAILED_ACK
||
200 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA
||
201 code
== MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE
);
204 void CCECAdapterMessage::push_escaped(int16_t byte
)
206 if (byte
>= MSGESC
&& byte
!= MSGSTART
)
209 push_back((uint8_t) (byte
- ESCOFFSET
));
212 push_back((uint8_t) byte
);
215 CAdapterCommunication::CAdapterCommunication(CLibCEC
*controller
) :
217 m_controller(controller
)
219 m_port
= new CSerialPort
;
222 CAdapterCommunication::~CAdapterCommunication(void)
233 bool CAdapterCommunication::Open(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
235 CLockObject
lock(&m_mutex
);
238 m_controller
->AddLog(CEC_LOG_ERROR
, "port is NULL");
244 m_controller
->AddLog(CEC_LOG_ERROR
, "port is already open");
247 if (!m_port
->Open(strPort
, iBaudRate
))
250 strError
.Format("error opening serial port '%s': %s", strPort
, m_port
->GetError().c_str());
251 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
255 m_controller
->AddLog(CEC_LOG_DEBUG
, "connection opened");
257 //clear any input bytes
259 m_port
->Read(buff
, sizeof(buff
), 500);
263 m_startCondition
.Wait(&m_mutex
);
264 m_controller
->AddLog(CEC_LOG_DEBUG
, "communication thread started");
269 m_controller
->AddLog(CEC_LOG_DEBUG
, "could not create a communication thread");
275 void CAdapterCommunication::Close(void)
277 CLockObject
lock(&m_mutex
);
278 m_startCondition
.Broadcast();
279 m_rcvCondition
.Broadcast();
283 void *CAdapterCommunication::Process(void)
286 CLockObject
lock(&m_mutex
);
287 m_startCondition
.Signal();
300 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout
)
307 iBytesRead
= m_port
->Read(buff
, sizeof(buff
), iTimeout
);
308 if (iBytesRead
< 0 || iBytesRead
> 256)
311 strError
.Format("error reading from serial port: %s", m_port
->GetError().c_str());
312 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
315 else if (iBytesRead
> 0)
316 AddData(buff
, (uint8_t) iBytesRead
);
318 return iBytesRead
> 0;
321 void CAdapterCommunication::AddData(uint8_t *data
, uint8_t iLen
)
323 CLockObject
lock(&m_mutex
);
324 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
325 m_inBuffer
.Push(data
[iPtr
]);
327 m_rcvCondition
.Signal();
330 void CAdapterCommunication::WriteNextCommand(void)
332 CCECAdapterMessagePtr msg
;
333 if (m_outBuffer
.Pop(msg
))
335 CLockObject
lock(&msg
->mutex
);
336 if (m_port
->Write(msg
) != (int32_t) msg
->size())
339 strError
.Format("error writing to serial port: %s", m_port
->GetError().c_str());
340 m_controller
->AddLog(CEC_LOG_ERROR
, strError
);
341 msg
->state
= ADAPTER_MESSAGE_STATE_ERROR
;
345 m_controller
->AddLog(CEC_LOG_DEBUG
, "command sent");
346 CCondition::Sleep((uint32_t) msg
->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
347 msg
->state
= ADAPTER_MESSAGE_STATE_SENT
;
349 msg
->condition
.Signal();
353 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data
)
355 data
->state
= ADAPTER_MESSAGE_STATE_WAITING
;
356 m_outBuffer
.Push(data
);
360 bool CAdapterCommunication::Read(CCECAdapterMessage
&msg
, uint32_t iTimeout
)
362 CLockObject
lock(&m_mutex
);
365 uint64_t iNow
= GetTimeMs();
366 uint64_t iTarget
= iNow
+ iTimeout
;
367 bool bGotFullMessage(false);
368 bool bNextIsEscaped(false);
369 bool bGotStart(false);
371 while(!bGotFullMessage
&& iNow
< iTarget
)
374 if (!m_inBuffer
.Pop(buf
))
376 if (!m_rcvCondition
.Wait(&m_mutex
, (uint32_t) (iTarget
- iNow
)))
386 else if (buf
== MSGSTART
) //we found a msgstart before msgend, this is not right, remove
388 m_controller
->AddLog(CEC_LOG_ERROR
, "received MSGSTART before MSGEND");
395 bGotFullMessage
= true;
397 else if (bNextIsEscaped
)
399 msg
.push_back(buf
+ (uint8_t)ESCOFFSET
);
400 bNextIsEscaped
= false;
402 else if (buf
== MSGESC
)
403 bNextIsEscaped
= true;
409 msg
.state
= ADAPTER_MESSAGE_STATE_RECEIVED
;
411 return bGotFullMessage
;
414 std::string
CAdapterCommunication::GetError(void) const
416 return m_port
->GetError();
419 bool CAdapterCommunication::StartBootloader(void)
424 m_controller
->AddLog(CEC_LOG_DEBUG
, "starting the bootloader");
425 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
427 output
->push_back(MSGSTART
);
428 output
->push_escaped(MSGCODE_START_BOOTLOADER
);
429 output
->push_back(MSGEND
);
433 m_controller
->AddLog(CEC_LOG_ERROR
, "could not start the bootloader");
436 m_controller
->AddLog(CEC_LOG_DEBUG
, "bootloader start command transmitted");
440 bool CAdapterCommunication::SetAckMask(uint16_t iMask
)
446 strLog
.Format("setting ackmask to %2x", iMask
);
447 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
449 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
451 output
->push_back(MSGSTART
);
452 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
453 output
->push_escaped(iMask
>> 8);
454 output
->push_escaped((uint8_t)iMask
);
455 output
->push_back(MSGEND
);
459 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
466 bool CAdapterCommunication::PingAdapter(void)
471 m_controller
->AddLog(CEC_LOG_DEBUG
, "sending ping");
472 CCECAdapterMessagePtr
output(new CCECAdapterMessage
);
474 output
->push_back(MSGSTART
);
475 output
->push_escaped(MSGCODE_PING
);
476 output
->push_back(MSGEND
);
480 m_controller
->AddLog(CEC_LOG_ERROR
, "could not send ping command");
484 m_controller
->AddLog(CEC_LOG_DEBUG
, "ping tranmitted");
486 // TODO check for pong
490 bool CAdapterCommunication::IsOpen(void) const
492 return !IsStopped() && m_port
->IsOpen() && IsRunning();