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