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