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
),
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
;
56 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
59 CCECBusDevice::~CCECBusDevice(void)
61 m_condition
.Broadcast();
65 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
67 return m_processor
->GetLogicalAddress();
70 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
72 return m_processor
->GetPhysicalAddress();
75 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
77 m_processor
->AddLog(level
, strMessage
);
80 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
82 if (language
.device
== m_iLogicalAddress
)
85 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
86 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
87 m_menuLanguage
= language
;
91 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
94 m_cecVersion
= newVersion
;
99 strLog
.Format("device %d reports CEC version 1.2", m_iLogicalAddress
);
101 case CEC_VERSION_1_2A
:
102 strLog
.Format("device %d reports CEC version 1.2a", m_iLogicalAddress
);
104 case CEC_VERSION_1_3
:
105 strLog
.Format("device %d reports CEC version 1.3", m_iLogicalAddress
);
107 case CEC_VERSION_1_3A
:
108 strLog
.Format("device %d reports CEC version 1.3a", m_iLogicalAddress
);
111 strLog
.Format("device %d reports an unknown CEC version", m_iLogicalAddress
);
112 m_cecVersion
= CEC_VERSION_UNKNOWN
;
115 AddLog(CEC_LOG_DEBUG
, strLog
);
118 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
121 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
122 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
123 m_powerStatus
= powerStatus
;
126 void CCECBusDevice::SetVendorId(const cec_datapacket
&data
)
130 AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
134 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
135 ((uint64_t)data
[1] << 2) +
138 SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
141 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
143 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
144 m_iVendorClass
= iVendorClass
;
148 case CEC_VENDOR_SAMSUNG
:
149 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
152 m_handler
= new CANCommandHandler(this);
156 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
159 m_handler
= new CSLCommandHandler(this);
163 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
166 m_handler
= new CCECCommandHandler(this);
172 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
173 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
176 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
178 CLockObject
lock(&m_mutex
);
179 m_iLastActive
= GetTimeMs();
180 m_handler
->HandleCommand(command
);
181 m_condition
.Signal();
185 const cec_vendor
&CCECBusDevice::GetVendor(void)
187 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
189 AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID");
191 cec_command::format(command
, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
192 CLockObject
lock(&m_mutex
);
194 if (m_processor
->Transmit(command
))
195 m_condition
.Wait(&m_mutex
, 1000);
201 void CCECBusDevice::PollVendorId(void)
203 CLockObject
lock(&m_mutex
);
204 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
205 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
206 GetTimeMs() - m_iLastActive
> 5000)
208 m_iLastActive
= GetTimeMs();
211 cec_command::format(command
, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
212 command
.ack_timeout
= 0;
213 m_processor
->Transmit(command
);
217 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
222 strLog
.Format(">> %i changed physical address from %04x to %04x", GetLogicalAddress(), m_iPhysicalAddress
, iNewAddress
);
223 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
225 m_iPhysicalAddress
= iNewAddress
;
229 bool CCECBusDevice::PowerOn(void)
232 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
233 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
236 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
238 return m_processor
->Transmit(command
);
241 bool CCECBusDevice::Standby(void)
244 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
245 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
248 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
250 return m_processor
->Transmit(command
);
253 bool CCECBusDevice::SetOSDString(cec_display_control duration
, const char *strMessage
)
256 strLog
.Format("<< display message '%s'", strMessage
);
257 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
260 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
261 command
.parameters
.push_back((uint8_t)duration
);
263 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
264 command
.parameters
.push_back(strMessage
[iPtr
]);
266 return m_processor
->Transmit(command
);
269 bool CCECBusDevice::ReportCECVersion(void)
271 AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
274 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_CEC_VERSION
);
275 command
.parameters
.push_back(CEC_VERSION_1_3A
);
277 return m_processor
->Transmit(command
);
280 bool CCECBusDevice::ReportDeckStatus(void)
282 // need to support opcodes play and deck control before doing anything with this
283 AddLog(CEC_LOG_NOTICE
, "<< deck status requested, feature abort");
284 m_processor
->TransmitAbort(m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
288 bool CCECBusDevice::ReportMenuState(bool bActive
/* = true */)
291 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
293 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
296 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_MENU_STATUS
);
297 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
299 return m_processor
->Transmit(command
);
302 bool CCECBusDevice::ReportOSDName(void)
304 const char *osdname
= m_processor
->GetDeviceName().c_str();
306 strLog
.Format("<< reporting OSD name as %s", osdname
);
307 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
310 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_NAME
);
311 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
312 command
.parameters
.push_back(osdname
[iPtr
]);
314 return m_processor
->Transmit(command
);
317 bool CCECBusDevice::ReportPowerState(bool bOn
/* = true */)
320 AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
322 AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
325 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_REPORT_POWER_STATUS
);
326 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
328 return m_processor
->Transmit(command
);
331 bool CCECBusDevice::ReportVendorID(void)
333 AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
334 m_processor
->TransmitAbort(m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
338 bool CCECBusDevice::BroadcastActiveView(void)
340 AddLog(CEC_LOG_DEBUG
, "<< setting active view");
343 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
344 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
345 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
347 return m_processor
->Transmit(command
);
350 bool CCECBusDevice::BroadcastInactiveView(void)
352 AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
355 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
356 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
357 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
359 return m_processor
->Transmit(command
);
362 bool CCECBusDevice::BroadcastPhysicalAddress(void)
365 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
366 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
369 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
370 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
371 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
372 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
374 return m_processor
->Transmit(command
);
377 bool CCECBusDevice::BroadcastActiveSource(void)
379 AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
382 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
383 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
384 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
386 return m_processor
->Transmit(command
);
389 cec_version
CCECBusDevice::GetCecVersion(void)
391 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
393 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
395 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
396 CLockObject
lock(&m_mutex
);
397 if (m_processor
->Transmit(command
))
398 m_condition
.Wait(&m_mutex
, 1000);
404 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
406 if (!strcmp(m_menuLanguage
.language
, "???"))
408 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
410 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
411 CLockObject
lock(&m_mutex
);
412 if (m_processor
->Transmit(command
))
413 m_condition
.Wait(&m_mutex
, 1000);
416 return m_menuLanguage
;
419 cec_power_status
CCECBusDevice::GetPowerStatus(void)
421 if (m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
423 AddLog(CEC_LOG_NOTICE
, "<< requesting power status");
425 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
426 CLockObject
lock(&m_mutex
);
427 if (m_processor
->Transmit(command
))
428 m_condition
.Wait(&m_mutex
, 1000);
431 return m_powerStatus
;