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
),
52 m_iLastCommandSent(0),
54 m_cecVersion(CEC_VERSION_UNKNOWN
)
56 m_handler
= new CCECCommandHandler(this);
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
;
63 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
64 m_type
= CEC_DEVICE_TYPE_RESERVED
;
65 m_strDeviceName
= CCECCommandHandler::ToString(m_iLogicalAddress
);
68 CCECBusDevice::~CCECBusDevice(void)
70 m_condition
.Broadcast();
74 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
76 m_processor
->AddLog(level
, strMessage
);
79 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
81 CLockObject
lock(&m_mutex
);
82 m_iLastActive
= GetTimeMs();
83 m_handler
->HandleCommand(command
);
88 void CCECBusDevice::PollVendorId(void)
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())
97 strLog
.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
98 AddLog(CEC_LOG_NOTICE
, strLog
);
99 m_iLastCommandSent
= GetTimeMs();
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);
108 bool CCECBusDevice::PowerOn(void)
111 strLog
.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
112 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
115 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
117 return m_processor
->Transmit(command
);
120 bool CCECBusDevice::Standby(void)
123 strLog
.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
124 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
127 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
129 return m_processor
->Transmit(command
);
134 cec_version
CCECBusDevice::GetCecVersion(void)
136 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
138 if (!MyLogicalAddressContains(m_iLogicalAddress
))
141 strLog
.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
142 AddLog(CEC_LOG_NOTICE
, strLog
);
144 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
145 CLockObject
lock(&m_mutex
);
146 if (m_processor
->Transmit(command
))
147 m_condition
.Wait(&m_mutex
, 1000);
154 const char* CCECBusDevice::GetLogicalAddressName(void) const
156 return CCECCommandHandler::ToString(m_iLogicalAddress
);
159 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
161 if (!strcmp(m_menuLanguage
.language
, "???"))
163 if (!MyLogicalAddressContains(m_iLogicalAddress
))
166 strLog
.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
167 AddLog(CEC_LOG_NOTICE
, strLog
);
169 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
170 CLockObject
lock(&m_mutex
);
171 if (m_processor
->Transmit(command
))
172 m_condition
.Wait(&m_mutex
, 1000);
176 return m_menuLanguage
;
179 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
181 return m_processor
->GetLogicalAddress();
184 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
186 return m_processor
->GetPhysicalAddress();
189 cec_power_status
CCECBusDevice::GetPowerStatus(void)
191 if (m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
193 if (!MyLogicalAddressContains(m_iLogicalAddress
))
196 strLog
.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
197 AddLog(CEC_LOG_NOTICE
, strLog
);
199 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
200 CLockObject
lock(&m_mutex
);
201 if (m_processor
->Transmit(command
))
202 m_condition
.Wait(&m_mutex
, 1000);
206 return m_powerStatus
;
209 const cec_vendor
&CCECBusDevice::GetVendor(void)
211 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
213 if (!MyLogicalAddressContains(m_iLogicalAddress
))
216 strLog
.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
217 AddLog(CEC_LOG_NOTICE
, strLog
);
219 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
220 CLockObject
lock(&m_mutex
);
222 if (m_processor
->Transmit(command
))
223 m_condition
.Wait(&m_mutex
, 1000);
230 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
232 return m_processor
->HasLogicalAddress(address
);
239 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
242 m_cecVersion
= newVersion
;
246 case CEC_VERSION_1_2
:
247 strLog
.Format("%s (%X): CEC version 1.2", GetLogicalAddressName(), m_iLogicalAddress
);
249 case CEC_VERSION_1_2A
:
250 strLog
.Format("%s (%X): CEC version 1.2a", GetLogicalAddressName(), m_iLogicalAddress
);
252 case CEC_VERSION_1_3
:
253 strLog
.Format("%s (%X): CEC version 1.3", GetLogicalAddressName(), m_iLogicalAddress
);
255 case CEC_VERSION_1_3A
:
256 strLog
.Format("%s (%X): CEC version 1.3a", GetLogicalAddressName(), m_iLogicalAddress
);
259 strLog
.Format("%s (%X): unknown CEC version", GetLogicalAddressName(), m_iLogicalAddress
);
260 m_cecVersion
= CEC_VERSION_UNKNOWN
;
263 AddLog(CEC_LOG_DEBUG
, strLog
);
266 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
268 if (language
.device
== m_iLogicalAddress
)
271 strLog
.Format(">> %s (%X): menu language set to '%s'", GetLogicalAddressName(), m_iLogicalAddress
, language
.language
);
272 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
273 m_menuLanguage
= language
;
277 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
282 strLog
.Format(">> %s (%X): physical address changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
283 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
285 m_iPhysicalAddress
= iNewAddress
;
289 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
294 strLog
.Format(">> %s (%X): stream path changed from %04x to %04x", GetLogicalAddressName(), m_iLogicalAddress
, iOldAddress
, iNewAddress
);
295 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
297 m_iStreamPath
= iNewAddress
;
301 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
303 if (m_powerStatus
!= powerStatus
)
306 strLog
.Format(">> %s (%X): power status changed from %2x to %2x", GetLogicalAddressName(), m_iLogicalAddress
, m_powerStatus
, powerStatus
);
307 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
308 m_powerStatus
= powerStatus
;
312 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
314 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
315 m_iVendorClass
= iVendorClass
;
319 case CEC_VENDOR_SAMSUNG
:
320 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
323 m_handler
= new CANCommandHandler(this);
327 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
330 m_handler
= new CSLCommandHandler(this);
333 case CEC_VENDOR_PANASONIC
:
334 if (m_handler
->GetVendorId() != CEC_VENDOR_PANASONIC
)
337 m_handler
= new CVLCommandHandler(this);
341 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
344 m_handler
= new CCECCommandHandler(this);
350 strLog
.Format("%s (%X): vendor = %s (%06x) class = %2x", GetLogicalAddressName(), m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
351 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
355 /** @name Transmit methods */
357 bool CCECBusDevice::TransmitActiveSource(void)
362 strLog
.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
363 AddLog(CEC_LOG_NOTICE
, strLog
);
366 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
367 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
368 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
370 return m_processor
->Transmit(command
);
375 strLog
.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
376 AddLog(CEC_LOG_DEBUG
, strLog
);
382 bool CCECBusDevice::TransmitActiveView(void)
385 strLog
.Format("<< %s (%X) -> broadcast (F): active view (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
386 AddLog(CEC_LOG_NOTICE
, strLog
);
389 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
390 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
391 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
393 return m_processor
->Transmit(command
);
396 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
399 strLog
.Format("<< %s (%X) -> %s (%X): cec version ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
400 switch (m_cecVersion
)
402 case CEC_VERSION_1_2
:
403 strLog
.append("1.2");
405 case CEC_VERSION_1_2A
:
406 strLog
.append("1.2a");
408 case CEC_VERSION_1_3
:
409 strLog
.append("1.3");
411 case CEC_VERSION_1_3A
:
412 strLog
.append("1.3a");
415 strLog
.append("unknown");
418 AddLog(CEC_LOG_NOTICE
, strLog
);
421 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
422 command
.parameters
.push_back(m_cecVersion
);
424 return m_processor
->Transmit(command
);
427 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest
)
429 // need to support opcodes play and deck control before doing anything with this
431 strLog
.Format("<< %s (%X) -> %s (%X): deck status feature abort", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
432 AddLog(CEC_LOG_NOTICE
, strLog
);
434 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DECK_STATUS
);
438 bool CCECBusDevice::TransmitInactiveView(void)
441 strLog
.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress
);
442 AddLog(CEC_LOG_NOTICE
, strLog
);
445 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
446 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
447 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
449 return m_processor
->Transmit(command
);
452 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
455 strLog
.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
457 strLog
.append("menu active");
459 strLog
.append("menu inactive");
460 AddLog(CEC_LOG_NOTICE
, strLog
);
463 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
464 command
.parameters
.push_back(m_bMenuActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
466 return m_processor
->Transmit(command
);
469 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
472 strLog
.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, m_strDeviceName
.c_str());
473 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
476 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
477 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
478 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
480 return m_processor
->Transmit(command
);
483 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
486 strLog
.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, strMessage
);
487 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
490 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_STRING
);
491 command
.parameters
.push_back((uint8_t)duration
);
493 unsigned int iLen
= strlen(strMessage
);
494 if (iLen
> 13) iLen
= 13;
496 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
497 command
.parameters
.push_back(strMessage
[iPtr
]);
499 return m_processor
->Transmit(command
);
502 bool CCECBusDevice::TransmitPhysicalAddress(void)
505 strLog
.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
506 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
509 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
510 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
511 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
512 command
.parameters
.push_back((uint8_t) (m_type
));
514 return m_processor
->Transmit(command
);
517 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
521 if (dest
== CECDEVICE_UNKNOWN
)
522 dest
= m_iLogicalAddress
;
525 strLog
.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
526 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
529 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_NONE
);
530 CLockObject
lock(&m_mutex
);
532 bReturn
= m_processor
->Transmit(command
);
533 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
537 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
540 strLog
.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
542 switch (m_powerStatus
)
544 case CEC_POWER_STATUS_ON
:
545 strLog
.append("powered on");
547 case CEC_POWER_STATUS_STANDBY
:
548 strLog
.append("in standby mode");
550 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
551 strLog
.append("in transition from on to standby");
553 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
554 strLog
.append("in transition from standby to on");
557 strLog
.append("power state unknown");
560 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
562 strLog
.Format("<< reporting power status '%d'", m_powerStatus
);
563 AddLog(CEC_LOG_NOTICE
, strLog
);
566 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
567 command
.parameters
.push_back((uint8_t) m_powerStatus
);
569 return m_processor
->Transmit(command
);
572 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
)
575 strLog
.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
576 AddLog(CEC_LOG_NOTICE
, strLog
);
578 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);