cec: retry to get the firmware version when it failed. wait for active tranmission...
[deb_libcec.git] / src / lib / adapter / 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
ef7696f5 35#include "AdapterMessage.h"
b5f787b9
LOK
36#include "../CECProcessor.h"
37#include "../platform/serialport/serialport.h"
5477a250 38#include "../LibCEC.h"
a8f0bd18
LOK
39
40using namespace std;
41using namespace CEC;
f00ff009 42using namespace PLATFORM;
a8f0bd18 43
1113cb7d 44CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) :
12027dbe 45 m_port(NULL),
a171d2fd 46 m_processor(processor),
1fc16cfd
LOK
47 m_iLineTimeout(0),
48 m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN)
a8f0bd18 49{
f00ff009 50 m_port = new PLATFORM::CSerialPort;
a8f0bd18
LOK
51}
52
828682d3 53CAdapterCommunication::~CAdapterCommunication(void)
a8f0bd18 54{
12027dbe
LOK
55 Close();
56
57 if (m_port)
58 {
59 delete m_port;
60 m_port = NULL;
61 }
a8f0bd18
LOK
62}
63
25701fa6 64bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
a8f0bd18 65{
7b494bea
LOK
66 uint64_t iNow = GetTimeMs();
67 uint64_t iTimeout = iNow + iTimeoutMs;
68
f00ff009 69 CLockObject lock(m_mutex);
7b494bea 70
13fd6a66
LOK
71 if (!m_port)
72 {
5477a250 73 CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL");
a8f0bd18 74 return false;
13fd6a66
LOK
75 }
76
77 if (IsOpen())
78 {
5477a250 79 CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open");
7b494bea 80 return true;
13fd6a66 81 }
a8f0bd18 82
7b494bea
LOK
83 CStdString strError;
84 bool bConnected(false);
85 while (!bConnected && iNow < iTimeout)
86 {
d956c02e 87 if ((bConnected = m_port->Open(strPort, iBaudRate)) == false)
7b494bea
LOK
88 {
89 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
90 Sleep(250);
91 iNow = GetTimeMs();
92 }
93 }
94
95 if (!bConnected)
a8f0bd18 96 {
5477a250 97 CLibCEC::AddLog(CEC_LOG_ERROR, strError);
a8f0bd18
LOK
98 return false;
99 }
100
2c780401 101 CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
a8f0bd18
LOK
102
103 //clear any input bytes
2c780401
LOK
104 uint8_t buff[1024];
105 while (m_port->Read(buff, 1024, 100) > 0)
106 {
107 CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
108 Sleep(250);
109 }
a8f0bd18 110
828682d3 111 if (CreateThread())
a8f0bd18 112 {
5477a250 113 CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started");
a8f0bd18
LOK
114 return true;
115 }
116 else
117 {
5477a250 118 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread");
a8f0bd18
LOK
119 }
120
121 return false;
122}
123
828682d3 124void CAdapterCommunication::Close(void)
a8f0bd18 125{
f00ff009 126 CLockObject lock(m_mutex);
d5bffd3c 127 m_rcvCondition.Broadcast();
b9eea66d 128 StopThread();
a8f0bd18
LOK
129}
130
828682d3 131void *CAdapterCommunication::Process(void)
a8f0bd18 132{
13fd6a66 133 while (!IsStopped())
a8f0bd18 134 {
9e1cfa5c 135 ReadFromDevice(50);
c02980af 136 Sleep(5);
3c53ac93 137 WriteNextCommand();
a8f0bd18
LOK
138 }
139
ef7696f5 140 CCECAdapterMessage *msg(NULL);
a0878ee3
LOK
141 if (m_outBuffer.Pop(msg))
142 msg->condition.Broadcast();
143
a8f0bd18
LOK
144 return NULL;
145}
146
28352a04 147bool CAdapterCommunication::Write(CCECAdapterMessage *data)
3c53ac93 148{
5dcf9f25
LOK
149 bool bReturn(false);
150
151 CLockObject lock(data->mutex);
152 data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
3c53ac93 153 m_outBuffer.Push(data);
5dcf9f25
LOK
154 data->condition.Wait(data->mutex);
155
156 if (data->state != ADAPTER_MESSAGE_STATE_SENT)
157 {
5477a250 158 CLibCEC::AddLog(CEC_LOG_ERROR, "command was not sent");
5dcf9f25 159 }
b2f0b1ab 160 else if (data->expectControllerAck)
5dcf9f25 161 {
b2f0b1ab 162 bReturn = WaitForAck(*data);
4060d525
LOK
163 if (bReturn)
164 {
165 if (data->isTransmission)
166 data->state = ADAPTER_MESSAGE_STATE_SENT_ACKED;
167 }
168 else
169 {
170 data->state = ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
5477a250 171 CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack");
4060d525 172 }
5dcf9f25
LOK
173 }
174 else
175 {
4060d525 176 bReturn = true;
5dcf9f25
LOK
177 }
178
179 return bReturn;
a8f0bd18
LOK
180}
181
220537f2 182bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
a8f0bd18 183{
f00ff009 184 CLockObject lock(m_mutex);
a8f0bd18 185
ef7696f5 186 msg.Clear();
50aa01e6
LOK
187 uint64_t iNow = GetTimeMs();
188 uint64_t iTarget = iNow + iTimeout;
189 bool bGotFullMessage(false);
190 bool bNextIsEscaped(false);
191 bool bGotStart(false);
a8f0bd18 192
50aa01e6 193 while(!bGotFullMessage && iNow < iTarget)
a8f0bd18 194 {
50aa01e6
LOK
195 uint8_t buf = 0;
196 if (!m_inBuffer.Pop(buf))
a8f0bd18 197 {
f00ff009 198 if (!m_rcvCondition.Wait(m_mutex, (uint32_t) (iTarget - iNow)))
50aa01e6 199 return false;
a8f0bd18 200 }
a8f0bd18 201
50aa01e6 202 if (!bGotStart)
a8f0bd18 203 {
50aa01e6
LOK
204 if (buf == MSGSTART)
205 bGotStart = true;
206 continue;
a8f0bd18 207 }
50aa01e6 208 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
a8f0bd18 209 {
ef7696f5 210 if (msg.Size() > 0)
5477a250 211 CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
ef7696f5 212 msg.Clear();
50aa01e6 213 bGotStart = true;
a8f0bd18 214 }
a8f0bd18 215
50aa01e6 216 if (buf == MSGEND)
a8f0bd18 217 {
50aa01e6 218 bGotFullMessage = true;
a8f0bd18 219 }
50aa01e6
LOK
220 else if (bNextIsEscaped)
221 {
ef7696f5 222 msg.PushBack(buf + (uint8_t)ESCOFFSET);
50aa01e6
LOK
223 bNextIsEscaped = false;
224 }
225 else if (buf == MSGESC)
226 bNextIsEscaped = true;
227 else
ef7696f5 228 msg.PushBack(buf);
a8f0bd18
LOK
229 }
230
0e31a62c 231 if (bGotFullMessage)
5dcf9f25 232 msg.state = ADAPTER_MESSAGE_STATE_INCOMING;
0e31a62c 233
50aa01e6 234 return bGotFullMessage;
a8f0bd18
LOK
235}
236
828682d3 237std::string CAdapterCommunication::GetError(void) const
a8f0bd18
LOK
238{
239 return m_port->GetError();
240}
2abe74eb
LOK
241
242bool CAdapterCommunication::StartBootloader(void)
243{
28352a04 244 bool bReturn(false);
2abe74eb 245 if (!IsRunning())
28352a04 246 return bReturn;
2abe74eb 247
5477a250 248 CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader");
28352a04 249 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 250
ef7696f5
LOK
251 output->PushBack(MSGSTART);
252 output->PushEscaped(MSGCODE_START_BOOTLOADER);
253 output->PushBack(MSGEND);
5dcf9f25 254 output->isTransmission = false;
71c4a2f5 255 output->expectControllerAck = false;
2abe74eb 256
5dcf9f25 257 if ((bReturn = Write(output)) == false)
5477a250 258 CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader");
28352a04
LOK
259 delete output;
260
261 return bReturn;
2abe74eb
LOK
262}
263
2abe74eb
LOK
264bool CAdapterCommunication::PingAdapter(void)
265{
28352a04 266 bool bReturn(false);
2abe74eb 267 if (!IsRunning())
28352a04 268 return bReturn;
2abe74eb 269
5477a250 270 CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping");
28352a04 271 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 272
ef7696f5
LOK
273 output->PushBack(MSGSTART);
274 output->PushEscaped(MSGCODE_PING);
275 output->PushBack(MSGEND);
5dcf9f25 276 output->isTransmission = false;
2abe74eb 277
5dcf9f25 278 if ((bReturn = Write(output)) == false)
5477a250 279 CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter");
28352a04
LOK
280 delete output;
281
282 return bReturn;
2abe74eb 283}
13fd6a66 284
1fc16cfd
LOK
285uint16_t CAdapterCommunication::GetFirmwareVersion(void)
286{
287 uint16_t iReturn(m_iFirmwareVersion);
288 if (!IsRunning())
289 return iReturn;
290
291 if (iReturn == CEC_FW_VERSION_UNKNOWN)
292 {
5477a250 293 CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version");
1fc16cfd
LOK
294 CCECAdapterMessage *output = new CCECAdapterMessage;
295
296 output->PushBack(MSGSTART);
297 output->PushEscaped(MSGCODE_FIRMWARE_VERSION);
298 output->PushBack(MSGEND);
299 output->isTransmission = false;
300 output->expectControllerAck = false;
301
302 SendMessageToAdapter(output);
303 delete output;
304
305 CCECAdapterMessage input;
306 if (!Read(input, CEC_DEFAULT_TRANSMIT_WAIT) || input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
2c780401 307 CLibCEC::AddLog(CEC_LOG_ERROR, "no or invalid firmware version (size = %d, message = %d)", input.Size(), input.Message());
1fc16cfd
LOK
308 else
309 {
310 m_iFirmwareVersion = (input[1] << 8 | input[2]);
311 iReturn = m_iFirmwareVersion;
312 }
313 }
314
315 return iReturn;
316}
317
a171d2fd
LOK
318bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
319{
320 bool bReturn(m_iLineTimeout != iTimeout);
321
322 if (!bReturn)
323 {
324 CCECAdapterMessage *output = new CCECAdapterMessage;
325
ef7696f5
LOK
326 output->PushBack(MSGSTART);
327 output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
328 output->PushEscaped(iTimeout);
329 output->PushBack(MSGEND);
5dcf9f25 330 output->isTransmission = false;
a171d2fd
LOK
331
332 if ((bReturn = Write(output)) == false)
5477a250 333 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime");
a171d2fd
LOK
334 delete output;
335 }
336
337 return bReturn;
338}
339
5dcf9f25
LOK
340bool CAdapterCommunication::SetAckMask(uint16_t iMask)
341{
342 bool bReturn(false);
343 CStdString strLog;
344 strLog.Format("setting ackmask to %2x", iMask);
5477a250 345 CLibCEC::AddLog(CEC_LOG_DEBUG, strLog.c_str());
5dcf9f25
LOK
346
347 CCECAdapterMessage *output = new CCECAdapterMessage;
348
349 output->PushBack(MSGSTART);
350 output->PushEscaped(MSGCODE_SET_ACK_MASK);
351 output->PushEscaped(iMask >> 8);
352 output->PushEscaped((uint8_t)iMask);
353 output->PushBack(MSGEND);
354 output->isTransmission = false;
355
356 if ((bReturn = Write(output)) == false)
5477a250 357 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask");
5dcf9f25
LOK
358 delete output;
359
360 return bReturn;
361}
362
f00ff009 363bool CAdapterCommunication::IsOpen(void)
13fd6a66 364{
b9eea66d 365 return !IsStopped() && m_port->IsOpen() && IsRunning();
13fd6a66 366}
ef7696f5 367
b2f0b1ab 368bool CAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
6729ac71
LOK
369{
370 bool bError(false);
371 bool bTransmitSucceeded(false);
b2f0b1ab 372 uint8_t iPacketsLeft(message.Size() / 4);
6729ac71
LOK
373
374 int64_t iNow = GetTimeMs();
b2f0b1ab 375 int64_t iTargetTime = iNow + message.transmit_timeout;
6729ac71 376
b2f0b1ab 377 while (!bTransmitSucceeded && !bError && (message.transmit_timeout == 0 || iNow < iTargetTime))
6729ac71
LOK
378 {
379 CCECAdapterMessage msg;
e2800c15 380 int32_t iWait = (int32_t)(iTargetTime - iNow);
b2f0b1ab 381 if (iWait <= 5 || message.transmit_timeout <= 5)
e2800c15 382 iWait = CEC_DEFAULT_TRANSMIT_WAIT;
6729ac71 383
e2800c15 384 if (!Read(msg, iWait))
6729ac71
LOK
385 {
386 iNow = GetTimeMs();
387 continue;
388 }
389
390 if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
391 {
392 m_processor->HandlePoll(msg.Initiator(), msg.Destination());
393 iNow = GetTimeMs();
394 continue;
395 }
396
397 if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
398 m_processor->HandleReceiveFailed())
399 {
400 iNow = GetTimeMs();
401 continue;
402 }
403
404 bError = msg.IsError();
405 if (bError)
406 {
b2f0b1ab 407 message.reply = msg.Message();
5477a250 408 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
409 }
410 else
411 {
412 switch(msg.Message())
413 {
414 case MSGCODE_COMMAND_ACCEPTED:
5477a250 415 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
416 if (iPacketsLeft > 0)
417 iPacketsLeft--;
b2f0b1ab 418 if (!message.isTransmission && iPacketsLeft == 0)
5dcf9f25 419 bTransmitSucceeded = true;
6729ac71
LOK
420 break;
421 case MSGCODE_TRANSMIT_SUCCEEDED:
5477a250 422 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
423 bTransmitSucceeded = (iPacketsLeft == 0);
424 bError = !bTransmitSucceeded;
b2f0b1ab 425 message.reply = MSGCODE_TRANSMIT_SUCCEEDED;
6729ac71
LOK
426 break;
427 default:
428 // ignore other data while waiting
429 break;
430 }
431
432 iNow = GetTimeMs();
433 }
434 }
435
436 return bTransmitSucceeded && !bError;
437}
438
ef7696f5
LOK
439void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
440{
441 CLockObject lock(m_mutex);
442 for (uint8_t iPtr = 0; iPtr < iLen; iPtr++)
443 m_inBuffer.Push(data[iPtr]);
444
445 m_rcvCondition.Signal();
446}
447
448bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
449{
450 int32_t iBytesRead;
451 uint8_t buff[1024];
452 if (!m_port)
453 return false;
454
1fc16cfd 455 CLockObject lock(m_mutex);
ef7696f5
LOK
456 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
457 if (iBytesRead < 0 || iBytesRead > 256)
458 {
459 CStdString strError;
460 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
5477a250 461 CLibCEC::AddLog(CEC_LOG_ERROR, strError);
ef7696f5
LOK
462 return false;
463 }
464 else if (iBytesRead > 0)
465 AddData(buff, (uint8_t) iBytesRead);
466
467 return iBytesRead > 0;
468}
469
470void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
471{
1fc16cfd 472 CLockObject adapterLock(m_mutex);
ef7696f5
LOK
473 CLockObject lock(msg->mutex);
474 if (m_port->Write(msg->packet.data, msg->Size()) != (int32_t) msg->Size())
475 {
476 CStdString strError;
477 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
5477a250 478 CLibCEC::AddLog(CEC_LOG_ERROR, strError);
ef7696f5
LOK
479 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
480 }
481 else
482 {
5477a250 483 CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent");
ef7696f5
LOK
484 msg->state = ADAPTER_MESSAGE_STATE_SENT;
485 }
486 msg->condition.Signal();
487}
488
489void CAdapterCommunication::WriteNextCommand(void)
490{
491 CCECAdapterMessage *msg(NULL);
492 if (m_outBuffer.Pop(msg))
493 SendMessageToAdapter(msg);
494}