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