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