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_1_3A
)
51 m_handler
= new CCECCommandHandler(this);
53 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
54 m_menuLanguage
.language
[iPtr
] = '?';
55 m_menuLanguage
.language
[3] = 0;
56 m_menuLanguage
.device
= iLogicalAddress
;
58 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
59 m_type
= CEC_DEVICE_TYPE_RESERVED
;
60 m_strDeviceName
= "Unknown";
63 CCECBusDevice::~CCECBusDevice(void)
65 m_condition
.Broadcast();
69 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
71 return m_processor
->GetLogicalAddress();
74 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
76 return m_processor
->HasLogicalAddress(address
);
79 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
81 return m_processor
->GetPhysicalAddress();
84 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
86 m_processor
->AddLog(level
, strMessage
);
89 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
91 if (language
.device
== m_iLogicalAddress
)
94 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
95 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
96 m_menuLanguage
= language
;
100 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
103 m_cecVersion
= newVersion
;
107 case CEC_VERSION_1_2
:
108 strLog
.Format("device %d reports CEC version 1.2", m_iLogicalAddress
);
110 case CEC_VERSION_1_2A
:
111 strLog
.Format("device %d reports CEC version 1.2a", m_iLogicalAddress
);
113 case CEC_VERSION_1_3
:
114 strLog
.Format("device %d reports CEC version 1.3", m_iLogicalAddress
);
116 case CEC_VERSION_1_3A
:
117 strLog
.Format("device %d reports CEC version 1.3a", m_iLogicalAddress
);
120 strLog
.Format("device %d reports an unknown CEC version", m_iLogicalAddress
);
121 m_cecVersion
= CEC_VERSION_UNKNOWN
;
124 AddLog(CEC_LOG_DEBUG
, strLog
);
127 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
129 if (m_powerStatus
!= powerStatus
)
132 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
133 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
134 m_powerStatus
= powerStatus
;
138 void CCECBusDevice::SetVendorId(const cec_datapacket
&data
)
142 AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
146 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
147 ((uint64_t)data
[1] << 2) +
150 SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
153 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
155 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
156 m_iVendorClass
= iVendorClass
;
160 case CEC_VENDOR_SAMSUNG
:
161 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
164 m_handler
= new CANCommandHandler(this);
168 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
171 m_handler
= new CSLCommandHandler(this);
175 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
178 m_handler
= new CCECCommandHandler(this);
184 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
185 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
188 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
190 CLockObject
lock(&m_mutex
);
191 m_iLastActive
= GetTimeMs();
192 m_handler
->HandleCommand(command
);
193 m_condition
.Signal();
197 const cec_vendor
&CCECBusDevice::GetVendor(void)
199 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
201 AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID");
203 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
204 CLockObject
lock(&m_mutex
);
206 if (m_processor
->Transmit(command
))
207 m_condition
.Wait(&m_mutex
, 1000);
213 void CCECBusDevice::PollVendorId(void)
215 CLockObject
lock(&m_mutex
);
216 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
217 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
218 GetTimeMs() - m_iLastActive
> 5000)
220 m_iLastActive
= GetTimeMs();
223 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
224 if (m_processor
->Transmit(command
))
225 m_condition
.Wait(&m_mutex
, 1000);
229 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
234 strLog
.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
235 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
237 m_iPhysicalAddress
= iNewAddress
;
241 bool CCECBusDevice::PowerOn(void)
244 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
245 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
248 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
250 return m_processor
->Transmit(command
);
253 bool CCECBusDevice::Standby(void)
256 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
257 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
260 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
262 return m_processor
->Transmit(command
);
265 bool CCECBusDevice::SetOSDString(cec_display_control duration
, const char *strMessage
)
268 strLog
.Format("<< display message '%s'", strMessage
);
269 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
272 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
273 command
.parameters
.push_back((uint8_t)duration
);
275 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
276 command
.parameters
.push_back(strMessage
[iPtr
]);
278 return m_processor
->Transmit(command
);
281 bool CCECBusDevice::ReportCECVersion(cec_logical_address dest
)
283 AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
286 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
287 command
.parameters
.push_back(m_cecVersion
);
289 return m_processor
->Transmit(command
);
292 bool CCECBusDevice::ReportDeckStatus(cec_logical_address dest
)
294 // need to support opcodes play and deck control before doing anything with this
295 AddLog(CEC_LOG_NOTICE
, "<< deck status requested, feature abort");
296 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
300 bool CCECBusDevice::ReportMenuState(cec_logical_address dest
, bool bActive
/* = true */)
303 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
305 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
308 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
309 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
311 return m_processor
->Transmit(command
);
314 bool CCECBusDevice::ReportOSDName(cec_logical_address dest
)
317 strLog
.Format("<< reporting OSD name as %s", m_strDeviceName
.c_str());
318 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
321 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
322 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
323 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
325 return m_processor
->Transmit(command
);
328 bool CCECBusDevice::ReportPowerState(cec_logical_address dest
)
330 AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
333 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
334 // command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
335 command
.parameters
.push_back((uint8_t) CEC_POWER_STATUS_ON
);
337 return m_processor
->Transmit(command
);
340 bool CCECBusDevice::ReportVendorID(cec_logical_address dest
)
342 AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
343 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
347 bool CCECBusDevice::BroadcastActiveView(void)
349 AddLog(CEC_LOG_DEBUG
, "<< setting active view");
352 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
353 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
354 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
356 return m_processor
->Transmit(command
);
359 bool CCECBusDevice::BroadcastInactiveView(void)
361 AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
364 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
365 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
366 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
368 return m_processor
->Transmit(command
);
371 bool CCECBusDevice::BroadcastPhysicalAddress(void)
374 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
375 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
378 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
379 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
380 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
381 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
383 return m_processor
->Transmit(command
);
386 bool CCECBusDevice::BroadcastActiveSource(void)
388 AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
391 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
392 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
393 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
395 return m_processor
->Transmit(command
);
398 cec_version
CCECBusDevice::GetCecVersion(bool bRefresh
/* = true */)
400 if (bRefresh
|| m_cecVersion
== CEC_VERSION_UNKNOWN
)
402 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
404 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
405 CLockObject
lock(&m_mutex
);
406 if (m_processor
->Transmit(command
))
407 m_condition
.Wait(&m_mutex
, 1000);
413 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bRefresh
/* = true */)
415 if (bRefresh
|| !strcmp(m_menuLanguage
.language
, "???"))
417 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
419 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
420 CLockObject
lock(&m_mutex
);
421 if (m_processor
->Transmit(command
))
422 m_condition
.Wait(&m_mutex
, 1000);
425 return m_menuLanguage
;
428 cec_power_status
CCECBusDevice::GetPowerStatus(bool bRefresh
/* = true */)
430 if (bRefresh
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
432 AddLog(CEC_LOG_NOTICE
, "<< requesting power status");
434 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
435 CLockObject
lock(&m_mutex
);
436 if (m_processor
->Transmit(command
))
437 m_condition
.Wait(&m_mutex
, 1000);
440 return m_powerStatus
;
443 bool CCECBusDevice::PollDevice(cec_logical_address source
/* = CECDEVICE_UNKNOWN */)
447 if (source
== CECDEVICE_UNKNOWN
)
448 source
= GetMyLogicalAddress();
451 strLog
.Format("<< sending POLL from device %1x to device %1x", (int8_t)source
, m_iLogicalAddress
);
452 AddLog(CEC_LOG_DEBUG
, strLog
);
455 cec_command::format(command
, source
, m_iLogicalAddress
, CEC_OPCODE_NONE
);
456 CLockObject
lock(&m_mutex
);
458 bReturn
= m_processor
->Transmit(command
);
459 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");