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 CCECStandbyProtection::CCECStandbyProtection(CCECProcessor
* processor
) :
61 m_processor(processor
) {}
62 CCECStandbyProtection::~CCECStandbyProtection(void) {}
64 void* CCECStandbyProtection::Process(void)
66 int64_t last
= GetTimeMs();
70 PLATFORM::CEvent::Sleep(1000);
74 // reset the connection if the clock changed
75 if (next
< last
|| next
- last
> 10000)
77 libcec_parameter param
;
78 param
.paramData
= NULL
; param
.paramType
= CEC_PARAMETER_TYPE_UNKOWN
;
79 m_processor
->GetLib()->Alert(CEC_ALERT_CONNECTION_LOST
, param
);
88 CCECProcessor::CCECProcessor(CLibCEC
*libcec
) :
89 m_bInitialised(false),
90 m_communication(NULL
),
92 m_iStandardLineTimeout(3),
93 m_iRetryLineTimeout(3),
94 m_iLastTransmission(0),
96 m_addrAllocator(NULL
),
97 m_bStallCommunication(false),
100 m_busDevices
= new CCECDeviceMap(this);
103 CCECProcessor::~CCECProcessor(void)
105 m_bStallCommunication
= false;
106 DELETE_AND_NULL(m_addrAllocator
);
108 DELETE_AND_NULL(m_busDevices
);
111 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
113 CLockObject
lock(m_mutex
);
115 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
118 // create the processor thread
123 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
131 void CCECProcessor::Close(void)
133 // mark as uninitialised
134 SetCECInitialised(false);
136 // stop the processor
137 DELETE_AND_NULL(m_connCheck
);
139 m_inBuffer
.Broadcast();
142 // close the connection
143 DELETE_AND_NULL(m_communication
);
146 void CCECProcessor::ResetMembers(void)
148 // close the connection
149 DELETE_AND_NULL(m_communication
);
151 // reset the other members to the initial state
152 m_iStandardLineTimeout
= 3;
153 m_iRetryLineTimeout
= 3;
154 m_iLastTransmission
= 0;
155 m_busDevices
->ResetDeviceStatus();
158 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
161 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
163 // ensure that a previous connection is closed
166 // reset all member to the initial state
169 // check whether the Close() method deleted any previous connection
172 m_libcec
->AddLog(CEC_LOG_ERROR
, "previous connection could not be closed");
176 // create a new connection
177 m_communication
= CAdapterFactory(this->m_libcec
).GetInstance(strPort
, iBaudRate
);
179 // open a new connection
180 unsigned iConnectTry(0);
181 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
183 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
184 m_communication
->Close();
185 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
188 m_libcec
->AddLog(CEC_LOG_NOTICE
, "connection opened");
190 // mark as initialised
191 SetCECInitialised(true);
196 bool CCECProcessor::CECInitialised(void)
198 CLockObject
lock(m_threadMutex
);
199 return m_bInitialised
;
202 void CCECProcessor::SetCECInitialised(bool bSetTo
/* = true */)
205 CLockObject
lock(m_mutex
);
206 m_bInitialised
= bSetTo
;
212 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, cec_version libCECSpecVersion
/* = CEC_VERSION_1_4 */)
215 CCECBusDevice
*device
= m_busDevices
->At(address
);
218 // check if it's already marked as present or used
219 if (device
->IsPresent() || device
->IsHandledByLibCEC())
222 // poll the LA if not
223 return device
->TryLogicalAddress(libCECSpecVersion
);
229 void CCECProcessor::ReplaceHandlers(void)
231 if (!CECInitialised())
235 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
236 it
->second
->ReplaceHandler(true);
239 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
241 return m_inBuffer
.Push(command
);
244 void *CCECProcessor::Process(void)
246 m_libcec
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
249 m_connCheck
= new CCECStandbyProtection(this);
250 m_connCheck
->CreateThread();
252 cec_command command
; command
.Clear();
253 CTimeout
activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL
);
254 CTimeout
tvPresentCheck(TV_PRESENT_CHECK_INTERVAL
);
256 // as long as we're not being stopped and the connection is open
257 while (!IsStopped() && m_communication
->IsOpen())
259 // wait for a new incoming command, and process it
260 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
261 ProcessCommand(command
);
263 if (CECInitialised() && !IsStopped())
265 // check clients for keypress timeouts
266 m_libcec
->CheckKeypressTimeout();
268 // check if we need to replace handlers
271 // check whether we need to activate a source, if it failed before
272 if (activeSourceCheck
.TimeLeft() == 0)
274 if (CECInitialised())
275 TransmitPendingActiveSourceCommands();
276 activeSourceCheck
.Init(ACTIVE_SOURCE_CHECK_INTERVAL
);
279 // check whether the TV is present and responding
280 if (tvPresentCheck
.TimeLeft() == 0)
282 CCECClient
*primary
= GetPrimaryClient();
283 // only check whether the tv responds to polls when a client is connected and not in monitoring mode
284 if (primary
&& primary
->GetConfiguration()->bMonitorOnly
!= 1)
286 if (!m_busDevices
->At(CECDEVICE_TV
)->IsPresent())
288 libcec_parameter param
;
289 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
290 param
.paramData
= (void*)"TV does not respond to CEC polls";
291 primary
->Alert(CEC_ALERT_TV_POLL_FAILED
, param
);
294 tvPresentCheck
.Init(TV_PRESENT_CHECK_INTERVAL
);
302 bool CCECProcessor::ActivateSource(uint16_t iStreamPath
)
306 // find the device with the given PA
307 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
308 // and make it the active source when found
310 bReturn
= device
->ActivateSource();
312 m_libcec
->AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
317 void CCECProcessor::SetActiveSource(bool bSetTo
, bool bClientUnregistered
)
320 m_communication
->SetActiveSource(bSetTo
, bClientUnregistered
);
323 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
325 CLockObject
lock(m_mutex
);
326 m_iStandardLineTimeout
= iTimeout
;
329 uint8_t CCECProcessor::GetStandardLineTimeout(void)
331 CLockObject
lock(m_mutex
);
332 return m_iStandardLineTimeout
;
335 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
337 CLockObject
lock(m_mutex
);
338 m_iRetryLineTimeout
= iTimeout
;
341 uint8_t CCECProcessor::GetRetryLineTimeout(void)
343 CLockObject
lock(m_mutex
);
344 return m_iRetryLineTimeout
;
347 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
349 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iPhysicalAddress
);
350 return device
!= NULL
;
353 void CCECProcessor::LogOutput(const cec_command
&data
)
357 // initiator and destination
358 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
362 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
364 // append the parameters
365 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
366 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
369 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
372 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
374 // try to find the primary device
375 CCECBusDevice
*primary
= GetPrimaryDevice();
376 // poll the destination, with the primary as source
378 return primary
->TransmitPoll(iAddress
, true);
380 CCECBusDevice
*device
= m_busDevices
->At(CECDEVICE_UNREGISTERED
);
382 return device
->TransmitPoll(iAddress
, true);
387 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
389 return m_busDevices
?
390 m_busDevices
->GetDeviceByPhysicalAddress(iPhysicalAddress
, bSuppressUpdate
) :
394 CCECBusDevice
*CCECProcessor::GetDevice(cec_logical_address address
) const
396 return m_busDevices
?
397 m_busDevices
->At(address
) :
401 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
403 // get the device that is marked as active source from the device map
404 CCECBusDevice
*activeSource
= m_busDevices
->GetActiveSource();
406 return activeSource
->GetLogicalAddress();
408 if (bRequestActiveSource
)
410 // request the active source from the bus
411 CCECBusDevice
*primary
= GetPrimaryDevice();
414 primary
->RequestActiveSource();
415 return GetActiveSource(false);
420 return CECDEVICE_UNKNOWN
;
423 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
425 CCECBusDevice
*device
= m_busDevices
->At(iAddress
);
426 return device
&& device
->IsActiveSource();
429 bool CCECProcessor::Transmit(const cec_command
&data
, bool bIsReply
)
431 cec_command
transmitData(data
);
432 uint8_t iMaxTries(0);
436 // get the current timeout setting
437 uint8_t iLineTimeout(GetStandardLineTimeout());
439 // reset the state of this message to 'unknown'
440 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
442 if (!m_communication
->SupportsSourceLogicalAddress(transmitData
.initiator
))
444 if (transmitData
.initiator
== CECDEVICE_UNREGISTERED
&& m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
446 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData
.initiator
), ToString(CECDEVICE_FREEUSE
));
447 transmitData
.initiator
= CECDEVICE_FREEUSE
;
451 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData
.initiator
));
456 LogOutput(transmitData
);
458 // find the initiator device
459 CCECBusDevice
*initiator
= m_busDevices
->At(transmitData
.initiator
);
462 m_libcec
->AddLog(CEC_LOG_WARNING
, "invalid initiator");
466 // find the destination device, if it's not the broadcast address
467 if (transmitData
.destination
!= CECDEVICE_BROADCAST
)
469 // check if the device is marked as handled by libCEC
470 CCECBusDevice
*destination
= m_busDevices
->At(transmitData
.destination
);
471 if (destination
&& destination
->IsHandledByLibCEC())
473 // and reject the command if it's trying to send data to a device that is handled by libCEC
474 m_libcec
->AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
479 // wait until we finished allocating a new LA if it got lost
480 while (m_bStallCommunication
) Sleep(5);
483 CLockObject
lock(m_mutex
);
484 m_iLastTransmission
= GetTimeMs();
485 // set the number of tries
486 iMaxTries
= initiator
->GetHandler()->GetTransmitRetries() + 1;
487 initiator
->MarkHandlerReady();
490 // and try to send the command
491 while (bRetry
&& ++iTries
< iMaxTries
)
493 if (initiator
->IsUnsupportedFeature(transmitData
.opcode
))
496 adapterState
= !IsStopped() && m_communication
&& m_communication
->IsOpen() ?
497 m_communication
->Write(transmitData
, bRetry
, iLineTimeout
, bIsReply
) :
498 ADAPTER_MESSAGE_STATE_ERROR
;
499 iLineTimeout
= m_iRetryLineTimeout
;
503 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
|| adapterState
== ADAPTER_MESSAGE_STATE_SENT
|| adapterState
== ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
:
504 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
507 void CCECProcessor::TransmitAbort(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
509 m_libcec
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
512 cec_command::Format(command
, source
, destination
, CEC_OPCODE_FEATURE_ABORT
);
513 command
.parameters
.PushBack((uint8_t)opcode
);
514 command
.parameters
.PushBack((uint8_t)reason
);
516 Transmit(command
, true);
519 void CCECProcessor::ProcessCommand(const cec_command
&command
)
522 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, ToString(command
).c_str());
524 // find the initiator
525 CCECBusDevice
*device
= m_busDevices
->At(command
.initiator
);
528 device
->HandleCommand(command
);
531 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
533 CCECBusDevice
*device
= m_busDevices
->At(address
);
534 return device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
537 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
539 CECDEVICEVEC devices
;
540 m_busDevices
->GetByType(type
, devices
);
541 CCECDeviceMap::FilterActive(devices
);
542 return !devices
.empty();
545 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
547 return m_communication
? m_communication
->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
550 bool CCECProcessor::ClearLogicalAddresses(void)
552 cec_logical_addresses addresses
; addresses
.Clear();
553 return SetLogicalAddresses(addresses
);
556 bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
558 return m_communication
? m_communication
->SetLogicalAddresses(addresses
) : false;
561 bool CCECProcessor::StandbyDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
564 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
565 bReturn
&= (*it
)->Standby(initiator
);
569 bool CCECProcessor::StandbyDevice(const cec_logical_address initiator
, cec_logical_address address
)
571 CCECBusDevice
*device
= m_busDevices
->At(address
);
572 return device
? device
->Standby(initiator
) : false;
575 bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
578 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
579 bReturn
&= (*it
)->PowerOn(initiator
);
583 bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator
, cec_logical_address address
)
585 CCECBusDevice
*device
= m_busDevices
->At(address
);
586 return device
? device
->PowerOn(initiator
) : false;
589 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
592 // open a connection if no connection has been opened
593 if (!m_communication
&& strPort
)
595 CAdapterFactory
factory(this->m_libcec
);
596 IAdapterCommunication
*comm
= factory
.GetInstance(strPort
);
597 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
599 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
601 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
603 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
607 bReturn
= comm
->StartBootloader();
608 DELETE_AND_NULL(comm
);
614 m_communication
->StartBootloader();
622 bool CCECProcessor::PingAdapter(void)
624 return m_communication
->PingAdapter();
627 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
629 CCECBusDevice
*device
= m_busDevices
->At(destination
);
631 device
->HandlePollFrom(initiator
);
634 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
636 CCECBusDevice
*device
= m_busDevices
->At(initiator
);
637 return !device
|| !device
->HandleReceiveFailed();
640 bool CCECProcessor::CanPersistConfiguration(void)
642 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
645 bool CCECProcessor::PersistConfiguration(const libcec_configuration
&configuration
)
647 libcec_configuration persistConfiguration
= configuration
;
648 if (!CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
650 CCECBusDevice
*device
= GetPrimaryDevice();
652 persistConfiguration
.iPhysicalAddress
= device
->GetCurrentPhysicalAddress();
655 return m_communication
? m_communication
->PersistConfiguration(persistConfiguration
) : false;
658 void CCECProcessor::RescanActiveDevices(void)
660 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
661 it
->second
->GetStatus(true);
664 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
666 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
669 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
670 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
671 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
672 config
->adapterType
= m_communication
->GetAdapterType();
679 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
682 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
683 bReturn
&= it
->second
->TransmitPendingActiveSourceCommands();
687 CCECTV
*CCECProcessor::GetTV(void) const
689 return CCECBusDevice::AsTV(m_busDevices
->At(CECDEVICE_TV
));
692 CCECAudioSystem
*CCECProcessor::GetAudioSystem(void) const
694 return CCECBusDevice::AsAudioSystem(m_busDevices
->At(CECDEVICE_AUDIOSYSTEM
));
697 CCECPlaybackDevice
*CCECProcessor::GetPlaybackDevice(cec_logical_address address
) const
699 return CCECBusDevice::AsPlaybackDevice(m_busDevices
->At(address
));
702 CCECRecordingDevice
*CCECProcessor::GetRecordingDevice(cec_logical_address address
) const
704 return CCECBusDevice::AsRecordingDevice(m_busDevices
->At(address
));
707 CCECTuner
*CCECProcessor::GetTuner(cec_logical_address address
) const
709 return CCECBusDevice::AsTuner(m_busDevices
->At(address
));
712 bool CCECProcessor::AllocateLogicalAddresses(CCECClient
* client
)
714 libcec_configuration
&configuration
= *client
->GetConfiguration();
716 // mark as unregistered
717 client
->SetRegistered(false);
719 // unregister this client from the old addresses
720 CECDEVICEVEC devices
;
721 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
722 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
724 // remove client entry
725 CLockObject
lock(m_mutex
);
726 m_clients
.erase((*it
)->GetLogicalAddress());
729 // find logical addresses for this client
730 if (!client
->AllocateLogicalAddresses())
732 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to find a free logical address for the client");
736 // register this client on the new addresses
738 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
739 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
741 // set the physical address of the device at this LA
742 if (CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
743 (*it
)->SetPhysicalAddress(configuration
.iPhysicalAddress
);
745 // replace a previous client
746 CLockObject
lock(m_mutex
);
747 m_clients
.erase((*it
)->GetLogicalAddress());
748 m_clients
.insert(make_pair((*it
)->GetLogicalAddress(), client
));
751 // set the new ackmask
752 SetLogicalAddresses(GetLogicalAddresses());
754 // resume outgoing communication
755 m_bStallCommunication
= false;
760 uint16_t CCECProcessor::GetPhysicalAddressFromEeprom(void)
762 libcec_configuration config
; config
.Clear();
764 m_communication
->GetConfiguration(config
);
765 return config
.iPhysicalAddress
;
768 bool CCECProcessor::RegisterClient(CCECClient
*client
)
773 libcec_configuration
&configuration
= *client
->GetConfiguration();
775 if (configuration
.clientVersion
< CEC_CLIENT_VERSION_2_0_0
)
777 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
));
781 if (configuration
.bMonitorOnly
== 1)
784 if (!CECInitialised())
786 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: CEC processor is not initialised");
790 // unregister the client first if it's already been marked as registered
791 if (client
->IsRegistered())
792 UnregisterClient(client
);
794 // ensure that controlled mode is enabled
795 m_communication
->SetControlledMode(true);
798 // source logical address for requests
799 cec_logical_address
sourceAddress(CECDEVICE_UNREGISTERED
);
800 if (!m_communication
->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED
))
802 if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
803 sourceAddress
= CECDEVICE_FREEUSE
;
806 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: both unregistered and free use are not supported by the device");
811 // ensure that we know the vendor id of the TV
812 CCECBusDevice
*tv
= GetTV();
813 cec_vendor_id
tvVendor(tv
->GetVendorId(sourceAddress
));
815 // wait until the handler is replaced, to avoid double registrations
816 if (tvVendor
!= CEC_VENDOR_UNKNOWN
&&
817 CCECCommandHandler::HasSpecificHandler(tvVendor
))
819 while (!tv
->ReplaceHandler(false))
823 // get the configuration from the client
824 m_libcec
->AddLog(CEC_LOG_NOTICE
, "registering new CEC client - v%s", ToString((cec_client_version
)configuration
.clientVersion
));
826 // get the current ackmask, so we can restore it if polling fails
827 cec_logical_addresses previousMask
= GetLogicalAddresses();
829 // mark as uninitialised
830 client
->SetInitialised(false);
832 // find logical addresses for this client
833 if (!AllocateLogicalAddresses(client
))
835 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register the new CEC client - cannot allocate the requested device types");
836 SetLogicalAddresses(previousMask
);
840 // get the settings from the rom
841 if (configuration
.bGetSettingsFromROM
== 1)
843 libcec_configuration config
; config
.Clear();
844 m_communication
->GetConfiguration(config
);
846 CLockObject
lock(m_mutex
);
847 if (!config
.deviceTypes
.IsEmpty())
848 configuration
.deviceTypes
= config
.deviceTypes
;
849 if (CLibCEC::IsValidPhysicalAddress(config
.iPhysicalAddress
))
850 configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
851 snprintf(configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
854 // set the firmware version and build date
855 configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
856 configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
857 configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
858 configuration
.adapterType
= m_communication
->GetAdapterType();
860 // mark the client as registered
861 client
->SetRegistered(true);
863 sourceAddress
= client
->GetPrimaryLogicalAdddress();
865 // initialise the client
866 bool bReturn
= client
->OnRegister();
868 // log the new registration
870 strLog
.Format("%s: %s", bReturn
? "CEC client registered" : "failed to register the CEC client", client
->GetConnectionInfo().c_str());
871 m_libcec
->AddLog(bReturn
? CEC_LOG_NOTICE
: CEC_LOG_ERROR
, strLog
);
873 // display a warning if the firmware can be upgraded
874 if (bReturn
&& !IsRunningLatestFirmware())
876 const char *strUpgradeMessage
= "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
877 m_libcec
->AddLog(CEC_LOG_WARNING
, strUpgradeMessage
);
878 libcec_parameter param
;
879 param
.paramData
= (void*)strUpgradeMessage
; param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
880 client
->Alert(CEC_ALERT_SERVICE_DEVICE
, param
);
883 // ensure that the command handler for the TV is initialised
886 CCECCommandHandler
*handler
= GetTV()->GetHandler();
888 handler
->InitHandler();
889 GetTV()->MarkHandlerReady();
892 // report our OSD name to the TV, since some TVs don't request it
893 client
->GetPrimaryDevice()->TransmitOSDName(CECDEVICE_TV
, false);
895 // request the power status of the TV
896 tv
->RequestPowerStatus(sourceAddress
, true, true);
901 bool CCECProcessor::UnregisterClient(CCECClient
*client
)
906 if (client
->IsRegistered())
907 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering client: %s", client
->GetConnectionInfo().c_str());
909 // notify the client that it will be unregistered
910 client
->OnUnregister();
913 CLockObject
lock(m_mutex
);
914 // find all devices that match the LA's of this client
915 CECDEVICEVEC devices
;
916 m_busDevices
->GetByLogicalAddresses(devices
, client
->GetConfiguration()->logicalAddresses
);
917 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
920 map
<cec_logical_address
, CCECClient
*>::iterator entry
= m_clients
.find((*it
)->GetLogicalAddress());
921 // unregister the client
922 if (entry
!= m_clients
.end())
923 m_clients
.erase(entry
);
925 // reset the device status
926 (*it
)->ResetDeviceStatus(true);
930 // set the new ackmask
931 cec_logical_addresses addresses
= GetLogicalAddresses();
932 if (SetLogicalAddresses(addresses
))
934 // no more clients left, disable controlled mode
935 if (addresses
.IsEmpty() && !m_bMonitor
)
936 m_communication
->SetControlledMode(false);
944 void CCECProcessor::UnregisterClients(void)
946 m_libcec
->AddLog(CEC_LOG_DEBUG
, "unregistering all CEC clients");
948 vector
<CCECClient
*> clients
= m_libcec
->GetClients();
949 for (vector
<CCECClient
*>::iterator client
= clients
.begin(); client
!= clients
.end(); client
++)
950 UnregisterClient(*client
);
952 CLockObject
lock(m_mutex
);
956 CCECClient
*CCECProcessor::GetClient(const cec_logical_address address
)
958 CLockObject
lock(m_mutex
);
959 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.find(address
);
960 if (client
!= m_clients
.end())
961 return client
->second
;
965 CCECClient
*CCECProcessor::GetPrimaryClient(void)
967 CLockObject
lock(m_mutex
);
968 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin();
969 if (client
!= m_clients
.end())
970 return client
->second
;
974 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void)
976 return m_busDevices
->At(GetLogicalAddress());
979 cec_logical_address
CCECProcessor::GetLogicalAddress(void)
981 cec_logical_addresses addresses
= GetLogicalAddresses();
982 return addresses
.primary
;
985 cec_logical_addresses
CCECProcessor::GetLogicalAddresses(void)
987 CLockObject
lock(m_mutex
);
988 cec_logical_addresses addresses
;
990 for (map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin(); client
!= m_clients
.end(); client
++)
991 addresses
.Set(client
->first
);
996 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address
) const
998 CCECBusDevice
*device
= GetDevice(address
);
999 return device
&& device
->IsHandledByLibCEC();
1002 bool CCECProcessor::IsRunningLatestFirmware(void)
1004 return m_communication
&& m_communication
->IsOpen() ?
1005 m_communication
->IsRunningLatestFirmware() :
1009 void CCECProcessor::SwitchMonitoring(bool bSwitchTo
)
1012 CLockObject
lock(m_mutex
);
1013 m_bMonitor
= bSwitchTo
;
1016 UnregisterClients();
1019 void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress
)
1021 // stall outgoing messages until we know our new LA
1022 m_bStallCommunication
= true;
1024 m_libcec
->AddLog(CEC_LOG_NOTICE
, "logical address %x was taken by another device, allocating a new address", oldAddress
);
1025 CCECClient
* client
= GetClient(oldAddress
);
1027 client
= GetPrimaryClient();
1030 if (m_addrAllocator
)
1031 while (m_addrAllocator
->IsRunning()) Sleep(5);
1032 delete m_addrAllocator
;
1034 m_addrAllocator
= new CCECAllocateLogicalAddress(this, client
);
1035 m_addrAllocator
->CreateThread();
1039 void CCECProcessor::HandlePhysicalAddressChanged(uint16_t iNewAddress
)
1041 m_libcec
->AddLog(CEC_LOG_NOTICE
, "physical address changed to %04x", iNewAddress
);
1042 CCECClient
* client
= GetPrimaryClient();
1044 client
->SetPhysicalAddress(iNewAddress
);
1047 uint16_t CCECProcessor::GetAdapterVendorId(void) const
1049 return m_communication
? m_communication
->GetAdapterVendorId() : 0;
1052 uint16_t CCECProcessor::GetAdapterProductId(void) const
1054 return m_communication
? m_communication
->GetAdapterProductId() : 0;
1057 CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor
* processor
, CCECClient
* client
) :
1058 m_processor(processor
),
1059 m_client(client
) { }
1061 void* CCECAllocateLogicalAddress::Process(void)
1063 m_processor
->AllocateLogicalAddresses(m_client
);