cec: added a callback that is called when libCEC's configuration changed.
[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"
ba65909d 44#include "platform/util/timeutils.h"
abbca718
LOK
45
46using namespace CEC;
47using namespace std;
f00ff009 48using namespace PLATFORM;
abbca718 49
caca2d81
LOK
50CCECProcessor::CCECProcessor(CLibCEC *controller, const libcec_configuration *configuration) :
51 m_bInitialised(false),
caca2d81
LOK
52 m_communication(NULL),
53 m_controller(controller),
54 m_bMonitor(false),
55 m_iStandardLineTimeout(3),
56 m_iRetryLineTimeout(3),
30b4aac0 57 m_iLastTransmission(0)
caca2d81
LOK
58{
59 m_logicalAddresses.Clear();
60 CreateBusDevices();
30b4aac0
LOK
61 m_configuration.Clear();
62
63 SetConfiguration(configuration);
64
65 if (m_configuration.tvVendor != CEC_VENDOR_UNKNOWN)
ed63a515 66 m_busDevices[CECDEVICE_TV]->ReplaceHandler(false);
caca2d81
LOK
67}
68
69CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress, cec_client_version clientVersion) :
b64db02e 70 m_bInitialised(false),
99666519 71 m_communication(NULL),
f8513317 72 m_controller(controller),
dcd240b2
LOK
73 m_bMonitor(false),
74 m_iStandardLineTimeout(3),
5f316715 75 m_iRetryLineTimeout(3),
30b4aac0 76 m_iLastTransmission(0)
f8513317 77{
30b4aac0
LOK
78 m_configuration.Clear();
79
80 // client version < 1.5.0
81 m_configuration.clientVersion = clientVersion;
82 snprintf(m_configuration.strDeviceName, 13, "%s", strDeviceName);
83 m_configuration.deviceTypes = types;
84 m_configuration.iPhysicalAddress = iPhysicalAddress;
85 m_configuration.baseDevice = (cec_logical_address)CEC_DEFAULT_BASE_DEVICE;
86 m_configuration.iHDMIPort = CEC_DEFAULT_HDMI_PORT;
87
88 if (m_configuration.deviceTypes.IsEmpty())
89 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
ab1469a0 90 m_logicalAddresses.Clear();
caca2d81
LOK
91 CreateBusDevices();
92}
93
94void CCECProcessor::CreateBusDevices(void)
95{
f8513317 96 for (int iPtr = 0; iPtr < 16; iPtr++)
51b2a094
LOK
97 {
98 switch(iPtr)
99 {
100 case CECDEVICE_AUDIOSYSTEM:
5d5a2dc2 101 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
102 break;
103 case CECDEVICE_PLAYBACKDEVICE1:
104 case CECDEVICE_PLAYBACKDEVICE2:
105 case CECDEVICE_PLAYBACKDEVICE3:
5d5a2dc2 106 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
107 break;
108 case CECDEVICE_RECORDINGDEVICE1:
109 case CECDEVICE_RECORDINGDEVICE2:
110 case CECDEVICE_RECORDINGDEVICE3:
5d5a2dc2 111 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
112 break;
113 case CECDEVICE_TUNER1:
114 case CECDEVICE_TUNER2:
115 case CECDEVICE_TUNER3:
116 case CECDEVICE_TUNER4:
5d5a2dc2 117 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
118 break;
119 case CECDEVICE_TV:
120 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
121 break;
122 default:
5d5a2dc2 123 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
124 break;
125 }
126 }
f8513317
LOK
127}
128
2abe74eb 129CCECProcessor::~CCECProcessor(void)
abbca718 130{
eca71746 131 Close();
7c63a480 132
d1998c7b
LOK
133 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
134 delete m_busDevices[iPtr];
abbca718
LOK
135}
136
eca71746
LOK
137void CCECProcessor::Close(void)
138{
139 StopThread();
140
141 CLockObject lock(m_mutex);
142 if (m_communication)
143 {
144 m_communication->Close();
145 delete m_communication;
146 m_communication = NULL;
147 }
148}
149
578c3905 150bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs)
abbca718 151{
a674fd68 152 bool bReturn(false);
578c3905 153 CLockObject lock(m_mutex);
99666519 154 if (m_communication)
578c3905 155 {
9f9c8c82
LOK
156 CLibCEC::AddLog(CEC_LOG_WARNING, "existing connection handler found, deleting it");
157 m_communication->Close();
158 delete m_communication;
578c3905 159 }
1113cb7d 160
7bb4ed43 161 m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
99666519 162
578c3905
LOK
163 /* check for an already opened connection */
164 if (m_communication->IsOpen())
1113cb7d 165 {
5477a250 166 CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened");
578c3905
LOK
167 return bReturn;
168 }
abbca718 169
c520af4f
LOK
170 uint64_t iNow = GetTimeMs();
171 uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
172
578c3905 173 /* open a new connection */
efed01e1
LOK
174 unsigned iConnectTry(0);
175 while (iNow < iTarget && (bReturn = m_communication->Open(this, iTimeoutMs)) == false)
3d78df91
LOK
176 {
177 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
178 Sleep(500);
179 iNow = GetTimeMs();
180 }
56035c86 181
efed01e1 182 if (bReturn)
30b4aac0 183 CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication->GetFirmwareVersion(), ToString(m_configuration.clientVersion));
3d78df91 184
578c3905
LOK
185 return bReturn;
186}
187
0cfdeb5a
LOK
188bool CCECProcessor::IsInitialised(void)
189{
190 CLockObject lock(m_mutex);
191 return m_bInitialised;
192}
193
2db8981f 194void CCECProcessor::SetInitialised(bool bSetTo /* = true */)
578c3905 195{
578c3905 196 CLockObject lock(m_mutex);
2db8981f
LOK
197 m_bInitialised = bSetTo;
198}
578c3905 199
2db8981f
LOK
200bool CCECProcessor::Initialise(void)
201{
202 bool bReturn(false);
578c3905 203 {
2db8981f
LOK
204 CLockObject lock(m_mutex);
205 if (!m_logicalAddresses.IsEmpty())
206 m_logicalAddresses.Clear();
578c3905 207
2db8981f
LOK
208 if (!FindLogicalAddresses())
209 {
5477a250 210 CLibCEC::AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
2db8981f
LOK
211 return bReturn;
212 }
213
214 /* only set our OSD name for the primary device */
30b4aac0 215 m_busDevices[m_logicalAddresses.primary]->m_strDeviceName = m_configuration.strDeviceName;
2db8981f 216 }
578c3905
LOK
217
218 /* get the vendor id from the TV, so we are using the correct handler */
30b4aac0 219 m_busDevices[CECDEVICE_TV]->GetVendorId();
578c3905 220
30b4aac0 221 if (m_configuration.iPhysicalAddress != 0)
4f362964 222 {
30b4aac0
LOK
223 CLibCEC::AddLog(CEC_LOG_NOTICE, "setting the physical address to %4x", m_configuration.iPhysicalAddress);
224 m_busDevices[m_logicalAddresses.primary]->m_iPhysicalAddress = m_configuration.iPhysicalAddress;
4f362964 225 if ((bReturn = m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress()) == false)
30b4aac0 226 CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set the physical address to %4x", m_configuration.iPhysicalAddress);
4f362964 227 }
30b4aac0
LOK
228 else if (m_configuration.iPhysicalAddress == 0 && (bReturn = SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort, true)) == false)
229 CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set HDMI port %d on %s (%x)", m_configuration.iHDMIPort, ToString(m_configuration.baseDevice), (uint8_t)m_configuration.baseDevice);
2db8981f 230
30b4aac0 231 WakeDevices();
d068b3d4 232
2db8981f 233 SetInitialised(bReturn);
32403cc3 234 CLibCEC::ConfigurationChanged(m_configuration);
578c3905
LOK
235
236 return bReturn;
237}
238
30b4aac0
LOK
239void CCECProcessor::WakeDevices(void)
240{
241 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
242 {
243 if (m_configuration.wakeDevices[iPtr])
244 m_busDevices[iPtr]->PowerOn();
245 }
246}
247
578c3905
LOK
248bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
249{
250 bool bReturn(false);
251
252 {
253 CLockObject lock(m_mutex);
254 if (!OpenConnection(strPort, iBaudRate, iTimeoutMs))
a674fd68 255 return bReturn;
994dbaaa 256
a674fd68 257 /* create the processor thread */
8e57f83c 258 if (!CreateThread())
a674fd68 259 {
5477a250 260 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
a674fd68 261 return bReturn;
994dbaaa 262 }
a674fd68
LOK
263 }
264
578c3905 265 if ((bReturn = Initialise()) == false)
a674fd68 266 {
5477a250 267 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
578c3905 268 StopThread(true);
a674fd68
LOK
269 }
270 else
271 {
5477a250 272 CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
a674fd68
LOK
273 }
274
275 return bReturn;
abbca718
LOK
276}
277
b58d9277 278bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
f8513317 279{
93fff5c1 280 if (m_busDevices[address]->TryLogicalAddress())
f8513317 281 {
ab1469a0 282 m_logicalAddresses.Set(address);
f8513317
LOK
283 return true;
284 }
285
f8513317
LOK
286 return false;
287}
288
b58d9277 289bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
f8513317 290{
5477a250 291 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
b58d9277
LOK
292 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
293 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
294 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
f8513317
LOK
295}
296
b58d9277 297bool CCECProcessor::FindLogicalAddressTuner(void)
f8513317 298{
5477a250 299 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
b58d9277
LOK
300 return TryLogicalAddress(CECDEVICE_TUNER1) ||
301 TryLogicalAddress(CECDEVICE_TUNER2) ||
302 TryLogicalAddress(CECDEVICE_TUNER3) ||
303 TryLogicalAddress(CECDEVICE_TUNER4);
f8513317
LOK
304}
305
b58d9277 306bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
f8513317 307{
5477a250 308 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
b58d9277
LOK
309 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
310 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
311 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
f8513317
LOK
312}
313
b58d9277 314bool CCECProcessor::FindLogicalAddressAudioSystem(void)
f8513317 315{
5477a250 316 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
b58d9277 317 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
f8513317
LOK
318}
319
3e61b350
LOK
320bool CCECProcessor::ChangeDeviceType(cec_device_type from, cec_device_type to)
321{
322 bool bChanged(false);
323
5477a250 324 CLibCEC::AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
3e61b350 325
f00ff009 326 CLockObject lock(m_mutex);
3e61b350
LOK
327 CCECBusDevice *previousDevice = GetDeviceByType(from);
328 m_logicalAddresses.primary = CECDEVICE_UNKNOWN;
329
330 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
331 {
30b4aac0 332 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
3e61b350
LOK
333 continue;
334
30b4aac0 335 if (m_configuration.deviceTypes.types[iPtr] == from)
3e61b350
LOK
336 {
337 bChanged = true;
30b4aac0 338 m_configuration.deviceTypes.types[iPtr] = to;
3e61b350 339 }
30b4aac0 340 else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
3e61b350 341 {
30b4aac0 342 m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
3e61b350
LOK
343 }
344 }
345
346 if (bChanged)
347 {
348 FindLogicalAddresses();
349
350 CCECBusDevice *newDevice = GetDeviceByType(to);
351 if (previousDevice && newDevice)
352 {
353 newDevice->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC);
4478bc79 354 previousDevice->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
3e61b350
LOK
355
356 newDevice->SetCecVersion(previousDevice->GetCecVersion(false));
357 previousDevice->SetCecVersion(CEC_VERSION_UNKNOWN);
358
359 newDevice->SetMenuLanguage(previousDevice->GetMenuLanguage(false));
360 cec_menu_language lang;
361 lang.device = previousDevice->GetLogicalAddress();
362 for (unsigned int iPtr = 0; iPtr < 4; iPtr++)
363 lang.language[iPtr] = '?';
364 lang.language[3] = 0;
365 previousDevice->SetMenuLanguage(lang);
366
367 newDevice->SetMenuState(previousDevice->GetMenuState());
368 previousDevice->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
369
370 newDevice->SetOSDName(previousDevice->GetOSDName(false));
371 previousDevice->SetOSDName(ToString(previousDevice->GetLogicalAddress()));
372
373 newDevice->SetPhysicalAddress(previousDevice->GetPhysicalAddress(false));
374 previousDevice->SetPhysicalAddress(0xFFFF);
375
376 newDevice->SetPowerStatus(previousDevice->GetPowerStatus(false));
377 previousDevice->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN);
378
379 newDevice->SetVendorId(previousDevice->GetVendorId(false));
380 previousDevice->SetVendorId(CEC_VENDOR_UNKNOWN);
381
382 if ((from == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || from == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
383 (to == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || to == CEC_DEVICE_TYPE_RECORDING_DEVICE))
384 {
385 ((CCECPlaybackDevice *) newDevice)->SetDeckControlMode(((CCECPlaybackDevice *) previousDevice)->GetDeckControlMode());
386 ((CCECPlaybackDevice *) previousDevice)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP);
387
388 ((CCECPlaybackDevice *) newDevice)->SetDeckStatus(((CCECPlaybackDevice *) previousDevice)->GetDeckStatus());
389 ((CCECPlaybackDevice *) previousDevice)->SetDeckStatus(CEC_DECK_INFO_STOP);
390 }
391 }
392 }
393
394 return true;
395}
396
f8513317
LOK
397bool CCECProcessor::FindLogicalAddresses(void)
398{
399 bool bReturn(true);
ab1469a0 400 m_logicalAddresses.Clear();
f8513317 401
30b4aac0 402 if (m_configuration.deviceTypes.IsEmpty())
c39611ec
LOK
403 {
404 CLibCEC::AddLog(CEC_LOG_ERROR, "no device types set");
405 return false;
406 }
407
f8513317
LOK
408 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
409 {
30b4aac0 410 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
f8513317
LOK
411 continue;
412
30b4aac0 413 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - device %d: type %d", __FUNCTION__, iPtr, m_configuration.deviceTypes.types[iPtr]);
f8513317 414
30b4aac0 415 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
b58d9277 416 bReturn &= FindLogicalAddressRecordingDevice();
30b4aac0 417 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
b58d9277 418 bReturn &= FindLogicalAddressTuner();
30b4aac0 419 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
b58d9277 420 bReturn &= FindLogicalAddressPlaybackDevice();
30b4aac0 421 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
b58d9277 422 bReturn &= FindLogicalAddressAudioSystem();
f8513317
LOK
423 }
424
a674fd68
LOK
425 if (bReturn)
426 SetAckMask(m_logicalAddresses.AckMask());
427
f8513317
LOK
428 return bReturn;
429}
430
a3ffc068
LOK
431void CCECProcessor::ReplaceHandlers(void)
432{
0cfdeb5a
LOK
433 if (!IsInitialised())
434 return;
a3ffc068 435 for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++)
591170fd 436 m_busDevices[iPtr]->ReplaceHandler(m_bInitialised);
a3ffc068
LOK
437}
438
b1f94db1
LOK
439bool CCECProcessor::OnCommandReceived(const cec_command &command)
440{
441 m_commandBuffer.Push(command);
442 return true;
443}
444
2abe74eb 445void *CCECProcessor::Process(void)
f99bc831 446{
8e57f83c
LOK
447 cec_command command;
448 CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
abbca718 449
b1f94db1 450 while (!IsStopped() && m_communication->IsOpen())
abbca718 451 {
591170fd 452 ReplaceHandlers();
b1f94db1 453 if (m_commandBuffer.Pop(command))
9dee1670 454 ParseCommand(command);
b5f34cf9 455
13fd6a66 456 m_controller->CheckKeypressTimeout();
b1f94db1 457 Sleep(5);
abbca718
LOK
458 }
459
60fa4578 460 return NULL;
abbca718
LOK
461}
462
18203d17 463bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
abbca718 464{
8ac9c610
LOK
465 bool bReturn(false);
466
60fa4578 467 if (!IsRunning())
8ac9c610 468 return bReturn;
f99bc831 469
18203d17 470 cec_logical_address addr = m_logicalAddresses.primary;
04437dcf 471
18203d17
LOK
472 if (type != CEC_DEVICE_TYPE_RESERVED)
473 {
90ea9066 474 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
18203d17
LOK
475 {
476 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
477 {
478 addr = (cec_logical_address) iPtr;
479 break;
480 }
481 }
482 }
483
37b0c572 484 m_busDevices[addr]->SetActiveSource();
d383d6d0 485 if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF)
bbc71623 486 bReturn = m_busDevices[addr]->ActivateSource();
8d915412
LOK
487
488 return bReturn;
18203d17
LOK
489}
490
37b0c572
LOK
491bool CCECProcessor::SetActiveSource(uint16_t iStreamPath)
492{
493 bool bReturn(false);
494
495 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
496 if (device)
497 {
498 device->SetActiveSource();
499 bReturn = true;
500 }
501
502 return bReturn;
503}
504
dcd240b2
LOK
505void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
506{
f00ff009 507 CLockObject lock(m_mutex);
dcd240b2
LOK
508 m_iStandardLineTimeout = iTimeout;
509}
510
511void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
512{
f00ff009 513 CLockObject lock(m_mutex);
dcd240b2
LOK
514 m_iRetryLineTimeout = iTimeout;
515}
516
18203d17
LOK
517bool CCECProcessor::SetActiveView(void)
518{
30b4aac0 519 return SetActiveSource(m_configuration.deviceTypes.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_configuration.deviceTypes[0]);
8ac9c610
LOK
520}
521
28fa6c97 522bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
a9232a79
LOK
523{
524 bool bReturn(false);
525
526 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
527 if (device)
528 {
529 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
28fa6c97
LOK
530 if (bSendUpdate)
531 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
532 bReturn = true;
533 }
534
535 return bReturn;
536}
537
28fa6c97 538bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
a9232a79
LOK
539{
540 bool bReturn(false);
541
542 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
543 if (device)
544 {
545 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
28fa6c97
LOK
546 if (bSendUpdate)
547 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
548 bReturn = true;
549 }
550
551 return bReturn;
552}
553
d2f1c157 554bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
16b1e052
LOK
555{
556 bool bReturn(false);
f00ff009 557 CLockObject lock(m_mutex);
16b1e052 558
30b4aac0
LOK
559 m_configuration.baseDevice = iBaseDevice;
560 m_configuration.iHDMIPort = iPort;
8e57f83c 561 if (!IsRunning() && !bForce)
89b3c9df 562 return true;
16b1e052 563
5477a250 564 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
56035c86 565
16b1e052 566 uint16_t iPhysicalAddress(0);
1e7afacd 567 if (iBaseDevice > CECDEVICE_TV)
5d0c1164 568 {
f00ff009 569 lock.Unlock();
1e7afacd 570 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
5d0c1164
LOK
571 lock.Lock();
572 }
1e7afacd 573
a674fd68 574 if (iPhysicalAddress < 0xffff)
1e7afacd 575 {
1e7afacd 576 if (iPhysicalAddress == 0)
5d355340 577 iPhysicalAddress += 0x1000 * iPort;
1e7afacd 578 else if (iPhysicalAddress % 0x1000 == 0)
5d355340 579 iPhysicalAddress += 0x100 * iPort;
1e7afacd 580 else if (iPhysicalAddress % 0x100 == 0)
5d355340 581 iPhysicalAddress += 0x10 * iPort;
1e7afacd 582 else if (iPhysicalAddress % 0x10 == 0)
5d355340 583 iPhysicalAddress += iPort;
1e7afacd 584
5d355340 585 bReturn = true;
16b1e052
LOK
586 }
587
a674fd68 588 if (!bReturn)
5477a250 589 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
f070564b
LOK
590 else
591 {
f00ff009 592 lock.Unlock();
f070564b
LOK
593 SetPhysicalAddress(iPhysicalAddress);
594 }
a674fd68 595
16b1e052
LOK
596 return bReturn;
597}
598
45b97de7 599bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
16b1e052 600{
9fd73dd4 601 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
16b1e052
LOK
602 {
603 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
604 return true;
605 }
606 return false;
607}
608
ab27363d 609bool CCECProcessor::TransmitInactiveSource(void)
abbca718 610{
60fa4578 611 if (!IsRunning())
f99bc831
LOK
612 return false;
613
ab1469a0 614 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
ab27363d 615 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
cc60ab1c 616 return false;
abbca718
LOK
617}
618
9dee1670 619void CCECProcessor::LogOutput(const cec_command &data)
abbca718 620{
1d3ca3de 621 CStdString strTx;
57f45e6c
LOK
622 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
623 if (data.opcode_set)
624 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
9dee1670 625
06a1f7ce 626 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
1d3ca3de 627 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
5477a250 628 CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
9dee1670 629}
2abe74eb 630
06bfd4d7 631bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 632{
f00ff009 633 CLockObject lock(m_mutex);
f8513317 634 if (m_logicalAddresses.primary != iLogicalAddress)
06775107 635 {
5477a250 636 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
f8513317 637 m_logicalAddresses.primary = iLogicalAddress;
ab1469a0
LOK
638 m_logicalAddresses.Set(iLogicalAddress);
639 return SetAckMask(m_logicalAddresses.AckMask());
06775107 640 }
2abe74eb 641
06bfd4d7 642 return true;
abbca718 643}
825ddb96 644
28fa6c97
LOK
645bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
646{
647 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
648 {
649 if (m_logicalAddresses[iPtr])
650 m_busDevices[iPtr]->SetMenuState(state);
651 }
652
653 if (bSendUpdate)
654 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
655
656 return true;
657}
658
bebb19dc 659bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
2492216a 660{
f070564b
LOK
661 bool bSendActiveView(false);
662 bool bReturn(false);
4d6f5ac7 663 cec_logical_addresses sendUpdatesTo;
ded7e7ce 664 sendUpdatesTo.Clear();
ff684fef 665
f070564b 666 {
f00ff009 667 CLockObject lock(m_mutex);
30b4aac0 668 m_configuration.iPhysicalAddress = iPhysicalAddress;
4f362964 669
f070564b
LOK
670 if (!m_logicalAddresses.IsEmpty())
671 {
672 bool bWasActiveSource(false);
673 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
674 if (m_logicalAddresses[iPtr])
675 {
676 bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
677 m_busDevices[iPtr]->SetInactiveSource();
678 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
679 if (bSendUpdate)
4d6f5ac7 680 sendUpdatesTo.Set((cec_logical_address)iPtr);
f070564b
LOK
681 }
682
683 bSendActiveView = bWasActiveSource && bSendUpdate;
684 bReturn = true;
685 }
cc60ab1c 686 }
f070564b 687
4d6f5ac7
LOK
688 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
689 if (sendUpdatesTo[iPtr])
690 m_busDevices[iPtr]->TransmitPhysicalAddress();
691
f070564b
LOK
692 if (bSendActiveView)
693 SetActiveView();
694
32403cc3
LOK
695 if (bReturn)
696 CLibCEC::ConfigurationChanged(m_configuration);
697
f070564b 698 return bReturn;
1969b140
LOK
699}
700
8b7e5ff6
LOK
701bool CCECProcessor::SwitchMonitoring(bool bEnable)
702{
5477a250 703 CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
8b7e5ff6 704
855a3a98 705 {
f00ff009 706 CLockObject lock(m_mutex);
855a3a98 707 m_bMonitor = bEnable;
855a3a98
LOK
708 }
709
8b7e5ff6 710 if (bEnable)
06bfd4d7 711 return SetAckMask(0);
8b7e5ff6 712 else
ab1469a0 713 return SetAckMask(m_logicalAddresses.AckMask());
8b7e5ff6
LOK
714}
715
57f45e6c
LOK
716bool CCECProcessor::PollDevice(cec_logical_address iAddress)
717{
718 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
95a73fa7
LOK
719 {
720 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
721 m_busDevices[iAddress]->TransmitPoll(iAddress) :
722 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
723 }
57f45e6c
LOK
724 return false;
725}
726
5c73f7f7 727uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
04e637f9
LOK
728{
729 uint8_t status = 0;
37b0c572 730 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
5c73f7f7 731 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
04e637f9
LOK
732
733 return status;
734}
735
5c73f7f7 736uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
04e637f9
LOK
737{
738 uint8_t status = 0;
37b0c572 739 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
5c73f7f7 740 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
04e637f9
LOK
741
742 return status;
743}
744
5c73f7f7 745uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
04e637f9
LOK
746{
747 uint8_t status = 0;
37b0c572 748 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
5c73f7f7 749 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
04e637f9
LOK
750
751 return status;
752}
753
16b1e052 754CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
8ac9c610 755{
16b1e052 756 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
b58d9277 757 return m_busDevices[m_logicalAddresses.primary];
8ac9c610 758
b58d9277 759 CCECBusDevice *device = NULL;
8ac9c610
LOK
760 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
761 {
16b1e052 762 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
8ac9c610
LOK
763 {
764 device = m_busDevices[iPtr];
765 break;
766 }
767 }
768
769 return device;
770}
771
a9232a79
LOK
772CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
773{
774 CCECBusDevice *device = NULL;
775
7619faad 776 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
a9232a79 777 {
b64db02e 778 if (m_busDevices[iPtr]->m_type == type && m_logicalAddresses[iPtr])
a9232a79
LOK
779 {
780 device = m_busDevices[iPtr];
781 break;
782 }
783 }
784
785 return device;
786}
787
842262d8
LOK
788CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
789{
790 CCECBusDevice *device(NULL);
791 cec_logical_address primary = m_logicalAddresses.primary;
792 if (primary != CECDEVICE_UNKNOWN)
793 device = m_busDevices[primary];
794 return device;
795}
796
6a1c0009
LOK
797cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
798{
799 return m_busDevices[iAddress]->GetCecVersion();
800}
801
ed21be2a
LOK
802cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
803{
804 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
805 cec_osd_name retVal;
806
807 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
808 retVal.device = iAddress;
809
810 return retVal;
811}
812
a3269a0a
LOK
813bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
814{
cc60ab1c
LOK
815 if (m_busDevices[iAddress])
816 {
817 *language = m_busDevices[iAddress]->GetMenuLanguage();
5744fbba 818 return (strcmp(language->language, "???") != 0);
cc60ab1c
LOK
819 }
820 return false;
a3269a0a
LOK
821}
822
44c74256
LOK
823uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
824{
cc60ab1c
LOK
825 if (m_busDevices[iAddress])
826 return m_busDevices[iAddress]->GetVendorId();
827 return false;
44c74256
LOK
828}
829
eab72c40
LOK
830uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
831{
832 if (m_busDevices[iAddress])
833 return m_busDevices[iAddress]->GetPhysicalAddress(false);
834 return false;
835}
836
e55f3f70
LOK
837cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
838{
cc60ab1c
LOK
839 if (m_busDevices[iAddress])
840 return m_busDevices[iAddress]->GetPowerStatus();
841 return CEC_POWER_STATUS_UNKNOWN;
e55f3f70
LOK
842}
843
b4b1b49b
LOK
844cec_logical_address CCECProcessor::GetActiveSource(void)
845{
846 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
847 {
848 if (m_busDevices[iPtr]->IsActiveSource())
849 return (cec_logical_address)iPtr;
850 }
851
852 return CECDEVICE_UNKNOWN;
853}
854
855bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
856{
857 return m_busDevices[iAddress]->IsActiveSource();
858}
859
8d84e2c0 860bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 861{
7bb4ed43 862 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
eb35e4ca 863 {
7bb4ed43
LOK
864 CLockObject lock(m_mutex);
865 LogOutput(data);
5f316715 866 m_iLastTransmission = GetTimeMs();
7bb4ed43
LOK
867 if (!m_communication)
868 return false;
869 uint8_t iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
870 retVal = m_communication->Write(data, iMaxTries, m_iLineTimeout, m_iRetryLineTimeout);
2abe74eb
LOK
871 }
872
7bb4ed43
LOK
873 /* set to "not present" on failed ack */
874 if (retVal == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED &&
875 data.destination != CECDEVICE_BROADCAST)
876 m_busDevices[data.destination]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
9902f4e8 877
7bb4ed43 878 return retVal == ADAPTER_MESSAGE_STATE_SENT_ACKED;
825ddb96 879}
abbca718 880
22b4e74a 881void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 882{
5477a250 883 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
9dee1670 884
06a1f7ce 885 cec_command command;
f8513317 886 // TODO
ab1469a0
LOK
887 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
888 command.parameters.PushBack((uint8_t)opcode);
889 command.parameters.PushBack((uint8_t)reason);
9dee1670
LOK
890
891 Transmit(command);
abbca718
LOK
892}
893
b1f94db1 894void CCECProcessor::ParseCommand(const cec_command &command)
abbca718 895{
e9de9629 896 CStdString dataStr;
1d3ca3de 897 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
898 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
899 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
5477a250 900 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 901
7871d66e 902 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
e9de9629 903 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
904}
905
6d858ba4
LOK
906cec_logical_addresses CCECProcessor::GetActiveDevices(void)
907{
908 cec_logical_addresses addresses;
988de7b9 909 addresses.Clear();
6d858ba4
LOK
910 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
911 {
912 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
913 addresses.Set((cec_logical_address) iPtr);
914 }
915 return addresses;
916}
917
37b0c572 918bool CCECProcessor::IsPresentDevice(cec_logical_address address)
6d858ba4
LOK
919{
920 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
921}
922
37b0c572 923bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
6d858ba4 924{
6d858ba4 925 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
2813bd07
LOK
926 {
927 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
6d858ba4 928 return true;
2813bd07
LOK
929 }
930
6d858ba4
LOK
931 return false;
932}
933
0f23c85c
LOK
934uint16_t CCECProcessor::GetPhysicalAddress(void) const
935{
ab1469a0 936 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
16b1e052 937 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
cc60ab1c 938 return false;
0f23c85c
LOK
939}
940
06bfd4d7
LOK
941bool CCECProcessor::SetAckMask(uint16_t iMask)
942{
5dcf9f25 943 return m_communication->SetAckMask(iMask);
06bfd4d7 944}
a33794d8 945
4bec9d79 946bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
a33794d8 947{
86869f8f 948 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
a33794d8
LOK
949}
950
4bec9d79 951bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
a33794d8 952{
86869f8f 953 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
a33794d8 954}
7c63a480 955
3e61b350
LOK
956const char *CCECProcessor::ToString(const cec_device_type type)
957{
958 switch (type)
959 {
960 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
961 return "audio system";
962 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
963 return "playback device";
964 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
965 return "recording device";
966 case CEC_DEVICE_TYPE_RESERVED:
967 return "reserved";
968 case CEC_DEVICE_TYPE_TUNER:
969 return "tuner";
970 case CEC_DEVICE_TYPE_TV:
971 return "TV";
972 default:
973 return "unknown";
974 }
975}
976
03ae897d
LOK
977const char *CCECProcessor::ToString(const cec_menu_state state)
978{
979 switch (state)
980 {
981 case CEC_MENU_STATE_ACTIVATED:
982 return "activated";
983 case CEC_MENU_STATE_DEACTIVATED:
984 return "deactivated";
985 default:
986 return "unknown";
987 }
988}
989
990const char *CCECProcessor::ToString(const cec_version version)
991{
992 switch (version)
993 {
994 case CEC_VERSION_1_2:
995 return "1.2";
996 case CEC_VERSION_1_2A:
997 return "1.2a";
998 case CEC_VERSION_1_3:
999 return "1.3";
1000 case CEC_VERSION_1_3A:
1001 return "1.3a";
1002 case CEC_VERSION_1_4:
1003 return "1.4";
1004 default:
1005 return "unknown";
1006 }
1007}
1008
1009const char *CCECProcessor::ToString(const cec_power_status status)
1010{
1011 switch (status)
1012 {
1013 case CEC_POWER_STATUS_ON:
1014 return "on";
1015 case CEC_POWER_STATUS_STANDBY:
1016 return "standby";
1017 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1018 return "in transition from on to standby";
1019 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1020 return "in transition from standby to on";
1021 default:
1022 return "unknown";
1023 }
1024}
1025
1026const char *CCECProcessor::ToString(const cec_logical_address address)
1027{
1028 switch(address)
1029 {
1030 case CECDEVICE_AUDIOSYSTEM:
1031 return "Audio";
1032 case CECDEVICE_BROADCAST:
1033 return "Broadcast";
1034 case CECDEVICE_FREEUSE:
1035 return "Free use";
1036 case CECDEVICE_PLAYBACKDEVICE1:
1037 return "Playback 1";
1038 case CECDEVICE_PLAYBACKDEVICE2:
1039 return "Playback 2";
1040 case CECDEVICE_PLAYBACKDEVICE3:
1041 return "Playback 3";
1042 case CECDEVICE_RECORDINGDEVICE1:
1043 return "Recorder 1";
1044 case CECDEVICE_RECORDINGDEVICE2:
1045 return "Recorder 2";
1046 case CECDEVICE_RECORDINGDEVICE3:
1047 return "Recorder 3";
1048 case CECDEVICE_RESERVED1:
1049 return "Reserved 1";
1050 case CECDEVICE_RESERVED2:
1051 return "Reserved 2";
1052 case CECDEVICE_TUNER1:
1053 return "Tuner 1";
1054 case CECDEVICE_TUNER2:
1055 return "Tuner 2";
1056 case CECDEVICE_TUNER3:
1057 return "Tuner 3";
1058 case CECDEVICE_TUNER4:
1059 return "Tuner 4";
1060 case CECDEVICE_TV:
1061 return "TV";
1062 default:
1063 return "unknown";
1064 }
1065}
1066
1067const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1068{
1069 switch (mode)
1070 {
1071 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1072 return "skip forward wind";
1073 case CEC_DECK_CONTROL_MODE_EJECT:
1074 return "eject";
1075 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1076 return "reverse rewind";
1077 case CEC_DECK_CONTROL_MODE_STOP:
1078 return "stop";
1079 default:
1080 return "unknown";
1081 }
1082}
1083
1084const char *CCECProcessor::ToString(const cec_deck_info status)
1085{
1086 switch (status)
1087 {
1088 case CEC_DECK_INFO_PLAY:
1089 return "play";
1090 case CEC_DECK_INFO_RECORD:
1091 return "record";
1092 case CEC_DECK_INFO_PLAY_REVERSE:
1093 return "play reverse";
1094 case CEC_DECK_INFO_STILL:
1095 return "still";
1096 case CEC_DECK_INFO_SLOW:
1097 return "slow";
1098 case CEC_DECK_INFO_SLOW_REVERSE:
1099 return "slow reverse";
1100 case CEC_DECK_INFO_FAST_FORWARD:
1101 return "fast forward";
1102 case CEC_DECK_INFO_FAST_REVERSE:
1103 return "fast reverse";
1104 case CEC_DECK_INFO_NO_MEDIA:
1105 return "no media";
1106 case CEC_DECK_INFO_STOP:
1107 return "stop";
1108 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1109 return "info skip forward wind";
1110 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1111 return "info skip reverse rewind";
1112 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1113 return "info index search forward";
1114 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1115 return "info index search reverse";
1116 case CEC_DECK_INFO_OTHER_STATUS:
1117 return "other";
1118 default:
1119 return "unknown";
1120 }
1121}
1122
1123const char *CCECProcessor::ToString(const cec_opcode opcode)
1124{
1125 switch (opcode)
1126 {
1127 case CEC_OPCODE_ACTIVE_SOURCE:
1128 return "active source";
1129 case CEC_OPCODE_IMAGE_VIEW_ON:
1130 return "image view on";
1131 case CEC_OPCODE_TEXT_VIEW_ON:
1132 return "text view on";
1133 case CEC_OPCODE_INACTIVE_SOURCE:
1134 return "inactive source";
1135 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1136 return "request active source";
1137 case CEC_OPCODE_ROUTING_CHANGE:
1138 return "routing change";
1139 case CEC_OPCODE_ROUTING_INFORMATION:
1140 return "routing information";
1141 case CEC_OPCODE_SET_STREAM_PATH:
1142 return "set stream path";
1143 case CEC_OPCODE_STANDBY:
1144 return "standby";
1145 case CEC_OPCODE_RECORD_OFF:
1146 return "record off";
1147 case CEC_OPCODE_RECORD_ON:
1148 return "record on";
1149 case CEC_OPCODE_RECORD_STATUS:
1150 return "record status";
1151 case CEC_OPCODE_RECORD_TV_SCREEN:
1152 return "record tv screen";
1153 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1154 return "clear analogue timer";
1155 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1156 return "clear digital timer";
1157 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1158 return "clear external timer";
1159 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1160 return "set analogue timer";
1161 case CEC_OPCODE_SET_DIGITAL_TIMER:
1162 return "set digital timer";
1163 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1164 return "set external timer";
1165 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1166 return "set timer program title";
1167 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1168 return "timer cleared status";
1169 case CEC_OPCODE_TIMER_STATUS:
1170 return "timer status";
1171 case CEC_OPCODE_CEC_VERSION:
1172 return "cec version";
1173 case CEC_OPCODE_GET_CEC_VERSION:
1174 return "get cec version";
1175 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1176 return "give physical address";
1177 case CEC_OPCODE_GET_MENU_LANGUAGE:
1178 return "get menu language";
1179 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1180 return "report physical address";
1181 case CEC_OPCODE_SET_MENU_LANGUAGE:
1182 return "set menu language";
1183 case CEC_OPCODE_DECK_CONTROL:
1184 return "deck control";
1185 case CEC_OPCODE_DECK_STATUS:
1186 return "deck status";
1187 case CEC_OPCODE_GIVE_DECK_STATUS:
1188 return "give deck status";
1189 case CEC_OPCODE_PLAY:
1190 return "play";
1191 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1192 return "give tuner status";
1193 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1194 return "select analogue service";
1195 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1196 return "set digital service";
1197 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1198 return "tuner device status";
1199 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1200 return "tuner step decrement";
1201 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1202 return "tuner step increment";
1203 case CEC_OPCODE_DEVICE_VENDOR_ID:
1204 return "device vendor id";
1205 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1206 return "give device vendor id";
1207 case CEC_OPCODE_VENDOR_COMMAND:
1208 return "vendor command";
1209 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1210 return "vendor command with id";
1211 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1212 return "vendor remote button down";
1213 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1214 return "vendor remote button up";
1215 case CEC_OPCODE_SET_OSD_STRING:
1216 return "set osd string";
1217 case CEC_OPCODE_GIVE_OSD_NAME:
1218 return "give osd name";
1219 case CEC_OPCODE_SET_OSD_NAME:
1220 return "set osd name";
1221 case CEC_OPCODE_MENU_REQUEST:
1222 return "menu request";
1223 case CEC_OPCODE_MENU_STATUS:
1224 return "menu status";
1225 case CEC_OPCODE_USER_CONTROL_PRESSED:
1226 return "user control pressed";
1227 case CEC_OPCODE_USER_CONTROL_RELEASE:
1228 return "user control release";
1229 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1230 return "give device power status";
1231 case CEC_OPCODE_REPORT_POWER_STATUS:
1232 return "report power status";
1233 case CEC_OPCODE_FEATURE_ABORT:
1234 return "feature abort";
1235 case CEC_OPCODE_ABORT:
1236 return "abort";
1237 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1238 return "give audio status";
1239 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1240 return "give audio mode status";
1241 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1242 return "report audio status";
1243 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1244 return "set system audio mode";
1245 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1246 return "system audio mode request";
1247 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1248 return "system audio mode status";
1249 case CEC_OPCODE_SET_AUDIO_RATE:
1250 return "set audio rate";
1251 default:
1252 return "UNKNOWN";
1253 }
1254}
1255
1256const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1257{
1258 switch(mode)
1259 {
1260 case CEC_SYSTEM_AUDIO_STATUS_ON:
1261 return "on";
1262 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1263 return "off";
1264 default:
1265 return "unknown";
1266 }
1267}
1268
1de6617c 1269const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
03ae897d
LOK
1270{
1271 // TODO this is a mask
1272 return "TODO";
1273}
1274
1275const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1276{
1277 switch (vendor)
1278 {
1279 case CEC_VENDOR_SAMSUNG:
1280 return "Samsung";
1281 case CEC_VENDOR_LG:
1282 return "LG";
1283 case CEC_VENDOR_PANASONIC:
1284 return "Panasonic";
1285 case CEC_VENDOR_PIONEER:
1286 return "Pioneer";
1287 case CEC_VENDOR_ONKYO:
1288 return "Onkyo";
1289 case CEC_VENDOR_YAMAHA:
1290 return "Yamaha";
ec0deac1
LOK
1291 case CEC_VENDOR_PHILIPS:
1292 return "Philips";
1a87cacc
LOK
1293 case CEC_VENDOR_SONY:
1294 return "Sony";
03ae897d
LOK
1295 default:
1296 return "Unknown";
1297 }
1298}
1299
caca2d81
LOK
1300const char *CCECProcessor::ToString(const cec_client_version version)
1301{
1302 switch (version)
1303 {
1304 case CEC_CLIENT_VERSION_PRE_1_5:
1305 return "pre-1.5";
1306 case CEC_CLIENT_VERSION_1_5_0:
1307 return "1.5.0";
1308 default:
1309 return "Unknown";
1310 }
1311}
1312
7c63a480
LOK
1313void *CCECBusScan::Process(void)
1314{
1315 CCECBusDevice *device(NULL);
fe6f8e37 1316 uint8_t iCounter(0);
5f316715 1317
5e5637c6 1318 while (!IsStopped())
7c63a480 1319 {
bdb0f7f4 1320 if (++iCounter < 10)
fe6f8e37
LOK
1321 {
1322 Sleep(1000);
1323 continue;
1324 }
5f316715 1325 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
7c63a480 1326 {
5f316715
LOK
1327 device = m_processor->m_busDevices[iPtr];
1328 WaitUntilIdle();
1329 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
5e5637c6 1330 {
5f316715
LOK
1331 WaitUntilIdle();
1332 if (!IsStopped())
1333 device->GetVendorId();
1334
1335 WaitUntilIdle();
1336 if (!IsStopped())
1337 device->GetPowerStatus(true);
5e5637c6 1338 }
7c63a480
LOK
1339 }
1340 }
5f316715 1341
7c63a480
LOK
1342 return NULL;
1343}
1113cb7d 1344
5f316715
LOK
1345void CCECBusScan::WaitUntilIdle(void)
1346{
1347 if (IsStopped())
1348 return;
1349
701f753b 1350 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
5f316715
LOK
1351 while (iWaitTime > 0)
1352 {
1353 Sleep(iWaitTime);
701f753b 1354 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
5f316715
LOK
1355 }
1356}
1357
1113cb7d
LOK
1358bool CCECProcessor::StartBootloader(void)
1359{
1360 return m_communication->StartBootloader();
1361}
1362
1363bool CCECProcessor::PingAdapter(void)
1364{
1365 return m_communication->PingAdapter();
1366}
6729ac71
LOK
1367
1368void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1369{
0cfdeb5a 1370 m_busDevices[initiator]->HandlePoll(destination);
6729ac71
LOK
1371}
1372
7bb4ed43 1373bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
6729ac71 1374{
0cfdeb5a 1375 return !m_busDevices[initiator]->HandleReceiveFailed();
6729ac71 1376}
f42d3e0f
LOK
1377
1378bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1379{
1380 // stream path changes are sent by the TV
1381 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1382}
d40928b5 1383
30b4aac0
LOK
1384bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1385{
1386 bool bNeedsReinit(false);
1387 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
1388 m_configuration.clientVersion = configuration->clientVersion;
1389
1390 // client version 1.5.0
1391
1392 // device types
1393 bNeedsReinit |= IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
1394 m_configuration.deviceTypes = configuration->deviceTypes;
1395
1396 // physical address
1397 bNeedsReinit |= IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
1398 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1399
1400 // base device
1401 bNeedsReinit |= IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
1402 m_configuration.baseDevice = configuration->baseDevice;
1403
1404 // hdmi port
1405 bNeedsReinit |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
1406 m_configuration.iHDMIPort = configuration->iHDMIPort;
1407
1408 // device name
1409 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1410 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1411 {
1412 primary->SetOSDName(m_configuration.strDeviceName);
1413 if (!bNeedsReinit && IsRunning())
1414 primary->TransmitOSDName(CECDEVICE_TV);
1415 }
1416
1417 // tv vendor id override
1418 if (m_configuration.tvVendor != configuration->tvVendor)
1419 {
1420 m_configuration.tvVendor= configuration->tvVendor;
1421 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1422 }
1423
1424 // wake CEC devices
1425 if (m_configuration.wakeDevices != configuration->wakeDevices)
1426 {
1427 m_configuration.wakeDevices = configuration->wakeDevices;
1428 if (!bNeedsReinit && IsRunning())
1429 WakeDevices();
1430 }
1431
1432 // just copy these
1433 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
1434 m_configuration.bPowerOnStartup = configuration->bPowerOnStartup;
1435 m_configuration.bPowerOffShutdown = configuration->bPowerOffShutdown;
1436 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1437 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1438
1439 // ensure that there is at least 1 device type set
1440 if (m_configuration.deviceTypes.IsEmpty())
1441 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1442
1443 if (bNeedsReinit)
1444 {
1445 SetInitialised(false);
1446 m_logicalAddresses.Clear();
1447 return Initialise();
1448 }
1449
1450 return true;
1451}
1452
d40928b5
LOK
1453bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1454{
30b4aac0
LOK
1455 m_configuration.tvVendor = m_busDevices[CECDEVICE_TV]->GetVendorId();
1456
1457 // client version 1.5.0
1458 configuration->clientVersion = m_configuration.clientVersion;
1459 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1460 configuration->deviceTypes = m_configuration.deviceTypes;
1461 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1462 configuration->baseDevice = m_configuration.baseDevice;
1463 configuration->iHDMIPort = m_configuration.iHDMIPort;
1464 configuration->tvVendor = m_configuration.tvVendor;
1465 configuration->wakeDevices = m_configuration.wakeDevices;
1466 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
1467 configuration->bPowerOnStartup = m_configuration.bPowerOnStartup;
1468 configuration->bPowerOffShutdown = m_configuration.bPowerOffShutdown;
1469 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1470 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1471
d40928b5
LOK
1472 return true;
1473}
224ea877
LOK
1474
1475bool CCECProcessor::CanPersistConfiguration(void)
1476{
1477 return m_communication->GetFirmwareVersion() >= 2;
1478}
1479
1480bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1481{
1482 return m_communication->PersistConfiguration(configuration);
1483}