cec: fixed - old client versions didn't always provide a valid physical address in...
[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
aa4c0d34 54#define ACTIVE_SOURCE_CHECK_INTERVAL 500
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
0b8c7eab
LOK
151 // mark as initialised
152 SetCECInitialised(true);
153
578c3905
LOK
154 return bReturn;
155}
156
004b8382 157bool CCECProcessor::CECInitialised(void)
0cfdeb5a 158{
0b714871 159 CLockObject lock(m_threadMutex);
0cfdeb5a
LOK
160 return m_bInitialised;
161}
162
004b8382 163void CCECProcessor::SetCECInitialised(bool bSetTo /* = true */)
578c3905 164{
c0152c09
LOK
165 {
166 CLockObject lock(m_mutex);
167 m_bInitialised = bSetTo;
168 }
004b8382
LOK
169 if (!bSetTo)
170 UnregisterClients();
abbca718
LOK
171}
172
b58d9277 173bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
f8513317 174{
c0152c09 175 // find the device
004b8382
LOK
176 CCECBusDevice *device = m_busDevices->At(address);
177 if (device)
c39611ec 178 {
c0152c09 179 // check if it's already marked as present or used
004b8382
LOK
180 if (device->IsPresent() || device->IsHandledByLibCEC())
181 return false;
c39611ec 182
c0152c09 183 // poll the LA if not
004b8382
LOK
184 SetAckMask(0);
185 return device->TryLogicalAddress();
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
a4b9f561 210 cec_command command;
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)
311 return primary->TransmitPoll(iAddress);
312
313 // try to find the destination
314 CCECBusDevice *device = m_busDevices->At(iAddress);
315 // and poll the destination, with the same LA as source
004b8382 316 if (device)
c0152c09
LOK
317 return device->TransmitPoll(iAddress);
318
cc60ab1c 319 return false;
44c74256
LOK
320}
321
004b8382 322CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */)
eab72c40 323{
004b8382
LOK
324 return m_busDevices ?
325 m_busDevices->GetDeviceByPhysicalAddress(iPhysicalAddress, bSuppressUpdate) :
326 NULL;
eab72c40
LOK
327}
328
004b8382 329CCECBusDevice *CCECProcessor::GetDevice(cec_logical_address address) const
e55f3f70 330{
004b8382
LOK
331 return m_busDevices ?
332 m_busDevices->At(address) :
333 NULL;
e55f3f70
LOK
334}
335
5734016c 336cec_logical_address CCECProcessor::GetActiveSource(bool bRequestActiveSource /* = true */)
b4b1b49b 337{
004b8382
LOK
338 // get the device that is marked as active source from the device map
339 CCECBusDevice *activeSource = m_busDevices->GetActiveSource();
340 if (activeSource)
341 return activeSource->GetLogicalAddress();
b4b1b49b 342
004b8382 343 if (bRequestActiveSource)
5734016c 344 {
004b8382
LOK
345 // request the active source from the bus
346 CCECBusDevice *primary = GetPrimaryDevice();
5734016c 347 if (primary)
004b8382 348 {
5734016c 349 primary->RequestActiveSource();
004b8382
LOK
350 return GetActiveSource(false);
351 }
5734016c
LOK
352 }
353
004b8382 354 // unknown or none
b4b1b49b
LOK
355 return CECDEVICE_UNKNOWN;
356}
357
358bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
359{
004b8382
LOK
360 CCECBusDevice *device = m_busDevices->At(iAddress);
361 return device && device->IsActiveSource();
b4b1b49b
LOK
362}
363
8d84e2c0 364bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 365{
c0152c09
LOK
366 uint8_t iMaxTries(0);
367 bool bRetry(true);
368 uint8_t iTries(0);
369
370 // get the current timeout setting
371 uint8_t iLineTimeout(GetStandardLineTimeout());
372
373 // reset the state of this message to 'unknown'
374 cec_adapter_message_state adapterState = ADAPTER_MESSAGE_STATE_UNKNOWN;
375
376 LogOutput(data);
377
378 // find the initiator device
004b8382
LOK
379 CCECBusDevice *initiator = m_busDevices->At(data.initiator);
380 if (!initiator)
c4287bcd 381 {
004b8382 382 m_libcec->AddLog(CEC_LOG_WARNING, "invalid initiator");
c4287bcd
LOK
383 return false;
384 }
385
c0152c09 386 // find the destination device, if it's not the broadcast address
004b8382
LOK
387 if (data.destination != CECDEVICE_BROADCAST)
388 {
c0152c09 389 // check if the device is marked as handled by libCEC
004b8382
LOK
390 CCECBusDevice *destination = m_busDevices->At(data.destination);
391 if (destination && destination->IsHandledByLibCEC())
392 {
c0152c09 393 // and reject the command if it's trying to send data to a device that is handled by libCEC
004b8382
LOK
394 m_libcec->AddLog(CEC_LOG_WARNING, "not sending data to myself!");
395 return false;
396 }
397 }
398
eb35e4ca 399 {
7bb4ed43 400 CLockObject lock(m_mutex);
5f316715 401 m_iLastTransmission = GetTimeMs();
c0152c09 402 // set the number of tries
004b8382 403 iMaxTries = initiator->GetHandler()->GetTransmitRetries() + 1;
91ef4e2d 404 initiator->MarkHandlerReady();
2abe74eb
LOK
405 }
406
c0152c09 407 // and try to send the command
33dd87a9
MK
408 while (bRetry && ++iTries < iMaxTries)
409 {
004b8382 410 if (initiator->IsUnsupportedFeature(data.opcode))
33dd87a9
MK
411 return false;
412
c0152c09
LOK
413 adapterState = !IsStopped() && m_communication && m_communication->IsOpen() ?
414 m_communication->Write(data, bRetry, iLineTimeout) :
415 ADAPTER_MESSAGE_STATE_ERROR;
33dd87a9
MK
416 iLineTimeout = m_iRetryLineTimeout;
417 }
418
419 return adapterState == ADAPTER_MESSAGE_STATE_SENT_ACKED;
825ddb96 420}
abbca718 421
004b8382 422void CCECProcessor::TransmitAbort(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 423{
004b8382 424 m_libcec->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
9dee1670 425
06a1f7ce 426 cec_command command;
004b8382 427 cec_command::Format(command, source, destination, CEC_OPCODE_FEATURE_ABORT);
ab1469a0
LOK
428 command.parameters.PushBack((uint8_t)opcode);
429 command.parameters.PushBack((uint8_t)reason);
9dee1670
LOK
430
431 Transmit(command);
abbca718
LOK
432}
433
c0152c09 434void CCECProcessor::ProcessCommand(const cec_command &command)
abbca718 435{
c0152c09 436 // log the command
e9de9629 437 CStdString dataStr;
d297cbd4
LOK
438 dataStr.Format(">> %1x%1x", command.initiator, command.destination);
439 if (command.opcode_set == 1)
440 dataStr.AppendFormat(":%02x", command.opcode);
e9de9629
LOK
441 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
442 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
004b8382 443 m_libcec->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 444
4a01fcd6
LOK
445 // find the initiator
446 CCECBusDevice *device = m_busDevices->At(command.initiator);
447
448 if (device)
449 device->HandleCommand(command);
6d858ba4
LOK
450}
451
37b0c572 452bool CCECProcessor::IsPresentDevice(cec_logical_address address)
6d858ba4 453{
004b8382
LOK
454 CCECBusDevice *device = m_busDevices->At(address);
455 return device && device->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
6d858ba4
LOK
456}
457
37b0c572 458bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
6d858ba4 459{
004b8382
LOK
460 CECDEVICEVEC devices;
461 m_busDevices->GetByType(type, devices);
462 CCECDeviceMap::FilterActive(devices);
463 return !devices.empty();
6d858ba4
LOK
464}
465
004b8382 466uint16_t CCECProcessor::GetDetectedPhysicalAddress(void) const
0f23c85c 467{
004b8382 468 return m_communication ? m_communication->GetPhysicalAddress() : CEC_INVALID_PHYSICAL_ADDRESS;
0f23c85c
LOK
469}
470
06bfd4d7
LOK
471bool CCECProcessor::SetAckMask(uint16_t iMask)
472{
0b714871 473 return m_communication ? m_communication->SetAckMask(iMask) : false;
06bfd4d7 474}
a33794d8 475
004b8382 476bool CCECProcessor::StandbyDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
a33794d8 477{
004b8382
LOK
478 bool bReturn(true);
479 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
480 bReturn &= (*it)->Standby(initiator);
481 return bReturn;
a33794d8
LOK
482}
483
004b8382 484bool CCECProcessor::StandbyDevice(const cec_logical_address initiator, cec_logical_address address)
a33794d8 485{
004b8382
LOK
486 CCECBusDevice *device = m_busDevices->At(address);
487 return device ? device->Standby(initiator) : false;
a33794d8 488}
7c63a480 489
004b8382 490bool CCECProcessor::PowerOnDevices(const cec_logical_address initiator, const CECDEVICEVEC &devices)
8670c970 491{
004b8382
LOK
492 bool bReturn(true);
493 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
494 bReturn &= (*it)->PowerOn(initiator);
495 return bReturn;
8670c970
LOK
496}
497
004b8382 498bool CCECProcessor::PowerOnDevice(const cec_logical_address initiator, cec_logical_address address)
ca27e6cf 499{
004b8382
LOK
500 CCECBusDevice *device = m_busDevices->At(address);
501 return device ? device->PowerOn(initiator) : false;
ca27e6cf
LOK
502}
503
004b8382 504bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
ca27e6cf 505{
004b8382 506 bool bReturn(false);
c0152c09 507 // open a connection if no connection has been opened
004b8382 508 if (!m_communication && strPort)
ca27e6cf 509 {
004b8382
LOK
510 IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
511 CTimeout timeout(CEC_DEFAULT_CONNECT_TIMEOUT);
512 int iConnectTry(0);
513 while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
514 {
515 m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
516 comm->Close();
517 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
518 }
519 if (comm->IsOpen())
ca27e6cf 520 {
004b8382 521 bReturn = comm->StartBootloader();
c9d15485 522 DELETE_AND_NULL(comm);
ca27e6cf
LOK
523 }
524 return bReturn;
525 }
004b8382 526 else
3e61b350 527 {
004b8382
LOK
528 m_communication->StartBootloader();
529 Close();
530 bReturn = true;
3e61b350 531 }
3e61b350 532
004b8382 533 return bReturn;
03ae897d
LOK
534}
535
004b8382 536bool CCECProcessor::PingAdapter(void)
03ae897d 537{
004b8382 538 return m_communication->PingAdapter();
03ae897d
LOK
539}
540
004b8382 541void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
03ae897d 542{
004b8382
LOK
543 CCECBusDevice *device = m_busDevices->At(destination);
544 if (device)
545 device->HandlePollFrom(initiator);
03ae897d
LOK
546}
547
004b8382 548bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
03ae897d 549{
004b8382
LOK
550 CCECBusDevice *device = m_busDevices->At(initiator);
551 return !device || !device->HandleReceiveFailed();
03ae897d
LOK
552}
553
004b8382 554bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
03ae897d 555{
004b8382 556 // stream path changes are sent by the TV
91ef4e2d
LOK
557 bool bReturn = GetTV()->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
558 GetTV()->MarkHandlerReady();
559 return bReturn;
03ae897d
LOK
560}
561
004b8382 562bool CCECProcessor::CanPersistConfiguration(void)
03ae897d 563{
004b8382 564 return m_communication ? m_communication->GetFirmwareVersion() >= 2 : false;
03ae897d
LOK
565}
566
c0152c09 567bool CCECProcessor::PersistConfiguration(const libcec_configuration &configuration)
03ae897d 568{
004b8382 569 return m_communication ? m_communication->PersistConfiguration(configuration) : false;
03ae897d
LOK
570}
571
004b8382 572void CCECProcessor::RescanActiveDevices(void)
03ae897d 573{
004b8382
LOK
574 for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
575 it->second->GetStatus(true);
03ae897d
LOK
576}
577
004b8382 578bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = CEC_DEFAULT_CONNECT_TIMEOUT */)
03ae897d 579{
004b8382
LOK
580 if (!OpenConnection(strPort, CEC_SERIAL_DEFAULT_BAUDRATE, iTimeoutMs, false))
581 return false;
03ae897d 582
004b8382
LOK
583 config->iFirmwareVersion = m_communication->GetFirmwareVersion();
584 config->iPhysicalAddress = m_communication->GetPhysicalAddress();
585 config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
03ae897d 586
004b8382 587 return true;
caca2d81
LOK
588}
589
004b8382 590bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
3efda01a 591{
004b8382
LOK
592 bool bReturn(true);
593 for (CECDEVICEMAP::iterator it = m_busDevices->Begin(); it != m_busDevices->End(); it++)
594 bReturn &= it->second->TransmitPendingActiveSourceCommands();
595 return bReturn;
3efda01a
LOK
596}
597
004b8382 598CCECTV *CCECProcessor::GetTV(void) const
1113cb7d 599{
004b8382 600 return CCECBusDevice::AsTV(m_busDevices->At(CECDEVICE_TV));
1113cb7d
LOK
601}
602
004b8382 603CCECAudioSystem *CCECProcessor::GetAudioSystem(void) const
1113cb7d 604{
004b8382 605 return CCECBusDevice::AsAudioSystem(m_busDevices->At(CECDEVICE_AUDIOSYSTEM));
1113cb7d 606}
6729ac71 607
004b8382 608CCECPlaybackDevice *CCECProcessor::GetPlaybackDevice(cec_logical_address address) const
6729ac71 609{
004b8382 610 return CCECBusDevice::AsPlaybackDevice(m_busDevices->At(address));
6729ac71
LOK
611}
612
004b8382 613CCECRecordingDevice *CCECProcessor::GetRecordingDevice(cec_logical_address address) const
6729ac71 614{
004b8382 615 return CCECBusDevice::AsRecordingDevice(m_busDevices->At(address));
6729ac71 616}
f42d3e0f 617
004b8382 618CCECTuner *CCECProcessor::GetTuner(cec_logical_address address) const
f42d3e0f 619{
004b8382 620 return CCECBusDevice::AsTuner(m_busDevices->At(address));
f42d3e0f 621}
d40928b5 622
004b8382 623bool CCECProcessor::RegisterClient(CCECClient *client)
30b4aac0 624{
5f2f3609
LOK
625 if (!client)
626 return false;
627
628 libcec_configuration &configuration = *client->GetConfiguration();
629
630 if (configuration.clientVersion >= CEC_CLIENT_VERSION_1_6_3 && configuration.bMonitorOnly == 1)
631 return true;
632
633 if (!CECInitialised())
0b8c7eab
LOK
634 {
635 m_libcec->AddLog(CEC_LOG_ERROR, "failed to register a new CEC client: CEC processor is not initialised");
004b8382 636 return false;
0b8c7eab 637 }
30b4aac0 638
42d28d15
LOK
639 // ensure that we know the vendor id of the TV
640 GetTV()->GetVendorId(CECDEVICE_UNREGISTERED);
641
c0152c09
LOK
642 // unregister the client first if it's already been marked as registered
643 if (client->IsRegistered())
644 UnregisterClient(client);
645
646 // get the configuration from the client
004b8382 647 m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
30b4aac0 648
c0152c09 649 // mark as uninitialised and unregistered
004b8382
LOK
650 client->SetRegistered(false);
651 client->SetInitialised(false);
30b4aac0 652
c0152c09 653 // get the current ackmask, so we can restore it if polling fails
004b8382 654 uint16_t iPreviousMask(m_communication->GetAckMask());
30a09f32 655
004b8382 656 // find logical addresses for this client
c0152c09 657 if (!client->AllocateLogicalAddresses())
8670c970 658 {
c0152c09 659 m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types");
004b8382
LOK
660 SetAckMask(iPreviousMask);
661 return false;
8670c970
LOK
662 }
663
004b8382
LOK
664 // register this client on the new addresses
665 CECDEVICEVEC devices;
666 m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses);
667 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
8670c970 668 {
b1d821c6
LOK
669 // set the physical address of the device at this LA
670 if (CLibCEC::IsValidPhysicalAddress(configuration.iPhysicalAddress))
671 (*it)->SetPhysicalAddress(configuration.iPhysicalAddress);
672
c0152c09 673 // replace a previous client
004b8382
LOK
674 CLockObject lock(m_mutex);
675 m_clients.erase((*it)->GetLogicalAddress());
676 m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
8670c970 677 }
30b4aac0 678
004b8382
LOK
679 // get the settings from the rom
680 if (configuration.bGetSettingsFromROM == 1)
8670c970 681 {
004b8382 682 libcec_configuration config;
c0152c09 683 m_communication->GetConfiguration(config);
f0154449 684
004b8382
LOK
685 CLockObject lock(m_mutex);
686 if (!config.deviceTypes.IsEmpty())
687 configuration.deviceTypes = config.deviceTypes;
688 if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress))
689 configuration.iPhysicalAddress = config.iPhysicalAddress;
690 snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName);
30b4aac0
LOK
691 }
692
004b8382 693 // set the firmware version and build date
99aeafb9
LOK
694 configuration.serverVersion = LIBCEC_VERSION_CURRENT;
695 configuration.iFirmwareVersion = m_communication->GetFirmwareVersion();
004b8382
LOK
696 configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
697
c0152c09
LOK
698 // mark the client as registered
699 client->SetRegistered(true);
30b4aac0 700
c0152c09
LOK
701 // set the new ack mask
702 bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) &&
703 // and initialise the client
704 client->OnRegister();
41e3372a 705
c0152c09
LOK
706 // log the new registration
707 CStdString strLog;
708 strLog.Format("%s: %s", bReturn ? "CEC client registered" : "failed to register the CEC client", client->GetConnectionInfo().c_str());
709 m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog);
b98fc43d 710
004b8382 711 // display a warning if the firmware can be upgraded
c0152c09 712 if (bReturn && !IsRunningLatestFirmware())
3ef17606 713 {
004b8382
LOK
714 const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
715 m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage);
c30acafa
LOK
716 libcec_parameter param;
717 param.paramData = (void*)strUpgradeMessage; param.paramType = CEC_PARAMETER_TYPE_STRING;
718 client->Alert(CEC_ALERT_SERVICE_DEVICE, param);
3ef17606 719 }
30b4aac0 720
42d28d15
LOK
721 // ensure that the command handler for the TV is initialised
722 if (bReturn)
723 {
724 CCECCommandHandler *handler = GetTV()->GetHandler();
725 if (handler)
726 handler->InitHandler();
91ef4e2d 727 GetTV()->MarkHandlerReady();
42d28d15
LOK
728 }
729
43b2dfdd 730 return bReturn;
30b4aac0
LOK
731}
732
4a01fcd6 733bool CCECProcessor::UnregisterClient(CCECClient *client)
d40928b5 734{
c0152c09 735 if (!client)
4a01fcd6 736 return false;
c0152c09 737
4691dc3b
LOK
738 if (client->IsRegistered())
739 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
c0152c09
LOK
740
741 // notify the client that it will be unregistered
742 client->OnUnregister();
743
7f274e72 744 {
c0152c09
LOK
745 CLockObject lock(m_mutex);
746 // find all devices that match the LA's of this client
747 CECDEVICEVEC devices;
748 m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses);
749 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
750 {
751 // find the client
752 map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress());
753 // unregister the client
754 if (entry != m_clients.end())
755 m_clients.erase(entry);
756
757 // reset the device status
758 (*it)->ResetDeviceStatus();
759 }
7f274e72 760 }
c0152c09
LOK
761
762 // set the new ackmask
4a01fcd6 763 return SetAckMask(GetLogicalAddresses().AckMask());
d40928b5 764}
224ea877 765
004b8382 766void CCECProcessor::UnregisterClients(void)
224ea877 767{
c0152c09
LOK
768 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering all CEC clients");
769
770 vector<CCECClient *> clients = m_libcec->GetClients();
771 for (vector<CCECClient *>::iterator client = clients.begin(); client != clients.end(); client++)
772 UnregisterClient(*client);
773
004b8382 774 CLockObject lock(m_mutex);
004b8382 775 m_clients.clear();
224ea877
LOK
776}
777
004b8382 778CCECClient *CCECProcessor::GetClient(const cec_logical_address address)
224ea877 779{
004b8382
LOK
780 CLockObject lock(m_mutex);
781 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address);
782 if (client != m_clients.end())
783 return client->second;
784 return NULL;
224ea877 785}
3efda01a 786
004b8382 787CCECClient *CCECProcessor::GetPrimaryClient(void)
3efda01a 788{
004b8382
LOK
789 CLockObject lock(m_mutex);
790 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin();
791 if (client != m_clients.end())
792 return client->second;
793 return NULL;
3efda01a 794}
f80cd208 795
c0152c09 796CCECBusDevice *CCECProcessor::GetPrimaryDevice(void)
f80cd208 797{
004b8382
LOK
798 return m_busDevices->At(GetLogicalAddress());
799}
f80cd208 800
c0152c09 801cec_logical_address CCECProcessor::GetLogicalAddress(void)
004b8382
LOK
802{
803 cec_logical_addresses addresses = GetLogicalAddresses();
804 return addresses.primary;
f80cd208 805}
b78b4e33 806
c0152c09 807cec_logical_addresses CCECProcessor::GetLogicalAddresses(void)
b78b4e33 808{
c0152c09 809 CLockObject lock(m_mutex);
004b8382 810 cec_logical_addresses addresses;
c30acafa 811 addresses.Clear();
004b8382
LOK
812 for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++)
813 addresses.Set(client->first);
814
815 return addresses;
b78b4e33 816}
d2d1660c 817
004b8382 818bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const
d2d1660c 819{
004b8382
LOK
820 CCECBusDevice *device = GetDevice(address);
821 return device && device->IsHandledByLibCEC();
d2d1660c 822}
c0152c09
LOK
823
824bool CCECProcessor::IsRunningLatestFirmware(void)
825{
826 return m_communication && m_communication->IsOpen() ?
827 m_communication->IsRunningLatestFirmware() :
828 true;
829}