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