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 "../platform/timeutils.h"
42 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
) :
43 m_iPhysicalAddress(iPhysicalAddress
),
44 m_iLogicalAddress(iLogicalAddress
),
45 m_powerStatus(CEC_POWER_STATUS_UNKNOWN
),
46 m_processor(processor
),
48 m_iVendorClass(CEC_VENDOR_UNKNOWN
),
50 m_cecVersion(CEC_VERSION_1_3A
)
52 m_handler
= new CCECCommandHandler(this);
54 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
55 m_menuLanguage
.language
[iPtr
] = '?';
56 m_menuLanguage
.language
[3] = 0;
57 m_menuLanguage
.device
= iLogicalAddress
;
59 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
60 m_type
= CEC_DEVICE_TYPE_RESERVED
;
61 m_strDeviceName
= "Unknown";
64 CCECBusDevice::~CCECBusDevice(void)
66 m_condition
.Broadcast();
70 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
72 m_processor
->AddLog(level
, strMessage
);
75 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
77 CLockObject
lock(&m_mutex
);
78 m_iLastActive
= GetTimeMs();
79 m_handler
->HandleCommand(command
);
84 void CCECBusDevice::PollVendorId(void)
86 CLockObject
lock(&m_mutex
);
87 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
88 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
89 GetTimeMs() - m_iLastActive
> 5000)
91 m_iLastActive
= GetTimeMs();
94 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
95 if (m_processor
->Transmit(command
))
96 m_condition
.Wait(&m_mutex
, 1000);
100 bool CCECBusDevice::PowerOn(void)
103 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
104 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
107 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
109 return m_processor
->Transmit(command
);
112 bool CCECBusDevice::Standby(void)
115 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
116 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
119 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
121 return m_processor
->Transmit(command
);
126 cec_version
CCECBusDevice::GetCecVersion(bool bRefresh
/* = true */)
128 if (bRefresh
|| m_cecVersion
== CEC_VERSION_UNKNOWN
)
130 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
132 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
133 CLockObject
lock(&m_mutex
);
134 if (m_processor
->Transmit(command
))
135 m_condition
.Wait(&m_mutex
, 1000);
141 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bRefresh
/* = true */)
143 if (bRefresh
|| !strcmp(m_menuLanguage
.language
, "???"))
145 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
147 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
148 CLockObject
lock(&m_mutex
);
149 if (m_processor
->Transmit(command
))
150 m_condition
.Wait(&m_mutex
, 1000);
153 return m_menuLanguage
;
156 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
158 return m_processor
->GetLogicalAddress();
161 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
163 return m_processor
->GetPhysicalAddress();
166 cec_power_status
CCECBusDevice::GetPowerStatus(bool bRefresh
/* = true */)
168 if (bRefresh
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
170 AddLog(CEC_LOG_NOTICE
, "<< requesting power status");
172 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
173 CLockObject
lock(&m_mutex
);
174 if (m_processor
->Transmit(command
))
175 m_condition
.Wait(&m_mutex
, 1000);
178 return m_powerStatus
;
181 const cec_vendor
&CCECBusDevice::GetVendor(void)
183 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
185 AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID");
187 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
188 CLockObject
lock(&m_mutex
);
190 if (m_processor
->Transmit(command
))
191 m_condition
.Wait(&m_mutex
, 1000);
197 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
199 return m_processor
->HasLogicalAddress(address
);
206 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
209 m_cecVersion
= newVersion
;
213 case CEC_VERSION_1_2
:
214 strLog
.Format("device %d reports CEC version 1.2", m_iLogicalAddress
);
216 case CEC_VERSION_1_2A
:
217 strLog
.Format("device %d reports CEC version 1.2a", m_iLogicalAddress
);
219 case CEC_VERSION_1_3
:
220 strLog
.Format("device %d reports CEC version 1.3", m_iLogicalAddress
);
222 case CEC_VERSION_1_3A
:
223 strLog
.Format("device %d reports CEC version 1.3a", m_iLogicalAddress
);
226 strLog
.Format("device %d reports an unknown CEC version", m_iLogicalAddress
);
227 m_cecVersion
= CEC_VERSION_UNKNOWN
;
230 AddLog(CEC_LOG_DEBUG
, strLog
);
233 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
235 if (language
.device
== m_iLogicalAddress
)
238 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
239 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
240 m_menuLanguage
= language
;
244 bool CCECBusDevice::SetOSDString(cec_display_control duration
, const char *strMessage
)
247 strLog
.Format("<< display message '%s'", strMessage
);
248 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
251 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
252 command
.parameters
.push_back((uint8_t)duration
);
254 unsigned int iLen
= strlen(strMessage
);
255 if (iLen
> 13) iLen
= 13;
257 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
258 command
.parameters
.push_back(strMessage
[iPtr
]);
260 return m_processor
->Transmit(command
);
263 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
268 strLog
.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
269 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
271 m_iPhysicalAddress
= iNewAddress
;
275 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
277 if (m_powerStatus
!= powerStatus
)
280 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
281 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
282 m_powerStatus
= powerStatus
;
286 void CCECBusDevice::SetVendorId(const cec_datapacket
&data
)
290 AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
294 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
295 ((uint64_t)data
[1] << 2) +
298 SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
301 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
303 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
304 m_iVendorClass
= iVendorClass
;
308 case CEC_VENDOR_SAMSUNG
:
309 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
312 m_handler
= new CANCommandHandler(this);
316 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
319 m_handler
= new CSLCommandHandler(this);
323 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
326 m_handler
= new CCECCommandHandler(this);
332 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
333 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
337 /** @name Transmit methods */
339 bool CCECBusDevice::TransmitActiveSource(void)
341 AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
344 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
345 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
346 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
348 return m_processor
->Transmit(command
);
351 bool CCECBusDevice::TransmitActiveView(void)
353 AddLog(CEC_LOG_DEBUG
, "<< setting active view");
356 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
357 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
358 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
360 return m_processor
->Transmit(command
);
363 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
365 AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
368 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
369 command
.parameters
.push_back(m_cecVersion
);
371 return m_processor
->Transmit(command
);
374 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest
)
376 // need to support opcodes play and deck control before doing anything with this
377 AddLog(CEC_LOG_NOTICE
, "<< deck status requested, feature abort");
378 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
382 bool CCECBusDevice::TransmitInactiveView(void)
384 AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
387 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
388 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
389 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
391 return m_processor
->Transmit(command
);
394 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
397 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
399 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
402 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
403 command
.parameters
.push_back(m_bMenuActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
405 return m_processor
->Transmit(command
);
408 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
411 strLog
.Format("<< reporting OSD name as %s", m_strDeviceName
.c_str());
412 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
415 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
416 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
417 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
419 return m_processor
->Transmit(command
);
422 bool CCECBusDevice::TransmitPhysicalAddress(void)
425 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
426 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
429 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
430 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
431 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
432 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
434 return m_processor
->Transmit(command
);
437 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
441 if (dest
== CECDEVICE_UNKNOWN
)
442 dest
= m_iLogicalAddress
;
445 strLog
.Format("<< sending POLL from device %1x to device %1x", m_iLogicalAddress
, (int8_t)dest
);
446 AddLog(CEC_LOG_DEBUG
, strLog
);
449 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_NONE
);
450 CLockObject
lock(&m_mutex
);
452 bReturn
= m_processor
->Transmit(command
);
453 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
457 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
460 strLog
.Format("<< reporting power status '%d'", m_powerStatus
);
461 AddLog(CEC_LOG_NOTICE
, strLog
);
464 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
465 command
.parameters
.push_back((uint8_t) m_powerStatus
);
467 return m_processor
->Transmit(command
);
470 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
)
472 AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
473 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);