cec: and now proper predicates
[deb_libcec.git] / src / lib / adapter / USBCECAdapterCommunication.cpp
CommitLineData
a8f0bd18
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
b492c10e 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
a8f0bd18
LOK
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
7bb4ed43 33#include "USBCECAdapterCommunication.h"
ba65909d
LOK
34#include "../platform/sockets/serialport.h"
35#include "../platform/util/timeutils.h"
5477a250 36#include "../LibCEC.h"
7bb4ed43 37#include "../CECProcessor.h"
a8f0bd18
LOK
38
39using namespace std;
40using namespace CEC;
f00ff009 41using namespace PLATFORM;
a8f0bd18 42
7bb4ed43 43CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate /* = 38400 */) :
12027dbe 44 m_port(NULL),
a171d2fd 45 m_processor(processor),
960f33c6 46 m_bHasData(false),
1fc16cfd 47 m_iLineTimeout(0),
7bb4ed43
LOK
48 m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN),
49 m_lastInitiator(CECDEVICE_UNKNOWN),
50 m_bNextIsEscaped(false),
51 m_bGotStart(false)
a8f0bd18 52{
99666519 53 m_port = new PLATFORM::CSerialPort(strPort, iBaudRate);
a8f0bd18
LOK
54}
55
7bb4ed43 56CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void)
a8f0bd18 57{
12027dbe 58 Close();
a8f0bd18
LOK
59}
60
efed01e1 61bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */)
a8f0bd18 62{
efed01e1 63 bool bReturn(false);
7b494bea 64 uint64_t iNow = GetTimeMs();
efed01e1 65 uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
7b494bea 66
efed01e1
LOK
67 /* try to ping the adapter */
68 bool bPinged(false);
69 unsigned iPingTry(0);
70 while (iNow < iTarget && (bPinged = PingAdapter()) == false)
13fd6a66 71 {
efed01e1
LOK
72 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry);
73 Sleep(500);
74 iNow = GetTimeMs();
13fd6a66
LOK
75 }
76
efed01e1
LOK
77 /* try to read the firmware version */
78 m_iFirmwareVersion = CEC_FW_VERSION_UNKNOWN;
79 unsigned iFwVersionTry(0);
80 while (bPinged && iNow < iTarget && (m_iFirmwareVersion = GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN)
13fd6a66 81 {
efed01e1
LOK
82 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry);
83 Sleep(500);
84 iNow = GetTimeMs();
13fd6a66 85 }
a8f0bd18 86
efed01e1 87 if (m_iFirmwareVersion >= 2)
7b494bea 88 {
efed01e1
LOK
89 /* try to set controlled mode */
90 unsigned iControlledTry(0);
91 bool bControlled(false);
92 while (iNow < iTarget && (bControlled = SetControlledMode(true)) == false)
7b494bea 93 {
efed01e1
LOK
94 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry);
95 Sleep(500);
7b494bea
LOK
96 iNow = GetTimeMs();
97 }
efed01e1 98 bReturn = bControlled;
7b494bea 99 }
efed01e1
LOK
100 else
101 bReturn = true;
7b494bea 102
efed01e1
LOK
103 return bReturn;
104}
a8f0bd18 105
efed01e1
LOK
106bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */)
107{
108 uint64_t iNow = GetTimeMs();
109 uint64_t iTimeout = iNow + iTimeoutMs;
a8f0bd18 110
2c780401 111 {
efed01e1
LOK
112 CLockObject lock(m_mutex);
113
114 if (!m_port)
115 {
116 CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL");
117 return false;
118 }
119
120 if (IsOpen())
121 {
122 CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open");
123 return true;
124 }
125
126 m_callback = cb;
127 CStdString strError;
128 bool bConnected(false);
129 while (!bConnected && iNow < iTimeout)
130 {
131 if ((bConnected = m_port->Open(iTimeout)) == false)
132 {
133 strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str());
134 Sleep(250);
135 iNow = GetTimeMs();
136 }
137 }
138
139 if (!bConnected)
140 {
141 CLibCEC::AddLog(CEC_LOG_ERROR, strError);
142 return false;
143 }
144
145 CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
146
147 //clear any input bytes
148 uint8_t buff[1024];
149 while (m_port->Read(buff, 1024, 100) > 0)
150 {
151 CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
152 Sleep(250);
153 }
2c780401 154 }
a8f0bd18 155
828682d3 156 if (CreateThread())
a8f0bd18 157 {
efed01e1
LOK
158 if (!CheckAdapter())
159 {
160 StopThread();
161 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks");
162 }
163 else
164 {
165 CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started");
166 return true;
167 }
a8f0bd18 168 }
efed01e1 169 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread");
a8f0bd18
LOK
170
171 return false;
172}
173
7bb4ed43 174void CUSBCECAdapterCommunication::Close(void)
a8f0bd18 175{
f967a750 176 SetAckMask(0);
f00ff009 177 CLockObject lock(m_mutex);
960f33c6 178 m_bHasData = true;
d5bffd3c 179 m_rcvCondition.Broadcast();
b9eea66d 180 StopThread();
a8f0bd18
LOK
181}
182
7bb4ed43 183void *CUSBCECAdapterCommunication::Process(void)
a8f0bd18 184{
b1f94db1 185 cec_command command;
efed01e1 186 bool bCommandReceived(false);
13fd6a66 187 while (!IsStopped())
a8f0bd18 188 {
efed01e1
LOK
189 {
190 CLockObject lock(m_mutex);
191 ReadFromDevice(50);
192 bCommandReceived = m_callback && Read(command, 0);
193 }
b1f94db1
LOK
194
195 /* push the next command to the callback method if there is one */
efed01e1 196 if (!IsStopped() && bCommandReceived)
b1f94db1
LOK
197 m_callback->OnCommandReceived(command);
198
efed01e1
LOK
199 if (!IsStopped())
200 {
201 Sleep(5);
202 WriteNextCommand();
203 }
a8f0bd18
LOK
204 }
205
ef7696f5 206 CCECAdapterMessage *msg(NULL);
a0878ee3 207 if (m_outBuffer.Pop(msg))
960f33c6 208 msg->event.Broadcast();
a0878ee3 209
9f9c8c82
LOK
210 if (m_port)
211 {
212 delete m_port;
213 m_port = NULL;
214 }
215
a8f0bd18
LOK
216 return NULL;
217}
218
7bb4ed43
LOK
219cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */)
220{
221 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
9f68cc28
LOK
222 if (!IsRunning())
223 return retVal;
7bb4ed43
LOK
224
225 CCECAdapterMessage *output = new CCECAdapterMessage(data);
226
227 /* set the number of retries */
228 if (data.opcode == CEC_OPCODE_NONE) //TODO
229 output->maxTries = 1;
230 else if (data.initiator != CECDEVICE_BROADCAST)
231 output->maxTries = iMaxTries;
232
233 output->lineTimeout = iLineTimeout;
234 output->retryTimeout = iRetryLineTimeout;
235 output->tries = 0;
236
237 bool bRetry(true);
238 while (bRetry && ++output->tries < output->maxTries)
239 {
240 bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0;
241 if (bRetry)
242 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
243 }
244 retVal = output->state;
245
246 delete output;
247 return retVal;
248}
249
250bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data)
3c53ac93 251{
5dcf9f25 252 data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
3c53ac93 253 m_outBuffer.Push(data);
960f33c6 254 data->event.Wait();
5dcf9f25 255
b1f94db1
LOK
256 if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) ||
257 (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT))
5dcf9f25 258 {
b1f94db1
LOK
259 CLibCEC::AddLog(CEC_LOG_DEBUG, "command was not %s", data->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED ? "acked" : "sent");
260 return false;
5dcf9f25
LOK
261 }
262
b1f94db1 263 return true;
a8f0bd18
LOK
264}
265
7bb4ed43 266bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout)
a8f0bd18 267{
9f68cc28
LOK
268 if (!IsRunning())
269 return false;
270
7bb4ed43
LOK
271 CCECAdapterMessage msg;
272 if (Read(msg, iTimeout))
a8f0bd18 273 {
7bb4ed43 274 if (ParseMessage(msg))
a8f0bd18 275 {
7bb4ed43
LOK
276 command = m_currentframe;
277 m_currentframe.Clear();
278 return true;
a8f0bd18 279 }
7bb4ed43
LOK
280 }
281 return false;
282}
a8f0bd18 283
7bb4ed43
LOK
284bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
285{
286 CLockObject lock(m_mutex);
a8f0bd18 287
7bb4ed43
LOK
288 msg.Clear();
289 CCECAdapterMessage *buf(NULL);
a8f0bd18 290
7bb4ed43
LOK
291 if (!m_inBuffer.Pop(buf))
292 {
960f33c6 293 if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, m_bHasData, iTimeout))
7bb4ed43
LOK
294 return false;
295 m_inBuffer.Pop(buf);
960f33c6 296 m_bHasData = m_inBuffer.Size() > 0;
7bb4ed43 297 }
0e31a62c 298
7bb4ed43
LOK
299 if (buf)
300 {
301 msg.packet = buf->packet;
66e5bd72 302 msg.state = ADAPTER_MESSAGE_STATE_INCOMING;
7bb4ed43
LOK
303 delete buf;
304 return true;
305 }
306 return false;
a8f0bd18
LOK
307}
308
7bb4ed43 309CStdString CUSBCECAdapterCommunication::GetError(void) const
a8f0bd18 310{
ba65909d
LOK
311 CStdString strError;
312 strError = m_port->GetError();
313 return strError;
a8f0bd18 314}
2abe74eb 315
7bb4ed43 316bool CUSBCECAdapterCommunication::StartBootloader(void)
2abe74eb 317{
28352a04 318 bool bReturn(false);
2abe74eb 319 if (!IsRunning())
28352a04 320 return bReturn;
2abe74eb 321
5477a250 322 CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader");
28352a04 323 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 324
ef7696f5
LOK
325 output->PushBack(MSGSTART);
326 output->PushEscaped(MSGCODE_START_BOOTLOADER);
327 output->PushBack(MSGEND);
5dcf9f25 328 output->isTransmission = false;
71c4a2f5 329 output->expectControllerAck = false;
2abe74eb 330
5dcf9f25 331 if ((bReturn = Write(output)) == false)
5477a250 332 CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader");
28352a04
LOK
333 delete output;
334
335 return bReturn;
2abe74eb
LOK
336}
337
7bb4ed43 338bool CUSBCECAdapterCommunication::PingAdapter(void)
2abe74eb 339{
28352a04 340 bool bReturn(false);
2abe74eb 341 if (!IsRunning())
28352a04 342 return bReturn;
2abe74eb 343
5477a250 344 CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping");
28352a04 345 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 346
ef7696f5
LOK
347 output->PushBack(MSGSTART);
348 output->PushEscaped(MSGCODE_PING);
349 output->PushBack(MSGEND);
5dcf9f25 350 output->isTransmission = false;
2abe74eb 351
5dcf9f25 352 if ((bReturn = Write(output)) == false)
5477a250 353 CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter");
28352a04
LOK
354 delete output;
355
356 return bReturn;
2abe74eb 357}
13fd6a66 358
7bb4ed43
LOK
359bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
360{
361 bool bEom(false);
362 bool bIsError(msg.IsError());
363
364 if (msg.IsEmpty())
365 return bEom;
366
367 switch(msg.Message())
368 {
369 case MSGCODE_FRAME_START:
370 {
371 m_currentframe.Clear();
372 if (msg.Size() >= 2)
373 {
374 m_currentframe.initiator = msg.Initiator();
375 m_currentframe.destination = msg.Destination();
376 m_currentframe.ack = msg.IsACK();
377 m_currentframe.eom = msg.IsEOM();
378 }
379 if (m_currentframe.ack == 0x1)
380 {
381 m_lastInitiator = m_currentframe.initiator;
382 m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
383 }
384 }
385 break;
386 case MSGCODE_RECEIVE_FAILED:
387 {
388 m_currentframe.Clear();
389 if (m_lastInitiator != CECDEVICE_UNKNOWN)
390 bIsError = m_processor->HandleReceiveFailed(m_lastInitiator);
391 }
392 break;
393 case MSGCODE_FRAME_DATA:
394 {
395 if (msg.Size() >= 2)
396 {
397 m_currentframe.PushBack(msg[1]);
398 m_currentframe.eom = msg.IsEOM();
399 }
400 bEom = msg.IsEOM();
401 }
402 break;
403 default:
404 break;
405 }
406
407 CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
408 return bEom;
409}
410
411uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
1fc16cfd
LOK
412{
413 uint16_t iReturn(m_iFirmwareVersion);
414 if (!IsRunning())
415 return iReturn;
416
417 if (iReturn == CEC_FW_VERSION_UNKNOWN)
418 {
006b76b9 419 CLockObject lock(m_mutex);
5477a250 420 CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version");
1fc16cfd
LOK
421 CCECAdapterMessage *output = new CCECAdapterMessage;
422
423 output->PushBack(MSGSTART);
424 output->PushEscaped(MSGCODE_FIRMWARE_VERSION);
425 output->PushBack(MSGEND);
426 output->isTransmission = false;
427 output->expectControllerAck = false;
428
efed01e1
LOK
429 SendMessageToAdapter(output);
430 bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT;
1fc16cfd 431 delete output;
b1f94db1
LOK
432 if (!bWriteOk)
433 {
434 CLibCEC::AddLog(CEC_LOG_ERROR, "could not request the firmware version");
efed01e1 435 return iReturn;
b1f94db1 436 }
efed01e1
LOK
437
438 Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32
439 ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, 5 /* start + msgcode + 2 bytes for fw version + end */);
440 CCECAdapterMessage input;
441 if (Read(input, 0))
1fc16cfd 442 {
efed01e1
LOK
443 if (input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
444 CLibCEC::AddLog(CEC_LOG_ERROR, "invalid firmware version (size = %d, message = %d)", input.Size(), input.Message());
b1f94db1
LOK
445 else
446 {
447 m_iFirmwareVersion = (input[1] << 8 | input[2]);
448 iReturn = m_iFirmwareVersion;
449 }
1fc16cfd 450 }
efed01e1
LOK
451 else
452 {
453 CLibCEC::AddLog(CEC_LOG_ERROR, "no firmware version received");
454 }
1fc16cfd
LOK
455 }
456
457 return iReturn;
458}
459
7bb4ed43 460bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
a171d2fd 461{
7bb4ed43
LOK
462 m_iLineTimeout = iTimeout;
463 return true;
464 //TODO
465// bool bReturn(m_iLineTimeout != iTimeout);
466//
467// if (!bReturn)
468// {
469// CCECAdapterMessage *output = new CCECAdapterMessage;
470//
471// output->PushBack(MSGSTART);
472// output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
473// output->PushEscaped(iTimeout);
474// output->PushBack(MSGEND);
475// output->isTransmission = false;
476//
477// if ((bReturn = Write(output)) == false)
478// CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime");
479// delete output;
480// }
481//
482// return bReturn;
a171d2fd
LOK
483}
484
7bb4ed43 485bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
5dcf9f25
LOK
486{
487 bool bReturn(false);
bca69ca1 488 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask);
5dcf9f25
LOK
489
490 CCECAdapterMessage *output = new CCECAdapterMessage;
491
492 output->PushBack(MSGSTART);
493 output->PushEscaped(MSGCODE_SET_ACK_MASK);
494 output->PushEscaped(iMask >> 8);
495 output->PushEscaped((uint8_t)iMask);
496 output->PushBack(MSGEND);
497 output->isTransmission = false;
498
499 if ((bReturn = Write(output)) == false)
5477a250 500 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask");
5dcf9f25
LOK
501 delete output;
502
503 return bReturn;
504}
505
b057edad
BL
506
507bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
508{
509 bool bReturn(false);
bca69ca1 510 CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off");
b057edad
BL
511
512 CCECAdapterMessage *output = new CCECAdapterMessage;
513
514 output->PushBack(MSGSTART);
515 output->PushEscaped(MSGCODE_SET_CONTROLLED);
516 output->PushEscaped(controlled);
517 output->PushBack(MSGEND);
518 output->isTransmission = false;
519
520 if ((bReturn = Write(output)) == false)
521 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode");
522 delete output;
523
524 return bReturn;
525}
526
7bb4ed43 527bool CUSBCECAdapterCommunication::IsOpen(void)
13fd6a66 528{
b9eea66d 529 return !IsStopped() && m_port->IsOpen() && IsRunning();
13fd6a66 530}
ef7696f5 531
7bb4ed43 532bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
6729ac71
LOK
533{
534 bool bError(false);
535 bool bTransmitSucceeded(false);
b2f0b1ab 536 uint8_t iPacketsLeft(message.Size() / 4);
6729ac71
LOK
537
538 int64_t iNow = GetTimeMs();
b1f94db1 539 int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout);
6729ac71 540
b1f94db1 541 while (!bTransmitSucceeded && !bError && iNow < iTargetTime)
6729ac71 542 {
b1f94db1 543 ReadFromDevice(50);
6729ac71 544 CCECAdapterMessage msg;
b1f94db1 545 if (!Read(msg, 0))
6729ac71
LOK
546 {
547 iNow = GetTimeMs();
548 continue;
549 }
550
551 if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
552 {
553 m_processor->HandlePoll(msg.Initiator(), msg.Destination());
7bb4ed43 554 m_lastInitiator = msg.Initiator();
6729ac71
LOK
555 iNow = GetTimeMs();
556 continue;
557 }
558
559 if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
7bb4ed43
LOK
560 m_lastInitiator != CECDEVICE_UNKNOWN &&
561 m_processor->HandleReceiveFailed(m_lastInitiator))
6729ac71
LOK
562 {
563 iNow = GetTimeMs();
564 continue;
565 }
566
567 bError = msg.IsError();
568 if (bError)
569 {
b2f0b1ab 570 message.reply = msg.Message();
5477a250 571 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
572 }
573 else
574 {
575 switch(msg.Message())
576 {
577 case MSGCODE_COMMAND_ACCEPTED:
5477a250 578 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
579 if (iPacketsLeft > 0)
580 iPacketsLeft--;
b2f0b1ab 581 if (!message.isTransmission && iPacketsLeft == 0)
5dcf9f25 582 bTransmitSucceeded = true;
6729ac71
LOK
583 break;
584 case MSGCODE_TRANSMIT_SUCCEEDED:
5477a250 585 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
586 bTransmitSucceeded = (iPacketsLeft == 0);
587 bError = !bTransmitSucceeded;
b2f0b1ab 588 message.reply = MSGCODE_TRANSMIT_SUCCEEDED;
6729ac71
LOK
589 break;
590 default:
591 // ignore other data while waiting
592 break;
593 }
594
595 iNow = GetTimeMs();
596 }
597 }
598
b1f94db1
LOK
599 message.state = bTransmitSucceeded && !bError ?
600 ADAPTER_MESSAGE_STATE_SENT_ACKED :
601 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
602
6729ac71
LOK
603 return bTransmitSucceeded && !bError;
604}
605
7bb4ed43 606void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen)
ef7696f5
LOK
607{
608 CLockObject lock(m_mutex);
99666519 609 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
7bb4ed43
LOK
610 {
611 if (!m_bGotStart)
612 {
613 if (data[iPtr] == MSGSTART)
614 m_bGotStart = true;
615 }
616 else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove
617 {
618 if (m_currentAdapterMessage.Size() > 0)
619 CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
620 m_currentAdapterMessage.Clear();
621 m_bGotStart = true;
622 }
623 else if (data[iPtr] == MSGEND)
624 {
625 CCECAdapterMessage *newMessage = new CCECAdapterMessage;
626 newMessage->packet = m_currentAdapterMessage.packet;
627 m_inBuffer.Push(newMessage);
628 m_currentAdapterMessage.Clear();
629 m_bGotStart = false;
630 m_bNextIsEscaped = false;
960f33c6 631 m_bHasData = true;
7bb4ed43
LOK
632 m_rcvCondition.Signal();
633 }
634 else if (m_bNextIsEscaped)
635 {
636 m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET);
637 m_bNextIsEscaped = false;
638 }
639 else if (data[iPtr] == MSGESC)
640 {
641 m_bNextIsEscaped = true;
642 }
643 else
644 {
645 m_currentAdapterMessage.PushBack(data[iPtr]);
646 }
647 }
ef7696f5
LOK
648}
649
b1f94db1 650bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */)
ef7696f5 651{
99666519
LOK
652 ssize_t iBytesRead;
653 uint8_t buff[256];
ef7696f5
LOK
654 if (!m_port)
655 return false;
b1f94db1
LOK
656 if (iSize > 256)
657 iSize = 256;
ef7696f5 658
1fc16cfd 659 CLockObject lock(m_mutex);
b1f94db1 660 iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
ef7696f5
LOK
661 if (iBytesRead < 0 || iBytesRead > 256)
662 {
99666519 663 CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
ef7696f5
LOK
664 return false;
665 }
666 else if (iBytesRead > 0)
99666519
LOK
667 {
668 AddData(buff, iBytesRead);
669 }
ef7696f5
LOK
670
671 return iBytesRead > 0;
672}
673
7bb4ed43 674void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
ef7696f5 675{
1fc16cfd 676 CLockObject adapterLock(m_mutex);
7bb4ed43
LOK
677 if (msg->tries == 1)
678 SetLineTimeout(msg->lineTimeout);
679 else
680 SetLineTimeout(msg->retryTimeout);
681
99666519 682 if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size())
ef7696f5 683 {
b74fd339 684 CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str());
ef7696f5
LOK
685 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
686 }
687 else
688 {
5477a250 689 CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent");
ef7696f5 690 msg->state = ADAPTER_MESSAGE_STATE_SENT;
b1f94db1
LOK
691
692 if (msg->expectControllerAck)
693 {
694 if (!WaitForAck(*msg))
695 CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack");
696 }
ef7696f5 697 }
960f33c6 698 msg->event.Signal();
ef7696f5
LOK
699}
700
7bb4ed43 701void CUSBCECAdapterCommunication::WriteNextCommand(void)
ef7696f5
LOK
702{
703 CCECAdapterMessage *msg(NULL);
704 if (m_outBuffer.Pop(msg))
705 SendMessageToAdapter(msg);
706}