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