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)
110 cec_power_status current
= GetPowerStatus();
111 if (current
!= CEC_POWER_STATUS_ON
&&
112 current
!= CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
)
115 strLog
.Format("<< powering on '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
116 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
118 SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
121 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
123 return m_processor
->Transmit(command
);
129 bool CCECBusDevice::Standby(void)
132 strLog
.Format("<< putting '%s' (%X) in standby mode", GetLogicalAddressName(), m_iLogicalAddress
);
133 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
136 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
138 return m_processor
->Transmit(command
);
143 cec_version
CCECBusDevice::GetCecVersion(void)
145 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
147 if (!MyLogicalAddressContains(m_iLogicalAddress
))
150 strLog
.Format("<< requesting CEC version of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
151 AddLog(CEC_LOG_NOTICE
, strLog
);
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);
163 const char* CCECBusDevice::GetLogicalAddressName(void) const
165 return CCECCommandHandler::ToString(m_iLogicalAddress
);
168 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
170 if (!strcmp(m_menuLanguage
.language
, "???"))
172 if (!MyLogicalAddressContains(m_iLogicalAddress
))
175 strLog
.Format("<< requesting menu language of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
176 AddLog(CEC_LOG_NOTICE
, strLog
);
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);
185 return m_menuLanguage
;
188 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
190 return m_processor
->GetLogicalAddress();
193 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
195 return m_processor
->GetPhysicalAddress();
198 cec_power_status
CCECBusDevice::GetPowerStatus(void)
200 if (m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
202 if (!MyLogicalAddressContains(m_iLogicalAddress
))
205 strLog
.Format("<< requesting power status of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
206 AddLog(CEC_LOG_NOTICE
, strLog
);
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);
215 return m_powerStatus
;
218 const cec_vendor
&CCECBusDevice::GetVendor(void)
220 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
222 if (!MyLogicalAddressContains(m_iLogicalAddress
))
225 strLog
.Format("<< requesting vendor ID of '%s' (%X)", GetLogicalAddressName(), m_iLogicalAddress
);
226 AddLog(CEC_LOG_NOTICE
, strLog
);
228 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
229 CLockObject
lock(&m_mutex
);
231 if (m_processor
->Transmit(command
))
232 m_condition
.Wait(&m_mutex
, 1000);
239 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
241 return m_processor
->HasLogicalAddress(address
);
248 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
251 m_cecVersion
= newVersion
;
255 case CEC_VERSION_1_2
:
256 strLog
.Format("%s (%X): CEC version 1.2", GetLogicalAddressName(), m_iLogicalAddress
);
258 case CEC_VERSION_1_2A
:
259 strLog
.Format("%s (%X): CEC version 1.2a", GetLogicalAddressName(), m_iLogicalAddress
);
261 case CEC_VERSION_1_3
:
262 strLog
.Format("%s (%X): CEC version 1.3", GetLogicalAddressName(), m_iLogicalAddress
);
264 case CEC_VERSION_1_3A
:
265 strLog
.Format("%s (%X): CEC version 1.3a", GetLogicalAddressName(), m_iLogicalAddress
);
268 strLog
.Format("%s (%X): unknown CEC version", GetLogicalAddressName(), m_iLogicalAddress
);
269 m_cecVersion
= CEC_VERSION_UNKNOWN
;
272 AddLog(CEC_LOG_DEBUG
, strLog
);
275 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
277 if (language
.device
== m_iLogicalAddress
)
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
;
286 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
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());
294 m_iPhysicalAddress
= iNewAddress
;
298 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
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());
306 m_iStreamPath
= iNewAddress
;
309 SetPowerStatus(CEC_POWER_STATUS_ON
);
313 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
315 if (m_powerStatus
!= powerStatus
)
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
;
324 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
326 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
327 m_iVendorClass
= iVendorClass
;
331 case CEC_VENDOR_SAMSUNG
:
332 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
335 m_handler
= new CANCommandHandler(this);
339 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
342 m_handler
= new CSLCommandHandler(this);
345 case CEC_VENDOR_PANASONIC
:
346 if (m_handler
->GetVendorId() != CEC_VENDOR_PANASONIC
)
349 m_handler
= new CVLCommandHandler(this);
353 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
356 m_handler
= new CCECCommandHandler(this);
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());
367 /** @name Transmit methods */
369 bool CCECBusDevice::TransmitActiveSource(void)
374 strLog
.Format("<< %s (%X) -> broadcast (F): active source (%4x)", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
375 AddLog(CEC_LOG_NOTICE
, strLog
);
378 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
379 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
380 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
382 return m_processor
->Transmit(command
);
387 strLog
.Format("<< %s (%X) is not the active source", GetLogicalAddressName(), m_iLogicalAddress
);
388 AddLog(CEC_LOG_DEBUG
, strLog
);
394 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
397 strLog
.Format("<< %s (%X) -> %s (%X): cec version ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
398 switch (m_cecVersion
)
400 case CEC_VERSION_1_2
:
401 strLog
.append("1.2");
403 case CEC_VERSION_1_2A
:
404 strLog
.append("1.2a");
406 case CEC_VERSION_1_3
:
407 strLog
.append("1.3");
409 case CEC_VERSION_1_3A
:
410 strLog
.append("1.3a");
413 strLog
.append("unknown");
416 AddLog(CEC_LOG_NOTICE
, strLog
);
419 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
420 command
.parameters
.push_back(m_cecVersion
);
422 return m_processor
->Transmit(command
);
425 bool CCECBusDevice::TransmitInactiveView(void)
428 strLog
.Format("<< %s (%X) -> broadcast (F): inactive view", GetLogicalAddressName(), m_iLogicalAddress
);
429 AddLog(CEC_LOG_NOTICE
, strLog
);
432 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
433 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
434 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
436 return m_processor
->Transmit(command
);
439 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
442 strLog
.Format("<< %s (%X) -> %s (%X): ", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
444 strLog
.append("menu active");
446 strLog
.append("menu inactive");
447 AddLog(CEC_LOG_NOTICE
, strLog
);
450 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
451 command
.parameters
.push_back(m_bMenuActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
453 return m_processor
->Transmit(command
);
456 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
459 strLog
.Format("<< %s (%X) -> %s (%X): OSD name '%s'", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, m_strDeviceName
.c_str());
460 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
463 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
464 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
465 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
467 return m_processor
->Transmit(command
);
470 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
473 strLog
.Format("<< %s (%X) -> %s (%X): display OSD message '%s'", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, strMessage
);
474 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
477 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_STRING
);
478 command
.parameters
.push_back((uint8_t)duration
);
480 unsigned int iLen
= strlen(strMessage
);
481 if (iLen
> 13) iLen
= 13;
483 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
484 command
.parameters
.push_back(strMessage
[iPtr
]);
486 return m_processor
->Transmit(command
);
489 bool CCECBusDevice::TransmitPhysicalAddress(void)
492 strLog
.Format("<< %s (%X) -> broadcast (F): physical adddress %4x", GetLogicalAddressName(), m_iLogicalAddress
, m_iPhysicalAddress
);
493 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
496 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
497 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
498 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
499 command
.parameters
.push_back((uint8_t) (m_type
));
501 return m_processor
->Transmit(command
);
504 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
508 if (dest
== CECDEVICE_UNKNOWN
)
509 dest
= m_iLogicalAddress
;
512 strLog
.Format("<< %s (%X) -> %s (%X): POLL", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
513 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
516 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_NONE
);
517 CLockObject
lock(&m_mutex
);
519 bReturn
= m_processor
->Transmit(command
);
520 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
524 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
527 strLog
.Format("<< %s (%X) -> %s (%X): %s", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
, CCECCommandHandler::ToString(m_powerStatus
));
528 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
531 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
532 command
.parameters
.push_back((uint8_t) m_powerStatus
);
534 return m_processor
->Transmit(command
);
537 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
)
540 strLog
.Format("<< %s (%X) -> %s (%X): vendor id feature abort", GetLogicalAddressName(), m_iLogicalAddress
, CCECCommandHandler::ToString(dest
), dest
);
541 AddLog(CEC_LOG_NOTICE
, strLog
);
543 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);