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