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)
67 m_busDevices
= new CCECDeviceMap(this);
70 CCECProcessor::~CCECProcessor(void)
73 DELETE_AND_NULL(m_busDevices
);
76 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
78 CLockObject
lock(m_mutex
);
80 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
83 // create the processor thread
88 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
96 void CCECProcessor::Close(void)
98 // mark as uninitialised
99 SetCECInitialised(false);
101 // stop the processor
104 // close the connection
105 DELETE_AND_NULL(m_communication
);
108 void CCECProcessor::ResetMembers(void)
110 // close the connection
111 DELETE_AND_NULL(m_communication
);
113 // reset the other members to the initial state
114 m_iStandardLineTimeout
= 3;
115 m_iRetryLineTimeout
= 3;
116 m_iLastTransmission
= 0;
117 m_busDevices
->ResetDeviceStatus();
120 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
123 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
125 // ensure that a previous connection is closed
128 // reset all member to the initial state
131 // check whether the Close() method deleted any previous connection
134 m_libcec
->AddLog(CEC_LOG_ERROR
, "previous connection could not be closed");
138 // create a new connection
139 m_communication
= CAdapterFactory(this->m_libcec
).GetInstance(strPort
, iBaudRate
);
141 // open a new connection
142 unsigned iConnectTry(0);
143 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
145 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
146 m_communication
->Close();
147 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
150 m_libcec
->AddLog(CEC_LOG_NOTICE
, "connection opened");
152 // mark as initialised
153 SetCECInitialised(true);
158 bool CCECProcessor::CECInitialised(void)
160 CLockObject
lock(m_threadMutex
);
161 return m_bInitialised
;
164 void CCECProcessor::SetCECInitialised(bool bSetTo
/* = true */)
167 CLockObject
lock(m_mutex
);
168 m_bInitialised
= bSetTo
;
174 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, cec_version libCECSpecVersion
/* = CEC_VERSION_1_4 */)
177 CCECBusDevice
*device
= m_busDevices
->At(address
);
180 // check if it's already marked as present or used
181 if (device
->IsPresent() || device
->IsHandledByLibCEC())
184 // poll the LA if not
185 return device
->TryLogicalAddress(libCECSpecVersion
);
191 void CCECProcessor::ReplaceHandlers(void)
193 if (!CECInitialised())
197 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
198 it
->second
->ReplaceHandler(true);
201 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
203 return m_inBuffer
.Push(command
);
206 void *CCECProcessor::Process(void)
208 m_libcec
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
210 cec_command command
; command
.Clear();
211 CTimeout
activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL
);
213 // as long as we're not being stopped and the connection is open
214 while (!IsStopped() && m_communication
->IsOpen())
216 // wait for a new incoming command, and process it
217 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
218 ProcessCommand(command
);
220 if (CECInitialised())
222 // check clients for keypress timeouts
223 m_libcec
->CheckKeypressTimeout();
225 // check if we need to replace handlers
228 // check whether we need to activate a source, if it failed before
229 if (activeSourceCheck
.TimeLeft() == 0)
231 if (CECInitialised())
232 TransmitPendingActiveSourceCommands();
233 activeSourceCheck
.Init(ACTIVE_SOURCE_CHECK_INTERVAL
);
241 bool CCECProcessor::ActivateSource(uint16_t iStreamPath
)
245 // find the device with the given PA
246 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
247 // and make it the active source when found
249 bReturn
= device
->ActivateSource();
251 m_libcec
->AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
256 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
258 CLockObject
lock(m_mutex
);
259 m_iStandardLineTimeout
= iTimeout
;
262 uint8_t CCECProcessor::GetStandardLineTimeout(void)
264 CLockObject
lock(m_mutex
);
265 return m_iStandardLineTimeout
;
268 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
270 CLockObject
lock(m_mutex
);
271 m_iRetryLineTimeout
= iTimeout
;
274 uint8_t CCECProcessor::GetRetryLineTimeout(void)
276 CLockObject
lock(m_mutex
);
277 return m_iRetryLineTimeout
;
280 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
282 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iPhysicalAddress
);
283 return device
!= NULL
;
286 void CCECProcessor::LogOutput(const cec_command
&data
)
290 // initiator and destination
291 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
295 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
297 // append the parameters
298 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
299 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
302 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
305 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
307 // try to find the primary device
308 CCECBusDevice
*primary
= GetPrimaryDevice();
309 // poll the destination, with the primary as source
311 return primary
->TransmitPoll(iAddress
, false);
313 CCECBusDevice
*device
= m_busDevices
->At(CECDEVICE_UNREGISTERED
);
315 return device
->TransmitPoll(iAddress
, false);
320 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
322 return m_busDevices
?
323 m_busDevices
->GetDeviceByPhysicalAddress(iPhysicalAddress
, bSuppressUpdate
) :
327 CCECBusDevice
*CCECProcessor::GetDevice(cec_logical_address address
) const
329 return m_busDevices
?
330 m_busDevices
->At(address
) :
334 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
336 // get the device that is marked as active source from the device map
337 CCECBusDevice
*activeSource
= m_busDevices
->GetActiveSource();
339 return activeSource
->GetLogicalAddress();
341 if (bRequestActiveSource
)
343 // request the active source from the bus
344 CCECBusDevice
*primary
= GetPrimaryDevice();
347 primary
->RequestActiveSource();
348 return GetActiveSource(false);
353 return CECDEVICE_UNKNOWN
;
356 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
358 CCECBusDevice
*device
= m_busDevices
->At(iAddress
);
359 return device
&& device
->IsActiveSource();
362 bool CCECProcessor::Transmit(const cec_command
&data
, bool bIsReply
)
364 cec_command
transmitData(data
);
365 uint8_t iMaxTries(0);
369 // get the current timeout setting
370 uint8_t iLineTimeout(GetStandardLineTimeout());
372 // reset the state of this message to 'unknown'
373 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
375 if (!m_communication
->SupportsSourceLogicalAddress(transmitData
.initiator
))
377 if (transmitData
.initiator
== CECDEVICE_UNREGISTERED
&& m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
379 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData
.initiator
), ToString(CECDEVICE_FREEUSE
));
380 transmitData
.initiator
= CECDEVICE_FREEUSE
;
384 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData
.initiator
));
389 LogOutput(transmitData
);
391 // find the initiator device
392 CCECBusDevice
*initiator
= m_busDevices
->At(transmitData
.initiator
);
395 m_libcec
->AddLog(CEC_LOG_WARNING
, "invalid initiator");
399 // find the destination device, if it's not the broadcast address
400 if (transmitData
.destination
!= CECDEVICE_BROADCAST
)
402 // check if the device is marked as handled by libCEC
403 CCECBusDevice
*destination
= m_busDevices
->At(transmitData
.destination
);
404 if (destination
&& destination
->IsHandledByLibCEC())
406 // and reject the command if it's trying to send data to a device that is handled by libCEC
407 m_libcec
->AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
413 CLockObject
lock(m_mutex
);
414 m_iLastTransmission
= GetTimeMs();
415 // set the number of tries
416 iMaxTries
= initiator
->GetHandler()->GetTransmitRetries() + 1;
417 initiator
->MarkHandlerReady();
420 // and try to send the command
421 while (bRetry
&& ++iTries
< iMaxTries
)
423 if (initiator
->IsUnsupportedFeature(transmitData
.opcode
))
426 adapterState
= !IsStopped() && m_communication
&& m_communication
->IsOpen() ?
427 m_communication
->Write(transmitData
, bRetry
, iLineTimeout
, bIsReply
) :
428 ADAPTER_MESSAGE_STATE_ERROR
;
429 iLineTimeout
= m_iRetryLineTimeout
;
432 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
435 void CCECProcessor::TransmitAbort(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
437 m_libcec
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
440 cec_command::Format(command
, source
, destination
, CEC_OPCODE_FEATURE_ABORT
);
441 command
.parameters
.PushBack((uint8_t)opcode
);
442 command
.parameters
.PushBack((uint8_t)reason
);
444 Transmit(command
, true);
447 void CCECProcessor::ProcessCommand(const cec_command
&command
)
451 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
452 if (command
.opcode_set
== 1)
453 dataStr
.AppendFormat(":%02x", command
.opcode
);
454 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
455 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
456 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
458 // find the initiator
459 CCECBusDevice
*device
= m_busDevices
->At(command
.initiator
);
462 device
->HandleCommand(command
);
465 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
467 CCECBusDevice
*device
= m_busDevices
->At(address
);
468 return device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
471 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
473 CECDEVICEVEC devices
;
474 m_busDevices
->GetByType(type
, devices
);
475 CCECDeviceMap::FilterActive(devices
);
476 return !devices
.empty();
479 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
481 return m_communication
? m_communication
->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
484 bool CCECProcessor::ClearLogicalAddresses(void)
486 cec_logical_addresses addresses
; addresses
.Clear();
487 return SetLogicalAddresses(addresses
);
490 bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
492 return m_communication
? m_communication
->SetLogicalAddresses(addresses
) : false;
495 bool CCECProcessor::StandbyDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
498 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
499 bReturn
&= (*it
)->Standby(initiator
);
503 bool CCECProcessor::StandbyDevice(const cec_logical_address initiator
, cec_logical_address address
)
505 CCECBusDevice
*device
= m_busDevices
->At(address
);
506 return device
? device
->Standby(initiator
) : false;
509 bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
512 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
513 bReturn
&= (*it
)->PowerOn(initiator
);
517 bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator
, cec_logical_address address
)
519 CCECBusDevice
*device
= m_busDevices
->At(address
);
520 return device
? device
->PowerOn(initiator
) : false;
523 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
526 // open a connection if no connection has been opened
527 if (!m_communication
&& strPort
)
529 CAdapterFactory
factory(this->m_libcec
);
530 IAdapterCommunication
*comm
= factory
.GetInstance(strPort
);
531 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
533 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
535 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
537 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
541 bReturn
= comm
->StartBootloader();
542 DELETE_AND_NULL(comm
);
548 m_communication
->StartBootloader();
556 bool CCECProcessor::PingAdapter(void)
558 return m_communication
->PingAdapter();
561 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
563 CCECBusDevice
*device
= m_busDevices
->At(destination
);
565 device
->HandlePollFrom(initiator
);
568 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
570 CCECBusDevice
*device
= m_busDevices
->At(initiator
);
571 return !device
|| !device
->HandleReceiveFailed();
574 bool CCECProcessor::CanPersistConfiguration(void)
576 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
579 bool CCECProcessor::PersistConfiguration(const libcec_configuration
&configuration
)
581 libcec_configuration persistConfiguration
= configuration
;
582 if (!CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
584 CCECBusDevice
*device
= GetPrimaryDevice();
586 persistConfiguration
.iPhysicalAddress
= device
->GetCurrentPhysicalAddress();
589 return m_communication
? m_communication
->PersistConfiguration(persistConfiguration
) : false;
592 void CCECProcessor::RescanActiveDevices(void)
594 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
595 it
->second
->GetStatus(true);
598 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
600 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
603 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
604 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
605 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
610 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
613 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
614 bReturn
&= it
->second
->TransmitPendingActiveSourceCommands();
618 CCECTV
*CCECProcessor::GetTV(void) const
620 return CCECBusDevice::AsTV(m_busDevices
->At(CECDEVICE_TV
));
623 CCECAudioSystem
*CCECProcessor::GetAudioSystem(void) const
625 return CCECBusDevice::AsAudioSystem(m_busDevices
->At(CECDEVICE_AUDIOSYSTEM
));
628 CCECPlaybackDevice
*CCECProcessor::GetPlaybackDevice(cec_logical_address address
) const
630 return CCECBusDevice::AsPlaybackDevice(m_busDevices
->At(address
));
633 CCECRecordingDevice
*CCECProcessor::GetRecordingDevice(cec_logical_address address
) const
635 return CCECBusDevice::AsRecordingDevice(m_busDevices
->At(address
));
638 CCECTuner
*CCECProcessor::GetTuner(cec_logical_address address
) const
640 return CCECBusDevice::AsTuner(m_busDevices
->At(address
));
643 bool CCECProcessor::RegisterClient(CCECClient
*client
)
648 libcec_configuration
&configuration
= *client
->GetConfiguration();
650 if (configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_6_3
&& configuration
.bMonitorOnly
== 1)
653 if (!CECInitialised())
655 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: CEC processor is not initialised");
659 // unregister the client first if it's already been marked as registered
660 if (client
->IsRegistered())
661 UnregisterClient(client
);
663 // ensure that controlled mode is enabled
664 m_communication
->SetControlledMode(true);
666 // ensure that we know the vendor id of the TV
667 CCECBusDevice
*tv
= GetTV();
668 if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED
))
669 tv
->GetVendorId(CECDEVICE_UNREGISTERED
);
670 else if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
671 tv
->GetVendorId(CECDEVICE_FREEUSE
);
673 // get the configuration from the client
674 m_libcec
->AddLog(CEC_LOG_NOTICE
, "registering new CEC client - v%s", ToString((cec_client_version
)configuration
.clientVersion
));
676 // mark as uninitialised and unregistered
677 client
->SetRegistered(false);
678 client
->SetInitialised(false);
680 // get the current ackmask, so we can restore it if polling fails
681 cec_logical_addresses previousMask
= GetLogicalAddresses();
683 // find logical addresses for this client
684 if (!client
->AllocateLogicalAddresses())
686 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register the new CEC client - cannot allocate the requested device types");
687 SetLogicalAddresses(previousMask
);
691 // register this client on the new addresses
692 CECDEVICEVEC devices
;
693 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
694 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
696 // set the physical address of the device at this LA
697 if (CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
698 (*it
)->SetPhysicalAddress(configuration
.iPhysicalAddress
);
700 // replace a previous client
701 CLockObject
lock(m_mutex
);
702 m_clients
.erase((*it
)->GetLogicalAddress());
703 m_clients
.insert(make_pair
<cec_logical_address
, CCECClient
*>((*it
)->GetLogicalAddress(), client
));
706 // get the settings from the rom
707 if (configuration
.bGetSettingsFromROM
== 1)
709 libcec_configuration config
; config
.Clear();
710 m_communication
->GetConfiguration(config
);
712 CLockObject
lock(m_mutex
);
713 if (!config
.deviceTypes
.IsEmpty())
714 configuration
.deviceTypes
= config
.deviceTypes
;
715 if (CLibCEC::IsValidPhysicalAddress(config
.iPhysicalAddress
))
716 configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
717 snprintf(configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
720 // set the firmware version and build date
721 configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
722 configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
723 configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
725 // mark the client as registered
726 client
->SetRegistered(true);
728 // set the new ack mask
729 bool bReturn
= SetLogicalAddresses(GetLogicalAddresses()) &&
730 // and initialise the client
731 client
->OnRegister();
733 // log the new registration
735 strLog
.Format("%s: %s", bReturn
? "CEC client registered" : "failed to register the CEC client", client
->GetConnectionInfo().c_str());
736 m_libcec
->AddLog(bReturn
? CEC_LOG_NOTICE
: CEC_LOG_ERROR
, strLog
);
738 // display a warning if the firmware can be upgraded
739 if (bReturn
&& !IsRunningLatestFirmware())
741 const char *strUpgradeMessage
= "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
742 m_libcec
->AddLog(CEC_LOG_WARNING
, strUpgradeMessage
);
743 libcec_parameter param
;
744 param
.paramData
= (void*)strUpgradeMessage
; param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
745 client
->Alert(CEC_ALERT_SERVICE_DEVICE
, param
);
748 // ensure that the command handler for the TV is initialised
751 CCECCommandHandler
*handler
= GetTV()->GetHandler();
753 handler
->InitHandler();
754 GetTV()->MarkHandlerReady();
760 bool CCECProcessor::UnregisterClient(CCECClient
*client
)
765 if (client
->IsRegistered())
766 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering client: %s", client
->GetConnectionInfo().c_str());
768 // notify the client that it will be unregistered
769 client
->OnUnregister();
772 CLockObject
lock(m_mutex
);
773 // find all devices that match the LA's of this client
774 CECDEVICEVEC devices
;
775 m_busDevices
->GetByLogicalAddresses(devices
, client
->GetConfiguration()->logicalAddresses
);
776 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
779 map
<cec_logical_address
, CCECClient
*>::iterator entry
= m_clients
.find((*it
)->GetLogicalAddress());
780 // unregister the client
781 if (entry
!= m_clients
.end())
782 m_clients
.erase(entry
);
784 // reset the device status
785 (*it
)->ResetDeviceStatus();
789 // set the new ackmask
790 cec_logical_addresses addresses
= GetLogicalAddresses();
791 if (SetLogicalAddresses(addresses
))
793 // no more clients left, disable controlled mode
794 if (addresses
.IsEmpty())
795 m_communication
->SetControlledMode(false);
803 void CCECProcessor::UnregisterClients(void)
805 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering all CEC clients");
807 vector
<CCECClient
*> clients
= m_libcec
->GetClients();
808 for (vector
<CCECClient
*>::iterator client
= clients
.begin(); client
!= clients
.end(); client
++)
809 UnregisterClient(*client
);
811 CLockObject
lock(m_mutex
);
815 CCECClient
*CCECProcessor::GetClient(const cec_logical_address address
)
817 CLockObject
lock(m_mutex
);
818 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.find(address
);
819 if (client
!= m_clients
.end())
820 return client
->second
;
824 CCECClient
*CCECProcessor::GetPrimaryClient(void)
826 CLockObject
lock(m_mutex
);
827 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin();
828 if (client
!= m_clients
.end())
829 return client
->second
;
833 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void)
835 return m_busDevices
->At(GetLogicalAddress());
838 cec_logical_address
CCECProcessor::GetLogicalAddress(void)
840 cec_logical_addresses addresses
= GetLogicalAddresses();
841 return addresses
.primary
;
844 cec_logical_addresses
CCECProcessor::GetLogicalAddresses(void)
846 CLockObject
lock(m_mutex
);
847 cec_logical_addresses addresses
;
849 for (map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin(); client
!= m_clients
.end(); client
++)
850 addresses
.Set(client
->first
);
855 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address
) const
857 CCECBusDevice
*device
= GetDevice(address
);
858 return device
&& device
->IsHandledByLibCEC();
861 bool CCECProcessor::IsRunningLatestFirmware(void)
863 return m_communication
&& m_communication
->IsOpen() ?
864 m_communication
->IsRunningLatestFirmware() :