cec: only send 'active source' when powered on
[deb_libcec.git] / src / lib / devices / CECBusDevice.cpp
... / ...
CommitLineData
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
41using namespace CEC;
42
43CCECBusDevice::CCECBusDevice(CCECProcessor *processor, cec_logical_address iLogicalAddress, uint16_t iPhysicalAddress) :
44 m_iPhysicalAddress(iPhysicalAddress),
45 m_iStreamPath(0),
46 m_iLogicalAddress(iLogicalAddress),
47 m_powerStatus(CEC_POWER_STATUS_UNKNOWN),
48 m_processor(processor),
49 m_bMenuActive(true),
50 m_bActiveSource(false),
51 m_iVendorClass(CEC_VENDOR_UNKNOWN),
52 m_iLastCommandSent(0),
53 m_iLastActive(0),
54 m_cecVersion(CEC_VERSION_UNKNOWN)
55{
56 m_handler = new CCECCommandHandler(this);
57
58 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
59 m_menuLanguage.language[iPtr] = '?';
60 m_menuLanguage.language[3] = 0;
61 m_menuLanguage.device = iLogicalAddress;
62
63 m_vendor.vendor = CEC_VENDOR_UNKNOWN;
64 m_type = CEC_DEVICE_TYPE_RESERVED;
65 m_strDeviceName = CCECCommandHandler::ToString(m_iLogicalAddress);
66}
67
68CCECBusDevice::~CCECBusDevice(void)
69{
70 m_condition.Broadcast();
71 delete m_handler;
72}
73
74void CCECBusDevice::AddLog(cec_log_level level, const CStdString &strMessage)
75{
76 m_processor->AddLog(level, strMessage);
77}
78
79bool CCECBusDevice::HandleCommand(const cec_command &command)
80{
81 CLockObject lock(&m_mutex);
82 m_iLastActive = GetTimeMs();
83 m_handler->HandleCommand(command);
84 m_condition.Signal();
85 return true;
86}
87
88void CCECBusDevice::PollVendorId(void)
89{
90 CLockObject lock(&m_mutex);
91 if (m_iLastActive > 0 && m_iLogicalAddress != CECDEVICE_BROADCAST &&
92 m_vendor.vendor == CEC_VENDOR_UNKNOWN &&
93 GetTimeMs() - m_iLastCommandSent > 5000 &&
94 !m_processor->IsMonitoring())
95 {
96 CStdString strLog;
97 strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
98 AddLog(CEC_LOG_NOTICE, strLog);
99 m_iLastCommandSent = GetTimeMs();
100
101 cec_command command;
102 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
103 if (m_processor->Transmit(command))
104 m_condition.Wait(&m_mutex, 1000);
105 }
106}
107
108bool CCECBusDevice::PowerOn(void)
109{
110 cec_power_status current = GetPowerStatus();
111 if (current != CEC_POWER_STATUS_ON &&
112 current != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
113 {
114 CStdString strLog;
115 strLog.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
116 AddLog(CEC_LOG_DEBUG, strLog.c_str());
117
118 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
119
120 cec_command command;
121 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_IMAGE_VIEW_ON);
122
123 return m_processor->Transmit(command);
124 }
125
126 return true;
127}
128
129bool 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//@{
143cec_version CCECBusDevice::GetCecVersion(void)
144{
145 if (m_cecVersion == CEC_VERSION_UNKNOWN)
146 {
147 if (!MyLogicalAddressContains(m_iLogicalAddress))
148 {
149 CStdString strLog;
150 strLog.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
151 AddLog(CEC_LOG_NOTICE, strLog);
152 cec_command command;
153 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_CEC_VERSION);
154 CLockObject lock(&m_mutex);
155 if (m_processor->Transmit(command))
156 m_condition.Wait(&m_mutex, 1000);
157 }
158 }
159
160 return m_cecVersion;
161}
162
163const char* CCECBusDevice::GetLogicalAddressName(void) const
164{
165 return CCECCommandHandler::ToString(m_iLogicalAddress);
166}
167
168cec_menu_language &CCECBusDevice::GetMenuLanguage(void)
169{
170 if (!strcmp(m_menuLanguage.language, "???"))
171 {
172 if (!MyLogicalAddressContains(m_iLogicalAddress))
173 {
174 CStdString strLog;
175 strLog.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
176 AddLog(CEC_LOG_NOTICE, strLog);
177 cec_command command;
178 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GET_MENU_LANGUAGE);
179 CLockObject lock(&m_mutex);
180 if (m_processor->Transmit(command))
181 m_condition.Wait(&m_mutex, 1000);
182 }
183 }
184
185 return m_menuLanguage;
186}
187
188cec_logical_address CCECBusDevice::GetMyLogicalAddress(void) const
189{
190 return m_processor->GetLogicalAddress();
191}
192
193uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
194{
195 return m_processor->GetPhysicalAddress();
196}
197
198cec_power_status CCECBusDevice::GetPowerStatus(void)
199{
200 if (m_powerStatus == CEC_POWER_STATUS_UNKNOWN)
201 {
202 if (!MyLogicalAddressContains(m_iLogicalAddress))
203 {
204 CStdString strLog;
205 strLog.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
206 AddLog(CEC_LOG_NOTICE, strLog);
207 cec_command command;
208 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
209 CLockObject lock(&m_mutex);
210 if (m_processor->Transmit(command))
211 m_condition.Wait(&m_mutex, 1000);
212 }
213 }
214
215 return m_powerStatus;
216}
217
218const cec_vendor &CCECBusDevice::GetVendor(void)
219{
220 if (m_vendor.vendor == CEC_VENDOR_UNKNOWN)
221 {
222 if (!MyLogicalAddressContains(m_iLogicalAddress))
223 {
224 CStdString strLog;
225 strLog.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress);
226 AddLog(CEC_LOG_NOTICE, strLog);
227 cec_command command;
228 cec_command::format(command, GetMyLogicalAddress(), m_iLogicalAddress, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
229 CLockObject lock(&m_mutex);
230
231 if (m_processor->Transmit(command))
232 m_condition.Wait(&m_mutex, 1000);
233 }
234 }
235
236 return m_vendor;
237}
238
239bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address) const
240{
241 return m_processor->HasLogicalAddress(address);
242}
243
244//@}
245
246/** @name Setters */
247//@{
248void CCECBusDevice::SetCecVersion(const cec_version newVersion)
249{
250 CStdString strLog;
251 m_cecVersion = newVersion;
252
253 switch (newVersion)
254 {
255 case CEC_VERSION_1_2:
256 strLog.Format("%s (%X): CEC version 1.2", GetLogicalAddressName(), m_iLogicalAddress);
257 break;
258 case CEC_VERSION_1_2A:
259 strLog.Format("%s (%X): CEC version 1.2a", GetLogicalAddressName(), m_iLogicalAddress);
260 break;
261 case CEC_VERSION_1_3:
262 strLog.Format("%s (%X): CEC version 1.3", GetLogicalAddressName(), m_iLogicalAddress);
263 break;
264 case CEC_VERSION_1_3A:
265 strLog.Format("%s (%X): CEC version 1.3a", GetLogicalAddressName(), m_iLogicalAddress);
266 break;
267 default:
268 strLog.Format("%s (%X): unknown CEC version", GetLogicalAddressName(), m_iLogicalAddress);
269 m_cecVersion = CEC_VERSION_UNKNOWN;
270 break;
271 }
272 AddLog(CEC_LOG_DEBUG, strLog);
273}
274
275void CCECBusDevice::SetMenuLanguage(const cec_menu_language &language)
276{
277 if (language.device == m_iLogicalAddress)
278 {
279 CStdString strLog;
280 strLog.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress, language.language);
281 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
282 m_menuLanguage = language;
283 }
284}
285
286void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress)
287{
288 if (iNewAddress > 0)
289 {
290 CStdString strLog;
291 strLog.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress, iNewAddress);
292 AddLog(CEC_LOG_DEBUG, strLog.c_str());
293
294 m_iPhysicalAddress = iNewAddress;
295 }
296}
297
298void CCECBusDevice::SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress /* = 0 */)
299{
300 if (iNewAddress > 0)
301 {
302 CStdString strLog;
303 strLog.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress, iOldAddress, iNewAddress);
304 AddLog(CEC_LOG_DEBUG, strLog.c_str());
305
306 m_iStreamPath = iNewAddress;
307
308 if (iNewAddress > 0)
309 SetPowerStatus(CEC_POWER_STATUS_ON);
310 }
311}
312
313void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus)
314{
315 if (m_powerStatus != powerStatus)
316 {
317 CStdString strLog;
318 strLog.Format(">> %s (%X): power status changed from '%s' to '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(m_powerStatus), CCECCommandHandler::ToString(powerStatus));
319 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
320 m_powerStatus = powerStatus;
321 }
322}
323
324void CCECBusDevice::SetVendorId(uint64_t iVendorId, uint8_t iVendorClass /* = 0 */)
325{
326 m_vendor.vendor = (cec_vendor_id)iVendorId;
327 m_iVendorClass = iVendorClass;
328
329 switch (iVendorId)
330 {
331 case CEC_VENDOR_SAMSUNG:
332 if (m_handler->GetVendorId() != CEC_VENDOR_SAMSUNG)
333 {
334 delete m_handler;
335 m_handler = new CANCommandHandler(this);
336 }
337 break;
338 case CEC_VENDOR_LG:
339 if (m_handler->GetVendorId() != CEC_VENDOR_LG)
340 {
341 delete m_handler;
342 m_handler = new CSLCommandHandler(this);
343 }
344 break;
345 case CEC_VENDOR_PANASONIC:
346 if (m_handler->GetVendorId() != CEC_VENDOR_PANASONIC)
347 {
348 delete m_handler;
349 m_handler = new CVLCommandHandler(this);
350 }
351 break;
352 default:
353 if (m_handler->GetVendorId() != CEC_VENDOR_UNKNOWN)
354 {
355 delete m_handler;
356 m_handler = new CCECCommandHandler(this);
357 }
358 break;
359 }
360
361 CStdString strLog;
362 strLog.Format("%s (%X): vendor = %s (%06x) class = %2x", GetLogicalAddressName(), m_iLogicalAddress, GetVendorName(), GetVendorId(), GetVendorClass());
363 m_processor->AddLog(CEC_LOG_DEBUG, strLog.c_str());
364}
365//@}
366
367/** @name Transmit methods */
368//@{
369bool CCECBusDevice::TransmitActiveSource(void)
370{
371 if (m_powerStatus != CEC_POWER_STATUS_ON)
372 {
373 CStdString strLog;
374 strLog.Format("<< %s (%X) is not powered on", GetLogicalAddressName(), m_iLogicalAddress);
375 AddLog(CEC_LOG_DEBUG, strLog);
376 }
377 else if (m_bActiveSource)
378 {
379 CStdString strLog;
380 strLog.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
381 AddLog(CEC_LOG_NOTICE, strLog);
382
383 cec_command command;
384 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
385 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
386 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
387
388 return m_processor->Transmit(command);
389 }
390 else
391 {
392 CStdString strLog;
393 strLog.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress);
394 AddLog(CEC_LOG_DEBUG, strLog);
395 }
396
397 return false;
398}
399
400bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest)
401{
402 CStdString strLog;
403 strLog.Format("<< %s (%X) -> %s (%X): cec version ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
404 switch (m_cecVersion)
405 {
406 case CEC_VERSION_1_2:
407 strLog.append("1.2");
408 break;
409 case CEC_VERSION_1_2A:
410 strLog.append("1.2a");
411 break;
412 case CEC_VERSION_1_3:
413 strLog.append("1.3");
414 break;
415 case CEC_VERSION_1_3A:
416 strLog.append("1.3a");
417 break;
418 default:
419 strLog.append("unknown");
420 break;
421 }
422 AddLog(CEC_LOG_NOTICE, strLog);
423
424 cec_command command;
425 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_CEC_VERSION);
426 command.parameters.push_back(m_cecVersion);
427
428 return m_processor->Transmit(command);
429}
430
431bool CCECBusDevice::TransmitInactiveView(void)
432{
433 CStdString strLog;
434 strLog.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress);
435 AddLog(CEC_LOG_NOTICE, strLog);
436
437 cec_command command;
438 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
439 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
440 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
441
442 return m_processor->Transmit(command);
443}
444
445bool CCECBusDevice::TransmitMenuState(cec_logical_address dest)
446{
447 CStdString strLog;
448 strLog.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
449 if (m_bMenuActive)
450 strLog.append("menu active");
451 else
452 strLog.append("menu inactive");
453 AddLog(CEC_LOG_NOTICE, strLog);
454
455 cec_command command;
456 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_MENU_STATUS);
457 command.parameters.push_back(m_bMenuActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
458
459 return m_processor->Transmit(command);
460}
461
462bool CCECBusDevice::TransmitOSDName(cec_logical_address dest)
463{
464 CStdString strLog;
465 strLog.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest, m_strDeviceName.c_str());
466 AddLog(CEC_LOG_NOTICE, strLog.c_str());
467
468 cec_command command;
469 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_NAME);
470 for (unsigned int iPtr = 0; iPtr < m_strDeviceName.length(); iPtr++)
471 command.parameters.push_back(m_strDeviceName.at(iPtr));
472
473 return m_processor->Transmit(command);
474}
475
476bool CCECBusDevice::TransmitOSDString(cec_logical_address dest, cec_display_control duration, const char *strMessage)
477{
478 CStdString strLog;
479 strLog.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest, strMessage);
480 AddLog(CEC_LOG_NOTICE, strLog.c_str());
481
482 cec_command command;
483 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_SET_OSD_STRING);
484 command.parameters.push_back((uint8_t)duration);
485
486 unsigned int iLen = strlen(strMessage);
487 if (iLen > 13) iLen = 13;
488
489 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
490 command.parameters.push_back(strMessage[iPtr]);
491
492 return m_processor->Transmit(command);
493}
494
495bool CCECBusDevice::TransmitPhysicalAddress(void)
496{
497 CStdString strLog;
498 strLog.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress, m_iPhysicalAddress);
499 AddLog(CEC_LOG_NOTICE, strLog.c_str());
500
501 cec_command command;
502 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
503 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
504 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
505 command.parameters.push_back((uint8_t) (m_type));
506
507 return m_processor->Transmit(command);
508}
509
510bool CCECBusDevice::TransmitPoll(cec_logical_address dest)
511{
512 bool bReturn(false);
513
514 if (dest == CECDEVICE_UNKNOWN)
515 dest = m_iLogicalAddress;
516
517 CStdString strLog;
518 strLog.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
519 AddLog(CEC_LOG_NOTICE, strLog.c_str());
520
521 cec_command command;
522 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_NONE);
523 CLockObject lock(&m_mutex);
524
525 bReturn = m_processor->Transmit(command);
526 AddLog(CEC_LOG_DEBUG, bReturn ? ">> POLL sent" : ">> POLL not sent");
527 return bReturn;
528}
529
530bool CCECBusDevice::TransmitPowerState(cec_logical_address dest)
531{
532 CStdString strLog;
533 strLog.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest, CCECCommandHandler::ToString(m_powerStatus));
534 AddLog(CEC_LOG_NOTICE, strLog.c_str());
535
536 cec_command command;
537 cec_command::format(command, m_iLogicalAddress, dest, CEC_OPCODE_REPORT_POWER_STATUS);
538 command.parameters.push_back((uint8_t) m_powerStatus);
539
540 return m_processor->Transmit(command);
541}
542
543bool CCECBusDevice::TransmitVendorID(cec_logical_address dest)
544{
545 CStdString strLog;
546 strLog.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress, CCECCommandHandler::ToString(dest), dest);
547 AddLog(CEC_LOG_NOTICE, strLog);
548
549 m_processor->TransmitAbort(dest, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
550 return false;
551}
552//@}