2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
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"
43 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
) :
44 m_iPhysicalAddress(iPhysicalAddress
),
46 m_iLogicalAddress(iLogicalAddress
),
47 m_powerStatus(CEC_POWER_STATUS_UNKNOWN
),
48 m_processor(processor
),
50 m_bActiveSource(false),
51 m_iVendorClass(CEC_VENDOR_UNKNOWN
),
53 m_cecVersion(CEC_VERSION_UNKNOWN
)
55 m_handler
= new CCECCommandHandler(this);
57 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
58 m_menuLanguage
.language
[iPtr
] = '?';
59 m_menuLanguage
.language
[3] = 0;
60 m_menuLanguage
.device
= iLogicalAddress
;
62 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
63 m_type
= CEC_DEVICE_TYPE_RESERVED
;
64 m_strDeviceName
= CCECCommandHandler::ToString(m_iLogicalAddress
);
67 CCECBusDevice::~CCECBusDevice(void)
69 m_condition
.Broadcast();
73 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
75 m_processor
->AddLog(level
, strMessage
);
78 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
80 CLockObject
lock(&m_mutex
);
81 m_iLastActive
= GetTimeMs();
82 m_handler
->HandleCommand(command
);
87 void CCECBusDevice::PollVendorId(void)
89 CLockObject
lock(&m_mutex
);
90 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
91 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
92 GetTimeMs() - m_iLastActive
> 5000 &&
93 !m_processor
->IsMonitoring())
96 strLog
.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
97 AddLog(CEC_LOG_NOTICE
, strLog
);
98 m_iLastActive
= GetTimeMs();
101 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
102 if (m_processor
->Transmit(command
))
103 m_condition
.Wait(&m_mutex
, 1000);
107 bool CCECBusDevice::PowerOn(void)
110 strLog
.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
111 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
114 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
116 return m_processor
->Transmit(command
);
119 bool CCECBusDevice::Standby(void)
122 strLog
.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
123 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
126 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
128 return m_processor
->Transmit(command
);
133 cec_version
CCECBusDevice::GetCecVersion(void)
135 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
137 if (!MyLogicalAddressContains(m_iLogicalAddress
))
140 strLog
.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
141 AddLog(CEC_LOG_NOTICE
, strLog
);
143 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
144 CLockObject
lock(&m_mutex
);
145 if (m_processor
->Transmit(command
))
146 m_condition
.Wait(&m_mutex
, 1000);
153 const char* CCECBusDevice::GetLogicalAddressName(void) const
155 return CCECCommandHandler::ToString(m_iLogicalAddress
);
158 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
160 if (!strcmp(m_menuLanguage
.language
, "???"))
162 if (!MyLogicalAddressContains(m_iLogicalAddress
))
165 strLog
.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
166 AddLog(CEC_LOG_NOTICE
, strLog
);
168 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
169 CLockObject
lock(&m_mutex
);
170 if (m_processor
->Transmit(command
))
171 m_condition
.Wait(&m_mutex
, 1000);
175 return m_menuLanguage
;
178 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
180 return m_processor
->GetLogicalAddress();
183 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
185 return m_processor
->GetPhysicalAddress();
188 cec_power_status
CCECBusDevice::GetPowerStatus(void)
190 if (m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
192 if (!MyLogicalAddressContains(m_iLogicalAddress
))
195 strLog
.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
196 AddLog(CEC_LOG_NOTICE
, strLog
);
198 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
199 CLockObject
lock(&m_mutex
);
200 if (m_processor
->Transmit(command
))
201 m_condition
.Wait(&m_mutex
, 1000);
205 return m_powerStatus
;
208 const cec_vendor
&CCECBusDevice::GetVendor(void)
210 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
212 if (!MyLogicalAddressContains(m_iLogicalAddress
))
215 strLog
.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
216 AddLog(CEC_LOG_NOTICE
, strLog
);
218 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
219 CLockObject
lock(&m_mutex
);
221 if (m_processor
->Transmit(command
))
222 m_condition
.Wait(&m_mutex
, 1000);
229 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
231 return m_processor
->HasLogicalAddress(address
);
238 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
241 m_cecVersion
= newVersion
;
245 case CEC_VERSION_1_2
:
246 strLog
.Format("%s (%X): CEC version 1.2", GetLogicalAddressName(), m_iLogicalAddress
);
248 case CEC_VERSION_1_2A
:
249 strLog
.Format("%s (%X): CEC version 1.2a", GetLogicalAddressName(), m_iLogicalAddress
);
251 case CEC_VERSION_1_3
:
252 strLog
.Format("%s (%X): CEC version 1.3", GetLogicalAddressName(), m_iLogicalAddress
);
254 case CEC_VERSION_1_3A
:
255 strLog
.Format("%s (%X): CEC version 1.3a", GetLogicalAddressName(), m_iLogicalAddress
);
258 strLog
.Format("%s (%X): unknown CEC version", GetLogicalAddressName(), m_iLogicalAddress
);
259 m_cecVersion
= CEC_VERSION_UNKNOWN
;
262 AddLog(CEC_LOG_DEBUG
, strLog
);
265 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
267 if (language
.device
== m_iLogicalAddress
)
270 strLog
.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, language
.language
);
271 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
272 m_menuLanguage
= language
;
276 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
281 strLog
.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
282 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
284 m_iPhysicalAddress
= iNewAddress
;
288 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
293 strLog
.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
, iNewAddress
);
294 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
296 m_iStreamPath
= iNewAddress
;
300 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
302 if (m_powerStatus
!= powerStatus
)
305 strLog
.Format(">> %s (%X): power status changed from %2x to %2x", GetLogicalAddressName(), m_iLogicalAddress
, m_powerStatus
, powerStatus
);
306 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
307 m_powerStatus
= powerStatus
;
311 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
313 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
314 m_iVendorClass
= iVendorClass
;
318 case CEC_VENDOR_SAMSUNG
:
319 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
322 m_handler
= new CANCommandHandler(this);
326 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
329 m_handler
= new CSLCommandHandler(this);
332 case CEC_VENDOR_PANASONIC
:
333 if (m_handler
->GetVendorId() != CEC_VENDOR_PANASONIC
)
336 m_handler
= new CVLCommandHandler(this);
340 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
343 m_handler
= new CCECCommandHandler(this);
349 strLog
.Format("%s (%X): vendor = %s (%06x) class = %2x", GetLogicalAddressName(), m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
350 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
354 /** @name Transmit methods */
356 bool CCECBusDevice::TransmitActiveSource(void)
361 strLog
.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
362 AddLog(CEC_LOG_NOTICE
, strLog
);
365 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
366 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
367 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
369 return m_processor
->Transmit(command
);
374 strLog
.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
375 AddLog(CEC_LOG_DEBUG
, strLog
);
381 bool CCECBusDevice::TransmitActiveView(void)
384 strLog
.Format("<< %s (%X) -> broadcast (F): active view (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
385 AddLog(CEC_LOG_NOTICE
, strLog
);
388 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
389 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
390 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
392 return m_processor
->Transmit(command
);
395 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
398 strLog
.Format("<< %s (%X) -> %s (%X): cec version ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
399 switch (m_cecVersion
)
401 case CEC_VERSION_1_2
:
402 strLog
.append("1.2");
404 case CEC_VERSION_1_2A
:
405 strLog
.append("1.2a");
407 case CEC_VERSION_1_3
:
408 strLog
.append("1.3");
410 case CEC_VERSION_1_3A
:
411 strLog
.append("1.3a");
414 strLog
.append("unknown");
417 AddLog(CEC_LOG_NOTICE
, strLog
);
420 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
421 command
.parameters
.push_back(m_cecVersion
);
423 return m_processor
->Transmit(command
);
426 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest
)
428 // need to support opcodes play and deck control before doing anything with this
430 strLog
.Format("<< %s (%X) -> %s (%X): deck status feature abort", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
431 AddLog(CEC_LOG_NOTICE
, strLog
);
433 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DECK_STATUS
);
437 bool CCECBusDevice::TransmitInactiveView(void)
440 strLog
.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress
);
441 AddLog(CEC_LOG_NOTICE
, strLog
);
444 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
445 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
446 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
448 return m_processor
->Transmit(command
);
451 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
454 strLog
.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
456 strLog
.append("menu active");
458 strLog
.append("menu inactive");
459 AddLog(CEC_LOG_NOTICE
, strLog
);
462 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
463 command
.parameters
.push_back(m_bMenuActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
465 return m_processor
->Transmit(command
);
468 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
471 strLog
.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, m_strDeviceName
.c_str());
472 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
475 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
476 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
477 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
479 return m_processor
->Transmit(command
);
482 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
485 strLog
.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, strMessage
);
486 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
489 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_STRING
);
490 command
.parameters
.push_back((uint8_t)duration
);
492 unsigned int iLen
= strlen(strMessage
);
493 if (iLen
> 13) iLen
= 13;
495 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
496 command
.parameters
.push_back(strMessage
[iPtr
]);
498 return m_processor
->Transmit(command
);
501 bool CCECBusDevice::TransmitPhysicalAddress(void)
504 strLog
.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
505 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
508 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
509 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
510 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
511 command
.parameters
.push_back((uint8_t) (m_type
));
513 return m_processor
->Transmit(command
);
516 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
520 if (dest
== CECDEVICE_UNKNOWN
)
521 dest
= m_iLogicalAddress
;
524 strLog
.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
525 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
528 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_NONE
);
529 CLockObject
lock(&m_mutex
);
531 bReturn
= m_processor
->Transmit(command
);
532 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
536 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
539 strLog
.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
541 switch (m_powerStatus
)
543 case CEC_POWER_STATUS_ON
:
544 strLog
.append("powered on");
546 case CEC_POWER_STATUS_STANDBY
:
547 strLog
.append("in standby mode");
549 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
550 strLog
.append("in transition from on to standby");
552 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
553 strLog
.append("in transition from standby to on");
556 strLog
.append("power state unknown");
559 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
561 strLog
.Format("<< reporting power status '%d'", m_powerStatus
);
562 AddLog(CEC_LOG_NOTICE
, strLog
);
565 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
566 command
.parameters
.push_back((uint8_t) m_powerStatus
);
568 return m_processor
->Transmit(command
);
571 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
)
574 strLog
.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
575 AddLog(CEC_LOG_NOTICE
, strLog
);
577 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);