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