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_processor(processor
),
47 m_iVendorClass(CEC_VENDOR_UNKNOWN
),
49 m_cecVersion(CEC_VERSION_UNKNOWN
)
51 m_handler
= new CCECCommandHandler(this);
52 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
53 m_menuLanguage
.language
[iPtr
] = '?';
54 m_menuLanguage
.language
[3] = 0;
55 m_menuLanguage
.device
= iLogicalAddress
;
58 CCECBusDevice::~CCECBusDevice(void)
60 m_condition
.Broadcast();
64 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
66 return m_processor
->GetLogicalAddress();
69 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
71 return m_processor
->GetPhysicalAddress();
74 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
76 m_processor
->AddLog(level
, strMessage
);
79 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
81 if (language
.device
== m_iLogicalAddress
)
84 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
85 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
86 m_menuLanguage
= language
;
90 void CCECBusDevice::SetCecVersion(cec_version newVersion
)
93 m_cecVersion
= newVersion
;
98 strLog
.Format("device %d reports CEC version 1.2", m_iLogicalAddress
);
100 case CEC_VERSION_1_2A
:
101 strLog
.Format("device %d reports CEC version 1.2a", m_iLogicalAddress
);
103 case CEC_VERSION_1_3
:
104 strLog
.Format("device %d reports CEC version 1.3", m_iLogicalAddress
);
106 case CEC_VERSION_1_3A
:
107 strLog
.Format("device %d reports CEC version 1.3a", m_iLogicalAddress
);
110 strLog
.Format("device %d reports an unknown CEC version", m_iLogicalAddress
);
111 m_cecVersion
= CEC_VERSION_UNKNOWN
;
114 AddLog(CEC_LOG_DEBUG
, strLog
);
117 void CCECBusDevice::SetVendorId(const cec_datapacket
&data
)
121 AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
125 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
126 ((uint64_t)data
[1] << 2) +
129 SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
132 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
134 m_iVendorId
= iVendorId
;
135 m_iVendorClass
= iVendorClass
;
139 case CEC_VENDOR_SAMSUNG
:
140 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
143 m_handler
= new CANCommandHandler(this);
147 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
150 m_handler
= new CSLCommandHandler(this);
154 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
157 m_handler
= new CCECCommandHandler(this);
163 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
164 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
167 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
169 CLockObject
lock(&m_mutex
);
170 m_iLastActive
= GetTimeMs();
171 m_handler
->HandleCommand(command
);
172 m_condition
.Signal();
176 void CCECBusDevice::PollVendorId(void)
178 CLockObject
lock(&m_mutex
);
179 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
180 m_iVendorId
== CEC_VENDOR_UNKNOWN
&&
181 GetTimeMs() - m_iLastActive
> 5000)
183 m_iLastActive
= GetTimeMs();
186 cec_command::format(command
, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
187 command
.ack_timeout
= 0;
188 m_processor
->Transmit(command
);
192 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
195 strLog
.Format(">> %i changed physical address from %04x to %04x", GetLogicalAddress(), m_iPhysicalAddress
, iNewAddress
);
196 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
198 m_iPhysicalAddress
= iNewAddress
;
201 bool CCECBusDevice::PowerOn(void)
204 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
205 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
208 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
210 return m_processor
->Transmit(command
);
213 bool CCECBusDevice::Standby(void)
216 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
217 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
220 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
222 return m_processor
->Transmit(command
);
225 bool CCECBusDevice::SetOSDString(cec_display_control duration
, const char *strMessage
)
228 strLog
.Format("<< display message '%s'", strMessage
);
229 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
232 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
233 command
.parameters
.push_back((uint8_t)duration
);
235 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
236 command
.parameters
.push_back(strMessage
[iPtr
]);
238 return m_processor
->Transmit(command
);
241 bool CCECBusDevice::ReportCECVersion(void)
243 AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
246 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_CEC_VERSION
);
247 command
.parameters
.push_back(CEC_VERSION_1_3A
);
249 return m_processor
->Transmit(command
);
252 bool CCECBusDevice::ReportDeckStatus(void)
254 // need to support opcodes play and deck control before doing anything with this
255 AddLog(CEC_LOG_NOTICE
, "<< deck status requested, feature abort");
256 m_processor
->TransmitAbort(m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
260 bool CCECBusDevice::ReportMenuState(bool bActive
/* = true */)
263 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
265 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
268 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_MENU_STATUS
);
269 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
271 return m_processor
->Transmit(command
);
274 bool CCECBusDevice::ReportOSDName(void)
276 const char *osdname
= m_processor
->GetDeviceName().c_str();
278 strLog
.Format("<< reporting OSD name as %s", osdname
);
279 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
282 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_NAME
);
283 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
284 command
.parameters
.push_back(osdname
[iPtr
]);
286 return m_processor
->Transmit(command
);
289 bool CCECBusDevice::ReportPowerState(bool bOn
/* = true */)
292 AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
294 AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
297 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_REPORT_POWER_STATUS
);
298 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
300 return m_processor
->Transmit(command
);
303 bool CCECBusDevice::ReportVendorID(void)
305 AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
306 m_processor
->TransmitAbort(m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
310 bool CCECBusDevice::BroadcastActiveView(void)
312 AddLog(CEC_LOG_DEBUG
, "<< setting active view");
315 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
316 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
317 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
319 return m_processor
->Transmit(command
);
322 bool CCECBusDevice::BroadcastInactiveView(void)
324 AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
327 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
328 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
329 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
331 return m_processor
->Transmit(command
);
334 bool CCECBusDevice::BroadcastPhysicalAddress(void)
337 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
338 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
341 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
342 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
343 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
344 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
346 return m_processor
->Transmit(command
);
349 bool CCECBusDevice::BroadcastActiveSource(void)
351 AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
354 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
355 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
356 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
358 return m_processor
->Transmit(command
);
361 cec_version
CCECBusDevice::GetCecVersion(void)
363 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
365 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
367 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
368 CLockObject
lock(&m_mutex
);
369 if (m_processor
->Transmit(command
))
370 m_condition
.Wait(&m_mutex
, 1000);
376 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
378 if (!strcmp(m_menuLanguage
.language
, "???"))
380 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
382 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
383 CLockObject
lock(&m_mutex
);
384 if (m_processor
->Transmit(command
))
385 m_condition
.Wait(&m_mutex
, 1000);
388 return m_menuLanguage
;
391 const char *CCECBusDevice::CECVendorIdToString(const uint64_t iVendorId
)
395 case CEC_VENDOR_SAMSUNG
: