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