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_addrAllocator(NULL
),
68 m_bStallCommunication(false)
70 m_busDevices
= new CCECDeviceMap(this);
73 CCECProcessor::~CCECProcessor(void)
75 m_bStallCommunication
= false;
76 DELETE_AND_NULL(m_addrAllocator
);
78 DELETE_AND_NULL(m_busDevices
);
81 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
83 CLockObject
lock(m_mutex
);
85 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
88 // create the processor thread
93 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
101 void CCECProcessor::Close(void)
103 // mark as uninitialised
104 SetCECInitialised(false);
106 // stop the processor
108 m_inBuffer
.Broadcast();
111 // close the connection
112 DELETE_AND_NULL(m_communication
);
115 void CCECProcessor::ResetMembers(void)
117 // close the connection
118 DELETE_AND_NULL(m_communication
);
120 // reset the other members to the initial state
121 m_iStandardLineTimeout
= 3;
122 m_iRetryLineTimeout
= 3;
123 m_iLastTransmission
= 0;
124 m_busDevices
->ResetDeviceStatus();
127 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
130 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
132 // ensure that a previous connection is closed
135 // reset all member to the initial state
138 // check whether the Close() method deleted any previous connection
141 m_libcec
->AddLog(CEC_LOG_ERROR
, "previous connection could not be closed");
145 // create a new connection
146 m_communication
= CAdapterFactory(this->m_libcec
).GetInstance(strPort
, iBaudRate
);
148 // open a new connection
149 unsigned iConnectTry(0);
150 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
152 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
153 m_communication
->Close();
154 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
157 m_libcec
->AddLog(CEC_LOG_NOTICE
, "connection opened");
159 // mark as initialised
160 SetCECInitialised(true);
165 bool CCECProcessor::CECInitialised(void)
167 CLockObject
lock(m_threadMutex
);
168 return m_bInitialised
;
171 void CCECProcessor::SetCECInitialised(bool bSetTo
/* = true */)
174 CLockObject
lock(m_mutex
);
175 m_bInitialised
= bSetTo
;
181 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, cec_version libCECSpecVersion
/* = CEC_VERSION_1_4 */)
184 CCECBusDevice
*device
= m_busDevices
->At(address
);
187 // check if it's already marked as present or used
188 if (device
->IsPresent() || device
->IsHandledByLibCEC())
191 // poll the LA if not
192 return device
->TryLogicalAddress(libCECSpecVersion
);
198 void CCECProcessor::ReplaceHandlers(void)
200 if (!CECInitialised())
204 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
205 it
->second
->ReplaceHandler(true);
208 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
210 return m_inBuffer
.Push(command
);
213 void *CCECProcessor::Process(void)
215 m_libcec
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
217 cec_command command
; command
.Clear();
218 CTimeout
activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL
);
220 // as long as we're not being stopped and the connection is open
221 while (!IsStopped() && m_communication
->IsOpen())
223 // wait for a new incoming command, and process it
224 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
225 ProcessCommand(command
);
227 if (CECInitialised() && !IsStopped())
229 // check clients for keypress timeouts
230 m_libcec
->CheckKeypressTimeout();
232 // check if we need to replace handlers
235 // check whether we need to activate a source, if it failed before
236 if (activeSourceCheck
.TimeLeft() == 0)
238 if (CECInitialised())
239 TransmitPendingActiveSourceCommands();
240 activeSourceCheck
.Init(ACTIVE_SOURCE_CHECK_INTERVAL
);
248 bool CCECProcessor::ActivateSource(uint16_t iStreamPath
)
252 // find the device with the given PA
253 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
254 // and make it the active source when found
256 bReturn
= device
->ActivateSource();
258 m_libcec
->AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
263 void CCECProcessor::SetActiveSource(bool bSetTo
, bool bClientUnregistered
)
266 m_communication
->SetActiveSource(bSetTo
, bClientUnregistered
);
269 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
271 CLockObject
lock(m_mutex
);
272 m_iStandardLineTimeout
= iTimeout
;
275 uint8_t CCECProcessor::GetStandardLineTimeout(void)
277 CLockObject
lock(m_mutex
);
278 return m_iStandardLineTimeout
;
281 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
283 CLockObject
lock(m_mutex
);
284 m_iRetryLineTimeout
= iTimeout
;
287 uint8_t CCECProcessor::GetRetryLineTimeout(void)
289 CLockObject
lock(m_mutex
);
290 return m_iRetryLineTimeout
;
293 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
295 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iPhysicalAddress
);
296 return device
!= NULL
;
299 void CCECProcessor::LogOutput(const cec_command
&data
)
303 // initiator and destination
304 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
308 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
310 // append the parameters
311 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
312 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
315 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
318 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
320 // try to find the primary device
321 CCECBusDevice
*primary
= GetPrimaryDevice();
322 // poll the destination, with the primary as source
324 return primary
->TransmitPoll(iAddress
, true);
326 CCECBusDevice
*device
= m_busDevices
->At(CECDEVICE_UNREGISTERED
);
328 return device
->TransmitPoll(iAddress
, true);
333 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
335 return m_busDevices
?
336 m_busDevices
->GetDeviceByPhysicalAddress(iPhysicalAddress
, bSuppressUpdate
) :
340 CCECBusDevice
*CCECProcessor::GetDevice(cec_logical_address address
) const
342 return m_busDevices
?
343 m_busDevices
->At(address
) :
347 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
349 // get the device that is marked as active source from the device map
350 CCECBusDevice
*activeSource
= m_busDevices
->GetActiveSource();
352 return activeSource
->GetLogicalAddress();
354 if (bRequestActiveSource
&& m_busDevices
->GetActiveSourceAddress() == CEC_INVALID_PHYSICAL_ADDRESS
)
356 // request the active source from the bus
357 CCECBusDevice
*primary
= GetPrimaryDevice();
360 primary
->RequestActiveSource();
361 return GetActiveSource(false);
366 return CECDEVICE_UNKNOWN
;
369 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
371 CCECBusDevice
*device
= m_busDevices
->At(iAddress
);
372 return device
&& device
->IsActiveSource();
375 bool CCECProcessor::Transmit(const cec_command
&data
, bool bIsReply
)
377 cec_command
transmitData(data
);
378 uint8_t iMaxTries(0);
382 // get the current timeout setting
383 uint8_t iLineTimeout(GetStandardLineTimeout());
385 // reset the state of this message to 'unknown'
386 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
388 if (!m_communication
->SupportsSourceLogicalAddress(transmitData
.initiator
))
390 if (transmitData
.initiator
== CECDEVICE_UNREGISTERED
&& m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
392 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData
.initiator
), ToString(CECDEVICE_FREEUSE
));
393 transmitData
.initiator
= CECDEVICE_FREEUSE
;
397 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData
.initiator
));
402 LogOutput(transmitData
);
404 // find the initiator device
405 CCECBusDevice
*initiator
= m_busDevices
->At(transmitData
.initiator
);
408 m_libcec
->AddLog(CEC_LOG_WARNING
, "invalid initiator");
412 // find the destination device, if it's not the broadcast address
413 if (transmitData
.destination
!= CECDEVICE_BROADCAST
)
415 // check if the device is marked as handled by libCEC
416 CCECBusDevice
*destination
= m_busDevices
->At(transmitData
.destination
);
417 if (destination
&& destination
->IsHandledByLibCEC())
419 // and reject the command if it's trying to send data to a device that is handled by libCEC
420 m_libcec
->AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
425 // wait until we finished allocating a new LA if it got lost
426 while (m_bStallCommunication
) Sleep(5);
429 CLockObject
lock(m_mutex
);
430 m_iLastTransmission
= GetTimeMs();
431 // set the number of tries
432 iMaxTries
= initiator
->GetHandler()->GetTransmitRetries() + 1;
433 initiator
->MarkHandlerReady();
436 // and try to send the command
437 while (bRetry
&& ++iTries
< iMaxTries
)
439 if (initiator
->IsUnsupportedFeature(transmitData
.opcode
))
442 adapterState
= !IsStopped() && m_communication
&& m_communication
->IsOpen() ?
443 m_communication
->Write(transmitData
, bRetry
, iLineTimeout
, bIsReply
) :
444 ADAPTER_MESSAGE_STATE_ERROR
;
445 iLineTimeout
= m_iRetryLineTimeout
;
449 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
|| adapterState
== ADAPTER_MESSAGE_STATE_SENT
|| adapterState
== ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
:
450 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
453 void CCECProcessor::TransmitAbort(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
455 m_libcec
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
458 cec_command::Format(command
, source
, destination
, CEC_OPCODE_FEATURE_ABORT
);
459 command
.parameters
.PushBack((uint8_t)opcode
);
460 command
.parameters
.PushBack((uint8_t)reason
);
462 Transmit(command
, true);
465 void CCECProcessor::ProcessCommand(const cec_command
&command
)
469 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
470 if (command
.opcode_set
== 1)
471 dataStr
.AppendFormat(":%02x", command
.opcode
);
472 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
473 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
474 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
476 // find the initiator
477 CCECBusDevice
*device
= m_busDevices
->At(command
.initiator
);
480 device
->HandleCommand(command
);
483 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
485 CCECBusDevice
*device
= m_busDevices
->At(address
);
486 return device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
489 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
491 CECDEVICEVEC devices
;
492 m_busDevices
->GetByType(type
, devices
);
493 CCECDeviceMap::FilterActive(devices
);
494 return !devices
.empty();
497 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
499 return m_communication
? m_communication
->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
502 bool CCECProcessor::ClearLogicalAddresses(void)
504 cec_logical_addresses addresses
; addresses
.Clear();
505 return SetLogicalAddresses(addresses
);
508 bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
510 return m_communication
? m_communication
->SetLogicalAddresses(addresses
) : false;
513 bool CCECProcessor::StandbyDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
516 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
517 bReturn
&= (*it
)->Standby(initiator
);
521 bool CCECProcessor::StandbyDevice(const cec_logical_address initiator
, cec_logical_address address
)
523 CCECBusDevice
*device
= m_busDevices
->At(address
);
524 return device
? device
->Standby(initiator
) : false;
527 bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
530 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
531 bReturn
&= (*it
)->PowerOn(initiator
);
535 bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator
, cec_logical_address address
)
537 CCECBusDevice
*device
= m_busDevices
->At(address
);
538 return device
? device
->PowerOn(initiator
) : false;
541 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
544 // open a connection if no connection has been opened
545 if (!m_communication
&& strPort
)
547 CAdapterFactory
factory(this->m_libcec
);
548 IAdapterCommunication
*comm
= factory
.GetInstance(strPort
);
549 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
551 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
553 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
555 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
559 bReturn
= comm
->StartBootloader();
560 DELETE_AND_NULL(comm
);
566 m_communication
->StartBootloader();
574 bool CCECProcessor::PingAdapter(void)
576 return m_communication
->PingAdapter();
579 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
581 CCECBusDevice
*device
= m_busDevices
->At(destination
);
583 device
->HandlePollFrom(initiator
);
586 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
588 CCECBusDevice
*device
= m_busDevices
->At(initiator
);
589 return !device
|| !device
->HandleReceiveFailed();
592 bool CCECProcessor::CanPersistConfiguration(void)
594 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
597 bool CCECProcessor::PersistConfiguration(const libcec_configuration
&configuration
)
599 libcec_configuration persistConfiguration
= configuration
;
600 if (!CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
602 CCECBusDevice
*device
= GetPrimaryDevice();
604 persistConfiguration
.iPhysicalAddress
= device
->GetCurrentPhysicalAddress();
607 return m_communication
? m_communication
->PersistConfiguration(persistConfiguration
) : false;
610 void CCECProcessor::RescanActiveDevices(void)
612 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
613 it
->second
->GetStatus(true);
616 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
618 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
621 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
622 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
623 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
624 config
->adapterType
= m_communication
->GetAdapterType();
629 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
632 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
633 bReturn
&= it
->second
->TransmitPendingActiveSourceCommands();
637 CCECTV
*CCECProcessor::GetTV(void) const
639 return CCECBusDevice::AsTV(m_busDevices
->At(CECDEVICE_TV
));
642 CCECAudioSystem
*CCECProcessor::GetAudioSystem(void) const
644 return CCECBusDevice::AsAudioSystem(m_busDevices
->At(CECDEVICE_AUDIOSYSTEM
));
647 CCECPlaybackDevice
*CCECProcessor::GetPlaybackDevice(cec_logical_address address
) const
649 return CCECBusDevice::AsPlaybackDevice(m_busDevices
->At(address
));
652 CCECRecordingDevice
*CCECProcessor::GetRecordingDevice(cec_logical_address address
) const
654 return CCECBusDevice::AsRecordingDevice(m_busDevices
->At(address
));
657 CCECTuner
*CCECProcessor::GetTuner(cec_logical_address address
) const
659 return CCECBusDevice::AsTuner(m_busDevices
->At(address
));
662 bool CCECProcessor::AllocateLogicalAddresses(CCECClient
* client
)
664 libcec_configuration
&configuration
= *client
->GetConfiguration();
666 // mark as unregistered
667 client
->SetRegistered(false);
669 // unregister this client from the old addresses
670 CECDEVICEVEC devices
;
671 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
672 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
674 // remove client entry
675 CLockObject
lock(m_mutex
);
676 m_clients
.erase((*it
)->GetLogicalAddress());
679 // find logical addresses for this client
680 if (!client
->AllocateLogicalAddresses())
682 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to find a free logical address for the client");
686 // register this client on the new addresses
688 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
689 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
691 // set the physical address of the device at this LA
692 if (CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
693 (*it
)->SetPhysicalAddress(configuration
.iPhysicalAddress
);
695 // replace a previous client
696 CLockObject
lock(m_mutex
);
697 m_clients
.erase((*it
)->GetLogicalAddress());
698 m_clients
.insert(make_pair
<cec_logical_address
, CCECClient
*>((*it
)->GetLogicalAddress(), client
));
701 // set the new ackmask
702 SetLogicalAddresses(GetLogicalAddresses());
704 // resume outgoing communication
705 m_bStallCommunication
= false;
710 uint16_t CCECProcessor::GetPhysicalAddressFromEeprom(void)
712 libcec_configuration config
; config
.Clear();
714 m_communication
->GetConfiguration(config
);
715 return config
.iPhysicalAddress
;
718 bool CCECProcessor::RegisterClient(CCECClient
*client
)
723 libcec_configuration
&configuration
= *client
->GetConfiguration();
725 if (configuration
.clientVersion
< CEC_CLIENT_VERSION_2_0_0
)
727 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
));
731 if (configuration
.bMonitorOnly
== 1)
734 if (!CECInitialised())
736 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: CEC processor is not initialised");
740 // unregister the client first if it's already been marked as registered
741 if (client
->IsRegistered())
742 UnregisterClient(client
);
744 // ensure that controlled mode is enabled
745 m_communication
->SetControlledMode(true);
748 // source logical address for requests
749 cec_logical_address
sourceAddress(CECDEVICE_UNREGISTERED
);
750 if (!m_communication
->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED
))
752 if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
753 sourceAddress
= CECDEVICE_FREEUSE
;
756 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: both unregistered and free use are not supported by the device");
761 // ensure that we know the vendor id of the TV
762 CCECBusDevice
*tv
= GetTV();
763 cec_vendor_id
tvVendor(tv
->GetVendorId(sourceAddress
));
765 // wait until the handler is replaced, to avoid double registrations
766 if (tvVendor
!= CEC_VENDOR_UNKNOWN
&&
767 CCECCommandHandler::HasSpecificHandler(tvVendor
))
769 while (!tv
->ReplaceHandler(false))
773 // get the configuration from the client
774 m_libcec
->AddLog(CEC_LOG_NOTICE
, "registering new CEC client - v%s", ToString((cec_client_version
)configuration
.clientVersion
));
776 // get the current ackmask, so we can restore it if polling fails
777 cec_logical_addresses previousMask
= GetLogicalAddresses();
779 // mark as uninitialised
780 client
->SetInitialised(false);
782 // find logical addresses for this client
783 if (!AllocateLogicalAddresses(client
))
785 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register the new CEC client - cannot allocate the requested device types");
786 SetLogicalAddresses(previousMask
);
790 // get the settings from the rom
791 if (configuration
.bGetSettingsFromROM
== 1)
793 libcec_configuration config
; config
.Clear();
794 m_communication
->GetConfiguration(config
);
796 CLockObject
lock(m_mutex
);
797 if (!config
.deviceTypes
.IsEmpty())
798 configuration
.deviceTypes
= config
.deviceTypes
;
799 if (CLibCEC::IsValidPhysicalAddress(config
.iPhysicalAddress
))
800 configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
801 snprintf(configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
804 // set the firmware version and build date
805 configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
806 configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
807 configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
808 configuration
.adapterType
= m_communication
->GetAdapterType();
810 // mark the client as registered
811 client
->SetRegistered(true);
813 sourceAddress
= client
->GetPrimaryLogicalAdddress();
815 // initialise the client
816 bool bReturn
= client
->OnRegister();
818 // log the new registration
820 strLog
.Format("%s: %s", bReturn
? "CEC client registered" : "failed to register the CEC client", client
->GetConnectionInfo().c_str());
821 m_libcec
->AddLog(bReturn
? CEC_LOG_NOTICE
: CEC_LOG_ERROR
, strLog
);
823 // display a warning if the firmware can be upgraded
824 if (bReturn
&& !IsRunningLatestFirmware())
826 const char *strUpgradeMessage
= "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
827 m_libcec
->AddLog(CEC_LOG_WARNING
, strUpgradeMessage
);
828 libcec_parameter param
;
829 param
.paramData
= (void*)strUpgradeMessage
; param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
830 client
->Alert(CEC_ALERT_SERVICE_DEVICE
, param
);
833 // ensure that the command handler for the TV is initialised
836 CCECCommandHandler
*handler
= GetTV()->GetHandler();
838 handler
->InitHandler();
839 GetTV()->MarkHandlerReady();
842 // request the power status of the TV
843 tv
->RequestPowerStatus(sourceAddress
, true);
848 bool CCECProcessor::UnregisterClient(CCECClient
*client
)
853 if (client
->IsRegistered())
854 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering client: %s", client
->GetConnectionInfo().c_str());
856 // notify the client that it will be unregistered
857 client
->OnUnregister();
860 CLockObject
lock(m_mutex
);
861 // find all devices that match the LA's of this client
862 CECDEVICEVEC devices
;
863 m_busDevices
->GetByLogicalAddresses(devices
, client
->GetConfiguration()->logicalAddresses
);
864 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
867 map
<cec_logical_address
, CCECClient
*>::iterator entry
= m_clients
.find((*it
)->GetLogicalAddress());
868 // unregister the client
869 if (entry
!= m_clients
.end())
870 m_clients
.erase(entry
);
872 // reset the device status
873 (*it
)->ResetDeviceStatus(true);
877 // set the new ackmask
878 cec_logical_addresses addresses
= GetLogicalAddresses();
879 if (SetLogicalAddresses(addresses
))
881 // no more clients left, disable controlled mode
882 if (addresses
.IsEmpty() && !m_bMonitor
)
883 m_communication
->SetControlledMode(false);
891 void CCECProcessor::UnregisterClients(void)
893 m_libcec
->AddLog(CEC_LOG_DEBUG
, "unregistering all CEC clients");
895 vector
<CCECClient
*> clients
= m_libcec
->GetClients();
896 for (vector
<CCECClient
*>::iterator client
= clients
.begin(); client
!= clients
.end(); client
++)
897 UnregisterClient(*client
);
899 CLockObject
lock(m_mutex
);
903 CCECClient
*CCECProcessor::GetClient(const cec_logical_address address
)
905 CLockObject
lock(m_mutex
);
906 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.find(address
);
907 if (client
!= m_clients
.end())
908 return client
->second
;
912 CCECClient
*CCECProcessor::GetPrimaryClient(void)
914 CLockObject
lock(m_mutex
);
915 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin();
916 if (client
!= m_clients
.end())
917 return client
->second
;
921 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void)
923 return m_busDevices
->At(GetLogicalAddress());
926 cec_logical_address
CCECProcessor::GetLogicalAddress(void)
928 cec_logical_addresses addresses
= GetLogicalAddresses();
929 return addresses
.primary
;
932 cec_logical_addresses
CCECProcessor::GetLogicalAddresses(void)
934 CLockObject
lock(m_mutex
);
935 cec_logical_addresses addresses
;
937 for (map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin(); client
!= m_clients
.end(); client
++)
938 addresses
.Set(client
->first
);
943 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address
) const
945 CCECBusDevice
*device
= GetDevice(address
);
946 return device
&& device
->IsHandledByLibCEC();
949 bool CCECProcessor::IsRunningLatestFirmware(void)
951 return m_communication
&& m_communication
->IsOpen() ?
952 m_communication
->IsRunningLatestFirmware() :
956 void CCECProcessor::SwitchMonitoring(bool bSwitchTo
)
959 CLockObject
lock(m_mutex
);
960 m_bMonitor
= bSwitchTo
;
966 void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress
)
968 // stall outgoing messages until we know our new LA
969 m_bStallCommunication
= true;
971 m_libcec
->AddLog(CEC_LOG_NOTICE
, "logical address %x was taken by another device, allocating a new address", oldAddress
);
972 CCECClient
* client
= GetClient(oldAddress
);
974 client
= GetPrimaryClient();
978 while (m_addrAllocator
->IsRunning()) Sleep(5);
979 delete m_addrAllocator
;
981 m_addrAllocator
= new CCECAllocateLogicalAddress(this, client
);
982 m_addrAllocator
->CreateThread();
986 uint16_t CCECProcessor::GetAdapterVendorId(void) const
988 return m_communication
? m_communication
->GetAdapterVendorId() : 0;
991 uint16_t CCECProcessor::GetAdapterProductId(void) const
993 return m_communication
? m_communication
->GetAdapterProductId() : 0;
996 CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor
* processor
, CCECClient
* client
) :
997 m_processor(processor
),
1000 void* CCECAllocateLogicalAddress::Process(void)
1002 m_processor
->AllocateLogicalAddresses(m_client
);