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