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