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