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