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_bActiveSource(false),
51 m_iVendorClass(CEC_VENDOR_UNKNOWN
),
53 m_cecVersion(CEC_VERSION_UNKNOWN
)
55 m_handler
= new CCECCommandHandler(this);
57 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
58 m_menuLanguage
.language
[iPtr
] = '?';
59 m_menuLanguage
.language
[3] = 0;
60 m_menuLanguage
.device
= iLogicalAddress
;
62 m_vendor
.vendor
= CEC_VENDOR_UNKNOWN
;
63 m_type
= CEC_DEVICE_TYPE_RESERVED
;
64 m_strDeviceName
= "Unknown";
67 CCECBusDevice::~CCECBusDevice(void)
69 m_condition
.Broadcast();
73 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
75 m_processor
->AddLog(level
, strMessage
);
78 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
80 CLockObject
lock(&m_mutex
);
81 m_iLastActive
= GetTimeMs();
82 m_handler
->HandleCommand(command
);
87 void CCECBusDevice::PollVendorId(void)
89 CLockObject
lock(&m_mutex
);
90 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
91 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
92 GetTimeMs() - m_iLastActive
> 5000 &&
93 !m_processor
->IsMonitoring())
96 strLog
.Format("<< requesting vendor ID of device %x", m_iLogicalAddress
);
97 AddLog(CEC_LOG_NOTICE
, strLog
);
98 m_iLastActive
= GetTimeMs();
101 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
102 if (m_processor
->Transmit(command
))
103 m_condition
.Wait(&m_mutex
, 1000);
107 bool CCECBusDevice::PowerOn(void)
110 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
111 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
114 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
116 return m_processor
->Transmit(command
);
119 bool CCECBusDevice::Standby(void)
122 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
123 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
126 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
128 return m_processor
->Transmit(command
);
133 cec_version
CCECBusDevice::GetCecVersion(void)
135 if (m_cecVersion
== CEC_VERSION_UNKNOWN
)
137 if (!MyLogicalAddressContains(m_iLogicalAddress
))
140 strLog
.Format("<< requesting CEC version of device %x", m_iLogicalAddress
);
141 AddLog(CEC_LOG_NOTICE
, strLog
);
143 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
144 CLockObject
lock(&m_mutex
);
145 if (m_processor
->Transmit(command
))
146 m_condition
.Wait(&m_mutex
, 1000);
153 cec_menu_language
&CCECBusDevice::GetMenuLanguage(void)
155 if (!strcmp(m_menuLanguage
.language
, "???"))
157 if (!MyLogicalAddressContains(m_iLogicalAddress
))
160 strLog
.Format("<< requesting menu language of device %x", m_iLogicalAddress
);
161 AddLog(CEC_LOG_NOTICE
, strLog
);
163 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
164 CLockObject
lock(&m_mutex
);
165 if (m_processor
->Transmit(command
))
166 m_condition
.Wait(&m_mutex
, 1000);
170 return m_menuLanguage
;
173 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
175 return m_processor
->GetLogicalAddress();
178 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
180 return m_processor
->GetPhysicalAddress();
183 cec_power_status
CCECBusDevice::GetPowerStatus(void)
185 if (m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
187 if (!MyLogicalAddressContains(m_iLogicalAddress
))
190 strLog
.Format("<< requesting power status of device %x", m_iLogicalAddress
);
191 AddLog(CEC_LOG_NOTICE
, strLog
);
193 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
194 CLockObject
lock(&m_mutex
);
195 if (m_processor
->Transmit(command
))
196 m_condition
.Wait(&m_mutex
, 1000);
200 return m_powerStatus
;
203 const cec_vendor
&CCECBusDevice::GetVendor(void)
205 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
207 if (!MyLogicalAddressContains(m_iLogicalAddress
))
210 strLog
.Format("<< requesting vendor ID of device %x", m_iLogicalAddress
);
211 AddLog(CEC_LOG_NOTICE
, strLog
);
213 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
214 CLockObject
lock(&m_mutex
);
216 if (m_processor
->Transmit(command
))
217 m_condition
.Wait(&m_mutex
, 1000);
224 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
226 return m_processor
->HasLogicalAddress(address
);
233 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
236 m_cecVersion
= newVersion
;
240 case CEC_VERSION_1_2
:
241 strLog
.Format("device %d: CEC version 1.2", m_iLogicalAddress
);
243 case CEC_VERSION_1_2A
:
244 strLog
.Format("device %d: CEC version 1.2a", m_iLogicalAddress
);
246 case CEC_VERSION_1_3
:
247 strLog
.Format("device %d: CEC version 1.3", m_iLogicalAddress
);
249 case CEC_VERSION_1_3A
:
250 strLog
.Format("device %d: CEC version 1.3a", m_iLogicalAddress
);
253 strLog
.Format("device %d: unknown CEC version", m_iLogicalAddress
);
254 m_cecVersion
= CEC_VERSION_UNKNOWN
;
257 AddLog(CEC_LOG_DEBUG
, strLog
);
260 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
262 if (language
.device
== m_iLogicalAddress
)
265 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
266 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
267 m_menuLanguage
= language
;
271 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
)
276 strLog
.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
277 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
279 m_iPhysicalAddress
= iNewAddress
;
283 void CCECBusDevice::SetStreamPath(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
288 strLog
.Format(">> %i stream path from %04x to %04x", m_iLogicalAddress
, iOldAddress
, iNewAddress
);
289 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
291 m_iStreamPath
= iNewAddress
;
295 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
297 if (m_powerStatus
!= powerStatus
)
300 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
301 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
302 m_powerStatus
= powerStatus
;
306 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
308 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
309 m_iVendorClass
= iVendorClass
;
313 case CEC_VENDOR_SAMSUNG
:
314 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
317 m_handler
= new CANCommandHandler(this);
321 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
324 m_handler
= new CSLCommandHandler(this);
327 case CEC_VENDOR_PANASONIC
:
328 if (m_handler
->GetVendorId() != CEC_VENDOR_PANASONIC
)
331 m_handler
= new CVLCommandHandler(this);
335 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
338 m_handler
= new CCECCommandHandler(this);
344 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
345 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
349 /** @name Transmit methods */
351 bool CCECBusDevice::TransmitActiveSource(void)
356 strLog
.Format("<< %x -> broadcast: active source (%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((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
362 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
364 return m_processor
->Transmit(command
);
369 strLog
.Format("<< %x is not the active source", m_iLogicalAddress
);
370 AddLog(CEC_LOG_DEBUG
, strLog
);
376 bool CCECBusDevice::TransmitActiveView(void)
379 strLog
.Format("<< %x -> broadcast: active view (%4x)", m_iLogicalAddress
, m_iPhysicalAddress
);
380 AddLog(CEC_LOG_NOTICE
, strLog
);
383 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
384 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
385 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
387 return m_processor
->Transmit(command
);
390 bool CCECBusDevice::TransmitCECVersion(cec_logical_address dest
)
393 strLog
.Format("<< %x -> %x: cec version ", m_iLogicalAddress
, dest
);
394 switch (m_cecVersion
)
396 case CEC_VERSION_1_2
:
397 strLog
.append("1.2");
399 case CEC_VERSION_1_2A
:
400 strLog
.append("1.2a");
402 case CEC_VERSION_1_3
:
403 strLog
.append("1.3");
405 case CEC_VERSION_1_3A
:
406 strLog
.append("1.3a");
409 strLog
.append("unknown");
412 AddLog(CEC_LOG_NOTICE
, strLog
);
415 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
416 command
.parameters
.push_back(m_cecVersion
);
418 return m_processor
->Transmit(command
);
421 bool CCECBusDevice::TransmitDeckStatus(cec_logical_address dest
)
423 // need to support opcodes play and deck control before doing anything with this
425 strLog
.Format("<< %x -> %x: deck status feature abort", m_iLogicalAddress
, dest
);
426 AddLog(CEC_LOG_NOTICE
, strLog
);
428 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DECK_STATUS
);
432 bool CCECBusDevice::TransmitInactiveView(void)
435 strLog
.Format("<< %x -> broadcast: inactive view", m_iLogicalAddress
);
436 AddLog(CEC_LOG_NOTICE
, strLog
);
439 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
440 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
441 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
443 return m_processor
->Transmit(command
);
446 bool CCECBusDevice::TransmitMenuState(cec_logical_address dest
)
449 strLog
.Format("<< %x -> %x: ", m_iLogicalAddress
, dest
);
451 strLog
.append("menu active");
453 strLog
.append("menu inactive");
454 AddLog(CEC_LOG_NOTICE
, strLog
);
457 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
458 command
.parameters
.push_back(m_bMenuActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
460 return m_processor
->Transmit(command
);
463 bool CCECBusDevice::TransmitOSDName(cec_logical_address dest
)
466 strLog
.Format("<< %x -> %x: OSD name '%s'", m_iLogicalAddress
, dest
, m_strDeviceName
.c_str());
467 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
470 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
471 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
472 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
474 return m_processor
->Transmit(command
);
477 bool CCECBusDevice::TransmitOSDString(cec_logical_address dest
, cec_display_control duration
, const char *strMessage
)
480 strLog
.Format("<< %x -> %x: display OSD message '%s'", m_iLogicalAddress
, dest
, strMessage
);
481 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
484 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_STRING
);
485 command
.parameters
.push_back((uint8_t)duration
);
487 unsigned int iLen
= strlen(strMessage
);
488 if (iLen
> 13) iLen
= 13;
490 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
491 command
.parameters
.push_back(strMessage
[iPtr
]);
493 return m_processor
->Transmit(command
);
496 bool CCECBusDevice::TransmitPhysicalAddress(void)
499 strLog
.Format("<< %x -> broadcast: physical adddress %4x", m_iLogicalAddress
, m_iPhysicalAddress
);
500 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
503 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
504 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
505 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
506 command
.parameters
.push_back((uint8_t) (m_type
));
508 return m_processor
->Transmit(command
);
511 bool CCECBusDevice::TransmitPoll(cec_logical_address dest
)
515 if (dest
== CECDEVICE_UNKNOWN
)
516 dest
= m_iLogicalAddress
;
519 strLog
.Format("<< %x -> %x: POLL", m_iLogicalAddress
, dest
);
520 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
523 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_NONE
);
524 CLockObject
lock(&m_mutex
);
526 bReturn
= m_processor
->Transmit(command
);
527 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");
531 bool CCECBusDevice::TransmitPowerState(cec_logical_address dest
)
534 strLog
.Format("<< %x -> %x: ", m_iLogicalAddress
, dest
);
536 switch (m_powerStatus
)
538 case CEC_POWER_STATUS_ON
:
539 strLog
.append("powered on");
541 case CEC_POWER_STATUS_STANDBY
:
542 strLog
.append("in standby mode");
544 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
545 strLog
.append("in transition from on to standby");
547 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
548 strLog
.append("in transition from standby to on");
551 strLog
.append("power state unknown");
554 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
556 strLog
.Format("<< reporting power status '%d'", m_powerStatus
);
557 AddLog(CEC_LOG_NOTICE
, strLog
);
560 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
561 command
.parameters
.push_back((uint8_t) m_powerStatus
);
563 return m_processor
->Transmit(command
);
566 bool CCECBusDevice::TransmitVendorID(cec_logical_address dest
)
569 strLog
.Format("<< %x -> %x: vendor id feature abort", m_iLogicalAddress
, dest
);
570 AddLog(CEC_LOG_NOTICE
, strLog
);
572 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);