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