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