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/
34 #include "CECProcessor.h"
36 #include "adapter/AdapterFactory.h"
37 #include "devices/CECBusDevice.h"
38 #include "devices/CECAudioSystem.h"
39 #include "devices/CECPlaybackDevice.h"
40 #include "devices/CECRecordingDevice.h"
41 #include "devices/CECTuner.h"
42 #include "devices/CECTV.h"
43 #include "implementations/CECCommandHandler.h"
45 #include "CECClient.h"
46 #include "CECTypeUtils.h"
47 #include "platform/util/timeutils.h"
48 #include "platform/util/util.h"
52 using namespace PLATFORM
;
54 #define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
55 #define ACTIVE_SOURCE_CHECK_INTERVAL 500
57 #define ToString(x) CCECTypeUtils::ToString(x)
59 CCECProcessor::CCECProcessor(CLibCEC
*libcec
) :
60 m_bInitialised(false),
61 m_communication(NULL
),
63 m_iStandardLineTimeout(3),
64 m_iRetryLineTimeout(3),
65 m_iLastTransmission(0),
68 m_busDevices
= new CCECDeviceMap(this);
71 CCECProcessor::~CCECProcessor(void)
74 DELETE_AND_NULL(m_busDevices
);
77 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
79 CLockObject
lock(m_mutex
);
81 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
84 // create the processor thread
89 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
97 void CCECProcessor::Close(void)
99 // mark as uninitialised
100 SetCECInitialised(false);
102 // stop the processor
105 // close the connection
106 DELETE_AND_NULL(m_communication
);
109 void CCECProcessor::ResetMembers(void)
111 // close the connection
112 DELETE_AND_NULL(m_communication
);
114 // reset the other members to the initial state
115 m_iStandardLineTimeout
= 3;
116 m_iRetryLineTimeout
= 3;
117 m_iLastTransmission
= 0;
118 m_busDevices
->ResetDeviceStatus();
121 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
124 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
126 // ensure that a previous connection is closed
129 // reset all member to the initial state
132 // check whether the Close() method deleted any previous connection
135 m_libcec
->AddLog(CEC_LOG_ERROR
, "previous connection could not be closed");
139 // create a new connection
140 m_communication
= CAdapterFactory(this->m_libcec
).GetInstance(strPort
, iBaudRate
);
142 // open a new connection
143 unsigned iConnectTry(0);
144 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
146 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
147 m_communication
->Close();
148 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
151 m_libcec
->AddLog(CEC_LOG_NOTICE
, "connection opened");
153 // mark as initialised
154 SetCECInitialised(true);
159 bool CCECProcessor::CECInitialised(void)
161 CLockObject
lock(m_threadMutex
);
162 return m_bInitialised
;
165 void CCECProcessor::SetCECInitialised(bool bSetTo
/* = true */)
168 CLockObject
lock(m_mutex
);
169 m_bInitialised
= bSetTo
;
175 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, cec_version libCECSpecVersion
/* = CEC_VERSION_1_4 */)
178 CCECBusDevice
*device
= m_busDevices
->At(address
);
181 // check if it's already marked as present or used
182 if (device
->IsPresent() || device
->IsHandledByLibCEC())
185 // poll the LA if not
186 return device
->TryLogicalAddress(libCECSpecVersion
);
192 void CCECProcessor::ReplaceHandlers(void)
194 if (!CECInitialised())
198 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
199 it
->second
->ReplaceHandler(true);
202 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
204 return m_inBuffer
.Push(command
);
207 void *CCECProcessor::Process(void)
209 m_libcec
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
211 cec_command command
; command
.Clear();
212 CTimeout
activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL
);
214 // as long as we're not being stopped and the connection is open
215 while (!IsStopped() && m_communication
->IsOpen())
217 // wait for a new incoming command, and process it
218 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
219 ProcessCommand(command
);
221 if (CECInitialised())
223 // check clients for keypress timeouts
224 m_libcec
->CheckKeypressTimeout();
226 // check if we need to replace handlers
229 // check whether we need to activate a source, if it failed before
230 if (activeSourceCheck
.TimeLeft() == 0)
232 if (CECInitialised())
233 TransmitPendingActiveSourceCommands();
234 activeSourceCheck
.Init(ACTIVE_SOURCE_CHECK_INTERVAL
);
242 bool CCECProcessor::ActivateSource(uint16_t iStreamPath
)
246 // find the device with the given PA
247 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
248 // and make it the active source when found
250 bReturn
= device
->ActivateSource();
252 m_libcec
->AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
257 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
259 CLockObject
lock(m_mutex
);
260 m_iStandardLineTimeout
= iTimeout
;
263 uint8_t CCECProcessor::GetStandardLineTimeout(void)
265 CLockObject
lock(m_mutex
);
266 return m_iStandardLineTimeout
;
269 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
271 CLockObject
lock(m_mutex
);
272 m_iRetryLineTimeout
= iTimeout
;
275 uint8_t CCECProcessor::GetRetryLineTimeout(void)
277 CLockObject
lock(m_mutex
);
278 return m_iRetryLineTimeout
;
281 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
283 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iPhysicalAddress
);
284 return device
!= NULL
;
287 void CCECProcessor::LogOutput(const cec_command
&data
)
291 // initiator and destination
292 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
296 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
298 // append the parameters
299 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
300 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
303 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
306 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
308 // try to find the primary device
309 CCECBusDevice
*primary
= GetPrimaryDevice();
310 // poll the destination, with the primary as source
312 return primary
->TransmitPoll(iAddress
, false);
314 CCECBusDevice
*device
= m_busDevices
->At(CECDEVICE_UNREGISTERED
);
316 return device
->TransmitPoll(iAddress
, false);
321 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
323 return m_busDevices
?
324 m_busDevices
->GetDeviceByPhysicalAddress(iPhysicalAddress
, bSuppressUpdate
) :
328 CCECBusDevice
*CCECProcessor::GetDevice(cec_logical_address address
) const
330 return m_busDevices
?
331 m_busDevices
->At(address
) :
335 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
337 // get the device that is marked as active source from the device map
338 CCECBusDevice
*activeSource
= m_busDevices
->GetActiveSource();
340 return activeSource
->GetLogicalAddress();
342 if (bRequestActiveSource
)
344 // request the active source from the bus
345 CCECBusDevice
*primary
= GetPrimaryDevice();
348 primary
->RequestActiveSource();
349 return GetActiveSource(false);
354 return CECDEVICE_UNKNOWN
;
357 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
359 CCECBusDevice
*device
= m_busDevices
->At(iAddress
);
360 return device
&& device
->IsActiveSource();
363 bool CCECProcessor::Transmit(const cec_command
&data
, bool bIsReply
)
365 cec_command
transmitData(data
);
366 uint8_t iMaxTries(0);
370 // get the current timeout setting
371 uint8_t iLineTimeout(GetStandardLineTimeout());
373 // reset the state of this message to 'unknown'
374 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
376 if (!m_communication
->SupportsSourceLogicalAddress(transmitData
.initiator
))
378 if (transmitData
.initiator
== CECDEVICE_UNREGISTERED
&& m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
380 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData
.initiator
), ToString(CECDEVICE_FREEUSE
));
381 transmitData
.initiator
= CECDEVICE_FREEUSE
;
385 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData
.initiator
));
390 LogOutput(transmitData
);
392 // find the initiator device
393 CCECBusDevice
*initiator
= m_busDevices
->At(transmitData
.initiator
);
396 m_libcec
->AddLog(CEC_LOG_WARNING
, "invalid initiator");
400 // find the destination device, if it's not the broadcast address
401 if (transmitData
.destination
!= CECDEVICE_BROADCAST
)
403 // check if the device is marked as handled by libCEC
404 CCECBusDevice
*destination
= m_busDevices
->At(transmitData
.destination
);
405 if (destination
&& destination
->IsHandledByLibCEC())
407 // and reject the command if it's trying to send data to a device that is handled by libCEC
408 m_libcec
->AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
414 CLockObject
lock(m_mutex
);
415 m_iLastTransmission
= GetTimeMs();
416 // set the number of tries
417 iMaxTries
= initiator
->GetHandler()->GetTransmitRetries() + 1;
418 initiator
->MarkHandlerReady();
421 // and try to send the command
422 while (bRetry
&& ++iTries
< iMaxTries
)
424 if (initiator
->IsUnsupportedFeature(transmitData
.opcode
))
427 adapterState
= !IsStopped() && m_communication
&& m_communication
->IsOpen() ?
428 m_communication
->Write(transmitData
, bRetry
, iLineTimeout
, bIsReply
) :
429 ADAPTER_MESSAGE_STATE_ERROR
;
430 iLineTimeout
= m_iRetryLineTimeout
;
433 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
436 void CCECProcessor::TransmitAbort(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
438 m_libcec
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
441 cec_command::Format(command
, source
, destination
, CEC_OPCODE_FEATURE_ABORT
);
442 command
.parameters
.PushBack((uint8_t)opcode
);
443 command
.parameters
.PushBack((uint8_t)reason
);
445 Transmit(command
, true);
448 void CCECProcessor::ProcessCommand(const cec_command
&command
)
452 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
453 if (command
.opcode_set
== 1)
454 dataStr
.AppendFormat(":%02x", command
.opcode
);
455 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
456 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
457 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
459 // find the initiator
460 CCECBusDevice
*device
= m_busDevices
->At(command
.initiator
);
463 device
->HandleCommand(command
);
466 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
468 CCECBusDevice
*device
= m_busDevices
->At(address
);
469 return device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
472 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
474 CECDEVICEVEC devices
;
475 m_busDevices
->GetByType(type
, devices
);
476 CCECDeviceMap::FilterActive(devices
);
477 return !devices
.empty();
480 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
482 return m_communication
? m_communication
->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
485 bool CCECProcessor::ClearLogicalAddresses(void)
487 cec_logical_addresses addresses
; addresses
.Clear();
488 return SetLogicalAddresses(addresses
);
491 bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
493 return m_communication
? m_communication
->SetLogicalAddresses(addresses
) : false;
496 bool CCECProcessor::StandbyDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
499 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
500 bReturn
&= (*it
)->Standby(initiator
);
504 bool CCECProcessor::StandbyDevice(const cec_logical_address initiator
, cec_logical_address address
)
506 CCECBusDevice
*device
= m_busDevices
->At(address
);
507 return device
? device
->Standby(initiator
) : false;
510 bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
513 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
514 bReturn
&= (*it
)->PowerOn(initiator
);
518 bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator
, cec_logical_address address
)
520 CCECBusDevice
*device
= m_busDevices
->At(address
);
521 return device
? device
->PowerOn(initiator
) : false;
524 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
527 // open a connection if no connection has been opened
528 if (!m_communication
&& strPort
)
530 CAdapterFactory
factory(this->m_libcec
);
531 IAdapterCommunication
*comm
= factory
.GetInstance(strPort
);
532 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
534 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
536 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
538 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
542 bReturn
= comm
->StartBootloader();
543 DELETE_AND_NULL(comm
);
549 m_communication
->StartBootloader();
557 bool CCECProcessor::PingAdapter(void)
559 return m_communication
->PingAdapter();
562 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
564 CCECBusDevice
*device
= m_busDevices
->At(destination
);
566 device
->HandlePollFrom(initiator
);
569 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
571 CCECBusDevice
*device
= m_busDevices
->At(initiator
);
572 return !device
|| !device
->HandleReceiveFailed();
575 bool CCECProcessor::CanPersistConfiguration(void)
577 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
580 bool CCECProcessor::PersistConfiguration(const libcec_configuration
&configuration
)
582 libcec_configuration persistConfiguration
= configuration
;
583 if (!CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
585 CCECBusDevice
*device
= GetPrimaryDevice();
587 persistConfiguration
.iPhysicalAddress
= device
->GetCurrentPhysicalAddress();
590 return m_communication
? m_communication
->PersistConfiguration(persistConfiguration
) : false;
593 void CCECProcessor::RescanActiveDevices(void)
595 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
596 it
->second
->GetStatus(true);
599 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
601 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
604 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
605 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
606 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
611 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
614 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
615 bReturn
&= it
->second
->TransmitPendingActiveSourceCommands();
619 CCECTV
*CCECProcessor::GetTV(void) const
621 return CCECBusDevice::AsTV(m_busDevices
->At(CECDEVICE_TV
));
624 CCECAudioSystem
*CCECProcessor::GetAudioSystem(void) const
626 return CCECBusDevice::AsAudioSystem(m_busDevices
->At(CECDEVICE_AUDIOSYSTEM
));
629 CCECPlaybackDevice
*CCECProcessor::GetPlaybackDevice(cec_logical_address address
) const
631 return CCECBusDevice::AsPlaybackDevice(m_busDevices
->At(address
));
634 CCECRecordingDevice
*CCECProcessor::GetRecordingDevice(cec_logical_address address
) const
636 return CCECBusDevice::AsRecordingDevice(m_busDevices
->At(address
));
639 CCECTuner
*CCECProcessor::GetTuner(cec_logical_address address
) const
641 return CCECBusDevice::AsTuner(m_busDevices
->At(address
));
644 bool CCECProcessor::RegisterClient(CCECClient
*client
)
649 libcec_configuration
&configuration
= *client
->GetConfiguration();
651 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_3
&& configuration
.bMonitorOnly
== 1)
654 if (!CECInitialised())
656 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: CEC processor is not initialised");
660 // unregister the client first if it's already been marked as registered
661 if (client
->IsRegistered())
662 UnregisterClient(client
);
664 // ensure that controlled mode is enabled
665 m_communication
->SetControlledMode(true);
667 // ensure that we know the vendor id of the TV
668 CCECBusDevice
*tv
= GetTV();
669 cec_vendor_id tvVendor
= CEC_VENDOR_UNKNOWN
;
670 if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED
))
671 tvVendor
= tv
->GetVendorId(CECDEVICE_UNREGISTERED
);
672 else if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
673 tvVendor
= tv
->GetVendorId(CECDEVICE_FREEUSE
);
675 // wait until the handler is replaced, to avoid double registrations
676 if (tvVendor
!= CEC_VENDOR_UNKNOWN
&&
677 CCECCommandHandler::HasSpecificHandler(tvVendor
))
679 while (!tv
->ReplaceHandler(false))
683 // get the configuration from the client
684 m_libcec
->AddLog(CEC_LOG_NOTICE
, "registering new CEC client - v%s", ToString((cec_client_version
)configuration
.clientVersion
));
686 // mark as uninitialised and unregistered
687 client
->SetRegistered(false);
688 client
->SetInitialised(false);
690 // get the current ackmask, so we can restore it if polling fails
691 cec_logical_addresses previousMask
= GetLogicalAddresses();
693 // find logical addresses for this client
694 if (!client
->AllocateLogicalAddresses())
696 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register the new CEC client - cannot allocate the requested device types");
697 SetLogicalAddresses(previousMask
);
701 // register this client on the new addresses
702 CECDEVICEVEC devices
;
703 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
704 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
706 // set the physical address of the device at this LA
707 if (CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
708 (*it
)->SetPhysicalAddress(configuration
.iPhysicalAddress
);
710 // replace a previous client
711 CLockObject
lock(m_mutex
);
712 m_clients
.erase((*it
)->GetLogicalAddress());
713 m_clients
.insert(make_pair
<cec_logical_address
, CCECClient
*>((*it
)->GetLogicalAddress(), client
));
716 // get the settings from the rom
717 if (configuration
.bGetSettingsFromROM
== 1)
719 libcec_configuration config
; config
.Clear();
720 m_communication
->GetConfiguration(config
);
722 CLockObject
lock(m_mutex
);
723 if (!config
.deviceTypes
.IsEmpty())
724 configuration
.deviceTypes
= config
.deviceTypes
;
725 if (CLibCEC::IsValidPhysicalAddress(config
.iPhysicalAddress
))
726 configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
727 snprintf(configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
730 // set the firmware version and build date
731 configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
732 configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
733 configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
735 // mark the client as registered
736 client
->SetRegistered(true);
738 // set the new ack mask
739 bool bReturn
= SetLogicalAddresses(GetLogicalAddresses()) &&
740 // and initialise the client
741 client
->OnRegister();
743 // log the new registration
745 strLog
.Format("%s: %s", bReturn
? "CEC client registered" : "failed to register the CEC client", client
->GetConnectionInfo().c_str());
746 m_libcec
->AddLog(bReturn
? CEC_LOG_NOTICE
: CEC_LOG_ERROR
, strLog
);
748 // display a warning if the firmware can be upgraded
749 if (bReturn
&& !IsRunningLatestFirmware())
751 const char *strUpgradeMessage
= "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
752 m_libcec
->AddLog(CEC_LOG_WARNING
, strUpgradeMessage
);
753 libcec_parameter param
;
754 param
.paramData
= (void*)strUpgradeMessage
; param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
755 client
->Alert(CEC_ALERT_SERVICE_DEVICE
, param
);
758 // ensure that the command handler for the TV is initialised
761 CCECCommandHandler
*handler
= GetTV()->GetHandler();
763 handler
->InitHandler();
764 GetTV()->MarkHandlerReady();
770 bool CCECProcessor::UnregisterClient(CCECClient
*client
)
775 if (client
->IsRegistered())
776 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering client: %s", client
->GetConnectionInfo().c_str());
778 // notify the client that it will be unregistered
779 client
->OnUnregister();
782 CLockObject
lock(m_mutex
);
783 // find all devices that match the LA's of this client
784 CECDEVICEVEC devices
;
785 m_busDevices
->GetByLogicalAddresses(devices
, client
->GetConfiguration()->logicalAddresses
);
786 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
789 map
<cec_logical_address
, CCECClient
*>::iterator entry
= m_clients
.find((*it
)->GetLogicalAddress());
790 // unregister the client
791 if (entry
!= m_clients
.end())
792 m_clients
.erase(entry
);
794 // reset the device status
795 (*it
)->ResetDeviceStatus();
799 // set the new ackmask
800 cec_logical_addresses addresses
= GetLogicalAddresses();
801 if (SetLogicalAddresses(addresses
))
803 // no more clients left, disable controlled mode
804 if (addresses
.IsEmpty() && !m_bMonitor
)
805 m_communication
->SetControlledMode(false);
813 void CCECProcessor::UnregisterClients(void)
815 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering all CEC clients");
817 vector
<CCECClient
*> clients
= m_libcec
->GetClients();
818 for (vector
<CCECClient
*>::iterator client
= clients
.begin(); client
!= clients
.end(); client
++)
819 UnregisterClient(*client
);
821 CLockObject
lock(m_mutex
);
825 CCECClient
*CCECProcessor::GetClient(const cec_logical_address address
)
827 CLockObject
lock(m_mutex
);
828 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.find(address
);
829 if (client
!= m_clients
.end())
830 return client
->second
;
834 CCECClient
*CCECProcessor::GetPrimaryClient(void)
836 CLockObject
lock(m_mutex
);
837 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin();
838 if (client
!= m_clients
.end())
839 return client
->second
;
843 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void)
845 return m_busDevices
->At(GetLogicalAddress());
848 cec_logical_address
CCECProcessor::GetLogicalAddress(void)
850 cec_logical_addresses addresses
= GetLogicalAddresses();
851 return addresses
.primary
;
854 cec_logical_addresses
CCECProcessor::GetLogicalAddresses(void)
856 CLockObject
lock(m_mutex
);
857 cec_logical_addresses addresses
;
859 for (map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin(); client
!= m_clients
.end(); client
++)
860 addresses
.Set(client
->first
);
865 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address
) const
867 CCECBusDevice
*device
= GetDevice(address
);
868 return device
&& device
->IsHandledByLibCEC();
871 bool CCECProcessor::IsRunningLatestFirmware(void)
873 return m_communication
&& m_communication
->IsOpen() ?
874 m_communication
->IsRunningLatestFirmware() :
878 void CCECProcessor::SwitchMonitoring(bool bSwitchTo
)
881 CLockObject
lock(m_mutex
);
882 m_bMonitor
= bSwitchTo
;