Merge branch 'master' into release
[deb_libcec.git] / src / lib / adapter / Pulse-Eight / 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 "env.h"
34 #include "USBCECAdapterCommunication.h"
35
36 #include "USBCECAdapterCommands.h"
37 #include "USBCECAdapterMessageQueue.h"
38 #include "USBCECAdapterMessage.h"
39 #include "lib/platform/sockets/serialport.h"
40 #include "lib/platform/util/timeutils.h"
41 #include "lib/platform/util/util.h"
42 #include "lib/platform/util/edid.h"
43 #include "lib/platform/adl/adl-edid.h"
44 #include "lib/platform/nvidia/nv-edid.h"
45 #include "lib/LibCEC.h"
46 #include "lib/CECProcessor.h"
47
48 using namespace std;
49 using namespace CEC;
50 using namespace PLATFORM;
51
52 #define CEC_ADAPTER_PING_TIMEOUT 15000
53 #define CEC_ADAPTER_EEPROM_WRITE_INTERVAL 30000
54 #define CEC_ADAPTER_EEPROM_WRITE_RETRY 5000
55
56 // firmware version 2
57 #define CEC_LATEST_ADAPTER_FW_VERSION 2
58 // firmware date Thu Aug 2 08:31:24 UTC 2012
59 #define CEC_LATEST_ADAPTER_FW_DATE 0x501a4b0c
60
61 #define CEC_FW_DATE_EXTENDED_RESPONSE 0x501a4b0c
62
63 #define LIB_CEC m_callback->GetLib()
64
65 CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(IAdapterCommunicationCallback *callback, const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */) :
66 IAdapterCommunication(callback),
67 m_port(NULL),
68 m_iLineTimeout(0),
69 m_lastPollDestination(CECDEVICE_UNKNOWN),
70 m_bInitialised(false),
71 m_pingThread(NULL),
72 m_eepromWriteThread(NULL),
73 m_commands(NULL),
74 m_adapterMessageQueue(NULL)
75 {
76 m_logicalAddresses.Clear();
77 for (unsigned int iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
78 m_bWaitingForAck[iPtr] = false;
79 m_port = new CSerialPort(strPort, iBaudRate);
80 m_commands = new CUSBCECAdapterCommands(this);
81 }
82
83 CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void)
84 {
85 Close();
86 DELETE_AND_NULL(m_commands);
87 DELETE_AND_NULL(m_adapterMessageQueue);
88 DELETE_AND_NULL(m_port);
89 }
90
91 void CUSBCECAdapterCommunication::ResetMessageQueue(void)
92 {
93 DELETE_AND_NULL(m_adapterMessageQueue);
94 m_adapterMessageQueue = new CCECAdapterMessageQueue(this);
95 m_adapterMessageQueue->CreateThread();
96 }
97
98 bool CUSBCECAdapterCommunication::Open(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */, bool bSkipChecks /* = false */, bool bStartListening /* = true */)
99 {
100 bool bConnectionOpened(false);
101 {
102 CLockObject lock(m_mutex);
103
104 /* we need the port settings here */
105 if (!m_port)
106 {
107 LIB_CEC->AddLog(CEC_LOG_ERROR, "port is NULL");
108 return bConnectionOpened;
109 }
110
111 /* return true when the port is already open */
112 if (IsOpen())
113 {
114 LIB_CEC->AddLog(CEC_LOG_WARNING, "port is already open");
115 return true;
116 }
117
118 ResetMessageQueue();
119
120 /* try to open the connection */
121 CStdString strError;
122 CTimeout timeout(iTimeoutMs);
123 while (!bConnectionOpened && timeout.TimeLeft() > 0)
124 {
125 if ((bConnectionOpened = m_port->Open(timeout.TimeLeft())) == false)
126 {
127 strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str());
128 Sleep(250);
129 }
130 /* and retry every 250ms until the timeout passed */
131 }
132
133 /* return false when we couldn't connect */
134 if (!bConnectionOpened)
135 {
136 LIB_CEC->AddLog(CEC_LOG_ERROR, strError);
137
138 if (m_port->GetErrorNumber() == EACCES)
139 {
140 libcec_parameter param;
141 param.paramType = CEC_PARAMETER_TYPE_STRING;
142 param.paramData = (void*)"No permission to open the device";
143 LIB_CEC->Alert(CEC_ALERT_PERMISSION_ERROR, param);
144 }
145 else if (m_port->GetErrorNumber() == EBUSY)
146 {
147 libcec_parameter param;
148 param.paramType = CEC_PARAMETER_TYPE_STRING;
149 param.paramData = (void*)"The serial port is busy. Only one program can access the device directly.";
150 LIB_CEC->Alert(CEC_ALERT_PORT_BUSY, param);
151 }
152 return false;
153 }
154
155 LIB_CEC->AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
156 ClearInputBytes();
157 }
158
159 // always start by setting the ackmask to 0, to clear previous values
160 cec_logical_addresses addresses; addresses.Clear();
161 SetLogicalAddresses(addresses);
162
163 if (!CreateThread())
164 {
165 bConnectionOpened = false;
166 LIB_CEC->AddLog(CEC_LOG_ERROR, "could not create a communication thread");
167 }
168 else if (!bSkipChecks && !CheckAdapter())
169 {
170 bConnectionOpened = false;
171 LIB_CEC->AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks");
172 }
173 else if (bStartListening)
174 {
175 /* start the eeprom write thread, that handles all eeprom writes async */
176 m_eepromWriteThread = new CAdapterEepromWriteThread(this);
177 if (!m_eepromWriteThread->CreateThread())
178 {
179 bConnectionOpened = false;
180 LIB_CEC->AddLog(CEC_LOG_ERROR, "could not create the eeprom write thread");
181 }
182 else
183 {
184 /* start a ping thread, that will ping the adapter every 15 seconds
185 if it doesn't receive any ping for 30 seconds, it'll switch to auto mode */
186 m_pingThread = new CAdapterPingThread(this, CEC_ADAPTER_PING_TIMEOUT);
187 if (m_pingThread->CreateThread())
188 {
189 bConnectionOpened = true;
190 }
191 else
192 {
193 bConnectionOpened = false;
194 LIB_CEC->AddLog(CEC_LOG_ERROR, "could not create a ping thread");
195 }
196 }
197 }
198
199 if (!bConnectionOpened || !bStartListening)
200 StopThread(0);
201
202 return bConnectionOpened;
203 }
204
205 void CUSBCECAdapterCommunication::Close(void)
206 {
207 /* stop the reader thread */
208 StopThread(0);
209
210 CLockObject lock(m_mutex);
211
212 /* set the ackmask to 0 before closing the connection */
213 if (IsOpen() && m_port->GetErrorNumber() == 0)
214 {
215 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - closing the connection", __FUNCTION__);
216 cec_logical_addresses addresses; addresses.Clear();
217 SetLogicalAddresses(addresses);
218 if (m_commands->GetFirmwareVersion() >= 2)
219 SetControlledMode(false);
220 }
221
222 m_adapterMessageQueue->Clear();
223
224 /* stop and delete the write thread */
225 if (m_eepromWriteThread)
226 m_eepromWriteThread->Stop();
227 DELETE_AND_NULL(m_eepromWriteThread);
228
229 /* stop and delete the ping thread */
230 DELETE_AND_NULL(m_pingThread);
231
232 /* close and delete the com port connection */
233 if (m_port)
234 m_port->Close();
235 }
236
237 cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout, bool UNUSED(bIsReply))
238 {
239 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
240 if (!IsRunning())
241 return retVal;
242
243 CCECAdapterMessage *output = new CCECAdapterMessage(data, iLineTimeout);
244
245 /* mark as waiting for an ack from the destination */
246 MarkAsWaiting(data.destination);
247
248 /* send the message */
249 bRetry = (!m_adapterMessageQueue->Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0;
250 if (bRetry)
251 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
252 retVal = output->state;
253
254 delete output;
255 return retVal;
256 }
257
258 void *CUSBCECAdapterCommunication::Process(void)
259 {
260 CCECAdapterMessage msg;
261 LIB_CEC->AddLog(CEC_LOG_DEBUG, "communication thread started");
262
263 while (!IsStopped())
264 {
265 /* read from the serial port */
266 if (!ReadFromDevice(50, 5))
267 {
268 libcec_parameter param;
269 param.paramData = NULL; param.paramType = CEC_PARAMETER_TYPE_UNKOWN;
270 LIB_CEC->Alert(CEC_ALERT_CONNECTION_LOST, param);
271
272 break;
273 }
274
275 /* TODO sleep 5 ms so other threads can get a lock */
276 Sleep(5);
277 }
278
279 m_adapterMessageQueue->Clear();
280 LIB_CEC->AddLog(CEC_LOG_DEBUG, "communication thread ended");
281 return NULL;
282 }
283
284 bool CUSBCECAdapterCommunication::HandlePoll(const CCECAdapterMessage &msg)
285 {
286 bool bIsError(msg.IsError());
287 cec_adapter_messagecode messageCode(msg.Message());
288 CLockObject lock(m_mutex);
289
290 if (messageCode == MSGCODE_FRAME_START && msg.IsACK())
291 {
292 m_lastPollDestination = msg.Destination();
293 if (msg.Destination() < CECDEVICE_BROADCAST)
294 {
295 if (!m_bWaitingForAck[msg.Destination()] && !msg.IsEOM())
296 {
297 if (m_callback)
298 m_callback->HandlePoll(msg.Initiator(), msg.Destination());
299 }
300 else
301 m_bWaitingForAck[msg.Destination()] = false;
302 }
303 }
304 else if (messageCode == MSGCODE_RECEIVE_FAILED)
305 {
306 /* hack to suppress warnings when an LG is polling */
307 if (m_lastPollDestination != CECDEVICE_UNKNOWN)
308 bIsError = m_callback->HandleReceiveFailed(m_lastPollDestination);
309 }
310
311 return bIsError;
312 }
313
314 void CUSBCECAdapterCommunication::MarkAsWaiting(const cec_logical_address dest)
315 {
316 /* mark as waiting for an ack from the destination */
317 if (dest < CECDEVICE_BROADCAST)
318 {
319 CLockObject lock(m_mutex);
320 m_bWaitingForAck[dest] = true;
321 }
322 }
323
324 void CUSBCECAdapterCommunication::ClearInputBytes(uint32_t iTimeout /* = CEC_CLEAR_INPUT_DEFAULT_WAIT */)
325 {
326 CTimeout timeout(iTimeout);
327 uint8_t buff[1024];
328 ssize_t iBytesRead(0);
329 bool bGotMsgEnd(true);
330
331 while (timeout.TimeLeft() > 0 && ((iBytesRead = m_port->Read(buff, 1024, 5)) > 0 || !bGotMsgEnd))
332 {
333 bGotMsgEnd = false;
334 /* if something was received, wait for MSGEND */
335 for (ssize_t iPtr = 0; iPtr < iBytesRead; iPtr++)
336 bGotMsgEnd = buff[iPtr] == MSGEND;
337 }
338 }
339
340 bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
341 {
342 bool bReturn(true);
343 bool bChanged(false);
344
345 /* only send the command if the timeout changed */
346 {
347 CLockObject lock(m_mutex);
348 bChanged = (m_iLineTimeout != iTimeout);
349 m_iLineTimeout = iTimeout;
350 }
351
352 if (bChanged)
353 bReturn = m_commands->SetLineTimeout(iTimeout);
354
355 return bReturn;
356 }
357
358 bool CUSBCECAdapterCommunication::WriteToDevice(CCECAdapterMessage *message)
359 {
360 CLockObject adapterLock(m_mutex);
361 if (!IsOpen())
362 {
363 LIB_CEC->AddLog(CEC_LOG_DEBUG, "error writing command '%s' to serial port '%s': the connection is closed", CCECAdapterMessage::ToString(message->Message()), m_port->GetName().c_str());
364 message->state = ADAPTER_MESSAGE_STATE_ERROR;
365 return false;
366 }
367
368 /* write the message */
369 if (m_port->Write(message->packet.data, message->Size()) != (ssize_t) message->Size())
370 {
371 LIB_CEC->AddLog(CEC_LOG_DEBUG, "error writing command '%s' to serial port '%s': %s", CCECAdapterMessage::ToString(message->Message()), m_port->GetName().c_str(), m_port->GetError().c_str());
372 message->state = ADAPTER_MESSAGE_STATE_ERROR;
373 // this will trigger an alert in the reader thread
374 m_port->Close();
375 return false;
376 }
377
378 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command '%s' sent", message->IsTranmission() ? "CEC transmission" : CCECAdapterMessage::ToString(message->Message()));
379 message->state = ADAPTER_MESSAGE_STATE_SENT;
380 return true;
381 }
382
383 bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */)
384 {
385 ssize_t iBytesRead(0);
386 uint8_t buff[256];
387 if (iSize > 256)
388 iSize = 256;
389
390 /* read from the serial port */
391 {
392 CLockObject lock(m_mutex);
393 if (!IsOpen())
394 return false;
395
396 iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
397
398 if (m_port->GetErrorNumber())
399 {
400 LIB_CEC->AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
401 m_port->Close();
402 return false;
403 }
404 }
405
406 if (iBytesRead < 0 || iBytesRead > 256)
407 return false;
408 else if (iBytesRead > 0)
409 {
410 /* add the data to the current frame */
411 m_adapterMessageQueue->AddData(buff, iBytesRead);
412 }
413
414 return true;
415 }
416
417 CCECAdapterMessage *CUSBCECAdapterCommunication::SendCommand(cec_adapter_messagecode msgCode, CCECAdapterMessage &params, bool bIsRetry /* = false */)
418 {
419 if (!IsOpen() || !m_adapterMessageQueue)
420 return NULL;
421
422 /* create the adapter message for this command */
423 CCECAdapterMessage *output = new CCECAdapterMessage;
424 output->PushBack(MSGSTART);
425 output->PushEscaped((uint8_t)msgCode);
426 output->Append(params);
427 output->PushBack(MSGEND);
428
429 /* write the command */
430 if (!m_adapterMessageQueue->Write(output))
431 {
432 // this will trigger an alert in the reader thread
433 if (output->state == ADAPTER_MESSAGE_STATE_ERROR)
434 m_port->Close();
435 return output;
436 }
437 else
438 {
439 if (!bIsRetry && output->Reply() == MSGCODE_COMMAND_REJECTED && msgCode != MSGCODE_SET_CONTROLLED &&
440 msgCode != MSGCODE_GET_BUILDDATE /* same messagecode value had a different meaning in older fw builds */)
441 {
442 /* if the controller reported that the command was rejected, and we didn't send the command
443 to set controlled mode, then the controller probably switched to auto mode. set controlled
444 mode and retry */
445 LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting controlled mode and retrying");
446 delete output;
447 if (SetControlledMode(true))
448 return SendCommand(msgCode, params, true);
449 }
450 }
451
452 return output;
453 }
454
455 bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
456 {
457 bool bReturn(false);
458 CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
459
460 /* try to ping the adapter */
461 bool bPinged(false);
462 unsigned iPingTry(0);
463 while (timeout.TimeLeft() > 0 && (bPinged = PingAdapter()) == false)
464 {
465 LIB_CEC->AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry);
466 CEvent::Sleep(500);
467 }
468
469 /* try to read the firmware version */
470 if (bPinged && timeout.TimeLeft() > 0 && m_commands->RequestFirmwareVersion() >= 2)
471 {
472 /* try to set controlled mode for v2+ firmwares */
473 unsigned iControlledTry(0);
474 bool bControlled(false);
475 while (timeout.TimeLeft() > 0 && (bControlled = SetControlledMode(true)) == false)
476 {
477 LIB_CEC->AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry);
478 CEvent::Sleep(500);
479 }
480 bReturn = bControlled;
481 }
482 else
483 bReturn = true;
484
485 /* try to read the build date */
486 m_commands->RequestBuildDate();
487
488 SetInitialised(bReturn);
489 return bReturn;
490 }
491
492 bool CUSBCECAdapterCommunication::IsOpen(void)
493 {
494 /* thread is not being stopped, the port is open and the thread is running */
495 return !IsStopped() && m_port->IsOpen() && IsRunning();
496 }
497
498 std::string CUSBCECAdapterCommunication::GetError(void) const
499 {
500 return m_port->GetError();
501 }
502
503 void CUSBCECAdapterCommunication::SetInitialised(bool bSetTo /* = true */)
504 {
505 CLockObject lock(m_mutex);
506 m_bInitialised = bSetTo;
507 }
508
509 bool CUSBCECAdapterCommunication::IsInitialised(void)
510 {
511 CLockObject lock(m_mutex);
512 return m_bInitialised;
513 }
514
515 bool CUSBCECAdapterCommunication::StartBootloader(void)
516 {
517 if (m_port->IsOpen() && m_commands->StartBootloader())
518 {
519 m_port->Close();
520 return true;
521 }
522 return false;
523 }
524
525 bool CUSBCECAdapterCommunication::SetLogicalAddresses(const cec_logical_addresses &addresses)
526 {
527 {
528 CLockObject lock(m_mutex);
529 if (m_logicalAddresses == addresses)
530 return true;
531 }
532
533 if (IsOpen() && m_commands->SetAckMask(addresses.AckMask()))
534 {
535 CLockObject lock(m_mutex);
536 m_logicalAddresses = addresses;
537 return true;
538 }
539
540 LIB_CEC->AddLog(CEC_LOG_DEBUG, "couldn't change the ackmask: the connection is closed");
541 return false;
542 }
543
544 cec_logical_addresses CUSBCECAdapterCommunication::GetLogicalAddresses(void)
545 {
546 cec_logical_addresses addresses;
547 CLockObject lock(m_mutex);
548 addresses = m_logicalAddresses;
549 return addresses;
550 }
551
552 bool CUSBCECAdapterCommunication::PingAdapter(void)
553 {
554 return IsOpen() ? m_commands->PingAdapter() : false;
555 }
556
557 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
558 {
559 return m_commands ? m_commands->GetFirmwareVersion() : CEC_FW_VERSION_UNKNOWN;
560 }
561
562 uint32_t CUSBCECAdapterCommunication::GetFirmwareBuildDate(void)
563 {
564 uint32_t iBuildDate(0);
565 if (m_commands)
566 iBuildDate = m_commands->GetPersistedBuildDate();
567 if (iBuildDate == 0 && IsOpen())
568 iBuildDate = m_commands->RequestBuildDate();
569
570 return iBuildDate;
571 }
572
573 bool CUSBCECAdapterCommunication::ProvidesExtendedResponse(void)
574 {
575 uint32_t iBuildDate(0);
576 if (m_commands)
577 iBuildDate = m_commands->GetPersistedBuildDate();
578
579 return iBuildDate >= CEC_FW_DATE_EXTENDED_RESPONSE;
580 }
581
582 bool CUSBCECAdapterCommunication::IsRunningLatestFirmware(void)
583 {
584 return GetFirmwareBuildDate() >= CEC_LATEST_ADAPTER_FW_DATE &&
585 GetFirmwareVersion() >= CEC_LATEST_ADAPTER_FW_VERSION;
586 }
587
588 bool CUSBCECAdapterCommunication::PersistConfiguration(const libcec_configuration &configuration)
589 {
590 return IsOpen() ?
591 m_commands->PersistConfiguration(configuration) && m_eepromWriteThread->Write() :
592 false;
593 }
594
595 bool CUSBCECAdapterCommunication::GetConfiguration(libcec_configuration &configuration)
596 {
597 return IsOpen() ? m_commands->GetConfiguration(configuration) : false;
598 }
599
600 std::string CUSBCECAdapterCommunication::GetPortName(void)
601 {
602 return m_port->GetName();
603 }
604
605 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
606 {
607 return IsOpen() ? m_commands->SetControlledMode(controlled) : false;
608 }
609
610 uint16_t CUSBCECAdapterCommunication::GetPhysicalAddress(void)
611 {
612 uint16_t iPA(0);
613
614 // try to get the PA from ADL
615 #if defined(HAS_ADL_EDID_PARSER)
616 {
617 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - trying to get the physical address via ADL", __FUNCTION__);
618 CADLEdidParser adl;
619 iPA = adl.GetPhysicalAddress();
620 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - ADL returned physical address %04x", __FUNCTION__, iPA);
621 }
622 #endif
623
624 // try to get the PA from the nvidia driver
625 #if defined(HAS_NVIDIA_EDID_PARSER)
626 if (iPA == 0)
627 {
628 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - trying to get the physical address via nvidia driver", __FUNCTION__);
629 CNVEdidParser nv;
630 iPA = nv.GetPhysicalAddress();
631 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - nvidia driver returned physical address %04x", __FUNCTION__, iPA);
632 }
633 #endif
634
635 // try to get the PA from the OS
636 if (iPA == 0)
637 {
638 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - trying to get the physical address from the OS", __FUNCTION__);
639 iPA = CEDIDParser::GetPhysicalAddress();
640 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - OS returned physical address %04x", __FUNCTION__, iPA);
641 }
642
643 return iPA;
644 }
645
646 void *CAdapterPingThread::Process(void)
647 {
648 while (!IsStopped())
649 {
650 if (m_timeout.TimeLeft() == 0)
651 {
652 /* reinit the timeout */
653 m_timeout.Init(CEC_ADAPTER_PING_TIMEOUT);
654
655 /* send a ping to the adapter */
656 bool bPinged(false);
657 int iFailedCounter(0);
658 while (!bPinged && iFailedCounter < 3)
659 {
660 if (!m_com->PingAdapter())
661 {
662 /* sleep and retry */
663 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
664 ++iFailedCounter;
665 }
666 else
667 {
668 bPinged = true;
669 }
670 }
671
672 if (iFailedCounter == 3)
673 {
674 /* failed to ping the adapter 3 times in a row. something must be wrong with the connection */
675 m_com->LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to ping the adapter 3 times in a row. closing the connection.");
676 m_com->StopThread(false);
677 break;
678 }
679 }
680
681 Sleep(5);
682 }
683 return NULL;
684 }
685
686 void CAdapterEepromWriteThread::Stop(void)
687 {
688 StopThread(-1);
689 {
690 CLockObject lock(m_mutex);
691 if (m_iScheduleEepromWrite > 0)
692 m_com->LIB_CEC->AddLog(CEC_LOG_WARNING, "write thread stopped while a write was queued");
693 m_condition.Signal();
694 }
695 StopThread();
696 }
697
698 void *CAdapterEepromWriteThread::Process(void)
699 {
700 while (!IsStopped())
701 {
702 CLockObject lock(m_mutex);
703 if ((m_iScheduleEepromWrite > 0 && m_iScheduleEepromWrite < GetTimeMs()) ||
704 m_condition.Wait(m_mutex, m_bWrite, 100))
705 {
706 m_bWrite = false;
707 if (m_com->m_commands->WriteEEPROM())
708 {
709 m_iLastEepromWrite = GetTimeMs();
710 m_iScheduleEepromWrite = 0;
711 }
712 else
713 {
714 m_iScheduleEepromWrite = GetTimeMs() + CEC_ADAPTER_EEPROM_WRITE_RETRY;
715 }
716 }
717 }
718 return NULL;
719 }
720
721 bool CAdapterEepromWriteThread::Write(void)
722 {
723 CLockObject lock(m_mutex);
724 if (m_iScheduleEepromWrite == 0)
725 {
726 int64_t iNow = GetTimeMs();
727 if (m_iLastEepromWrite + CEC_ADAPTER_EEPROM_WRITE_INTERVAL > iNow)
728 {
729 m_com->LIB_CEC->AddLog(CEC_LOG_DEBUG, "delaying eeprom write by %ld ms", m_iLastEepromWrite + CEC_ADAPTER_EEPROM_WRITE_INTERVAL - iNow);
730 m_iScheduleEepromWrite = m_iLastEepromWrite + CEC_ADAPTER_EEPROM_WRITE_INTERVAL;
731 }
732 else
733 {
734 m_bWrite = true;
735 m_condition.Signal();
736 }
737 }
738 return true;
739 }