2d8957b05e839004b6c7ce771d080448e9c478ef
[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.Size() > 0;
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 switch(msg.Message())
395 {
396 case MSGCODE_FRAME_START:
397 {
398 m_currentframe.Clear();
399 if (msg.Size() >= 2)
400 {
401 m_currentframe.initiator = msg.Initiator();
402 m_currentframe.destination = msg.Destination();
403 m_currentframe.ack = msg.IsACK();
404 m_currentframe.eom = msg.IsEOM();
405 }
406 if (m_currentframe.ack == 0x1)
407 {
408 m_lastInitiator = m_currentframe.initiator;
409 m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
410 }
411 }
412 break;
413 case MSGCODE_RECEIVE_FAILED:
414 {
415 m_currentframe.Clear();
416 if (m_lastInitiator != CECDEVICE_UNKNOWN)
417 bIsError = m_processor->HandleReceiveFailed(m_lastInitiator);
418 }
419 break;
420 case MSGCODE_FRAME_DATA:
421 {
422 if (msg.Size() >= 2)
423 {
424 m_currentframe.PushBack(msg[1]);
425 m_currentframe.eom = msg.IsEOM();
426 }
427 bEom = msg.IsEOM();
428 }
429 break;
430 default:
431 break;
432 }
433
434 CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
435 return bEom;
436 }
437
438 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
439 {
440 uint16_t iReturn(m_iFirmwareVersion);
441 if (!IsRunning())
442 return iReturn;
443
444 if (iReturn == CEC_FW_VERSION_UNKNOWN)
445 {
446 CLockObject lock(m_mutex);
447 CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version");
448 CCECAdapterMessage *output = new CCECAdapterMessage;
449
450 output->PushBack(MSGSTART);
451 output->PushEscaped(MSGCODE_FIRMWARE_VERSION);
452 output->PushBack(MSGEND);
453 output->isTransmission = false;
454 output->expectControllerAck = false;
455
456 SendMessageToAdapter(output);
457 bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT;
458 delete output;
459 if (!bWriteOk)
460 {
461 CLibCEC::AddLog(CEC_LOG_ERROR, "could not request the firmware version");
462 return iReturn;
463 }
464
465 Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32
466 ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, 5 /* start + msgcode + 2 bytes for fw version + end */);
467 CCECAdapterMessage input;
468 if (Read(input, 0))
469 {
470 if (input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
471 CLibCEC::AddLog(CEC_LOG_ERROR, "invalid firmware version (size = %d, message = %d)", input.Size(), input.Message());
472 else
473 {
474 m_iFirmwareVersion = (input[1] << 8 | input[2]);
475 iReturn = m_iFirmwareVersion;
476 }
477 }
478 else
479 {
480 CLibCEC::AddLog(CEC_LOG_ERROR, "no firmware version received");
481 }
482 }
483
484 return iReturn;
485 }
486
487 bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
488 {
489 m_iLineTimeout = iTimeout;
490 return true;
491 //TODO
492 // bool bReturn(m_iLineTimeout != iTimeout);
493 //
494 // if (!bReturn)
495 // {
496 // CCECAdapterMessage *output = new CCECAdapterMessage;
497 //
498 // output->PushBack(MSGSTART);
499 // output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
500 // output->PushEscaped(iTimeout);
501 // output->PushBack(MSGEND);
502 // output->isTransmission = false;
503 //
504 // if ((bReturn = Write(output)) == false)
505 // CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime");
506 // delete output;
507 // }
508 //
509 // return bReturn;
510 }
511
512 bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
513 {
514 return SetAckMaskInternal(iMask, false);
515 }
516
517 bool CUSBCECAdapterCommunication::SetAckMaskInternal(uint16_t iMask, bool bWriteDirectly /* = false */)
518 {
519 bool bReturn(false);
520 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask);
521
522 CCECAdapterMessage *output = new CCECAdapterMessage;
523
524 output->PushBack(MSGSTART);
525 output->PushEscaped(MSGCODE_SET_ACK_MASK);
526 output->PushEscaped(iMask >> 8);
527 output->PushEscaped((uint8_t)iMask);
528 output->PushBack(MSGEND);
529 output->isTransmission = false;
530
531 if (bWriteDirectly)
532 SendMessageToAdapter(output);
533 else if ((bReturn = Write(output)) == false)
534 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask");
535 delete output;
536
537 return bReturn;
538 }
539
540
541 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
542 {
543 bool bReturn(false);
544 CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off");
545
546 CCECAdapterMessage *output = new CCECAdapterMessage;
547
548 output->PushBack(MSGSTART);
549 output->PushEscaped(MSGCODE_SET_CONTROLLED);
550 output->PushEscaped(controlled);
551 output->PushBack(MSGEND);
552 output->isTransmission = false;
553
554 if ((bReturn = Write(output)) == false)
555 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode");
556 delete output;
557
558 return bReturn;
559 }
560
561 bool CUSBCECAdapterCommunication::IsOpen(void)
562 {
563 return !IsStopped() && m_port->IsOpen() && IsRunning();
564 }
565
566 bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
567 {
568 bool bError(false);
569 bool bTransmitSucceeded(false);
570 uint8_t iPacketsLeft(message.Size() / 4);
571
572 int64_t iNow = GetTimeMs();
573 int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout);
574
575 while (!bTransmitSucceeded && !bError && iNow < iTargetTime)
576 {
577 ReadFromDevice(50);
578 CCECAdapterMessage msg;
579 if (!Read(msg, 0))
580 {
581 iNow = GetTimeMs();
582 continue;
583 }
584
585 if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
586 {
587 m_processor->HandlePoll(msg.Initiator(), msg.Destination());
588 m_lastInitiator = msg.Initiator();
589 iNow = GetTimeMs();
590 continue;
591 }
592
593 if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
594 m_lastInitiator != CECDEVICE_UNKNOWN &&
595 m_processor->HandleReceiveFailed(m_lastInitiator))
596 {
597 iNow = GetTimeMs();
598 continue;
599 }
600
601 bError = msg.IsError();
602 if (bError)
603 {
604 message.reply = msg.Message();
605 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
606 }
607 else
608 {
609 switch(msg.Message())
610 {
611 case MSGCODE_COMMAND_ACCEPTED:
612 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
613 if (iPacketsLeft > 0)
614 iPacketsLeft--;
615 if (!message.isTransmission && iPacketsLeft == 0)
616 bTransmitSucceeded = true;
617 break;
618 case MSGCODE_TRANSMIT_SUCCEEDED:
619 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
620 bTransmitSucceeded = (iPacketsLeft == 0);
621 bError = !bTransmitSucceeded;
622 message.reply = MSGCODE_TRANSMIT_SUCCEEDED;
623 break;
624 default:
625 // ignore other data while waiting
626 break;
627 }
628
629 iNow = GetTimeMs();
630 }
631 }
632
633 message.state = bTransmitSucceeded && !bError ?
634 ADAPTER_MESSAGE_STATE_SENT_ACKED :
635 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
636
637 return bTransmitSucceeded && !bError;
638 }
639
640 void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen)
641 {
642 CLockObject lock(m_mutex);
643 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
644 {
645 if (!m_bGotStart)
646 {
647 if (data[iPtr] == MSGSTART)
648 m_bGotStart = true;
649 }
650 else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove
651 {
652 if (m_currentAdapterMessage.Size() > 0)
653 CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
654 m_currentAdapterMessage.Clear();
655 m_bGotStart = true;
656 }
657 else if (data[iPtr] == MSGEND)
658 {
659 CCECAdapterMessage *newMessage = new CCECAdapterMessage;
660 newMessage->packet = m_currentAdapterMessage.packet;
661 m_inBuffer.Push(newMessage);
662 m_currentAdapterMessage.Clear();
663 m_bGotStart = false;
664 m_bNextIsEscaped = false;
665 m_bHasData = true;
666 m_rcvCondition.Signal();
667 }
668 else if (m_bNextIsEscaped)
669 {
670 m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET);
671 m_bNextIsEscaped = false;
672 }
673 else if (data[iPtr] == MSGESC)
674 {
675 m_bNextIsEscaped = true;
676 }
677 else
678 {
679 m_currentAdapterMessage.PushBack(data[iPtr]);
680 }
681 }
682 }
683
684 bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */)
685 {
686 ssize_t iBytesRead;
687 uint8_t buff[256];
688 if (!m_port)
689 return false;
690 if (iSize > 256)
691 iSize = 256;
692
693 CLockObject lock(m_mutex);
694 iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
695 if (iBytesRead < 0 || iBytesRead > 256)
696 {
697 CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
698 StopThread(false);
699 return false;
700 }
701 else if (iBytesRead > 0)
702 {
703 AddData(buff, iBytesRead);
704 }
705
706 return iBytesRead > 0;
707 }
708
709 void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
710 {
711 CLockObject adapterLock(m_mutex);
712 if (!m_port->IsOpen())
713 {
714 CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: the connection is closed");
715 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
716 return;
717 }
718
719 if (msg->tries == 1)
720 SetLineTimeout(msg->lineTimeout);
721 else
722 SetLineTimeout(msg->retryTimeout);
723
724 if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size())
725 {
726 CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str());
727 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
728 }
729 else
730 {
731 CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent");
732 msg->state = ADAPTER_MESSAGE_STATE_SENT;
733
734 if (msg->expectControllerAck)
735 {
736 if (!WaitForAck(*msg))
737 CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack");
738 }
739 }
740 msg->event.Signal();
741 }
742
743 void CUSBCECAdapterCommunication::WriteNextCommand(void)
744 {
745 CCECAdapterMessage *msg(NULL);
746 if (m_outBuffer.Pop(msg))
747 SendMessageToAdapter(msg);
748 }
749
750 CStdString CUSBCECAdapterCommunication::GetPortName(void)
751 {
752 CStdString strName;
753 strName = m_port->GetName();
754 return strName;
755 }