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 CLibCEC::AddLog(CEC_LOG_NOTICE
, "connected to the CEC adapter. firmware version = %d, client version = %s", m_configuration
.iFirmwareVersion
, ToString((cec_client_version
)m_configuration
.clientVersion
));
194 if (m_configuration
.bGetSettingsFromROM
== 1)
196 libcec_configuration config
;
198 m_communication
->GetConfiguration(&config
);
200 CLockObject
lock(m_mutex
);
201 if (!config
.deviceTypes
.IsEmpty())
202 m_configuration
.deviceTypes
= config
.deviceTypes
;
203 if (config
.iPhysicalAddress
> 0)
204 m_configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
205 snprintf(m_configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
211 bool CCECProcessor::IsInitialised(void)
213 CLockObject
lock(m_threadMutex
);
214 return m_bInitialised
;
217 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
219 CLockObject
lock(m_mutex
);
220 m_bInitialised
= bSetTo
;
223 bool CCECProcessor::Initialise(void)
227 CLockObject
lock(m_mutex
);
228 if (!m_configuration
.logicalAddresses
.IsEmpty())
229 m_configuration
.logicalAddresses
.Clear();
231 if (!FindLogicalAddresses())
233 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
237 /* only set our OSD name for the primary device */
238 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
240 /* make the primary device the active source if the option is set */
241 if (m_configuration
.bActivateSource
== 1)
242 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
244 /* set the default menu language for devices we control */
245 cec_menu_language language
;
246 language
.device
= m_configuration
.logicalAddresses
.primary
;
247 memcpy(language
.language
, m_configuration
.strDeviceLanguage
, 3);
248 language
.language
[3] = 0;
250 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
252 if (m_configuration
.logicalAddresses
[iPtr
])
254 language
.device
= (cec_logical_address
) iPtr
;
255 m_busDevices
[iPtr
]->SetMenuLanguage(language
);
260 /* get the vendor id from the TV, so we are using the correct handler */
261 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
263 if (m_configuration
.iPhysicalAddress
!= 0)
265 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
266 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
267 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
268 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
270 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
271 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
);
273 if (bReturn
&& m_configuration
.bActivateSource
== 1)
274 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
276 SetInitialised(bReturn
);
278 CLibCEC::ConfigurationChanged(m_configuration
);
283 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
288 CLockObject
lock(m_mutex
);
289 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
292 /* create the processor thread */
295 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
300 if ((bReturn
= Initialise()) == false)
302 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
307 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
313 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
315 if (m_busDevices
[address
]->TryLogicalAddress())
317 m_configuration
.logicalAddresses
.Set(address
);
324 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
326 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
327 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
328 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
329 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
332 bool CCECProcessor::FindLogicalAddressTuner(void)
334 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
335 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
336 TryLogicalAddress(CECDEVICE_TUNER2
) ||
337 TryLogicalAddress(CECDEVICE_TUNER3
) ||
338 TryLogicalAddress(CECDEVICE_TUNER4
);
341 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
343 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
344 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
345 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
346 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
349 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
351 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
352 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
355 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
357 bool bChanged(false);
359 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
361 CLockObject
lock(m_mutex
);
362 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
363 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
365 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
367 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
370 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
373 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
375 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
377 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
383 FindLogicalAddresses();
385 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
386 if (previousDevice
&& newDevice
)
388 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
389 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
391 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
392 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
394 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
396 newDevice
->SetMenuState(previousDevice
->GetMenuState());
397 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
399 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
400 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
402 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
403 previousDevice
->SetPhysicalAddress(0xFFFF);
405 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
406 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
408 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
409 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
411 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
412 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
414 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
415 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
417 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
418 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
426 bool CCECProcessor::FindLogicalAddresses(void)
429 m_configuration
.logicalAddresses
.Clear();
431 if (m_configuration
.deviceTypes
.IsEmpty())
433 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
437 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
439 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
442 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
444 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
445 bReturn
&= FindLogicalAddressRecordingDevice();
446 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
447 bReturn
&= FindLogicalAddressTuner();
448 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
449 bReturn
&= FindLogicalAddressPlaybackDevice();
450 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
451 bReturn
&= FindLogicalAddressAudioSystem();
455 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
460 void CCECProcessor::ReplaceHandlers(void)
462 if (!IsInitialised())
464 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
465 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
468 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
470 return m_inBuffer
.Push(command
);
473 void *CCECProcessor::Process(void)
475 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
480 while (!IsStopped() && m_communication
->IsOpen())
482 if (m_inBuffer
.Pop(command
, 500))
483 ParseCommand(command
);
489 m_controller
->CheckKeypressTimeout();
496 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
503 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
505 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
507 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
509 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
511 addr
= (cec_logical_address
) iPtr
;
517 m_busDevices
[addr
]->SetActiveSource();
518 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
519 bReturn
= m_busDevices
[addr
]->ActivateSource();
524 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
528 // suppress polls when searching for a device
529 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
, false, true);
532 device
->SetActiveSource();
539 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
541 CLockObject
lock(m_mutex
);
542 m_iStandardLineTimeout
= iTimeout
;
545 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
547 CLockObject
lock(m_mutex
);
548 m_iRetryLineTimeout
= iTimeout
;
551 bool CCECProcessor::SetActiveView(void)
553 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
554 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
557 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
561 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
564 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
566 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
573 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
577 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
580 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
582 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
589 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
593 // limit the HDMI port range to 1-15
600 CLockObject
lock(m_mutex
);
601 m_configuration
.baseDevice
= iBaseDevice
;
602 m_configuration
.iHDMIPort
= iPort
;
605 if (!IsRunning() && !bForce
)
608 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
610 uint16_t iPhysicalAddress(0);
611 if (iBaseDevice
> CECDEVICE_TV
)
613 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
616 if (iPhysicalAddress
< 0xffff)
618 if (iPhysicalAddress
== 0)
619 iPhysicalAddress
+= 0x1000 * iPort
;
620 else if (iPhysicalAddress
% 0x1000 == 0)
621 iPhysicalAddress
+= 0x100 * iPort
;
622 else if (iPhysicalAddress
% 0x100 == 0)
623 iPhysicalAddress
+= 0x10 * iPort
;
624 else if (iPhysicalAddress
% 0x10 == 0)
625 iPhysicalAddress
+= iPort
;
631 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
633 SetPhysicalAddress(iPhysicalAddress
);
638 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
640 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
642 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
648 bool CCECProcessor::TransmitInactiveSource(void)
653 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
654 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
658 void CCECProcessor::LogOutput(const cec_command
&data
)
661 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
663 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
665 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
666 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
667 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
670 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
672 CLockObject
lock(m_mutex
);
673 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
675 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
676 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
677 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
678 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
684 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
686 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
688 if (m_configuration
.logicalAddresses
[iPtr
])
689 m_busDevices
[iPtr
]->SetMenuState(state
);
693 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
698 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
700 bool bSendActiveView(false);
702 cec_logical_addresses sendUpdatesTo
;
703 sendUpdatesTo
.Clear();
706 CLockObject
lock(m_mutex
);
707 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
708 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
710 if (!m_configuration
.logicalAddresses
.IsEmpty())
712 bool bWasActiveSource(false);
713 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
714 if (m_configuration
.logicalAddresses
[iPtr
])
716 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
717 m_busDevices
[iPtr
]->SetInactiveSource();
718 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
720 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
723 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
728 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
729 if (sendUpdatesTo
[iPtr
])
730 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
737 libcec_configuration config
;
739 CLockObject
lock(m_mutex
);
740 config
= m_configuration
;
743 PersistConfiguration(&config
);
744 CLibCEC::ConfigurationChanged(config
);
750 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
752 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
755 CLockObject
lock(m_mutex
);
756 m_bMonitor
= bEnable
;
760 return SetAckMask(0);
762 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
765 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
767 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
769 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
770 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
771 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
776 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
779 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
780 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
785 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
788 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
789 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
794 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
797 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
798 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
803 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */, bool bSuppressPoll
/* = false */) const
805 if (m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
806 return m_busDevices
[m_configuration
.logicalAddresses
.primary
];
808 CCECBusDevice
*device
= NULL
;
809 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
811 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
, bSuppressPoll
) == iPhysicalAddress
)
813 device
= m_busDevices
[iPtr
];
821 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
823 CCECBusDevice
*device
= NULL
;
825 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
827 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
829 device
= m_busDevices
[iPtr
];
837 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
839 CCECBusDevice
*device(NULL
);
840 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
841 if (primary
!= CECDEVICE_UNKNOWN
)
842 device
= m_busDevices
[primary
];
846 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
848 return m_busDevices
[iAddress
]->GetCecVersion();
851 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
853 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
856 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
857 retVal
.device
= iAddress
;
862 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
864 if (m_busDevices
[iAddress
])
866 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
867 return (strcmp(language
->language
, "???") != 0);
872 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
874 if (m_busDevices
[iAddress
])
875 return m_busDevices
[iAddress
]->GetVendorId();
879 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
881 if (m_busDevices
[iAddress
])
882 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
886 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
888 if (m_busDevices
[iAddress
])
889 return m_busDevices
[iAddress
]->GetPowerStatus();
890 return CEC_POWER_STATUS_UNKNOWN
;
893 cec_logical_address
CCECProcessor::GetActiveSource(void)
895 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
897 if (m_busDevices
[iPtr
]->IsActiveSource())
898 return (cec_logical_address
)iPtr
;
901 return CECDEVICE_UNKNOWN
;
904 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
906 return iAddress
> CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
?
907 m_busDevices
[iAddress
]->IsActiveSource() :
911 bool CCECProcessor::Transmit(const cec_command
&data
)
913 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
915 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
919 uint8_t iMaxTries(0);
921 CLockObject
lock(m_mutex
);
925 m_iLastTransmission
= GetTimeMs();
926 if (!m_communication
|| !m_communication
->IsOpen())
928 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
931 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
936 uint8_t iLineTimeout
= m_iStandardLineTimeout
;
937 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
939 while (bRetry
&& ++iTries
< iMaxTries
)
941 if (m_busDevices
[data
.initiator
]->IsUnsupportedFeature(data
.opcode
))
944 adapterState
= m_communication
->Write(data
, bRetry
, iLineTimeout
);
945 iLineTimeout
= m_iRetryLineTimeout
;
948 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
951 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
953 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
957 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
958 command
.parameters
.PushBack((uint8_t)opcode
);
959 command
.parameters
.PushBack((uint8_t)reason
);
964 void CCECProcessor::ParseCommand(const cec_command
&command
)
967 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
968 if (command
.opcode_set
== 1)
969 dataStr
.AppendFormat(":%02x", command
.opcode
);
970 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
971 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
972 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
974 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
975 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
978 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
980 cec_logical_addresses addresses
;
982 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
984 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
985 addresses
.Set((cec_logical_address
) iPtr
);
990 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
992 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
995 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
997 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
999 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1006 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1008 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
1009 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false);
1013 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1015 return m_communication
? m_communication
->SetAckMask(iMask
) : false;
1018 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1020 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1023 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1025 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1028 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1030 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
1031 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1032 if (iPhysicalAddress
!= 0)
1034 m_configuration
.bAutodetectAddress
= 1;
1035 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1036 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1037 m_configuration
.iHDMIPort
= 0;
1038 return SetPhysicalAddress(iPhysicalAddress
);
1043 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1045 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1048 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1050 if (m_configuration
.powerOffDevices
[iPtr
])
1052 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - putting '%s' in standby mode", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1053 bReturn
&= m_busDevices
[iPtr
]->Standby();
1059 return m_busDevices
[address
]->Standby();
1062 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1064 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1067 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1069 if (m_configuration
.wakeDevices
[iPtr
])
1071 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - powering on '%s'", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1072 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1078 return m_busDevices
[address
]->PowerOn();
1081 const char *CCECProcessor::ToString(const cec_device_type type
)
1085 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1086 return "audio system";
1087 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1088 return "playback device";
1089 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1090 return "recording device";
1091 case CEC_DEVICE_TYPE_RESERVED
:
1093 case CEC_DEVICE_TYPE_TUNER
:
1095 case CEC_DEVICE_TYPE_TV
:
1102 const char *CCECProcessor::ToString(const cec_menu_state state
)
1106 case CEC_MENU_STATE_ACTIVATED
:
1108 case CEC_MENU_STATE_DEACTIVATED
:
1109 return "deactivated";
1115 const char *CCECProcessor::ToString(const cec_version version
)
1119 case CEC_VERSION_1_2
:
1121 case CEC_VERSION_1_2A
:
1123 case CEC_VERSION_1_3
:
1125 case CEC_VERSION_1_3A
:
1127 case CEC_VERSION_1_4
:
1134 const char *CCECProcessor::ToString(const cec_power_status status
)
1138 case CEC_POWER_STATUS_ON
:
1140 case CEC_POWER_STATUS_STANDBY
:
1142 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1143 return "in transition from on to standby";
1144 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1145 return "in transition from standby to on";
1151 const char *CCECProcessor::ToString(const cec_logical_address address
)
1155 case CECDEVICE_AUDIOSYSTEM
:
1157 case CECDEVICE_BROADCAST
:
1159 case CECDEVICE_FREEUSE
:
1161 case CECDEVICE_PLAYBACKDEVICE1
:
1162 return "Playback 1";
1163 case CECDEVICE_PLAYBACKDEVICE2
:
1164 return "Playback 2";
1165 case CECDEVICE_PLAYBACKDEVICE3
:
1166 return "Playback 3";
1167 case CECDEVICE_RECORDINGDEVICE1
:
1168 return "Recorder 1";
1169 case CECDEVICE_RECORDINGDEVICE2
:
1170 return "Recorder 2";
1171 case CECDEVICE_RECORDINGDEVICE3
:
1172 return "Recorder 3";
1173 case CECDEVICE_RESERVED1
:
1174 return "Reserved 1";
1175 case CECDEVICE_RESERVED2
:
1176 return "Reserved 2";
1177 case CECDEVICE_TUNER1
:
1179 case CECDEVICE_TUNER2
:
1181 case CECDEVICE_TUNER3
:
1183 case CECDEVICE_TUNER4
:
1192 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1196 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1197 return "skip forward wind";
1198 case CEC_DECK_CONTROL_MODE_EJECT
:
1200 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1201 return "reverse rewind";
1202 case CEC_DECK_CONTROL_MODE_STOP
:
1209 const char *CCECProcessor::ToString(const cec_deck_info status
)
1213 case CEC_DECK_INFO_PLAY
:
1215 case CEC_DECK_INFO_RECORD
:
1217 case CEC_DECK_INFO_PLAY_REVERSE
:
1218 return "play reverse";
1219 case CEC_DECK_INFO_STILL
:
1221 case CEC_DECK_INFO_SLOW
:
1223 case CEC_DECK_INFO_SLOW_REVERSE
:
1224 return "slow reverse";
1225 case CEC_DECK_INFO_FAST_FORWARD
:
1226 return "fast forward";
1227 case CEC_DECK_INFO_FAST_REVERSE
:
1228 return "fast reverse";
1229 case CEC_DECK_INFO_NO_MEDIA
:
1231 case CEC_DECK_INFO_STOP
:
1233 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1234 return "info skip forward wind";
1235 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1236 return "info skip reverse rewind";
1237 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1238 return "info index search forward";
1239 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1240 return "info index search reverse";
1241 case CEC_DECK_INFO_OTHER_STATUS
:
1248 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1252 case CEC_OPCODE_ACTIVE_SOURCE
:
1253 return "active source";
1254 case CEC_OPCODE_IMAGE_VIEW_ON
:
1255 return "image view on";
1256 case CEC_OPCODE_TEXT_VIEW_ON
:
1257 return "text view on";
1258 case CEC_OPCODE_INACTIVE_SOURCE
:
1259 return "inactive source";
1260 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1261 return "request active source";
1262 case CEC_OPCODE_ROUTING_CHANGE
:
1263 return "routing change";
1264 case CEC_OPCODE_ROUTING_INFORMATION
:
1265 return "routing information";
1266 case CEC_OPCODE_SET_STREAM_PATH
:
1267 return "set stream path";
1268 case CEC_OPCODE_STANDBY
:
1270 case CEC_OPCODE_RECORD_OFF
:
1271 return "record off";
1272 case CEC_OPCODE_RECORD_ON
:
1274 case CEC_OPCODE_RECORD_STATUS
:
1275 return "record status";
1276 case CEC_OPCODE_RECORD_TV_SCREEN
:
1277 return "record tv screen";
1278 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1279 return "clear analogue timer";
1280 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1281 return "clear digital timer";
1282 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1283 return "clear external timer";
1284 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1285 return "set analogue timer";
1286 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1287 return "set digital timer";
1288 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1289 return "set external timer";
1290 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1291 return "set timer program title";
1292 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1293 return "timer cleared status";
1294 case CEC_OPCODE_TIMER_STATUS
:
1295 return "timer status";
1296 case CEC_OPCODE_CEC_VERSION
:
1297 return "cec version";
1298 case CEC_OPCODE_GET_CEC_VERSION
:
1299 return "get cec version";
1300 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1301 return "give physical address";
1302 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1303 return "get menu language";
1304 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1305 return "report physical address";
1306 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1307 return "set menu language";
1308 case CEC_OPCODE_DECK_CONTROL
:
1309 return "deck control";
1310 case CEC_OPCODE_DECK_STATUS
:
1311 return "deck status";
1312 case CEC_OPCODE_GIVE_DECK_STATUS
:
1313 return "give deck status";
1314 case CEC_OPCODE_PLAY
:
1316 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1317 return "give tuner status";
1318 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1319 return "select analogue service";
1320 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1321 return "set digital service";
1322 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1323 return "tuner device status";
1324 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1325 return "tuner step decrement";
1326 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1327 return "tuner step increment";
1328 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1329 return "device vendor id";
1330 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1331 return "give device vendor id";
1332 case CEC_OPCODE_VENDOR_COMMAND
:
1333 return "vendor command";
1334 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1335 return "vendor command with id";
1336 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1337 return "vendor remote button down";
1338 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1339 return "vendor remote button up";
1340 case CEC_OPCODE_SET_OSD_STRING
:
1341 return "set osd string";
1342 case CEC_OPCODE_GIVE_OSD_NAME
:
1343 return "give osd name";
1344 case CEC_OPCODE_SET_OSD_NAME
:
1345 return "set osd name";
1346 case CEC_OPCODE_MENU_REQUEST
:
1347 return "menu request";
1348 case CEC_OPCODE_MENU_STATUS
:
1349 return "menu status";
1350 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1351 return "user control pressed";
1352 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1353 return "user control release";
1354 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1355 return "give device power status";
1356 case CEC_OPCODE_REPORT_POWER_STATUS
:
1357 return "report power status";
1358 case CEC_OPCODE_FEATURE_ABORT
:
1359 return "feature abort";
1360 case CEC_OPCODE_ABORT
:
1362 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1363 return "give audio status";
1364 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1365 return "give audio mode status";
1366 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1367 return "report audio status";
1368 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1369 return "set system audio mode";
1370 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1371 return "system audio mode request";
1372 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1373 return "system audio mode status";
1374 case CEC_OPCODE_SET_AUDIO_RATE
:
1375 return "set audio rate";
1376 case CEC_OPCODE_NONE
:
1383 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1387 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1389 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1396 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1398 // TODO this is a mask
1402 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1406 case CEC_VENDOR_SAMSUNG
:
1410 case CEC_VENDOR_PANASONIC
:
1412 case CEC_VENDOR_PIONEER
:
1414 case CEC_VENDOR_ONKYO
:
1416 case CEC_VENDOR_YAMAHA
:
1418 case CEC_VENDOR_PHILIPS
:
1420 case CEC_VENDOR_SONY
:
1422 case CEC_VENDOR_TOSHIBA
:
1429 const char *CCECProcessor::ToString(const cec_client_version version
)
1433 case CEC_CLIENT_VERSION_PRE_1_5
:
1435 case CEC_CLIENT_VERSION_1_5_0
:
1437 case CEC_CLIENT_VERSION_1_5_1
:
1439 case CEC_CLIENT_VERSION_1_5_2
:
1441 case CEC_CLIENT_VERSION_1_5_3
:
1443 case CEC_CLIENT_VERSION_1_6_0
:
1445 case CEC_CLIENT_VERSION_1_6_1
:
1447 case CEC_CLIENT_VERSION_1_6_2
:
1454 const char *CCECProcessor::ToString(const cec_server_version version
)
1458 case CEC_SERVER_VERSION_PRE_1_5
:
1460 case CEC_SERVER_VERSION_1_5_0
:
1462 case CEC_SERVER_VERSION_1_5_1
:
1464 case CEC_SERVER_VERSION_1_5_2
:
1466 case CEC_SERVER_VERSION_1_5_3
:
1468 case CEC_SERVER_VERSION_1_6_0
:
1470 case CEC_SERVER_VERSION_1_6_1
:
1472 case CEC_SERVER_VERSION_1_6_2
:
1479 void *CCECBusScan::Process(void)
1481 CCECBusDevice
*device(NULL
);
1482 uint8_t iCounter(0);
1484 while (!IsStopped())
1486 if (++iCounter
< 10)
1491 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1493 device
= m_processor
->m_busDevices
[iPtr
];
1495 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1499 device
->GetVendorId();
1503 device
->GetPowerStatus(true);
1511 void CCECBusScan::WaitUntilIdle(void)
1516 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1517 while (iWaitTime
> 0)
1520 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1524 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1526 if (!m_communication
&& strPort
)
1528 bool bReturn(false);
1529 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1530 CTimeout
timeout(10000);
1532 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
1534 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1540 bReturn
= comm
->StartBootloader();
1547 return m_communication
->StartBootloader();
1551 bool CCECProcessor::PingAdapter(void)
1553 return m_communication
->PingAdapter();
1556 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1558 if (destination
< CECDEVICE_BROADCAST
)
1559 m_busDevices
[destination
]->HandlePollFrom(initiator
);
1562 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1564 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1567 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1569 // stream path changes are sent by the TV
1570 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1573 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1575 bool bReinit(false);
1576 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1577 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1578 m_configuration
.clientVersion
= configuration
->clientVersion
;
1579 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString((cec_client_version
)configuration
->clientVersion
));
1581 // client version 1.5.0
1584 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1585 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1586 if (bDeviceTypeChanged
)
1587 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(configuration
->deviceTypes
[0]));
1589 bool bPhysicalAddressChanged(false);
1591 // autodetect address
1592 bool bPhysicalAutodetected(false);
1593 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1595 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1596 if (iPhysicalAddress
!= 0)
1599 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1601 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%x'", __FUNCTION__
, iPhysicalAddress
);
1602 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1603 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1604 m_configuration
.iHDMIPort
= 0;
1605 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1606 bPhysicalAutodetected
= true;
1611 if (!bPhysicalAutodetected
)
1613 if (configuration
->iPhysicalAddress
!= 0)
1614 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1615 if (bPhysicalAddressChanged
)
1617 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - physical address '%x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1618 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1622 bool bHdmiPortChanged(false);
1623 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1626 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1627 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1628 m_configuration
.baseDevice
= configuration
->baseDevice
;
1631 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1632 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1633 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1637 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1638 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1639 m_configuration
.iHDMIPort
= 0;
1642 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1645 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, configuration
->strDeviceName
);
1646 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1647 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1649 primary
->SetOSDName(m_configuration
.strDeviceName
);
1650 if (!bReinit
&& IsRunning())
1651 primary
->TransmitOSDName(CECDEVICE_TV
);
1654 // tv vendor id override
1655 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString((cec_vendor_id
)configuration
->tvVendor
));
1656 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1658 m_configuration
.tvVendor
= configuration
->tvVendor
;
1659 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1663 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1665 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1666 if (!bReinit
&& IsRunning())
1671 m_configuration
.bUseTVMenuLanguage
= configuration
->bUseTVMenuLanguage
;
1672 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1673 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1674 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1675 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1676 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1678 // client version 1.5.1
1679 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1680 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1682 // client version 1.6.0
1683 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1685 m_configuration
.bPowerOffDevicesOnStandby
= configuration
->bPowerOffDevicesOnStandby
;
1686 m_configuration
.bShutdownOnStandby
= configuration
->bShutdownOnStandby
;
1689 // client version 1.6.2
1690 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1692 snprintf(m_configuration
.strDeviceLanguage
, 3, "%s", configuration
->strDeviceLanguage
);
1695 // ensure that there is at least 1 device type set
1696 if (m_configuration
.deviceTypes
.IsEmpty())
1697 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1699 // persist the configuration
1701 m_communication
->PersistConfiguration(&m_configuration
);
1703 if (bReinit
|| m_configuration
.logicalAddresses
.IsEmpty())
1705 if (bDeviceTypeChanged
)
1706 return ChangeDeviceType(oldPrimaryType
, m_configuration
.deviceTypes
[0]);
1707 else if (bPhysicalAddressChanged
)
1708 return SetPhysicalAddress(m_configuration
.iPhysicalAddress
);
1710 return SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1712 else if (m_configuration
.bActivateSource
== 1 && IsRunning() && !IsActiveSource(m_configuration
.logicalAddresses
.primary
))
1714 // activate the source if we're not already the active source
1715 SetActiveSource(m_configuration
.deviceTypes
.types
[0]);
1721 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1723 // client version 1.5.0
1724 snprintf(configuration
->strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
1725 configuration
->deviceTypes
= m_configuration
.deviceTypes
;
1726 configuration
->bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
1727 configuration
->iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
1728 configuration
->baseDevice
= m_configuration
.baseDevice
;
1729 configuration
->iHDMIPort
= m_configuration
.iHDMIPort
;
1730 configuration
->clientVersion
= m_configuration
.clientVersion
;
1731 configuration
->serverVersion
= m_configuration
.serverVersion
;
1732 configuration
->tvVendor
= m_configuration
.tvVendor
;
1734 configuration
->bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
1735 configuration
->bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
1736 configuration
->bActivateSource
= m_configuration
.bActivateSource
;
1737 configuration
->wakeDevices
= m_configuration
.wakeDevices
;
1738 configuration
->powerOffDevices
= m_configuration
.powerOffDevices
;
1739 configuration
->bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
1740 configuration
->bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
1742 // client version 1.5.1
1743 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1744 configuration
->bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
1746 // client version 1.5.3
1747 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1748 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1750 // client version 1.6.0
1751 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1753 configuration
->iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
1754 configuration
->bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
1755 configuration
->bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
1761 bool CCECProcessor::CanPersistConfiguration(void)
1763 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
1766 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1768 return m_communication
? m_communication
->PersistConfiguration(configuration
) : false;
1771 void CCECProcessor::RescanActiveDevices(void)
1773 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
1774 m_busDevices
[iPtr
]->GetStatus(true);
1777 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = 10000 */)
1779 if (!OpenConnection(strPort
, 38400, iTimeoutMs
, false))
1782 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
1783 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1785 delete m_communication
;
1786 m_communication
= NULL
;