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