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