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