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