cec-config: fix physical address detection
[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
c0152c09 89 // mark as initialised
004b8382 90 SetCECInitialised(true);
7c63a480 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
004b8382 104 if (m_communication)
eca71746 105 {
eca71746
LOK
106 delete m_communication;
107 m_communication = NULL;
108 }
c0152c09 109}
99aeafb9 110
c0152c09
LOK
111void CCECProcessor::ResetMembers(void)
112{
113 // close the connection
114 if (m_communication)
115 {
116 delete m_communication;
117 m_communication = NULL;
118 }
119
120 // reset the other members to the initial state
99aeafb9
LOK
121 m_iStandardLineTimeout = 3;
122 m_iRetryLineTimeout = 3;
123 m_iLastTransmission = 0;
124 m_busDevices->ResetDeviceStatus();
eca71746
LOK
125}
126
f80cd208 127bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening /* = true */)
abbca718 128{
a674fd68 129 bool bReturn(false);
c0152c09
LOK
130 CTimeout timeout(iTimeoutMs > 0 ? iTimeoutMs : CEC_DEFAULT_TRANSMIT_WAIT);
131
132 // ensure that a previous connection is closed
80726797
LOK
133 Close();
134
c0152c09
LOK
135 // reset all member to the initial state
136 ResetMembers();
137
138 // check whether the Close() method deleted any previous connection
139 if (m_communication)
578c3905 140 {
c0152c09
LOK
141 m_libcec->AddLog(CEC_LOG_ERROR, "previous connection could not be closed");
142 return bReturn;
578c3905 143 }
1113cb7d 144
c0152c09
LOK
145 // create a new connection
146 m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
c520af4f 147
c0152c09 148 // open a new connection
efed01e1 149 unsigned iConnectTry(0);
a75e3a5a 150 while (timeout.TimeLeft() > 0 && (bReturn = m_communication->Open((timeout.TimeLeft() / CEC_CONNECT_TRIES), false, bStartListening)) == false)
3d78df91 151 {
004b8382 152 m_libcec->AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
d55f263f 153 m_communication->Close();
b32ffd87 154 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT);
3d78df91 155 }
56035c86 156
004b8382 157 m_libcec->AddLog(CEC_LOG_NOTICE, "connection opened");
12a36be9 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{
4691dc3b 618 if (!client || !IsRunning())
004b8382 619 return false;
30b4aac0 620
c0152c09
LOK
621 // unregister the client first if it's already been marked as registered
622 if (client->IsRegistered())
623 UnregisterClient(client);
624
625 // get the configuration from the client
004b8382
LOK
626 libcec_configuration &configuration = *client->GetConfiguration();
627 m_libcec->AddLog(CEC_LOG_NOTICE, "registering new CEC client - v%s", ToString((cec_client_version)configuration.clientVersion));
30b4aac0 628
c0152c09 629 // mark as uninitialised and unregistered
004b8382
LOK
630 client->SetRegistered(false);
631 client->SetInitialised(false);
30b4aac0 632
c0152c09 633 // get the current ackmask, so we can restore it if polling fails
004b8382 634 uint16_t iPreviousMask(m_communication->GetAckMask());
30a09f32 635
004b8382 636 // find logical addresses for this client
c0152c09 637 if (!client->AllocateLogicalAddresses())
8670c970 638 {
c0152c09 639 m_libcec->AddLog(CEC_LOG_ERROR, "failed to register the new CEC client - cannot allocate the requested device types");
004b8382
LOK
640 SetAckMask(iPreviousMask);
641 return false;
8670c970
LOK
642 }
643
004b8382
LOK
644 // register this client on the new addresses
645 CECDEVICEVEC devices;
646 m_busDevices->GetByLogicalAddresses(devices, configuration.logicalAddresses);
647 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
8670c970 648 {
c0152c09 649 // replace a previous client
004b8382
LOK
650 CLockObject lock(m_mutex);
651 m_clients.erase((*it)->GetLogicalAddress());
652 m_clients.insert(make_pair<cec_logical_address, CCECClient *>((*it)->GetLogicalAddress(), client));
8670c970 653 }
30b4aac0 654
004b8382
LOK
655 // get the settings from the rom
656 if (configuration.bGetSettingsFromROM == 1)
8670c970 657 {
004b8382 658 libcec_configuration config;
c0152c09 659 m_communication->GetConfiguration(config);
f0154449 660
004b8382
LOK
661 CLockObject lock(m_mutex);
662 if (!config.deviceTypes.IsEmpty())
663 configuration.deviceTypes = config.deviceTypes;
664 if (CLibCEC::IsValidPhysicalAddress(config.iPhysicalAddress))
665 configuration.iPhysicalAddress = config.iPhysicalAddress;
666 snprintf(configuration.strDeviceName, 13, "%s", config.strDeviceName);
30b4aac0
LOK
667 }
668
004b8382 669 // set the firmware version and build date
99aeafb9
LOK
670 configuration.serverVersion = LIBCEC_VERSION_CURRENT;
671 configuration.iFirmwareVersion = m_communication->GetFirmwareVersion();
004b8382
LOK
672 configuration.iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
673
c0152c09
LOK
674 // mark the client as registered
675 client->SetRegistered(true);
30b4aac0 676
c0152c09
LOK
677 // set the new ack mask
678 bool bReturn = SetAckMask(GetLogicalAddresses().AckMask()) &&
679 // and initialise the client
680 client->OnRegister();
41e3372a 681
c0152c09
LOK
682 // log the new registration
683 CStdString strLog;
684 strLog.Format("%s: %s", bReturn ? "CEC client registered" : "failed to register the CEC client", client->GetConnectionInfo().c_str());
685 m_libcec->AddLog(bReturn ? CEC_LOG_NOTICE : CEC_LOG_ERROR, strLog);
b98fc43d 686
004b8382 687 // display a warning if the firmware can be upgraded
c0152c09 688 if (bReturn && !IsRunningLatestFirmware())
3ef17606 689 {
004b8382
LOK
690 const char *strUpgradeMessage = "The firmware of this adapter can be upgraded. Please visit http://blog.pulse-eight.com/ for more information.";
691 m_libcec->AddLog(CEC_LOG_WARNING, strUpgradeMessage);
c30acafa
LOK
692 libcec_parameter param;
693 param.paramData = (void*)strUpgradeMessage; param.paramType = CEC_PARAMETER_TYPE_STRING;
694 client->Alert(CEC_ALERT_SERVICE_DEVICE, param);
3ef17606 695 }
30b4aac0 696
43b2dfdd 697 return bReturn;
30b4aac0
LOK
698}
699
4a01fcd6 700bool CCECProcessor::UnregisterClient(CCECClient *client)
d40928b5 701{
c0152c09 702 if (!client)
4a01fcd6 703 return false;
c0152c09 704
4691dc3b
LOK
705 if (client->IsRegistered())
706 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering client: %s", client->GetConnectionInfo().c_str());
c0152c09
LOK
707
708 // notify the client that it will be unregistered
709 client->OnUnregister();
710
7f274e72 711 {
c0152c09
LOK
712 CLockObject lock(m_mutex);
713 // find all devices that match the LA's of this client
714 CECDEVICEVEC devices;
715 m_busDevices->GetByLogicalAddresses(devices, client->GetConfiguration()->logicalAddresses);
716 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
717 {
718 // find the client
719 map<cec_logical_address, CCECClient *>::iterator entry = m_clients.find((*it)->GetLogicalAddress());
720 // unregister the client
721 if (entry != m_clients.end())
722 m_clients.erase(entry);
723
724 // reset the device status
725 (*it)->ResetDeviceStatus();
726 }
7f274e72 727 }
c0152c09
LOK
728
729 // set the new ackmask
4a01fcd6 730 return SetAckMask(GetLogicalAddresses().AckMask());
d40928b5 731}
224ea877 732
004b8382 733void CCECProcessor::UnregisterClients(void)
224ea877 734{
c0152c09
LOK
735 m_libcec->AddLog(CEC_LOG_NOTICE, "unregistering all CEC clients");
736
737 vector<CCECClient *> clients = m_libcec->GetClients();
738 for (vector<CCECClient *>::iterator client = clients.begin(); client != clients.end(); client++)
739 UnregisterClient(*client);
740
004b8382 741 CLockObject lock(m_mutex);
004b8382 742 m_clients.clear();
224ea877
LOK
743}
744
004b8382 745CCECClient *CCECProcessor::GetClient(const cec_logical_address address)
224ea877 746{
004b8382
LOK
747 CLockObject lock(m_mutex);
748 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.find(address);
749 if (client != m_clients.end())
750 return client->second;
751 return NULL;
224ea877 752}
3efda01a 753
004b8382 754CCECClient *CCECProcessor::GetPrimaryClient(void)
3efda01a 755{
004b8382
LOK
756 CLockObject lock(m_mutex);
757 map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin();
758 if (client != m_clients.end())
759 return client->second;
760 return NULL;
3efda01a 761}
f80cd208 762
c0152c09 763CCECBusDevice *CCECProcessor::GetPrimaryDevice(void)
f80cd208 764{
004b8382
LOK
765 return m_busDevices->At(GetLogicalAddress());
766}
f80cd208 767
c0152c09 768cec_logical_address CCECProcessor::GetLogicalAddress(void)
004b8382
LOK
769{
770 cec_logical_addresses addresses = GetLogicalAddresses();
771 return addresses.primary;
f80cd208 772}
b78b4e33 773
c0152c09 774cec_logical_addresses CCECProcessor::GetLogicalAddresses(void)
b78b4e33 775{
c0152c09 776 CLockObject lock(m_mutex);
004b8382 777 cec_logical_addresses addresses;
c30acafa 778 addresses.Clear();
004b8382
LOK
779 for (map<cec_logical_address, CCECClient *>::const_iterator client = m_clients.begin(); client != m_clients.end(); client++)
780 addresses.Set(client->first);
781
782 return addresses;
b78b4e33 783}
d2d1660c 784
004b8382 785bool CCECProcessor::IsHandledByLibCEC(const cec_logical_address address) const
d2d1660c 786{
004b8382
LOK
787 CCECBusDevice *device = GetDevice(address);
788 return device && device->IsHandledByLibCEC();
d2d1660c 789}
c0152c09
LOK
790
791bool CCECProcessor::IsRunningLatestFirmware(void)
792{
793 return m_communication && m_communication->IsOpen() ?
794 m_communication->IsRunningLatestFirmware() :
795 true;
796}