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