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