no need to cache the physical address of the active source in the device map; the...
[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)
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 MarkReady();
647 }
648 return bReturn;
649 }
650
651 bool CCECBusDevice::TransmitPowerState(const cec_logical_address destination, bool bIsReply)
652 {
653 cec_power_status state;
654 {
655 CLockObject lock(m_mutex);
656 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination, ToString(m_powerStatus));
657 state = m_powerStatus;
658 }
659
660 MarkBusy();
661 bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, destination, state, bIsReply);
662 MarkReady();
663 return bReturn;
664 }
665
666 cec_vendor_id CCECBusDevice::GetCurrentVendorId(void)
667 {
668 CLockObject lock(m_mutex);
669 return m_vendor;
670 }
671
672 cec_vendor_id CCECBusDevice::GetVendorId(const cec_logical_address initiator, bool bUpdate /* = false */)
673 {
674 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
675 bool bRequestUpdate(false);
676 {
677 CLockObject lock(m_mutex);
678 bRequestUpdate = (bIsPresent &&
679 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
680 }
681
682 if (bRequestUpdate)
683 RequestVendorId(initiator);
684
685 CLockObject lock(m_mutex);
686 return m_vendor;
687 }
688
689 const char *CCECBusDevice::GetVendorName(const cec_logical_address initiator, bool bUpdate /* = false */)
690 {
691 return ToString(GetVendorId(initiator, bUpdate));
692 }
693
694 bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
695 {
696 bool bVendorChanged(false);
697
698 {
699 CLockObject lock(m_mutex);
700 bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
701 m_vendor = (cec_vendor_id)iVendorId;
702 }
703
704 if (bVendorChanged)
705 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
706
707 return bVendorChanged;
708 }
709
710 bool CCECBusDevice::RequestVendorId(const cec_logical_address initiator, bool bWaitForResponse /* = true */)
711 {
712 bool bReturn(false);
713
714 if (!IsHandledByLibCEC() && initiator != CECDEVICE_UNKNOWN)
715 {
716 MarkBusy();
717 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
718 bReturn = m_handler->TransmitRequestVendorId(initiator, m_iLogicalAddress, bWaitForResponse);
719 MarkReady();
720
721 if (bWaitForResponse)
722 ReplaceHandler(true);
723 }
724 return bReturn;
725 }
726
727 bool CCECBusDevice::TransmitVendorID(const cec_logical_address destination, bool bSendAbort, bool bIsReply)
728 {
729 bool bReturn(false);
730 uint64_t iVendorId;
731 {
732 CLockObject lock(m_mutex);
733 iVendorId = (uint64_t)m_vendor;
734 }
735
736 MarkBusy();
737 if (iVendorId == CEC_VENDOR_UNKNOWN)
738 {
739 if (bSendAbort)
740 {
741 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(destination), destination);
742 m_processor->TransmitAbort(m_iLogicalAddress, destination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
743 bReturn = true;
744 }
745 }
746 else
747 {
748 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);
749 bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId, bIsReply);
750 }
751 MarkReady();
752 return bReturn;
753 }
754
755 cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */)
756 {
757 if (m_iLogicalAddress == CECDEVICE_BROADCAST)
758 return CEC_DEVICE_STATUS_NOT_PRESENT;
759
760 cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
761 bool bNeedsPoll(false);
762
763 {
764 CLockObject lock(m_mutex);
765 status = m_deviceStatus;
766 bNeedsPoll = !bSuppressPoll &&
767 m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC &&
768 // poll forced
769 (bForcePoll ||
770 // don't know the status
771 m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN ||
772 // always poll the TV if it's marked as not present
773 (m_deviceStatus == CEC_DEVICE_STATUS_NOT_PRESENT && m_iLogicalAddress == CECDEVICE_TV));
774 }
775
776 if (bNeedsPoll)
777 {
778 bool bPollAcked(false);
779 if (bNeedsPoll && NeedsPoll())
780 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
781
782 status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
783 SetDeviceStatus(status);
784 }
785
786 return status;
787 }
788
789 void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus, cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
790 {
791 if (m_iLogicalAddress == CECDEVICE_UNREGISTERED)
792 return;
793
794 {
795 CLockObject lock(m_mutex);
796 switch (newStatus)
797 {
798 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
799 if (m_deviceStatus != newStatus)
800 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'handled by libCEC'", GetLogicalAddressName(), m_iLogicalAddress);
801 SetPowerStatus (CEC_POWER_STATUS_ON);
802 SetVendorId (CEC_VENDOR_UNKNOWN);
803 SetMenuState (CEC_MENU_STATE_ACTIVATED);
804 SetCecVersion (libCECSpecVersion);
805 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
806 MarkAsInactiveSource();
807 m_iLastActive = 0;
808 m_deviceStatus = newStatus;
809 break;
810 case CEC_DEVICE_STATUS_PRESENT:
811 if (m_deviceStatus != newStatus)
812 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'present'", GetLogicalAddressName(), m_iLogicalAddress);
813 m_deviceStatus = newStatus;
814 m_iLastActive = GetTimeMs();
815 break;
816 case CEC_DEVICE_STATUS_NOT_PRESENT:
817 if (m_deviceStatus != newStatus)
818 {
819 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'not present'", GetLogicalAddressName(), m_iLogicalAddress);
820 ResetDeviceStatus(true);
821 m_deviceStatus = newStatus;
822 }
823 break;
824 default:
825 ResetDeviceStatus();
826 break;
827 }
828 }
829 }
830
831 void CCECBusDevice::ResetDeviceStatus(bool bClientUnregistered /* = false */)
832 {
833 CLockObject lock(m_mutex);
834 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN);
835 SetVendorId (CEC_VENDOR_UNKNOWN);
836 SetMenuState (CEC_MENU_STATE_ACTIVATED);
837 SetCecVersion (CEC_VERSION_UNKNOWN);
838 SetStreamPath (CEC_INVALID_PHYSICAL_ADDRESS);
839 SetOSDName (ToString(m_iLogicalAddress));
840 MarkAsInactiveSource(bClientUnregistered);
841
842 m_iLastActive = 0;
843 m_bVendorIdRequested = false;
844 m_unsupportedFeatures.clear();
845 m_waitForResponse->Clear();
846
847 if (m_deviceStatus != CEC_DEVICE_STATUS_UNKNOWN)
848 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): device status changed into 'unknown'", GetLogicalAddressName(), m_iLogicalAddress);
849 m_deviceStatus = CEC_DEVICE_STATUS_UNKNOWN;
850 }
851
852 bool CCECBusDevice::TransmitPoll(const cec_logical_address dest, bool bUpdateDeviceStatus)
853 {
854 bool bReturn(false);
855 cec_logical_address destination(dest);
856 if (destination == CECDEVICE_UNKNOWN)
857 destination = m_iLogicalAddress;
858
859 CCECBusDevice *destDevice = m_processor->GetDevice(destination);
860 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
861 return bReturn;
862
863 MarkBusy();
864 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
865 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, destination, false);
866 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
867
868 if (bUpdateDeviceStatus)
869 destDevice->SetDeviceStatus(bReturn ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT);
870
871 MarkReady();
872 return bReturn;
873 }
874
875 void CCECBusDevice::HandlePoll(const cec_logical_address destination)
876 {
877 if (destination >= 0 && destination < CECDEVICE_BROADCAST)
878 {
879 CCECBusDevice *device = m_processor->GetDevice(destination);
880 if (device)
881 device->HandlePollFrom(m_iLogicalAddress);
882 }
883 }
884
885 void CCECBusDevice::HandlePollFrom(const cec_logical_address initiator)
886 {
887 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress);
888 m_bAwaitingReceiveFailed = true;
889 }
890
891 bool CCECBusDevice::HandleReceiveFailed(void)
892 {
893 bool bReturn = m_bAwaitingReceiveFailed;
894 m_bAwaitingReceiveFailed = false;
895 return bReturn;
896 }
897
898 cec_menu_state CCECBusDevice::GetMenuState(const cec_logical_address UNUSED(initiator))
899 {
900 CLockObject lock(m_mutex);
901 return m_menuState;
902 }
903
904 void CCECBusDevice::SetMenuState(const cec_menu_state state)
905 {
906 CLockObject lock(m_mutex);
907 if (m_menuState != state)
908 {
909 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
910 m_menuState = state;
911 }
912 }
913
914 bool CCECBusDevice::TransmitMenuState(const cec_logical_address dest, bool bIsReply)
915 {
916 cec_menu_state menuState;
917 {
918 CLockObject lock(m_mutex);
919 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
920 menuState = m_menuState;
921 }
922
923 MarkBusy();
924 bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState, bIsReply);
925 MarkReady();
926 return bReturn;
927 }
928
929 bool CCECBusDevice::ActivateSource(uint64_t iDelay /* = 0 */)
930 {
931 MarkAsActiveSource();
932 MarkBusy();
933 bool bReturn(true);
934 if (iDelay == 0)
935 {
936 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending active source message for '%s'", ToString(m_iLogicalAddress));
937 bReturn = m_handler->ActivateSource();
938 }
939 else
940 {
941 LIB_CEC->AddLog(CEC_LOG_DEBUG, "scheduling active source message for '%s'", ToString(m_iLogicalAddress));
942 m_handler->ScheduleActivateSource(iDelay);
943 }
944 MarkReady();
945 return bReturn;
946 }
947
948 bool CCECBusDevice::RequestActiveSource(bool bWaitForResponse /* = true */)
949 {
950 bool bReturn(false);
951
952 if (IsHandledByLibCEC())
953 {
954 MarkBusy();
955 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting active source");
956
957 bReturn = m_handler->TransmitRequestActiveSource(m_iLogicalAddress, bWaitForResponse);
958 MarkReady();
959 }
960 return bReturn;
961 }
962
963 void CCECBusDevice::MarkAsActiveSource(void)
964 {
965 bool bWasActivated(false);
966
967 // set the power status to powered on
968 SetPowerStatus(CEC_POWER_STATUS_ON);
969
970 // mark this device as active source
971 {
972 CLockObject lock(m_mutex);
973 if (!m_bActiveSource)
974 {
975 LIB_CEC->AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
976 bWasActivated = true;
977 }
978 else
979 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%x) was already marked as active source", GetLogicalAddressName(), m_iLogicalAddress);
980
981 m_bActiveSource = true;
982 }
983
984 CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
985 if (tv)
986 tv->OnImageViewOnSent(false);
987
988 // mark other devices as inactive sources
989 CECDEVICEVEC devices;
990 m_processor->GetDevices()->Get(devices);
991 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
992 if ((*it)->GetLogicalAddress() != m_iLogicalAddress)
993 (*it)->MarkAsInactiveSource();
994
995 if (bWasActivated)
996 {
997 if (IsHandledByLibCEC())
998 m_processor->SetActiveSource(true, false);
999 CCECClient *client = GetClient();
1000 if (client)
1001 client->SourceActivated(m_iLogicalAddress);
1002 }
1003 }
1004
1005 void CCECBusDevice::MarkAsInactiveSource(bool bClientUnregistered /* = false */)
1006 {
1007 bool bWasDeactivated(false);
1008 {
1009 CLockObject lock(m_mutex);
1010 if (m_bActiveSource)
1011 {
1012 LIB_CEC->AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress);
1013 bWasDeactivated = true;
1014 }
1015 m_bActiveSource = false;
1016 }
1017
1018 if (bWasDeactivated)
1019 {
1020 if (IsHandledByLibCEC())
1021 m_processor->SetActiveSource(false, bClientUnregistered);
1022 CCECClient *client = GetClient();
1023 if (client)
1024 client->SourceDeactivated(m_iLogicalAddress);
1025 }
1026 }
1027
1028 bool CCECBusDevice::TransmitActiveSource(bool bIsReply)
1029 {
1030 bool bSendActiveSource(false);
1031 uint16_t iPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS);
1032
1033 {
1034 CLockObject lock(m_mutex);
1035 if (!HasValidPhysicalAddress())
1036 {
1037 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X) has an invalid physical address (%04x), not sending active source commands", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
1038 return false;
1039 }
1040
1041 iPhysicalAddress = m_iPhysicalAddress;
1042
1043 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
1044 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
1045 else if (m_bActiveSource)
1046 {
1047 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
1048 bSendActiveSource = true;
1049 }
1050 else
1051 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
1052 }
1053
1054 bool bActiveSourceSent(false);
1055 if (bSendActiveSource)
1056 {
1057 MarkBusy();
1058 bActiveSourceSent = m_handler->TransmitActiveSource(m_iLogicalAddress, iPhysicalAddress, bIsReply);
1059 MarkReady();
1060 }
1061
1062 return bActiveSourceSent;
1063 }
1064
1065 bool CCECBusDevice::TransmitImageViewOn(void)
1066 {
1067 {
1068 CLockObject lock(m_mutex);
1069 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
1070 {
1071 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
1072 return false;
1073 }
1074 }
1075
1076 CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
1077 if (!tv)
1078 {
1079 LIB_CEC->AddLog(CEC_LOG_ERROR, "%s - couldn't get TV instance", __FUNCTION__);
1080 return false;
1081 }
1082
1083 if (tv->ImageViewOnSent())
1084 {
1085 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - 'image view on' already sent", __FUNCTION__);
1086 return true;
1087 }
1088
1089 bool bImageViewOnSent(false);
1090 MarkBusy();
1091 bImageViewOnSent = m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
1092 MarkReady();
1093
1094 if (bImageViewOnSent)
1095 tv->OnImageViewOnSent(true);
1096
1097 return bImageViewOnSent;
1098 }
1099
1100 bool CCECBusDevice::TransmitInactiveSource(void)
1101 {
1102 uint16_t iPhysicalAddress;
1103 {
1104 CLockObject lock(m_mutex);
1105 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
1106 iPhysicalAddress = m_iPhysicalAddress;
1107 }
1108
1109 MarkBusy();
1110 bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
1111 MarkReady();
1112 return bReturn;
1113 }
1114
1115 bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1116 {
1117 MarkBusy();
1118 bool bReturn = m_handler->ActivateSource(true);
1119 MarkReady();
1120 return bReturn;
1121 }
1122
1123 void CCECBusDevice::SetActiveRoute(uint16_t iRoute)
1124 {
1125 CCECDeviceMap* map = m_processor->GetDevices();
1126 if (!map)
1127 return;
1128
1129 CCECBusDevice* previouslyActive = map->GetActiveSource();
1130 if (!previouslyActive)
1131 return;
1132
1133 CECDEVICEVEC devices;
1134 m_processor->GetDevices()->GetChildrenOf(devices, this);
1135
1136 for (CECDEVICEVEC::iterator it = devices.begin(); it != devices.end(); it++)
1137 {
1138 if (!CCECTypeUtils::PhysicalAddressIsIncluded(iRoute, (*it)->GetCurrentPhysicalAddress()))
1139 (*it)->MarkAsInactiveSource();
1140 }
1141 }
1142
1143 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = CEC_INVALID_PHYSICAL_ADDRESS */)
1144 {
1145 CLockObject lock(m_mutex);
1146 if (iNewAddress != m_iStreamPath)
1147 {
1148 LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
1149 m_iStreamPath = iNewAddress;
1150 }
1151
1152 if (!LIB_CEC->IsValidPhysicalAddress(iNewAddress))
1153 return;
1154
1155 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
1156 if (device)
1157 {
1158 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
1159 device->MarkAsActiveSource();
1160
1161 // respond with an active source message if this device is handled by libCEC
1162 if (device->IsHandledByLibCEC())
1163 device->TransmitActiveSource(true);
1164 }
1165 else
1166 {
1167 // try to find the device with the old address, and mark it as inactive when found
1168 device = m_processor->GetDeviceByPhysicalAddress(iOldAddress);
1169 if (device)
1170 device->MarkAsInactiveSource();
1171 }
1172 }
1173
1174 bool CCECBusDevice::PowerOn(const cec_logical_address initiator)
1175 {
1176 bool bReturn(false);
1177 GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor
1178
1179 MarkBusy();
1180 cec_power_status currentStatus;
1181 if (m_iLogicalAddress == CECDEVICE_TV ||
1182 ((currentStatus = GetPowerStatus(initiator, false)) != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON &&
1183 currentStatus != CEC_POWER_STATUS_ON))
1184 {
1185 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
1186 bReturn = m_handler->PowerOn(initiator, m_iLogicalAddress);
1187 }
1188 else
1189 {
1190 LIB_CEC->AddLog(CEC_LOG_DEBUG, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus));
1191 }
1192
1193 MarkReady();
1194 return bReturn;
1195 }
1196
1197 bool CCECBusDevice::Standby(const cec_logical_address initiator)
1198 {
1199 GetVendorId(initiator); // ensure that we got the vendor id, because the implementations vary per vendor
1200
1201 LIB_CEC->AddLog(CEC_LOG_NOTICE, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
1202 MarkBusy();
1203 bool bReturn = m_handler->TransmitStandby(initiator, m_iLogicalAddress);
1204 MarkReady();
1205 return bReturn;
1206 }
1207
1208 bool CCECBusDevice::NeedsPoll(void)
1209 {
1210 bool bSendPoll(false);
1211 cec_logical_address pollAddress(CECDEVICE_UNKNOWN);
1212 switch (m_iLogicalAddress)
1213 {
1214 case CECDEVICE_PLAYBACKDEVICE3:
1215 pollAddress = CECDEVICE_PLAYBACKDEVICE2;
1216 break;
1217 case CECDEVICE_PLAYBACKDEVICE2:
1218 pollAddress = CECDEVICE_PLAYBACKDEVICE1;
1219 break;
1220 case CECDEVICE_RECORDINGDEVICE3:
1221 pollAddress = CECDEVICE_RECORDINGDEVICE2;
1222 break;
1223 case CECDEVICE_RECORDINGDEVICE2:
1224 pollAddress = CECDEVICE_RECORDINGDEVICE1;
1225 break;
1226 case CECDEVICE_TUNER4:
1227 pollAddress = CECDEVICE_TUNER3;
1228 break;
1229 case CECDEVICE_TUNER3:
1230 pollAddress = CECDEVICE_TUNER2;
1231 break;
1232 case CECDEVICE_TUNER2:
1233 pollAddress = CECDEVICE_TUNER1;
1234 break;
1235 case CECDEVICE_AUDIOSYSTEM:
1236 case CECDEVICE_PLAYBACKDEVICE1:
1237 case CECDEVICE_RECORDINGDEVICE1:
1238 case CECDEVICE_TUNER1:
1239 case CECDEVICE_TV:
1240 bSendPoll = true;
1241 break;
1242 default:
1243 break;
1244 }
1245
1246 if (!bSendPoll && pollAddress != CECDEVICE_UNKNOWN)
1247 {
1248 CCECBusDevice *device = m_processor->GetDevice(pollAddress);
1249 if (device)
1250 {
1251 cec_bus_device_status status = device->GetStatus();
1252 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
1253 }
1254 else
1255 {
1256 bSendPoll = true;
1257 }
1258 }
1259
1260 return bSendPoll;
1261 }
1262
1263 void CCECBusDevice::CheckVendorIdRequested(const cec_logical_address initiator)
1264 {
1265 bool bRequestVendorId(false);
1266 {
1267 CLockObject lock(m_mutex);
1268 bRequestVendorId = !m_bVendorIdRequested;
1269 m_bVendorIdRequested = true;
1270 }
1271
1272 if (bRequestVendorId)
1273 {
1274 ReplaceHandler(false);
1275 GetVendorId(initiator);
1276 }
1277 }
1278 //@}
1279
1280 CCECAudioSystem *CCECBusDevice::AsAudioSystem(void)
1281 {
1282 return AsAudioSystem(this);
1283 }
1284
1285 CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(void)
1286 {
1287 return AsPlaybackDevice(this);
1288 }
1289
1290 CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(void)
1291 {
1292 return AsRecordingDevice(this);
1293 }
1294
1295 CCECTuner *CCECBusDevice::AsTuner(void)
1296 {
1297 return AsTuner(this);
1298 }
1299
1300 CCECTV *CCECBusDevice::AsTV(void)
1301 {
1302 return AsTV(this);
1303 }
1304
1305 CCECAudioSystem *CCECBusDevice::AsAudioSystem(CCECBusDevice *device)
1306 {
1307 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
1308 return static_cast<CCECAudioSystem *>(device);
1309 return NULL;
1310 }
1311
1312 CCECPlaybackDevice *CCECBusDevice::AsPlaybackDevice(CCECBusDevice *device)
1313 {
1314 if (device &&
1315 (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
1316 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
1317 return static_cast<CCECPlaybackDevice *>(device);
1318 return NULL;
1319 }
1320
1321 CCECRecordingDevice *CCECBusDevice::AsRecordingDevice(CCECBusDevice *device)
1322 {
1323 if (device && device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
1324 return static_cast<CCECRecordingDevice *>(device);
1325 return NULL;
1326 }
1327
1328 CCECTuner *CCECBusDevice::AsTuner(CCECBusDevice *device)
1329 {
1330 if (device && device->GetType() == CEC_DEVICE_TYPE_TUNER)
1331 return static_cast<CCECTuner *>(device);
1332 return NULL;
1333 }
1334
1335 CCECTV *CCECBusDevice::AsTV(CCECBusDevice *device)
1336 {
1337 if (device && device->GetType() == CEC_DEVICE_TYPE_TV)
1338 return static_cast<CCECTV *>(device);
1339 return NULL;
1340 }
1341
1342 void CCECBusDevice::MarkBusy(void)
1343 {
1344 CLockObject handlerLock(m_handlerMutex);
1345 ++m_iHandlerUseCount;
1346 }
1347
1348 void CCECBusDevice::MarkReady(void)
1349 {
1350 CLockObject handlerLock(m_handlerMutex);
1351 if (m_iHandlerUseCount > 0)
1352 --m_iHandlerUseCount;
1353 }
1354
1355 bool CCECBusDevice::TryLogicalAddress(cec_version libCECSpecVersion /* = CEC_VERSION_1_4 */)
1356 {
1357 LIB_CEC->AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
1358
1359 if (!TransmitPoll(m_iLogicalAddress, false))
1360 {
1361 LIB_CEC->AddLog(CEC_LOG_DEBUG, "using logical address '%s'", GetLogicalAddressName());
1362 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC, libCECSpecVersion);
1363
1364 return true;
1365 }
1366
1367 LIB_CEC->AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
1368 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
1369 return false;
1370 }
1371
1372 CCECClient *CCECBusDevice::GetClient(void)
1373 {
1374 return m_processor->GetClient(m_iLogicalAddress);
1375 }
1376
1377 void CCECBusDevice::SignalOpcode(cec_opcode opcode)
1378 {
1379 m_waitForResponse->Received(opcode);
1380 }
1381
1382 bool CCECBusDevice::WaitForOpcode(cec_opcode opcode)
1383 {
1384 return m_waitForResponse->Wait(opcode);
1385 }