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