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