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