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