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