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