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