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