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