cec: don't hold a lock in CCECBusDevice when calling GetStatus()
[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
69CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress, cec_client_version clientVersion) :
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
81 m_configuration.clientVersion = clientVersion;
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 230
ca27e6cf 231 PowerOnDevices();
d068b3d4 232
2db8981f 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{
30b4aac0 513 return SetActiveSource(m_configuration.deviceTypes.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_configuration.deviceTypes[0]);
8ac9c610
LOK
514}
515
28fa6c97 516bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
a9232a79
LOK
517{
518 bool bReturn(false);
519
520 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
521 if (device)
522 {
523 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
28fa6c97
LOK
524 if (bSendUpdate)
525 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
526 bReturn = true;
527 }
528
529 return bReturn;
530}
531
28fa6c97 532bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
a9232a79
LOK
533{
534 bool bReturn(false);
535
536 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
537 if (device)
538 {
539 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
28fa6c97
LOK
540 if (bSendUpdate)
541 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
542 bReturn = true;
543 }
544
545 return bReturn;
546}
547
d2f1c157 548bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
16b1e052
LOK
549{
550 bool bReturn(false);
f00ff009 551 CLockObject lock(m_mutex);
16b1e052 552
30b4aac0
LOK
553 m_configuration.baseDevice = iBaseDevice;
554 m_configuration.iHDMIPort = iPort;
8e57f83c 555 if (!IsRunning() && !bForce)
89b3c9df 556 return true;
16b1e052 557
5477a250 558 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
56035c86 559
16b1e052 560 uint16_t iPhysicalAddress(0);
1e7afacd 561 if (iBaseDevice > CECDEVICE_TV)
5d0c1164 562 {
f00ff009 563 lock.Unlock();
1e7afacd 564 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
5d0c1164
LOK
565 lock.Lock();
566 }
1e7afacd 567
a674fd68 568 if (iPhysicalAddress < 0xffff)
1e7afacd 569 {
1e7afacd 570 if (iPhysicalAddress == 0)
5d355340 571 iPhysicalAddress += 0x1000 * iPort;
1e7afacd 572 else if (iPhysicalAddress % 0x1000 == 0)
5d355340 573 iPhysicalAddress += 0x100 * iPort;
1e7afacd 574 else if (iPhysicalAddress % 0x100 == 0)
5d355340 575 iPhysicalAddress += 0x10 * iPort;
1e7afacd 576 else if (iPhysicalAddress % 0x10 == 0)
5d355340 577 iPhysicalAddress += iPort;
1e7afacd 578
5d355340 579 bReturn = true;
16b1e052
LOK
580 }
581
a674fd68 582 if (!bReturn)
5477a250 583 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
f070564b
LOK
584 else
585 {
f00ff009 586 lock.Unlock();
f070564b
LOK
587 SetPhysicalAddress(iPhysicalAddress);
588 }
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;
4f362964 663
f070564b
LOK
664 if (!m_logicalAddresses.IsEmpty())
665 {
666 bool bWasActiveSource(false);
667 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
668 if (m_logicalAddresses[iPtr])
669 {
670 bWasActiveSource |= m_busDevices[iPtr]->IsActiveSource();
671 m_busDevices[iPtr]->SetInactiveSource();
672 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
673 if (bSendUpdate)
4d6f5ac7 674 sendUpdatesTo.Set((cec_logical_address)iPtr);
f070564b
LOK
675 }
676
677 bSendActiveView = bWasActiveSource && bSendUpdate;
678 bReturn = true;
679 }
cc60ab1c 680 }
f070564b 681
4d6f5ac7
LOK
682 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
683 if (sendUpdatesTo[iPtr])
684 m_busDevices[iPtr]->TransmitPhysicalAddress();
685
f070564b
LOK
686 if (bSendActiveView)
687 SetActiveView();
688
32403cc3
LOK
689 if (bReturn)
690 CLibCEC::ConfigurationChanged(m_configuration);
691
f070564b 692 return bReturn;
1969b140
LOK
693}
694
8b7e5ff6
LOK
695bool CCECProcessor::SwitchMonitoring(bool bEnable)
696{
5477a250 697 CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
8b7e5ff6 698
855a3a98 699 {
f00ff009 700 CLockObject lock(m_mutex);
855a3a98 701 m_bMonitor = bEnable;
855a3a98
LOK
702 }
703
8b7e5ff6 704 if (bEnable)
06bfd4d7 705 return SetAckMask(0);
8b7e5ff6 706 else
ab1469a0 707 return SetAckMask(m_logicalAddresses.AckMask());
8b7e5ff6
LOK
708}
709
57f45e6c
LOK
710bool CCECProcessor::PollDevice(cec_logical_address iAddress)
711{
712 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
95a73fa7
LOK
713 {
714 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
715 m_busDevices[iAddress]->TransmitPoll(iAddress) :
716 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
717 }
57f45e6c
LOK
718 return false;
719}
720
5c73f7f7 721uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
04e637f9
LOK
722{
723 uint8_t status = 0;
37b0c572 724 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
5c73f7f7 725 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
04e637f9
LOK
726
727 return status;
728}
729
5c73f7f7 730uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
04e637f9
LOK
731{
732 uint8_t status = 0;
37b0c572 733 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
5c73f7f7 734 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
04e637f9
LOK
735
736 return status;
737}
738
5c73f7f7 739uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
04e637f9
LOK
740{
741 uint8_t status = 0;
37b0c572 742 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
5c73f7f7 743 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
04e637f9
LOK
744
745 return status;
746}
747
16b1e052 748CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
8ac9c610 749{
16b1e052 750 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
b58d9277 751 return m_busDevices[m_logicalAddresses.primary];
8ac9c610 752
b58d9277 753 CCECBusDevice *device = NULL;
8ac9c610
LOK
754 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
755 {
16b1e052 756 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
8ac9c610
LOK
757 {
758 device = m_busDevices[iPtr];
759 break;
760 }
761 }
762
763 return device;
764}
765
a9232a79
LOK
766CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
767{
768 CCECBusDevice *device = NULL;
769
7619faad 770 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
a9232a79 771 {
b64db02e 772 if (m_busDevices[iPtr]->m_type == type && m_logicalAddresses[iPtr])
a9232a79
LOK
773 {
774 device = m_busDevices[iPtr];
775 break;
776 }
777 }
778
779 return device;
780}
781
842262d8
LOK
782CCECBusDevice *CCECProcessor::GetPrimaryDevice(void) const
783{
784 CCECBusDevice *device(NULL);
785 cec_logical_address primary = m_logicalAddresses.primary;
786 if (primary != CECDEVICE_UNKNOWN)
787 device = m_busDevices[primary];
788 return device;
789}
790
6a1c0009
LOK
791cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
792{
793 return m_busDevices[iAddress]->GetCecVersion();
794}
795
ed21be2a
LOK
796cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
797{
798 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
799 cec_osd_name retVal;
800
801 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
802 retVal.device = iAddress;
803
804 return retVal;
805}
806
a3269a0a
LOK
807bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
808{
cc60ab1c
LOK
809 if (m_busDevices[iAddress])
810 {
811 *language = m_busDevices[iAddress]->GetMenuLanguage();
5744fbba 812 return (strcmp(language->language, "???") != 0);
cc60ab1c
LOK
813 }
814 return false;
a3269a0a
LOK
815}
816
44c74256
LOK
817uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
818{
cc60ab1c
LOK
819 if (m_busDevices[iAddress])
820 return m_busDevices[iAddress]->GetVendorId();
821 return false;
44c74256
LOK
822}
823
eab72c40
LOK
824uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress)
825{
826 if (m_busDevices[iAddress])
827 return m_busDevices[iAddress]->GetPhysicalAddress(false);
828 return false;
829}
830
e55f3f70
LOK
831cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
832{
cc60ab1c
LOK
833 if (m_busDevices[iAddress])
834 return m_busDevices[iAddress]->GetPowerStatus();
835 return CEC_POWER_STATUS_UNKNOWN;
e55f3f70
LOK
836}
837
b4b1b49b
LOK
838cec_logical_address CCECProcessor::GetActiveSource(void)
839{
840 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
841 {
842 if (m_busDevices[iPtr]->IsActiveSource())
843 return (cec_logical_address)iPtr;
844 }
845
846 return CECDEVICE_UNKNOWN;
847}
848
849bool CCECProcessor::IsActiveSource(cec_logical_address iAddress)
850{
851 return m_busDevices[iAddress]->IsActiveSource();
852}
853
8d84e2c0 854bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 855{
7bb4ed43 856 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
eb35e4ca 857 {
7bb4ed43
LOK
858 CLockObject lock(m_mutex);
859 LogOutput(data);
5f316715 860 m_iLastTransmission = GetTimeMs();
7bb4ed43
LOK
861 if (!m_communication)
862 return false;
863 uint8_t iMaxTries = m_busDevices[data.initiator]->GetHandler()->GetTransmitRetries() + 1;
864 retVal = m_communication->Write(data, iMaxTries, m_iLineTimeout, m_iRetryLineTimeout);
2abe74eb
LOK
865 }
866
7bb4ed43
LOK
867 /* set to "not present" on failed ack */
868 if (retVal == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED &&
869 data.destination != CECDEVICE_BROADCAST)
870 m_busDevices[data.destination]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
9902f4e8 871
7bb4ed43 872 return retVal == ADAPTER_MESSAGE_STATE_SENT_ACKED;
825ddb96 873}
abbca718 874
22b4e74a 875void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 876{
5477a250 877 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
9dee1670 878
06a1f7ce 879 cec_command command;
f8513317 880 // TODO
ab1469a0
LOK
881 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
882 command.parameters.PushBack((uint8_t)opcode);
883 command.parameters.PushBack((uint8_t)reason);
9dee1670
LOK
884
885 Transmit(command);
abbca718
LOK
886}
887
b1f94db1 888void CCECProcessor::ParseCommand(const cec_command &command)
abbca718 889{
e9de9629 890 CStdString dataStr;
1d3ca3de 891 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
892 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
893 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
5477a250 894 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 895
7871d66e 896 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
e9de9629 897 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
898}
899
6d858ba4
LOK
900cec_logical_addresses CCECProcessor::GetActiveDevices(void)
901{
902 cec_logical_addresses addresses;
988de7b9 903 addresses.Clear();
6d858ba4
LOK
904 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
905 {
906 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
907 addresses.Set((cec_logical_address) iPtr);
908 }
909 return addresses;
910}
911
37b0c572 912bool CCECProcessor::IsPresentDevice(cec_logical_address address)
6d858ba4
LOK
913{
914 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
915}
916
37b0c572 917bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
6d858ba4 918{
6d858ba4 919 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
2813bd07
LOK
920 {
921 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
6d858ba4 922 return true;
2813bd07
LOK
923 }
924
6d858ba4
LOK
925 return false;
926}
927
0f23c85c
LOK
928uint16_t CCECProcessor::GetPhysicalAddress(void) const
929{
ab1469a0 930 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
16b1e052 931 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
cc60ab1c 932 return false;
0f23c85c
LOK
933}
934
06bfd4d7
LOK
935bool CCECProcessor::SetAckMask(uint16_t iMask)
936{
5dcf9f25 937 return m_communication->SetAckMask(iMask);
06bfd4d7 938}
a33794d8 939
4bec9d79 940bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
a33794d8 941{
86869f8f 942 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
a33794d8
LOK
943}
944
4bec9d79 945bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
a33794d8 946{
86869f8f 947 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
a33794d8 948}
7c63a480 949
ca27e6cf
LOK
950bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
951{
952 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
953 {
954 bool bReturn(true);
955 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
956 {
957 if (m_configuration.powerOffDevices[iPtr])
958 bReturn &= m_busDevices[iPtr]->Standby();
959 }
960 return bReturn;
961 }
962
963 return m_busDevices[address]->Standby();
964}
965
966bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
967{
968 if (address == CECDEVICE_BROADCAST && m_configuration.clientVersion >= CEC_CLIENT_VERSION_1_5_0)
969 {
970 bool bReturn(true);
971 for (uint8_t iPtr = 0; iPtr <= 0xF; iPtr++)
972 {
973 if (m_configuration.powerOffDevices[iPtr])
974 bReturn &= m_busDevices[iPtr]->PowerOn();
975 }
976 return bReturn;
977 }
978
979 return m_busDevices[address]->PowerOn();
980}
981
3e61b350
LOK
982const char *CCECProcessor::ToString(const cec_device_type type)
983{
984 switch (type)
985 {
986 case CEC_DEVICE_TYPE_AUDIO_SYSTEM:
987 return "audio system";
988 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE:
989 return "playback device";
990 case CEC_DEVICE_TYPE_RECORDING_DEVICE:
991 return "recording device";
992 case CEC_DEVICE_TYPE_RESERVED:
993 return "reserved";
994 case CEC_DEVICE_TYPE_TUNER:
995 return "tuner";
996 case CEC_DEVICE_TYPE_TV:
997 return "TV";
998 default:
999 return "unknown";
1000 }
1001}
1002
03ae897d
LOK
1003const char *CCECProcessor::ToString(const cec_menu_state state)
1004{
1005 switch (state)
1006 {
1007 case CEC_MENU_STATE_ACTIVATED:
1008 return "activated";
1009 case CEC_MENU_STATE_DEACTIVATED:
1010 return "deactivated";
1011 default:
1012 return "unknown";
1013 }
1014}
1015
1016const char *CCECProcessor::ToString(const cec_version version)
1017{
1018 switch (version)
1019 {
1020 case CEC_VERSION_1_2:
1021 return "1.2";
1022 case CEC_VERSION_1_2A:
1023 return "1.2a";
1024 case CEC_VERSION_1_3:
1025 return "1.3";
1026 case CEC_VERSION_1_3A:
1027 return "1.3a";
1028 case CEC_VERSION_1_4:
1029 return "1.4";
1030 default:
1031 return "unknown";
1032 }
1033}
1034
1035const char *CCECProcessor::ToString(const cec_power_status status)
1036{
1037 switch (status)
1038 {
1039 case CEC_POWER_STATUS_ON:
1040 return "on";
1041 case CEC_POWER_STATUS_STANDBY:
1042 return "standby";
1043 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1044 return "in transition from on to standby";
1045 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1046 return "in transition from standby to on";
1047 default:
1048 return "unknown";
1049 }
1050}
1051
1052const char *CCECProcessor::ToString(const cec_logical_address address)
1053{
1054 switch(address)
1055 {
1056 case CECDEVICE_AUDIOSYSTEM:
1057 return "Audio";
1058 case CECDEVICE_BROADCAST:
1059 return "Broadcast";
1060 case CECDEVICE_FREEUSE:
1061 return "Free use";
1062 case CECDEVICE_PLAYBACKDEVICE1:
1063 return "Playback 1";
1064 case CECDEVICE_PLAYBACKDEVICE2:
1065 return "Playback 2";
1066 case CECDEVICE_PLAYBACKDEVICE3:
1067 return "Playback 3";
1068 case CECDEVICE_RECORDINGDEVICE1:
1069 return "Recorder 1";
1070 case CECDEVICE_RECORDINGDEVICE2:
1071 return "Recorder 2";
1072 case CECDEVICE_RECORDINGDEVICE3:
1073 return "Recorder 3";
1074 case CECDEVICE_RESERVED1:
1075 return "Reserved 1";
1076 case CECDEVICE_RESERVED2:
1077 return "Reserved 2";
1078 case CECDEVICE_TUNER1:
1079 return "Tuner 1";
1080 case CECDEVICE_TUNER2:
1081 return "Tuner 2";
1082 case CECDEVICE_TUNER3:
1083 return "Tuner 3";
1084 case CECDEVICE_TUNER4:
1085 return "Tuner 4";
1086 case CECDEVICE_TV:
1087 return "TV";
1088 default:
1089 return "unknown";
1090 }
1091}
1092
1093const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1094{
1095 switch (mode)
1096 {
1097 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1098 return "skip forward wind";
1099 case CEC_DECK_CONTROL_MODE_EJECT:
1100 return "eject";
1101 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1102 return "reverse rewind";
1103 case CEC_DECK_CONTROL_MODE_STOP:
1104 return "stop";
1105 default:
1106 return "unknown";
1107 }
1108}
1109
1110const char *CCECProcessor::ToString(const cec_deck_info status)
1111{
1112 switch (status)
1113 {
1114 case CEC_DECK_INFO_PLAY:
1115 return "play";
1116 case CEC_DECK_INFO_RECORD:
1117 return "record";
1118 case CEC_DECK_INFO_PLAY_REVERSE:
1119 return "play reverse";
1120 case CEC_DECK_INFO_STILL:
1121 return "still";
1122 case CEC_DECK_INFO_SLOW:
1123 return "slow";
1124 case CEC_DECK_INFO_SLOW_REVERSE:
1125 return "slow reverse";
1126 case CEC_DECK_INFO_FAST_FORWARD:
1127 return "fast forward";
1128 case CEC_DECK_INFO_FAST_REVERSE:
1129 return "fast reverse";
1130 case CEC_DECK_INFO_NO_MEDIA:
1131 return "no media";
1132 case CEC_DECK_INFO_STOP:
1133 return "stop";
1134 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1135 return "info skip forward wind";
1136 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1137 return "info skip reverse rewind";
1138 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1139 return "info index search forward";
1140 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1141 return "info index search reverse";
1142 case CEC_DECK_INFO_OTHER_STATUS:
1143 return "other";
1144 default:
1145 return "unknown";
1146 }
1147}
1148
1149const char *CCECProcessor::ToString(const cec_opcode opcode)
1150{
1151 switch (opcode)
1152 {
1153 case CEC_OPCODE_ACTIVE_SOURCE:
1154 return "active source";
1155 case CEC_OPCODE_IMAGE_VIEW_ON:
1156 return "image view on";
1157 case CEC_OPCODE_TEXT_VIEW_ON:
1158 return "text view on";
1159 case CEC_OPCODE_INACTIVE_SOURCE:
1160 return "inactive source";
1161 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1162 return "request active source";
1163 case CEC_OPCODE_ROUTING_CHANGE:
1164 return "routing change";
1165 case CEC_OPCODE_ROUTING_INFORMATION:
1166 return "routing information";
1167 case CEC_OPCODE_SET_STREAM_PATH:
1168 return "set stream path";
1169 case CEC_OPCODE_STANDBY:
1170 return "standby";
1171 case CEC_OPCODE_RECORD_OFF:
1172 return "record off";
1173 case CEC_OPCODE_RECORD_ON:
1174 return "record on";
1175 case CEC_OPCODE_RECORD_STATUS:
1176 return "record status";
1177 case CEC_OPCODE_RECORD_TV_SCREEN:
1178 return "record tv screen";
1179 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1180 return "clear analogue timer";
1181 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1182 return "clear digital timer";
1183 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1184 return "clear external timer";
1185 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1186 return "set analogue timer";
1187 case CEC_OPCODE_SET_DIGITAL_TIMER:
1188 return "set digital timer";
1189 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1190 return "set external timer";
1191 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1192 return "set timer program title";
1193 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1194 return "timer cleared status";
1195 case CEC_OPCODE_TIMER_STATUS:
1196 return "timer status";
1197 case CEC_OPCODE_CEC_VERSION:
1198 return "cec version";
1199 case CEC_OPCODE_GET_CEC_VERSION:
1200 return "get cec version";
1201 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1202 return "give physical address";
1203 case CEC_OPCODE_GET_MENU_LANGUAGE:
1204 return "get menu language";
1205 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1206 return "report physical address";
1207 case CEC_OPCODE_SET_MENU_LANGUAGE:
1208 return "set menu language";
1209 case CEC_OPCODE_DECK_CONTROL:
1210 return "deck control";
1211 case CEC_OPCODE_DECK_STATUS:
1212 return "deck status";
1213 case CEC_OPCODE_GIVE_DECK_STATUS:
1214 return "give deck status";
1215 case CEC_OPCODE_PLAY:
1216 return "play";
1217 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1218 return "give tuner status";
1219 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1220 return "select analogue service";
1221 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1222 return "set digital service";
1223 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1224 return "tuner device status";
1225 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1226 return "tuner step decrement";
1227 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1228 return "tuner step increment";
1229 case CEC_OPCODE_DEVICE_VENDOR_ID:
1230 return "device vendor id";
1231 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1232 return "give device vendor id";
1233 case CEC_OPCODE_VENDOR_COMMAND:
1234 return "vendor command";
1235 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1236 return "vendor command with id";
1237 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1238 return "vendor remote button down";
1239 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1240 return "vendor remote button up";
1241 case CEC_OPCODE_SET_OSD_STRING:
1242 return "set osd string";
1243 case CEC_OPCODE_GIVE_OSD_NAME:
1244 return "give osd name";
1245 case CEC_OPCODE_SET_OSD_NAME:
1246 return "set osd name";
1247 case CEC_OPCODE_MENU_REQUEST:
1248 return "menu request";
1249 case CEC_OPCODE_MENU_STATUS:
1250 return "menu status";
1251 case CEC_OPCODE_USER_CONTROL_PRESSED:
1252 return "user control pressed";
1253 case CEC_OPCODE_USER_CONTROL_RELEASE:
1254 return "user control release";
1255 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1256 return "give device power status";
1257 case CEC_OPCODE_REPORT_POWER_STATUS:
1258 return "report power status";
1259 case CEC_OPCODE_FEATURE_ABORT:
1260 return "feature abort";
1261 case CEC_OPCODE_ABORT:
1262 return "abort";
1263 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1264 return "give audio status";
1265 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1266 return "give audio mode status";
1267 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1268 return "report audio status";
1269 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1270 return "set system audio mode";
1271 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1272 return "system audio mode request";
1273 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1274 return "system audio mode status";
1275 case CEC_OPCODE_SET_AUDIO_RATE:
1276 return "set audio rate";
1277 default:
1278 return "UNKNOWN";
1279 }
1280}
1281
1282const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1283{
1284 switch(mode)
1285 {
1286 case CEC_SYSTEM_AUDIO_STATUS_ON:
1287 return "on";
1288 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1289 return "off";
1290 default:
1291 return "unknown";
1292 }
1293}
1294
1de6617c 1295const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
03ae897d
LOK
1296{
1297 // TODO this is a mask
1298 return "TODO";
1299}
1300
1301const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1302{
1303 switch (vendor)
1304 {
1305 case CEC_VENDOR_SAMSUNG:
1306 return "Samsung";
1307 case CEC_VENDOR_LG:
1308 return "LG";
1309 case CEC_VENDOR_PANASONIC:
1310 return "Panasonic";
1311 case CEC_VENDOR_PIONEER:
1312 return "Pioneer";
1313 case CEC_VENDOR_ONKYO:
1314 return "Onkyo";
1315 case CEC_VENDOR_YAMAHA:
1316 return "Yamaha";
ec0deac1
LOK
1317 case CEC_VENDOR_PHILIPS:
1318 return "Philips";
1a87cacc
LOK
1319 case CEC_VENDOR_SONY:
1320 return "Sony";
03ae897d
LOK
1321 default:
1322 return "Unknown";
1323 }
1324}
1325
caca2d81
LOK
1326const char *CCECProcessor::ToString(const cec_client_version version)
1327{
1328 switch (version)
1329 {
1330 case CEC_CLIENT_VERSION_PRE_1_5:
1331 return "pre-1.5";
1332 case CEC_CLIENT_VERSION_1_5_0:
1333 return "1.5.0";
1334 default:
1335 return "Unknown";
1336 }
1337}
1338
7c63a480
LOK
1339void *CCECBusScan::Process(void)
1340{
1341 CCECBusDevice *device(NULL);
fe6f8e37 1342 uint8_t iCounter(0);
5f316715 1343
5e5637c6 1344 while (!IsStopped())
7c63a480 1345 {
bdb0f7f4 1346 if (++iCounter < 10)
fe6f8e37
LOK
1347 {
1348 Sleep(1000);
1349 continue;
1350 }
5f316715 1351 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
7c63a480 1352 {
5f316715
LOK
1353 device = m_processor->m_busDevices[iPtr];
1354 WaitUntilIdle();
1355 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
5e5637c6 1356 {
5f316715
LOK
1357 WaitUntilIdle();
1358 if (!IsStopped())
1359 device->GetVendorId();
1360
1361 WaitUntilIdle();
1362 if (!IsStopped())
1363 device->GetPowerStatus(true);
5e5637c6 1364 }
7c63a480
LOK
1365 }
1366 }
5f316715 1367
7c63a480
LOK
1368 return NULL;
1369}
1113cb7d 1370
5f316715
LOK
1371void CCECBusScan::WaitUntilIdle(void)
1372{
1373 if (IsStopped())
1374 return;
1375
701f753b 1376 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
5f316715
LOK
1377 while (iWaitTime > 0)
1378 {
1379 Sleep(iWaitTime);
701f753b 1380 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
5f316715
LOK
1381 }
1382}
1383
1113cb7d
LOK
1384bool CCECProcessor::StartBootloader(void)
1385{
1386 return m_communication->StartBootloader();
1387}
1388
1389bool CCECProcessor::PingAdapter(void)
1390{
1391 return m_communication->PingAdapter();
1392}
6729ac71
LOK
1393
1394void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1395{
0cfdeb5a 1396 m_busDevices[initiator]->HandlePoll(destination);
6729ac71
LOK
1397}
1398
7bb4ed43 1399bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
6729ac71 1400{
0cfdeb5a 1401 return !m_busDevices[initiator]->HandleReceiveFailed();
6729ac71 1402}
f42d3e0f
LOK
1403
1404bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress)
1405{
1406 // stream path changes are sent by the TV
1407 return m_busDevices[CECDEVICE_TV]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress);
1408}
d40928b5 1409
30b4aac0
LOK
1410bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1411{
f0154449
LOK
1412 bool bReinit(false);
1413 bool bPhysicalAddressChanged(false);
1414 bool bHdmiPortChanged(false);
1415 bool bDeviceTypeChanged(false);
30b4aac0 1416 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
f0154449
LOK
1417 cec_device_type oldPrimaryType = primary ? primary->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE;
1418 m_configuration.clientVersion = configuration->clientVersion;
30b4aac0
LOK
1419
1420 // client version 1.5.0
1421
1422 // device types
f0154449 1423 bDeviceTypeChanged |= IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
30b4aac0
LOK
1424 m_configuration.deviceTypes = configuration->deviceTypes;
1425
1426 // physical address
f0154449 1427 bPhysicalAddressChanged |= IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
30b4aac0
LOK
1428 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1429
1430 // base device
f0154449 1431 bHdmiPortChanged |= IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
30b4aac0
LOK
1432 m_configuration.baseDevice = configuration->baseDevice;
1433
1434 // hdmi port
f0154449 1435 bHdmiPortChanged |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
30b4aac0
LOK
1436 m_configuration.iHDMIPort = configuration->iHDMIPort;
1437
f0154449
LOK
1438 bReinit = bPhysicalAddressChanged || bHdmiPortChanged || bDeviceTypeChanged;
1439
30b4aac0
LOK
1440 // device name
1441 snprintf(m_configuration.strDeviceName, 13, "%s", configuration->strDeviceName);
1442 if (primary && !primary->GetOSDName().Equals(m_configuration.strDeviceName))
1443 {
1444 primary->SetOSDName(m_configuration.strDeviceName);
f0154449 1445 if (!bReinit && IsRunning())
30b4aac0
LOK
1446 primary->TransmitOSDName(CECDEVICE_TV);
1447 }
1448
1449 // tv vendor id override
1450 if (m_configuration.tvVendor != configuration->tvVendor)
1451 {
1452 m_configuration.tvVendor= configuration->tvVendor;
1453 m_busDevices[CECDEVICE_TV]->SetVendorId((uint64_t)m_configuration.tvVendor);
1454 }
1455
1456 // wake CEC devices
1457 if (m_configuration.wakeDevices != configuration->wakeDevices)
1458 {
1459 m_configuration.wakeDevices = configuration->wakeDevices;
f0154449 1460 if (!bReinit && IsRunning())
ca27e6cf 1461 PowerOnDevices();
30b4aac0
LOK
1462 }
1463
1464 // just copy these
1465 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
ca27e6cf 1466 m_configuration.powerOffDevices = configuration->powerOffDevices;
30b4aac0
LOK
1467 m_configuration.bPowerOffScreensaver = configuration->bPowerOffScreensaver;
1468 m_configuration.bPowerOffOnStandby = configuration->bPowerOffOnStandby;
1469
1470 // ensure that there is at least 1 device type set
1471 if (m_configuration.deviceTypes.IsEmpty())
1472 m_configuration.deviceTypes.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
1473
f0154449 1474 if (bReinit)
30b4aac0 1475 {
f0154449
LOK
1476 if (bDeviceTypeChanged)
1477 return ChangeDeviceType(oldPrimaryType, m_configuration.deviceTypes[0]);
1478 else if (bPhysicalAddressChanged)
1479 return SetPhysicalAddress(m_configuration.iPhysicalAddress);
1480 else
1481 return SetHDMIPort(m_configuration.baseDevice, m_configuration.iHDMIPort);
30b4aac0
LOK
1482 }
1483
1484 return true;
1485}
1486
d40928b5
LOK
1487bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1488{
30b4aac0
LOK
1489 // client version 1.5.0
1490 configuration->clientVersion = m_configuration.clientVersion;
1491 snprintf(configuration->strDeviceName, 13, "%s", m_configuration.strDeviceName);
1492 configuration->deviceTypes = m_configuration.deviceTypes;
1493 configuration->iPhysicalAddress = m_configuration.iPhysicalAddress;
1494 configuration->baseDevice = m_configuration.baseDevice;
1495 configuration->iHDMIPort = m_configuration.iHDMIPort;
1496 configuration->tvVendor = m_configuration.tvVendor;
1497 configuration->wakeDevices = m_configuration.wakeDevices;
1498 configuration->bGetSettingsFromROM = m_configuration.bGetSettingsFromROM;
ca27e6cf 1499 configuration->powerOffDevices = m_configuration.powerOffDevices;
30b4aac0
LOK
1500 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1501 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1502
d40928b5
LOK
1503 return true;
1504}
224ea877
LOK
1505
1506bool CCECProcessor::CanPersistConfiguration(void)
1507{
1508 return m_communication->GetFirmwareVersion() >= 2;
1509}
1510
1511bool CCECProcessor::PersistConfiguration(libcec_configuration *configuration)
1512{
1513 return m_communication->PersistConfiguration(configuration);
1514}