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