f3c429453225c1fe47dd860ca24bea7cf5bbd3ad
[deb_libcec.git] / src / lib / devices / CECBusDevice.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 "CECBusDevice.h"
35
36 #include "lib/CECProcessor.h"
37 #include "lib/CECClient.h"
38 #include "lib/implementations/ANCommandHandler.h"
39 #include "lib/implementations/CECCommandHandler.h"
40 #include "lib/implementations/SLCommandHandler.h"
41 #include "lib/implementations/VLCommandHandler.h"
42 #include "lib/implementations/PHCommandHandler.h"
43 #include "lib/LibCEC.h"
44 #include "lib/CECTypeUtils.h"
45 #include "lib/platform/util/timeutils.h"
46 #include "lib/platform/util/util.h"
47
48 #include "CECAudioSystem.h"
49 #include "CECPlaybackDevice.h"
50 #include "CECRecordingDevice.h"
51 #include "CECTuner.h"
52 #include "CECTV.h"
53
54 using namespace std;
55 using namespace CEC;
56 using namespace PLATFORM;
57
58 #define LIB_CEC m_processor->GetLib()
59 #define ToString(p) CCECTypeUtils::ToString(p)
60
61 CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */) :
62 m_type (CEC_DEVICE_TYPE_RESERVED),
63 m_iPhysicalAddress (iPhysicalAddress),
64 m_iStreamPath (CEC_INVALID_PHYSICAL_ADDRESS),
65 m_iLogicalAddress (iLogicalAddress),
66 m_powerStatus (CEC_POWER_STATUS_UNKNOWN),
67 m_processor (processor),
68 m_vendor (CEC_VENDOR_UNKNOWN),
69 m_bReplaceHandler (false),
70 m_menuState (CEC_MENU_STATE_ACTIVATED),
71 m_bActiveSource (false),
72 m_iLastActive (0),
73 m_iLastPowerStateUpdate (0),
74 m_cecVersion (CEC_VERSION_UNKNOWN),
75 m_deviceStatus (CEC_DEVICE_STATUS_UNKNOWN),
76 m_iHandlerUseCount (0),
77 m_bAwaitingReceiveFailed(false),
78 m_bVendorIdRequested (false),
79 m_waitForResponse (new CWaitForResponse),
80 m_bImageViewOnSent (false)
81 {
82 m_handler = new CCECCommandHandler(this);
83
84 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
85 m_menuLanguage.language[iPtr] = '?';
86 m_menuLanguage.language[3] = 0;
87 m_menuLanguage.device = iLogicalAddress;
88
89 m_strDeviceName = ToString(m_iLogicalAddress);
90 }
91
92 CCECBusDevice::~CCECBusDevice(void)
93 {
94 DELETE_AND_NULL(m_handler);
95 DELETE_AND_NULL(m_waitForResponse);
96 }
97
98 bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
99 {
100 if (m_iLogicalAddress == CECDEVICE_BROADCAST)
101 return false;
102
103 bool bInitHandler(false);
104 {
105 CLockObject lock(m_mutex);
106 CLockObject handlerLock(m_handlerMutex);
107 if (m_iHandlerUseCount > 0)
108 return false;
109
110 MarkBusy();
111
112 if (m_vendor != m_handler->GetVendorId())
113 {
114 if (CCECCommandHandler::HasSpecificHandler(m_vendor))
115 {
116 LIB_CEC->AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
117
118 int32_t iTransmitTimeout = m_handler->m_iTransmitTimeout;
119 int32_t iTransmitWait = m_handler->m_iTransmitWait;
120 int8_t iTransmitRetries = m_handler->m_iTransmitRetries;
121 int64_t iActiveSourcePending = m_handler->m_iActiveSourcePending;
122
123 DELETE_AND_NULL(m_handler);
124
125 switch (m_vendor)
126 {
127 case CEC_VENDOR_SAMSUNG:
128 m_handler = new CANCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
129 break;
130 case CEC_VENDOR_LG:
131 m_handler = new CSLCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
132 break;
133 case CEC_VENDOR_PANASONIC:
134 m_handler = new CVLCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
135 break;
136 case CEC_VENDOR_PHILIPS:
137 m_handler = new CPHCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
138 break;
139 default:
140 m_handler = new CCECCommandHandler(this, iTransmitTimeout, iTransmitWait, iTransmitRetries, iActiveSourcePending);
141 break;
142 }
143
144 m_handler->SetVendorId(m_vendor);
145 bInitHandler = true;
146 }
147 }
148 }
149
150 if (bInitHandler)
151 {
152 CCECBusDevice *primary = GetProcessor()->GetPrimaryDevice();
153 if (primary->GetLogicalAddress() != CECDEVICE_UNREGISTERED)
154 {
155 m_handler->InitHandler();
156
157 if (bActivateSource && IsHandledByLibCEC() && IsActiveSource())
158 m_handler->ActivateSource();
159 }
160 }
161
162 MarkReady();
163
164 return true;
165 }
166
167 CCECCommandHandler *CCECBusDevice::GetHandler(void)
168 {
169 ReplaceHandler(false);
170 MarkBusy();
171 return m_handler;
172 }
173
174 bool CCECBusDevice::HandleCommand(const cec_command &command)
175 {
176 bool bHandled(false);
177
178 /* update "last active" */
179 {
180 CLockObject lock(m_mutex);
181 m_iLastActive = GetTimeMs();
182 MarkBusy();
183 }
184
185 /* handle the command */
186 bHandled = m_handler->HandleCommand(command);
187
188 /* change status to present */
189 if (bHandled && GetLogicalAddress() != CECDEVICE_BROADCAST && command.opcode_set == 1)
190 {
191 CLockObject lock(m_mutex);
192 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
193 {
194 if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
195 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
196 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
197 }
198 }
199
200 MarkReady();
201 return bHandled;
202 }
203
204 const char* CCECBusDevice::GetLogicalAddressName(void) const
205 {
206 return ToString(m_iLogicalAddress);
207 }
208
209 bool CCECBusDevice::IsPresent(void)
210 {
211 CLockObject lock(m_mutex);
212 return m_deviceStatus == CEC_DEVICE_STATUS_PRESENT;
213 }
214
215 bool CCECBusDevice::IsHandledByLibCEC(void)
216 {
217 CLockObject lock(m_mutex);
218 return m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC;
219 }
220
221 void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
222 {
223 // some commands should never be marked as unsupported
224 if (opcode == CEC_OPCODE_VENDOR_COMMAND ||
225 opcode == CEC_OPCODE_VENDOR_COMMAND_WITH_ID ||
226 opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN ||
227 opcode == CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP ||
228 opcode == CEC_OPCODE_ABORT ||
229 opcode == CEC_OPCODE_FEATURE_ABORT ||
230 opcode == CEC_OPCODE_NONE)
231 return;
232
233 {
234 CLockObject lock(m_mutex);
235 if (m_unsupportedFeatures.find(opcode) == m_unsupportedFeatures.end())
236 {
237 LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
238 m_unsupportedFeatures.insert(opcode);
239 }
240 }
241
242 // signal threads that are waiting for a reponse
243 MarkBusy();
244 SignalOpcode(cec_command::GetResponseOpcode(opcode));
245 MarkReady();
246 }
247
248 bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode)
249 {
250 CLockObject lock(m_mutex);
251 bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end());
252 if (bUnsupported)
253 LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
254 return bUnsupported;
255 }
256
257 bool CCECBusDevice::TransmitKeypress(const cec_logical_address initiator, cec_user_control_code key, bool bWait /* = true */)
258 {
259 MarkBusy();
260 bool bReturn = m_handler->TransmitKeypress(initiator, m_iLogicalAddress, key, bWait);
261 MarkReady();
262 return bReturn;
263 }
264
265 bool CCECBusDevice::TransmitKeyRelease(const cec_logical_address initiator, bool bWait /* = true */)
266 {
267 MarkBusy();
268 bool bReturn = m_handler->TransmitKeyRelease(initiator, m_iLogicalAddress, bWait);
269 MarkReady();
270 return bReturn;
271 }
272
273 cec_version CCECBusDevice::GetCecVersion(const cec_logical_address initiator, bool bUpdate /* = false */)
274 {
275 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
276 bool bRequestUpdate(false);
277 {
278 CLockObject lock(m_mutex);
279 bRequestUpdate = bIsPresent &&
280 (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN);
281 }
282
283 if (bRequestUpdate)
284 {
285 CheckVendorIdRequested(initiator);
286 RequestCecVersion(initiator);
287 }
288
289 CLockObject lock(m_mutex);
290 return m_cecVersion;
291 }
292
293 void CCECBusDevice::SetCecVersion(const cec_version newVersion)
294 {
295 CLockObject lock(m_mutex);
296 if (m_cecVersion != newVersion)
297 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
298 m_cecVersion = newVersion;
299 }
300
301 bool CCECBusDevice::RequestCecVersion(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
302 {
303 bool bReturn(false);
304
305 if (!IsHandledByLibCEC() &&
306 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION))
307 {
308 MarkBusy();
309 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
310 bReturn = m_handler->TransmitRequestCecVersion(initiator, m_iLogicalAddress, bWaitForResponse);
311 MarkReady();
312 }
313 return bReturn;
314 }
315
316 bool CCECBusDevice::TransmitCECVersion(const cec_logical_address destination, bool bIsReply)
317 {
318 cec_version version;
319 {
320 CLockObject lock(m_mutex);
321 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_cecVersion));
322 version = m_cecVersion;
323 }
324
325 MarkBusy();
326 bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, destination, version, bIsReply);
327 MarkReady();
328 return bReturn;
329 }
330
331 cec_menu_language &CCECBusDevice::GetMenuLanguage(const cec_logical_address initiator, bool bUpdate /* = false */)
332 {
333 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
334 bool bRequestUpdate(false);
335 {
336 CLockObject lock(m_mutex);
337 bRequestUpdate = (bIsPresent &&
338 (bUpdate || !strcmp(m_menuLanguage.language, "???")));
339 }
340
341 if (bRequestUpdate)
342 {
343 CheckVendorIdRequested(initiator);
344 RequestMenuLanguage(initiator);
345 }
346
347 CLockObject lock(m_mutex);
348 return m_menuLanguage;
349 }
350
351 void CCECBusDevice::SetMenuLanguage(const char *strLanguage)
352 {
353 if (!strLanguage)
354 return;
355
356 CLockObject lock(m_mutex);
357 if (strcmp(strLanguage, m_menuLanguage.language))
358 {
359 memcpy(m_menuLanguage.language, strLanguage, 3);
360 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, m_menuLanguage.language);
361 }
362 }
363
364 void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
365 {
366 if (language.device == m_iLogicalAddress)
367 SetMenuLanguage(language.language);
368 }
369
370 bool CCECBusDevice::RequestMenuLanguage(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
371 {
372 bool bReturn(false);
373
374 if (!IsHandledByLibCEC() &&
375 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE))
376 {
377 MarkBusy();
378 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
379 bReturn = m_handler->TransmitRequestMenuLanguage(initiator, m_iLogicalAddress, bWaitForResponse);
380 MarkReady();
381 }
382 return bReturn;
383 }
384
385 bool CCECBusDevice::TransmitSetMenuLanguage(const cec_logical_address destination, bool bIsReply)
386 {
387 bool bReturn(false);
388 cec_menu_language language;
389 {
390 CLockObject lock(m_mutex);
391 language = m_menuLanguage;
392 }
393
394 char lang[3];
395 {
396 CLockObject lock(m_mutex);
397 lang[0] = language.language[0];
398 lang[1] = language.language[1];
399 lang[2] = language.language[2];
400 }
401
402 MarkBusy();
403 if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?')
404 {
405 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination);
406 m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
407 bReturn = true;
408 }
409 else
410 {
411 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> broadcast (F): menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang);
412 bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang, bIsReply);
413 }
414 MarkReady();
415 return bReturn;
416 }
417
418 bool CCECBusDevice::TransmitOSDString(const cec_logical_address destination, cec_display_control duration, const char *strMessage, bool bIsReply)
419 {
420 bool bReturn(false);
421 if (!m_processor->GetDevice(destination)->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
422 {
423 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, strMessage);
424 MarkBusy();
425 bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, destination, duration, strMessage, bIsReply);
426 MarkReady();
427 }
428 return bReturn;
429 }
430
431 CStdString CCECBusDevice::GetCurrentOSDName(void)
432 {
433 CLockObject lock(m_mutex);
434 return m_strDeviceName;
435 }
436
437 CStdString CCECBusDevice::GetOSDName(const cec_logical_address initiator, bool bUpdate /* = false */)
438 {
439 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
440 bool bRequestUpdate(false);
441 {
442 CLockObject lock(m_mutex);
443 bRequestUpdate = bIsPresent &&
444 (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
445 m_type != CEC_DEVICE_TYPE_TV;
446 }
447
448 if (bRequestUpdate)
449 {
450 CheckVendorIdRequested(initiator);
451 RequestOSDName(initiator);
452 }
453
454 CLockObject lock(m_mutex);
455 return m_strDeviceName;
456 }
457
458 void CCECBusDevice::SetOSDName(CStdString strName)
459 {
460 CLockObject lock(m_mutex);
461 if (m_strDeviceName != strName)
462 {
463 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str());
464 m_strDeviceName = strName;
465 }
466 }
467
468 bool CCECBusDevice::RequestOSDName(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
469 {
470 bool bReturn(false);
471
472 if (!IsHandledByLibCEC() &&
473 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME))
474 {
475 MarkBusy();
476 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
477 bReturn = m_handler->TransmitRequestOSDName(initiator, m_iLogicalAddress, bWaitForResponse);
478 MarkReady();
479 }
480 return bReturn;
481 }
482
483 bool CCECBusDevice::TransmitOSDName(const cec_logical_address destination, bool bIsReply)
484 {
485 CStdString strDeviceName;
486 {
487 CLockObject lock(m_mutex);
488 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, m_strDeviceName.c_str());
489 strDeviceName = m_strDeviceName;
490 }
491
492 MarkBusy();
493 bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, destination, strDeviceName, bIsReply);
494 MarkReady();
495 return bReturn;
496 }
497
498 bool CCECBusDevice::HasValidPhysicalAddress(void)
499 {
500 CLockObject lock(m_mutex);
501 return CLibCEC::IsValidPhysicalAddress(m_iPhysicalAddress);
502 }
503
504 uint16_t CCECBusDevice::GetCurrentPhysicalAddress(void)
505 {
506 CLockObject lock(m_mutex);
507 return m_iPhysicalAddress;
508 }
509
510 uint16_t CCECBusDevice::GetPhysicalAddress(const cec_logical_address initiator, bool bSuppressUpdate /* = false */)
511 {
512 if (!bSuppressUpdate)
513 {
514 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
515 bool bRequestUpdate(false);
516 {
517 CLockObject lock(m_mutex);
518 bRequestUpdate = bIsPresent && m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS;
519 }
520
521 if (bRequestUpdate)
522 {
523 CheckVendorIdRequested(initiator);
524 if (!RequestPhysicalAddress(initiator))
525 LIB_CEC->AddLog(CEC_LOG_ERROR, "failed to request the physical address");
526 }
527 }
528
529 CLockObject lock(m_mutex);
530 return m_iPhysicalAddress;
531 }
532
533 bool CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
534 {
535 CLockObject lock(m_mutex);
536 if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
537 {
538 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
539 m_iPhysicalAddress = iNewAddress;
540 }
541 return true;
542 }
543
544 bool CCECBusDevice::RequestPhysicalAddress(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
545 {
546 bool bReturn(false);
547
548 if (!IsHandledByLibCEC())
549 {
550 MarkBusy();
551 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
552 bReturn = m_handler->TransmitRequestPhysicalAddress(initiator, m_iLogicalAddress, bWaitForResponse);
553 MarkReady();
554 }
555 return bReturn;
556 }
557
558 bool CCECBusDevice::TransmitPhysicalAddress(bool bIsReply)
559 {
560 uint16_t iPhysicalAddress;
561 cec_device_type type;
562 {
563 CLockObject lock(m_mutex);
564 if (m_iPhysicalAddress == CEC_INVALID_PHYSICAL_ADDRESS)
565 return false;
566
567 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
568 iPhysicalAddress = m_iPhysicalAddress;
569 type = m_type;
570 }
571
572 MarkBusy();
573 bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type, bIsReply);
574 MarkReady();
575 return bReturn;
576 }
577
578 cec_power_status CCECBusDevice::GetCurrentPowerStatus(void)
579 {
580 CLockObject lock(m_mutex);
581 return m_powerStatus;
582 }
583
584 cec_power_status CCECBusDevice::GetPowerStatus(const cec_logical_address initiator, bool bUpdate /* = false */)
585 {
586 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
587 bool bRequestUpdate(false);
588 {
589 CLockObject lock(m_mutex);
590 bRequestUpdate = (bIsPresent &&
591 (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
592 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON ||
593 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY ||
594 GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME));
595 }
596
597 if (bRequestUpdate)
598 {
599 CheckVendorIdRequested(initiator);
600 RequestPowerStatus(initiator);
601 }
602
603 CLockObject lock(m_mutex);
604 return m_powerStatus;
605 }
606
607 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
608 {
609 CLockObject lock(m_mutex);
610 if (m_powerStatus != powerStatus)
611 {
612 m_iLastPowerStateUpdate = GetTimeMs();
613 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
614 m_powerStatus = powerStatus;
615 }
616 }
617
618 void CCECBusDevice::OnImageViewOnSent(bool bSentByLibCEC)
619 {
620 CLockObject lock(m_mutex);
621 m_bImageViewOnSent = bSentByLibCEC;
622
623 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
624 {
625 m_iLastPowerStateUpdate = GetTimeMs();
626 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON));
627 m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON;
628 }
629 }
630
631 bool CCECBusDevice::ImageViewOnSent(void)
632 {
633 CLockObject lock(m_mutex);
634 return m_bImageViewOnSent;
635 }
636
637 bool CCECBusDevice::RequestPowerStatus(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
638 {
639 bool bReturn(false);
640
641 if (!IsHandledByLibCEC() &&
642 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
643 {
644 MarkBusy();
645 bReturn = m_handler->TransmitRequestPowerStatus(initiator, m_iLogicalAddress, bWaitForResponse);
646 if (!bReturn)
647 SetPowerStatus(CEC_POWER_STATUS_UNKNOWN);
648 MarkReady();
649 }
650 return bReturn;
651 }
652
653 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination, bool bIsReply)
654 {
655 cec_power_status state;
656 {
657 CLockObject lock(m_mutex);
658 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_powerStatus));
659 state = m_powerStatus;
660 }
661
662 MarkBusy();
663 bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, destination, state, bIsReply);
664 MarkReady();
665 return bReturn;
666 }
667
668 cec_vendor_id CCECBusDevice::GetCurrentVendorId(void)
669 {
670 CLockObject lock(m_mutex);
671 return m_vendor;
672 }
673
674 cec_vendor_id CCECBusDevice::GetVendorId(const cec_logical_address initiator, bool bUpdate /* = false */)
675 {
676 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
677 bool bRequestUpdate(false);
678 {
679 CLockObject lock(m_mutex);
680 bRequestUpdate = (bIsPresent &&
681 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
682 }
683
684 if (bRequestUpdate)
685 RequestVendorId(initiator);
686
687 CLockObject lock(m_mutex);
688 return m_vendor;
689 }
690
691 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator, bool bUpdate /* = false */)
692 {
693 return ToString(GetVendorId(initiator, bUpdate));
694 }
695
696 bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
697 {
698 bool bVendorChanged(false);
699
700 {
701 CLockObject lock(m_mutex);
702 bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
703 m_vendor = (cec_vendor_id)iVendorId;
704 }
705
706 if (bVendorChanged)
707 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
708
709 return bVendorChanged;
710 }
711
712 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
713 {
714 bool bReturn(false);
715
716 if (!IsHandledByLibCEC() && initiator != CECDEVICE_UNKNOWN)
717 {
718 MarkBusy();
719 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
720 bReturn = m_handler->TransmitRequestVendorId(initiator, m_iLogicalAddress, bWaitForResponse);
721 MarkReady();
722
723 if (bWaitForResponse)
724 ReplaceHandler(true);
725 }
726 return bReturn;
727 }
728
729 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination, bool bSendAbort, bool bIsReply)
730 {
731 bool bReturn(false);
732 uint64_t iVendorId;
733 {
734 CLockObject lock(m_mutex);
735 iVendorId = (uint64_t)m_vendor;
736 }
737
738 MarkBusy();
739 if (iVendorId == CEC_VENDOR_UNKNOWN)
740 {
741 if (bSendAbort)
742 {
743 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination);
744 m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
745 bReturn = true;
746 }
747 }
748 else
749 {
750 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString((cec_vendor_id)iVendorId), iVendorId);
751 bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, destination, iVendorId, bIsReply);
752 }
753 MarkReady();
754 return bReturn;
755 }
756
757 cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */)
758 {
759 if (m_iLogicalAddress == CECDEVICE_BROADCAST)
760 return CEC_DEVICE_STATUS_NOT_PRESENT;
761
762 cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
763 bool bNeedsPoll(false);
764
765 {
766 CLockObject lock(m_mutex);
767 status = m_deviceStatus;
768 bNeedsPoll = !bSuppressPoll &&
769 m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC &&
770 // poll forced
771 (bForcePoll ||
772 // don't know the status
773 m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN ||
774 // always poll the TV if it's marked as not present
775 (m_deviceStatus == CEC_DEVICE_STATUS_NOT_PRESENT && m_iLogicalAddress == CECDEVICE_TV));
776 }
777
778 if (bNeedsPoll)
779 {
780 bool bPollAcked(false);
781 if (bNeedsPoll && NeedsPoll())
782 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
783
784 status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
785 SetDeviceStatus(status);
786 }
787
788 return status;
789 }
790
791 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus, cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
792 {
793 if (m_iLogicalAddress == CECDEVICE_UNREGISTERED)
794 return;
795
796 {
797 CLockObject lock(m_mutex);
798 switch (newStatus)
799 {
800 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
801 if (m_deviceStatus != newStatus)
802 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress);
803 SetPowerStatus (CEC_POWER_STATUS_ON);
804 SetVendorId (CEC_VENDOR_UNKNOWN);
805 SetMenuState (CEC_MENU_STATE_ACTIVATED);
806 SetCecVersion (libCECSpecVersion);
807 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
808 MarkAsInactiveSource();
809 m_iLastActive = 0;
810 m_deviceStatus = newStatus;
811 break;
812 case CEC_DEVICE_STATUS_PRESENT:
813 if (m_deviceStatus != newStatus)
814 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress);
815 m_deviceStatus = newStatus;
816 m_iLastActive = GetTimeMs();
817 break;
818 case CEC_DEVICE_STATUS_NOT_PRESENT:
819 if (m_deviceStatus != newStatus)
820 {
821 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress);
822 ResetDeviceStatus(true);
823 m_deviceStatus = newStatus;
824 }
825 break;
826 default:
827 ResetDeviceStatus();
828 break;
829 }
830 }
831 }
832
833 void CCECBusDevice::ResetDeviceStatus(bool bClientUnregistered /* = false */)
834 {
835 CLockObject lock(m_mutex);
836 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN);
837 SetVendorId (CEC_VENDOR_UNKNOWN);
838 SetMenuState (CEC_MENU_STATE_ACTIVATED);
839 SetCecVersion (CEC_VERSION_UNKNOWN);
840 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
841 SetOSDName (ToString(m_iLogicalAddress));
842 MarkAsInactiveSource(bClientUnregistered);
843
844 m_iLastActive = 0;
845 m_bVendorIdRequested = false;
846 m_unsupportedFeatures.clear();
847 m_waitForResponse->Clear();
848
849 if (m_deviceStatus != CEC_DEVICE_STATUS_UNKNOWN)
850 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress);
851 m_deviceStatus = CEC_DEVICE_STATUS_UNKNOWN;
852 }
853
854 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest, bool bUpdateDeviceStatus)
855 {
856 bool bReturn(false);
857 cec_logical_address destination(dest);
858 if (destination == CECDEVICE_UNKNOWN)
859 destination = m_iLogicalAddress;
860
861 CCECBusDevice *destDevice = m_processor->GetDevice(destination);
862 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
863 return bReturn;
864
865 MarkBusy();
866 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
867 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, destination, false);
868 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
869
870 if (bUpdateDeviceStatus)
871 destDevice->SetDeviceStatus(bReturn ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT);
872
873 MarkReady();
874 return bReturn;
875 }
876
877 void CCECBusDevice::HandlePoll(const cec_logical_address destination)
878 {
879 if (destination >= 0 && destination < CECDEVICE_BROADCAST)
880 {
881 CCECBusDevice *device = m_processor->GetDevice(destination);
882 if (device)
883 device->HandlePollFrom(m_iLogicalAddress);
884 }
885 }
886
887 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator)
888 {
889 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress);
890 m_bAwaitingReceiveFailed = true;
891 }
892
893 bool CCECBusDevice::HandleReceiveFailed(void)
894 {
895 bool bReturn = m_bAwaitingReceiveFailed;
896 m_bAwaitingReceiveFailed = false;
897 return bReturn;
898 }
899
900 cec_menu_state CCECBusDevice::GetMenuState(const cec_logical_address UNUSED(initiator))
901 {
902 CLockObject lock(m_mutex);
903 return m_menuState;
904 }
905
906 void CCECBusDevice::SetMenuState(const cec_menu_state state)
907 {
908 CLockObject lock(m_mutex);
909 if (m_menuState != state)
910 {
911 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
912 m_menuState = state;
913 }
914 }
915
916 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest, bool bIsReply)
917 {
918 cec_menu_state menuState;
919 {
920 CLockObject lock(m_mutex);
921 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
922 menuState = m_menuState;
923 }
924
925 MarkBusy();
926 bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState, bIsReply);
927 MarkReady();
928 return bReturn;
929 }
930
931 bool CCECBusDevice::ActivateSource(uint64_t iDelay /* = 0 */)
932 {
933 MarkAsActiveSource();
934 MarkBusy();
935 bool bReturn(true);
936 if (iDelay == 0)
937 {
938 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending active source message for '%s'", ToString(m_iLogicalAddress));
939 bReturn = m_handler->ActivateSource();
940 }
941 else
942 {
943 LIB_CEC->AddLog(CEC_LOG_DEBUG, "scheduling active source message for '%s'", ToString(m_iLogicalAddress));
944 m_handler->ScheduleActivateSource(iDelay);
945 }
946 MarkReady();
947 return bReturn;
948 }
949
950 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse /* = true */)
951 {
952 bool bReturn(false);
953
954 if (IsHandledByLibCEC())
955 {
956 MarkBusy();
957 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting active source");
958
959 bReturn = m_handler->TransmitRequestActiveSource(m_iLogicalAddress, bWaitForResponse);
960 MarkReady();
961 }
962 return bReturn;
963 }
964
965 void CCECBusDevice::MarkAsActiveSource(void)
966 {
967 bool bWasActivated(false);
968
969 // set the power status to powered on
970 SetPowerStatus(CEC_POWER_STATUS_ON);
971
972 // mark this device as active source
973 {
974 CLockObject lock(m_mutex);
975 if (!m_bActiveSource)
976 {
977 LIB_CEC->AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
978 bWasActivated = true;
979 }
980 else
981 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress);
982
983 m_bActiveSource = true;
984 }
985
986 CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
987 if (tv)
988 tv->OnImageViewOnSent(false);
989
990 // mark other devices as inactive sources
991 CECDEVICEVEC devices;
992 m_processor->GetDevices()->Get(devices);
993 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
994 if ((*it)->GetLogicalAddress() != m_iLogicalAddress)
995 (*it)->MarkAsInactiveSource();
996
997 if (bWasActivated)
998 {
999 if (IsHandledByLibCEC())
1000 m_processor->SetActiveSource(true, false);
1001 CCECClient *client = GetClient();
1002 if (client)
1003 client->SourceActivated(m_iLogicalAddress);
1004 }
1005 }
1006
1007 void CCECBusDevice::MarkAsInactiveSource(bool bClientUnregistered /* = false */)
1008 {
1009 bool bWasDeactivated(false);
1010 {
1011 CLockObject lock(m_mutex);
1012 if (m_bActiveSource)
1013 {
1014 LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress);
1015 bWasDeactivated = true;
1016 }
1017 m_bActiveSource = false;
1018 }
1019
1020 if (bWasDeactivated)
1021 {
1022 if (IsHandledByLibCEC())
1023 m_processor->SetActiveSource(false, bClientUnregistered);
1024 CCECClient *client = GetClient();
1025 if (client)
1026 client->SourceDeactivated(m_iLogicalAddress);
1027 }
1028 }
1029
1030 bool CCECBusDevice::TransmitActiveSource(bool bIsReply)
1031 {
1032 bool bSendActiveSource(false);
1033 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS);
1034
1035 {
1036 CLockObject lock(m_mutex);
1037 if (!HasValidPhysicalAddress())
1038 {
1039 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X) has an invalid physical address (%04x), not sending active source commands", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
1040 return false;
1041 }
1042
1043 iPhysicalAddress = m_iPhysicalAddress;
1044
1045 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
1046 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
1047 else if (m_bActiveSource)
1048 {
1049 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
1050 bSendActiveSource = true;
1051 }
1052 else
1053 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
1054 }
1055
1056 bool bActiveSourceSent(false);
1057 if (bSendActiveSource)
1058 {
1059 MarkBusy();
1060 bActiveSourceSent = m_handler->TransmitActiveSource(m_iLogicalAddress, iPhysicalAddress, bIsReply);
1061 MarkReady();
1062 }
1063
1064 return bActiveSourceSent;
1065 }
1066
1067 bool CCECBusDevice::TransmitImageViewOn(void)
1068 {
1069 {
1070 CLockObject lock(m_mutex);
1071 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
1072 {
1073 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
1074 return false;
1075 }
1076 }
1077
1078 CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
1079 if (!tv)
1080 {
1081 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - couldn't get TV instance", __FUNCTION__);
1082 return false;
1083 }
1084
1085 if (tv->ImageViewOnSent())
1086 {
1087 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - 'image view on' already sent", __FUNCTION__);
1088 return true;
1089 }
1090
1091 bool bImageViewOnSent(false);
1092 MarkBusy();
1093 bImageViewOnSent = m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
1094 MarkReady();
1095
1096 if (bImageViewOnSent)
1097 tv->OnImageViewOnSent(true);
1098
1099 return bImageViewOnSent;
1100 }
1101
1102 bool CCECBusDevice::TransmitInactiveSource(void)
1103 {
1104 uint16_t iPhysicalAddress;
1105 {
1106 CLockObject lock(m_mutex);
1107 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
1108 iPhysicalAddress = m_iPhysicalAddress;
1109 }
1110
1111 MarkBusy();
1112 bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
1113 MarkReady();
1114 return bReturn;
1115 }
1116
1117 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1118 {
1119 MarkBusy();
1120 bool bReturn = m_handler->ActivateSource(true);
1121 MarkReady();
1122 return bReturn;
1123 }
1124
1125 void CCECBusDevice::SetActiveRoute(uint16_t iRoute)
1126 {
1127 SetPowerStatus(CEC_POWER_STATUS_ON);
1128
1129 CCECDeviceMap* map = m_processor->GetDevices();
1130 if (!map)
1131 return;
1132
1133 CCECBusDevice* newRoute = m_processor->GetDeviceByPhysicalAddress(iRoute, true);
1134 if (newRoute && newRoute->IsHandledByLibCEC())
1135 {
1136 newRoute->ActivateSource();
1137 return;
1138 }
1139
1140 CECDEVICEVEC devices;
1141 m_processor->GetDevices()->GetChildrenOf(devices, this);
1142
1143 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
1144 {
1145 if ((*it)->GetCurrentPhysicalAddress() == iRoute && (*it)->IsHandledByLibCEC())
1146 (*it)->ActivateSource();
1147 else if (!CCECTypeUtils::PhysicalAddressIsIncluded(iRoute, (*it)->GetCurrentPhysicalAddress()))
1148 (*it)->MarkAsInactiveSource();
1149 }
1150 }
1151
1152 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */)
1153 {
1154 if (iNewAddress != CEC_INVALID_PHYSICAL_ADDRESS)
1155 SetPowerStatus(CEC_POWER_STATUS_ON);
1156
1157 CLockObject lock(m_mutex);
1158 if (iNewAddress != m_iStreamPath)
1159 {
1160 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
1161 m_iStreamPath = iNewAddress;
1162 }
1163
1164 if (!LIB_CEC->IsValidPhysicalAddress(iNewAddress))
1165 return;
1166
1167 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
1168 if (device)
1169 {
1170 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1171 device->MarkAsActiveSource();
1172
1173 // respond with an active source message if this device is handled by libCEC
1174 if (device->IsHandledByLibCEC())
1175 device->TransmitActiveSource(true);
1176 }
1177 else
1178 {
1179 // try to find the device with the old address, and mark it as inactive when found
1180 device = m_processor->GetDeviceByPhysicalAddress(iOldAddress);
1181 if (device)
1182 device->MarkAsInactiveSource();
1183 }
1184 }
1185
1186 bool CCECBusDevice::PowerOn(const cec_logical_address initiator)
1187 {
1188 bool bReturn(false);
1189 GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor
1190
1191 MarkBusy();
1192 cec_power_status currentStatus;
1193 if (m_iLogicalAddress == CECDEVICE_TV ||
1194 ((currentStatus = GetPowerStatus(initiator, false)) != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON &&
1195 currentStatus != CEC_POWER_STATUS_ON))
1196 {
1197 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
1198 bReturn = m_handler->PowerOn(initiator, m_iLogicalAddress);
1199 }
1200 else
1201 {
1202 LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus));
1203 }
1204
1205 MarkReady();
1206 return bReturn;
1207 }
1208
1209 bool CCECBusDevice::Standby(const cec_logical_address initiator)
1210 {
1211 GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor
1212
1213 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
1214 MarkBusy();
1215 bool bReturn = m_handler->TransmitStandby(initiator, m_iLogicalAddress);
1216 MarkReady();
1217 return bReturn;
1218 }
1219
1220 bool CCECBusDevice::NeedsPoll(void)
1221 {
1222 bool bSendPoll(false);
1223 cec_logical_address pollAddress(CECDEVICE_UNKNOWN);
1224 switch (m_iLogicalAddress)
1225 {
1226 case CECDEVICE_PLAYBACKDEVICE3:
1227 pollAddress = CECDEVICE_PLAYBACKDEVICE2;
1228 break;
1229 case CECDEVICE_PLAYBACKDEVICE2:
1230 pollAddress = CECDEVICE_PLAYBACKDEVICE1;
1231 break;
1232 case CECDEVICE_RECORDINGDEVICE3:
1233 pollAddress = CECDEVICE_RECORDINGDEVICE2;
1234 break;
1235 case CECDEVICE_RECORDINGDEVICE2:
1236 pollAddress = CECDEVICE_RECORDINGDEVICE1;
1237 break;
1238 case CECDEVICE_TUNER4:
1239 pollAddress = CECDEVICE_TUNER3;
1240 break;
1241 case CECDEVICE_TUNER3:
1242 pollAddress = CECDEVICE_TUNER2;
1243 break;
1244 case CECDEVICE_TUNER2:
1245 pollAddress = CECDEVICE_TUNER1;
1246 break;
1247 case CECDEVICE_AUDIOSYSTEM:
1248 case CECDEVICE_PLAYBACKDEVICE1:
1249 case CECDEVICE_RECORDINGDEVICE1:
1250 case CECDEVICE_TUNER1:
1251 case CECDEVICE_TV:
1252 bSendPoll = true;
1253 break;
1254 default:
1255 break;
1256 }
1257
1258 if (!bSendPoll && pollAddress != CECDEVICE_UNKNOWN)
1259 {
1260 CCECBusDevice *device = m_processor->GetDevice(pollAddress);
1261 if (device)
1262 {
1263 cec_bus_device_status status = device->GetStatus();
1264 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
1265 }
1266 else
1267 {
1268 bSendPoll = true;
1269 }
1270 }
1271
1272 return bSendPoll;
1273 }
1274
1275 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator)
1276 {
1277 bool bRequestVendorId(false);
1278 {
1279 CLockObject lock(m_mutex);
1280 bRequestVendorId = !m_bVendorIdRequested;
1281 m_bVendorIdRequested = true;
1282 }
1283
1284 if (bRequestVendorId)
1285 {
1286 ReplaceHandler(false);
1287 GetVendorId(initiator);
1288 }
1289 }
1290 //@}
1291
1292 CCECAudioSystem *CCECBusDevice::AsAudioSystem(void)
1293 {
1294 return AsAudioSystem(this);
1295 }
1296
1297 CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(void)
1298 {
1299 return AsPlaybackDevice(this);
1300 }
1301
1302 CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(void)
1303 {
1304 return AsRecordingDevice(this);
1305 }
1306
1307 CCECTuner *CCECBusDevice::AsTuner(void)
1308 {
1309 return AsTuner(this);
1310 }
1311
1312 CCECTV *CCECBusDevice::AsTV(void)
1313 {
1314 return AsTV(this);
1315 }
1316
1317 CCECAudioSystem *CCECBusDevice::AsAudioSystem(CCECBusDevice *device)
1318 {
1319 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
1320 return static_cast<CCECAudioSystem *>(device);
1321 return NULL;
1322 }
1323
1324 CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(CCECBusDevice *device)
1325 {
1326 if (device &&
1327 (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
1328 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
1329 return static_cast<CCECPlaybackDevice *>(device);
1330 return NULL;
1331 }
1332
1333 CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(CCECBusDevice *device)
1334 {
1335 if (device && device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
1336 return static_cast<CCECRecordingDevice *>(device);
1337 return NULL;
1338 }
1339
1340 CCECTuner *CCECBusDevice::AsTuner(CCECBusDevice *device)
1341 {
1342 if (device && device->GetType() == CEC_DEVICE_TYPE_TUNER)
1343 return static_cast<CCECTuner *>(device);
1344 return NULL;
1345 }
1346
1347 CCECTV *CCECBusDevice::AsTV(CCECBusDevice *device)
1348 {
1349 if (device && device->GetType() == CEC_DEVICE_TYPE_TV)
1350 return static_cast<CCECTV *>(device);
1351 return NULL;
1352 }
1353
1354 void CCECBusDevice::MarkBusy(void)
1355 {
1356 CLockObject handlerLock(m_handlerMutex);
1357 ++m_iHandlerUseCount;
1358 }
1359
1360 void CCECBusDevice::MarkReady(void)
1361 {
1362 CLockObject handlerLock(m_handlerMutex);
1363 if (m_iHandlerUseCount > 0)
1364 --m_iHandlerUseCount;
1365 }
1366
1367 bool CCECBusDevice::TryLogicalAddress(cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
1368 {
1369 LIB_CEC->AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
1370
1371 if (!TransmitPoll(m_iLogicalAddress, false))
1372 {
1373 LIB_CEC->AddLog(CEC_LOG_DEBUG, "using logical address '%s'", GetLogicalAddressName());
1374 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC, libCECSpecVersion);
1375
1376 return true;
1377 }
1378
1379 LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
1380 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
1381 return false;
1382 }
1383
1384 CCECClient *CCECBusDevice::GetClient(void)
1385 {
1386 return m_processor->GetClient(m_iLogicalAddress);
1387 }
1388
1389 void CCECBusDevice::SignalOpcode(cec_opcode opcode)
1390 {
1391 m_waitForResponse->Received(opcode);
1392 }
1393
1394 bool CCECBusDevice::WaitForOpcode(cec_opcode opcode)
1395 {
1396 return m_waitForResponse->Wait(opcode);
1397 }