cec-config-gui: make the progress bar invisible when done. disable the physical addre...
[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{
c02a2b2e
LOK
176 {
177 CLockObject lock(m_mutex);
178 m_bHasData = true;
179 m_rcvCondition.Broadcast();
180 }
c4287bcd 181 SetAckMask(0);
b9eea66d 182 StopThread();
a8f0bd18
LOK
183}
184
7bb4ed43 185void *CUSBCECAdapterCommunication::Process(void)
a8f0bd18 186{
b1f94db1 187 cec_command command;
efed01e1 188 bool bCommandReceived(false);
13fd6a66 189 while (!IsStopped())
a8f0bd18 190 {
efed01e1
LOK
191 {
192 CLockObject lock(m_mutex);
193 ReadFromDevice(50);
194 bCommandReceived = m_callback && Read(command, 0);
195 }
b1f94db1
LOK
196
197 /* push the next command to the callback method if there is one */
efed01e1 198 if (!IsStopped() && bCommandReceived)
b1f94db1
LOK
199 m_callback->OnCommandReceived(command);
200
efed01e1
LOK
201 if (!IsStopped())
202 {
203 Sleep(5);
204 WriteNextCommand();
205 }
a8f0bd18
LOK
206 }
207
ef7696f5 208 CCECAdapterMessage *msg(NULL);
a0878ee3 209 if (m_outBuffer.Pop(msg))
960f33c6 210 msg->event.Broadcast();
a0878ee3 211
9f9c8c82
LOK
212 if (m_port)
213 {
214 delete m_port;
215 m_port = NULL;
216 }
217
a8f0bd18
LOK
218 return NULL;
219}
220
7bb4ed43
LOK
221cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */)
222{
223 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
9f68cc28
LOK
224 if (!IsRunning())
225 return retVal;
7bb4ed43
LOK
226
227 CCECAdapterMessage *output = new CCECAdapterMessage(data);
228
229 /* set the number of retries */
230 if (data.opcode == CEC_OPCODE_NONE) //TODO
231 output->maxTries = 1;
232 else if (data.initiator != CECDEVICE_BROADCAST)
233 output->maxTries = iMaxTries;
234
235 output->lineTimeout = iLineTimeout;
236 output->retryTimeout = iRetryLineTimeout;
237 output->tries = 0;
238
239 bool bRetry(true);
240 while (bRetry && ++output->tries < output->maxTries)
241 {
242 bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0;
243 if (bRetry)
244 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
245 }
246 retVal = output->state;
247
248 delete output;
249 return retVal;
250}
251
252bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data)
3c53ac93 253{
5dcf9f25 254 data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
3c53ac93 255 m_outBuffer.Push(data);
960f33c6 256 data->event.Wait();
5dcf9f25 257
b1f94db1
LOK
258 if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) ||
259 (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT))
5dcf9f25 260 {
b1f94db1
LOK
261 CLibCEC::AddLog(CEC_LOG_DEBUG, "command was not %s", data->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED ? "acked" : "sent");
262 return false;
5dcf9f25
LOK
263 }
264
b1f94db1 265 return true;
a8f0bd18
LOK
266}
267
7bb4ed43 268bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout)
a8f0bd18 269{
9f68cc28
LOK
270 if (!IsRunning())
271 return false;
272
7bb4ed43
LOK
273 CCECAdapterMessage msg;
274 if (Read(msg, iTimeout))
a8f0bd18 275 {
7bb4ed43 276 if (ParseMessage(msg))
a8f0bd18 277 {
7bb4ed43
LOK
278 command = m_currentframe;
279 m_currentframe.Clear();
280 return true;
a8f0bd18 281 }
7bb4ed43
LOK
282 }
283 return false;
284}
a8f0bd18 285
7bb4ed43
LOK
286bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
287{
288 CLockObject lock(m_mutex);
a8f0bd18 289
7bb4ed43
LOK
290 msg.Clear();
291 CCECAdapterMessage *buf(NULL);
a8f0bd18 292
7bb4ed43
LOK
293 if (!m_inBuffer.Pop(buf))
294 {
960f33c6 295 if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, m_bHasData, iTimeout))
7bb4ed43
LOK
296 return false;
297 m_inBuffer.Pop(buf);
960f33c6 298 m_bHasData = m_inBuffer.Size() > 0;
7bb4ed43 299 }
0e31a62c 300
7bb4ed43
LOK
301 if (buf)
302 {
303 msg.packet = buf->packet;
66e5bd72 304 msg.state = ADAPTER_MESSAGE_STATE_INCOMING;
7bb4ed43
LOK
305 delete buf;
306 return true;
307 }
308 return false;
a8f0bd18
LOK
309}
310
7bb4ed43 311CStdString CUSBCECAdapterCommunication::GetError(void) const
a8f0bd18 312{
ba65909d
LOK
313 CStdString strError;
314 strError = m_port->GetError();
315 return strError;
a8f0bd18 316}
2abe74eb 317
7bb4ed43 318bool CUSBCECAdapterCommunication::StartBootloader(void)
2abe74eb 319{
28352a04 320 bool bReturn(false);
2abe74eb 321 if (!IsRunning())
28352a04 322 return bReturn;
2abe74eb 323
5477a250 324 CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader");
28352a04 325 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 326
ef7696f5
LOK
327 output->PushBack(MSGSTART);
328 output->PushEscaped(MSGCODE_START_BOOTLOADER);
329 output->PushBack(MSGEND);
5dcf9f25 330 output->isTransmission = false;
71c4a2f5 331 output->expectControllerAck = false;
2abe74eb 332
5dcf9f25 333 if ((bReturn = Write(output)) == false)
5477a250 334 CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader");
28352a04
LOK
335 delete output;
336
337 return bReturn;
2abe74eb
LOK
338}
339
7bb4ed43 340bool CUSBCECAdapterCommunication::PingAdapter(void)
2abe74eb 341{
28352a04 342 bool bReturn(false);
2abe74eb 343 if (!IsRunning())
28352a04 344 return bReturn;
2abe74eb 345
5477a250 346 CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping");
28352a04 347 CCECAdapterMessage *output = new CCECAdapterMessage;
25701fa6 348
ef7696f5
LOK
349 output->PushBack(MSGSTART);
350 output->PushEscaped(MSGCODE_PING);
351 output->PushBack(MSGEND);
5dcf9f25 352 output->isTransmission = false;
2abe74eb 353
5dcf9f25 354 if ((bReturn = Write(output)) == false)
5477a250 355 CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter");
28352a04
LOK
356 delete output;
357
358 return bReturn;
2abe74eb 359}
13fd6a66 360
7bb4ed43
LOK
361bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
362{
363 bool bEom(false);
364 bool bIsError(msg.IsError());
365
366 if (msg.IsEmpty())
367 return bEom;
368
369 switch(msg.Message())
370 {
371 case MSGCODE_FRAME_START:
372 {
373 m_currentframe.Clear();
374 if (msg.Size() >= 2)
375 {
376 m_currentframe.initiator = msg.Initiator();
377 m_currentframe.destination = msg.Destination();
378 m_currentframe.ack = msg.IsACK();
379 m_currentframe.eom = msg.IsEOM();
380 }
381 if (m_currentframe.ack == 0x1)
382 {
383 m_lastInitiator = m_currentframe.initiator;
384 m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
385 }
386 }
387 break;
388 case MSGCODE_RECEIVE_FAILED:
389 {
390 m_currentframe.Clear();
391 if (m_lastInitiator != CECDEVICE_UNKNOWN)
392 bIsError = m_processor->HandleReceiveFailed(m_lastInitiator);
393 }
394 break;
395 case MSGCODE_FRAME_DATA:
396 {
397 if (msg.Size() >= 2)
398 {
399 m_currentframe.PushBack(msg[1]);
400 m_currentframe.eom = msg.IsEOM();
401 }
402 bEom = msg.IsEOM();
403 }
404 break;
405 default:
406 break;
407 }
408
409 CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
410 return bEom;
411}
412
413uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
1fc16cfd
LOK
414{
415 uint16_t iReturn(m_iFirmwareVersion);
416 if (!IsRunning())
417 return iReturn;
418
419 if (iReturn == CEC_FW_VERSION_UNKNOWN)
420 {
006b76b9 421 CLockObject lock(m_mutex);
5477a250 422 CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version");
1fc16cfd
LOK
423 CCECAdapterMessage *output = new CCECAdapterMessage;
424
425 output->PushBack(MSGSTART);
426 output->PushEscaped(MSGCODE_FIRMWARE_VERSION);
427 output->PushBack(MSGEND);
428 output->isTransmission = false;
429 output->expectControllerAck = false;
430
efed01e1
LOK
431 SendMessageToAdapter(output);
432 bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT;
1fc16cfd 433 delete output;
b1f94db1
LOK
434 if (!bWriteOk)
435 {
436 CLibCEC::AddLog(CEC_LOG_ERROR, "could not request the firmware version");
efed01e1 437 return iReturn;
b1f94db1 438 }
efed01e1
LOK
439
440 Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32
441 ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, 5 /* start + msgcode + 2 bytes for fw version + end */);
442 CCECAdapterMessage input;
443 if (Read(input, 0))
1fc16cfd 444 {
efed01e1
LOK
445 if (input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
446 CLibCEC::AddLog(CEC_LOG_ERROR, "invalid firmware version (size = %d, message = %d)", input.Size(), input.Message());
b1f94db1
LOK
447 else
448 {
449 m_iFirmwareVersion = (input[1] << 8 | input[2]);
450 iReturn = m_iFirmwareVersion;
451 }
1fc16cfd 452 }
efed01e1
LOK
453 else
454 {
455 CLibCEC::AddLog(CEC_LOG_ERROR, "no firmware version received");
456 }
1fc16cfd
LOK
457 }
458
459 return iReturn;
460}
461
7bb4ed43 462bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
a171d2fd 463{
7bb4ed43
LOK
464 m_iLineTimeout = iTimeout;
465 return true;
466 //TODO
467// bool bReturn(m_iLineTimeout != iTimeout);
468//
469// if (!bReturn)
470// {
471// CCECAdapterMessage *output = new CCECAdapterMessage;
472//
473// output->PushBack(MSGSTART);
474// output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
475// output->PushEscaped(iTimeout);
476// output->PushBack(MSGEND);
477// output->isTransmission = false;
478//
479// if ((bReturn = Write(output)) == false)
480// CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime");
481// delete output;
482// }
483//
484// return bReturn;
a171d2fd
LOK
485}
486
7bb4ed43 487bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
5dcf9f25
LOK
488{
489 bool bReturn(false);
bca69ca1 490 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask);
5dcf9f25
LOK
491
492 CCECAdapterMessage *output = new CCECAdapterMessage;
493
494 output->PushBack(MSGSTART);
495 output->PushEscaped(MSGCODE_SET_ACK_MASK);
496 output->PushEscaped(iMask >> 8);
497 output->PushEscaped((uint8_t)iMask);
498 output->PushBack(MSGEND);
499 output->isTransmission = false;
500
501 if ((bReturn = Write(output)) == false)
5477a250 502 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask");
5dcf9f25
LOK
503 delete output;
504
505 return bReturn;
506}
507
b057edad
BL
508
509bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
510{
511 bool bReturn(false);
bca69ca1 512 CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off");
b057edad
BL
513
514 CCECAdapterMessage *output = new CCECAdapterMessage;
515
516 output->PushBack(MSGSTART);
517 output->PushEscaped(MSGCODE_SET_CONTROLLED);
518 output->PushEscaped(controlled);
519 output->PushBack(MSGEND);
520 output->isTransmission = false;
521
522 if ((bReturn = Write(output)) == false)
523 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode");
524 delete output;
525
526 return bReturn;
527}
528
7bb4ed43 529bool CUSBCECAdapterCommunication::IsOpen(void)
13fd6a66 530{
b9eea66d 531 return !IsStopped() && m_port->IsOpen() && IsRunning();
13fd6a66 532}
ef7696f5 533
7bb4ed43 534bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
6729ac71
LOK
535{
536 bool bError(false);
537 bool bTransmitSucceeded(false);
b2f0b1ab 538 uint8_t iPacketsLeft(message.Size() / 4);
6729ac71
LOK
539
540 int64_t iNow = GetTimeMs();
b1f94db1 541 int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout);
6729ac71 542
b1f94db1 543 while (!bTransmitSucceeded && !bError && iNow < iTargetTime)
6729ac71 544 {
b1f94db1 545 ReadFromDevice(50);
6729ac71 546 CCECAdapterMessage msg;
b1f94db1 547 if (!Read(msg, 0))
6729ac71
LOK
548 {
549 iNow = GetTimeMs();
550 continue;
551 }
552
553 if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
554 {
555 m_processor->HandlePoll(msg.Initiator(), msg.Destination());
7bb4ed43 556 m_lastInitiator = msg.Initiator();
6729ac71
LOK
557 iNow = GetTimeMs();
558 continue;
559 }
560
561 if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
7bb4ed43
LOK
562 m_lastInitiator != CECDEVICE_UNKNOWN &&
563 m_processor->HandleReceiveFailed(m_lastInitiator))
6729ac71
LOK
564 {
565 iNow = GetTimeMs();
566 continue;
567 }
568
569 bError = msg.IsError();
570 if (bError)
571 {
b2f0b1ab 572 message.reply = msg.Message();
5477a250 573 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
574 }
575 else
576 {
577 switch(msg.Message())
578 {
579 case MSGCODE_COMMAND_ACCEPTED:
5477a250 580 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
581 if (iPacketsLeft > 0)
582 iPacketsLeft--;
b2f0b1ab 583 if (!message.isTransmission && iPacketsLeft == 0)
5dcf9f25 584 bTransmitSucceeded = true;
6729ac71
LOK
585 break;
586 case MSGCODE_TRANSMIT_SUCCEEDED:
5477a250 587 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
6729ac71
LOK
588 bTransmitSucceeded = (iPacketsLeft == 0);
589 bError = !bTransmitSucceeded;
b2f0b1ab 590 message.reply = MSGCODE_TRANSMIT_SUCCEEDED;
6729ac71
LOK
591 break;
592 default:
593 // ignore other data while waiting
594 break;
595 }
596
597 iNow = GetTimeMs();
598 }
599 }
600
b1f94db1
LOK
601 message.state = bTransmitSucceeded && !bError ?
602 ADAPTER_MESSAGE_STATE_SENT_ACKED :
603 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
604
6729ac71
LOK
605 return bTransmitSucceeded && !bError;
606}
607
7bb4ed43 608void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen)
ef7696f5
LOK
609{
610 CLockObject lock(m_mutex);
99666519 611 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
7bb4ed43
LOK
612 {
613 if (!m_bGotStart)
614 {
615 if (data[iPtr] == MSGSTART)
616 m_bGotStart = true;
617 }
618 else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove
619 {
620 if (m_currentAdapterMessage.Size() > 0)
621 CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
622 m_currentAdapterMessage.Clear();
623 m_bGotStart = true;
624 }
625 else if (data[iPtr] == MSGEND)
626 {
627 CCECAdapterMessage *newMessage = new CCECAdapterMessage;
628 newMessage->packet = m_currentAdapterMessage.packet;
629 m_inBuffer.Push(newMessage);
630 m_currentAdapterMessage.Clear();
631 m_bGotStart = false;
632 m_bNextIsEscaped = false;
960f33c6 633 m_bHasData = true;
7bb4ed43
LOK
634 m_rcvCondition.Signal();
635 }
636 else if (m_bNextIsEscaped)
637 {
638 m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET);
639 m_bNextIsEscaped = false;
640 }
641 else if (data[iPtr] == MSGESC)
642 {
643 m_bNextIsEscaped = true;
644 }
645 else
646 {
647 m_currentAdapterMessage.PushBack(data[iPtr]);
648 }
649 }
ef7696f5
LOK
650}
651
b1f94db1 652bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */)
ef7696f5 653{
99666519
LOK
654 ssize_t iBytesRead;
655 uint8_t buff[256];
ef7696f5
LOK
656 if (!m_port)
657 return false;
b1f94db1
LOK
658 if (iSize > 256)
659 iSize = 256;
ef7696f5 660
1fc16cfd 661 CLockObject lock(m_mutex);
b1f94db1 662 iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
ef7696f5
LOK
663 if (iBytesRead < 0 || iBytesRead > 256)
664 {
99666519 665 CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
60383b11 666 StopThread(false);
ef7696f5
LOK
667 return false;
668 }
669 else if (iBytesRead > 0)
99666519
LOK
670 {
671 AddData(buff, iBytesRead);
672 }
ef7696f5
LOK
673
674 return iBytesRead > 0;
675}
676
7bb4ed43 677void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
ef7696f5 678{
1fc16cfd 679 CLockObject adapterLock(m_mutex);
7bb4ed43
LOK
680 if (msg->tries == 1)
681 SetLineTimeout(msg->lineTimeout);
682 else
683 SetLineTimeout(msg->retryTimeout);
684
99666519 685 if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size())
ef7696f5 686 {
b74fd339 687 CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str());
ef7696f5
LOK
688 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
689 }
690 else
691 {
5477a250 692 CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent");
ef7696f5 693 msg->state = ADAPTER_MESSAGE_STATE_SENT;
b1f94db1
LOK
694
695 if (msg->expectControllerAck)
696 {
697 if (!WaitForAck(*msg))
698 CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack");
699 }
ef7696f5 700 }
960f33c6 701 msg->event.Signal();
ef7696f5
LOK
702}
703
7bb4ed43 704void CUSBCECAdapterCommunication::WriteNextCommand(void)
ef7696f5
LOK
705{
706 CCECAdapterMessage *msg(NULL);
707 if (m_outBuffer.Pop(msg))
708 SendMessageToAdapter(msg);
709}
cba904a6
LOK
710
711CStdString CUSBCECAdapterCommunication::GetPortName(void)
712{
713 CStdString strName;
714 strName = m_port->GetName();
715 return strName;
716}