cec: bump version to 1.6.3. check the client version, not the server version in libce...
[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 // mark as initialised
157 SetCECInitialised(true);
158
159 return bReturn;
160 }
161
162 bool CCECProcessor::CECInitialised(void)
163 {
164 CLockObject lock(m_threadMutex);
165 return m_bInitialised;
166 }
167
168 void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */)
169 {
170 {
171 CLockObject lock(m_mutex);
172 m_bInitialised = bSetTo;
173 }
174 if (!bSetTo)
175 UnregisterClients();
176 }
177
178 bool 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
196 void 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
206 bool CCECProcessor::OnCommandReceived(const cec_command &command)
207 {
208 return m_inBuffer.Push(command);
209 }
210
211 void *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
237 bool 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
252 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
253 {
254 CLockObject lock(m_mutex);
255 m_iStandardLineTimeout = iTimeout;
256 }
257
258 uint8_t CCECProcessor::GetStandardLineTimeout(void)
259 {
260 CLockObject lock(m_mutex);
261 return m_iStandardLineTimeout;
262 }
263
264 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
265 {
266 CLockObject lock(m_mutex);
267 m_iRetryLineTimeout = iTimeout;
268 }
269
270 uint8_t CCECProcessor::GetRetryLineTimeout(void)
271 {
272 CLockObject lock(m_mutex);
273 return m_iRetryLineTimeout;
274 }
275
276 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
277 {
278 CCECBusDevice *device = GetDeviceByPhysicalAddress(iPhysicalAddress);
279 return device != NULL;
280 }
281
282 void 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
301 bool 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
318 CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */)
319 {
320 return m_busDevices ?
321 m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) :
322 NULL;
323 }
324
325 CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const
326 {
327 return m_busDevices ?
328 m_busDevices->At(address) :
329 NULL;
330 }
331
332 cec_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
354 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
355 {
356 CCECBusDevice *device = m_busDevices->At(iAddress);
357 return device && device->IsActiveSource();
358 }
359
360 bool 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
417 void 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
429 void 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
447 bool 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
453 bool 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
461 uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
462 {
463 return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
464 }
465
466 bool CCECProcessor::SetAckMask(uint16_t iMask)
467 {
468 return m_communication ? m_communication->SetAckMask(iMask) : false;
469 }
470
471 bool 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
479 bool 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
485 bool 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
493 bool 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
499 bool 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
531 bool CCECProcessor::PingAdapter(void)
532 {
533 return m_communication->PingAdapter();
534 }
535
536 void 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
543 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
544 {
545 CCECBusDevice *device = m_busDevices->At(initiator);
546 return !device || !device->HandleReceiveFailed();
547 }
548
549 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
550 {
551 // stream path changes are sent by the TV
552 return GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
553 }
554
555 bool CCECProcessor::CanPersistConfiguration(void)
556 {
557 return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false;
558 }
559
560 bool CCECProcessor::PersistConfiguration(const libcec_configuration &configuration)
561 {
562 return m_communication ? m_communication->PersistConfiguration(configuration) : false;
563 }
564
565 void CCECProcessor::RescanActiveDevices(void)
566 {
567 for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
568 it->second->GetStatus(true);
569 }
570
571 bool 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
583 bool 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
591 CCECTV *CCECProcessor::GetTV(void) const
592 {
593 return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV));
594 }
595
596 CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const
597 {
598 return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM));
599 }
600
601 CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const
602 {
603 return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address));
604 }
605
606 CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const
607 {
608 return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address));
609 }
610
611 CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const
612 {
613 return CCECBusDevice::AsTuner(m_busDevices->At(address));
614 }
615
616 bool CCECProcessor::RegisterClient(CCECClient *client)
617 {
618 if (!client)
619 return false;
620
621 libcec_configuration &configuration = *client->GetConfiguration();
622
623 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3 && configuration.bMonitorOnly == 1)
624 return true;
625
626 if (!CECInitialised())
627 {
628 m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
629 return false;
630 }
631
632 // unregister the client first if it's already been marked as registered
633 if (client->IsRegistered())
634 UnregisterClient(client);
635
636 // get the configuration from the client
637 m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
638
639 // mark as uninitialised and unregistered
640 client->SetRegistered(false);
641 client->SetInitialised(false);
642
643 // get the current ackmask, so we can restore it if polling fails
644 uint16_t iPreviousMask(m_communication->GetAckMask());
645
646 // find logical addresses for this client
647 if (!client->AllocateLogicalAddresses())
648 {
649 m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types");
650 SetAckMask(iPreviousMask);
651 return false;
652 }
653
654 // register this client on the new addresses
655 CECDEVICEVEC devices;
656 m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses);
657 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
658 {
659 // replace a previous client
660 CLockObject lock(m_mutex);
661 m_clients.erase((*it)->GetLogicalAddress());
662 m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
663 }
664
665 // get the settings from the rom
666 if (configuration.bGetSettingsFromROM == 1)
667 {
668 libcec_configuration config;
669 m_communication->GetConfiguration(config);
670
671 CLockObject lock(m_mutex);
672 if (!config.deviceTypes.IsEmpty())
673 configuration.deviceTypes = config.deviceTypes;
674 if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress))
675 configuration.iPhysicalAddress = config.iPhysicalAddress;
676 snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName);
677 }
678
679 // set the firmware version and build date
680 configuration.serverVersion = LIBCEC_VERSION_CURRENT;
681 configuration.iFirmwareVersion = m_communication->GetFirmwareVersion();
682 configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
683
684 // mark the client as registered
685 client->SetRegistered(true);
686
687 // set the new ack mask
688 bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) &&
689 // and initialise the client
690 client->OnRegister();
691
692 // log the new registration
693 CStdString strLog;
694 strLog.Format("%s: %s", bReturn ? "CEC client registered" : "failed to register the CEC client", client->GetConnectionInfo().c_str());
695 m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog);
696
697 // display a warning if the firmware can be upgraded
698 if (bReturn && !IsRunningLatestFirmware())
699 {
700 const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
701 m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage);
702 libcec_parameter param;
703 param.paramData = (void*)strUpgradeMessage; param.paramType = CEC_PARAMETER_TYPE_STRING;
704 client->Alert(CEC_ALERT_SERVICE_DEVICE, param);
705 }
706
707 return bReturn;
708 }
709
710 bool CCECProcessor::UnregisterClient(CCECClient *client)
711 {
712 if (!client)
713 return false;
714
715 if (client->IsRegistered())
716 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
717
718 // notify the client that it will be unregistered
719 client->OnUnregister();
720
721 {
722 CLockObject lock(m_mutex);
723 // find all devices that match the LA's of this client
724 CECDEVICEVEC devices;
725 m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses);
726 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
727 {
728 // find the client
729 map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress());
730 // unregister the client
731 if (entry != m_clients.end())
732 m_clients.erase(entry);
733
734 // reset the device status
735 (*it)->ResetDeviceStatus();
736 }
737 }
738
739 // set the new ackmask
740 return SetAckMask(GetLogicalAddresses().AckMask());
741 }
742
743 void CCECProcessor::UnregisterClients(void)
744 {
745 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering all CEC clients");
746
747 vector<CCECClient *> clients = m_libcec->GetClients();
748 for (vector<CCECClient *>::iterator client = clients.begin(); client != clients.end(); client++)
749 UnregisterClient(*client);
750
751 CLockObject lock(m_mutex);
752 m_clients.clear();
753 }
754
755 CCECClient *CCECProcessor::GetClient(const cec_logical_address address)
756 {
757 CLockObject lock(m_mutex);
758 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address);
759 if (client != m_clients.end())
760 return client->second;
761 return NULL;
762 }
763
764 CCECClient *CCECProcessor::GetPrimaryClient(void)
765 {
766 CLockObject lock(m_mutex);
767 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin();
768 if (client != m_clients.end())
769 return client->second;
770 return NULL;
771 }
772
773 CCECBusDevice *CCECProcessor::GetPrimaryDevice(void)
774 {
775 return m_busDevices->At(GetLogicalAddress());
776 }
777
778 cec_logical_address CCECProcessor::GetLogicalAddress(void)
779 {
780 cec_logical_addresses addresses = GetLogicalAddresses();
781 return addresses.primary;
782 }
783
784 cec_logical_addresses CCECProcessor::GetLogicalAddresses(void)
785 {
786 CLockObject lock(m_mutex);
787 cec_logical_addresses addresses;
788 addresses.Clear();
789 for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++)
790 addresses.Set(client->first);
791
792 return addresses;
793 }
794
795 bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const
796 {
797 CCECBusDevice *device = GetDevice(address);
798 return device && device->IsHandledByLibCEC();
799 }
800
801 bool CCECProcessor::IsRunningLatestFirmware(void)
802 {
803 return m_communication && m_communication->IsOpen() ?
804 m_communication->IsRunningLatestFirmware() :
805 true;
806 }