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