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