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 CLockObject
lock(m_mutex
);
144 DELETE_AND_NULL(m_communication
);
147 void CCECProcessor::ResetMembers(void)
149 // close the connection
150 DELETE_AND_NULL(m_communication
);
152 // reset the other members to the initial state
153 m_iStandardLineTimeout
= 3;
154 m_iRetryLineTimeout
= 3;
155 m_iLastTransmission
= 0;
156 m_busDevices
->ResetDeviceStatus();
159 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
162 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
164 // ensure that a previous connection is closed
167 // reset all member to the initial state
170 // check whether the Close() method deleted any previous connection
173 m_libcec
->AddLog(CEC_LOG_ERROR
, "previous connection could not be closed");
177 // create a new connection
178 m_communication
= CAdapterFactory(this->m_libcec
).GetInstance(strPort
, iBaudRate
);
180 // open a new connection
181 unsigned iConnectTry(0);
182 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
184 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
185 m_communication
->Close();
186 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
189 m_libcec
->AddLog(CEC_LOG_NOTICE
, "connection opened");
191 // mark as initialised
192 SetCECInitialised(true);
197 bool CCECProcessor::CECInitialised(void)
199 CLockObject
lock(m_threadMutex
);
200 return m_bInitialised
;
203 void CCECProcessor::SetCECInitialised(bool bSetTo
/* = true */)
206 CLockObject
lock(m_mutex
);
207 m_bInitialised
= bSetTo
;
213 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, cec_version libCECSpecVersion
/* = CEC_VERSION_1_4 */)
216 CCECBusDevice
*device
= m_busDevices
->At(address
);
219 // check if it's already marked as present or used
220 if (device
->IsPresent() || device
->IsHandledByLibCEC())
223 // poll the LA if not
224 return device
->TryLogicalAddress(libCECSpecVersion
);
230 void CCECProcessor::ReplaceHandlers(void)
232 if (!CECInitialised())
236 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
237 it
->second
->ReplaceHandler(true);
240 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
242 return m_inBuffer
.Push(command
);
245 void *CCECProcessor::Process(void)
247 m_libcec
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
250 m_connCheck
= new CCECStandbyProtection(this);
251 m_connCheck
->CreateThread();
253 cec_command command
; command
.Clear();
254 CTimeout
activeSourceCheck(ACTIVE_SOURCE_CHECK_INTERVAL
);
255 CTimeout
tvPresentCheck(TV_PRESENT_CHECK_INTERVAL
);
257 // as long as we're not being stopped and the connection is open
258 while (!IsStopped() && m_communication
->IsOpen())
260 // wait for a new incoming command, and process it
261 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
262 ProcessCommand(command
);
264 if (CECInitialised() && !IsStopped())
266 // check clients for keypress timeouts
267 m_libcec
->CheckKeypressTimeout();
269 // check if we need to replace handlers
272 // check whether we need to activate a source, if it failed before
273 if (activeSourceCheck
.TimeLeft() == 0)
275 if (CECInitialised())
276 TransmitPendingActiveSourceCommands();
277 activeSourceCheck
.Init(ACTIVE_SOURCE_CHECK_INTERVAL
);
280 // check whether the TV is present and responding
281 if (tvPresentCheck
.TimeLeft() == 0)
283 CCECClient
*primary
= GetPrimaryClient();
284 // only check whether the tv responds to polls when a client is connected and not in monitoring mode
285 if (primary
&& primary
->GetConfiguration()->bMonitorOnly
!= 1)
287 if (!m_busDevices
->At(CECDEVICE_TV
)->IsPresent())
289 libcec_parameter param
;
290 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
291 param
.paramData
= (void*)"TV does not respond to CEC polls";
292 primary
->Alert(CEC_ALERT_TV_POLL_FAILED
, param
);
295 tvPresentCheck
.Init(TV_PRESENT_CHECK_INTERVAL
);
303 bool CCECProcessor::ActivateSource(uint16_t iStreamPath
)
307 // find the device with the given PA
308 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
309 // and make it the active source when found
311 bReturn
= device
->ActivateSource();
313 m_libcec
->AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
318 void CCECProcessor::SetActiveSource(bool bSetTo
, bool bClientUnregistered
)
321 m_communication
->SetActiveSource(bSetTo
, bClientUnregistered
);
324 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
326 CLockObject
lock(m_mutex
);
327 m_iStandardLineTimeout
= iTimeout
;
330 uint8_t CCECProcessor::GetStandardLineTimeout(void)
332 CLockObject
lock(m_mutex
);
333 return m_iStandardLineTimeout
;
336 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
338 CLockObject
lock(m_mutex
);
339 m_iRetryLineTimeout
= iTimeout
;
342 uint8_t CCECProcessor::GetRetryLineTimeout(void)
344 CLockObject
lock(m_mutex
);
345 return m_iRetryLineTimeout
;
348 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
350 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iPhysicalAddress
);
351 return device
!= NULL
;
354 void CCECProcessor::LogOutput(const cec_command
&data
)
358 // initiator and destination
359 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
363 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
365 // append the parameters
366 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
367 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
370 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
373 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
375 // try to find the primary device
376 CCECBusDevice
*primary
= GetPrimaryDevice();
377 // poll the destination, with the primary as source
379 return primary
->TransmitPoll(iAddress
, true);
381 CCECBusDevice
*device
= m_busDevices
->At(CECDEVICE_UNREGISTERED
);
383 return device
->TransmitPoll(iAddress
, true);
388 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
390 return m_busDevices
?
391 m_busDevices
->GetDeviceByPhysicalAddress(iPhysicalAddress
, bSuppressUpdate
) :
395 CCECBusDevice
*CCECProcessor::GetDevice(cec_logical_address address
) const
397 return m_busDevices
?
398 m_busDevices
->At(address
) :
402 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
404 // get the device that is marked as active source from the device map
405 CCECBusDevice
*activeSource
= m_busDevices
->GetActiveSource();
407 return activeSource
->GetLogicalAddress();
409 if (bRequestActiveSource
)
411 // request the active source from the bus
412 CCECBusDevice
*primary
= GetPrimaryDevice();
415 primary
->RequestActiveSource();
416 return GetActiveSource(false);
421 return CECDEVICE_UNKNOWN
;
424 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
426 CCECBusDevice
*device
= m_busDevices
->At(iAddress
);
427 return device
&& device
->IsActiveSource();
430 bool CCECProcessor::Transmit(const cec_command
&data
, bool bIsReply
)
432 cec_command
transmitData(data
);
433 uint8_t iMaxTries(0);
437 // get the current timeout setting
438 uint8_t iLineTimeout(GetStandardLineTimeout());
440 // reset the state of this message to 'unknown'
441 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
443 CLockObject
lock(m_mutex
);
444 if (!m_communication
)
447 if (!m_communication
->SupportsSourceLogicalAddress(transmitData
.initiator
))
449 if (transmitData
.initiator
== CECDEVICE_UNREGISTERED
&& m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
451 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter. using '%s' instead", ToString(transmitData
.initiator
), ToString(CECDEVICE_FREEUSE
));
452 transmitData
.initiator
= CECDEVICE_FREEUSE
;
456 m_libcec
->AddLog(CEC_LOG_DEBUG
, "initiator '%s' is not supported by the CEC adapter", ToString(transmitData
.initiator
));
461 LogOutput(transmitData
);
463 // find the initiator device
464 CCECBusDevice
*initiator
= m_busDevices
->At(transmitData
.initiator
);
467 m_libcec
->AddLog(CEC_LOG_WARNING
, "invalid initiator");
471 // find the destination device, if it's not the broadcast address
472 if (transmitData
.destination
!= CECDEVICE_BROADCAST
)
474 // check if the device is marked as handled by libCEC
475 CCECBusDevice
*destination
= m_busDevices
->At(transmitData
.destination
);
476 if (destination
&& destination
->IsHandledByLibCEC())
478 // and reject the command if it's trying to send data to a device that is handled by libCEC
479 m_libcec
->AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
484 // wait until we finished allocating a new LA if it got lost
486 while (m_bStallCommunication
) Sleep(5);
489 m_iLastTransmission
= GetTimeMs();
490 // set the number of tries
491 iMaxTries
= initiator
->GetHandler()->GetTransmitRetries() + 1;
492 initiator
->MarkHandlerReady();
494 // and try to send the command
495 while (bRetry
&& ++iTries
< iMaxTries
)
497 if (initiator
->IsUnsupportedFeature(transmitData
.opcode
))
500 adapterState
= !IsStopped() && m_communication
&& m_communication
->IsOpen() ?
501 m_communication
->Write(transmitData
, bRetry
, iLineTimeout
, bIsReply
) :
502 ADAPTER_MESSAGE_STATE_ERROR
;
503 iLineTimeout
= m_iRetryLineTimeout
;
507 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
|| adapterState
== ADAPTER_MESSAGE_STATE_SENT
|| adapterState
== ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT
:
508 adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
511 void CCECProcessor::TransmitAbort(cec_logical_address source
, cec_logical_address destination
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
513 m_libcec
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
516 cec_command::Format(command
, source
, destination
, CEC_OPCODE_FEATURE_ABORT
);
517 command
.parameters
.PushBack((uint8_t)opcode
);
518 command
.parameters
.PushBack((uint8_t)reason
);
520 Transmit(command
, true);
523 void CCECProcessor::ProcessCommand(const cec_command
&command
)
526 m_libcec
->AddLog(CEC_LOG_TRAFFIC
, ToString(command
).c_str());
528 // find the initiator
529 CCECBusDevice
*device
= m_busDevices
->At(command
.initiator
);
532 device
->HandleCommand(command
);
535 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
537 CCECBusDevice
*device
= m_busDevices
->At(address
);
538 return device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
541 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
543 CECDEVICEVEC devices
;
544 m_busDevices
->GetByType(type
, devices
);
545 CCECDeviceMap::FilterActive(devices
);
546 return !devices
.empty();
549 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
551 return m_communication
? m_communication
->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS
;
554 bool CCECProcessor::ClearLogicalAddresses(void)
556 cec_logical_addresses addresses
; addresses
.Clear();
557 return SetLogicalAddresses(addresses
);
560 bool CCECProcessor::SetLogicalAddresses(const cec_logical_addresses
&addresses
)
562 return m_communication
? m_communication
->SetLogicalAddresses(addresses
) : false;
565 bool CCECProcessor::StandbyDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
568 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
569 bReturn
&= (*it
)->Standby(initiator
);
573 bool CCECProcessor::StandbyDevice(const cec_logical_address initiator
, cec_logical_address address
)
575 CCECBusDevice
*device
= m_busDevices
->At(address
);
576 return device
? device
->Standby(initiator
) : false;
579 bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator
, const CECDEVICEVEC
&devices
)
582 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
583 bReturn
&= (*it
)->PowerOn(initiator
);
587 bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator
, cec_logical_address address
)
589 CCECBusDevice
*device
= m_busDevices
->At(address
);
590 return device
? device
->PowerOn(initiator
) : false;
593 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
596 // open a connection if no connection has been opened
597 if (!m_communication
&& strPort
)
599 CAdapterFactory
factory(this->m_libcec
);
600 IAdapterCommunication
*comm
= factory
.GetInstance(strPort
);
601 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
603 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
605 m_libcec
->AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
607 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
611 bReturn
= comm
->StartBootloader();
612 DELETE_AND_NULL(comm
);
618 m_communication
->StartBootloader();
626 bool CCECProcessor::PingAdapter(void)
628 return m_communication
->PingAdapter();
631 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
633 CCECBusDevice
*device
= m_busDevices
->At(destination
);
635 device
->HandlePollFrom(initiator
);
638 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
640 CCECBusDevice
*device
= m_busDevices
->At(initiator
);
641 return !device
|| !device
->HandleReceiveFailed();
644 bool CCECProcessor::CanPersistConfiguration(void)
646 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
649 bool CCECProcessor::PersistConfiguration(const libcec_configuration
&configuration
)
651 libcec_configuration persistConfiguration
= configuration
;
652 if (!CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
654 CCECBusDevice
*device
= GetPrimaryDevice();
656 persistConfiguration
.iPhysicalAddress
= device
->GetCurrentPhysicalAddress();
659 return m_communication
? m_communication
->PersistConfiguration(persistConfiguration
) : false;
662 void CCECProcessor::RescanActiveDevices(void)
664 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
665 it
->second
->GetStatus(true);
668 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
670 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
673 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
674 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
675 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
676 config
->adapterType
= m_communication
->GetAdapterType();
683 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
686 for (CECDEVICEMAP::iterator it
= m_busDevices
->Begin(); it
!= m_busDevices
->End(); it
++)
687 bReturn
&= it
->second
->TransmitPendingActiveSourceCommands();
691 CCECTV
*CCECProcessor::GetTV(void) const
693 return CCECBusDevice::AsTV(m_busDevices
->At(CECDEVICE_TV
));
696 CCECAudioSystem
*CCECProcessor::GetAudioSystem(void) const
698 return CCECBusDevice::AsAudioSystem(m_busDevices
->At(CECDEVICE_AUDIOSYSTEM
));
701 CCECPlaybackDevice
*CCECProcessor::GetPlaybackDevice(cec_logical_address address
) const
703 return CCECBusDevice::AsPlaybackDevice(m_busDevices
->At(address
));
706 CCECRecordingDevice
*CCECProcessor::GetRecordingDevice(cec_logical_address address
) const
708 return CCECBusDevice::AsRecordingDevice(m_busDevices
->At(address
));
711 CCECTuner
*CCECProcessor::GetTuner(cec_logical_address address
) const
713 return CCECBusDevice::AsTuner(m_busDevices
->At(address
));
716 bool CCECProcessor::AllocateLogicalAddresses(CCECClient
* client
)
718 libcec_configuration
&configuration
= *client
->GetConfiguration();
720 // mark as unregistered
721 client
->SetRegistered(false);
723 // unregister this client from the old addresses
724 CECDEVICEVEC devices
;
725 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
726 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
728 // remove client entry
729 CLockObject
lock(m_mutex
);
730 m_clients
.erase((*it
)->GetLogicalAddress());
733 // find logical addresses for this client
734 if (!client
->AllocateLogicalAddresses())
736 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to find a free logical address for the client");
740 // register this client on the new addresses
742 m_busDevices
->GetByLogicalAddresses(devices
, configuration
.logicalAddresses
);
743 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
745 // set the physical address of the device at this LA
746 if (CLibCEC::IsValidPhysicalAddress(configuration
.iPhysicalAddress
))
747 (*it
)->SetPhysicalAddress(configuration
.iPhysicalAddress
);
749 // replace a previous client
750 CLockObject
lock(m_mutex
);
751 m_clients
.erase((*it
)->GetLogicalAddress());
752 m_clients
.insert(make_pair((*it
)->GetLogicalAddress(), client
));
755 // set the new ackmask
756 SetLogicalAddresses(GetLogicalAddresses());
758 // resume outgoing communication
759 m_bStallCommunication
= false;
764 uint16_t CCECProcessor::GetPhysicalAddressFromEeprom(void)
766 libcec_configuration config
; config
.Clear();
768 m_communication
->GetConfiguration(config
);
769 return config
.iPhysicalAddress
;
772 bool CCECProcessor::RegisterClient(CCECClient
*client
)
777 libcec_configuration
&configuration
= *client
->GetConfiguration();
779 if (configuration
.clientVersion
< CEC_CLIENT_VERSION_2_0_0
)
781 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
));
785 if (configuration
.bMonitorOnly
== 1)
788 if (!CECInitialised())
790 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: CEC processor is not initialised");
794 // unregister the client first if it's already been marked as registered
795 if (client
->IsRegistered())
796 UnregisterClient(client
);
798 // ensure that controlled mode is enabled
799 m_communication
->SetControlledMode(true);
802 // source logical address for requests
803 cec_logical_address
sourceAddress(CECDEVICE_UNREGISTERED
);
804 if (!m_communication
->SupportsSourceLogicalAddress(CECDEVICE_UNREGISTERED
))
806 if (m_communication
->SupportsSourceLogicalAddress(CECDEVICE_FREEUSE
))
807 sourceAddress
= CECDEVICE_FREEUSE
;
810 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register a new CEC client: both unregistered and free use are not supported by the device");
815 // ensure that we know the vendor id of the TV
816 CCECBusDevice
*tv
= GetTV();
817 cec_vendor_id
tvVendor(tv
->GetVendorId(sourceAddress
));
819 // wait until the handler is replaced, to avoid double registrations
820 if (tvVendor
!= CEC_VENDOR_UNKNOWN
&&
821 CCECCommandHandler::HasSpecificHandler(tvVendor
))
823 while (!tv
->ReplaceHandler(false))
827 // get the configuration from the client
828 m_libcec
->AddLog(CEC_LOG_NOTICE
, "registering new CEC client - v%s", ToString((cec_client_version
)configuration
.clientVersion
));
830 // get the current ackmask, so we can restore it if polling fails
831 cec_logical_addresses previousMask
= GetLogicalAddresses();
833 // mark as uninitialised
834 client
->SetInitialised(false);
836 // find logical addresses for this client
837 if (!AllocateLogicalAddresses(client
))
839 m_libcec
->AddLog(CEC_LOG_ERROR
, "failed to register the new CEC client - cannot allocate the requested device types");
840 SetLogicalAddresses(previousMask
);
844 // get the settings from the rom
845 if (configuration
.bGetSettingsFromROM
== 1)
847 libcec_configuration config
; config
.Clear();
848 m_communication
->GetConfiguration(config
);
850 CLockObject
lock(m_mutex
);
851 if (!config
.deviceTypes
.IsEmpty())
852 configuration
.deviceTypes
= config
.deviceTypes
;
853 if (CLibCEC::IsValidPhysicalAddress(config
.iPhysicalAddress
))
854 configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
855 snprintf(configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
858 // set the firmware version and build date
859 configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
860 configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
861 configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
862 configuration
.adapterType
= m_communication
->GetAdapterType();
864 // mark the client as registered
865 client
->SetRegistered(true);
867 sourceAddress
= client
->GetPrimaryLogicalAdddress();
869 // initialise the client
870 bool bReturn
= client
->OnRegister();
872 // log the new registration
874 strLog
.Format("%s: %s", bReturn
? "CEC client registered" : "failed to register the CEC client", client
->GetConnectionInfo().c_str());
875 m_libcec
->AddLog(bReturn
? CEC_LOG_NOTICE
: CEC_LOG_ERROR
, strLog
);
877 // display a warning if the firmware can be upgraded
878 if (bReturn
&& !IsRunningLatestFirmware())
880 const char *strUpgradeMessage
= "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
881 m_libcec
->AddLog(CEC_LOG_WARNING
, strUpgradeMessage
);
882 libcec_parameter param
;
883 param
.paramData
= (void*)strUpgradeMessage
; param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
884 client
->Alert(CEC_ALERT_SERVICE_DEVICE
, param
);
887 // ensure that the command handler for the TV is initialised
890 CCECCommandHandler
*handler
= GetTV()->GetHandler();
892 handler
->InitHandler();
893 GetTV()->MarkHandlerReady();
896 // report our OSD name to the TV, since some TVs don't request it
897 client
->GetPrimaryDevice()->TransmitOSDName(CECDEVICE_TV
, false);
899 // request the power status of the TV
900 tv
->RequestPowerStatus(sourceAddress
, true, true);
905 bool CCECProcessor::UnregisterClient(CCECClient
*client
)
910 if (client
->IsRegistered())
911 m_libcec
->AddLog(CEC_LOG_NOTICE
, "unregistering client: %s", client
->GetConnectionInfo().c_str());
913 // notify the client that it will be unregistered
914 client
->OnUnregister();
917 CLockObject
lock(m_mutex
);
918 // find all devices that match the LA's of this client
919 CECDEVICEVEC devices
;
920 m_busDevices
->GetByLogicalAddresses(devices
, client
->GetConfiguration()->logicalAddresses
);
921 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
924 map
<cec_logical_address
, CCECClient
*>::iterator entry
= m_clients
.find((*it
)->GetLogicalAddress());
925 // unregister the client
926 if (entry
!= m_clients
.end())
927 m_clients
.erase(entry
);
929 // reset the device status
930 (*it
)->ResetDeviceStatus(true);
934 // set the new ackmask
935 cec_logical_addresses addresses
= GetLogicalAddresses();
936 if (SetLogicalAddresses(addresses
))
938 // no more clients left, disable controlled mode
939 if (addresses
.IsEmpty() && !m_bMonitor
)
940 m_communication
->SetControlledMode(false);
948 void CCECProcessor::UnregisterClients(void)
950 m_libcec
->AddLog(CEC_LOG_DEBUG
, "unregistering all CEC clients");
952 vector
<CCECClient
*> clients
= m_libcec
->GetClients();
953 for (vector
<CCECClient
*>::iterator client
= clients
.begin(); client
!= clients
.end(); client
++)
954 UnregisterClient(*client
);
956 CLockObject
lock(m_mutex
);
960 CCECClient
*CCECProcessor::GetClient(const cec_logical_address address
)
962 CLockObject
lock(m_mutex
);
963 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.find(address
);
964 if (client
!= m_clients
.end())
965 return client
->second
;
969 CCECClient
*CCECProcessor::GetPrimaryClient(void)
971 CLockObject
lock(m_mutex
);
972 map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin();
973 if (client
!= m_clients
.end())
974 return client
->second
;
978 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void)
980 return m_busDevices
->At(GetLogicalAddress());
983 cec_logical_address
CCECProcessor::GetLogicalAddress(void)
985 cec_logical_addresses addresses
= GetLogicalAddresses();
986 return addresses
.primary
;
989 cec_logical_addresses
CCECProcessor::GetLogicalAddresses(void)
991 CLockObject
lock(m_mutex
);
992 cec_logical_addresses addresses
;
994 for (map
<cec_logical_address
, CCECClient
*>::const_iterator client
= m_clients
.begin(); client
!= m_clients
.end(); client
++)
995 addresses
.Set(client
->first
);
1000 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address
) const
1002 CCECBusDevice
*device
= GetDevice(address
);
1003 return device
&& device
->IsHandledByLibCEC();
1006 bool CCECProcessor::IsRunningLatestFirmware(void)
1008 return m_communication
&& m_communication
->IsOpen() ?
1009 m_communication
->IsRunningLatestFirmware() :
1013 void CCECProcessor::SwitchMonitoring(bool bSwitchTo
)
1016 CLockObject
lock(m_mutex
);
1017 m_bMonitor
= bSwitchTo
;
1020 UnregisterClients();
1023 void CCECProcessor::HandleLogicalAddressLost(cec_logical_address oldAddress
)
1025 // stall outgoing messages until we know our new LA
1026 m_bStallCommunication
= true;
1028 m_libcec
->AddLog(CEC_LOG_NOTICE
, "logical address %x was taken by another device, allocating a new address", oldAddress
);
1029 CCECClient
* client
= GetClient(oldAddress
);
1031 client
= GetPrimaryClient();
1034 if (m_addrAllocator
)
1035 while (m_addrAllocator
->IsRunning()) Sleep(5);
1036 delete m_addrAllocator
;
1038 m_addrAllocator
= new CCECAllocateLogicalAddress(this, client
);
1039 m_addrAllocator
->CreateThread();
1043 void CCECProcessor::HandlePhysicalAddressChanged(uint16_t iNewAddress
)
1045 m_libcec
->AddLog(CEC_LOG_NOTICE
, "physical address changed to %04x", iNewAddress
);
1046 CCECClient
* client
= GetPrimaryClient();
1048 client
->SetPhysicalAddress(iNewAddress
);
1051 uint16_t CCECProcessor::GetAdapterVendorId(void) const
1053 return m_communication
? m_communication
->GetAdapterVendorId() : 0;
1056 uint16_t CCECProcessor::GetAdapterProductId(void) const
1058 return m_communication
? m_communication
->GetAdapterProductId() : 0;
1061 CCECAllocateLogicalAddress::CCECAllocateLogicalAddress(CCECProcessor
* processor
, CCECClient
* client
) :
1062 m_processor(processor
),
1063 m_client(client
) { }
1065 void* CCECAllocateLogicalAddress::Process(void)
1067 m_processor
->AllocateLogicalAddresses(m_client
);