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