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