cec: fix power on of LG devices
[deb_libcec.git] / src / lib / AdapterCommunication.cpp
CommitLineData
a8f0bd18
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
828682d3 33#include "AdapterCommunication.h"
2abe74eb 34
1113cb7d 35#include "CECProcessor.h"
b9187cc6 36#include "platform/serialport.h"
a8f0bd18 37#include "util/StdString.h"
50aa01e6 38#include "platform/timeutils.h"
a8f0bd18
LOK
39
40using namespace std;
41using namespace CEC;
42
ad24cbaf
LOK
43CCECAdapterMessage::CCECAdapterMessage(const cec_command &command)
44{
45 clear();
9b223b29 46 maxTries = command.retries + 1;
ad24cbaf
LOK
47
48 //set ack polarity to high when transmitting to the broadcast address
49 //set ack polarity low when transmitting to any other address
50 push_back(MSGSTART);
51 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY);
52 if (command.destination == CECDEVICE_BROADCAST)
53 push_escaped(CEC_TRUE);
54 else
55 push_escaped(CEC_FALSE);
56 push_back(MSGEND);
57
58 // add source and destination
59 push_back(MSGSTART);
57f45e6c 60 push_escaped(command.opcode_set == 0 ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
ad24cbaf
LOK
61 push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
62 push_back(MSGEND);
63
64 // add opcode
57f45e6c 65 if (command.opcode_set == 1)
ad24cbaf
LOK
66 {
67 push_back(MSGSTART);
ab1469a0 68 push_escaped(command.parameters.IsEmpty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
57f45e6c
LOK
69 push_back((uint8_t) command.opcode);
70 push_back(MSGEND);
ad24cbaf 71
57f45e6c
LOK
72 // add parameters
73 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
74 {
75 push_back(MSGSTART);
ad24cbaf 76
57f45e6c
LOK
77 if (iPtr == command.parameters.size - 1)
78 push_escaped( MSGCODE_TRANSMIT_EOM);
79 else
80 push_escaped(MSGCODE_TRANSMIT);
ad24cbaf 81
57f45e6c
LOK
82 push_escaped(command.parameters[iPtr]);
83
84 push_back(MSGEND);
85 }
ad24cbaf 86 }
06bfd4d7
LOK
87
88 // set timeout
89 transmit_timeout = command.transmit_timeout;
ad24cbaf
LOK
90}
91
92CCECAdapterMessage &CCECAdapterMessage::operator =(const CCECAdapterMessage &msg)
93{
94 packet = msg.packet;
88c43521 95 state = msg.state;
ad24cbaf
LOK
96 return *this;
97}
98
b8c91906 99CStdString CCECAdapterMessage::MessageCodeAsString(void) const
e41b06f9
LOK
100{
101 CStdString strMsg;
102 switch (message())
103 {
104 case MSGCODE_NOTHING:
105 strMsg = "NOTHING";
106 break;
107 case MSGCODE_PING:
108 strMsg = "PING";
109 break;
110 case MSGCODE_TIMEOUT_ERROR:
b8c91906
LOK
111 strMsg = "TIMEOUT";
112 break;
e41b06f9 113 case MSGCODE_HIGH_ERROR:
b8c91906
LOK
114 strMsg = "HIGH_ERROR";
115 break;
e41b06f9 116 case MSGCODE_LOW_ERROR:
b8c91906 117 strMsg = "LOW_ERROR";
e41b06f9
LOK
118 break;
119 case MSGCODE_FRAME_START:
120 strMsg = "FRAME_START";
e41b06f9
LOK
121 break;
122 case MSGCODE_FRAME_DATA:
123 strMsg = "FRAME_DATA";
e41b06f9
LOK
124 break;
125 case MSGCODE_RECEIVE_FAILED:
126 strMsg = "RECEIVE_FAILED";
127 break;
128 case MSGCODE_COMMAND_ACCEPTED:
129 strMsg = "COMMAND_ACCEPTED";
130 break;
131 case MSGCODE_COMMAND_REJECTED:
132 strMsg = "COMMAND_REJECTED";
133 break;
134 case MSGCODE_SET_ACK_MASK:
135 strMsg = "SET_ACK_MASK";
136 break;
137 case MSGCODE_TRANSMIT:
138 strMsg = "TRANSMIT";
139 break;
140 case MSGCODE_TRANSMIT_EOM:
141 strMsg = "TRANSMIT_EOM";
142 break;
143 case MSGCODE_TRANSMIT_IDLETIME:
144 strMsg = "TRANSMIT_IDLETIME";
145 break;
146 case MSGCODE_TRANSMIT_ACK_POLARITY:
147 strMsg = "TRANSMIT_ACK_POLARITY";
148 break;
149 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
150 strMsg = "TRANSMIT_LINE_TIMEOUT";
151 break;
152 case MSGCODE_TRANSMIT_SUCCEEDED:
153 strMsg = "TRANSMIT_SUCCEEDED";
154 break;
155 case MSGCODE_TRANSMIT_FAILED_LINE:
156 strMsg = "TRANSMIT_FAILED_LINE";
157 break;
158 case MSGCODE_TRANSMIT_FAILED_ACK:
159 strMsg = "TRANSMIT_FAILED_ACK";
160 break;
161 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
162 strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA";
163 break;
164 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
165 strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE";
166 break;
167 case MSGCODE_FIRMWARE_VERSION:
168 strMsg = "FIRMWARE_VERSION";
169 break;
170 case MSGCODE_START_BOOTLOADER:
171 strMsg = "START_BOOTLOADER";
172 break;
173 case MSGCODE_FRAME_EOM:
174 strMsg = "FRAME_EOM";
175 break;
176 case MSGCODE_FRAME_ACK:
177 strMsg = "FRAME_ACK";
178 break;
179 }
180
181 return strMsg;
182}
183
b8c91906
LOK
184CStdString CCECAdapterMessage::ToString(void) const
185{
186 CStdString strMsg;
fd5e333a 187 if (size() == 0)
b8c91906 188 {
fd5e333a
LOK
189 strMsg = "empty message";
190 }
191 else
192 {
193 strMsg = MessageCodeAsString();
194
195 switch (message())
b8c91906 196 {
fd5e333a
LOK
197 case MSGCODE_TIMEOUT_ERROR:
198 case MSGCODE_HIGH_ERROR:
199 case MSGCODE_LOW_ERROR:
200 {
201 int iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0;
202 uint32_t iTime = (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
203 strMsg.AppendFormat(" line:%i", iLine);
204 strMsg.AppendFormat(" time:%u", iTime);
205 }
206 break;
207 case MSGCODE_FRAME_START:
208 if (size() >= 2)
209 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
210 break;
211 case MSGCODE_FRAME_DATA:
212 if (size() >= 2)
213 strMsg.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
214 break;
215 default:
216 break;
b8c91906 217 }
b8c91906
LOK
218 }
219
220 return strMsg;
221}
222
e41b06f9
LOK
223bool CCECAdapterMessage::is_error(void) const
224{
225 cec_adapter_messagecode code = message();
855a3a98 226 return (code == MSGCODE_HIGH_ERROR ||
e41b06f9
LOK
227 code == MSGCODE_LOW_ERROR ||
228 code == MSGCODE_RECEIVE_FAILED ||
229 code == MSGCODE_COMMAND_REJECTED ||
ae693aaa 230 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
e41b06f9 231 code == MSGCODE_TRANSMIT_FAILED_LINE ||
ae693aaa 232 code == MSGCODE_TRANSMIT_FAILED_ACK ||
e41b06f9
LOK
233 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
234 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
235}
236
c7903c02 237void CCECAdapterMessage::push_escaped(uint8_t byte)
ad24cbaf 238{
a96eb42e 239 if (byte >= MSGESC)
ad24cbaf
LOK
240 {
241 push_back(MSGESC);
c7903c02 242 push_back(byte - ESCOFFSET);
ad24cbaf
LOK
243 }
244 else
c7903c02 245 push_back(byte);
ad24cbaf
LOK
246}
247
1113cb7d 248CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) :
12027dbe 249 m_port(NULL),
a171d2fd
LOK
250 m_processor(processor),
251 m_iLineTimeout(0)
a8f0bd18
LOK
252{
253 m_port = new CSerialPort;
254}
255
828682d3 256CAdapterCommunication::~CAdapterCommunication(void)
a8f0bd18 257{
12027dbe
LOK
258 Close();
259
260 if (m_port)
261 {
262 delete m_port;
263 m_port = NULL;
264 }
a8f0bd18
LOK
265}
266
25701fa6 267bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
a8f0bd18 268{
f077e8ed 269 CLockObject lock(&m_mutex);
13fd6a66
LOK
270 if (!m_port)
271 {
1113cb7d 272 m_processor->AddLog(CEC_LOG_ERROR, "port is NULL");
a8f0bd18 273 return false;
13fd6a66
LOK
274 }
275
276 if (IsOpen())
277 {
1113cb7d 278 m_processor->AddLog(CEC_LOG_ERROR, "port is already open");
13fd6a66 279 }
a8f0bd18
LOK
280
281 if (!m_port->Open(strPort, iBaudRate))
282 {
283 CStdString strError;
284 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
1113cb7d 285 m_processor->AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
286 return false;
287 }
288
1113cb7d 289 m_processor->AddLog(CEC_LOG_DEBUG, "connection opened");
a8f0bd18
LOK
290
291 //clear any input bytes
aee82ba0
LOK
292 uint8_t buff[1];
293 while (m_port->Read(buff, 1, 5) == 1) {}
a8f0bd18 294
828682d3 295 if (CreateThread())
a8f0bd18 296 {
b9eea66d 297 m_startCondition.Wait(&m_mutex);
1113cb7d 298 m_processor->AddLog(CEC_LOG_DEBUG, "communication thread started");
a8f0bd18
LOK
299 return true;
300 }
301 else
302 {
1113cb7d 303 m_processor->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
a8f0bd18
LOK
304 }
305
306 return false;
307}
308
828682d3 309void CAdapterCommunication::Close(void)
a8f0bd18 310{
f077e8ed 311 CLockObject lock(&m_mutex);
b9eea66d 312 m_startCondition.Broadcast();
d5bffd3c 313 m_rcvCondition.Broadcast();
b9eea66d 314 StopThread();
a8f0bd18
LOK
315}
316
828682d3 317void *CAdapterCommunication::Process(void)
a8f0bd18 318{
b9eea66d
LOK
319 {
320 CLockObject lock(&m_mutex);
321 m_startCondition.Signal();
322 }
12027dbe 323
13fd6a66 324 while (!IsStopped())
a8f0bd18 325 {
9e1cfa5c 326 ReadFromDevice(50);
c02980af 327 Sleep(5);
3c53ac93 328 WriteNextCommand();
a8f0bd18
LOK
329 }
330
a8f0bd18
LOK
331 return NULL;
332}
333
25701fa6 334bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
a8f0bd18 335{
25701fa6 336 int32_t iBytesRead;
13fd6a66
LOK
337 uint8_t buff[1024];
338 if (!m_port)
339 return false;
b6c82769 340
13fd6a66
LOK
341 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
342 if (iBytesRead < 0 || iBytesRead > 256)
a8f0bd18 343 {
13fd6a66
LOK
344 CStdString strError;
345 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
1113cb7d 346 m_processor->AddLog(CEC_LOG_ERROR, strError);
13fd6a66 347 return false;
a8f0bd18 348 }
13fd6a66
LOK
349 else if (iBytesRead > 0)
350 AddData(buff, (uint8_t) iBytesRead);
25701fa6 351
13fd6a66 352 return iBytesRead > 0;
a8f0bd18
LOK
353}
354
8bca69de 355void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
a8f0bd18 356{
f077e8ed 357 CLockObject lock(&m_mutex);
50aa01e6
LOK
358 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
359 m_inBuffer.Push(data[iPtr]);
d5bffd3c
LOK
360
361 m_rcvCondition.Signal();
a8f0bd18
LOK
362}
363
3c53ac93 364void CAdapterCommunication::WriteNextCommand(void)
a8f0bd18 365{
28352a04 366 CCECAdapterMessage *msg;
3c53ac93 367 if (m_outBuffer.Pop(msg))
a96eb42e
LOK
368 SendMessageToAdapter(msg);
369}
370
371void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
372{
a96eb42e
LOK
373 CLockObject lock(&msg->mutex);
374 if (m_port->Write(msg) != (int32_t) msg->size())
a8f0bd18 375 {
a96eb42e
LOK
376 CStdString strError;
377 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
1113cb7d 378 m_processor->AddLog(CEC_LOG_ERROR, strError);
a96eb42e 379 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
25701fa6 380 }
a96eb42e
LOK
381 else
382 {
1113cb7d 383 m_processor->AddLog(CEC_LOG_DEBUG, "command sent");
a96eb42e
LOK
384 msg->state = ADAPTER_MESSAGE_STATE_SENT;
385 }
386 msg->condition.Signal();
3c53ac93 387}
2abe74eb 388
28352a04 389bool CAdapterCommunication::Write(CCECAdapterMessage *data)
3c53ac93 390{
0e31a62c 391 data->state = ADAPTER_MESSAGE_STATE_WAITING;
3c53ac93 392 m_outBuffer.Push(data);
a8f0bd18
LOK
393 return true;
394}
395
220537f2 396bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
a8f0bd18 397{
f077e8ed 398 CLockObject lock(&m_mutex);
a8f0bd18 399
50aa01e6
LOK
400 msg.clear();
401 uint64_t iNow = GetTimeMs();
402 uint64_t iTarget = iNow + iTimeout;
403 bool bGotFullMessage(false);
404 bool bNextIsEscaped(false);
405 bool bGotStart(false);
a8f0bd18 406
50aa01e6 407 while(!bGotFullMessage && iNow < iTarget)
a8f0bd18 408 {
50aa01e6
LOK
409 uint8_t buf = 0;
410 if (!m_inBuffer.Pop(buf))
a8f0bd18 411 {
56701628 412 if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow)))
50aa01e6 413 return false;
a8f0bd18 414 }
a8f0bd18 415
50aa01e6 416 if (!bGotStart)
a8f0bd18 417 {
50aa01e6
LOK
418 if (buf == MSGSTART)
419 bGotStart = true;
420 continue;
a8f0bd18 421 }
50aa01e6 422 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
a8f0bd18 423 {
855a3a98 424 if (msg.size() > 0)
1113cb7d 425 m_processor->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
50aa01e6
LOK
426 msg.clear();
427 bGotStart = true;
a8f0bd18 428 }
a8f0bd18 429
50aa01e6 430 if (buf == MSGEND)
a8f0bd18 431 {
50aa01e6 432 bGotFullMessage = true;
a8f0bd18 433 }
50aa01e6
LOK
434 else if (bNextIsEscaped)
435 {
436 msg.push_back(buf + (uint8_t)ESCOFFSET);
437 bNextIsEscaped = false;
438 }
439 else if (buf == MSGESC)
440 bNextIsEscaped = true;
441 else
442 msg.push_back(buf);
a8f0bd18
LOK
443 }
444
0e31a62c
LOK
445 if (bGotFullMessage)
446 msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
447
50aa01e6 448 return bGotFullMessage;
a8f0bd18
LOK
449}
450
828682d3 451std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
452{
453 return m_port->GetError();
454}
2abe74eb
LOK
455
456bool CAdapterCommunication::StartBootloader(void)
457{
28352a04 458 bool bReturn(false);
2abe74eb 459 if (!IsRunning())
28352a04 460 return bReturn;
2abe74eb 461
1113cb7d 462 m_processor->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
28352a04 463 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 464
5a1e87c8
LOK
465 output->push_back(MSGSTART);
466 output->push_escaped(MSGCODE_START_BOOTLOADER);
467 output->push_back(MSGEND);
2abe74eb 468
802b7a0f
LOK
469 CLockObject lock(&output->mutex);
470 if (Write(output))
471 output->condition.Wait(&output->mutex);
a96eb42e 472 bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT;
28352a04
LOK
473 delete output;
474
475 return bReturn;
2abe74eb
LOK
476}
477
2abe74eb
LOK
478bool CAdapterCommunication::PingAdapter(void)
479{
28352a04 480 bool bReturn(false);
2abe74eb 481 if (!IsRunning())
28352a04 482 return bReturn;
2abe74eb 483
1113cb7d 484 m_processor->AddLog(CEC_LOG_DEBUG, "sending ping");
28352a04 485 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 486
5a1e87c8
LOK
487 output->push_back(MSGSTART);
488 output->push_escaped(MSGCODE_PING);
489 output->push_back(MSGEND);
2abe74eb 490
802b7a0f
LOK
491 CLockObject lock(&output->mutex);
492 if (Write(output))
493 output->condition.Wait(&output->mutex);
a96eb42e 494 bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT;
28352a04
LOK
495 delete output;
496
497 return bReturn;
2abe74eb 498}
13fd6a66 499
a171d2fd
LOK
500bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
501{
502 bool bReturn(m_iLineTimeout != iTimeout);
503
504 if (!bReturn)
505 {
506 CCECAdapterMessage *output = new CCECAdapterMessage;
507
508 output->push_back(MSGSTART);
509 output->push_escaped(MSGCODE_TRANSMIT_IDLETIME);
510 output->push_escaped(iTimeout);
511 output->push_back(MSGEND);
512
513 if ((bReturn = Write(output)) == false)
514 m_processor->AddLog(CEC_LOG_ERROR, "could not set the idletime");
515 delete output;
516 }
517
518 return bReturn;
519}
520
13fd6a66
LOK
521bool CAdapterCommunication::IsOpen(void) const
522{
b9eea66d 523 return !IsStopped() && m_port->IsOpen() && IsRunning();
13fd6a66 524}