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