cec: don't hold a lock in CCECBusDevice when calling GetStatus()
[deb_libcec.git] / src / lib / CECProcessor.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
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
33#include "CECProcessor.h"
34
35#include "adapter/USBCECAdapterCommunication.h"
36#include "devices/CECBusDevice.h"
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"
42#include "implementations/CECCommandHandler.h"
43#include "LibCEC.h"
44#include "platform/util/timeutils.h"
45
46using namespace CEC;
47using namespace std;
48using namespace PLATFORM;
49
50CCECProcessor::CCECProcessor(CLibCEC *controller, const libcec_configuration *configuration) :
51 m_bInitialised(false),
52 m_communication(NULL),
53 m_controller(controller),
54 m_bMonitor(false),
55 m_iStandardLineTimeout(3),
56 m_iRetryLineTimeout(3),
57 m_iLastTransmission(0)
58{
59 m_logicalAddresses.Clear();
60 CreateBusDevices();
61 m_configuration.Clear();
62
63 SetConfiguration(configuration);
64
65 if (m_configuration.tvVendor != CEC_VENDOR_UNKNOWN)
66 m_busDevices[CECDEVICE_TV]->ReplaceHandler(false);
67}
68
69CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress, cec_client_version clientVersion) :
70 m_bInitialised(false),
71 m_communication(NULL),
72 m_controller(controller),
73 m_bMonitor(false),
74 m_iStandardLineTimeout(3),
75 m_iRetryLineTimeout(3),
76 m_iLastTransmission(0)
77{
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);
90 m_logicalAddresses.Clear();
91 CreateBusDevices();
92}
93
94void CCECProcessor::CreateBusDevices(void)
95{
96 for (int iPtr = 0; iPtr < 16; iPtr++)
97 {
98 switch(iPtr)
99 {
100 case CECDEVICE_AUDIOSYSTEM:
101 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
102 break;
103 case CECDEVICE_PLAYBACKDEVICE1:
104 case CECDEVICE_PLAYBACKDEVICE2:
105 case CECDEVICE_PLAYBACKDEVICE3:
106 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
107 break;
108 case CECDEVICE_RECORDINGDEVICE1:
109 case CECDEVICE_RECORDINGDEVICE2:
110 case CECDEVICE_RECORDINGDEVICE3:
111 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
112 break;
113 case CECDEVICE_TUNER1:
114 case CECDEVICE_TUNER2:
115 case CECDEVICE_TUNER3:
116 case CECDEVICE_TUNER4:
117 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
118 break;
119 case CECDEVICE_TV:
120 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
121 break;
122 default:
123 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
124 break;
125 }
126 }
127}
128
129CCECProcessor::~CCECProcessor(void)
130{
131 Close();
132
133 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
134 delete m_busDevices[iPtr];
135}
136
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
150bool CCECProcessor::OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs)
151{
152 bool bReturn(false);
153 CLockObject lock(m_mutex);
154 if (m_communication)
155 {
156 CLibCEC::AddLog(CEC_LOG_WARNING, "existing connection handler found, deleting it");
157 m_communication->Close();
158 delete m_communication;
159 }
160
161 m_communication = new CUSBCECAdapterCommunication(this, strPort, iBaudRate);
162
163 /* check for an already opened connection */
164 if (m_communication->IsOpen())
165 {
166 CLibCEC::AddLog(CEC_LOG_ERROR, "connection already opened");
167 return bReturn;
168 }
169
170 uint64_t iNow = GetTimeMs();
171 uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
172
173 /* open a new connection */
174 unsigned iConnectTry(0);
175 while (iNow < iTarget && (bReturn = m_communication->Open(this, iTimeoutMs)) == false)
176 {
177 CLibCEC::AddLog(CEC_LOG_ERROR, "could not open a connection (try %d)", ++iConnectTry);
178 Sleep(500);
179 iNow = GetTimeMs();
180 }
181
182 if (bReturn)
183 CLibCEC::AddLog(CEC_LOG_NOTICE, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication->GetFirmwareVersion(), ToString(m_configuration.clientVersion));
184
185 return bReturn;
186}
187
188bool CCECProcessor::IsInitialised(void)
189{
190 CLockObject lock(m_mutex);
191 return m_bInitialised;
192}
193
194void CCECProcessor::SetInitialised(bool bSetTo /* = true */)
195{
196 CLockObject lock(m_mutex);
197 m_bInitialised = bSetTo;
198}
199
200bool CCECProcessor::Initialise(void)
201{
202 bool bReturn(false);
203 {
204 CLockObject lock(m_mutex);
205 if (!m_logicalAddresses.IsEmpty())
206 m_logicalAddresses.Clear();
207
208 if (!FindLogicalAddresses())
209 {
210 CLibCEC::AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
211 return bReturn;
212 }
213
214 /* only set our OSD name for the primary device */
215 m_busDevices[m_logicalAddresses.primary]->m_strDeviceName = m_configuration.strDeviceName;
216 }
217
218 /* get the vendor id from the TV, so we are using the correct handler */
219 m_busDevices[CECDEVICE_TV]->GetVendorId();
220
221 if (m_configuration.iPhysicalAddress != 0)
222 {
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;
225 if ((bReturn = m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress()) == false)
226 CLibCEC::AddLog(CEC_LOG_ERROR, "unable to set the physical address to %4x", m_configuration.iPhysicalAddress);
227 }
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);
230
231 PowerOnDevices();
232
233 SetInitialised(bReturn);
234 CLibCEC::ConfigurationChanged(m_configuration);
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))
246 return bReturn;
247
248 /* create the processor thread */
249 if (!CreateThread())
250 {
251 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
252 return bReturn;
253 }
254 }
255
256 if ((bReturn = Initialise()) == false)
257 {
258 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a processor thread");
259 StopThread(true);
260 }
261 else
262 {
263 CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
264 }
265
266 return bReturn;
267}
268
269bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
270{
271 if (m_busDevices[address]->TryLogicalAddress())
272 {
273 m_logicalAddresses.Set(address);
274 return true;
275 }
276
277 return false;
278}
279
280bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
281{
282 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
283 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
284 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
285 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
286}
287
288bool CCECProcessor::FindLogicalAddressTuner(void)
289{
290 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
291 return TryLogicalAddress(CECDEVICE_TUNER1) ||
292 TryLogicalAddress(CECDEVICE_TUNER2) ||
293 TryLogicalAddress(CECDEVICE_TUNER3) ||
294 TryLogicalAddress(CECDEVICE_TUNER4);
295}
296
297bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
298{
299 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
300 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
301 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
302 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
303}
304
305bool CCECProcessor::FindLogicalAddressAudioSystem(void)
306{
307 CLibCEC::AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
308 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
309}
310
311bool CCECProcessor::ChangeDeviceType(cec_device_type from, cec_device_type to)
312{
313 bool bChanged(false);
314
315 CLibCEC::AddLog(CEC_LOG_NOTICE, "changing device type '%s' into '%s'", ToString(from), ToString(to));
316
317 CLockObject lock(m_mutex);
318 CCECBusDevice *previousDevice = GetDeviceByType(from);
319 m_logicalAddresses.primary = CECDEVICE_UNKNOWN;
320
321 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
322 {
323 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
324 continue;
325
326 if (m_configuration.deviceTypes.types[iPtr] == from)
327 {
328 bChanged = true;
329 m_configuration.deviceTypes.types[iPtr] = to;
330 }
331 else if (m_configuration.deviceTypes.types[iPtr] == to && bChanged)
332 {
333 m_configuration.deviceTypes.types[iPtr] = CEC_DEVICE_TYPE_RESERVED;
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);
345 previousDevice->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT);
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
388bool CCECProcessor::FindLogicalAddresses(void)
389{
390 bool bReturn(true);
391 m_logicalAddresses.Clear();
392
393 if (m_configuration.deviceTypes.IsEmpty())
394 {
395 CLibCEC::AddLog(CEC_LOG_ERROR, "no device types set");
396 return false;
397 }
398
399 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
400 {
401 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
402 continue;
403
404 CLibCEC::AddLog(CEC_LOG_DEBUG, "%s - device %d: type %d", __FUNCTION__, iPtr, m_configuration.deviceTypes.types[iPtr]);
405
406 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
407 bReturn &= FindLogicalAddressRecordingDevice();
408 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
409 bReturn &= FindLogicalAddressTuner();
410 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
411 bReturn &= FindLogicalAddressPlaybackDevice();
412 if (m_configuration.deviceTypes.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
413 bReturn &= FindLogicalAddressAudioSystem();
414 }
415
416 if (bReturn)
417 SetAckMask(m_logicalAddresses.AckMask());
418
419 return bReturn;
420}
421
422void CCECProcessor::ReplaceHandlers(void)
423{
424 if (!IsInitialised())
425 return;
426 for (uint8_t iPtr = 0; iPtr <= CECDEVICE_PLAYBACKDEVICE3; iPtr++)
427 m_busDevices[iPtr]->ReplaceHandler(m_bInitialised);
428}
429
430bool CCECProcessor::OnCommandReceived(const cec_command &command)
431{
432 m_commandBuffer.Push(command);
433 return true;
434}
435
436void *CCECProcessor::Process(void)
437{
438 cec_command command;
439 CLibCEC::AddLog(CEC_LOG_DEBUG, "processor thread started");
440
441 while (!IsStopped() && m_communication->IsOpen())
442 {
443 if (IsInitialised())
444 {
445 ReplaceHandlers();
446 if (m_commandBuffer.Pop(command))
447 ParseCommand(command);
448
449 m_controller->CheckKeypressTimeout();
450 }
451 Sleep(5);
452 }
453
454 return NULL;
455}
456
457bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
458{
459 bool bReturn(false);
460
461 if (!IsRunning())
462 return bReturn;
463
464 cec_logical_address addr = m_logicalAddresses.primary;
465
466 if (type != CEC_DEVICE_TYPE_RESERVED)
467 {
468 for (uint8_t iPtr = 0; iPtr <= 11; iPtr++)
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
478 m_busDevices[addr]->SetActiveSource();
479 if (m_busDevices[addr]->GetPhysicalAddress(false) != 0xFFFF)
480 bReturn = m_busDevices[addr]->ActivateSource();
481
482 return bReturn;
483}
484
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
499void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
500{
501 CLockObject lock(m_mutex);
502 m_iStandardLineTimeout = iTimeout;
503}
504
505void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
506{
507 CLockObject lock(m_mutex);
508 m_iRetryLineTimeout = iTimeout;
509}
510
511bool CCECProcessor::SetActiveView(void)
512{
513 return SetActiveSource(m_configuration.deviceTypes.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_configuration.deviceTypes[0]);
514}
515
516bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
517{
518 bool bReturn(false);
519
520 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
521 if (device)
522 {
523 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
524 if (bSendUpdate)
525 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
526 bReturn = true;
527 }
528
529 return bReturn;
530}
531
532bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
533{
534 bool bReturn(false);
535
536 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
537 if (device)
538 {
539 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
540 if (bSendUpdate)
541 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
542 bReturn = true;
543 }
544
545 return bReturn;
546}
547
548bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
549{
550 bool bReturn(false);
551 CLockObject lock(m_mutex);
552
553 m_configuration.baseDevice = iBaseDevice;
554 m_configuration.iHDMIPort = iPort;
555 if (!IsRunning() && !bForce)
556 return true;
557
558 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
559
560 uint16_t iPhysicalAddress(0);
561 if (iBaseDevice > CECDEVICE_TV)
562 {
563 lock.Unlock();
564 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
565 lock.Lock();
566 }
567
568 if (iPhysicalAddress < 0xffff)
569 {
570 if (iPhysicalAddress == 0)
571 iPhysicalAddress += 0x1000 * iPort;
572 else if (iPhysicalAddress % 0x1000 == 0)
573 iPhysicalAddress += 0x100 * iPort;
574 else if (iPhysicalAddress % 0x100 == 0)
575 iPhysicalAddress += 0x10 * iPort;
576 else if (iPhysicalAddress % 0x10 == 0)
577 iPhysicalAddress += iPort;
578
579 bReturn = true;
580 }
581
582 if (!bReturn)
583 CLibCEC::AddLog(CEC_LOG_ERROR, "failed to set the physical address");
584 else
585 {
586 lock.Unlock();
587 SetPhysicalAddress(iPhysicalAddress);
588 }
589
590 return bReturn;
591}
592
593bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
594{
595 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
596 {
597 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
598 return true;
599 }
600 return false;
601}
602
603bool CCECProcessor::TransmitInactiveSource(void)
604{
605 if (!IsRunning())
606 return false;
607
608 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
609 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
610 return false;
611}
612
613void CCECProcessor::LogOutput(const cec_command &data)
614{
615 CStdString strTx;
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);
619
620 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
621 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
622 CLibCEC::AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
623}
624
625bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
626{
627 CLockObject lock(m_mutex);
628 if (m_logicalAddresses.primary != iLogicalAddress)
629 {
630 CLibCEC::AddLog(CEC_LOG_NOTICE, "<< setting primary logical address to %1x", iLogicalAddress);
631 m_logicalAddresses.primary = iLogicalAddress;
632 m_logicalAddresses.Set(iLogicalAddress);
633 return SetAckMask(m_logicalAddresses.AckMask());
634 }
635
636 return true;
637}
638
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
653bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate /* = true */)
654{
655 bool bSendActiveView(false);
656 bool bReturn(false);
657 cec_logical_addresses sendUpdatesTo;
658 sendUpdatesTo.Clear();
659
660 {
661 CLockObject lock(m_mutex);
662 m_configuration.iPhysicalAddress = iPhysicalAddress;
663
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)
674 sendUpdatesTo.Set((cec_logical_address)iPtr);
675 }
676
677 bSendActiveView = bWasActiveSource && bSendUpdate;
678 bReturn = true;
679 }
680 }
681
682 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
683 if (sendUpdatesTo[iPtr])
684 m_busDevices[iPtr]->TransmitPhysicalAddress();
685
686 if (bSendActiveView)
687 SetActiveView();
688
689 if (bReturn)
690 CLibCEC::ConfigurationChanged(m_configuration);
691
692 return bReturn;
693}
694
695bool CCECProcessor::SwitchMonitoring(bool bEnable)
696{
697 CLibCEC::AddLog(CEC_LOG_NOTICE, "== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
698
699 {
700 CLockObject lock(m_mutex);
701 m_bMonitor = bEnable;
702 }
703
704 if (bEnable)
705 return SetAckMask(0);
706 else
707 return SetAckMask(m_logicalAddresses.AckMask());
708}
709
710bool CCECProcessor::PollDevice(cec_logical_address iAddress)
711{
712 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
713 {
714 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
715 m_busDevices[iAddress]->TransmitPoll(iAddress) :
716 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
717 }
718 return false;
719}
720
721uint8_t CCECProcessor::VolumeUp(bool bSendRelease /* = true */)
722{
723 uint8_t status = 0;
724 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
725 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp(bSendRelease);
726
727 return status;
728}
729
730uint8_t CCECProcessor::VolumeDown(bool bSendRelease /* = true */)
731{
732 uint8_t status = 0;
733 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
734 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown(bSendRelease);
735
736 return status;
737}
738
739uint8_t CCECProcessor::MuteAudio(bool bSendRelease /* = true */)
740{
741 uint8_t status = 0;
742 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM))
743 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio(bSendRelease);
744
745 return status;
746}
747
748CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
749{
750 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
751 return m_busDevices[m_logicalAddresses.primary];
752
753 CCECBusDevice *device = NULL;
754 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
755 {
756 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
757 {
758 device = m_busDevices[iPtr];
759 break;
760 }
761 }
762
763 return device;
764}
765
766CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
767{
768 CCECBusDevice *device = NULL;
769
770 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
771 {
772 if (m_busDevices[iPtr]->m_type == type && m_logicalAddresses[iPtr])
773 {
774 device = m_busDevices[iPtr];
775 break;
776 }
777 }
778
779 return device;
780}
781
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
791cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
792{
793 return m_busDevices[iAddress]->GetCecVersion();
794}
795
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
807bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
808{
809 if (m_busDevices[iAddress])
810 {
811 *language = m_busDevices[iAddress]->GetMenuLanguage();
812 return (strcmp(language->language, "???") != 0);
813 }
814 return false;
815}
816
817uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
818{
819 if (m_busDevices[iAddress])
820 return m_busDevices[iAddress]->GetVendorId();
821 return false;
822}
823
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
831cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
832{
833 if (m_busDevices[iAddress])
834 return m_busDevices[iAddress]->GetPowerStatus();
835 return CEC_POWER_STATUS_UNKNOWN;
836}
837
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
854bool CCECProcessor::Transmit(const cec_command &data)
855{
856 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
857 {
858 CLockObject lock(m_mutex);
859 LogOutput(data);
860 m_iLastTransmission = GetTimeMs();
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);
865 }
866
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);
871
872 return retVal == ADAPTER_MESSAGE_STATE_SENT_ACKED;
873}
874
875void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
876{
877 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
878
879 cec_command command;
880 // TODO
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);
884
885 Transmit(command);
886}
887
888void CCECProcessor::ParseCommand(const cec_command &command)
889{
890 CStdString dataStr;
891 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
892 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
893 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
894 CLibCEC::AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
895
896 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
897 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
898}
899
900cec_logical_addresses CCECProcessor::GetActiveDevices(void)
901{
902 cec_logical_addresses addresses;
903 addresses.Clear();
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
912bool CCECProcessor::IsPresentDevice(cec_logical_address address)
913{
914 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
915}
916
917bool CCECProcessor::IsPresentDeviceType(cec_device_type type)
918{
919 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
920 {
921 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
922 return true;
923 }
924
925 return false;
926}
927
928uint16_t CCECProcessor::GetPhysicalAddress(void) const
929{
930 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
931 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
932 return false;
933}
934
935bool CCECProcessor::SetAckMask(uint16_t iMask)
936{
937 return m_communication->SetAckMask(iMask);
938}
939
940bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
941{
942 return m_busDevices[iDestination]->TransmitKeypress(key, bWait);
943}
944
945bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination, bool bWait /* = true */)
946{
947 return m_busDevices[iDestination]->TransmitKeyRelease(bWait);
948}
949
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
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
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
1295const char *CCECProcessor::ToString(const cec_audio_status UNUSED(status))
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";
1317 case CEC_VENDOR_PHILIPS:
1318 return "Philips";
1319 case CEC_VENDOR_SONY:
1320 return "Sony";
1321 default:
1322 return "Unknown";
1323 }
1324}
1325
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
1339void *CCECBusScan::Process(void)
1340{
1341 CCECBusDevice *device(NULL);
1342 uint8_t iCounter(0);
1343
1344 while (!IsStopped())
1345 {
1346 if (++iCounter < 10)
1347 {
1348 Sleep(1000);
1349 continue;
1350 }
1351 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
1352 {
1353 device = m_processor->m_busDevices[iPtr];
1354 WaitUntilIdle();
1355 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1356 {
1357 WaitUntilIdle();
1358 if (!IsStopped())
1359 device->GetVendorId();
1360
1361 WaitUntilIdle();
1362 if (!IsStopped())
1363 device->GetPowerStatus(true);
1364 }
1365 }
1366 }
1367
1368 return NULL;
1369}
1370
1371void CCECBusScan::WaitUntilIdle(void)
1372{
1373 if (IsStopped())
1374 return;
1375
1376 int32_t iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1377 while (iWaitTime > 0)
1378 {
1379 Sleep(iWaitTime);
1380 iWaitTime = 3000 - (int32_t)(GetTimeMs() - m_processor->GetLastTransmission());
1381 }
1382}
1383
1384bool CCECProcessor::StartBootloader(void)
1385{
1386 return m_communication->StartBootloader();
1387}
1388
1389bool CCECProcessor::PingAdapter(void)
1390{
1391 return m_communication->PingAdapter();
1392}
1393
1394void CCECProcessor::HandlePoll(cec_logical_address initiator, cec_logical_address destination)
1395{
1396 m_busDevices[initiator]->HandlePoll(destination);
1397}
1398
1399bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator)
1400{
1401 return !m_busDevices[initiator]->HandleReceiveFailed();
1402}
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}
1409
1410bool CCECProcessor::SetConfiguration(const libcec_configuration *configuration)
1411{
1412 bool bReinit(false);
1413 bool bPhysicalAddressChanged(false);
1414 bool bHdmiPortChanged(false);
1415 bool bDeviceTypeChanged(false);
1416 CCECBusDevice *primary = IsRunning() ? GetPrimaryDevice() : NULL;
1417 cec_device_type oldPrimaryType = primary ? primary->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE;
1418 m_configuration.clientVersion = configuration->clientVersion;
1419
1420 // client version 1.5.0
1421
1422 // device types
1423 bDeviceTypeChanged |= IsRunning () && m_configuration.deviceTypes != configuration->deviceTypes;
1424 m_configuration.deviceTypes = configuration->deviceTypes;
1425
1426 // physical address
1427 bPhysicalAddressChanged |= IsRunning() && m_configuration.iPhysicalAddress != configuration->iPhysicalAddress;
1428 m_configuration.iPhysicalAddress = configuration->iPhysicalAddress;
1429
1430 // base device
1431 bHdmiPortChanged |= IsRunning() && m_configuration.baseDevice != configuration->baseDevice;
1432 m_configuration.baseDevice = configuration->baseDevice;
1433
1434 // hdmi port
1435 bHdmiPortChanged |= IsRunning() && m_configuration.iHDMIPort != configuration->iHDMIPort;
1436 m_configuration.iHDMIPort = configuration->iHDMIPort;
1437
1438 bReinit = bPhysicalAddressChanged || bHdmiPortChanged || bDeviceTypeChanged;
1439
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);
1445 if (!bReinit && IsRunning())
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;
1460 if (!bReinit && IsRunning())
1461 PowerOnDevices();
1462 }
1463
1464 // just copy these
1465 m_configuration.bGetSettingsFromROM = configuration->bGetSettingsFromROM;
1466 m_configuration.powerOffDevices = configuration->powerOffDevices;
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
1474 if (bReinit)
1475 {
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);
1482 }
1483
1484 return true;
1485}
1486
1487bool CCECProcessor::GetCurrentConfiguration(libcec_configuration *configuration)
1488{
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;
1499 configuration->powerOffDevices = m_configuration.powerOffDevices;
1500 configuration->bPowerOffScreensaver = m_configuration.bPowerOffScreensaver;
1501 configuration->bPowerOffOnStandby = m_configuration.bPowerOffOnStandby;
1502
1503 return true;
1504}
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}