2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECProcessor.h"
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"
44 #include "platform/util/timeutils.h"
48 using namespace PLATFORM
;
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, libcec_configuration
*configuration
) :
51 m_bConnectionOpened(false),
52 m_bInitialised(false),
53 m_communication(NULL
),
54 m_controller(controller
),
56 m_iStandardLineTimeout(3),
57 m_iRetryLineTimeout(3),
58 m_iLastTransmission(0)
61 m_configuration
.Clear();
62 m_configuration
.serverVersion
= CEC_SERVER_VERSION_1_6_0
;
63 SetConfiguration(configuration
);
65 if (m_configuration
.tvVendor
!= CEC_VENDOR_UNKNOWN
)
66 m_busDevices
[CECDEVICE_TV
]->ReplaceHandler(false);
69 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
, uint16_t iPhysicalAddress
) :
70 m_bConnectionOpened(false),
71 m_bInitialised(false),
72 m_communication(NULL
),
73 m_controller(controller
),
75 m_iStandardLineTimeout(3),
76 m_iRetryLineTimeout(3),
77 m_iLastTransmission(0)
79 m_configuration
.Clear();
80 m_configuration
.serverVersion
= CEC_SERVER_VERSION_1_6_0
;
82 // client version < 1.5.0
83 m_configuration
.clientVersion
= (uint32_t)CEC_CLIENT_VERSION_PRE_1_5
;
84 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
);
85 m_configuration
.deviceTypes
= types
;
86 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
87 m_configuration
.baseDevice
= (cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
;
88 m_configuration
.iHDMIPort
= CEC_DEFAULT_HDMI_PORT
;
90 if (m_configuration
.deviceTypes
.IsEmpty())
91 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
95 void CCECProcessor::CreateBusDevices(void)
97 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
101 case CECDEVICE_AUDIOSYSTEM
:
102 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
104 case CECDEVICE_PLAYBACKDEVICE1
:
105 case CECDEVICE_PLAYBACKDEVICE2
:
106 case CECDEVICE_PLAYBACKDEVICE3
:
107 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
109 case CECDEVICE_RECORDINGDEVICE1
:
110 case CECDEVICE_RECORDINGDEVICE2
:
111 case CECDEVICE_RECORDINGDEVICE3
:
112 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
114 case CECDEVICE_TUNER1
:
115 case CECDEVICE_TUNER2
:
116 case CECDEVICE_TUNER3
:
117 case CECDEVICE_TUNER4
:
118 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
121 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
124 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
130 CCECProcessor::~CCECProcessor(void)
134 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
135 delete m_busDevices
[iPtr
];
138 void CCECProcessor::Close(void)
141 SetInitialised(false);
146 CLockObject
lock(m_mutex
);
147 bClose
= m_bConnectionOpened
;
148 m_bConnectionOpened
= false;
151 if (bClose
&& m_communication
)
153 m_communication
->PersistConfiguration(&m_configuration
);
154 m_communication
->Close();
155 delete m_communication
;
156 m_communication
= NULL
;
160 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
166 CLockObject
lock(m_mutex
);
167 if (m_bConnectionOpened
)
169 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
172 m_communication
= new CUSBCECAdapterCommunication(this, strPort
, iBaudRate
);
173 m_bConnectionOpened
= (m_communication
!= NULL
);
176 /* check for an already opened connection */
177 if (m_communication
->IsOpen())
179 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
183 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
185 /* open a new connection */
186 unsigned iConnectTry(0);
187 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open(this, (timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
189 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
190 m_communication
->Close();
196 m_configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
197 CLibCEC::AddLog(CEC_LOG_NOTICE
, "connected to the CEC adapter. firmware version = %d, client version = %s", m_configuration
.iFirmwareVersion
, ToString((cec_client_version
)m_configuration
.clientVersion
));
200 if (m_configuration
.bGetSettingsFromROM
== 1)
201 m_communication
->GetConfiguration(&m_configuration
);
206 bool CCECProcessor::IsInitialised(void)
208 CLockObject
lock(m_mutex
);
209 return m_bInitialised
;
212 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
214 CLockObject
lock(m_mutex
);
215 m_bInitialised
= bSetTo
;
218 bool CCECProcessor::Initialise(void)
222 CLockObject
lock(m_mutex
);
223 if (!m_configuration
.logicalAddresses
.IsEmpty())
224 m_configuration
.logicalAddresses
.Clear();
226 if (!FindLogicalAddresses())
228 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
232 /* only set our OSD name for the primary device */
233 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
235 /* make the primary device the active source if the option is set */
236 if (m_configuration
.bActivateSource
== 1)
237 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
240 /* get the vendor id from the TV, so we are using the correct handler */
241 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
243 if (m_configuration
.iPhysicalAddress
!= 0)
245 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
246 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
247 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
248 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
250 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
251 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
);
253 if (m_configuration
.bActivateSource
== 1)
254 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
256 SetInitialised(bReturn
);
258 CLibCEC::ConfigurationChanged(m_configuration
);
263 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
268 CLockObject
lock(m_mutex
);
269 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
272 /* create the processor thread */
275 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
280 if ((bReturn
= Initialise()) == false)
282 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
287 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
293 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
295 if (m_busDevices
[address
]->TryLogicalAddress())
297 m_configuration
.logicalAddresses
.Set(address
);
304 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
306 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
307 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
308 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
309 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
312 bool CCECProcessor::FindLogicalAddressTuner(void)
314 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
315 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
316 TryLogicalAddress(CECDEVICE_TUNER2
) ||
317 TryLogicalAddress(CECDEVICE_TUNER3
) ||
318 TryLogicalAddress(CECDEVICE_TUNER4
);
321 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
323 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
324 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
325 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
326 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
329 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
331 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
332 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
335 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
337 bool bChanged(false);
339 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
341 CLockObject
lock(m_mutex
);
342 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
343 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
345 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
347 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
350 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
353 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
355 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
357 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
363 FindLogicalAddresses();
365 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
366 if (previousDevice
&& newDevice
)
368 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
369 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
371 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
372 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
374 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
375 cec_menu_language lang
;
376 lang
.device
= previousDevice
->GetLogicalAddress();
377 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
378 lang
.language
[iPtr
] = '?';
379 lang
.language
[3] = 0;
380 previousDevice
->SetMenuLanguage(lang
);
382 newDevice
->SetMenuState(previousDevice
->GetMenuState());
383 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
385 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
386 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
388 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
389 previousDevice
->SetPhysicalAddress(0xFFFF);
391 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
392 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
394 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
395 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
397 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
398 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
400 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
401 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
403 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
404 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
412 bool CCECProcessor::FindLogicalAddresses(void)
415 m_configuration
.logicalAddresses
.Clear();
417 if (m_configuration
.deviceTypes
.IsEmpty())
419 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
423 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
425 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
428 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
430 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
431 bReturn
&= FindLogicalAddressRecordingDevice();
432 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
433 bReturn
&= FindLogicalAddressTuner();
434 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
435 bReturn
&= FindLogicalAddressPlaybackDevice();
436 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
437 bReturn
&= FindLogicalAddressAudioSystem();
441 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
446 void CCECProcessor::ReplaceHandlers(void)
448 if (!IsInitialised())
450 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
451 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
454 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
456 ParseCommand(command
);
460 void *CCECProcessor::Process(void)
462 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
464 while (!IsStopped() && m_communication
->IsOpen())
470 m_controller
->CheckKeypressTimeout();
478 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
485 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
487 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
489 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
491 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
493 addr
= (cec_logical_address
) iPtr
;
499 m_busDevices
[addr
]->SetActiveSource();
500 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
501 bReturn
= m_busDevices
[addr
]->ActivateSource();
506 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
510 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
513 device
->SetActiveSource();
520 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
522 CLockObject
lock(m_mutex
);
523 m_iStandardLineTimeout
= iTimeout
;
526 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
528 CLockObject
lock(m_mutex
);
529 m_iRetryLineTimeout
= iTimeout
;
532 bool CCECProcessor::SetActiveView(void)
534 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
535 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
538 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
542 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
545 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
547 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
554 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
558 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
561 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
563 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
570 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
575 CLockObject
lock(m_mutex
);
576 m_configuration
.baseDevice
= iBaseDevice
;
577 m_configuration
.iHDMIPort
= iPort
;
580 if (!IsRunning() && !bForce
)
583 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
585 uint16_t iPhysicalAddress(0);
586 if (iBaseDevice
> CECDEVICE_TV
)
588 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
591 if (iPhysicalAddress
< 0xffff)
593 if (iPhysicalAddress
== 0)
594 iPhysicalAddress
+= 0x1000 * iPort
;
595 else if (iPhysicalAddress
% 0x1000 == 0)
596 iPhysicalAddress
+= 0x100 * iPort
;
597 else if (iPhysicalAddress
% 0x100 == 0)
598 iPhysicalAddress
+= 0x10 * iPort
;
599 else if (iPhysicalAddress
% 0x10 == 0)
600 iPhysicalAddress
+= iPort
;
606 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
608 SetPhysicalAddress(iPhysicalAddress
);
613 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
615 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
617 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
623 bool CCECProcessor::TransmitInactiveSource(void)
628 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
629 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
633 void CCECProcessor::LogOutput(const cec_command
&data
)
636 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
638 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
640 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
641 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
642 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
645 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
647 CLockObject
lock(m_mutex
);
648 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
650 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
651 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
652 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
653 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
659 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
661 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
663 if (m_configuration
.logicalAddresses
[iPtr
])
664 m_busDevices
[iPtr
]->SetMenuState(state
);
668 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
673 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
675 bool bSendActiveView(false);
677 cec_logical_addresses sendUpdatesTo
;
678 sendUpdatesTo
.Clear();
681 CLockObject
lock(m_mutex
);
682 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
683 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
685 if (!m_configuration
.logicalAddresses
.IsEmpty())
687 bool bWasActiveSource(false);
688 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
689 if (m_configuration
.logicalAddresses
[iPtr
])
691 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
692 m_busDevices
[iPtr
]->SetInactiveSource();
693 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
695 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
698 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
703 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
704 if (sendUpdatesTo
[iPtr
])
705 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
711 CLibCEC::ConfigurationChanged(m_configuration
);
716 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
718 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
721 CLockObject
lock(m_mutex
);
722 m_bMonitor
= bEnable
;
726 return SetAckMask(0);
728 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
731 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
733 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
735 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
736 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
737 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
742 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
745 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
746 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
751 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
754 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
755 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
760 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
763 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
764 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
769 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
771 if (m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
772 return m_busDevices
[m_configuration
.logicalAddresses
.primary
];
774 CCECBusDevice
*device
= NULL
;
775 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
777 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
779 device
= m_busDevices
[iPtr
];
787 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
789 CCECBusDevice
*device
= NULL
;
791 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
793 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
795 device
= m_busDevices
[iPtr
];
803 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
805 CCECBusDevice
*device(NULL
);
806 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
807 if (primary
!= CECDEVICE_UNKNOWN
)
808 device
= m_busDevices
[primary
];
812 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
814 return m_busDevices
[iAddress
]->GetCecVersion();
817 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
819 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
822 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
823 retVal
.device
= iAddress
;
828 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
830 if (m_busDevices
[iAddress
])
832 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
833 return (strcmp(language
->language
, "???") != 0);
838 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
840 if (m_busDevices
[iAddress
])
841 return m_busDevices
[iAddress
]->GetVendorId();
845 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
847 if (m_busDevices
[iAddress
])
848 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
852 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
854 if (m_busDevices
[iAddress
])
855 return m_busDevices
[iAddress
]->GetPowerStatus();
856 return CEC_POWER_STATUS_UNKNOWN
;
859 cec_logical_address
CCECProcessor::GetActiveSource(void)
861 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
863 if (m_busDevices
[iPtr
]->IsActiveSource())
864 return (cec_logical_address
)iPtr
;
867 return CECDEVICE_UNKNOWN
;
870 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
872 return m_busDevices
[iAddress
]->IsActiveSource();
875 bool CCECProcessor::Transmit(const cec_command
&data
)
877 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
879 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
883 uint8_t iMaxTries(0);
885 CLockObject
lock(m_mutex
);
889 m_iLastTransmission
= GetTimeMs();
890 if (!m_communication
|| !m_communication
->IsOpen())
892 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
895 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
898 return m_communication
->Write(data
, iMaxTries
, m_iStandardLineTimeout
, m_iRetryLineTimeout
)
899 == ADAPTER_MESSAGE_STATE_SENT_ACKED
;
902 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
904 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
908 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
909 command
.parameters
.PushBack((uint8_t)opcode
);
910 command
.parameters
.PushBack((uint8_t)reason
);
915 void CCECProcessor::ParseCommand(const cec_command
&command
)
918 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
919 if (command
.opcode_set
== 1)
920 dataStr
.AppendFormat(":%02x", command
.opcode
);
921 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
922 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
923 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
925 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
926 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
929 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
931 cec_logical_addresses addresses
;
933 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
935 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
936 addresses
.Set((cec_logical_address
) iPtr
);
941 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
943 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
946 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
948 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
950 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
957 uint16_t CCECProcessor::GetPhysicalAddress(void) const
959 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
960 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false);
964 bool CCECProcessor::SetAckMask(uint16_t iMask
)
966 return m_communication
->SetAckMask(iMask
);
969 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
971 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
974 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
976 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
979 bool CCECProcessor::EnablePhysicalAddressDetection(void)
981 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
982 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
983 if (iPhysicalAddress
!= 0)
985 m_configuration
.bAutodetectAddress
= 1;
986 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
987 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
988 m_configuration
.iHDMIPort
= 0;
989 return SetPhysicalAddress(iPhysicalAddress
);
994 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
996 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
999 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1001 if (m_configuration
.powerOffDevices
[iPtr
])
1002 bReturn
&= m_busDevices
[iPtr
]->Standby();
1007 return m_busDevices
[address
]->Standby();
1010 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1012 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1015 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1017 if (m_configuration
.powerOffDevices
[iPtr
])
1018 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1023 return m_busDevices
[address
]->PowerOn();
1026 const char *CCECProcessor::ToString(const cec_device_type type
)
1030 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1031 return "audio system";
1032 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1033 return "playback device";
1034 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1035 return "recording device";
1036 case CEC_DEVICE_TYPE_RESERVED
:
1038 case CEC_DEVICE_TYPE_TUNER
:
1040 case CEC_DEVICE_TYPE_TV
:
1047 const char *CCECProcessor::ToString(const cec_menu_state state
)
1051 case CEC_MENU_STATE_ACTIVATED
:
1053 case CEC_MENU_STATE_DEACTIVATED
:
1054 return "deactivated";
1060 const char *CCECProcessor::ToString(const cec_version version
)
1064 case CEC_VERSION_1_2
:
1066 case CEC_VERSION_1_2A
:
1068 case CEC_VERSION_1_3
:
1070 case CEC_VERSION_1_3A
:
1072 case CEC_VERSION_1_4
:
1079 const char *CCECProcessor::ToString(const cec_power_status status
)
1083 case CEC_POWER_STATUS_ON
:
1085 case CEC_POWER_STATUS_STANDBY
:
1087 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1088 return "in transition from on to standby";
1089 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1090 return "in transition from standby to on";
1096 const char *CCECProcessor::ToString(const cec_logical_address address
)
1100 case CECDEVICE_AUDIOSYSTEM
:
1102 case CECDEVICE_BROADCAST
:
1104 case CECDEVICE_FREEUSE
:
1106 case CECDEVICE_PLAYBACKDEVICE1
:
1107 return "Playback 1";
1108 case CECDEVICE_PLAYBACKDEVICE2
:
1109 return "Playback 2";
1110 case CECDEVICE_PLAYBACKDEVICE3
:
1111 return "Playback 3";
1112 case CECDEVICE_RECORDINGDEVICE1
:
1113 return "Recorder 1";
1114 case CECDEVICE_RECORDINGDEVICE2
:
1115 return "Recorder 2";
1116 case CECDEVICE_RECORDINGDEVICE3
:
1117 return "Recorder 3";
1118 case CECDEVICE_RESERVED1
:
1119 return "Reserved 1";
1120 case CECDEVICE_RESERVED2
:
1121 return "Reserved 2";
1122 case CECDEVICE_TUNER1
:
1124 case CECDEVICE_TUNER2
:
1126 case CECDEVICE_TUNER3
:
1128 case CECDEVICE_TUNER4
:
1137 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1141 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1142 return "skip forward wind";
1143 case CEC_DECK_CONTROL_MODE_EJECT
:
1145 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1146 return "reverse rewind";
1147 case CEC_DECK_CONTROL_MODE_STOP
:
1154 const char *CCECProcessor::ToString(const cec_deck_info status
)
1158 case CEC_DECK_INFO_PLAY
:
1160 case CEC_DECK_INFO_RECORD
:
1162 case CEC_DECK_INFO_PLAY_REVERSE
:
1163 return "play reverse";
1164 case CEC_DECK_INFO_STILL
:
1166 case CEC_DECK_INFO_SLOW
:
1168 case CEC_DECK_INFO_SLOW_REVERSE
:
1169 return "slow reverse";
1170 case CEC_DECK_INFO_FAST_FORWARD
:
1171 return "fast forward";
1172 case CEC_DECK_INFO_FAST_REVERSE
:
1173 return "fast reverse";
1174 case CEC_DECK_INFO_NO_MEDIA
:
1176 case CEC_DECK_INFO_STOP
:
1178 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1179 return "info skip forward wind";
1180 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1181 return "info skip reverse rewind";
1182 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1183 return "info index search forward";
1184 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1185 return "info index search reverse";
1186 case CEC_DECK_INFO_OTHER_STATUS
:
1193 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1197 case CEC_OPCODE_ACTIVE_SOURCE
:
1198 return "active source";
1199 case CEC_OPCODE_IMAGE_VIEW_ON
:
1200 return "image view on";
1201 case CEC_OPCODE_TEXT_VIEW_ON
:
1202 return "text view on";
1203 case CEC_OPCODE_INACTIVE_SOURCE
:
1204 return "inactive source";
1205 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1206 return "request active source";
1207 case CEC_OPCODE_ROUTING_CHANGE
:
1208 return "routing change";
1209 case CEC_OPCODE_ROUTING_INFORMATION
:
1210 return "routing information";
1211 case CEC_OPCODE_SET_STREAM_PATH
:
1212 return "set stream path";
1213 case CEC_OPCODE_STANDBY
:
1215 case CEC_OPCODE_RECORD_OFF
:
1216 return "record off";
1217 case CEC_OPCODE_RECORD_ON
:
1219 case CEC_OPCODE_RECORD_STATUS
:
1220 return "record status";
1221 case CEC_OPCODE_RECORD_TV_SCREEN
:
1222 return "record tv screen";
1223 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1224 return "clear analogue timer";
1225 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1226 return "clear digital timer";
1227 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1228 return "clear external timer";
1229 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1230 return "set analogue timer";
1231 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1232 return "set digital timer";
1233 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1234 return "set external timer";
1235 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1236 return "set timer program title";
1237 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1238 return "timer cleared status";
1239 case CEC_OPCODE_TIMER_STATUS
:
1240 return "timer status";
1241 case CEC_OPCODE_CEC_VERSION
:
1242 return "cec version";
1243 case CEC_OPCODE_GET_CEC_VERSION
:
1244 return "get cec version";
1245 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1246 return "give physical address";
1247 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1248 return "get menu language";
1249 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1250 return "report physical address";
1251 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1252 return "set menu language";
1253 case CEC_OPCODE_DECK_CONTROL
:
1254 return "deck control";
1255 case CEC_OPCODE_DECK_STATUS
:
1256 return "deck status";
1257 case CEC_OPCODE_GIVE_DECK_STATUS
:
1258 return "give deck status";
1259 case CEC_OPCODE_PLAY
:
1261 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1262 return "give tuner status";
1263 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1264 return "select analogue service";
1265 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1266 return "set digital service";
1267 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1268 return "tuner device status";
1269 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1270 return "tuner step decrement";
1271 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1272 return "tuner step increment";
1273 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1274 return "device vendor id";
1275 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1276 return "give device vendor id";
1277 case CEC_OPCODE_VENDOR_COMMAND
:
1278 return "vendor command";
1279 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1280 return "vendor command with id";
1281 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1282 return "vendor remote button down";
1283 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1284 return "vendor remote button up";
1285 case CEC_OPCODE_SET_OSD_STRING
:
1286 return "set osd string";
1287 case CEC_OPCODE_GIVE_OSD_NAME
:
1288 return "give osd name";
1289 case CEC_OPCODE_SET_OSD_NAME
:
1290 return "set osd name";
1291 case CEC_OPCODE_MENU_REQUEST
:
1292 return "menu request";
1293 case CEC_OPCODE_MENU_STATUS
:
1294 return "menu status";
1295 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1296 return "user control pressed";
1297 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1298 return "user control release";
1299 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1300 return "give device power status";
1301 case CEC_OPCODE_REPORT_POWER_STATUS
:
1302 return "report power status";
1303 case CEC_OPCODE_FEATURE_ABORT
:
1304 return "feature abort";
1305 case CEC_OPCODE_ABORT
:
1307 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1308 return "give audio status";
1309 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1310 return "give audio mode status";
1311 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1312 return "report audio status";
1313 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1314 return "set system audio mode";
1315 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1316 return "system audio mode request";
1317 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1318 return "system audio mode status";
1319 case CEC_OPCODE_SET_AUDIO_RATE
:
1320 return "set audio rate";
1321 case CEC_OPCODE_NONE
:
1328 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1332 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1334 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1341 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1343 // TODO this is a mask
1347 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1351 case CEC_VENDOR_SAMSUNG
:
1355 case CEC_VENDOR_PANASONIC
:
1357 case CEC_VENDOR_PIONEER
:
1359 case CEC_VENDOR_ONKYO
:
1361 case CEC_VENDOR_YAMAHA
:
1363 case CEC_VENDOR_PHILIPS
:
1365 case CEC_VENDOR_SONY
:
1367 case CEC_VENDOR_TOSHIBA
:
1374 const char *CCECProcessor::ToString(const cec_client_version version
)
1378 case CEC_CLIENT_VERSION_PRE_1_5
:
1380 case CEC_CLIENT_VERSION_1_5_0
:
1382 case CEC_CLIENT_VERSION_1_5_1
:
1384 case CEC_CLIENT_VERSION_1_5_2
:
1386 case CEC_CLIENT_VERSION_1_5_3
:
1388 case CEC_CLIENT_VERSION_1_6_0
:
1395 const char *CCECProcessor::ToString(const cec_server_version version
)
1399 case CEC_SERVER_VERSION_PRE_1_5
:
1401 case CEC_SERVER_VERSION_1_5_0
:
1403 case CEC_SERVER_VERSION_1_5_1
:
1405 case CEC_SERVER_VERSION_1_5_2
:
1407 case CEC_SERVER_VERSION_1_5_3
:
1409 case CEC_SERVER_VERSION_1_6_0
:
1416 void *CCECBusScan::Process(void)
1418 CCECBusDevice
*device(NULL
);
1419 uint8_t iCounter(0);
1421 while (!IsStopped())
1423 if (++iCounter
< 10)
1428 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1430 device
= m_processor
->m_busDevices
[iPtr
];
1432 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1436 device
->GetVendorId();
1440 device
->GetPowerStatus(true);
1448 void CCECBusScan::WaitUntilIdle(void)
1453 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1454 while (iWaitTime
> 0)
1457 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1461 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1463 if (!m_communication
&& strPort
)
1465 bool bReturn(false);
1466 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1467 CTimeout
timeout(10000);
1469 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(NULL
, (timeout
.TimeLeft() / CEC_CONNECT_TRIES
)), true) == false)
1471 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1477 bReturn
= comm
->StartBootloader();
1484 return m_communication
->StartBootloader();
1488 bool CCECProcessor::PingAdapter(void)
1490 return m_communication
->PingAdapter();
1493 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1495 m_busDevices
[destination
]->HandlePoll(initiator
);
1498 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1500 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1503 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1505 // stream path changes are sent by the TV
1506 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1509 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1511 bool bReinit(false);
1512 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1513 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1514 m_configuration
.clientVersion
= configuration
->clientVersion
;
1516 // client version 1.5.0
1519 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1520 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1522 bool bPhysicalAddressChanged(false);
1524 // autodetect address
1525 bool bPhysicalAutodetected(false);
1526 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1528 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1529 if (iPhysicalAddress
!= 0)
1532 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1533 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1534 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1535 m_configuration
.iHDMIPort
= 0;
1536 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1537 bPhysicalAutodetected
= true;
1542 if (!bPhysicalAutodetected
)
1544 if (configuration
->iPhysicalAddress
!= 0)
1545 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1547 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%4x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1548 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1551 bool bHdmiPortChanged(false);
1552 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1555 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1557 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1558 m_configuration
.baseDevice
= configuration
->baseDevice
;
1561 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1563 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1564 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1569 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1570 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1571 m_configuration
.iHDMIPort
= 0;
1574 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1577 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1578 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1580 primary
->SetOSDName(m_configuration
.strDeviceName
);
1581 if (!bReinit
&& IsRunning())
1582 primary
->TransmitOSDName(CECDEVICE_TV
);
1585 // tv vendor id override
1586 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1588 m_configuration
.tvVendor
= configuration
->tvVendor
;
1589 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1593 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1595 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1596 if (!bReinit
&& IsRunning())
1601 m_configuration
.clientVersion
= configuration
->clientVersion
;
1602 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1603 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1604 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1605 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1606 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1608 // client version 1.5.1
1609 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1610 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1612 // ensure that there is at least 1 device type set
1613 if (m_configuration
.deviceTypes
.IsEmpty())
1614 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1618 if (bDeviceTypeChanged
)
1619 return ChangeDeviceType(oldPrimaryType
, m_configuration
.deviceTypes
[0]);
1620 else if (bPhysicalAddressChanged
)
1621 return SetPhysicalAddress(m_configuration
.iPhysicalAddress
);
1623 return SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1625 else if (m_configuration
.bActivateSource
== 1 && IsRunning() && !IsActiveSource(m_configuration
.logicalAddresses
.primary
))
1627 // activate the source if we're not already the active source
1628 SetActiveSource(m_configuration
.deviceTypes
.types
[0]);
1634 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1636 // client version 1.5.0
1637 snprintf(configuration
->strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
1638 configuration
->deviceTypes
= m_configuration
.deviceTypes
;
1639 configuration
->bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
1640 configuration
->iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
1641 configuration
->baseDevice
= m_configuration
.baseDevice
;
1642 configuration
->iHDMIPort
= m_configuration
.iHDMIPort
;
1643 configuration
->clientVersion
= m_configuration
.clientVersion
;
1644 configuration
->serverVersion
= m_configuration
.serverVersion
;
1645 configuration
->tvVendor
= m_configuration
.tvVendor
;
1647 configuration
->bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
1648 configuration
->bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
1649 configuration
->bActivateSource
= m_configuration
.bActivateSource
;
1650 configuration
->wakeDevices
= m_configuration
.wakeDevices
;
1651 configuration
->powerOffDevices
= m_configuration
.powerOffDevices
;
1652 configuration
->bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
1653 configuration
->bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
1655 // client version 1.5.1
1656 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1657 configuration
->bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
1659 // client version 1.5.3
1660 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1661 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1663 // client version 1.6.0
1664 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1665 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1670 bool CCECProcessor::CanPersistConfiguration(void)
1672 return m_communication
->GetFirmwareVersion() >= 2;
1675 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1677 return m_communication
->PersistConfiguration(configuration
);
1680 void CCECProcessor::RescanActiveDevices(void)
1682 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
1683 m_busDevices
[iPtr
]->GetStatus(true);
1686 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = 10000 */)
1688 if (!OpenConnection(strPort
, 38400, iTimeoutMs
, false))
1691 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
1692 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1694 delete m_communication
;
1695 m_communication
= NULL
;