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