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_1
;
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_1
;
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
++)
136 delete m_busDevices
[iPtr
];
137 m_busDevices
[iPtr
] = NULL
;
141 void CCECProcessor::Close(void)
144 SetInitialised(false);
149 CLockObject
lock(m_mutex
);
150 bClose
= m_bConnectionOpened
;
151 m_bConnectionOpened
= false;
154 if (bClose
&& m_communication
)
156 delete m_communication
;
157 m_communication
= NULL
;
161 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
167 CLockObject
lock(m_mutex
);
168 if (m_bConnectionOpened
)
170 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
173 m_communication
= new CUSBCECAdapterCommunication(this, strPort
, iBaudRate
);
174 m_bConnectionOpened
= (m_communication
!= NULL
);
177 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
179 /* open a new connection */
180 unsigned iConnectTry(0);
181 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
183 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
184 m_communication
->Close();
190 m_configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
191 uint32_t iBuildDate
= m_communication
->GetFirmwareBuildDate();
192 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
));
195 time_t buildTime
= (time_t)iBuildDate
;
196 CLibCEC::AddLog(CEC_LOG_NOTICE
, "Firmware build date: %s", asctime(localtime(&buildTime
)));
201 if (m_configuration
.bGetSettingsFromROM
== 1)
203 libcec_configuration config
;
205 m_communication
->GetConfiguration(&config
);
207 CLockObject
lock(m_mutex
);
208 if (!config
.deviceTypes
.IsEmpty())
209 m_configuration
.deviceTypes
= config
.deviceTypes
;
210 if (config
.iPhysicalAddress
> 0)
211 m_configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
212 snprintf(m_configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
218 bool CCECProcessor::IsInitialised(void)
220 CLockObject
lock(m_threadMutex
);
221 return m_bInitialised
;
224 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
226 CLockObject
lock(m_mutex
);
227 m_bInitialised
= bSetTo
;
230 bool CCECProcessor::Initialise(void)
234 CLockObject
lock(m_mutex
);
235 if (!m_configuration
.logicalAddresses
.IsEmpty())
236 m_configuration
.logicalAddresses
.Clear();
238 if (!FindLogicalAddresses())
240 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
244 /* only set our OSD name for the primary device */
245 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
247 /* make the primary device the active source if the option is set */
248 if (m_configuration
.bActivateSource
== 1)
249 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
251 /* set the default menu language for devices we control */
252 cec_menu_language language
;
253 language
.device
= m_configuration
.logicalAddresses
.primary
;
254 memcpy(language
.language
, m_configuration
.strDeviceLanguage
, 3);
255 language
.language
[3] = 0;
257 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
259 if (m_configuration
.logicalAddresses
[iPtr
])
261 language
.device
= (cec_logical_address
) iPtr
;
262 m_busDevices
[iPtr
]->SetMenuLanguage(language
);
267 /* get the vendor id from the TV, so we are using the correct handler */
268 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
270 if (m_configuration
.iPhysicalAddress
!= 0)
272 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
273 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
274 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
275 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
277 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
278 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
);
280 if (bReturn
&& m_configuration
.bActivateSource
== 1)
281 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
283 SetInitialised(bReturn
);
285 CLibCEC::ConfigurationChanged(m_configuration
);
290 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
295 CLockObject
lock(m_mutex
);
296 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
299 /* create the processor thread */
302 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
307 if ((bReturn
= Initialise()) == false)
309 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
314 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
320 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
322 if (m_busDevices
[address
]->TryLogicalAddress())
324 m_configuration
.logicalAddresses
.Set(address
);
331 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
333 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
334 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
335 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
336 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
339 bool CCECProcessor::FindLogicalAddressTuner(void)
341 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
342 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
343 TryLogicalAddress(CECDEVICE_TUNER2
) ||
344 TryLogicalAddress(CECDEVICE_TUNER3
) ||
345 TryLogicalAddress(CECDEVICE_TUNER4
);
348 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
350 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
351 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
352 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
353 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
356 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
358 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
359 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
362 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
364 bool bChanged(false);
366 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
368 CLockObject
lock(m_mutex
);
369 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
370 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
372 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
374 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
377 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
380 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
382 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
384 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
390 FindLogicalAddresses();
392 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
393 if (previousDevice
&& newDevice
)
395 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
396 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
398 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
399 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
401 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
403 newDevice
->SetMenuState(previousDevice
->GetMenuState());
404 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
406 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
407 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
409 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
410 previousDevice
->SetPhysicalAddress(0xFFFF);
412 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
413 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
415 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
416 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
418 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
419 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
421 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
422 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
424 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
425 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
433 bool CCECProcessor::FindLogicalAddresses(void)
436 m_configuration
.logicalAddresses
.Clear();
438 if (m_configuration
.deviceTypes
.IsEmpty())
440 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
444 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
446 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
449 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
451 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
452 bReturn
&= FindLogicalAddressRecordingDevice();
453 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
454 bReturn
&= FindLogicalAddressTuner();
455 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
456 bReturn
&= FindLogicalAddressPlaybackDevice();
457 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
458 bReturn
&= FindLogicalAddressAudioSystem();
462 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
467 void CCECProcessor::ReplaceHandlers(void)
469 if (!IsInitialised())
471 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
472 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
475 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
477 return m_inBuffer
.Push(command
);
480 void *CCECProcessor::Process(void)
482 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
487 while (!IsStopped() && m_communication
->IsOpen())
489 if (m_inBuffer
.Pop(command
, 500))
490 ParseCommand(command
);
496 m_controller
->CheckKeypressTimeout();
503 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
510 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
512 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
514 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
516 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
518 addr
= (cec_logical_address
) iPtr
;
524 m_busDevices
[addr
]->SetActiveSource();
525 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
526 bReturn
= m_busDevices
[addr
]->ActivateSource();
531 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
535 // suppress polls when searching for a device
536 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
, false, true);
539 device
->SetActiveSource();
546 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
548 CLockObject
lock(m_mutex
);
549 m_iStandardLineTimeout
= iTimeout
;
552 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
554 CLockObject
lock(m_mutex
);
555 m_iRetryLineTimeout
= iTimeout
;
558 bool CCECProcessor::SetActiveView(void)
560 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
561 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
564 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
568 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
571 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
573 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
580 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
584 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
587 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
589 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
596 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
600 // limit the HDMI port range to 1-15
607 CLockObject
lock(m_mutex
);
608 m_configuration
.baseDevice
= iBaseDevice
;
609 m_configuration
.iHDMIPort
= iPort
;
612 if (!IsRunning() && !bForce
)
615 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
617 uint16_t iPhysicalAddress(0);
618 if (iBaseDevice
> CECDEVICE_TV
)
620 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
623 if (iPhysicalAddress
< 0xffff)
625 if (iPhysicalAddress
== 0)
626 iPhysicalAddress
+= 0x1000 * iPort
;
627 else if (iPhysicalAddress
% 0x1000 == 0)
628 iPhysicalAddress
+= 0x100 * iPort
;
629 else if (iPhysicalAddress
% 0x100 == 0)
630 iPhysicalAddress
+= 0x10 * iPort
;
631 else if (iPhysicalAddress
% 0x10 == 0)
632 iPhysicalAddress
+= iPort
;
638 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
640 SetPhysicalAddress(iPhysicalAddress
);
645 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
647 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
649 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
655 bool CCECProcessor::TransmitInactiveSource(void)
660 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
661 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
665 void CCECProcessor::LogOutput(const cec_command
&data
)
668 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
670 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
672 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
673 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
674 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
677 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
679 CLockObject
lock(m_mutex
);
680 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
682 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
683 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
684 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
685 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
691 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
693 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
695 if (m_configuration
.logicalAddresses
[iPtr
])
696 m_busDevices
[iPtr
]->SetMenuState(state
);
700 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
705 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
707 bool bSendActiveView(false);
709 cec_logical_addresses sendUpdatesTo
;
710 sendUpdatesTo
.Clear();
713 CLockObject
lock(m_mutex
);
714 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
715 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
717 if (!m_configuration
.logicalAddresses
.IsEmpty())
719 bool bWasActiveSource(false);
720 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
721 if (m_configuration
.logicalAddresses
[iPtr
])
723 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
724 m_busDevices
[iPtr
]->SetInactiveSource();
725 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
727 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
730 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
735 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
736 if (sendUpdatesTo
[iPtr
])
737 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
744 libcec_configuration config
;
746 CLockObject
lock(m_mutex
);
747 config
= m_configuration
;
750 PersistConfiguration(&config
);
751 CLibCEC::ConfigurationChanged(config
);
757 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
759 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
762 CLockObject
lock(m_mutex
);
763 m_bMonitor
= bEnable
;
767 return SetAckMask(0);
769 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
772 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
774 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
776 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
777 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
778 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
783 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
786 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
787 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
792 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
795 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
796 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
801 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
804 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
805 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
810 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */, bool bSuppressPoll
/* = false */) const
812 if (m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
813 return m_busDevices
[m_configuration
.logicalAddresses
.primary
];
815 CCECBusDevice
*device
= NULL
;
816 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
818 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
, bSuppressPoll
) == iPhysicalAddress
)
820 device
= m_busDevices
[iPtr
];
828 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
830 CCECBusDevice
*device
= NULL
;
832 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
834 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
836 device
= m_busDevices
[iPtr
];
844 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
846 CCECBusDevice
*device(NULL
);
847 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
848 if (primary
!= CECDEVICE_UNKNOWN
)
849 device
= m_busDevices
[primary
];
853 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
855 return m_busDevices
[iAddress
]->GetCecVersion();
858 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
860 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
863 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
864 retVal
.device
= iAddress
;
869 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
871 if (m_busDevices
[iAddress
])
873 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
874 return (strcmp(language
->language
, "???") != 0);
879 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
881 if (m_busDevices
[iAddress
])
882 return m_busDevices
[iAddress
]->GetVendorId();
886 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
888 if (m_busDevices
[iAddress
])
889 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
893 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
895 if (m_busDevices
[iAddress
])
896 return m_busDevices
[iAddress
]->GetPowerStatus();
897 return CEC_POWER_STATUS_UNKNOWN
;
900 cec_logical_address
CCECProcessor::GetActiveSource(void)
902 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
904 if (m_busDevices
[iPtr
]->IsActiveSource())
905 return (cec_logical_address
)iPtr
;
908 return CECDEVICE_UNKNOWN
;
911 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
913 return iAddress
> CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
?
914 m_busDevices
[iAddress
]->IsActiveSource() :
918 bool CCECProcessor::Transmit(const cec_command
&data
)
920 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
922 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
926 uint8_t iMaxTries(0);
928 CLockObject
lock(m_mutex
);
932 m_iLastTransmission
= GetTimeMs();
933 if (!m_communication
|| !m_communication
->IsOpen())
935 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
938 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
943 uint8_t iLineTimeout
= m_iStandardLineTimeout
;
944 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
946 while (bRetry
&& ++iTries
< iMaxTries
)
948 if (m_busDevices
[data
.initiator
]->IsUnsupportedFeature(data
.opcode
))
951 adapterState
= m_communication
->Write(data
, bRetry
, iLineTimeout
);
952 iLineTimeout
= m_iRetryLineTimeout
;
955 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
958 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
960 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
964 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
965 command
.parameters
.PushBack((uint8_t)opcode
);
966 command
.parameters
.PushBack((uint8_t)reason
);
971 void CCECProcessor::ParseCommand(const cec_command
&command
)
974 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
975 if (command
.opcode_set
== 1)
976 dataStr
.AppendFormat(":%02x", command
.opcode
);
977 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
978 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
979 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
981 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
982 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
985 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
987 cec_logical_addresses addresses
;
989 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
991 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
992 addresses
.Set((cec_logical_address
) iPtr
);
997 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
999 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
1002 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
1004 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1006 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1013 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1015 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
1016 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false);
1020 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1022 return m_communication
? m_communication
->SetAckMask(iMask
) : false;
1025 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1027 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1030 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1032 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1035 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1037 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
1038 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1039 if (iPhysicalAddress
!= 0)
1041 m_configuration
.bAutodetectAddress
= 1;
1042 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1043 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1044 m_configuration
.iHDMIPort
= 0;
1045 return SetPhysicalAddress(iPhysicalAddress
);
1050 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1052 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1055 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1057 if (m_configuration
.powerOffDevices
[iPtr
])
1059 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - putting '%s' in standby mode", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1060 bReturn
&= m_busDevices
[iPtr
]->Standby();
1066 return m_busDevices
[address
]->Standby();
1069 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1071 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1074 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1076 if (m_configuration
.wakeDevices
[iPtr
])
1078 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - powering on '%s'", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1079 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1085 return m_busDevices
[address
]->PowerOn();
1088 const char *CCECProcessor::ToString(const cec_device_type type
)
1092 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1093 return "audio system";
1094 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1095 return "playback device";
1096 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1097 return "recording device";
1098 case CEC_DEVICE_TYPE_RESERVED
:
1100 case CEC_DEVICE_TYPE_TUNER
:
1102 case CEC_DEVICE_TYPE_TV
:
1109 const char *CCECProcessor::ToString(const cec_menu_state state
)
1113 case CEC_MENU_STATE_ACTIVATED
:
1115 case CEC_MENU_STATE_DEACTIVATED
:
1116 return "deactivated";
1122 const char *CCECProcessor::ToString(const cec_version version
)
1126 case CEC_VERSION_1_2
:
1128 case CEC_VERSION_1_2A
:
1130 case CEC_VERSION_1_3
:
1132 case CEC_VERSION_1_3A
:
1134 case CEC_VERSION_1_4
:
1141 const char *CCECProcessor::ToString(const cec_power_status status
)
1145 case CEC_POWER_STATUS_ON
:
1147 case CEC_POWER_STATUS_STANDBY
:
1149 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1150 return "in transition from on to standby";
1151 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1152 return "in transition from standby to on";
1158 const char *CCECProcessor::ToString(const cec_logical_address address
)
1162 case CECDEVICE_AUDIOSYSTEM
:
1164 case CECDEVICE_BROADCAST
:
1166 case CECDEVICE_FREEUSE
:
1168 case CECDEVICE_PLAYBACKDEVICE1
:
1169 return "Playback 1";
1170 case CECDEVICE_PLAYBACKDEVICE2
:
1171 return "Playback 2";
1172 case CECDEVICE_PLAYBACKDEVICE3
:
1173 return "Playback 3";
1174 case CECDEVICE_RECORDINGDEVICE1
:
1175 return "Recorder 1";
1176 case CECDEVICE_RECORDINGDEVICE2
:
1177 return "Recorder 2";
1178 case CECDEVICE_RECORDINGDEVICE3
:
1179 return "Recorder 3";
1180 case CECDEVICE_RESERVED1
:
1181 return "Reserved 1";
1182 case CECDEVICE_RESERVED2
:
1183 return "Reserved 2";
1184 case CECDEVICE_TUNER1
:
1186 case CECDEVICE_TUNER2
:
1188 case CECDEVICE_TUNER3
:
1190 case CECDEVICE_TUNER4
:
1199 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1203 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1204 return "skip forward wind";
1205 case CEC_DECK_CONTROL_MODE_EJECT
:
1207 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1208 return "reverse rewind";
1209 case CEC_DECK_CONTROL_MODE_STOP
:
1216 const char *CCECProcessor::ToString(const cec_deck_info status
)
1220 case CEC_DECK_INFO_PLAY
:
1222 case CEC_DECK_INFO_RECORD
:
1224 case CEC_DECK_INFO_PLAY_REVERSE
:
1225 return "play reverse";
1226 case CEC_DECK_INFO_STILL
:
1228 case CEC_DECK_INFO_SLOW
:
1230 case CEC_DECK_INFO_SLOW_REVERSE
:
1231 return "slow reverse";
1232 case CEC_DECK_INFO_FAST_FORWARD
:
1233 return "fast forward";
1234 case CEC_DECK_INFO_FAST_REVERSE
:
1235 return "fast reverse";
1236 case CEC_DECK_INFO_NO_MEDIA
:
1238 case CEC_DECK_INFO_STOP
:
1240 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1241 return "info skip forward wind";
1242 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1243 return "info skip reverse rewind";
1244 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1245 return "info index search forward";
1246 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1247 return "info index search reverse";
1248 case CEC_DECK_INFO_OTHER_STATUS
:
1255 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1259 case CEC_OPCODE_ACTIVE_SOURCE
:
1260 return "active source";
1261 case CEC_OPCODE_IMAGE_VIEW_ON
:
1262 return "image view on";
1263 case CEC_OPCODE_TEXT_VIEW_ON
:
1264 return "text view on";
1265 case CEC_OPCODE_INACTIVE_SOURCE
:
1266 return "inactive source";
1267 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1268 return "request active source";
1269 case CEC_OPCODE_ROUTING_CHANGE
:
1270 return "routing change";
1271 case CEC_OPCODE_ROUTING_INFORMATION
:
1272 return "routing information";
1273 case CEC_OPCODE_SET_STREAM_PATH
:
1274 return "set stream path";
1275 case CEC_OPCODE_STANDBY
:
1277 case CEC_OPCODE_RECORD_OFF
:
1278 return "record off";
1279 case CEC_OPCODE_RECORD_ON
:
1281 case CEC_OPCODE_RECORD_STATUS
:
1282 return "record status";
1283 case CEC_OPCODE_RECORD_TV_SCREEN
:
1284 return "record tv screen";
1285 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1286 return "clear analogue timer";
1287 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1288 return "clear digital timer";
1289 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1290 return "clear external timer";
1291 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1292 return "set analogue timer";
1293 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1294 return "set digital timer";
1295 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1296 return "set external timer";
1297 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1298 return "set timer program title";
1299 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1300 return "timer cleared status";
1301 case CEC_OPCODE_TIMER_STATUS
:
1302 return "timer status";
1303 case CEC_OPCODE_CEC_VERSION
:
1304 return "cec version";
1305 case CEC_OPCODE_GET_CEC_VERSION
:
1306 return "get cec version";
1307 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1308 return "give physical address";
1309 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1310 return "get menu language";
1311 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1312 return "report physical address";
1313 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1314 return "set menu language";
1315 case CEC_OPCODE_DECK_CONTROL
:
1316 return "deck control";
1317 case CEC_OPCODE_DECK_STATUS
:
1318 return "deck status";
1319 case CEC_OPCODE_GIVE_DECK_STATUS
:
1320 return "give deck status";
1321 case CEC_OPCODE_PLAY
:
1323 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1324 return "give tuner status";
1325 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1326 return "select analogue service";
1327 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1328 return "set digital service";
1329 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1330 return "tuner device status";
1331 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1332 return "tuner step decrement";
1333 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1334 return "tuner step increment";
1335 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1336 return "device vendor id";
1337 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1338 return "give device vendor id";
1339 case CEC_OPCODE_VENDOR_COMMAND
:
1340 return "vendor command";
1341 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1342 return "vendor command with id";
1343 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1344 return "vendor remote button down";
1345 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1346 return "vendor remote button up";
1347 case CEC_OPCODE_SET_OSD_STRING
:
1348 return "set osd string";
1349 case CEC_OPCODE_GIVE_OSD_NAME
:
1350 return "give osd name";
1351 case CEC_OPCODE_SET_OSD_NAME
:
1352 return "set osd name";
1353 case CEC_OPCODE_MENU_REQUEST
:
1354 return "menu request";
1355 case CEC_OPCODE_MENU_STATUS
:
1356 return "menu status";
1357 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1358 return "user control pressed";
1359 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1360 return "user control release";
1361 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1362 return "give device power status";
1363 case CEC_OPCODE_REPORT_POWER_STATUS
:
1364 return "report power status";
1365 case CEC_OPCODE_FEATURE_ABORT
:
1366 return "feature abort";
1367 case CEC_OPCODE_ABORT
:
1369 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1370 return "give audio status";
1371 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1372 return "give audio mode status";
1373 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1374 return "report audio status";
1375 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1376 return "set system audio mode";
1377 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1378 return "system audio mode request";
1379 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1380 return "system audio mode status";
1381 case CEC_OPCODE_SET_AUDIO_RATE
:
1382 return "set audio rate";
1383 case CEC_OPCODE_NONE
:
1390 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1394 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1396 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1403 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1405 // TODO this is a mask
1409 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1413 case CEC_VENDOR_SAMSUNG
:
1417 case CEC_VENDOR_PANASONIC
:
1419 case CEC_VENDOR_PIONEER
:
1421 case CEC_VENDOR_ONKYO
:
1423 case CEC_VENDOR_YAMAHA
:
1425 case CEC_VENDOR_PHILIPS
:
1427 case CEC_VENDOR_SONY
:
1429 case CEC_VENDOR_TOSHIBA
:
1436 const char *CCECProcessor::ToString(const cec_client_version version
)
1440 case CEC_CLIENT_VERSION_PRE_1_5
:
1442 case CEC_CLIENT_VERSION_1_5_0
:
1444 case CEC_CLIENT_VERSION_1_5_1
:
1446 case CEC_CLIENT_VERSION_1_5_2
:
1448 case CEC_CLIENT_VERSION_1_5_3
:
1450 case CEC_CLIENT_VERSION_1_6_0
:
1452 case CEC_CLIENT_VERSION_1_6_1
:
1454 case CEC_CLIENT_VERSION_1_6_2
:
1461 const char *CCECProcessor::ToString(const cec_server_version version
)
1465 case CEC_SERVER_VERSION_PRE_1_5
:
1467 case CEC_SERVER_VERSION_1_5_0
:
1469 case CEC_SERVER_VERSION_1_5_1
:
1471 case CEC_SERVER_VERSION_1_5_2
:
1473 case CEC_SERVER_VERSION_1_5_3
:
1475 case CEC_SERVER_VERSION_1_6_0
:
1477 case CEC_SERVER_VERSION_1_6_1
:
1479 case CEC_SERVER_VERSION_1_6_2
:
1486 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1488 if (!m_communication
&& strPort
)
1490 bool bReturn(false);
1491 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1492 CTimeout
timeout(10000);
1494 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
1496 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1502 bReturn
= comm
->StartBootloader();
1509 return m_communication
->StartBootloader();
1513 bool CCECProcessor::PingAdapter(void)
1515 return m_communication
->PingAdapter();
1518 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1520 if (destination
< CECDEVICE_BROADCAST
)
1521 m_busDevices
[destination
]->HandlePollFrom(initiator
);
1524 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1526 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1529 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1531 // stream path changes are sent by the TV
1532 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1535 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1537 bool bReinit(false);
1538 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1539 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1540 m_configuration
.clientVersion
= configuration
->clientVersion
;
1541 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString((cec_client_version
)configuration
->clientVersion
));
1543 // client version 1.5.0
1546 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1547 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1548 if (bDeviceTypeChanged
)
1549 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(configuration
->deviceTypes
[0]));
1551 bool bPhysicalAddressChanged(false);
1553 // autodetect address
1554 bool bPhysicalAutodetected(false);
1555 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1557 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1558 if (iPhysicalAddress
!= 0)
1561 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1563 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%x'", __FUNCTION__
, iPhysicalAddress
);
1564 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1565 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1566 m_configuration
.iHDMIPort
= 0;
1567 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1568 bPhysicalAutodetected
= true;
1573 if (!bPhysicalAutodetected
)
1575 if (configuration
->iPhysicalAddress
!= 0)
1576 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1577 if (bPhysicalAddressChanged
)
1579 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - physical address '%x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1580 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1584 bool bHdmiPortChanged(false);
1585 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1588 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1589 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1590 m_configuration
.baseDevice
= configuration
->baseDevice
;
1593 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1594 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1595 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1599 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1600 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1601 m_configuration
.iHDMIPort
= 0;
1604 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1607 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, configuration
->strDeviceName
);
1608 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1609 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1611 primary
->SetOSDName(m_configuration
.strDeviceName
);
1612 if (!bReinit
&& IsRunning())
1613 primary
->TransmitOSDName(CECDEVICE_TV
);
1616 // tv vendor id override
1617 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString((cec_vendor_id
)configuration
->tvVendor
));
1618 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1620 m_configuration
.tvVendor
= configuration
->tvVendor
;
1621 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1625 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1627 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1628 if (!bReinit
&& IsRunning())
1633 m_configuration
.bUseTVMenuLanguage
= configuration
->bUseTVMenuLanguage
;
1634 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1635 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1636 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1637 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1638 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1640 // client version 1.5.1
1641 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1642 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1644 // client version 1.6.0
1645 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1647 m_configuration
.bPowerOffDevicesOnStandby
= configuration
->bPowerOffDevicesOnStandby
;
1648 m_configuration
.bShutdownOnStandby
= configuration
->bShutdownOnStandby
;
1651 // client version 1.6.2
1652 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1654 snprintf(m_configuration
.strDeviceLanguage
, 3, "%s", configuration
->strDeviceLanguage
);
1657 // ensure that there is at least 1 device type set
1658 if (m_configuration
.deviceTypes
.IsEmpty())
1659 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1661 // persist the configuration
1663 m_communication
->PersistConfiguration(&m_configuration
);
1665 if (bReinit
|| m_configuration
.logicalAddresses
.IsEmpty())
1667 if (bDeviceTypeChanged
)
1668 return ChangeDeviceType(oldPrimaryType
, m_configuration
.deviceTypes
[0]);
1669 else if (bPhysicalAddressChanged
)
1670 return SetPhysicalAddress(m_configuration
.iPhysicalAddress
);
1672 return SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1674 else if (m_configuration
.bActivateSource
== 1 && IsRunning() && !IsActiveSource(m_configuration
.logicalAddresses
.primary
))
1676 // activate the source if we're not already the active source
1677 SetActiveSource(m_configuration
.deviceTypes
.types
[0]);
1683 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1685 // client version 1.5.0
1686 snprintf(configuration
->strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
1687 configuration
->deviceTypes
= m_configuration
.deviceTypes
;
1688 configuration
->bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
1689 configuration
->iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
1690 configuration
->baseDevice
= m_configuration
.baseDevice
;
1691 configuration
->iHDMIPort
= m_configuration
.iHDMIPort
;
1692 configuration
->clientVersion
= m_configuration
.clientVersion
;
1693 configuration
->serverVersion
= m_configuration
.serverVersion
;
1694 configuration
->tvVendor
= m_configuration
.tvVendor
;
1696 configuration
->bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
1697 configuration
->bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
1698 configuration
->bActivateSource
= m_configuration
.bActivateSource
;
1699 configuration
->wakeDevices
= m_configuration
.wakeDevices
;
1700 configuration
->powerOffDevices
= m_configuration
.powerOffDevices
;
1701 configuration
->bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
1702 configuration
->bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
1704 // client version 1.5.1
1705 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1706 configuration
->bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
1708 // client version 1.5.3
1709 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1710 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1712 // client version 1.6.0
1713 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1715 configuration
->iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
1716 configuration
->bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
1717 configuration
->bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
1723 bool CCECProcessor::CanPersistConfiguration(void)
1725 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
1728 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1730 return m_communication
? m_communication
->PersistConfiguration(configuration
) : false;
1733 void CCECProcessor::RescanActiveDevices(void)
1735 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1736 m_busDevices
[iPtr
]->GetStatus(true);
1739 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = 10000 */)
1741 if (!OpenConnection(strPort
, 38400, iTimeoutMs
, false))
1744 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
1745 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1747 delete m_communication
;
1748 m_communication
= NULL
;
1752 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
1755 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1756 bReturn
&= m_busDevices
[iPtr
]->TransmitPendingActiveSourceCommands();