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