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 "../implementations/VLCommandHandler.h"
39 #include "../platform/timeutils.h"
43 CCECBusDevice::CCECBusDevice(CCECProcessor
*processor
, cec_logical_address iLogicalAddress
, uint16_t iPhysicalAddress
) :
44 m_iPhysicalAddress(iPhysicalAddress
),
46 m_iLogicalAddress(iLogicalAddress
),
47 m_powerStatus(CEC_POWER_STATUS_UNKNOWN
),
48 m_processor(processor
),
50 m_iVendorClass(CEC_VENDOR_UNKNOWN
),
52 m_cecVersion(CEC_VERSION_1_3A
)
54 m_handler
= new CCECCommandHandler(this);
56 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
57 m_menuLanguage
.language
[iPtr
] = '?';
58 m_menuLanguage
.language
[3] = 0;
59 m_menuLanguage
.device
= iLogicalAddress
;
61 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
62 m_type
= CEC_DEVICE_TYPE_RESERVED
;
63 m_strDeviceName
= "Unknown";
66 CCECBusDevice::~CCECBusDevice(void)
68 m_condition
.Broadcast();
72 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
74 m_processor
->AddLog(level
, strMessage
);
77 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
79 CLockObject
lock(&m_mutex
);
80 m_iLastActive
= GetTimeMs();
81 m_handler
->HandleCommand(command
);
86 void CCECBusDevice::PollVendorId(void)
88 CLockObject
lock(&m_mutex
);
89 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
90 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
91 GetTimeMs() - m_iLastActive
> 5000 &&
92 !m_processor
->IsMonitoring())
94 m_iLastActive
= GetTimeMs();
97 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
98 if (m_processor
->Transmit(command
))
99 m_condition
.Wait(&m_mutex
, 1000);
103 bool CCECBusDevice::PowerOn(void)
106 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
107 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
110 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
112 return m_processor
->Transmit(command
);
115 bool CCECBusDevice::Standby(void)
118 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
119 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
122 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
124 return m_processor
->Transmit(command
);
129 cec_version
CCECBusDevice::GetCecVersion(void)
131 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
133 if (!MyLogicalAddressContains(m_iLogicalAddress
))
135 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
137 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
138 CLockObject
lock(&m_mutex
);
139 if (m_processor
->Transmit(command
))
140 m_condition
.Wait(&m_mutex
, 1000);
147 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
149 if (!strcmp(m_menuLanguage
.language
, "???"))
151 if (!MyLogicalAddressContains(m_iLogicalAddress
))
153 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
155 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
156 CLockObject
lock(&m_mutex
);
157 if (m_processor
->Transmit(command
))
158 m_condition
.Wait(&m_mutex
, 1000);
162 return m_menuLanguage
;
165 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
167 return m_processor
->GetLogicalAddress();
170 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
172 return m_processor
->GetPhysicalAddress();
175 cec_power_status
CCECBusDevice::GetPowerStatus(void)
177 if (m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
179 if (!MyLogicalAddressContains(m_iLogicalAddress
))
181 AddLog(CEC_LOG_NOTICE
, "<< requesting power status");
183 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
184 CLockObject
lock(&m_mutex
);
185 if (m_processor
->Transmit(command
))
186 m_condition
.Wait(&m_mutex
, 1000);
190 return m_powerStatus
;
193 const cec_vendor
&CCECBusDevice::GetVendor(void)
195 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
197 if (!MyLogicalAddressContains(m_iLogicalAddress
))
199 AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID");
201 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
202 CLockObject
lock(&m_mutex
);
204 if (m_processor
->Transmit(command
))
205 m_condition
.Wait(&m_mutex
, 1000);
212 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
214 return m_processor
->HasLogicalAddress(address
);
221 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
224 m_cecVersion
= newVersion
;
228 case CEC_VERSION_1_2
:
229 strLog
.Format("device %d reports CEC version 1.2", m_iLogicalAddress
);
231 case CEC_VERSION_1_2A
:
232 strLog
.Format("device %d reports CEC version 1.2a", m_iLogicalAddress
);
234 case CEC_VERSION_1_3
:
235 strLog
.Format("device %d reports CEC version 1.3", m_iLogicalAddress
);
237 case CEC_VERSION_1_3A
:
238 strLog
.Format("device %d reports CEC version 1.3a", m_iLogicalAddress
);
241 strLog
.Format("device %d reports an unknown CEC version", m_iLogicalAddress
);
242 m_cecVersion
= CEC_VERSION_UNKNOWN
;
245 AddLog(CEC_LOG_DEBUG
, strLog
);
248 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
250 if (language
.device
== m_iLogicalAddress
)
253 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
254 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
255 m_menuLanguage
= language
;
259 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
264 strLog
.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
265 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
267 m_iPhysicalAddress
= iNewAddress
;
271 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
276 strLog
.Format(">> %i stream path from %04x to %04x", m_iLogicalAddress
, iOldAddress
, iNewAddress
);
277 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
279 m_iStreamPath
= iNewAddress
;
283 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
285 if (m_powerStatus
!= powerStatus
)
288 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
289 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
290 m_powerStatus
= powerStatus
;
294 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
296 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
297 m_iVendorClass
= iVendorClass
;
301 case CEC_VENDOR_SAMSUNG
:
302 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
305 m_handler
= new CANCommandHandler(this);
309 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
312 m_handler
= new CSLCommandHandler(this);
315 case CEC_VENDOR_PANASONIC
:
316 if (m_handler
->GetVendorId() != CEC_VENDOR_PANASONIC
)
319 m_handler
= new CVLCommandHandler(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)
342 strLog
.Format("<< %x -> broadcast: active source (%4x)", m_iLogicalAddress
, m_iPhysicalAddress
);
343 AddLog(CEC_LOG_NOTICE
, strLog
);
346 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
347 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
348 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
350 return m_processor
->Transmit(command
);
353 bool CCECBusDevice::TransmitActiveView(void)
356 strLog
.Format("<< %x -> broadcast: active view (%4x)", m_iLogicalAddress
, m_iPhysicalAddress
);
357 AddLog(CEC_LOG_NOTICE
, strLog
);
360 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
361 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
362 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
364 return m_processor
->Transmit(command
);
367 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
370 strLog
.Format("<< %x -> %x: cec version ", m_iLogicalAddress
, dest
);
371 switch (m_cecVersion
)
373 case CEC_VERSION_1_2
:
374 strLog
.append("1.2");
376 case CEC_VERSION_1_2A
:
377 strLog
.append("1.2a");
379 case CEC_VERSION_1_3
:
380 strLog
.append("1.3");
382 case CEC_VERSION_1_3A
:
383 strLog
.append("1.3a");
386 strLog
.append("unknown");
389 AddLog(CEC_LOG_NOTICE
, strLog
);
392 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
393 command
.parameters
.push_back(m_cecVersion
);
395 return m_processor
->Transmit(command
);
398 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest
)
400 // need to support opcodes play and deck control before doing anything with this
402 strLog
.Format("<< %x -> %x: deck status feature abort", m_iLogicalAddress
, dest
);
403 AddLog(CEC_LOG_NOTICE
, strLog
);
405 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DECK_STATUS
);
409 bool CCECBusDevice::TransmitInactiveView(void)
412 strLog
.Format("<< %x -> broadcast: inactive view", m_iLogicalAddress
);
413 AddLog(CEC_LOG_NOTICE
, strLog
);
416 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
417 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
418 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
420 return m_processor
->Transmit(command
);
423 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
426 strLog
.Format("<< %x -> %x: ", m_iLogicalAddress
, dest
);
428 strLog
.append("menu active");
430 strLog
.append("menu inactive");
431 AddLog(CEC_LOG_NOTICE
, strLog
);
434 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
435 command
.parameters
.push_back(m_bMenuActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
437 return m_processor
->Transmit(command
);
440 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
443 strLog
.Format("<< %x -> %x: OSD name '%s'", m_iLogicalAddress
, dest
, m_strDeviceName
.c_str());
444 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
447 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
448 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
449 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
451 return m_processor
->Transmit(command
);
454 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
457 strLog
.Format("<< %x -> %x: display OSD message '%s'", m_iLogicalAddress
, dest
, strMessage
);
458 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
461 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_STRING
);
462 command
.parameters
.push_back((uint8_t)duration
);
464 unsigned int iLen
= strlen(strMessage
);
465 if (iLen
> 13) iLen
= 13;
467 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
468 command
.parameters
.push_back(strMessage
[iPtr
]);
470 return m_processor
->Transmit(command
);
473 bool CCECBusDevice::TransmitPhysicalAddress(void)
476 strLog
.Format("<< %x -> broadcast: physical adddress %4x", m_iLogicalAddress
, m_iPhysicalAddress
);
477 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
480 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
481 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
482 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
483 command
.parameters
.push_back((uint8_t) (m_type
));
485 return m_processor
->Transmit(command
);
488 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
492 if (dest
== CECDEVICE_UNKNOWN
)
493 dest
= m_iLogicalAddress
;
496 strLog
.Format("<< %x -> %x: POLL", m_iLogicalAddress
, dest
);
497 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
500 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_NONE
);
501 CLockObject
lock(&m_mutex
);
503 bReturn
= m_processor
->Transmit(command
);
504 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
508 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
511 strLog
.Format("<< %x -> %x: ", m_iLogicalAddress
, dest
);
513 switch (m_powerStatus
)
515 case CEC_POWER_STATUS_ON
:
516 strLog
.append("powered on");
518 case CEC_POWER_STATUS_STANDBY
:
519 strLog
.append("in standby mode");
521 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
522 strLog
.append("in transition from on to standby");
524 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
525 strLog
.append("in transition from standby to on");
528 strLog
.append("power state unknown");
531 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
533 strLog
.Format("<< reporting power status '%d'", m_powerStatus
);
534 AddLog(CEC_LOG_NOTICE
, strLog
);
537 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
538 command
.parameters
.push_back((uint8_t) m_powerStatus
);
540 return m_processor
->Transmit(command
);
543 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
)
546 strLog
.Format("<< %x -> %x: vendor id feature abort", m_iLogicalAddress
, dest
);
547 AddLog(CEC_LOG_NOTICE
, strLog
);
549 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);