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