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
;
58 switch(iLogicalAddress
)
60 case CECDEVICE_AUDIOSYSTEM
:
61 m_type
= CEC_DEVICE_TYPE_AUDIO_SYSTEM
;
62 m_strDeviceName
= "Audio";
64 case CECDEVICE_PLAYBACKDEVICE1
:
65 case CECDEVICE_PLAYBACKDEVICE2
:
66 case CECDEVICE_PLAYBACKDEVICE3
:
67 m_type
= CEC_DEVICE_TYPE_PLAYBACK_DEVICE
;
68 m_strDeviceName
= "Player";
70 case CECDEVICE_RECORDINGDEVICE1
:
71 case CECDEVICE_RECORDINGDEVICE2
:
72 case CECDEVICE_RECORDINGDEVICE3
:
73 m_type
= CEC_DEVICE_TYPE_RECORDING_DEVICE
;
74 m_strDeviceName
= "Recorder";
76 case CECDEVICE_TUNER1
:
77 case CECDEVICE_TUNER2
:
78 case CECDEVICE_TUNER3
:
79 case CECDEVICE_TUNER4
:
80 m_type
= CEC_DEVICE_TYPE_TUNER
;
81 m_strDeviceName
= "Tuner";
84 m_type
= CEC_DEVICE_TYPE_TV
;
85 m_strDeviceName
= "TV";
88 m_type
= CEC_DEVICE_TYPE_RESERVED
;
89 m_strDeviceName
= "Unknown";
94 CCECBusDevice::~CCECBusDevice(void)
96 m_condition
.Broadcast();
100 cec_logical_address
CCECBusDevice::GetMyLogicalAddress(void) const
102 return m_processor
->GetLogicalAddress();
105 bool CCECBusDevice::MyLogicalAddressContains(cec_logical_address address
) const
107 return m_processor
->HasLogicalAddress(address
);
110 uint16_t CCECBusDevice::GetMyPhysicalAddress(void) const
112 return m_processor
->GetPhysicalAddress();
115 void CCECBusDevice::AddLog(cec_log_level level
, const CStdString
&strMessage
)
117 m_processor
->AddLog(level
, strMessage
);
120 void CCECBusDevice::SetMenuLanguage(const cec_menu_language
&language
)
122 if (language
.device
== m_iLogicalAddress
)
125 strLog
.Format("device %d menu language set to '%s'", m_iLogicalAddress
, language
.language
);
126 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
127 m_menuLanguage
= language
;
131 void CCECBusDevice::SetCecVersion(const cec_version newVersion
)
134 m_cecVersion
= newVersion
;
138 case CEC_VERSION_1_2
:
139 strLog
.Format("device %d reports CEC version 1.2", m_iLogicalAddress
);
141 case CEC_VERSION_1_2A
:
142 strLog
.Format("device %d reports CEC version 1.2a", m_iLogicalAddress
);
144 case CEC_VERSION_1_3
:
145 strLog
.Format("device %d reports CEC version 1.3", m_iLogicalAddress
);
147 case CEC_VERSION_1_3A
:
148 strLog
.Format("device %d reports CEC version 1.3a", m_iLogicalAddress
);
151 strLog
.Format("device %d reports an unknown CEC version", m_iLogicalAddress
);
152 m_cecVersion
= CEC_VERSION_UNKNOWN
;
155 AddLog(CEC_LOG_DEBUG
, strLog
);
158 void CCECBusDevice::SetPowerStatus(const cec_power_status powerStatus
)
160 if (m_powerStatus
!= powerStatus
)
163 strLog
.Format("device %d power status changed from %2x to %2x", m_iLogicalAddress
, m_powerStatus
, powerStatus
);
164 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
165 m_powerStatus
= powerStatus
;
169 void CCECBusDevice::SetVendorId(const cec_datapacket
&data
)
173 AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
177 uint64_t iVendorId
= ((uint64_t)data
[0] << 3) +
178 ((uint64_t)data
[1] << 2) +
181 SetVendorId(iVendorId
, data
.size
>= 4 ? data
[3] : 0);
184 void CCECBusDevice::SetVendorId(uint64_t iVendorId
, uint8_t iVendorClass
/* = 0 */)
186 m_vendor
.vendor
= (cec_vendor_id
)iVendorId
;
187 m_iVendorClass
= iVendorClass
;
191 case CEC_VENDOR_SAMSUNG
:
192 if (m_handler
->GetVendorId() != CEC_VENDOR_SAMSUNG
)
195 m_handler
= new CANCommandHandler(this);
199 if (m_handler
->GetVendorId() != CEC_VENDOR_LG
)
202 m_handler
= new CSLCommandHandler(this);
206 if (m_handler
->GetVendorId() != CEC_VENDOR_UNKNOWN
)
209 m_handler
= new CCECCommandHandler(this);
215 strLog
.Format("device %d: vendor = %s (%06x) class = %2x", m_iLogicalAddress
, GetVendorName(), GetVendorId(), GetVendorClass());
216 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
219 bool CCECBusDevice::HandleCommand(const cec_command
&command
)
221 CLockObject
lock(&m_mutex
);
222 m_iLastActive
= GetTimeMs();
223 m_handler
->HandleCommand(command
);
224 m_condition
.Signal();
228 const cec_vendor
&CCECBusDevice::GetVendor(void)
230 if (m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
)
232 AddLog(CEC_LOG_NOTICE
, "<< requesting vendor ID");
234 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
235 CLockObject
lock(&m_mutex
);
237 if (m_processor
->Transmit(command
))
238 m_condition
.Wait(&m_mutex
, 1000);
244 void CCECBusDevice::PollVendorId(void)
246 CLockObject
lock(&m_mutex
);
247 if (m_iLastActive
> 0 && m_iLogicalAddress
!= CECDEVICE_BROADCAST
&&
248 m_vendor
.vendor
== CEC_VENDOR_UNKNOWN
&&
249 GetTimeMs() - m_iLastActive
> 5000)
251 m_iLastActive
= GetTimeMs();
254 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
255 if (m_processor
->Transmit(command
))
256 m_condition
.Wait(&m_mutex
, 1000);
260 void CCECBusDevice::SetPhysicalAddress(uint16_t iNewAddress
, uint16_t iOldAddress
/* = 0 */)
265 strLog
.Format(">> %i changed physical address from %04x to %04x", m_iLogicalAddress
, m_iPhysicalAddress
, iNewAddress
);
266 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
268 m_iPhysicalAddress
= iNewAddress
;
272 bool CCECBusDevice::PowerOn(void)
275 strLog
.Format("<< powering on device with logical address %d", (int8_t)m_iLogicalAddress
);
276 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
279 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_IMAGE_VIEW_ON
);
281 return m_processor
->Transmit(command
);
284 bool CCECBusDevice::Standby(void)
287 strLog
.Format("<< putting device with logical address %d in standby mode", (int8_t)m_iLogicalAddress
);
288 AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
291 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_STANDBY
);
293 return m_processor
->Transmit(command
);
296 bool CCECBusDevice::SetOSDString(cec_display_control duration
, const char *strMessage
)
299 strLog
.Format("<< display message '%s'", strMessage
);
300 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
303 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_SET_OSD_STRING
);
304 command
.parameters
.push_back((uint8_t)duration
);
306 for (unsigned int iPtr
= 0; iPtr
< strlen(strMessage
); iPtr
++)
307 command
.parameters
.push_back(strMessage
[iPtr
]);
309 return m_processor
->Transmit(command
);
312 bool CCECBusDevice::ReportCECVersion(cec_logical_address dest
)
314 AddLog(CEC_LOG_NOTICE
, "<< reporting CEC version as 1.3a");
317 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_CEC_VERSION
);
318 command
.parameters
.push_back(CEC_VERSION_1_3A
);
320 return m_processor
->Transmit(command
);
323 bool CCECBusDevice::ReportDeckStatus(cec_logical_address dest
)
325 // need to support opcodes play and deck control before doing anything with this
326 AddLog(CEC_LOG_NOTICE
, "<< deck status requested, feature abort");
327 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
331 bool CCECBusDevice::ReportMenuState(cec_logical_address dest
, bool bActive
/* = true */)
334 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as active");
336 AddLog(CEC_LOG_NOTICE
, "<< reporting menu state as inactive");
339 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_MENU_STATUS
);
340 command
.parameters
.push_back(bActive
? (uint8_t) CEC_MENU_STATE_ACTIVATED
: (uint8_t) CEC_MENU_STATE_DEACTIVATED
);
342 return m_processor
->Transmit(command
);
345 bool CCECBusDevice::ReportOSDName(cec_logical_address dest
)
348 strLog
.Format("<< reporting OSD name as %s", m_strDeviceName
.c_str());
349 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
352 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_SET_OSD_NAME
);
353 for (unsigned int iPtr
= 0; iPtr
< m_strDeviceName
.length(); iPtr
++)
354 command
.parameters
.push_back(m_strDeviceName
.at(iPtr
));
356 return m_processor
->Transmit(command
);
359 bool CCECBusDevice::ReportPowerState(cec_logical_address dest
)
361 AddLog(CEC_LOG_NOTICE
, "<< reporting \"On\" power status");
364 cec_command::format(command
, m_iLogicalAddress
, dest
, CEC_OPCODE_REPORT_POWER_STATUS
);
365 // command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
366 command
.parameters
.push_back((uint8_t) CEC_POWER_STATUS_ON
);
368 return m_processor
->Transmit(command
);
371 bool CCECBusDevice::ReportVendorID(cec_logical_address dest
)
373 AddLog(CEC_LOG_NOTICE
, "<< vendor ID requested, feature abort");
374 m_processor
->TransmitAbort(dest
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
378 bool CCECBusDevice::BroadcastActiveView(void)
380 AddLog(CEC_LOG_DEBUG
, "<< setting active view");
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::BroadcastInactiveView(void)
392 AddLog(CEC_LOG_DEBUG
, "<< setting inactive view");
395 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_INACTIVE_SOURCE
);
396 command
.parameters
.push_back((m_iPhysicalAddress
>> 8) & 0xFF);
397 command
.parameters
.push_back(m_iPhysicalAddress
& 0xFF);
399 return m_processor
->Transmit(command
);
402 bool CCECBusDevice::BroadcastPhysicalAddress(void)
405 strLog
.Format("<< reporting physical address as %04x", m_iPhysicalAddress
);
406 AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
409 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
410 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
411 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
412 command
.parameters
.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE
));
414 return m_processor
->Transmit(command
);
417 bool CCECBusDevice::BroadcastActiveSource(void)
419 AddLog(CEC_LOG_NOTICE
, "<< broadcasting active source");
422 cec_command::format(command
, m_iLogicalAddress
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
423 command
.parameters
.push_back((uint8_t) ((m_iPhysicalAddress
>> 8) & 0xFF));
424 command
.parameters
.push_back((uint8_t) (m_iPhysicalAddress
& 0xFF));
426 return m_processor
->Transmit(command
);
429 cec_version
CCECBusDevice::GetCecVersion(bool bRefresh
/* = true */)
431 if (bRefresh
|| m_cecVersion
== CEC_VERSION_UNKNOWN
)
433 AddLog(CEC_LOG_NOTICE
, "<< requesting CEC version");
435 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_CEC_VERSION
);
436 CLockObject
lock(&m_mutex
);
437 if (m_processor
->Transmit(command
))
438 m_condition
.Wait(&m_mutex
, 1000);
444 cec_menu_language
&CCECBusDevice::GetMenuLanguage(bool bRefresh
/* = true */)
446 if (bRefresh
|| !strcmp(m_menuLanguage
.language
, "???"))
448 AddLog(CEC_LOG_NOTICE
, "<< requesting menu language");
450 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GET_MENU_LANGUAGE
);
451 CLockObject
lock(&m_mutex
);
452 if (m_processor
->Transmit(command
))
453 m_condition
.Wait(&m_mutex
, 1000);
456 return m_menuLanguage
;
459 cec_power_status
CCECBusDevice::GetPowerStatus(bool bRefresh
/* = true */)
461 if (bRefresh
|| m_powerStatus
== CEC_POWER_STATUS_UNKNOWN
)
463 AddLog(CEC_LOG_NOTICE
, "<< requesting power status");
465 cec_command::format(command
, GetMyLogicalAddress(), m_iLogicalAddress
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
466 CLockObject
lock(&m_mutex
);
467 if (m_processor
->Transmit(command
))
468 m_condition
.Wait(&m_mutex
, 1000);
471 return m_powerStatus
;
474 bool CCECBusDevice::PollDevice(cec_logical_address source
/* = CECDEVICE_UNKNOWN */)
478 if (source
== CECDEVICE_UNKNOWN
)
479 source
= GetMyLogicalAddress();
482 strLog
.Format("<< sending POLL from device %1x to device %1x", (int8_t)source
, m_iLogicalAddress
);
483 AddLog(CEC_LOG_DEBUG
, strLog
);
486 cec_command::format(command
, source
, m_iLogicalAddress
, CEC_OPCODE_NONE
);
487 CLockObject
lock(&m_mutex
);
489 bReturn
= m_processor
->Transmit(command
);
490 AddLog(CEC_LOG_DEBUG
, bReturn
? ">> POLL sent" : ">> POLL not sent");