cec: persist settings directly when they're changed, only persist settings that actua...
[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();
e69d8f19 62 m_configuration.serverVersion = CEC_SERVER_VERSION_1_6_0;
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();
e69d8f19 80 m_configuration.serverVersion = CEC_SERVER_VERSION_1_6_0;
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{
897 return m_busDevices[iAddress]->IsActiveSource();
898}
899
8d84e2c0 900bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 901{
dd18e809 902 if (m_configuration.logicalAddresses[(uint8_t)data.destination])
c4287bcd
LOK
903 {
904 CLibCEC::AddLog(CEC_LOG_WARNING, "not sending data to myself!");
905 return false;
906 }
907
7cf8ba15 908 uint8_t iMaxTries(0);
eb35e4ca 909 {
7bb4ed43 910 CLockObject lock(m_mutex);
44a1d92a
LOK
911 if (IsStopped())
912 return false;
7bb4ed43 913 LogOutput(data);
5f316715 914 m_iLastTransmission = GetTimeMs();
44195125 915 if (!m_communication || !m_communication->IsOpen())
f9e01dac
LOK
916 {
917 CLibCEC::AddLog(CEC_LOG_ERROR, "cannot transmit command: connection closed");
7bb4ed43 918 return false;
f9e01dac 919 }
7cf8ba15 920 iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
2abe74eb
LOK
921 }
922
5de67222 923 return m_communication->Write(data, iMaxTries, m_iStandardLineTimeout, m_iRetryLineTimeout)
7cf8ba15 924 == ADAPTER_MESSAGE_STATE_SENT_ACKED;
825ddb96 925}
abbca718 926
22b4e74a 927void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 928{
5477a250 929 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
9dee1670 930
06a1f7ce 931 cec_command command;
f8513317 932 // TODO
dd18e809 933 cec_command::Format(command, m_configuration.logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
ab1469a0
LOK
934 command.parameters.PushBack((uint8_t)opcode);
935 command.parameters.PushBack((uint8_t)reason);
9dee1670
LOK
936
937 Transmit(command);
abbca718
LOK
938}
939
b1f94db1 940void CCECProcessor::ParseCommand(const cec_command &command)
abbca718 941{
e9de9629 942 CStdString dataStr;
d297cbd4
LOK
943 dataStr.Format(">> %1x%1x", command.initiator, command.destination);
944 if (command.opcode_set == 1)
945 dataStr.AppendFormat(":%02x", command.opcode);
e9de9629
LOK
946 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
947 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
5477a250 948 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 949
7871d66e 950 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
e9de9629 951 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
952}
953
6d858ba4
LOK
954cec_logical_addresses CCECProcessor::GetActiveDevices(void)
955{
956 cec_logical_addresses addresses;
988de7b9 957 addresses.Clear();
6d858ba4
LOK
958 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
959 {
960 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
961 addresses.Set((cec_logical_address) iPtr);
962 }
963 return addresses;
964}
965
37b0c572 966bool CCECProcessor::IsPresentDevice(cec_logical_address address)
6d858ba4
LOK
967{
968 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
969}
970
37b0c572 971bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
6d858ba4 972{
6d858ba4 973 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
2813bd07
LOK
974 {
975 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
6d858ba4 976 return true;
2813bd07
LOK
977 }
978
6d858ba4
LOK
979 return false;
980}
981
0f23c85c
LOK
982uint16_t CCECProcessor::GetPhysicalAddress(void) const
983{
dd18e809
LOK
984 if (!m_configuration.logicalAddresses.IsEmpty() && m_busDevices[m_configuration.logicalAddresses.primary])
985 return m_busDevices[m_configuration.logicalAddresses.primary]->GetPhysicalAddress(false);
cc60ab1c 986 return false;
0f23c85c
LOK
987}
988
06bfd4d7
LOK
989bool CCECProcessor::SetAckMask(uint16_t iMask)
990{
5dcf9f25 991 return m_communication->SetAckMask(iMask);
06bfd4d7 992}
a33794d8 993
4bec9d79 994bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
a33794d8 995{
86869f8f 996 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
a33794d8
LOK
997}
998
4bec9d79 999bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
a33794d8 1000{
86869f8f 1001 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
a33794d8 1002}
7c63a480 1003
8670c970
LOK
1004bool CCECProcessor::EnablePhysicalAddressDetection(void)
1005{
1006 CLibCEC::AddLog(CEC_LOG_WARNING, "deprecated method %s called", __FUNCTION__);
1007 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
1008 if (iPhysicalAddress != 0)
1009 {
1010 m_configuration.bAutodetectAddress = 1;
1011 m_configuration.iPhysicalAddress = iPhysicalAddress;
1012 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1013 m_configuration.iHDMIPort = 0;
1014 return SetPhysicalAddress(iPhysicalAddress);
1015 }
1016 return false;
1017}
1018
ca27e6cf
LOK
1019bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1020{
1021 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1022 {
1023 bool bReturn(true);
1024 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1025 {
1026 if (m_configuration.powerOffDevices[iPtr])
1027 bReturn &= m_busDevices[iPtr]->Standby();
1028 }
1029 return bReturn;
1030 }
1031
1032 return m_busDevices[address]->Standby();
1033}
1034
1035bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
1036{
1037 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
1038 {
1039 bool bReturn(true);
1040 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
1041 {
1042 if (m_configuration.powerOffDevices[iPtr])
1043 bReturn &= m_busDevices[iPtr]->PowerOn();
1044 }
1045 return bReturn;
1046 }
1047
1048 return m_busDevices[address]->PowerOn();
1049}
1050
3e61b350
LOK
1051const char *CCECProcessor::ToString(const cec_device_type type)
1052{
1053 switch (type)
1054 {
1055 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
1056 return "audio system";
1057 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
1058 return "playback device";
1059 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
1060 return "recording device";
1061 case CEC_DEVICE_TYPE_RESERVED:
1062 return "reserved";
1063 case CEC_DEVICE_TYPE_TUNER:
1064 return "tuner";
1065 case CEC_DEVICE_TYPE_TV:
1066 return "TV";
1067 default:
1068 return "unknown";
1069 }
1070}
1071
03ae897d
LOK
1072const char *CCECProcessor::ToString(const cec_menu_state state)
1073{
1074 switch (state)
1075 {
1076 case CEC_MENU_STATE_ACTIVATED:
1077 return "activated";
1078 case CEC_MENU_STATE_DEACTIVATED:
1079 return "deactivated";
1080 default:
1081 return "unknown";
1082 }
1083}
1084
1085const char *CCECProcessor::ToString(const cec_version version)
1086{
1087 switch (version)
1088 {
1089 case CEC_VERSION_1_2:
1090 return "1.2";
1091 case CEC_VERSION_1_2A:
1092 return "1.2a";
1093 case CEC_VERSION_1_3:
1094 return "1.3";
1095 case CEC_VERSION_1_3A:
1096 return "1.3a";
1097 case CEC_VERSION_1_4:
1098 return "1.4";
1099 default:
1100 return "unknown";
1101 }
1102}
1103
1104const char *CCECProcessor::ToString(const cec_power_status status)
1105{
1106 switch (status)
1107 {
1108 case CEC_POWER_STATUS_ON:
1109 return "on";
1110 case CEC_POWER_STATUS_STANDBY:
1111 return "standby";
1112 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1113 return "in transition from on to standby";
1114 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1115 return "in transition from standby to on";
1116 default:
1117 return "unknown";
1118 }
1119}
1120
1121const char *CCECProcessor::ToString(const cec_logical_address address)
1122{
1123 switch(address)
1124 {
1125 case CECDEVICE_AUDIOSYSTEM:
1126 return "Audio";
1127 case CECDEVICE_BROADCAST:
1128 return "Broadcast";
1129 case CECDEVICE_FREEUSE:
1130 return "Free use";
1131 case CECDEVICE_PLAYBACKDEVICE1:
1132 return "Playback 1";
1133 case CECDEVICE_PLAYBACKDEVICE2:
1134 return "Playback 2";
1135 case CECDEVICE_PLAYBACKDEVICE3:
1136 return "Playback 3";
1137 case CECDEVICE_RECORDINGDEVICE1:
1138 return "Recorder 1";
1139 case CECDEVICE_RECORDINGDEVICE2:
1140 return "Recorder 2";
1141 case CECDEVICE_RECORDINGDEVICE3:
1142 return "Recorder 3";
1143 case CECDEVICE_RESERVED1:
1144 return "Reserved 1";
1145 case CECDEVICE_RESERVED2:
1146 return "Reserved 2";
1147 case CECDEVICE_TUNER1:
1148 return "Tuner 1";
1149 case CECDEVICE_TUNER2:
1150 return "Tuner 2";
1151 case CECDEVICE_TUNER3:
1152 return "Tuner 3";
1153 case CECDEVICE_TUNER4:
1154 return "Tuner 4";
1155 case CECDEVICE_TV:
1156 return "TV";
1157 default:
1158 return "unknown";
1159 }
1160}
1161
1162const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1163{
1164 switch (mode)
1165 {
1166 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1167 return "skip forward wind";
1168 case CEC_DECK_CONTROL_MODE_EJECT:
1169 return "eject";
1170 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1171 return "reverse rewind";
1172 case CEC_DECK_CONTROL_MODE_STOP:
1173 return "stop";
1174 default:
1175 return "unknown";
1176 }
1177}
1178
1179const char *CCECProcessor::ToString(const cec_deck_info status)
1180{
1181 switch (status)
1182 {
1183 case CEC_DECK_INFO_PLAY:
1184 return "play";
1185 case CEC_DECK_INFO_RECORD:
1186 return "record";
1187 case CEC_DECK_INFO_PLAY_REVERSE:
1188 return "play reverse";
1189 case CEC_DECK_INFO_STILL:
1190 return "still";
1191 case CEC_DECK_INFO_SLOW:
1192 return "slow";
1193 case CEC_DECK_INFO_SLOW_REVERSE:
1194 return "slow reverse";
1195 case CEC_DECK_INFO_FAST_FORWARD:
1196 return "fast forward";
1197 case CEC_DECK_INFO_FAST_REVERSE:
1198 return "fast reverse";
1199 case CEC_DECK_INFO_NO_MEDIA:
1200 return "no media";
1201 case CEC_DECK_INFO_STOP:
1202 return "stop";
1203 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1204 return "info skip forward wind";
1205 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1206 return "info skip reverse rewind";
1207 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1208 return "info index search forward";
1209 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1210 return "info index search reverse";
1211 case CEC_DECK_INFO_OTHER_STATUS:
1212 return "other";
1213 default:
1214 return "unknown";
1215 }
1216}
1217
1218const char *CCECProcessor::ToString(const cec_opcode opcode)
1219{
1220 switch (opcode)
1221 {
1222 case CEC_OPCODE_ACTIVE_SOURCE:
1223 return "active source";
1224 case CEC_OPCODE_IMAGE_VIEW_ON:
1225 return "image view on";
1226 case CEC_OPCODE_TEXT_VIEW_ON:
1227 return "text view on";
1228 case CEC_OPCODE_INACTIVE_SOURCE:
1229 return "inactive source";
1230 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1231 return "request active source";
1232 case CEC_OPCODE_ROUTING_CHANGE:
1233 return "routing change";
1234 case CEC_OPCODE_ROUTING_INFORMATION:
1235 return "routing information";
1236 case CEC_OPCODE_SET_STREAM_PATH:
1237 return "set stream path";
1238 case CEC_OPCODE_STANDBY:
1239 return "standby";
1240 case CEC_OPCODE_RECORD_OFF:
1241 return "record off";
1242 case CEC_OPCODE_RECORD_ON:
1243 return "record on";
1244 case CEC_OPCODE_RECORD_STATUS:
1245 return "record status";
1246 case CEC_OPCODE_RECORD_TV_SCREEN:
1247 return "record tv screen";
1248 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1249 return "clear analogue timer";
1250 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1251 return "clear digital timer";
1252 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1253 return "clear external timer";
1254 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1255 return "set analogue timer";
1256 case CEC_OPCODE_SET_DIGITAL_TIMER:
1257 return "set digital timer";
1258 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1259 return "set external timer";
1260 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1261 return "set timer program title";
1262 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1263 return "timer cleared status";
1264 case CEC_OPCODE_TIMER_STATUS:
1265 return "timer status";
1266 case CEC_OPCODE_CEC_VERSION:
1267 return "cec version";
1268 case CEC_OPCODE_GET_CEC_VERSION:
1269 return "get cec version";
1270 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1271 return "give physical address";
1272 case CEC_OPCODE_GET_MENU_LANGUAGE:
1273 return "get menu language";
1274 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1275 return "report physical address";
1276 case CEC_OPCODE_SET_MENU_LANGUAGE:
1277 return "set menu language";
1278 case CEC_OPCODE_DECK_CONTROL:
1279 return "deck control";
1280 case CEC_OPCODE_DECK_STATUS:
1281 return "deck status";
1282 case CEC_OPCODE_GIVE_DECK_STATUS:
1283 return "give deck status";
1284 case CEC_OPCODE_PLAY:
1285 return "play";
1286 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1287 return "give tuner status";
1288 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1289 return "select analogue service";
1290 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1291 return "set digital service";
1292 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1293 return "tuner device status";
1294 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1295 return "tuner step decrement";
1296 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1297 return "tuner step increment";
1298 case CEC_OPCODE_DEVICE_VENDOR_ID:
1299 return "device vendor id";
1300 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1301 return "give device vendor id";
1302 case CEC_OPCODE_VENDOR_COMMAND:
1303 return "vendor command";
1304 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1305 return "vendor command with id";
1306 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1307 return "vendor remote button down";
1308 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1309 return "vendor remote button up";
1310 case CEC_OPCODE_SET_OSD_STRING:
1311 return "set osd string";
1312 case CEC_OPCODE_GIVE_OSD_NAME:
1313 return "give osd name";
1314 case CEC_OPCODE_SET_OSD_NAME:
1315 return "set osd name";
1316 case CEC_OPCODE_MENU_REQUEST:
1317 return "menu request";
1318 case CEC_OPCODE_MENU_STATUS:
1319 return "menu status";
1320 case CEC_OPCODE_USER_CONTROL_PRESSED:
1321 return "user control pressed";
1322 case CEC_OPCODE_USER_CONTROL_RELEASE:
1323 return "user control release";
1324 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1325 return "give device power status";
1326 case CEC_OPCODE_REPORT_POWER_STATUS:
1327 return "report power status";
1328 case CEC_OPCODE_FEATURE_ABORT:
1329 return "feature abort";
1330 case CEC_OPCODE_ABORT:
1331 return "abort";
1332 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1333 return "give audio status";
1334 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1335 return "give audio mode status";
1336 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1337 return "report audio status";
1338 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1339 return "set system audio mode";
1340 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1341 return "system audio mode request";
1342 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1343 return "system audio mode status";
1344 case CEC_OPCODE_SET_AUDIO_RATE:
1345 return "set audio rate";
24dd566c
LOK
1346 case CEC_OPCODE_NONE:
1347 return "poll";
03ae897d
LOK
1348 default:
1349 return "UNKNOWN";
1350 }
1351}
1352
1353const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1354{
1355 switch(mode)
1356 {
1357 case CEC_SYSTEM_AUDIO_STATUS_ON:
1358 return "on";
1359 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1360 return "off";
1361 default:
1362 return "unknown";
1363 }
1364}
1365
1de6617c 1366const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
03ae897d
LOK
1367{
1368 // TODO this is a mask
1369 return "TODO";
1370}
1371
1372const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1373{
1374 switch (vendor)
1375 {
1376 case CEC_VENDOR_SAMSUNG:
1377 return "Samsung";
1378 case CEC_VENDOR_LG:
1379 return "LG";
1380 case CEC_VENDOR_PANASONIC:
1381 return "Panasonic";
1382 case CEC_VENDOR_PIONEER:
1383 return "Pioneer";
1384 case CEC_VENDOR_ONKYO:
1385 return "Onkyo";
1386 case CEC_VENDOR_YAMAHA:
1387 return "Yamaha";
ec0deac1
LOK
1388 case CEC_VENDOR_PHILIPS:
1389 return "Philips";
1a87cacc
LOK
1390 case CEC_VENDOR_SONY:
1391 return "Sony";
26555d6b
LOK
1392 case CEC_VENDOR_TOSHIBA:
1393 return "Toshiba";
03ae897d
LOK
1394 default:
1395 return "Unknown";
1396 }
1397}
1398
caca2d81
LOK
1399const char *CCECProcessor::ToString(const cec_client_version version)
1400{
1401 switch (version)
1402 {
1403 case CEC_CLIENT_VERSION_PRE_1_5:
1404 return "pre-1.5";
1405 case CEC_CLIENT_VERSION_1_5_0:
1406 return "1.5.0";
41e3372a
LOK
1407 case CEC_CLIENT_VERSION_1_5_1:
1408 return "1.5.1";
2e82cff0
LOK
1409 case CEC_CLIENT_VERSION_1_5_2:
1410 return "1.5.2";
e69d8f19
LOK
1411 case CEC_CLIENT_VERSION_1_5_3:
1412 return "1.5.3";
1413 case CEC_CLIENT_VERSION_1_6_0:
1414 return "1.6.0";
caca2d81
LOK
1415 default:
1416 return "Unknown";
1417 }
1418}
1419
3efda01a
LOK
1420const char *CCECProcessor::ToString(const cec_server_version version)
1421{
1422 switch (version)
1423 {
1424 case CEC_SERVER_VERSION_PRE_1_5:
1425 return "pre-1.5";
1426 case CEC_SERVER_VERSION_1_5_0:
1427 return "1.5.0";
41e3372a 1428 case CEC_SERVER_VERSION_1_5_1:
2e82cff0
LOK
1429 return "1.5.1";
1430 case CEC_SERVER_VERSION_1_5_2:
1431 return "1.5.2";
e69d8f19
LOK
1432 case CEC_SERVER_VERSION_1_5_3:
1433 return "1.5.3";
1434 case CEC_SERVER_VERSION_1_6_0:
1435 return "1.6.0";
3efda01a
LOK
1436 default:
1437 return "Unknown";
1438 }
1439}
1440
7c63a480
LOK
1441void *CCECBusScan::Process(void)
1442{
1443 CCECBusDevice *device(NULL);
fe6f8e37 1444 uint8_t iCounter(0);
5f316715 1445
5e5637c6 1446 while (!IsStopped())
7c63a480 1447 {
bdb0f7f4 1448 if (++iCounter < 10)
fe6f8e37
LOK
1449 {
1450 Sleep(1000);
1451 continue;
1452 }
5f316715 1453 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
7c63a480 1454 {
5f316715
LOK
1455 device = m_processor->m_busDevices[iPtr];
1456 WaitUntilIdle();
1457 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
5e5637c6 1458 {
5f316715
LOK
1459 WaitUntilIdle();
1460 if (!IsStopped())
1461 device->GetVendorId();
1462
1463 WaitUntilIdle();
1464 if (!IsStopped())
1465 device->GetPowerStatus(true);
5e5637c6 1466 }
7c63a480
LOK
1467 }
1468 }
5f316715 1469
7c63a480
LOK
1470 return NULL;
1471}
1113cb7d 1472
5f316715
LOK
1473void CCECBusScan::WaitUntilIdle(void)
1474{
1475 if (IsStopped())
1476 return;
1477
701f753b 1478 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
5f316715
LOK
1479 while (iWaitTime > 0)
1480 {
1481 Sleep(iWaitTime);
701f753b 1482 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
5f316715
LOK
1483 }
1484}
1485
a2198e5e 1486bool CCECProcessor::StartBootloader(const char *strPort /* = NULL */)
1113cb7d 1487{
a2198e5e
LOK
1488 if (!m_communication && strPort)
1489 {
1490 bool bReturn(false);
1491 IAdapterCommunication *comm = new CUSBCECAdapterCommunication(this, strPort);
1492 CTimeout timeout(10000);
1493 int iConnectTry(0);
3c5b4d05 1494 while (timeout.TimeLeft() > 0 && (bReturn = comm->Open(timeout.TimeLeft() / CEC_CONNECT_TRIES, true)) == false)
a2198e5e
LOK
1495 {
1496 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
1497 comm->Close();
1498 Sleep(500);
1499 }
1500 if (comm->IsOpen())
1501 {
1502 bReturn = comm->StartBootloader();
1503 delete comm;
1504 }
1505 return bReturn;
1506 }
1507 else
1508 {
1509 return m_communication->StartBootloader();
1510 }
1113cb7d
LOK
1511}
1512
1513bool CCECProcessor::PingAdapter(void)
1514{
1515 return m_communication->PingAdapter();
1516}
6729ac71
LOK
1517
1518void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1519{
a75e3a5a
LOK
1520 if (destination < CECDEVICE_BROADCAST)
1521 m_busDevices[destination]->HandlePollFrom(initiator);
6729ac71
LOK
1522}
1523
7bb4ed43 1524bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
6729ac71 1525{
0cfdeb5a 1526 return !m_busDevices[initiator]->HandleReceiveFailed();
6729ac71 1527}
f42d3e0f
LOK
1528
1529bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1530{
1531 // stream path changes are sent by the TV
1532 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1533}
d40928b5 1534
30b4aac0
LOK
1535bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1536{
30a09f32 1537 bool bReinit(false);
30b4aac0 1538 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
30a09f32 1539 cec_device_type oldPrimaryType = primary ? primary->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE;
f0154449 1540 m_configuration.clientVersion = configuration->clientVersion;
7c5c5bf4 1541 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using client version '%s'", __FUNCTION__, ToString((cec_client_version)configuration->clientVersion));
30b4aac0
LOK
1542
1543 // client version 1.5.0
1544
1545 // device types
8670c970 1546 bool bDeviceTypeChanged = IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
30b4aac0 1547 m_configuration.deviceTypes = configuration->deviceTypes;
7c5c5bf4
LOK
1548 if (bDeviceTypeChanged)
1549 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using primary device type '%s'", __FUNCTION__, ToString(configuration->deviceTypes[0]));
30b4aac0 1550
30a09f32
LOK
1551 bool bPhysicalAddressChanged(false);
1552
8670c970 1553 // autodetect address
30a09f32
LOK
1554 bool bPhysicalAutodetected(false);
1555 if (IsRunning() && configuration->bAutodetectAddress == 1)
8670c970 1556 {
30a09f32
LOK
1557 uint16_t iPhysicalAddress = m_communication->GetPhysicalAddress();
1558 if (iPhysicalAddress != 0)
1559 {
1560 if (IsRunning())
1561 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - autodetected physical address '%4x'", __FUNCTION__, iPhysicalAddress);
7c5c5bf4
LOK
1562 else
1563 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using physical address '%x'", __FUNCTION__, iPhysicalAddress);
30a09f32
LOK
1564 bPhysicalAddressChanged = (m_configuration.iPhysicalAddress != iPhysicalAddress);
1565 m_configuration.iPhysicalAddress = iPhysicalAddress;
1566 m_configuration.iHDMIPort = 0;
1567 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1568 bPhysicalAutodetected = true;
1569 }
8670c970
LOK
1570 }
1571
30b4aac0 1572 // physical address
8670c970
LOK
1573 if (!bPhysicalAutodetected)
1574 {
30a09f32
LOK
1575 if (configuration->iPhysicalAddress != 0)
1576 bPhysicalAddressChanged = IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
2270ce69
MK
1577 if (bPhysicalAddressChanged)
1578 {
7c5c5bf4 1579 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - physical address '%x'", __FUNCTION__, configuration->iPhysicalAddress);
2270ce69
MK
1580 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1581 }
8670c970 1582 }
30b4aac0 1583
8670c970
LOK
1584 bool bHdmiPortChanged(false);
1585 if (!bPhysicalAutodetected && !bPhysicalAddressChanged)
1586 {
30a09f32 1587 // base device
8670c970 1588 bHdmiPortChanged = IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
7c5c5bf4 1589 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using base device '%x'", __FUNCTION__, (int)configuration->baseDevice);
8670c970 1590 m_configuration.baseDevice = configuration->baseDevice;
30b4aac0 1591
30a09f32 1592 // hdmi port
8670c970 1593 bHdmiPortChanged |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
7c5c5bf4 1594 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using HDMI port '%d'", __FUNCTION__, configuration->iHDMIPort);
8670c970
LOK
1595 m_configuration.iHDMIPort = configuration->iHDMIPort;
1596 }
1597 else
1598 {
7c5c5bf4 1599 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - resetting HDMI port and base device to defaults", __FUNCTION__);
30a09f32
LOK
1600 m_configuration.baseDevice = CECDEVICE_UNKNOWN;
1601 m_configuration.iHDMIPort = 0;
8670c970 1602 }
30b4aac0 1603
30a09f32 1604 bReinit = bPhysicalAddressChanged || bHdmiPortChanged || bDeviceTypeChanged;
f0154449 1605
30b4aac0 1606 // device name
7c5c5bf4 1607 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - using OSD name '%s'", __FUNCTION__, configuration->strDeviceName);
30b4aac0
LOK
1608 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1609 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1610 {
1611 primary->SetOSDName(m_configuration.strDeviceName);
f0154449 1612 if (!bReinit && IsRunning())
30b4aac0
LOK
1613 primary->TransmitOSDName(CECDEVICE_TV);
1614 }
1615
1616 // tv vendor id override
7c5c5bf4 1617 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - vendor id '%s'", __FUNCTION__, ToString((cec_vendor_id)configuration->tvVendor));
30b4aac0
LOK
1618 if (m_configuration.tvVendor != configuration->tvVendor)
1619 {
1620 m_configuration.tvVendor= configuration->tvVendor;
1621 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1622 }
1623
1624 // wake CEC devices
1625 if (m_configuration.wakeDevices != configuration->wakeDevices)
1626 {
1627 m_configuration.wakeDevices = configuration->wakeDevices;
f0154449 1628 if (!bReinit && IsRunning())
ca27e6cf 1629 PowerOnDevices();
30b4aac0
LOK
1630 }
1631
1632 // just copy these
4c42105f 1633 m_configuration.bUseTVMenuLanguage = configuration->bUseTVMenuLanguage;
3efda01a 1634 m_configuration.bActivateSource = configuration->bActivateSource;
30b4aac0 1635 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
ca27e6cf 1636 m_configuration.powerOffDevices = configuration->powerOffDevices;
30b4aac0
LOK
1637 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1638 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1639
41e3372a
LOK
1640 // client version 1.5.1
1641 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1642 m_configuration.bSendInactiveSource = configuration->bSendInactiveSource;
1643
b98fc43d
LOK
1644 // client version 1.6.0
1645 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_6_0)
fa4b80df 1646 {
b98fc43d 1647 m_configuration.bPowerOffDevicesOnStandby = configuration->bPowerOffDevicesOnStandby;
fa4b80df
MK
1648 m_configuration.bShutdownOnStandby = configuration->bShutdownOnStandby;
1649 }
b98fc43d 1650
30b4aac0
LOK
1651 // ensure that there is at least 1 device type set
1652 if (m_configuration.deviceTypes.IsEmpty())
1653 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1654
7c5c5bf4
LOK
1655 // persist the configuration
1656 if (IsRunning())
1657 m_communication->PersistConfiguration(&m_configuration);
1658
f0154449 1659 if (bReinit)
30b4aac0 1660 {
44a1d92a
LOK
1661 if (bDeviceTypeChanged)
1662 return ChangeDeviceType(oldPrimaryType, m_configuration.deviceTypes[0]);
1663 else if (bPhysicalAddressChanged)
1664 return SetPhysicalAddress(m_configuration.iPhysicalAddress);
8670c970 1665 else
f0154449 1666 return SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort);
30b4aac0 1667 }
dd18e809 1668 else if (m_configuration.bActivateSource == 1 && IsRunning() && !IsActiveSource(m_configuration.logicalAddresses.primary))
c3c13157
LOK
1669 {
1670 // activate the source if we're not already the active source
1671 SetActiveSource(m_configuration.deviceTypes.types[0]);
1672 }
30b4aac0
LOK
1673
1674 return true;
1675}
1676
d40928b5
LOK
1677bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1678{
30b4aac0 1679 // client version 1.5.0
30b4aac0
LOK
1680 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1681 configuration->deviceTypes = m_configuration.deviceTypes;
8670c970 1682 configuration->bAutodetectAddress = m_configuration.bAutodetectAddress;
30b4aac0
LOK
1683 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1684 configuration->baseDevice = m_configuration.baseDevice;
1685 configuration->iHDMIPort = m_configuration.iHDMIPort;
3efda01a
LOK
1686 configuration->clientVersion = m_configuration.clientVersion;
1687 configuration->serverVersion = m_configuration.serverVersion;
30b4aac0 1688 configuration->tvVendor = m_configuration.tvVendor;
3efda01a 1689
30b4aac0 1690 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
3efda01a
LOK
1691 configuration->bUseTVMenuLanguage = m_configuration.bUseTVMenuLanguage;
1692 configuration->bActivateSource = m_configuration.bActivateSource;
1693 configuration->wakeDevices = m_configuration.wakeDevices;
ca27e6cf 1694 configuration->powerOffDevices = m_configuration.powerOffDevices;
30b4aac0
LOK
1695 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1696 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1697
41e3372a
LOK
1698 // client version 1.5.1
1699 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_1)
1700 configuration->bSendInactiveSource = m_configuration.bSendInactiveSource;
1701
dd18e809
LOK
1702 // client version 1.5.3
1703 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_5_3)
1704 configuration->logicalAddresses = m_configuration.logicalAddresses;
1705
e69d8f19 1706 // client version 1.6.0
b98fc43d
LOK
1707 if (configuration->clientVersion >= CEC_CLIENT_VERSION_1_6_0)
1708 {
1709 configuration->iFirmwareVersion = m_configuration.iFirmwareVersion;
1710 configuration->bPowerOffDevicesOnStandby = m_configuration.bPowerOffDevicesOnStandby;
fa4b80df 1711 configuration->bShutdownOnStandby = m_configuration.bShutdownOnStandby;
b98fc43d 1712 }
e69d8f19 1713
d40928b5
LOK
1714 return true;
1715}
224ea877
LOK
1716
1717bool CCECProcessor::CanPersistConfiguration(void)
1718{
1719 return m_communication->GetFirmwareVersion() >= 2;
1720}
1721
1722bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1723{
1724 return m_communication->PersistConfiguration(configuration);
1725}
3efda01a
LOK
1726
1727void CCECProcessor::RescanActiveDevices(void)
1728{
1729 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
1730 m_busDevices[iPtr]->GetStatus(true);
1731}
f80cd208
LOK
1732
1733bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs /* = 10000 */)
1734{
1735 if (!OpenConnection(strPort, 38400, iTimeoutMs, false))
1736 return false;
1737
1738 config->iFirmwareVersion = m_communication->GetFirmwareVersion();
1739 config->iPhysicalAddress = m_communication->GetPhysicalAddress();
1740
1741 delete m_communication;
f3b6afa4 1742 m_communication = NULL;
f80cd208
LOK
1743 return true;
1744}