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