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