2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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 "CECProcessor.h"
35 #include "adapter/USBCECAdapterCommunication.h"
36 #include "devices/CECBusDevice.h"
37 #include "devices/CECAudioSystem.h"
38 #include "devices/CECPlaybackDevice.h"
39 #include "devices/CECRecordingDevice.h"
40 #include "devices/CECTuner.h"
41 #include "devices/CECTV.h"
42 #include "implementations/CECCommandHandler.h"
44 #include "platform/util/timeutils.h"
48 using namespace PLATFORM
;
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, libcec_configuration
*configuration
) :
51 m_bConnectionOpened(false),
52 m_bInitialised(false),
53 m_communication(NULL
),
54 m_controller(controller
),
56 m_iStandardLineTimeout(3),
57 m_iRetryLineTimeout(3),
58 m_iLastTransmission(0)
61 m_configuration
.Clear();
62 m_configuration
.serverVersion
= CEC_SERVER_VERSION_1_6_1
;
63 SetConfiguration(configuration
);
65 if (m_configuration
.tvVendor
!= CEC_VENDOR_UNKNOWN
)
66 m_busDevices
[CECDEVICE_TV
]->ReplaceHandler(false);
69 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
, uint16_t iPhysicalAddress
) :
70 m_bConnectionOpened(false),
71 m_bInitialised(false),
72 m_communication(NULL
),
73 m_controller(controller
),
75 m_iStandardLineTimeout(3),
76 m_iRetryLineTimeout(3),
77 m_iLastTransmission(0)
79 m_configuration
.Clear();
80 m_configuration
.serverVersion
= CEC_SERVER_VERSION_1_6_1
;
82 // client version < 1.5.0
83 m_configuration
.clientVersion
= (uint32_t)CEC_CLIENT_VERSION_PRE_1_5
;
84 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
);
85 m_configuration
.deviceTypes
= types
;
86 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
87 m_configuration
.baseDevice
= (cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
;
88 m_configuration
.iHDMIPort
= CEC_DEFAULT_HDMI_PORT
;
90 if (m_configuration
.deviceTypes
.IsEmpty())
91 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
95 void CCECProcessor::CreateBusDevices(void)
97 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
101 case CECDEVICE_AUDIOSYSTEM
:
102 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
104 case CECDEVICE_PLAYBACKDEVICE1
:
105 case CECDEVICE_PLAYBACKDEVICE2
:
106 case CECDEVICE_PLAYBACKDEVICE3
:
107 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
109 case CECDEVICE_RECORDINGDEVICE1
:
110 case CECDEVICE_RECORDINGDEVICE2
:
111 case CECDEVICE_RECORDINGDEVICE3
:
112 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
114 case CECDEVICE_TUNER1
:
115 case CECDEVICE_TUNER2
:
116 case CECDEVICE_TUNER3
:
117 case CECDEVICE_TUNER4
:
118 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
121 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
124 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
130 CCECProcessor::~CCECProcessor(void)
134 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
136 delete m_busDevices
[iPtr
];
137 m_busDevices
[iPtr
] = NULL
;
141 void CCECProcessor::Close(void)
144 SetInitialised(false);
149 CLockObject
lock(m_mutex
);
150 bClose
= m_bConnectionOpened
;
151 m_bConnectionOpened
= false;
154 if (bClose
&& m_communication
)
156 delete m_communication
;
157 m_communication
= NULL
;
161 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
167 CLockObject
lock(m_mutex
);
168 if (m_bConnectionOpened
)
170 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
173 m_communication
= new CUSBCECAdapterCommunication(this, strPort
, iBaudRate
);
174 m_bConnectionOpened
= (m_communication
!= NULL
);
177 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
179 /* open a new connection */
180 unsigned iConnectTry(0);
181 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
183 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
184 m_communication
->Close();
190 m_configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
191 uint32_t iBuildDate
= m_communication
->GetFirmwareBuildDate();
193 strLog
.Format("connected to the CEC adapter. libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version
)m_configuration
.serverVersion
), ToString((cec_client_version
)m_configuration
.clientVersion
), m_configuration
.iFirmwareVersion
);
196 time_t buildTime
= (time_t)iBuildDate
;
197 strLog
.AppendFormat(", firmware build date: %s +0000", asctime(gmtime(&buildTime
)));
198 strLog
= strLog
.Left(strLog
.length() - 1); // strip \n added by asctime
200 CLibCEC::AddLog(CEC_LOG_NOTICE
, strLog
);
203 if (m_configuration
.bGetSettingsFromROM
== 1)
205 libcec_configuration config
;
207 m_communication
->GetConfiguration(&config
);
209 CLockObject
lock(m_mutex
);
210 if (!config
.deviceTypes
.IsEmpty())
211 m_configuration
.deviceTypes
= config
.deviceTypes
;
212 if (config
.iPhysicalAddress
> 0)
213 m_configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
214 snprintf(m_configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
220 bool CCECProcessor::IsInitialised(void)
222 CLockObject
lock(m_threadMutex
);
223 return m_bInitialised
;
226 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
228 CLockObject
lock(m_mutex
);
229 m_bInitialised
= bSetTo
;
232 bool CCECProcessor::Initialise(void)
236 CLockObject
lock(m_mutex
);
237 if (!m_configuration
.logicalAddresses
.IsEmpty())
238 m_configuration
.logicalAddresses
.Clear();
240 if (!FindLogicalAddresses())
242 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
246 /* only set our OSD name for the primary device */
247 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
249 /* make the primary device the active source if the option is set */
250 if (m_configuration
.bActivateSource
== 1)
251 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
253 /* set the default menu language for devices we control */
254 cec_menu_language language
;
255 language
.device
= m_configuration
.logicalAddresses
.primary
;
256 memcpy(language
.language
, m_configuration
.strDeviceLanguage
, 3);
257 language
.language
[3] = 0;
259 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
261 if (m_configuration
.logicalAddresses
[iPtr
])
263 language
.device
= (cec_logical_address
) iPtr
;
264 m_busDevices
[iPtr
]->SetMenuLanguage(language
);
269 /* get the vendor id from the TV, so we are using the correct handler */
270 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
272 if (m_configuration
.iPhysicalAddress
!= 0)
274 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
275 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
276 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
277 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
279 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
280 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set HDMI port %d on %s (%x)", m_configuration
.iHDMIPort
, ToString(m_configuration
.baseDevice
), (uint8_t)m_configuration
.baseDevice
);
282 if (bReturn
&& m_configuration
.bActivateSource
== 1)
283 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
285 SetInitialised(bReturn
);
287 CLibCEC::ConfigurationChanged(m_configuration
);
292 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
297 CLockObject
lock(m_mutex
);
298 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
301 /* create the processor thread */
304 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
309 if ((bReturn
= Initialise()) == false)
311 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
316 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
322 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
324 if (m_busDevices
[address
]->TryLogicalAddress())
326 m_configuration
.logicalAddresses
.Set(address
);
333 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
335 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
336 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
337 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
338 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
341 bool CCECProcessor::FindLogicalAddressTuner(void)
343 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
344 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
345 TryLogicalAddress(CECDEVICE_TUNER2
) ||
346 TryLogicalAddress(CECDEVICE_TUNER3
) ||
347 TryLogicalAddress(CECDEVICE_TUNER4
);
350 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
352 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
353 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
354 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
355 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
358 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
360 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
361 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
364 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
366 bool bChanged(false);
368 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
370 CLockObject
lock(m_mutex
);
371 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
372 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
374 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
376 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
379 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
382 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
384 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
386 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
392 FindLogicalAddresses();
394 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
395 if (previousDevice
&& newDevice
)
397 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
398 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
400 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
401 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
403 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
405 newDevice
->SetMenuState(previousDevice
->GetMenuState());
406 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
408 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
409 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
411 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
412 previousDevice
->SetPhysicalAddress(0xFFFF);
414 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
415 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
417 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
418 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
420 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
421 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
423 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
424 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
426 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
427 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
435 bool CCECProcessor::FindLogicalAddresses(void)
438 m_configuration
.logicalAddresses
.Clear();
440 if (m_configuration
.deviceTypes
.IsEmpty())
442 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
446 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
448 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
451 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
453 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
454 bReturn
&= FindLogicalAddressRecordingDevice();
455 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
456 bReturn
&= FindLogicalAddressTuner();
457 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
458 bReturn
&= FindLogicalAddressPlaybackDevice();
459 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
460 bReturn
&= FindLogicalAddressAudioSystem();
464 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
469 void CCECProcessor::ReplaceHandlers(void)
471 if (!IsInitialised())
473 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
474 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
477 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
479 return m_inBuffer
.Push(command
);
482 void *CCECProcessor::Process(void)
484 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
489 while (!IsStopped() && m_communication
->IsOpen())
491 if (m_inBuffer
.Pop(command
, 500))
492 ParseCommand(command
);
498 m_controller
->CheckKeypressTimeout();
505 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
512 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
514 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
516 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
518 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
520 addr
= (cec_logical_address
) iPtr
;
526 m_busDevices
[addr
]->SetActiveSource();
527 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
528 bReturn
= m_busDevices
[addr
]->ActivateSource();
533 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
537 // suppress polls when searching for a device
538 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
, false, true);
541 device
->SetActiveSource();
548 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
550 CLockObject
lock(m_mutex
);
551 m_iStandardLineTimeout
= iTimeout
;
554 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
556 CLockObject
lock(m_mutex
);
557 m_iRetryLineTimeout
= iTimeout
;
560 bool CCECProcessor::SetActiveView(void)
562 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
563 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
566 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
570 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
573 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
575 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
582 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
586 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
589 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
591 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
598 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
602 // limit the HDMI port range to 1-15
609 CLockObject
lock(m_mutex
);
610 m_configuration
.baseDevice
= iBaseDevice
;
611 m_configuration
.iHDMIPort
= iPort
;
614 if (!IsRunning() && !bForce
)
617 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
619 uint16_t iPhysicalAddress(0);
620 if (iBaseDevice
> CECDEVICE_TV
)
622 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
625 if (iPhysicalAddress
< 0xffff)
627 if (iPhysicalAddress
== 0)
628 iPhysicalAddress
+= 0x1000 * iPort
;
629 else if (iPhysicalAddress
% 0x1000 == 0)
630 iPhysicalAddress
+= 0x100 * iPort
;
631 else if (iPhysicalAddress
% 0x100 == 0)
632 iPhysicalAddress
+= 0x10 * iPort
;
633 else if (iPhysicalAddress
% 0x10 == 0)
634 iPhysicalAddress
+= iPort
;
640 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
642 SetPhysicalAddress(iPhysicalAddress
);
647 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
649 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
651 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
657 bool CCECProcessor::TransmitInactiveSource(void)
662 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
663 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
667 void CCECProcessor::LogOutput(const cec_command
&data
)
670 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
672 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
674 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
675 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
676 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
679 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
681 CLockObject
lock(m_mutex
);
682 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
684 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
685 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
686 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
687 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
693 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
695 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
697 if (m_configuration
.logicalAddresses
[iPtr
])
698 m_busDevices
[iPtr
]->SetMenuState(state
);
702 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
707 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
709 bool bSendActiveView(false);
711 cec_logical_addresses sendUpdatesTo
;
712 sendUpdatesTo
.Clear();
715 CLockObject
lock(m_mutex
);
716 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
717 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
719 if (!m_configuration
.logicalAddresses
.IsEmpty())
721 bool bWasActiveSource(false);
722 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
723 if (m_configuration
.logicalAddresses
[iPtr
])
725 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
726 m_busDevices
[iPtr
]->SetInactiveSource();
727 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
729 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
732 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
737 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
738 if (sendUpdatesTo
[iPtr
])
739 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
746 libcec_configuration config
;
748 CLockObject
lock(m_mutex
);
749 config
= m_configuration
;
752 PersistConfiguration(&config
);
753 CLibCEC::ConfigurationChanged(config
);
759 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
761 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
764 CLockObject
lock(m_mutex
);
765 m_bMonitor
= bEnable
;
769 return SetAckMask(0);
771 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
774 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
776 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
778 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
779 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
780 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
785 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
788 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
789 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
794 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
797 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
798 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
803 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
806 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
807 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
812 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */, bool bSuppressPoll
/* = false */) const
814 if (m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
815 return m_busDevices
[m_configuration
.logicalAddresses
.primary
];
817 CCECBusDevice
*device
= NULL
;
818 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
820 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
, bSuppressPoll
) == iPhysicalAddress
)
822 device
= m_busDevices
[iPtr
];
830 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
832 CCECBusDevice
*device
= NULL
;
834 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
836 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
838 device
= m_busDevices
[iPtr
];
846 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
848 CCECBusDevice
*device(NULL
);
849 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
850 if (primary
!= CECDEVICE_UNKNOWN
)
851 device
= m_busDevices
[primary
];
855 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
857 return m_busDevices
[iAddress
]->GetCecVersion();
860 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
862 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
865 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
866 retVal
.device
= iAddress
;
871 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
873 if (m_busDevices
[iAddress
])
875 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
876 return (strcmp(language
->language
, "???") != 0);
881 CStdString
CCECProcessor::GetDeviceName(void) const
884 strName
= m_configuration
.strDeviceName
;
888 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
890 if (m_busDevices
[iAddress
])
891 return m_busDevices
[iAddress
]->GetVendorId();
895 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
897 if (m_busDevices
[iAddress
])
898 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
902 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
904 if (m_busDevices
[iAddress
])
905 return m_busDevices
[iAddress
]->GetPowerStatus();
906 return CEC_POWER_STATUS_UNKNOWN
;
909 cec_logical_address
CCECProcessor::GetActiveSource(void)
911 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
913 if (m_busDevices
[iPtr
]->IsActiveSource())
914 return (cec_logical_address
)iPtr
;
917 return CECDEVICE_UNKNOWN
;
920 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
922 return iAddress
> CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
?
923 m_busDevices
[iAddress
]->IsActiveSource() :
927 bool CCECProcessor::Transmit(const cec_command
&data
)
929 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
931 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
935 uint8_t iMaxTries(0);
937 CLockObject
lock(m_mutex
);
941 m_iLastTransmission
= GetTimeMs();
942 if (!m_communication
|| !m_communication
->IsOpen())
944 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
947 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
952 uint8_t iLineTimeout
= m_iStandardLineTimeout
;
953 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
955 while (bRetry
&& ++iTries
< iMaxTries
)
957 if (m_busDevices
[data
.initiator
]->IsUnsupportedFeature(data
.opcode
))
960 adapterState
= m_communication
->Write(data
, bRetry
, iLineTimeout
);
961 iLineTimeout
= m_iRetryLineTimeout
;
964 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
967 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
969 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
973 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
974 command
.parameters
.PushBack((uint8_t)opcode
);
975 command
.parameters
.PushBack((uint8_t)reason
);
980 void CCECProcessor::ParseCommand(const cec_command
&command
)
983 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
984 if (command
.opcode_set
== 1)
985 dataStr
.AppendFormat(":%02x", command
.opcode
);
986 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
987 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
988 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
990 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
991 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
994 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
996 cec_logical_addresses addresses
;
998 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1000 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1001 addresses
.Set((cec_logical_address
) iPtr
);
1006 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
1008 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
1011 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
1013 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1015 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1022 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1024 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
1025 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false);
1029 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1031 return m_communication
? m_communication
->SetAckMask(iMask
) : false;
1034 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1036 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1039 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1041 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1044 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1046 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
1047 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1048 if (iPhysicalAddress
!= 0)
1050 m_configuration
.bAutodetectAddress
= 1;
1051 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1052 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1053 m_configuration
.iHDMIPort
= 0;
1054 return SetPhysicalAddress(iPhysicalAddress
);
1059 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1061 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1064 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1066 if (m_configuration
.powerOffDevices
[iPtr
])
1068 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - putting '%s' in standby mode", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1069 bReturn
&= m_busDevices
[iPtr
]->Standby();
1075 return m_busDevices
[address
]->Standby();
1078 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1080 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1083 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1085 if (m_configuration
.wakeDevices
[iPtr
])
1087 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - powering on '%s'", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1088 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1094 return m_busDevices
[address
]->PowerOn();
1097 const char *CCECProcessor::ToString(const cec_device_type type
)
1101 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1102 return "audio system";
1103 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1104 return "playback device";
1105 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1106 return "recording device";
1107 case CEC_DEVICE_TYPE_RESERVED
:
1109 case CEC_DEVICE_TYPE_TUNER
:
1111 case CEC_DEVICE_TYPE_TV
:
1118 const char *CCECProcessor::ToString(const cec_menu_state state
)
1122 case CEC_MENU_STATE_ACTIVATED
:
1124 case CEC_MENU_STATE_DEACTIVATED
:
1125 return "deactivated";
1131 const char *CCECProcessor::ToString(const cec_version version
)
1135 case CEC_VERSION_1_2
:
1137 case CEC_VERSION_1_2A
:
1139 case CEC_VERSION_1_3
:
1141 case CEC_VERSION_1_3A
:
1143 case CEC_VERSION_1_4
:
1150 const char *CCECProcessor::ToString(const cec_power_status status
)
1154 case CEC_POWER_STATUS_ON
:
1156 case CEC_POWER_STATUS_STANDBY
:
1158 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1159 return "in transition from on to standby";
1160 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1161 return "in transition from standby to on";
1167 const char *CCECProcessor::ToString(const cec_logical_address address
)
1171 case CECDEVICE_AUDIOSYSTEM
:
1173 case CECDEVICE_BROADCAST
:
1175 case CECDEVICE_FREEUSE
:
1177 case CECDEVICE_PLAYBACKDEVICE1
:
1178 return "Playback 1";
1179 case CECDEVICE_PLAYBACKDEVICE2
:
1180 return "Playback 2";
1181 case CECDEVICE_PLAYBACKDEVICE3
:
1182 return "Playback 3";
1183 case CECDEVICE_RECORDINGDEVICE1
:
1184 return "Recorder 1";
1185 case CECDEVICE_RECORDINGDEVICE2
:
1186 return "Recorder 2";
1187 case CECDEVICE_RECORDINGDEVICE3
:
1188 return "Recorder 3";
1189 case CECDEVICE_RESERVED1
:
1190 return "Reserved 1";
1191 case CECDEVICE_RESERVED2
:
1192 return "Reserved 2";
1193 case CECDEVICE_TUNER1
:
1195 case CECDEVICE_TUNER2
:
1197 case CECDEVICE_TUNER3
:
1199 case CECDEVICE_TUNER4
:
1208 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1212 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1213 return "skip forward wind";
1214 case CEC_DECK_CONTROL_MODE_EJECT
:
1216 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1217 return "reverse rewind";
1218 case CEC_DECK_CONTROL_MODE_STOP
:
1225 const char *CCECProcessor::ToString(const cec_deck_info status
)
1229 case CEC_DECK_INFO_PLAY
:
1231 case CEC_DECK_INFO_RECORD
:
1233 case CEC_DECK_INFO_PLAY_REVERSE
:
1234 return "play reverse";
1235 case CEC_DECK_INFO_STILL
:
1237 case CEC_DECK_INFO_SLOW
:
1239 case CEC_DECK_INFO_SLOW_REVERSE
:
1240 return "slow reverse";
1241 case CEC_DECK_INFO_FAST_FORWARD
:
1242 return "fast forward";
1243 case CEC_DECK_INFO_FAST_REVERSE
:
1244 return "fast reverse";
1245 case CEC_DECK_INFO_NO_MEDIA
:
1247 case CEC_DECK_INFO_STOP
:
1249 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1250 return "info skip forward wind";
1251 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1252 return "info skip reverse rewind";
1253 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1254 return "info index search forward";
1255 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1256 return "info index search reverse";
1257 case CEC_DECK_INFO_OTHER_STATUS
:
1264 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1268 case CEC_OPCODE_ACTIVE_SOURCE
:
1269 return "active source";
1270 case CEC_OPCODE_IMAGE_VIEW_ON
:
1271 return "image view on";
1272 case CEC_OPCODE_TEXT_VIEW_ON
:
1273 return "text view on";
1274 case CEC_OPCODE_INACTIVE_SOURCE
:
1275 return "inactive source";
1276 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1277 return "request active source";
1278 case CEC_OPCODE_ROUTING_CHANGE
:
1279 return "routing change";
1280 case CEC_OPCODE_ROUTING_INFORMATION
:
1281 return "routing information";
1282 case CEC_OPCODE_SET_STREAM_PATH
:
1283 return "set stream path";
1284 case CEC_OPCODE_STANDBY
:
1286 case CEC_OPCODE_RECORD_OFF
:
1287 return "record off";
1288 case CEC_OPCODE_RECORD_ON
:
1290 case CEC_OPCODE_RECORD_STATUS
:
1291 return "record status";
1292 case CEC_OPCODE_RECORD_TV_SCREEN
:
1293 return "record tv screen";
1294 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1295 return "clear analogue timer";
1296 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1297 return "clear digital timer";
1298 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1299 return "clear external timer";
1300 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1301 return "set analogue timer";
1302 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1303 return "set digital timer";
1304 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1305 return "set external timer";
1306 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1307 return "set timer program title";
1308 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1309 return "timer cleared status";
1310 case CEC_OPCODE_TIMER_STATUS
:
1311 return "timer status";
1312 case CEC_OPCODE_CEC_VERSION
:
1313 return "cec version";
1314 case CEC_OPCODE_GET_CEC_VERSION
:
1315 return "get cec version";
1316 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1317 return "give physical address";
1318 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1319 return "get menu language";
1320 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1321 return "report physical address";
1322 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1323 return "set menu language";
1324 case CEC_OPCODE_DECK_CONTROL
:
1325 return "deck control";
1326 case CEC_OPCODE_DECK_STATUS
:
1327 return "deck status";
1328 case CEC_OPCODE_GIVE_DECK_STATUS
:
1329 return "give deck status";
1330 case CEC_OPCODE_PLAY
:
1332 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1333 return "give tuner status";
1334 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1335 return "select analogue service";
1336 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1337 return "set digital service";
1338 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1339 return "tuner device status";
1340 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1341 return "tuner step decrement";
1342 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1343 return "tuner step increment";
1344 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1345 return "device vendor id";
1346 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1347 return "give device vendor id";
1348 case CEC_OPCODE_VENDOR_COMMAND
:
1349 return "vendor command";
1350 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1351 return "vendor command with id";
1352 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1353 return "vendor remote button down";
1354 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1355 return "vendor remote button up";
1356 case CEC_OPCODE_SET_OSD_STRING
:
1357 return "set osd string";
1358 case CEC_OPCODE_GIVE_OSD_NAME
:
1359 return "give osd name";
1360 case CEC_OPCODE_SET_OSD_NAME
:
1361 return "set osd name";
1362 case CEC_OPCODE_MENU_REQUEST
:
1363 return "menu request";
1364 case CEC_OPCODE_MENU_STATUS
:
1365 return "menu status";
1366 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1367 return "user control pressed";
1368 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1369 return "user control release";
1370 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1371 return "give device power status";
1372 case CEC_OPCODE_REPORT_POWER_STATUS
:
1373 return "report power status";
1374 case CEC_OPCODE_FEATURE_ABORT
:
1375 return "feature abort";
1376 case CEC_OPCODE_ABORT
:
1378 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1379 return "give audio status";
1380 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1381 return "give audio mode status";
1382 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1383 return "report audio status";
1384 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1385 return "set system audio mode";
1386 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1387 return "system audio mode request";
1388 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1389 return "system audio mode status";
1390 case CEC_OPCODE_SET_AUDIO_RATE
:
1391 return "set audio rate";
1392 case CEC_OPCODE_NONE
:
1399 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1403 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1405 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1412 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1414 // TODO this is a mask
1418 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1422 case CEC_VENDOR_SAMSUNG
:
1426 case CEC_VENDOR_PANASONIC
:
1428 case CEC_VENDOR_PIONEER
:
1430 case CEC_VENDOR_ONKYO
:
1432 case CEC_VENDOR_YAMAHA
:
1434 case CEC_VENDOR_PHILIPS
:
1436 case CEC_VENDOR_SONY
:
1438 case CEC_VENDOR_TOSHIBA
:
1445 const char *CCECProcessor::ToString(const cec_client_version version
)
1449 case CEC_CLIENT_VERSION_PRE_1_5
:
1451 case CEC_CLIENT_VERSION_1_5_0
:
1453 case CEC_CLIENT_VERSION_1_5_1
:
1455 case CEC_CLIENT_VERSION_1_5_2
:
1457 case CEC_CLIENT_VERSION_1_5_3
:
1459 case CEC_CLIENT_VERSION_1_6_0
:
1461 case CEC_CLIENT_VERSION_1_6_1
:
1463 case CEC_CLIENT_VERSION_1_6_2
:
1470 const char *CCECProcessor::ToString(const cec_server_version version
)
1474 case CEC_SERVER_VERSION_PRE_1_5
:
1476 case CEC_SERVER_VERSION_1_5_0
:
1478 case CEC_SERVER_VERSION_1_5_1
:
1480 case CEC_SERVER_VERSION_1_5_2
:
1482 case CEC_SERVER_VERSION_1_5_3
:
1484 case CEC_SERVER_VERSION_1_6_0
:
1486 case CEC_SERVER_VERSION_1_6_1
:
1488 case CEC_SERVER_VERSION_1_6_2
:
1495 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1497 if (!m_communication
&& strPort
)
1499 bool bReturn(false);
1500 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1501 CTimeout
timeout(10000);
1503 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
1505 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1511 bReturn
= comm
->StartBootloader();
1518 return m_communication
->StartBootloader();
1522 bool CCECProcessor::PingAdapter(void)
1524 return m_communication
->PingAdapter();
1527 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1529 if (destination
< CECDEVICE_BROADCAST
)
1530 m_busDevices
[destination
]->HandlePollFrom(initiator
);
1533 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1535 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1538 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1540 // stream path changes are sent by the TV
1541 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1544 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1546 bool bReinit(false);
1547 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1548 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1549 m_configuration
.clientVersion
= configuration
->clientVersion
;
1550 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString((cec_client_version
)configuration
->clientVersion
));
1552 // client version 1.5.0
1555 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1556 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1557 if (bDeviceTypeChanged
)
1558 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(configuration
->deviceTypes
[0]));
1560 bool bPhysicalAddressChanged(false);
1562 // autodetect address
1563 bool bPhysicalAutodetected(false);
1564 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1566 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1567 if (iPhysicalAddress
!= 0)
1570 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1572 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%x'", __FUNCTION__
, iPhysicalAddress
);
1573 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1574 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1575 m_configuration
.iHDMIPort
= 0;
1576 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1577 bPhysicalAutodetected
= true;
1582 if (!bPhysicalAutodetected
)
1584 if (configuration
->iPhysicalAddress
!= 0)
1585 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1586 if (bPhysicalAddressChanged
)
1588 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - physical address '%x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1589 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1593 bool bHdmiPortChanged(false);
1594 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1597 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1598 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1599 m_configuration
.baseDevice
= configuration
->baseDevice
;
1602 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1603 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1604 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1608 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1609 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1610 m_configuration
.iHDMIPort
= 0;
1613 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1616 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, configuration
->strDeviceName
);
1617 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1618 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1620 primary
->SetOSDName(m_configuration
.strDeviceName
);
1621 if (!bReinit
&& IsRunning())
1622 primary
->TransmitOSDName(CECDEVICE_TV
);
1625 // tv vendor id override
1626 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString((cec_vendor_id
)configuration
->tvVendor
));
1627 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1629 m_configuration
.tvVendor
= configuration
->tvVendor
;
1630 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1634 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1636 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1637 if (!bReinit
&& IsRunning())
1642 m_configuration
.bUseTVMenuLanguage
= configuration
->bUseTVMenuLanguage
;
1643 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1644 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1645 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1646 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1647 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1649 // client version 1.5.1
1650 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1651 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1653 // client version 1.6.0
1654 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1656 m_configuration
.bPowerOffDevicesOnStandby
= configuration
->bPowerOffDevicesOnStandby
;
1657 m_configuration
.bShutdownOnStandby
= configuration
->bShutdownOnStandby
;
1660 // client version 1.6.2
1661 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1663 snprintf(m_configuration
.strDeviceLanguage
, 3, "%s", configuration
->strDeviceLanguage
);
1666 // ensure that there is at least 1 device type set
1667 if (m_configuration
.deviceTypes
.IsEmpty())
1668 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1670 // persist the configuration
1672 m_communication
->PersistConfiguration(&m_configuration
);
1674 if (bReinit
|| m_configuration
.logicalAddresses
.IsEmpty())
1676 if (bDeviceTypeChanged
)
1677 return ChangeDeviceType(oldPrimaryType
, m_configuration
.deviceTypes
[0]);
1678 else if (bPhysicalAddressChanged
)
1679 return SetPhysicalAddress(m_configuration
.iPhysicalAddress
);
1681 return SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1683 else if (m_configuration
.bActivateSource
== 1 && IsRunning() && !IsActiveSource(m_configuration
.logicalAddresses
.primary
))
1685 // activate the source if we're not already the active source
1686 SetActiveSource(m_configuration
.deviceTypes
.types
[0]);
1692 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1694 // client version 1.5.0
1695 snprintf(configuration
->strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
1696 configuration
->deviceTypes
= m_configuration
.deviceTypes
;
1697 configuration
->bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
1698 configuration
->iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
1699 configuration
->baseDevice
= m_configuration
.baseDevice
;
1700 configuration
->iHDMIPort
= m_configuration
.iHDMIPort
;
1701 configuration
->clientVersion
= m_configuration
.clientVersion
;
1702 configuration
->serverVersion
= m_configuration
.serverVersion
;
1703 configuration
->tvVendor
= m_configuration
.tvVendor
;
1705 configuration
->bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
1706 configuration
->bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
1707 configuration
->bActivateSource
= m_configuration
.bActivateSource
;
1708 configuration
->wakeDevices
= m_configuration
.wakeDevices
;
1709 configuration
->powerOffDevices
= m_configuration
.powerOffDevices
;
1710 configuration
->bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
1711 configuration
->bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
1713 // client version 1.5.1
1714 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1715 configuration
->bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
1717 // client version 1.5.3
1718 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1719 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1721 // client version 1.6.0
1722 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1724 configuration
->iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
1725 configuration
->bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
1726 configuration
->bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
1732 bool CCECProcessor::CanPersistConfiguration(void)
1734 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
1737 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1739 return m_communication
? m_communication
->PersistConfiguration(configuration
) : false;
1742 void CCECProcessor::RescanActiveDevices(void)
1744 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1745 m_busDevices
[iPtr
]->GetStatus(true);
1748 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = 10000 */)
1750 if (!OpenConnection(strPort
, 38400, iTimeoutMs
, false))
1753 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
1754 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1756 delete m_communication
;
1757 m_communication
= NULL
;
1761 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
1764 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1765 bReturn
&= m_busDevices
[iPtr
]->TransmitPendingActiveSourceCommands();