2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 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
56 #define TV_PRESENT_CHECK_INTERVAL 30000
58 #define ToString(x) CCECTypeUtils::ToString(x)
60 CCECProcessor::CCECProcessor(CLibCEC
*libcec
) :
61 m_bInitialised(false),
62 m_communication(NULL
),
64 m_iStandardLineTimeout(3),
65 m_iRetryLineTimeout(3),
66 m_iLastTransmission(0),
68 m_addrAllocator(NULL
),
69 m_bStallCommunication(false)
71 m_busDevices
= new CCECDeviceMap(this);
74 CCECProcessor::~CCECProcessor(void)
76 m_bStallCommunication
= false;
77 DELETE_AND_NULL(m_addrAllocator
);
79 DELETE_AND_NULL(m_busDevices
);
82 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
84 CLockObject
lock(m_mutex
);
86 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
89 // create the processor thread
94 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
102 void CCECProcessor::Close(void)
104 // mark as uninitialised
105 SetCECInitialised(false);
107 // stop the processor
109 m_inBuffer
.Broadcast();
112 // close the connection
113 DELETE_AND_NULL(m_communication
);
116 void CCECProcessor::ResetMembers(void)
118 // close the connection
119 DELETE_AND_NULL(m_communication
);
121 // reset the other members to the initial state
122 m_iStandardLineTimeout
= 3;
123 m_iRetryLineTimeout
= 3;
124 m_iLastTransmission
= 0;
125 m_busDevices
->ResetDeviceStatus();
128 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
131 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
133 // ensure that a previous connection is closed
136 // reset all member to the initial state
139 // check whether the Close() method deleted any previous connection
142 m_libcec
->AddLog(CEC_LOG_ERROR
, "previous connection could not be closed");
146 // create a new connection
147 m_communication
= CAdapterFactory(this->m_libcec
).GetInstance(strPort
, iBaudRate
);
149 // open a new connection
150 unsigned iConnectTry(0);
151 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
153 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
154 m_communication
->Close();
155 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
158 m_libcec
->AddLog(CEC_LOG_NOTICE
, "connection opened");
160 // mark as initialised
161 SetCECInitialised(true);
166 bool CCECProcessor::CECInitialised(void)
168 CLockObject
lock(m_threadMutex
);
169 return m_bInitialised
;
172 void CCECProcessor::SetCECInitialised(bool bSetTo
/* = true */)
175 CLockObject
lock(m_mutex
);
176 m_bInitialised
= bSetTo
;
182 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, cec_version libCECSpecVersion
/* = CEC_VERSION_1_4 */)
185 CCECBusDevice
*device
= m_busDevices
->At(address
);
188 // check if it's already marked as present or used
189 if (device
->IsPresent() || device
->IsHandledByLibCEC())
192 // poll the LA if not
193 return device
->TryLogicalAddress(libCECSpecVersion
);
199 void CCECProcessor::ReplaceHandlers(void)
201 if (!CECInitialised())
205 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
206 it
->second
->ReplaceHandler(true);
209 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
211 return m_inBuffer
.Push(command
);
214 void *CCECProcessor::Process(void)
216 m_libcec
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
218 cec_command command
; command
.Clear();
219 CTimeout
activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL
);
220 CTimeout
tvPresentCheck(TV_PRESENT_CHECK_INTERVAL
);
222 // as long as we're not being stopped and the connection is open
223 while (!IsStopped() && m_communication
->IsOpen())
225 // wait for a new incoming command, and process it
226 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
227 ProcessCommand(command
);
229 if (CECInitialised() && !IsStopped())
231 // check clients for keypress timeouts
232 m_libcec
->CheckKeypressTimeout();
234 // check if we need to replace handlers
237 // check whether we need to activate a source, if it failed before
238 if (activeSourceCheck
.TimeLeft() == 0)
240 if (CECInitialised())
241 TransmitPendingActiveSourceCommands();
242 activeSourceCheck
.Init(ACTIVE_SOURCE_CHECK_INTERVAL
);
245 // check whether the TV is present and responding
246 if (tvPresentCheck
.TimeLeft() == 0)
248 if (!m_busDevices
->At(CECDEVICE_TV
)->IsPresent())
250 libcec_parameter param
;
251 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
252 param
.paramData
= (void*)"TV does not respond to CEC polls";
253 GetPrimaryClient()->Alert(CEC_ALERT_TV_POLL_FAILED
, param
);
255 tvPresentCheck
.Init(TV_PRESENT_CHECK_INTERVAL
);
263 bool CCECProcessor::ActivateSource(uint16_t iStreamPath
)
267 // find the device with the given PA
268 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
269 // and make it the active source when found
271 bReturn
= device
->ActivateSource();
273 m_libcec
->AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
278 void CCECProcessor::SetActiveSource(bool bSetTo
, bool bClientUnregistered
)
281 m_communication
->SetActiveSource(bSetTo
, bClientUnregistered
);
284 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
286 CLockObject
lock(m_mutex
);
287 m_iStandardLineTimeout
= iTimeout
;
290 uint8_t CCECProcessor::GetStandardLineTimeout(void)
292 CLockObject
lock(m_mutex
);
293 return m_iStandardLineTimeout
;
296 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
298 CLockObject
lock(m_mutex
);
299 m_iRetryLineTimeout
= iTimeout
;
302 uint8_t CCECProcessor::GetRetryLineTimeout(void)
304 CLockObject
lock(m_mutex
);
305 return m_iRetryLineTimeout
;
308 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
310 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iPhysicalAddress
);
311 return device
!= NULL
;
314 void CCECProcessor::LogOutput(const cec_command
&data
)
318 // initiator and destination
319 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
323 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
325 // append the parameters
326 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
327 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
330 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
333 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
335 // try to find the primary device
336 CCECBusDevice
*primary
= GetPrimaryDevice();
337 // poll the destination, with the primary as source
339 return primary
->TransmitPoll(iAddress
, true);
341 CCECBusDevice
*device
= m_busDevices
->At(CECDEVICE_UNREGISTERED
);
343 return device
->TransmitPoll(iAddress
, true);
348 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
350 return m_busDevices
?
351 m_busDevices
->GetDeviceByPhysicalAddress(iPhysicalAddress
, bSuppressUpdate
) :
355 CCECBusDevice
*CCECProcessor::GetDevice(cec_logical_address address
) const
357 return m_busDevices
?
358 m_busDevices
->At(address
) :
362 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
364 // get the device that is marked as active source from the device map
365 CCECBusDevice
*activeSource
= m_busDevices
->GetActiveSource();
367 return activeSource
->GetLogicalAddress();
369 if (bRequestActiveSource
)
371 // request the active source from the bus
372 CCECBusDevice
*primary
= GetPrimaryDevice();
375 primary
->RequestActiveSource();
376 return GetActiveSource(false);
381 return CECDEVICE_UNKNOWN
;
384 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
386 CCECBusDevice
*device
= m_busDevices
->At(iAddress
);
387 return device
&& device
->IsActiveSource();
390 bool CCECProcessor::Transmit(const cec_command
&data
, bool bIsReply
)
392 cec_command
transmitData(data
);
393 uint8_t iMaxTries(0);
397 // get the current timeout setting
398 uint8_t iLineTimeout(GetStandardLineTimeout());
400 // reset the state of this message to 'unknown'
401 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
403 if (!m_communication
->SupportsSourceLogicalAddress(transmitData
.initiator
))
405 if (transmitData
.initiator
== CECDEVICE_UNREGISTERED
&& m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
407 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData
.initiator
), ToString(CECDEVICE_FREEUSE
));
408 transmitData
.initiator
= CECDEVICE_FREEUSE
;
412 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData
.initiator
));
417 LogOutput(transmitData
);
419 // find the initiator device
420 CCECBusDevice
*initiator
= m_busDevices
->At(transmitData
.initiator
);
423 m_libcec
->AddLog(CEC_LOG_WARNING
, "invalid initiator");
427 // find the destination device, if it's not the broadcast address
428 if (transmitData
.destination
!= CECDEVICE_BROADCAST
)
430 // check if the device is marked as handled by libCEC
431 CCECBusDevice
*destination
= m_busDevices
->At(transmitData
.destination
);
432 if (destination
&& destination
->IsHandledByLibCEC())
434 // and reject the command if it's trying to send data to a device that is handled by libCEC
435 m_libcec
->AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
440 // wait until we finished allocating a new LA if it got lost
441 while (m_bStallCommunication
) Sleep(5);
444 CLockObject
lock(m_mutex
);
445 m_iLastTransmission
= GetTimeMs();
446 // set the number of tries
447 iMaxTries
= initiator
->GetHandler()->GetTransmitRetries() + 1;
448 initiator
->MarkHandlerReady();
451 // and try to send the command
452 while (bRetry
&& ++iTries
< iMaxTries
)
454 if (initiator
->IsUnsupportedFeature(transmitData
.opcode
))
457 adapterState
= !IsStopped() && m_communication
&& m_communication
->IsOpen() ?
458 m_communication
->Write(transmitData
, bRetry
, iLineTimeout
, bIsReply
) :
459 ADAPTER_MESSAGE_STATE_ERROR
;
460 iLineTimeout
= m_iRetryLineTimeout
;
464 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
|| adapterState
== ADAPTER_MESSAGE_STATE_SENT
|| adapterState
== ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
:
465 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
468 void CCECProcessor::TransmitAbort(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
470 m_libcec
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
473 cec_command::Format(command
, source
, destination
, CEC_OPCODE_FEATURE_ABORT
);
474 command
.parameters
.PushBack((uint8_t)opcode
);
475 command
.parameters
.PushBack((uint8_t)reason
);
477 Transmit(command
, true);
480 void CCECProcessor::ProcessCommand(const cec_command
&command
)
483 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, CCECTypeUtils::ToString(command
).c_str());
485 // find the initiator
486 CCECBusDevice
*device
= m_busDevices
->At(command
.initiator
);
489 device
->HandleCommand(command
);
492 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
494 CCECBusDevice
*device
= m_busDevices
->At(address
);
495 return device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
498 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
500 CECDEVICEVEC devices
;
501 m_busDevices
->GetByType(type
, devices
);
502 CCECDeviceMap::FilterActive(devices
);
503 return !devices
.empty();
506 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
508 return m_communication
? m_communication
->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
511 bool CCECProcessor::ClearLogicalAddresses(void)
513 cec_logical_addresses addresses
; addresses
.Clear();
514 return SetLogicalAddresses(addresses
);
517 bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
519 return m_communication
? m_communication
->SetLogicalAddresses(addresses
) : false;
522 bool CCECProcessor::StandbyDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
525 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
526 bReturn
&= (*it
)->Standby(initiator
);
530 bool CCECProcessor::StandbyDevice(const cec_logical_address initiator
, cec_logical_address address
)
532 CCECBusDevice
*device
= m_busDevices
->At(address
);
533 return device
? device
->Standby(initiator
) : false;
536 bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
539 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
540 bReturn
&= (*it
)->PowerOn(initiator
);
544 bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator
, cec_logical_address address
)
546 CCECBusDevice
*device
= m_busDevices
->At(address
);
547 return device
? device
->PowerOn(initiator
) : false;
550 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
553 // open a connection if no connection has been opened
554 if (!m_communication
&& strPort
)
556 CAdapterFactory
factory(this->m_libcec
);
557 IAdapterCommunication
*comm
= factory
.GetInstance(strPort
);
558 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
560 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
562 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
564 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
568 bReturn
= comm
->StartBootloader();
569 DELETE_AND_NULL(comm
);
575 m_communication
->StartBootloader();
583 bool CCECProcessor::PingAdapter(void)
585 return m_communication
->PingAdapter();
588 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
590 CCECBusDevice
*device
= m_busDevices
->At(destination
);
592 device
->HandlePollFrom(initiator
);
595 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
597 CCECBusDevice
*device
= m_busDevices
->At(initiator
);
598 return !device
|| !device
->HandleReceiveFailed();
601 bool CCECProcessor::CanPersistConfiguration(void)
603 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
606 bool CCECProcessor::PersistConfiguration(const libcec_configuration
&configuration
)
608 libcec_configuration persistConfiguration
= configuration
;
609 if (!CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
611 CCECBusDevice
*device
= GetPrimaryDevice();
613 persistConfiguration
.iPhysicalAddress
= device
->GetCurrentPhysicalAddress();
616 return m_communication
? m_communication
->PersistConfiguration(persistConfiguration
) : false;
619 void CCECProcessor::RescanActiveDevices(void)
621 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
622 it
->second
->GetStatus(true);
625 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
627 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
630 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
631 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
632 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
633 config
->adapterType
= m_communication
->GetAdapterType();
640 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
643 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
644 bReturn
&= it
->second
->TransmitPendingActiveSourceCommands();
648 CCECTV
*CCECProcessor::GetTV(void) const
650 return CCECBusDevice::AsTV(m_busDevices
->At(CECDEVICE_TV
));
653 CCECAudioSystem
*CCECProcessor::GetAudioSystem(void) const
655 return CCECBusDevice::AsAudioSystem(m_busDevices
->At(CECDEVICE_AUDIOSYSTEM
));
658 CCECPlaybackDevice
*CCECProcessor::GetPlaybackDevice(cec_logical_address address
) const
660 return CCECBusDevice::AsPlaybackDevice(m_busDevices
->At(address
));
663 CCECRecordingDevice
*CCECProcessor::GetRecordingDevice(cec_logical_address address
) const
665 return CCECBusDevice::AsRecordingDevice(m_busDevices
->At(address
));
668 CCECTuner
*CCECProcessor::GetTuner(cec_logical_address address
) const
670 return CCECBusDevice::AsTuner(m_busDevices
->At(address
));
673 bool CCECProcessor::AllocateLogicalAddresses(CCECClient
* client
)
675 libcec_configuration
&configuration
= *client
->GetConfiguration();
677 // mark as unregistered
678 client
->SetRegistered(false);
680 // unregister this client from the old addresses
681 CECDEVICEVEC devices
;
682 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
683 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
685 // remove client entry
686 CLockObject
lock(m_mutex
);
687 m_clients
.erase((*it
)->GetLogicalAddress());
690 // find logical addresses for this client
691 if (!client
->AllocateLogicalAddresses())
693 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to find a free logical address for the client");
697 // register this client on the new addresses
699 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
700 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
702 // set the physical address of the device at this LA
703 if (CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
704 (*it
)->SetPhysicalAddress(configuration
.iPhysicalAddress
);
706 // replace a previous client
707 CLockObject
lock(m_mutex
);
708 m_clients
.erase((*it
)->GetLogicalAddress());
709 m_clients
.insert(make_pair
<cec_logical_address
, CCECClient
*>((*it
)->GetLogicalAddress(), client
));
712 // set the new ackmask
713 SetLogicalAddresses(GetLogicalAddresses());
715 // resume outgoing communication
716 m_bStallCommunication
= false;
721 uint16_t CCECProcessor::GetPhysicalAddressFromEeprom(void)
723 libcec_configuration config
; config
.Clear();
725 m_communication
->GetConfiguration(config
);
726 return config
.iPhysicalAddress
;
729 bool CCECProcessor::RegisterClient(CCECClient
*client
)
734 libcec_configuration
&configuration
= *client
->GetConfiguration();
736 if (configuration
.clientVersion
< CEC_CLIENT_VERSION_2_0_0
)
738 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: client version %s is no longer supported", ToString((cec_client_version
)configuration
.clientVersion
));
742 if (configuration
.bMonitorOnly
== 1)
745 if (!CECInitialised())
747 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: CEC processor is not initialised");
751 // unregister the client first if it's already been marked as registered
752 if (client
->IsRegistered())
753 UnregisterClient(client
);
755 // ensure that controlled mode is enabled
756 m_communication
->SetControlledMode(true);
759 // source logical address for requests
760 cec_logical_address
sourceAddress(CECDEVICE_UNREGISTERED
);
761 if (!m_communication
->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED
))
763 if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
764 sourceAddress
= CECDEVICE_FREEUSE
;
767 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: both unregistered and free use are not supported by the device");
772 // ensure that we know the vendor id of the TV
773 CCECBusDevice
*tv
= GetTV();
774 cec_vendor_id
tvVendor(tv
->GetVendorId(sourceAddress
));
776 // wait until the handler is replaced, to avoid double registrations
777 if (tvVendor
!= CEC_VENDOR_UNKNOWN
&&
778 CCECCommandHandler::HasSpecificHandler(tvVendor
))
780 while (!tv
->ReplaceHandler(false))
784 // get the configuration from the client
785 m_libcec
->AddLog(CEC_LOG_NOTICE
, "registering new CEC client - v%s", ToString((cec_client_version
)configuration
.clientVersion
));
787 // get the current ackmask, so we can restore it if polling fails
788 cec_logical_addresses previousMask
= GetLogicalAddresses();
790 // mark as uninitialised
791 client
->SetInitialised(false);
793 // find logical addresses for this client
794 if (!AllocateLogicalAddresses(client
))
796 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register the new CEC client - cannot allocate the requested device types");
797 SetLogicalAddresses(previousMask
);
801 // get the settings from the rom
802 if (configuration
.bGetSettingsFromROM
== 1)
804 libcec_configuration config
; config
.Clear();
805 m_communication
->GetConfiguration(config
);
807 CLockObject
lock(m_mutex
);
808 if (!config
.deviceTypes
.IsEmpty())
809 configuration
.deviceTypes
= config
.deviceTypes
;
810 if (CLibCEC::IsValidPhysicalAddress(config
.iPhysicalAddress
))
811 configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
812 snprintf(configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
815 // set the firmware version and build date
816 configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
817 configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
818 configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
819 configuration
.adapterType
= m_communication
->GetAdapterType();
821 // mark the client as registered
822 client
->SetRegistered(true);
824 sourceAddress
= client
->GetPrimaryLogicalAdddress();
826 // initialise the client
827 bool bReturn
= client
->OnRegister();
829 // log the new registration
831 strLog
.Format("%s: %s", bReturn
? "CEC client registered" : "failed to register the CEC client", client
->GetConnectionInfo().c_str());
832 m_libcec
->AddLog(bReturn
? CEC_LOG_NOTICE
: CEC_LOG_ERROR
, strLog
);
834 // display a warning if the firmware can be upgraded
835 if (bReturn
&& !IsRunningLatestFirmware())
837 const char *strUpgradeMessage
= "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
838 m_libcec
->AddLog(CEC_LOG_WARNING
, strUpgradeMessage
);
839 libcec_parameter param
;
840 param
.paramData
= (void*)strUpgradeMessage
; param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
841 client
->Alert(CEC_ALERT_SERVICE_DEVICE
, param
);
844 // ensure that the command handler for the TV is initialised
847 CCECCommandHandler
*handler
= GetTV()->GetHandler();
849 handler
->InitHandler();
850 GetTV()->MarkHandlerReady();
853 // report our OSD name to the TV, since some TVs don't request it
854 client
->GetPrimaryDevice()->TransmitOSDName(CECDEVICE_TV
, false);
856 // request the power status of the TV
857 tv
->RequestPowerStatus(sourceAddress
, true);
862 bool CCECProcessor::UnregisterClient(CCECClient
*client
)
867 if (client
->IsRegistered())
868 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering client: %s", client
->GetConnectionInfo().c_str());
870 // notify the client that it will be unregistered
871 client
->OnUnregister();
874 CLockObject
lock(m_mutex
);
875 // find all devices that match the LA's of this client
876 CECDEVICEVEC devices
;
877 m_busDevices
->GetByLogicalAddresses(devices
, client
->GetConfiguration()->logicalAddresses
);
878 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
881 map
<cec_logical_address
, CCECClient
*>::iterator entry
= m_clients
.find((*it
)->GetLogicalAddress());
882 // unregister the client
883 if (entry
!= m_clients
.end())
884 m_clients
.erase(entry
);
886 // reset the device status
887 (*it
)->ResetDeviceStatus(true);
891 // set the new ackmask
892 cec_logical_addresses addresses
= GetLogicalAddresses();
893 if (SetLogicalAddresses(addresses
))
895 // no more clients left, disable controlled mode
896 if (addresses
.IsEmpty() && !m_bMonitor
)
897 m_communication
->SetControlledMode(false);
905 void CCECProcessor::UnregisterClients(void)
907 m_libcec
->AddLog(CEC_LOG_DEBUG
, "unregistering all CEC clients");
909 vector
<CCECClient
*> clients
= m_libcec
->GetClients();
910 for (vector
<CCECClient
*>::iterator client
= clients
.begin(); client
!= clients
.end(); client
++)
911 UnregisterClient(*client
);
913 CLockObject
lock(m_mutex
);
917 CCECClient
*CCECProcessor::GetClient(const cec_logical_address address
)
919 CLockObject
lock(m_mutex
);
920 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.find(address
);
921 if (client
!= m_clients
.end())
922 return client
->second
;
926 CCECClient
*CCECProcessor::GetPrimaryClient(void)
928 CLockObject
lock(m_mutex
);
929 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin();
930 if (client
!= m_clients
.end())
931 return client
->second
;
935 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void)
937 return m_busDevices
->At(GetLogicalAddress());
940 cec_logical_address
CCECProcessor::GetLogicalAddress(void)
942 cec_logical_addresses addresses
= GetLogicalAddresses();
943 return addresses
.primary
;
946 cec_logical_addresses
CCECProcessor::GetLogicalAddresses(void)
948 CLockObject
lock(m_mutex
);
949 cec_logical_addresses addresses
;
951 for (map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin(); client
!= m_clients
.end(); client
++)
952 addresses
.Set(client
->first
);
957 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address
) const
959 CCECBusDevice
*device
= GetDevice(address
);
960 return device
&& device
->IsHandledByLibCEC();
963 bool CCECProcessor::IsRunningLatestFirmware(void)
965 return m_communication
&& m_communication
->IsOpen() ?
966 m_communication
->IsRunningLatestFirmware() :
970 void CCECProcessor::SwitchMonitoring(bool bSwitchTo
)
973 CLockObject
lock(m_mutex
);
974 m_bMonitor
= bSwitchTo
;
980 void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress
)
982 // stall outgoing messages until we know our new LA
983 m_bStallCommunication
= true;
985 m_libcec
->AddLog(CEC_LOG_NOTICE
, "logical address %x was taken by another device, allocating a new address", oldAddress
);
986 CCECClient
* client
= GetClient(oldAddress
);
988 client
= GetPrimaryClient();
992 while (m_addrAllocator
->IsRunning()) Sleep(5);
993 delete m_addrAllocator
;
995 m_addrAllocator
= new CCECAllocateLogicalAddress(this, client
);
996 m_addrAllocator
->CreateThread();
1000 void CCECProcessor::HandlePhysicalAddressChanged(uint16_t iNewAddress
)
1002 m_libcec
->AddLog(CEC_LOG_NOTICE
, "physical address changed to %04x", iNewAddress
);
1003 CCECClient
* client
= GetPrimaryClient();
1005 client
->SetPhysicalAddress(iNewAddress
);
1008 uint16_t CCECProcessor::GetAdapterVendorId(void) const
1010 return m_communication
? m_communication
->GetAdapterVendorId() : 0;
1013 uint16_t CCECProcessor::GetAdapterProductId(void) const
1015 return m_communication
? m_communication
->GetAdapterProductId() : 0;
1018 CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor
* processor
, CCECClient
* client
) :
1019 m_processor(processor
),
1020 m_client(client
) { }
1022 void* CCECAllocateLogicalAddress::Process(void)
1024 m_processor
->AllocateLogicalAddresses(m_client
);