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
)
120 if (m_powerStatus
!= powerStatus
)
123 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
124 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
125 m_powerStatus
= powerStatus
;
129 void CCECBusDevice::SetVendorId(const cec_datapacket
&data
)
133 AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
137 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
138 ((uint64_t)data
[1] << 2) +
141 SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
144 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
146 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
147 m_iVendorClass
= iVendorClass
;
151 case CEC_VENDOR_SAMSUNG
:
152 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
155 m_handler
= new CANCommandHandler(this);
159 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
162 m_handler
= new CSLCommandHandler(this);
166 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
169 m_handler
= new CCECCommandHandler(this);
175 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
176 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
179 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
181 CLockObject
lock(&m_mutex
);
182 m_iLastActive
= GetTimeMs();
183 m_handler
->HandleCommand(command
);
184 m_condition
.Signal();
188 const cec_vendor
&CCECBusDevice::GetVendor(void)
190 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
192 AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID");
194 cec_command::format(command
, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
195 CLockObject
lock(&m_mutex
);
197 if (m_processor
->Transmit(command
))
198 m_condition
.Wait(&m_mutex
, 1000);
204 void CCECBusDevice::PollVendorId(void)
206 CLockObject
lock(&m_mutex
);
207 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
208 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
209 GetTimeMs() - m_iLastActive
> 5000)
211 m_iLastActive
= GetTimeMs();
214 cec_command::format(command
, GetMyLogicalAddress(), GetLogicalAddress(), CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
215 if (m_processor
->Transmit(command
))
216 m_condition
.Wait(&m_mutex
, 1000);
220 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
225 strLog
.Format(">> %i changed physical address from %04x to %04x", GetLogicalAddress(), m_iPhysicalAddress
, iNewAddress
);
226 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
228 m_iPhysicalAddress
= iNewAddress
;
232 bool CCECBusDevice::PowerOn(void)
235 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
236 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
239 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
241 return m_processor
->Transmit(command
);
244 bool CCECBusDevice::Standby(void)
247 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
248 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
251 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
253 return m_processor
->Transmit(command
);
256 bool CCECBusDevice::SetOSDString(cec_display_control duration
, const char *strMessage
)
259 strLog
.Format("<< display message '%s'", strMessage
);
260 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
263 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
264 command
.parameters
.push_back((uint8_t)duration
);
266 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
267 command
.parameters
.push_back(strMessage
[iPtr
]);
269 return m_processor
->Transmit(command
);
272 bool CCECBusDevice::ReportCECVersion(void)
274 AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
277 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_CEC_VERSION
);
278 command
.parameters
.push_back(CEC_VERSION_1_3A
);
280 return m_processor
->Transmit(command
);
283 bool CCECBusDevice::ReportDeckStatus(void)
285 // need to support opcodes play and deck control before doing anything with this
286 AddLog(CEC_LOG_NOTICE
, "<< deck status requested, feature abort");
287 m_processor
->TransmitAbort(m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
291 bool CCECBusDevice::ReportMenuState(bool bActive
/* = true */)
294 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
296 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
299 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_MENU_STATUS
);
300 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
302 return m_processor
->Transmit(command
);
305 bool CCECBusDevice::ReportOSDName(void)
307 const char *osdname
= m_processor
->GetDeviceName().c_str();
309 strLog
.Format("<< reporting OSD name as %s", osdname
);
310 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
313 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_NAME
);
314 for (unsigned int iPtr
= 0; iPtr
< strlen(osdname
); iPtr
++)
315 command
.parameters
.push_back(osdname
[iPtr
]);
317 return m_processor
->Transmit(command
);
320 bool CCECBusDevice::ReportPowerState(bool bOn
/* = true */)
323 AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
325 AddLog(CEC_LOG_NOTICE
, "<< reporting \"Off\" power status");
328 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_REPORT_POWER_STATUS
);
329 command
.parameters
.push_back(bOn
? (uint8_t) CEC_POWER_STATUS_ON
: (uint8_t) CEC_POWER_STATUS_STANDBY
);
331 return m_processor
->Transmit(command
);
334 bool CCECBusDevice::ReportVendorID(void)
336 AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
337 m_processor
->TransmitAbort(m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
341 bool CCECBusDevice::BroadcastActiveView(void)
343 AddLog(CEC_LOG_DEBUG
, "<< setting active view");
346 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
347 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
348 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
350 return m_processor
->Transmit(command
);
353 bool CCECBusDevice::BroadcastInactiveView(void)
355 AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
358 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
359 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
360 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
362 return m_processor
->Transmit(command
);
365 bool CCECBusDevice::BroadcastPhysicalAddress(void)
368 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
369 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
372 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
373 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
374 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
375 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
377 return m_processor
->Transmit(command
);
380 bool CCECBusDevice::BroadcastActiveSource(void)
382 AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
385 cec_command::format(command
, GetMyLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
386 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
387 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
389 return m_processor
->Transmit(command
);
392 cec_version
CCECBusDevice::GetCecVersion(bool bRefresh
/* = true */)
394 if (bRefresh
|| m_cecVersion
== CEC_VERSION_UNKNOWN
)
396 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
398 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
399 CLockObject
lock(&m_mutex
);
400 if (m_processor
->Transmit(command
))
401 m_condition
.Wait(&m_mutex
, 1000);
407 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bRefresh
/* = true */)
409 if (bRefresh
|| !strcmp(m_menuLanguage
.language
, "???"))
411 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
413 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
414 CLockObject
lock(&m_mutex
);
415 if (m_processor
->Transmit(command
))
416 m_condition
.Wait(&m_mutex
, 1000);
419 return m_menuLanguage
;
422 cec_power_status
CCECBusDevice::GetPowerStatus(bool bRefresh
/* = true */)
424 if (bRefresh
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
426 AddLog(CEC_LOG_NOTICE
, "<< requesting power status");
428 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
429 CLockObject
lock(&m_mutex
);
430 if (m_processor
->Transmit(command
))
431 m_condition
.Wait(&m_mutex
, 1000);
434 return m_powerStatus
;
437 bool CCECBusDevice::PollDevice(void)
442 strLog
.Format("<< sending POLL from device %1x to device %1x", (int8_t)GetMyLogicalAddress(), m_iLogicalAddress
);
443 AddLog(CEC_LOG_DEBUG
, strLog
);
446 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_NONE
);
447 CLockObject
lock(&m_mutex
);
449 bReturn
= m_processor
->Transmit(command
);
450 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");