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