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