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