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