cec-config: fix physical address detection
[deb_libcec.git] / src / lib / CECProcessor.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33#include "CECProcessor.h"
34
35#include "adapter/USBCECAdapterCommunication.h"
36#include "devices/CECBusDevice.h"
37#include "devices/CECAudioSystem.h"
38#include "devices/CECPlaybackDevice.h"
39#include "devices/CECRecordingDevice.h"
40#include "devices/CECTuner.h"
41#include "devices/CECTV.h"
42#include "implementations/CECCommandHandler.h"
43#include "LibCEC.h"
44#include "CECClient.h"
45#include "platform/util/timeutils.h"
46
47using namespace CEC;
48using namespace std;
49using namespace PLATFORM;
50
51#define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
52
53#define ToString(x) m_libcec->ToString(x)
54
55CCECProcessor::CCECProcessor(CLibCEC *libcec) :
56 m_bInitialised(false),
57 m_communication(NULL),
58 m_libcec(libcec),
59 m_iStandardLineTimeout(3),
60 m_iRetryLineTimeout(3),
61 m_iLastTransmission(0)
62{
63 m_busDevices = new CCECDeviceMap(this);
64}
65
66CCECProcessor::~CCECProcessor(void)
67{
68 Close();
69 delete m_busDevices;
70}
71
72bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
73{
74 CLockObject lock(m_mutex);
75 // open a connection
76 if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
77 return false;
78
79 // create the processor thread
80 if (!IsRunning())
81 {
82 if (!CreateThread())
83 {
84 m_libcec->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
85 return false;
86 }
87 }
88
89 // mark as initialised
90 SetCECInitialised(true);
91
92 return true;
93}
94
95void CCECProcessor::Close(void)
96{
97 // mark as uninitialised
98 SetCECInitialised(false);
99
100 // stop the processor
101 StopThread();
102
103 // close the connection
104 if (m_communication)
105 {
106 delete m_communication;
107 m_communication = NULL;
108 }
109}
110
111void CCECProcessor::ResetMembers(void)
112{
113 // close the connection
114 if (m_communication)
115 {
116 delete m_communication;
117 m_communication = NULL;
118 }
119
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();
125}
126
127bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */)
128{
129 bool bReturn(false);
130 CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
131
132 // ensure that a previous connection is closed
133 Close();
134
135 // reset all member to the initial state
136 ResetMembers();
137
138 // check whether the Close() method deleted any previous connection
139 if (m_communication)
140 {
141 m_libcec->AddLog(CEC_LOG_ERROR, "previous connection could not be closed");
142 return bReturn;
143 }
144
145 // create a new connection
146 m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
147
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)
151 {
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);
155 }
156
157 m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened");
158
159 return bReturn;
160}
161
162bool CCECProcessor::CECInitialised(void)
163{
164 CLockObject lock(m_threadMutex);
165 return m_bInitialised;
166}
167
168void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */)
169{
170 {
171 CLockObject lock(m_mutex);
172 m_bInitialised = bSetTo;
173 }
174 if (!bSetTo)
175 UnregisterClients();
176}
177
178bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
179{
180 // find the device
181 CCECBusDevice *device = m_busDevices->At(address);
182 if (device)
183 {
184 // check if it's already marked as present or used
185 if (device->IsPresent() || device->IsHandledByLibCEC())
186 return false;
187
188 // poll the LA if not
189 SetAckMask(0);
190 return device->TryLogicalAddress();
191 }
192
193 return false;
194}
195
196void CCECProcessor::ReplaceHandlers(void)
197{
198 if (!CECInitialised())
199 return;
200
201 // check each device
202 for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
203 it->second->ReplaceHandler(true);
204}
205
206bool CCECProcessor::OnCommandReceived(const cec_command &command)
207{
208 return m_inBuffer.Push(command);
209}
210
211void *CCECProcessor::Process(void)
212{
213 m_libcec->AddLog(CEC_LOG_DEBUG, "processor thread started");
214
215 cec_command command;
216
217 // as long as we're not being stopped and the connection is open
218 while (!IsStopped() && m_communication->IsOpen())
219 {
220 // wait for a new incoming command, and process it
221 if (m_inBuffer.Pop(command, CEC_PROCESSOR_SIGNAL_WAIT_TIME))
222 ProcessCommand(command);
223
224 if (CECInitialised())
225 {
226 // check clients for keypress timeouts
227 m_libcec->CheckKeypressTimeout();
228
229 // check if we need to replace handlers
230 ReplaceHandlers();
231 }
232 }
233
234 return NULL;
235}
236
237bool CCECProcessor::ActivateSource(uint16_t iStreamPath)
238{
239 bool bReturn(false);
240
241 // find the device with the given PA
242 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
243 // and make it the active source when found
244 if (device)
245 bReturn = device->ActivateSource();
246 else
247 m_libcec->AddLog(CEC_LOG_DEBUG, "device with PA '%04x' not found", iStreamPath);
248
249 return bReturn;
250}
251
252void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
253{
254 CLockObject lock(m_mutex);
255 m_iStandardLineTimeout = iTimeout;
256}
257
258uint8_t CCECProcessor::GetStandardLineTimeout(void)
259{
260 CLockObject lock(m_mutex);
261 return m_iStandardLineTimeout;
262}
263
264void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
265{
266 CLockObject lock(m_mutex);
267 m_iRetryLineTimeout = iTimeout;
268}
269
270uint8_t CCECProcessor::GetRetryLineTimeout(void)
271{
272 CLockObject lock(m_mutex);
273 return m_iRetryLineTimeout;
274}
275
276bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
277{
278 CCECBusDevice *device = GetDeviceByPhysicalAddress(iPhysicalAddress);
279 return device != NULL;
280}
281
282void CCECProcessor::LogOutput(const cec_command &data)
283{
284 CStdString strTx;
285
286 // initiator and destination
287 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
288
289 // append the opcode
290 if (data.opcode_set)
291 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
292
293 // append the parameters
294 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
295 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
296
297 // and log it
298 m_libcec->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
299}
300
301bool CCECProcessor::PollDevice(cec_logical_address iAddress)
302{
303 // try to find the primary device
304 CCECBusDevice *primary = GetPrimaryDevice();
305 // poll the destination, with the primary as source
306 if (primary)
307 return primary->TransmitPoll(iAddress);
308
309 // try to find the destination
310 CCECBusDevice *device = m_busDevices->At(iAddress);
311 // and poll the destination, with the same LA as source
312 if (device)
313 return device->TransmitPoll(iAddress);
314
315 return false;
316}
317
318CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */)
319{
320 return m_busDevices ?
321 m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) :
322 NULL;
323}
324
325CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const
326{
327 return m_busDevices ?
328 m_busDevices->At(address) :
329 NULL;
330}
331
332cec_logical_address CCECProcessor::GetActiveSource(bool bRequestActiveSource /* = true */)
333{
334 // get the device that is marked as active source from the device map
335 CCECBusDevice *activeSource = m_busDevices->GetActiveSource();
336 if (activeSource)
337 return activeSource->GetLogicalAddress();
338
339 if (bRequestActiveSource)
340 {
341 // request the active source from the bus
342 CCECBusDevice *primary = GetPrimaryDevice();
343 if (primary)
344 {
345 primary->RequestActiveSource();
346 return GetActiveSource(false);
347 }
348 }
349
350 // unknown or none
351 return CECDEVICE_UNKNOWN;
352}
353
354bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
355{
356 CCECBusDevice *device = m_busDevices->At(iAddress);
357 return device && device->IsActiveSource();
358}
359
360bool CCECProcessor::Transmit(const cec_command &data)
361{
362 uint8_t iMaxTries(0);
363 bool bRetry(true);
364 uint8_t iTries(0);
365
366 // get the current timeout setting
367 uint8_t iLineTimeout(GetStandardLineTimeout());
368
369 // reset the state of this message to 'unknown'
370 cec_adapter_message_state adapterState = ADAPTER_MESSAGE_STATE_UNKNOWN;
371
372 LogOutput(data);
373
374 // find the initiator device
375 CCECBusDevice *initiator = m_busDevices->At(data.initiator);
376 if (!initiator)
377 {
378 m_libcec->AddLog(CEC_LOG_WARNING, "invalid initiator");
379 return false;
380 }
381
382 // find the destination device, if it's not the broadcast address
383 if (data.destination != CECDEVICE_BROADCAST)
384 {
385 // check if the device is marked as handled by libCEC
386 CCECBusDevice *destination = m_busDevices->At(data.destination);
387 if (destination && destination->IsHandledByLibCEC())
388 {
389 // and reject the command if it's trying to send data to a device that is handled by libCEC
390 m_libcec->AddLog(CEC_LOG_WARNING, "not sending data to myself!");
391 return false;
392 }
393 }
394
395 {
396 CLockObject lock(m_mutex);
397 m_iLastTransmission = GetTimeMs();
398 // set the number of tries
399 iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1;
400 }
401
402 // and try to send the command
403 while (bRetry && ++iTries < iMaxTries)
404 {
405 if (initiator->IsUnsupportedFeature(data.opcode))
406 return false;
407
408 adapterState = !IsStopped() && m_communication && m_communication->IsOpen() ?
409 m_communication->Write(data, bRetry, iLineTimeout) :
410 ADAPTER_MESSAGE_STATE_ERROR;
411 iLineTimeout = m_iRetryLineTimeout;
412 }
413
414 return adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED;
415}
416
417void CCECProcessor::TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
418{
419 m_libcec->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
420
421 cec_command command;
422 cec_command::Format(command, source, destination, CEC_OPCODE_FEATURE_ABORT);
423 command.parameters.PushBack((uint8_t)opcode);
424 command.parameters.PushBack((uint8_t)reason);
425
426 Transmit(command);
427}
428
429void CCECProcessor::ProcessCommand(const cec_command &command)
430{
431 // log the command
432 CStdString dataStr;
433 dataStr.Format(">> %1x%1x", command.initiator, command.destination);
434 if (command.opcode_set == 1)
435 dataStr.AppendFormat(":%02x", command.opcode);
436 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
437 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
438 m_libcec->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
439
440 // find the initiator
441 CCECBusDevice *device = m_busDevices->At(command.initiator);
442
443 if (device)
444 device->HandleCommand(command);
445}
446
447bool CCECProcessor::IsPresentDevice(cec_logical_address address)
448{
449 CCECBusDevice *device = m_busDevices->At(address);
450 return device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
451}
452
453bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
454{
455 CECDEVICEVEC devices;
456 m_busDevices->GetByType(type, devices);
457 CCECDeviceMap::FilterActive(devices);
458 return !devices.empty();
459}
460
461uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
462{
463 return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
464}
465
466bool CCECProcessor::SetAckMask(uint16_t iMask)
467{
468 return m_communication ? m_communication->SetAckMask(iMask) : false;
469}
470
471bool CCECProcessor::StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
472{
473 bool bReturn(true);
474 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
475 bReturn &= (*it)->Standby(initiator);
476 return bReturn;
477}
478
479bool CCECProcessor::StandbyDevice(const cec_logical_address initiator, cec_logical_address address)
480{
481 CCECBusDevice *device = m_busDevices->At(address);
482 return device ? device->Standby(initiator) : false;
483}
484
485bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
486{
487 bool bReturn(true);
488 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
489 bReturn &= (*it)->PowerOn(initiator);
490 return bReturn;
491}
492
493bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator, cec_logical_address address)
494{
495 CCECBusDevice *device = m_busDevices->At(address);
496 return device ? device->PowerOn(initiator) : false;
497}
498
499bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
500{
501 bool bReturn(false);
502 // open a connection if no connection has been opened
503 if (!m_communication && strPort)
504 {
505 IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
506 CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
507 int iConnectTry(0);
508 while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
509 {
510 m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
511 comm->Close();
512 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
513 }
514 if (comm->IsOpen())
515 {
516 bReturn = comm->StartBootloader();
517 delete comm;
518 }
519 return bReturn;
520 }
521 else
522 {
523 m_communication->StartBootloader();
524 Close();
525 bReturn = true;
526 }
527
528 return bReturn;
529}
530
531bool CCECProcessor::PingAdapter(void)
532{
533 return m_communication->PingAdapter();
534}
535
536void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
537{
538 CCECBusDevice *device = m_busDevices->At(destination);
539 if (device)
540 device->HandlePollFrom(initiator);
541}
542
543bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
544{
545 CCECBusDevice *device = m_busDevices->At(initiator);
546 return !device || !device->HandleReceiveFailed();
547}
548
549bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
550{
551 // stream path changes are sent by the TV
552 return GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
553}
554
555bool CCECProcessor::CanPersistConfiguration(void)
556{
557 return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false;
558}
559
560bool CCECProcessor::PersistConfiguration(const libcec_configuration &configuration)
561{
562 return m_communication ? m_communication->PersistConfiguration(configuration) : false;
563}
564
565void CCECProcessor::RescanActiveDevices(void)
566{
567 for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
568 it->second->GetStatus(true);
569}
570
571bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
572{
573 if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false))
574 return false;
575
576 config->iFirmwareVersion = m_communication->GetFirmwareVersion();
577 config->iPhysicalAddress = m_communication->GetPhysicalAddress();
578 config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
579
580 return true;
581}
582
583bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
584{
585 bool bReturn(true);
586 for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
587 bReturn &= it->second->TransmitPendingActiveSourceCommands();
588 return bReturn;
589}
590
591CCECTV *CCECProcessor::GetTV(void) const
592{
593 return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV));
594}
595
596CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const
597{
598 return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM));
599}
600
601CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const
602{
603 return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address));
604}
605
606CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const
607{
608 return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address));
609}
610
611CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const
612{
613 return CCECBusDevice::AsTuner(m_busDevices->At(address));
614}
615
616bool CCECProcessor::RegisterClient(CCECClient *client)
617{
618 if (!client || !IsRunning())
619 return false;
620
621 // unregister the client first if it's already been marked as registered
622 if (client->IsRegistered())
623 UnregisterClient(client);
624
625 // get the configuration from the client
626 libcec_configuration &configuration = *client->GetConfiguration();
627 m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
628
629 // mark as uninitialised and unregistered
630 client->SetRegistered(false);
631 client->SetInitialised(false);
632
633 // get the current ackmask, so we can restore it if polling fails
634 uint16_t iPreviousMask(m_communication->GetAckMask());
635
636 // find logical addresses for this client
637 if (!client->AllocateLogicalAddresses())
638 {
639 m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types");
640 SetAckMask(iPreviousMask);
641 return false;
642 }
643
644 // register this client on the new addresses
645 CECDEVICEVEC devices;
646 m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses);
647 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
648 {
649 // replace a previous client
650 CLockObject lock(m_mutex);
651 m_clients.erase((*it)->GetLogicalAddress());
652 m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
653 }
654
655 // get the settings from the rom
656 if (configuration.bGetSettingsFromROM == 1)
657 {
658 libcec_configuration config;
659 m_communication->GetConfiguration(config);
660
661 CLockObject lock(m_mutex);
662 if (!config.deviceTypes.IsEmpty())
663 configuration.deviceTypes = config.deviceTypes;
664 if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress))
665 configuration.iPhysicalAddress = config.iPhysicalAddress;
666 snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName);
667 }
668
669 // set the firmware version and build date
670 configuration.serverVersion = LIBCEC_VERSION_CURRENT;
671 configuration.iFirmwareVersion = m_communication->GetFirmwareVersion();
672 configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
673
674 // mark the client as registered
675 client->SetRegistered(true);
676
677 // set the new ack mask
678 bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) &&
679 // and initialise the client
680 client->OnRegister();
681
682 // log the new registration
683 CStdString strLog;
684 strLog.Format("%s: %s", bReturn ? "CEC client registered" : "failed to register the CEC client", client->GetConnectionInfo().c_str());
685 m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog);
686
687 // display a warning if the firmware can be upgraded
688 if (bReturn && !IsRunningLatestFirmware())
689 {
690 const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
691 m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage);
692 libcec_parameter param;
693 param.paramData = (void*)strUpgradeMessage; param.paramType = CEC_PARAMETER_TYPE_STRING;
694 client->Alert(CEC_ALERT_SERVICE_DEVICE, param);
695 }
696
697 return bReturn;
698}
699
700bool CCECProcessor::UnregisterClient(CCECClient *client)
701{
702 if (!client)
703 return false;
704
705 if (client->IsRegistered())
706 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
707
708 // notify the client that it will be unregistered
709 client->OnUnregister();
710
711 {
712 CLockObject lock(m_mutex);
713 // find all devices that match the LA's of this client
714 CECDEVICEVEC devices;
715 m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses);
716 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
717 {
718 // find the client
719 map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress());
720 // unregister the client
721 if (entry != m_clients.end())
722 m_clients.erase(entry);
723
724 // reset the device status
725 (*it)->ResetDeviceStatus();
726 }
727 }
728
729 // set the new ackmask
730 return SetAckMask(GetLogicalAddresses().AckMask());
731}
732
733void CCECProcessor::UnregisterClients(void)
734{
735 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering all CEC clients");
736
737 vector<CCECClient *> clients = m_libcec->GetClients();
738 for (vector<CCECClient *>::iterator client = clients.begin(); client != clients.end(); client++)
739 UnregisterClient(*client);
740
741 CLockObject lock(m_mutex);
742 m_clients.clear();
743}
744
745CCECClient *CCECProcessor::GetClient(const cec_logical_address address)
746{
747 CLockObject lock(m_mutex);
748 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address);
749 if (client != m_clients.end())
750 return client->second;
751 return NULL;
752}
753
754CCECClient *CCECProcessor::GetPrimaryClient(void)
755{
756 CLockObject lock(m_mutex);
757 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin();
758 if (client != m_clients.end())
759 return client->second;
760 return NULL;
761}
762
763CCECBusDevice *CCECProcessor::GetPrimaryDevice(void)
764{
765 return m_busDevices->At(GetLogicalAddress());
766}
767
768cec_logical_address CCECProcessor::GetLogicalAddress(void)
769{
770 cec_logical_addresses addresses = GetLogicalAddresses();
771 return addresses.primary;
772}
773
774cec_logical_addresses CCECProcessor::GetLogicalAddresses(void)
775{
776 CLockObject lock(m_mutex);
777 cec_logical_addresses addresses;
778 addresses.Clear();
779 for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++)
780 addresses.Set(client->first);
781
782 return addresses;
783}
784
785bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const
786{
787 CCECBusDevice *device = GetDevice(address);
788 return device && device->IsHandledByLibCEC();
789}
790
791bool CCECProcessor::IsRunningLatestFirmware(void)
792{
793 return m_communication && m_communication->IsOpen() ?
794 m_communication->IsRunningLatestFirmware() :
795 true;
796}