cec: cleaned up CCECBusDevice::GetStatus()
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33#include "CECBusDevice.h"
34#include "../CECProcessor.h"
35#include "../implementations/ANCommandHandler.h"
36#include "../implementations/CECCommandHandler.h"
37#include "../implementations/SLCommandHandler.h"
38#include "../implementations/VLCommandHandler.h"
39#include "../LibCEC.h"
40#include "../platform/util/timeutils.h"
41
42using namespace CEC;
43using namespace PLATFORM;
44
45#define ToString(p) m_processor->ToString(p)
46
47CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
48 m_type(CEC_DEVICE_TYPE_RESERVED),
49 m_iPhysicalAddress(iPhysicalAddress),
50 m_iStreamPath(0),
51 m_iLogicalAddress(iLogicalAddress),
52 m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
53 m_processor(processor),
54 m_vendor(CEC_VENDOR_UNKNOWN),
55 m_bReplaceHandler(false),
56 m_menuState(CEC_MENU_STATE_ACTIVATED),
57 m_bActiveSource(false),
58 m_iLastActive(0),
59 m_iLastPowerStateUpdate(0),
60 m_cecVersion(CEC_VERSION_UNKNOWN),
61 m_deviceStatus(CEC_DEVICE_STATUS_UNKNOWN),
62 m_iHandlerUseCount(0),
63 m_bAwaitingReceiveFailed(false)
64{
65 m_handler = new CCECCommandHandler(this);
66
67 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
68 m_menuLanguage.language[iPtr] = '?';
69 m_menuLanguage.language[3] = 0;
70 m_menuLanguage.device = iLogicalAddress;
71
72 m_strDeviceName = ToString(m_iLogicalAddress);
73}
74
75CCECBusDevice::~CCECBusDevice(void)
76{
77 delete m_handler;
78}
79
80bool CCECBusDevice::HandleCommand(const cec_command &command)
81{
82 bool bHandled(false);
83
84 /* update "last active" */
85 {
86 CLockObject lock(m_mutex);
87 m_iLastActive = GetTimeMs();
88
89 /* don't call GetStatus() here, just read the value with the mutex locked */
90 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
91 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
92
93 MarkBusy();
94 }
95
96 /* handle the command */
97 bHandled = m_handler->HandleCommand(command);
98
99 /* change status to present */
100 if (bHandled)
101 {
102 CLockObject lock(m_mutex);
103 if (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
104 {
105 if (m_deviceStatus != CEC_DEVICE_STATUS_PRESENT)
106 CLibCEC::AddLog(CEC_LOG_DEBUG, "device %s (%x) status changed to present after command %s", GetLogicalAddressName(), (uint8_t)GetLogicalAddress(), ToString(command.opcode));
107 m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
108 }
109 }
110
111 MarkReady();
112 return bHandled;
113}
114
115bool CCECBusDevice::PowerOn(void)
116{
117 bool bReturn(false);
118 GetVendorId(); // ensure that we got the vendor id, because the implementations vary per vendor
119
120 MarkBusy();
121 cec_power_status currentStatus = GetPowerStatus(false);
122 if (currentStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON &&
123 currentStatus != CEC_POWER_STATUS_ON)
124 {
125 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
126 if (m_handler->PowerOn(GetMyLogicalAddress(), m_iLogicalAddress))
127 {
128 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
129 bReturn = true;
130 }
131 }
132 else
133 {
134 CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' (%X) is already '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(currentStatus));
135 }
136
137 MarkReady();
138 return bReturn;
139}
140
141bool CCECBusDevice::Standby(void)
142{
143 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress);
144 MarkBusy();
145 bool bReturn = m_handler->TransmitStandby(GetMyLogicalAddress(), m_iLogicalAddress);
146 MarkReady();
147 return bReturn;
148}
149
150/** @name Getters */
151//@{
152cec_version CCECBusDevice::GetCecVersion(bool bUpdate /* = false */)
153{
154 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
155 bool bRequestUpdate(false);
156 {
157 CLockObject lock(m_mutex);
158 bRequestUpdate = bIsPresent &&
159 (bUpdate || m_cecVersion == CEC_VERSION_UNKNOWN);
160 }
161
162 if (bRequestUpdate)
163 RequestCecVersion();
164
165 CLockObject lock(m_mutex);
166 return m_cecVersion;
167}
168
169bool CCECBusDevice::RequestCecVersion(void)
170{
171 bool bReturn(false);
172
173 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
174 !IsUnsupportedFeature(CEC_OPCODE_GET_CEC_VERSION))
175 {
176 MarkBusy();
177 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
178
179 bReturn = m_handler->TransmitRequestCecVersion(GetMyLogicalAddress(), m_iLogicalAddress);
180 MarkReady();
181 }
182 return bReturn;
183}
184
185const char* CCECBusDevice::GetLogicalAddressName(void) const
186{
187 return ToString(m_iLogicalAddress);
188}
189
190cec_menu_language &CCECBusDevice::GetMenuLanguage(bool bUpdate /* = false */)
191{
192 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
193 bool bRequestUpdate(false);
194 {
195 CLockObject lock(m_mutex);
196 bRequestUpdate = (bIsPresent &&
197 (bUpdate || !strcmp(m_menuLanguage.language, "???")));
198 }
199
200 if (bRequestUpdate)
201 RequestMenuLanguage();
202
203 CLockObject lock(m_mutex);
204 return m_menuLanguage;
205}
206
207bool CCECBusDevice::RequestMenuLanguage(void)
208{
209 bool bReturn(false);
210
211 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
212 !IsUnsupportedFeature(CEC_OPCODE_GET_MENU_LANGUAGE))
213 {
214 MarkBusy();
215 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
216 bReturn = m_handler->TransmitRequestMenuLanguage(GetMyLogicalAddress(), m_iLogicalAddress);
217 MarkReady();
218 }
219 return bReturn;
220}
221
222cec_menu_state CCECBusDevice::GetMenuState(void)
223{
224 CLockObject lock(m_mutex);
225 return m_menuState;
226}
227
228cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
229{
230 return m_processor->GetLogicalAddress();
231}
232
233uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
234{
235 return m_processor->GetPhysicalAddress();
236}
237
238CStdString CCECBusDevice::GetOSDName(bool bUpdate /* = false */)
239{
240 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
241 bool bRequestUpdate(false);
242 {
243 CLockObject lock(m_mutex);
244 bRequestUpdate = bIsPresent &&
245 (bUpdate || m_strDeviceName.Equals(ToString(m_iLogicalAddress))) &&
246 m_type != CEC_DEVICE_TYPE_TV;
247 }
248
249 if (bRequestUpdate)
250 RequestOSDName();
251
252 CLockObject lock(m_mutex);
253 return m_strDeviceName;
254}
255
256bool CCECBusDevice::RequestOSDName(void)
257{
258 bool bReturn(false);
259
260 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
261 !IsUnsupportedFeature(CEC_OPCODE_GIVE_OSD_NAME))
262 {
263 MarkBusy();
264 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting OSD name of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
265 bReturn = m_handler->TransmitRequestOSDName(GetMyLogicalAddress(), m_iLogicalAddress);
266 MarkReady();
267 }
268 return bReturn;
269}
270
271uint16_t CCECBusDevice::GetPhysicalAddress(bool bUpdate /* = false */)
272{
273 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
274 bool bRequestUpdate(false);
275 {
276 CLockObject lock(m_mutex);
277 bRequestUpdate = bIsPresent &&
278 (m_iPhysicalAddress == 0xFFFF || bUpdate);
279 }
280
281 if (bRequestUpdate && !RequestPhysicalAddress())
282 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address");
283
284 CLockObject lock(m_mutex);
285 return m_iPhysicalAddress;
286}
287
288bool CCECBusDevice::RequestPhysicalAddress(void)
289{
290 bool bReturn(false);
291
292 if (!MyLogicalAddressContains(m_iLogicalAddress))
293 {
294 MarkBusy();
295 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
296 bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress);
297 MarkReady();
298 }
299 return bReturn;
300}
301
302cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */)
303{
304 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
305 bool bRequestUpdate(false);
306 {
307 CLockObject lock(m_mutex);
308 bRequestUpdate = (bIsPresent &&
309 (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
310 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON ||
311 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY ||
312 GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME));
313 }
314
315 if (bRequestUpdate)
316 RequestPowerStatus();
317
318 CLockObject lock(m_mutex);
319 return m_powerStatus;
320}
321
322bool CCECBusDevice::RequestPowerStatus(void)
323{
324 bool bReturn(false);
325
326 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
327 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
328 {
329 MarkBusy();
330 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
331 bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress);
332 MarkReady();
333 }
334 return bReturn;
335}
336
337cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */)
338{
339 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
340 bool bRequestUpdate(false);
341 {
342 CLockObject lock(m_mutex);
343 bRequestUpdate = (bIsPresent &&
344 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
345 }
346
347 if (bRequestUpdate)
348 RequestVendorId();
349
350 CLockObject lock(m_mutex);
351 return m_vendor;
352}
353
354bool CCECBusDevice::RequestVendorId(void)
355{
356 bool bReturn(false);
357
358 if (!MyLogicalAddressContains(m_iLogicalAddress))
359 {
360 MarkBusy();
361 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
362 bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress);
363 MarkReady();
364
365 ReplaceHandler(true);
366 }
367 return bReturn;
368}
369
370const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */)
371{
372 return ToString(GetVendorId(bUpdate));
373}
374
375bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
376{
377 return m_processor->HasLogicalAddress(address);
378}
379
380bool CCECBusDevice::NeedsPoll(void)
381{
382 bool bSendPoll(false);
383 switch (m_iLogicalAddress)
384 {
385 case CECDEVICE_PLAYBACKDEVICE3:
386 {
387 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus();
388 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
389 }
390 break;
391 case CECDEVICE_PLAYBACKDEVICE2:
392 {
393 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus();
394 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
395 }
396 break;
397 case CECDEVICE_RECORDINGDEVICE3:
398 {
399 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus();
400 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
401 }
402 break;
403 case CECDEVICE_RECORDINGDEVICE2:
404 {
405 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus();
406 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
407 }
408 break;
409 case CECDEVICE_TUNER4:
410 {
411 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus();
412 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
413 }
414 break;
415 case CECDEVICE_TUNER3:
416 {
417 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus();
418 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
419 }
420 break;
421 case CECDEVICE_TUNER2:
422 {
423 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus();
424 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
425 }
426 break;
427 case CECDEVICE_AUDIOSYSTEM:
428 case CECDEVICE_PLAYBACKDEVICE1:
429 case CECDEVICE_RECORDINGDEVICE1:
430 case CECDEVICE_TUNER1:
431 case CECDEVICE_TV:
432 bSendPoll = true;
433 break;
434 default:
435 break;
436 }
437
438 return bSendPoll;
439}
440
441cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */)
442{
443 cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
444 bool bNeedsPoll(false);
445
446 {
447 CLockObject lock(m_mutex);
448 status = m_deviceStatus;
449 bNeedsPoll = (m_deviceStatus != CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC &&
450 (m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN || bForcePoll));
451 }
452
453 if (bNeedsPoll)
454 {
455 bool bPollAcked(false);
456 if (bNeedsPoll || NeedsPoll())
457 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
458
459 status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
460 SetDeviceStatus(status);
461 }
462
463 return status;
464}
465
466//@}
467
468/** @name Setters */
469//@{
470void CCECBusDevice::SetCecVersion(const cec_version newVersion)
471{
472 m_cecVersion = newVersion;
473 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
474}
475
476void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
477{
478 CLockObject lock(m_mutex);
479 if (language.device == m_iLogicalAddress)
480 {
481 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
482 m_menuLanguage = language;
483 }
484}
485
486void CCECBusDevice::SetOSDName(CStdString strName)
487{
488 CLockObject lock(m_mutex);
489 if (m_strDeviceName != strName)
490 {
491 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str());
492 m_strDeviceName = strName;
493 }
494}
495
496void CCECBusDevice::SetMenuState(const cec_menu_state state)
497{
498 CLockObject lock(m_mutex);
499 if (m_menuState != state)
500 {
501 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
502 m_menuState = state;
503 }
504}
505
506void CCECBusDevice::SetInactiveSource(void)
507{
508 {
509 CLockObject lock(m_mutex);
510 m_bActiveSource = false;
511 }
512
513 if (MyLogicalAddressContains(m_iLogicalAddress))
514 SetPowerStatus(CEC_POWER_STATUS_STANDBY);
515}
516
517void CCECBusDevice::SetActiveSource(void)
518{
519 CLockObject lock(m_mutex);
520 if (!m_bActiveSource)
521 CLibCEC::AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
522
523 for (int iPtr = 0; iPtr < 16; iPtr++)
524 if (iPtr != m_iLogicalAddress)
525 m_processor->m_busDevices[iPtr]->SetInactiveSource();
526
527 m_bActiveSource = true;
528 SetPowerStatus(CEC_POWER_STATUS_ON);
529}
530
531bool CCECBusDevice::TryLogicalAddress(void)
532{
533 CLibCEC::AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
534
535 m_processor->SetAckMask(0);
536 if (!TransmitPoll(m_iLogicalAddress))
537 {
538 CLibCEC::AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName());
539 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
540
541 return true;
542 }
543
544 CLibCEC::AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
545 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
546 return false;
547}
548
549void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
550{
551 CLockObject lock(m_mutex);
552 switch (newStatus)
553 {
554 case CEC_DEVICE_STATUS_UNKNOWN:
555 if (m_deviceStatus != newStatus)
556 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress));
557 m_iStreamPath = 0;
558 m_powerStatus = CEC_POWER_STATUS_UNKNOWN;
559 m_vendor = CEC_VENDOR_UNKNOWN;
560 m_menuState = CEC_MENU_STATE_ACTIVATED;
561 m_bActiveSource = false;
562 m_iLastActive = 0;
563 m_cecVersion = CEC_VERSION_UNKNOWN;
564 m_deviceStatus = newStatus;
565 break;
566 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
567 if (m_deviceStatus != newStatus)
568 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress));
569 m_iStreamPath = 0;
570 m_powerStatus = CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON;
571 m_vendor = CEC_VENDOR_UNKNOWN;
572 m_menuState = CEC_MENU_STATE_ACTIVATED;
573 m_bActiveSource = false;
574 m_iLastActive = 0;
575 m_cecVersion = CEC_VERSION_1_3A;
576 m_deviceStatus = newStatus;
577 break;
578 case CEC_DEVICE_STATUS_PRESENT:
579 if (m_deviceStatus != newStatus)
580 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'present'", ToString(m_iLogicalAddress));
581 m_deviceStatus = newStatus;
582 break;
583 case CEC_DEVICE_STATUS_NOT_PRESENT:
584 if (m_deviceStatus != newStatus)
585 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress));
586 m_deviceStatus = newStatus;
587 break;
588 }
589}
590
591void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
592{
593 CLockObject lock(m_mutex);
594 if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
595 {
596 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
597 m_iPhysicalAddress = iNewAddress;
598 }
599}
600
601void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
602{
603 CLockObject lock(m_mutex);
604 if (iNewAddress > 0)
605 {
606 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
607 m_iStreamPath = iNewAddress;
608
609 if (iNewAddress > 0)
610 {
611 lock.Unlock();
612 SetPowerStatus(CEC_POWER_STATUS_ON);
613 }
614 }
615}
616
617void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
618{
619 CLockObject lock(m_mutex);
620 if (m_powerStatus != powerStatus)
621 {
622 m_iLastPowerStateUpdate = GetTimeMs();
623 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
624 m_powerStatus = powerStatus;
625 }
626}
627
628void CCECBusDevice::MarkBusy(void)
629{
630 CLockObject handlerLock(m_handlerMutex);
631 ++m_iHandlerUseCount;
632}
633
634void CCECBusDevice::MarkReady(void)
635{
636 CLockObject handlerLock(m_handlerMutex);
637 if (m_iHandlerUseCount > 0)
638 --m_iHandlerUseCount;
639}
640
641bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
642{
643 bool bInitHandler(false);
644 {
645 CTryLockObject lock(m_mutex);
646 if (!lock.IsLocked())
647 return false;
648
649 CLockObject handlerLock(m_handlerMutex);
650 if (m_iHandlerUseCount > 0)
651 return false;
652
653 MarkBusy();
654
655 if (m_vendor != m_handler->GetVendorId())
656 {
657 if (CCECCommandHandler::HasSpecificHandler(m_vendor))
658 {
659 CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
660 delete m_handler;
661
662 switch (m_vendor)
663 {
664 case CEC_VENDOR_SAMSUNG:
665 m_handler = new CANCommandHandler(this);
666 break;
667 case CEC_VENDOR_LG:
668 m_handler = new CSLCommandHandler(this);
669 break;
670 case CEC_VENDOR_PANASONIC:
671 m_handler = new CVLCommandHandler(this);
672 break;
673 default:
674 m_handler = new CCECCommandHandler(this);
675 break;
676 }
677
678 m_handler->SetVendorId(m_vendor);
679 bInitHandler = true;
680 }
681 }
682 }
683
684 if (bInitHandler)
685 {
686 m_handler->InitHandler();
687
688 if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource())
689 m_handler->ActivateSource();
690 }
691
692 MarkReady();
693
694 return true;
695}
696
697bool 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 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
708
709 return bVendorChanged;
710}
711//@}
712
713/** @name Transmit methods */
714//@{
715bool CCECBusDevice::TransmitActiveSource(void)
716{
717 bool bSendActiveSource(false);
718
719 {
720 CLockObject lock(m_mutex);
721 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
722 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
723 else if (m_bActiveSource)
724 {
725 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
726 bSendActiveSource = true;
727 }
728 else
729 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
730 }
731
732 if (bSendActiveSource)
733 {
734 MarkBusy();
735 m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
736 MarkReady();
737 return true;
738 }
739
740 return false;
741}
742
743bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
744{
745 cec_version version;
746 {
747 CLockObject lock(m_mutex);
748 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion));
749 version = m_cecVersion;
750 }
751
752 MarkBusy();
753 bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version);
754 MarkReady();
755 return bReturn;
756}
757
758bool CCECBusDevice::TransmitImageViewOn(void)
759{
760 {
761 CLockObject lock(m_mutex);
762 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
763 {
764 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
765 return false;
766 }
767 }
768
769 MarkBusy();
770 m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
771 MarkReady();
772 return true;
773}
774
775bool CCECBusDevice::TransmitInactiveSource(void)
776{
777 uint16_t iPhysicalAddress;
778 {
779 CLockObject lock(m_mutex);
780 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
781 iPhysicalAddress = m_iPhysicalAddress;
782 }
783
784 MarkBusy();
785 bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
786 MarkReady();
787 return bReturn;
788}
789
790bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
791{
792 cec_menu_state menuState;
793 {
794 CLockObject lock(m_mutex);
795 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
796 menuState = m_menuState;
797 }
798
799 MarkBusy();
800 bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState);
801 MarkReady();
802 return bReturn;
803}
804
805bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
806{
807 CStdString strDeviceName;
808 {
809 CLockObject lock(m_mutex);
810 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str());
811 strDeviceName = m_strDeviceName;
812 }
813
814 MarkBusy();
815 bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName);
816 MarkReady();
817 return bReturn;
818}
819
820bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
821{
822 bool bReturn(false);
823 if (!m_processor->m_busDevices[dest]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
824 {
825 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage);
826 MarkBusy();
827 bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage);
828 MarkReady();
829 }
830 return bReturn;
831}
832
833bool CCECBusDevice::TransmitPhysicalAddress(void)
834{
835 uint16_t iPhysicalAddress;
836 cec_device_type type;
837 {
838 CLockObject lock(m_mutex);
839 if (m_iPhysicalAddress == 0xffff)
840 return false;
841
842 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
843 iPhysicalAddress = m_iPhysicalAddress;
844 type = m_type;
845 }
846
847 MarkBusy();
848 bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type);
849 MarkReady();
850 return bReturn;
851}
852
853bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
854{
855 bool bReturn(false);
856 if (dest == CECDEVICE_UNKNOWN)
857 dest = m_iLogicalAddress;
858
859 CCECBusDevice *destDevice = m_processor->m_busDevices[dest];
860 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
861 return bReturn;
862
863 MarkBusy();
864 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
865 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest);
866 CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
867
868 CLockObject lock(m_mutex);
869 if (bReturn)
870 {
871 m_iLastActive = GetTimeMs();
872 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
873 }
874 else
875 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
876
877 MarkReady();
878 return bReturn;
879}
880
881bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
882{
883 cec_power_status state;
884 {
885 CLockObject lock(m_mutex);
886 if (!IsActiveSource())
887 SetPowerStatus(CEC_POWER_STATUS_STANDBY);
888
889 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus));
890 state = m_powerStatus;
891 }
892
893 MarkBusy();
894 bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, dest, state);
895 MarkReady();
896 return bReturn;
897}
898
899bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */)
900{
901 bool bReturn(false);
902 uint64_t iVendorId;
903 {
904 CLockObject lock(m_mutex);
905 iVendorId = (uint64_t)m_vendor;
906 }
907
908 MarkBusy();
909 if (iVendorId == CEC_VENDOR_UNKNOWN)
910 {
911 if (bSendAbort)
912 {
913 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
914 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
915 bReturn = true;
916 }
917 }
918 else
919 {
920 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id %s (%x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString((cec_vendor_id)iVendorId), iVendorId);
921 bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId);
922 }
923 MarkReady();
924 return bReturn;
925}
926
927bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */)
928{
929 MarkBusy();
930 bool bReturn = m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait);
931 MarkReady();
932 return bReturn;
933}
934
935bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */)
936{
937 MarkBusy();
938 bool bReturn = m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait);
939 MarkReady();
940 return bReturn;
941}
942
943bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode) const
944{
945 bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end());
946 if (bUnsupported)
947 CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
948 return bUnsupported;
949}
950
951void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
952{
953 CLibCEC::AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
954 m_unsupportedFeatures.insert(opcode);
955}
956
957bool CCECBusDevice::ActivateSource(void)
958{
959 MarkBusy();
960 bool bReturn = m_handler->ActivateSource();
961 MarkReady();
962 return bReturn;
963}
964
965void CCECBusDevice::HandlePoll(cec_logical_address iDestination)
966{
967 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(m_iLogicalAddress), m_iLogicalAddress, ToString(iDestination), iDestination);
968 m_bAwaitingReceiveFailed = true;
969}
970
971bool CCECBusDevice::HandleReceiveFailed(void)
972{
973 bool bReturn = m_bAwaitingReceiveFailed;
974 m_bAwaitingReceiveFailed = false;
975 return bReturn;
976}
977
978//@}