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