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