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