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