cec: fixed compilation warning on win64
[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 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 bUpdate /* = false */, bool bSuppressPoll /* = false */)
282{
283 bool bIsPresent(GetStatus(false, bSuppressPoll) == CEC_DEVICE_STATUS_PRESENT);
284 bool bRequestUpdate(false);
285 {
286 CLockObject lock(m_mutex);
287 bRequestUpdate = bIsPresent &&
288 (m_iPhysicalAddress == 0xFFFF || bUpdate);
289 }
290
291 if (bRequestUpdate)
292 {
293 CheckVendorIdRequested();
294 if (!RequestPhysicalAddress())
295 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to request the physical address");
296 }
297
298 CLockObject lock(m_mutex);
299 return m_iPhysicalAddress;
300}
301
302bool CCECBusDevice::RequestPhysicalAddress(bool bWaitForResponse /* = true */)
303{
304 bool bReturn(false);
305
306 if (!MyLogicalAddressContains(m_iLogicalAddress))
307 {
308 MarkBusy();
309 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting physical address of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
310 bReturn = m_handler->TransmitRequestPhysicalAddress(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
311 MarkReady();
312 }
313 return bReturn;
314}
315
316cec_power_status CCECBusDevice::GetPowerStatus(bool bUpdate /* = false */)
317{
318 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
319 bool bRequestUpdate(false);
320 {
321 CLockObject lock(m_mutex);
322 bRequestUpdate = (bIsPresent &&
323 (bUpdate || m_powerStatus == CEC_POWER_STATUS_UNKNOWN ||
324 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON ||
325 m_powerStatus == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY ||
326 GetTimeMs() - m_iLastPowerStateUpdate >= CEC_POWER_STATE_REFRESH_TIME));
327 }
328
329 if (bRequestUpdate)
330 {
331 CheckVendorIdRequested();
332 RequestPowerStatus();
333 }
334
335 CLockObject lock(m_mutex);
336 return m_powerStatus;
337}
338
339bool CCECBusDevice::RequestPowerStatus(bool bWaitForResponse /* = true */)
340{
341 bool bReturn(false);
342
343 if (!MyLogicalAddressContains(m_iLogicalAddress) &&
344 !IsUnsupportedFeature(CEC_OPCODE_GIVE_DEVICE_POWER_STATUS))
345 {
346 MarkBusy();
347 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
348 bReturn = m_handler->TransmitRequestPowerStatus(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
349 MarkReady();
350 }
351 return bReturn;
352}
353
354cec_vendor_id CCECBusDevice::GetVendorId(bool bUpdate /* = false */)
355{
356 bool bIsPresent(GetStatus() == CEC_DEVICE_STATUS_PRESENT);
357 bool bRequestUpdate(false);
358 {
359 CLockObject lock(m_mutex);
360 bRequestUpdate = (bIsPresent &&
361 (bUpdate || m_vendor == CEC_VENDOR_UNKNOWN));
362 }
363
364 if (bRequestUpdate)
365 RequestVendorId();
366
367 CLockObject lock(m_mutex);
368 return m_vendor;
369}
370
371bool CCECBusDevice::RequestVendorId(bool bWaitForResponse /* = true */)
372{
373 bool bReturn(false);
374
375 if (!MyLogicalAddressContains(m_iLogicalAddress) && GetMyLogicalAddress() != CECDEVICE_UNKNOWN)
376 {
377 MarkBusy();
378 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
379 bReturn = m_handler->TransmitRequestVendorId(GetMyLogicalAddress(), m_iLogicalAddress, bWaitForResponse);
380 MarkReady();
381
382 if (bWaitForResponse)
383 ReplaceHandler(true);
384 }
385 return bReturn;
386}
387
388const char *CCECBusDevice::GetVendorName(bool bUpdate /* = false */)
389{
390 return ToString(GetVendorId(bUpdate));
391}
392
393bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
394{
395 return m_processor->HasLogicalAddress(address);
396}
397
398bool CCECBusDevice::NeedsPoll(void)
399{
400 bool bSendPoll(false);
401 switch (m_iLogicalAddress)
402 {
403 case CECDEVICE_PLAYBACKDEVICE3:
404 {
405 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE2]->GetStatus();
406 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
407 }
408 break;
409 case CECDEVICE_PLAYBACKDEVICE2:
410 {
411 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_PLAYBACKDEVICE1]->GetStatus();
412 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
413 }
414 break;
415 case CECDEVICE_RECORDINGDEVICE3:
416 {
417 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE2]->GetStatus();
418 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
419 }
420 break;
421 case CECDEVICE_RECORDINGDEVICE2:
422 {
423 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_RECORDINGDEVICE1]->GetStatus();
424 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
425 }
426 break;
427 case CECDEVICE_TUNER4:
428 {
429 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER3]->GetStatus();
430 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
431 }
432 break;
433 case CECDEVICE_TUNER3:
434 {
435 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER2]->GetStatus();
436 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
437 }
438 break;
439 case CECDEVICE_TUNER2:
440 {
441 cec_bus_device_status status = m_processor->m_busDevices[CECDEVICE_TUNER1]->GetStatus();
442 bSendPoll = (status == CEC_DEVICE_STATUS_PRESENT || status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
443 }
444 break;
445 case CECDEVICE_AUDIOSYSTEM:
446 case CECDEVICE_PLAYBACKDEVICE1:
447 case CECDEVICE_RECORDINGDEVICE1:
448 case CECDEVICE_TUNER1:
449 case CECDEVICE_TV:
450 bSendPoll = true;
451 break;
452 default:
453 break;
454 }
455
456 return bSendPoll;
457}
458
459cec_bus_device_status CCECBusDevice::GetStatus(bool bForcePoll /* = false */, bool bSuppressPoll /* = false */)
460{
461 cec_bus_device_status status(CEC_DEVICE_STATUS_UNKNOWN);
462 bool bNeedsPoll(false);
463
464 {
465 CLockObject lock(m_mutex);
466 status = m_deviceStatus;
467 bNeedsPoll = !bSuppressPoll &&
468 (bForcePoll || m_deviceStatus == CEC_DEVICE_STATUS_UNKNOWN);
469 }
470
471 if (bNeedsPoll)
472 {
473 bool bPollAcked(false);
474 if (bNeedsPoll && NeedsPoll())
475 bPollAcked = m_processor->PollDevice(m_iLogicalAddress);
476
477 status = bPollAcked ? CEC_DEVICE_STATUS_PRESENT : CEC_DEVICE_STATUS_NOT_PRESENT;
478 SetDeviceStatus(status);
479 }
480
481 return status;
482}
483
484//@}
485
486/** @name Setters */
487//@{
488void CCECBusDevice::SetCecVersion(const cec_version newVersion)
489{
490 if (m_cecVersion != newVersion)
491 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): CEC version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(newVersion));
492 m_cecVersion = newVersion;
493}
494
495void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
496{
497 CLockObject lock(m_mutex);
498 if (language.device == m_iLogicalAddress &&
499 strcmp(language.language, m_menuLanguage.language))
500 {
501 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
502 m_menuLanguage = language;
503 }
504}
505
506void CCECBusDevice::SetOSDName(CStdString strName)
507{
508 CLockObject lock(m_mutex);
509 if (m_strDeviceName != strName)
510 {
511 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): osd name set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, strName.c_str());
512 m_strDeviceName = strName;
513 }
514}
515
516void CCECBusDevice::SetMenuState(const cec_menu_state state)
517{
518 CLockObject lock(m_mutex);
519 if (m_menuState != state)
520 {
521 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): menu state set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_menuState));
522 m_menuState = state;
523 }
524}
525
526void CCECBusDevice::SetInactiveSource(void)
527{
528 {
529 CLockObject lock(m_mutex);
530 if (m_bActiveSource)
531 CLibCEC::AddLog(CEC_LOG_DEBUG, "marking %s (%X) as inactive source", GetLogicalAddressName(), m_iLogicalAddress);
532 m_bActiveSource = false;
533 }
534
535 if (MyLogicalAddressContains(m_iLogicalAddress))
536 SetPowerStatus(CEC_POWER_STATUS_STANDBY);
537}
538
539void CCECBusDevice::SetActiveSource(void)
540{
541 CLockObject lock(m_mutex);
542 if (!m_bActiveSource)
543 CLibCEC::AddLog(CEC_LOG_DEBUG, "making %s (%x) the active source", GetLogicalAddressName(), m_iLogicalAddress);
544
545 for (int iPtr = 0; iPtr < 16; iPtr++)
546 if (iPtr != m_iLogicalAddress)
547 m_processor->m_busDevices[iPtr]->SetInactiveSource();
548
549 m_bActiveSource = true;
550 SetPowerStatus(CEC_POWER_STATUS_ON);
551}
552
553bool CCECBusDevice::TryLogicalAddress(void)
554{
555 CLibCEC::AddLog(CEC_LOG_DEBUG, "trying logical address '%s'", GetLogicalAddressName());
556
557 m_processor->SetAckMask(0);
558 if (!TransmitPoll(m_iLogicalAddress))
559 {
560 CLibCEC::AddLog(CEC_LOG_NOTICE, "using logical address '%s'", GetLogicalAddressName());
561 SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
562
563 return true;
564 }
565
566 CLibCEC::AddLog(CEC_LOG_DEBUG, "logical address '%s' already taken", GetLogicalAddressName());
567 SetDeviceStatus(CEC_DEVICE_STATUS_PRESENT);
568 return false;
569}
570
571void CCECBusDevice::ResetDeviceStatus(void)
572{
573 CLockObject lock(m_mutex);
574 SetPowerStatus (CEC_POWER_STATUS_UNKNOWN);
575 SetVendorId (CEC_VENDOR_UNKNOWN);
576 SetMenuState (CEC_MENU_STATE_ACTIVATED);
577 SetCecVersion (CEC_VERSION_UNKNOWN);
578 SetStreamPath (0);
579 SetOSDName (ToString(m_iLogicalAddress));
580 SetInactiveSource();
581 m_iLastActive = 0;
582}
583
584void CCECBusDevice::SetDeviceStatus(const cec_bus_device_status newStatus)
585{
586 {
587 CLockObject lock(m_mutex);
588 switch (newStatus)
589 {
590 case CEC_DEVICE_STATUS_UNKNOWN:
591 if (m_deviceStatus != newStatus)
592 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'unknown'", ToString(m_iLogicalAddress));
593 ResetDeviceStatus();
594 m_deviceStatus = newStatus;
595 break;
596 case CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC:
597 if (m_deviceStatus != newStatus)
598 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'handled by libCEC'", ToString(m_iLogicalAddress));
599 SetPowerStatus (CEC_POWER_STATUS_ON);
600 SetVendorId (CEC_VENDOR_UNKNOWN);
601 SetMenuState (CEC_MENU_STATE_ACTIVATED);
602 SetCecVersion (CEC_VERSION_1_3A);
603 SetStreamPath (0);
604 SetInactiveSource();
605 m_iLastActive = 0;
606 m_deviceStatus = newStatus;
607 break;
608 case CEC_DEVICE_STATUS_PRESENT:
609 if (m_deviceStatus != newStatus)
610 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'present'", ToString(m_iLogicalAddress));
611 m_deviceStatus = newStatus;
612 break;
613 case CEC_DEVICE_STATUS_NOT_PRESENT:
614 if (m_deviceStatus != newStatus)
615 {
616 CLibCEC::AddLog(CEC_LOG_DEBUG, "device status of %s changed into 'not present'", ToString(m_iLogicalAddress));
617 ResetDeviceStatus();
618 m_deviceStatus = newStatus;
619 }
620 break;
621 }
622 }
623
624 if (newStatus == CEC_DEVICE_STATUS_PRESENT)
625 RequestVendorId(false);
626}
627
628void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
629{
630 CLockObject lock(m_mutex);
631 if (iNewAddress > 0 && m_iPhysicalAddress != iNewAddress)
632 {
633 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
634 m_iPhysicalAddress = iNewAddress;
635 }
636}
637
638void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
639{
640 CLockObject lock(m_mutex);
641 if (iNewAddress > 0)
642 {
643 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress == 0 ? m_iStreamPath : iOldAddress, iNewAddress);
644 m_iStreamPath = iNewAddress;
645
646 // suppress polls when searching for a device
647 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress, false, true);
648 if (device)
649 {
650 // if a device is found with the new physical address, mark it as active, which will automatically mark all other devices as inactive
651 device->SetActiveSource();
652 }
653 else
654 {
655 // try to find the device with the old address, and mark it as inactive when found
656 device = m_processor->GetDeviceByPhysicalAddress(iOldAddress, false, true);
657 if (device)
658 device->SetInactiveSource();
659 }
660
661 if (iNewAddress > 0)
662 {
663 lock.Unlock();
664 SetPowerStatus(CEC_POWER_STATUS_ON);
665 }
666 }
667}
668
669void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
670{
671 CLockObject lock(m_mutex);
672 if (m_powerStatus != powerStatus)
673 {
674 m_iLastPowerStateUpdate = GetTimeMs();
675 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_powerStatus), ToString(powerStatus));
676 m_powerStatus = powerStatus;
677 }
678}
679
680void CCECBusDevice::MarkBusy(void)
681{
682 CLockObject handlerLock(m_handlerMutex);
683 ++m_iHandlerUseCount;
684}
685
686void CCECBusDevice::MarkReady(void)
687{
688 CLockObject handlerLock(m_handlerMutex);
689 if (m_iHandlerUseCount > 0)
690 --m_iHandlerUseCount;
691}
692
693bool CCECBusDevice::ReplaceHandler(bool bActivateSource /* = true */)
694{
695 bool bInitHandler(false);
696 {
697 CTryLockObject lock(m_mutex);
698 if (!lock.IsLocked())
699 return false;
700
701 CLockObject handlerLock(m_handlerMutex);
702 if (m_iHandlerUseCount > 0)
703 return false;
704
705 MarkBusy();
706
707 if (m_vendor != m_handler->GetVendorId())
708 {
709 if (CCECCommandHandler::HasSpecificHandler(m_vendor))
710 {
711 CLibCEC::AddLog(CEC_LOG_DEBUG, "replacing the command handler for device '%s' (%x)", GetLogicalAddressName(), GetLogicalAddress());
712 delete m_handler;
713
714 switch (m_vendor)
715 {
716 case CEC_VENDOR_SAMSUNG:
717 m_handler = new CANCommandHandler(this);
718 break;
719 case CEC_VENDOR_LG:
720 m_handler = new CSLCommandHandler(this);
721 break;
722 case CEC_VENDOR_PANASONIC:
723 m_handler = new CVLCommandHandler(this);
724 break;
725 default:
726 m_handler = new CCECCommandHandler(this);
727 break;
728 }
729
730 m_handler->SetVendorId(m_vendor);
731 bInitHandler = true;
732 }
733 }
734 }
735
736 if (bInitHandler)
737 {
738 m_handler->InitHandler();
739
740 if (bActivateSource && m_processor->GetLogicalAddresses().IsSet(m_iLogicalAddress) && m_processor->IsInitialised() && IsActiveSource())
741 m_handler->ActivateSource();
742 }
743
744 MarkReady();
745
746 return true;
747}
748
749bool CCECBusDevice::SetVendorId(uint64_t iVendorId)
750{
751 bool bVendorChanged(false);
752
753 {
754 CLockObject lock(m_mutex);
755 bVendorChanged = (m_vendor != (cec_vendor_id)iVendorId);
756 m_vendor = (cec_vendor_id)iVendorId;
757 }
758
759 if (bVendorChanged)
760 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s (%X): vendor = %s (%06x)", GetLogicalAddressName(), m_iLogicalAddress, ToString(m_vendor), m_vendor);
761
762 return bVendorChanged;
763}
764//@}
765
766/** @name Transmit methods */
767//@{
768bool CCECBusDevice::TransmitActiveSource(void)
769{
770 bool bSendActiveSource(false);
771
772 {
773 CLockObject lock(m_mutex);
774 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
775 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
776 else if (m_bActiveSource)
777 {
778 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
779 bSendActiveSource = true;
780 }
781 else
782 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
783 }
784
785 if (bSendActiveSource)
786 {
787 MarkBusy();
788 m_handler->TransmitActiveSource(m_iLogicalAddress, m_iPhysicalAddress);
789 MarkReady();
790 return true;
791 }
792
793 return false;
794}
795
796bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
797{
798 cec_version version;
799 {
800 CLockObject lock(m_mutex);
801 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): cec version %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_cecVersion));
802 version = m_cecVersion;
803 }
804
805 MarkBusy();
806 bool bReturn = m_handler->TransmitCECVersion(m_iLogicalAddress, dest, version);
807 MarkReady();
808 return bReturn;
809}
810
811bool CCECBusDevice::TransmitImageViewOn(void)
812{
813 {
814 CLockObject lock(m_mutex);
815 if (m_powerStatus != CEC_POWER_STATUS_ON && m_powerStatus != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
816 {
817 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
818 return false;
819 }
820 }
821
822 MarkBusy();
823 m_handler->TransmitImageViewOn(m_iLogicalAddress, CECDEVICE_TV);
824 MarkReady();
825 return true;
826}
827
828bool CCECBusDevice::TransmitInactiveSource(void)
829{
830 uint16_t iPhysicalAddress;
831 {
832 CLockObject lock(m_mutex);
833 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): inactive source", GetLogicalAddressName(), m_iLogicalAddress);
834 iPhysicalAddress = m_iPhysicalAddress;
835 }
836
837 MarkBusy();
838 bool bReturn = m_handler->TransmitInactiveSource(m_iLogicalAddress, iPhysicalAddress);
839 MarkReady();
840 return bReturn;
841}
842
843bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
844{
845 cec_menu_state menuState;
846 {
847 CLockObject lock(m_mutex);
848 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): menu state '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_menuState));
849 menuState = m_menuState;
850 }
851
852 MarkBusy();
853 bool bReturn = m_handler->TransmitMenuState(m_iLogicalAddress, dest, menuState);
854 MarkReady();
855 return bReturn;
856}
857
858bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
859{
860 CStdString strDeviceName;
861 {
862 CLockObject lock(m_mutex);
863 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, m_strDeviceName.c_str());
864 strDeviceName = m_strDeviceName;
865 }
866
867 MarkBusy();
868 bool bReturn = m_handler->TransmitOSDName(m_iLogicalAddress, dest, strDeviceName);
869 MarkReady();
870 return bReturn;
871}
872
873bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
874{
875 bool bReturn(false);
876 if (!m_processor->m_busDevices[dest]->IsUnsupportedFeature(CEC_OPCODE_SET_OSD_STRING))
877 {
878 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, strMessage);
879 MarkBusy();
880 bReturn = m_handler->TransmitOSDString(m_iLogicalAddress, dest, duration, strMessage);
881 MarkReady();
882 }
883 return bReturn;
884}
885
886bool CCECBusDevice::TransmitPhysicalAddress(void)
887{
888 uint16_t iPhysicalAddress;
889 cec_device_type type;
890 {
891 CLockObject lock(m_mutex);
892 if (m_iPhysicalAddress == 0xffff)
893 return false;
894
895 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
896 iPhysicalAddress = m_iPhysicalAddress;
897 type = m_type;
898 }
899
900 MarkBusy();
901 bool bReturn = m_handler->TransmitPhysicalAddress(m_iLogicalAddress, iPhysicalAddress, type);
902 MarkReady();
903 return bReturn;
904}
905
906bool CCECBusDevice::TransmitSetMenuLanguage(cec_logical_address dest)
907{
908 bool bReturn(false);
909 cec_menu_language language = GetMenuLanguage();
910
911 char lang[3];
912 {
913 CLockObject lock(m_mutex);
914 lang[0] = language.language[0];
915 lang[1] = language.language[1];
916 lang[2] = language.language[2];
917 }
918
919 MarkBusy();
920 if (lang[0] == '?' && lang[1] == '?' && lang[2] == '?')
921 {
922 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): Menu language feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
923 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
924 bReturn = true;
925 }
926 else
927 {
928 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> broadcast (F): Menu language '%s'", GetLogicalAddressName(), m_iLogicalAddress, lang);
929 bReturn = m_handler->TransmitSetMenuLanguage(m_iLogicalAddress, lang);
930 }
931 MarkReady();
932 return bReturn;
933}
934
935bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
936{
937 bool bReturn(false);
938 if (dest == CECDEVICE_UNKNOWN)
939 dest = m_iLogicalAddress;
940
941 CCECBusDevice *destDevice = m_processor->m_busDevices[dest];
942 if (destDevice->m_deviceStatus == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
943 return bReturn;
944
945 MarkBusy();
946 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
947 bReturn = m_handler->TransmitPoll(m_iLogicalAddress, dest);
948 CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
949
950 CLockObject lock(m_mutex);
951 if (bReturn)
952 {
953 m_iLastActive = GetTimeMs();
954 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_PRESENT;
955 }
956 else
957 destDevice->m_deviceStatus = CEC_DEVICE_STATUS_NOT_PRESENT;
958
959 MarkReady();
960 return bReturn;
961}
962
963bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
964{
965 cec_power_status state;
966 {
967 CLockObject lock(m_mutex);
968 if (!IsActiveSource())
969 {
970 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);
971 SetPowerStatus(CEC_POWER_STATUS_STANDBY);
972 }
973
974 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest, ToString(m_powerStatus));
975 state = m_powerStatus;
976 }
977
978 MarkBusy();
979 bool bReturn = m_handler->TransmitPowerState(m_iLogicalAddress, dest, state);
980 MarkReady();
981 return bReturn;
982}
983
984bool CCECBusDevice::TransmitVendorID(cec_logical_address dest, bool bSendAbort /* = true */)
985{
986 bool bReturn(false);
987 uint64_t iVendorId;
988 {
989 CLockObject lock(m_mutex);
990 iVendorId = (uint64_t)m_vendor;
991 }
992
993 MarkBusy();
994 if (iVendorId == CEC_VENDOR_UNKNOWN)
995 {
996 if (bSendAbort)
997 {
998 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, ToString(dest), dest);
999 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
1000 bReturn = true;
1001 }
1002 }
1003 else
1004 {
1005 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);
1006 bReturn = m_handler->TransmitVendorID(m_iLogicalAddress, iVendorId);
1007 }
1008 MarkReady();
1009 return bReturn;
1010}
1011
1012bool CCECBusDevice::TransmitKeypress(cec_user_control_code key, bool bWait /* = true */)
1013{
1014 MarkBusy();
1015 bool bReturn = m_handler->TransmitKeypress(m_processor->GetLogicalAddress(), m_iLogicalAddress, key, bWait);
1016 MarkReady();
1017 return bReturn;
1018}
1019
1020bool CCECBusDevice::TransmitKeyRelease(bool bWait /* = true */)
1021{
1022 MarkBusy();
1023 bool bReturn = m_handler->TransmitKeyRelease(m_processor->GetLogicalAddress(), m_iLogicalAddress, bWait);
1024 MarkReady();
1025 return bReturn;
1026}
1027
1028bool CCECBusDevice::IsUnsupportedFeature(cec_opcode opcode)
1029{
1030 CLockObject lock(m_mutex);
1031 bool bUnsupported = (m_unsupportedFeatures.find(opcode) != m_unsupportedFeatures.end());
1032 if (bUnsupported)
1033 CLibCEC::AddLog(CEC_LOG_NOTICE, "'%s' is marked as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
1034 return bUnsupported;
1035}
1036
1037void CCECBusDevice::SetUnsupportedFeature(cec_opcode opcode)
1038{
1039 CLockObject lock(m_mutex);
1040 CLibCEC::AddLog(CEC_LOG_DEBUG, "marking opcode '%s' as unsupported feature for device '%s'", ToString(opcode), GetLogicalAddressName());
1041 m_unsupportedFeatures.insert(opcode);
1042}
1043
1044bool CCECBusDevice::ActivateSource(void)
1045{
1046 CLibCEC::AddLog(CEC_LOG_DEBUG, "activating source '%s'", ToString(m_iLogicalAddress));
1047 MarkBusy();
1048 bool bReturn = m_handler->ActivateSource();
1049 MarkReady();
1050 return bReturn;
1051}
1052
1053void CCECBusDevice::HandlePoll(cec_logical_address destination)
1054{
1055 if (destination >= 0 && destination < CECDEVICE_BROADCAST)
1056 {
1057 CCECBusDevice *device = m_processor->m_busDevices[destination];
1058 if (device)
1059 device->HandlePollFrom(m_iLogicalAddress);
1060 }
1061}
1062
1063void CCECBusDevice::HandlePollFrom(cec_logical_address initiator)
1064{
1065 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", ToString(initiator), initiator, ToString(m_iLogicalAddress), m_iLogicalAddress);
1066 m_bAwaitingReceiveFailed = true;
1067}
1068
1069bool CCECBusDevice::HandleReceiveFailed(void)
1070{
1071 bool bReturn = m_bAwaitingReceiveFailed;
1072 m_bAwaitingReceiveFailed = false;
1073 return bReturn;
1074}
1075
1076void CCECBusDevice::CheckVendorIdRequested(void)
1077{
1078 bool bRequestVendorId(false);
1079 {
1080 CLockObject lock(m_mutex);
1081 bRequestVendorId = !m_bVendorIdRequested;
1082 m_bVendorIdRequested = true;
1083 }
1084
1085 if (bRequestVendorId)
1086 {
1087 ReplaceHandler(false);
1088 GetVendorId();
1089 }
1090}
1091
1092bool CCECBusDevice::TransmitPendingActiveSourceCommands(void)
1093{
1094 MarkBusy();
1095 bool bReturn = m_handler->TransmitPendingActiveSourceCommands();
1096 MarkReady();
1097 return bReturn;
1098}
1099
1100//@}