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