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