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_2
;
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_2
;
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 m_configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
193 strLog
.Format("connected to the CEC adapter. libCEC version = %s, client version = %s, firmware version = %d", ToString((cec_server_version
)m_configuration
.serverVersion
), ToString((cec_client_version
)m_configuration
.clientVersion
), m_configuration
.iFirmwareVersion
);
194 if (m_configuration
.iFirmwareBuildDate
> 0)
196 time_t buildTime
= (time_t)m_configuration
.iFirmwareBuildDate
;
197 strLog
.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime
)));
198 strLog
= strLog
.Left((int)strLog
.length() - 1); // strip \n added by asctime
199 strLog
.append(" +0000");
201 CLibCEC::AddLog(CEC_LOG_NOTICE
, strLog
);
204 if (m_configuration
.bGetSettingsFromROM
== 1)
206 libcec_configuration config
;
208 m_communication
->GetConfiguration(&config
);
210 CLockObject
lock(m_mutex
);
211 if (!config
.deviceTypes
.IsEmpty())
212 m_configuration
.deviceTypes
= config
.deviceTypes
;
213 if (config
.iPhysicalAddress
> 0)
214 m_configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
215 snprintf(m_configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
221 bool CCECProcessor::IsInitialised(void)
223 CLockObject
lock(m_threadMutex
);
224 return m_bInitialised
;
227 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
229 CLockObject
lock(m_mutex
);
230 m_bInitialised
= bSetTo
;
233 bool CCECProcessor::Initialise(void)
237 CLockObject
lock(m_mutex
);
238 if (!m_configuration
.logicalAddresses
.IsEmpty())
239 m_configuration
.logicalAddresses
.Clear();
241 if (!FindLogicalAddresses())
243 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
247 /* only set our OSD name for the primary device */
248 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
250 /* make the primary device the active source if the option is set */
251 if (m_configuration
.bActivateSource
== 1)
252 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
254 /* set the default menu language for devices we control */
255 cec_menu_language language
;
256 language
.device
= m_configuration
.logicalAddresses
.primary
;
257 memcpy(language
.language
, m_configuration
.strDeviceLanguage
, 3);
258 language
.language
[3] = 0;
260 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
262 if (m_configuration
.logicalAddresses
[iPtr
])
264 language
.device
= (cec_logical_address
) iPtr
;
265 m_busDevices
[iPtr
]->SetMenuLanguage(language
);
270 /* get the vendor id from the TV, so we are using the correct handler */
271 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
273 if (m_configuration
.iPhysicalAddress
!= 0)
275 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
276 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
277 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
278 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
280 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
281 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
);
283 if (bReturn
&& m_configuration
.bActivateSource
== 1)
284 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
286 SetInitialised(bReturn
);
288 CLibCEC::ConfigurationChanged(m_configuration
);
293 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
298 CLockObject
lock(m_mutex
);
299 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
302 /* create the processor thread */
305 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
310 if ((bReturn
= Initialise()) == false)
312 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
317 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
323 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
325 if (m_busDevices
[address
]->TryLogicalAddress())
327 m_configuration
.logicalAddresses
.Set(address
);
334 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
336 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
337 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
338 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
339 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
342 bool CCECProcessor::FindLogicalAddressTuner(void)
344 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
345 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
346 TryLogicalAddress(CECDEVICE_TUNER2
) ||
347 TryLogicalAddress(CECDEVICE_TUNER3
) ||
348 TryLogicalAddress(CECDEVICE_TUNER4
);
351 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
353 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
354 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
355 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
356 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
359 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
361 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
362 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
365 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
367 bool bChanged(false);
369 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
371 CLockObject
lock(m_mutex
);
372 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
373 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
375 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
377 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
380 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
383 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
385 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
387 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
393 FindLogicalAddresses();
395 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
396 if (previousDevice
&& newDevice
)
398 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
399 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
401 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
402 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
404 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
406 newDevice
->SetMenuState(previousDevice
->GetMenuState());
407 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
409 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
410 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
412 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress());
413 previousDevice
->SetPhysicalAddress(0xFFFF);
415 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
416 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
418 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
419 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
421 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
422 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
424 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
425 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
427 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
428 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
436 bool CCECProcessor::FindLogicalAddresses(void)
439 m_configuration
.logicalAddresses
.Clear();
441 if (m_configuration
.deviceTypes
.IsEmpty())
443 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
447 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
449 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
452 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
454 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
455 bReturn
&= FindLogicalAddressRecordingDevice();
456 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
457 bReturn
&= FindLogicalAddressTuner();
458 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
459 bReturn
&= FindLogicalAddressPlaybackDevice();
460 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
461 bReturn
&= FindLogicalAddressAudioSystem();
465 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
470 void CCECProcessor::ReplaceHandlers(void)
472 if (!IsInitialised())
474 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
475 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
478 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
480 return m_inBuffer
.Push(command
);
483 void *CCECProcessor::Process(void)
485 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
490 while (!IsStopped() && m_communication
->IsOpen())
492 if (m_inBuffer
.Pop(command
, 500))
493 ParseCommand(command
);
499 m_controller
->CheckKeypressTimeout();
506 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
513 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
515 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
517 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
519 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
521 addr
= (cec_logical_address
) iPtr
;
527 m_busDevices
[addr
]->SetActiveSource();
528 if (m_busDevices
[addr
]->GetPhysicalAddress() != 0xFFFF)
529 bReturn
= m_busDevices
[addr
]->ActivateSource();
534 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
538 // suppress polls when searching for a device
539 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
542 device
->SetActiveSource();
547 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
553 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
555 CLockObject
lock(m_mutex
);
556 m_iStandardLineTimeout
= iTimeout
;
559 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
561 CLockObject
lock(m_mutex
);
562 m_iRetryLineTimeout
= iTimeout
;
565 bool CCECProcessor::SetActiveView(void)
567 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
568 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
571 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
575 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
578 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
580 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
587 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
591 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
594 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
596 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
603 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
607 // limit the HDMI port range to 1-15
614 CLockObject
lock(m_mutex
);
615 m_configuration
.baseDevice
= iBaseDevice
;
616 m_configuration
.iHDMIPort
= iPort
;
619 if (!IsRunning() && !bForce
)
622 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
624 uint16_t iPhysicalAddress(0);
625 if (iBaseDevice
> CECDEVICE_TV
)
627 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
630 if (iPhysicalAddress
< 0xffff)
632 if (iPhysicalAddress
== 0)
633 iPhysicalAddress
+= 0x1000 * iPort
;
634 else if (iPhysicalAddress
% 0x1000 == 0)
635 iPhysicalAddress
+= 0x100 * iPort
;
636 else if (iPhysicalAddress
% 0x100 == 0)
637 iPhysicalAddress
+= 0x10 * iPort
;
638 else if (iPhysicalAddress
% 0x10 == 0)
639 iPhysicalAddress
+= iPort
;
645 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
647 SetPhysicalAddress(iPhysicalAddress
);
652 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
654 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
656 if (m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
662 bool CCECProcessor::TransmitInactiveSource(void)
667 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
668 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
672 void CCECProcessor::LogOutput(const cec_command
&data
)
675 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
677 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
679 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
680 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
681 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
684 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
686 CLockObject
lock(m_mutex
);
687 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
689 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
690 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
691 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
692 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
698 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
700 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
702 if (m_configuration
.logicalAddresses
[iPtr
])
703 m_busDevices
[iPtr
]->SetMenuState(state
);
707 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
712 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
714 bool bSendActiveView(false);
716 cec_logical_addresses sendUpdatesTo
;
717 sendUpdatesTo
.Clear();
720 CLockObject
lock(m_mutex
);
721 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
722 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
724 if (!m_configuration
.logicalAddresses
.IsEmpty())
726 bool bWasActiveSource(false);
727 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
728 if (m_configuration
.logicalAddresses
[iPtr
])
730 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
731 m_busDevices
[iPtr
]->SetInactiveSource();
732 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
734 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
737 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
742 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
743 if (sendUpdatesTo
[iPtr
])
744 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
751 libcec_configuration config
;
753 CLockObject
lock(m_mutex
);
754 config
= m_configuration
;
757 PersistConfiguration(&config
);
758 CLibCEC::ConfigurationChanged(config
);
764 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
766 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
769 CLockObject
lock(m_mutex
);
770 m_bMonitor
= bEnable
;
774 return SetAckMask(0);
776 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
779 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
781 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
783 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
784 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
785 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
790 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
793 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
794 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
799 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
802 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
803 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
808 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
811 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
812 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
817 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
819 CCECBusDevice
*device(NULL
);
822 if (iPhysicalAddress
== 0xFFFF)
825 // check each device until we found a match
826 for (unsigned int iPtr
= 0; !device
&& iPtr
< 16; iPtr
++)
828 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bSuppressUpdate
) == iPhysicalAddress
)
829 device
= m_busDevices
[iPtr
];
835 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
837 CCECBusDevice
*device
= NULL
;
839 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
841 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
843 device
= m_busDevices
[iPtr
];
851 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
853 CCECBusDevice
*device(NULL
);
854 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
855 if (primary
!= CECDEVICE_UNKNOWN
)
856 device
= m_busDevices
[primary
];
860 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
862 return m_busDevices
[iAddress
]->GetCecVersion();
865 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
867 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
870 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
871 retVal
.device
= iAddress
;
876 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
878 if (m_busDevices
[iAddress
])
880 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
881 return (strcmp(language
->language
, "???") != 0);
886 CStdString
CCECProcessor::GetDeviceName(void) const
889 strName
= m_configuration
.strDeviceName
;
893 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
895 if (m_busDevices
[iAddress
])
896 return m_busDevices
[iAddress
]->GetVendorId();
900 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
902 if (m_busDevices
[iAddress
])
903 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
907 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
909 if (m_busDevices
[iAddress
])
910 return m_busDevices
[iAddress
]->GetPowerStatus();
911 return CEC_POWER_STATUS_UNKNOWN
;
914 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
916 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
918 if (m_busDevices
[iPtr
]->IsActiveSource())
919 return (cec_logical_address
)iPtr
;
922 if (bRequestActiveSource
&& m_configuration
.logicalAddresses
.primary
!= CECDEVICE_UNKNOWN
)
924 CCECBusDevice
*primary
= m_busDevices
[m_configuration
.logicalAddresses
.primary
];
926 primary
->RequestActiveSource();
928 return GetActiveSource(false);
931 return CECDEVICE_UNKNOWN
;
934 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
936 return iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
?
937 m_busDevices
[iAddress
]->IsActiveSource() :
941 bool CCECProcessor::Transmit(const cec_command
&data
)
943 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
945 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
949 uint8_t iMaxTries(0);
951 CLockObject
lock(m_mutex
);
955 m_iLastTransmission
= GetTimeMs();
956 if (!m_communication
|| !m_communication
->IsOpen())
958 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
961 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
966 uint8_t iLineTimeout
= m_iStandardLineTimeout
;
967 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
969 while (bRetry
&& ++iTries
< iMaxTries
)
971 if (m_busDevices
[data
.initiator
]->IsUnsupportedFeature(data
.opcode
))
974 adapterState
= m_communication
->Write(data
, bRetry
, iLineTimeout
);
975 iLineTimeout
= m_iRetryLineTimeout
;
978 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
981 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
983 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
987 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
988 command
.parameters
.PushBack((uint8_t)opcode
);
989 command
.parameters
.PushBack((uint8_t)reason
);
994 void CCECProcessor::ParseCommand(const cec_command
&command
)
997 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
998 if (command
.opcode_set
== 1)
999 dataStr
.AppendFormat(":%02x", command
.opcode
);
1000 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
1001 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
1002 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
1004 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
1005 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
1008 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
1010 cec_logical_addresses addresses
;
1012 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1014 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1015 addresses
.Set((cec_logical_address
) iPtr
);
1020 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
1022 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
1025 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
1027 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1029 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1036 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1038 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
1039 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress();
1043 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1045 return m_communication
? m_communication
->SetAckMask(iMask
) : false;
1048 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1050 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1053 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1055 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1058 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1060 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
1061 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1062 if (iPhysicalAddress
!= 0)
1064 m_configuration
.bAutodetectAddress
= 1;
1065 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1066 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1067 m_configuration
.iHDMIPort
= 0;
1068 return SetPhysicalAddress(iPhysicalAddress
);
1073 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1075 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1078 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1080 if (m_configuration
.powerOffDevices
[iPtr
])
1082 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - putting '%s' in standby mode", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1083 bReturn
&= m_busDevices
[iPtr
]->Standby();
1089 return m_busDevices
[address
]->Standby();
1092 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1094 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1097 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1099 if (m_configuration
.wakeDevices
[iPtr
])
1101 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - powering on '%s'", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1102 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1108 return m_busDevices
[address
]->PowerOn();
1111 const char *CCECProcessor::ToString(const cec_device_type type
)
1115 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1116 return "audio system";
1117 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1118 return "playback device";
1119 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1120 return "recording device";
1121 case CEC_DEVICE_TYPE_RESERVED
:
1123 case CEC_DEVICE_TYPE_TUNER
:
1125 case CEC_DEVICE_TYPE_TV
:
1132 const char *CCECProcessor::ToString(const cec_menu_state state
)
1136 case CEC_MENU_STATE_ACTIVATED
:
1138 case CEC_MENU_STATE_DEACTIVATED
:
1139 return "deactivated";
1145 const char *CCECProcessor::ToString(const cec_version version
)
1149 case CEC_VERSION_1_2
:
1151 case CEC_VERSION_1_2A
:
1153 case CEC_VERSION_1_3
:
1155 case CEC_VERSION_1_3A
:
1157 case CEC_VERSION_1_4
:
1164 const char *CCECProcessor::ToString(const cec_power_status status
)
1168 case CEC_POWER_STATUS_ON
:
1170 case CEC_POWER_STATUS_STANDBY
:
1172 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1173 return "in transition from on to standby";
1174 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1175 return "in transition from standby to on";
1181 const char *CCECProcessor::ToString(const cec_logical_address address
)
1185 case CECDEVICE_AUDIOSYSTEM
:
1187 case CECDEVICE_BROADCAST
:
1189 case CECDEVICE_FREEUSE
:
1191 case CECDEVICE_PLAYBACKDEVICE1
:
1192 return "Playback 1";
1193 case CECDEVICE_PLAYBACKDEVICE2
:
1194 return "Playback 2";
1195 case CECDEVICE_PLAYBACKDEVICE3
:
1196 return "Playback 3";
1197 case CECDEVICE_RECORDINGDEVICE1
:
1198 return "Recorder 1";
1199 case CECDEVICE_RECORDINGDEVICE2
:
1200 return "Recorder 2";
1201 case CECDEVICE_RECORDINGDEVICE3
:
1202 return "Recorder 3";
1203 case CECDEVICE_RESERVED1
:
1204 return "Reserved 1";
1205 case CECDEVICE_RESERVED2
:
1206 return "Reserved 2";
1207 case CECDEVICE_TUNER1
:
1209 case CECDEVICE_TUNER2
:
1211 case CECDEVICE_TUNER3
:
1213 case CECDEVICE_TUNER4
:
1222 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1226 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1227 return "skip forward wind";
1228 case CEC_DECK_CONTROL_MODE_EJECT
:
1230 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1231 return "reverse rewind";
1232 case CEC_DECK_CONTROL_MODE_STOP
:
1239 const char *CCECProcessor::ToString(const cec_deck_info status
)
1243 case CEC_DECK_INFO_PLAY
:
1245 case CEC_DECK_INFO_RECORD
:
1247 case CEC_DECK_INFO_PLAY_REVERSE
:
1248 return "play reverse";
1249 case CEC_DECK_INFO_STILL
:
1251 case CEC_DECK_INFO_SLOW
:
1253 case CEC_DECK_INFO_SLOW_REVERSE
:
1254 return "slow reverse";
1255 case CEC_DECK_INFO_FAST_FORWARD
:
1256 return "fast forward";
1257 case CEC_DECK_INFO_FAST_REVERSE
:
1258 return "fast reverse";
1259 case CEC_DECK_INFO_NO_MEDIA
:
1261 case CEC_DECK_INFO_STOP
:
1263 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1264 return "info skip forward wind";
1265 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1266 return "info skip reverse rewind";
1267 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1268 return "info index search forward";
1269 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1270 return "info index search reverse";
1271 case CEC_DECK_INFO_OTHER_STATUS
:
1278 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1282 case CEC_OPCODE_ACTIVE_SOURCE
:
1283 return "active source";
1284 case CEC_OPCODE_IMAGE_VIEW_ON
:
1285 return "image view on";
1286 case CEC_OPCODE_TEXT_VIEW_ON
:
1287 return "text view on";
1288 case CEC_OPCODE_INACTIVE_SOURCE
:
1289 return "inactive source";
1290 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1291 return "request active source";
1292 case CEC_OPCODE_ROUTING_CHANGE
:
1293 return "routing change";
1294 case CEC_OPCODE_ROUTING_INFORMATION
:
1295 return "routing information";
1296 case CEC_OPCODE_SET_STREAM_PATH
:
1297 return "set stream path";
1298 case CEC_OPCODE_STANDBY
:
1300 case CEC_OPCODE_RECORD_OFF
:
1301 return "record off";
1302 case CEC_OPCODE_RECORD_ON
:
1304 case CEC_OPCODE_RECORD_STATUS
:
1305 return "record status";
1306 case CEC_OPCODE_RECORD_TV_SCREEN
:
1307 return "record tv screen";
1308 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1309 return "clear analogue timer";
1310 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1311 return "clear digital timer";
1312 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1313 return "clear external timer";
1314 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1315 return "set analogue timer";
1316 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1317 return "set digital timer";
1318 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1319 return "set external timer";
1320 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1321 return "set timer program title";
1322 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1323 return "timer cleared status";
1324 case CEC_OPCODE_TIMER_STATUS
:
1325 return "timer status";
1326 case CEC_OPCODE_CEC_VERSION
:
1327 return "cec version";
1328 case CEC_OPCODE_GET_CEC_VERSION
:
1329 return "get cec version";
1330 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1331 return "give physical address";
1332 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1333 return "get menu language";
1334 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1335 return "report physical address";
1336 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1337 return "set menu language";
1338 case CEC_OPCODE_DECK_CONTROL
:
1339 return "deck control";
1340 case CEC_OPCODE_DECK_STATUS
:
1341 return "deck status";
1342 case CEC_OPCODE_GIVE_DECK_STATUS
:
1343 return "give deck status";
1344 case CEC_OPCODE_PLAY
:
1346 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1347 return "give tuner status";
1348 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1349 return "select analogue service";
1350 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1351 return "set digital service";
1352 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1353 return "tuner device status";
1354 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1355 return "tuner step decrement";
1356 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1357 return "tuner step increment";
1358 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1359 return "device vendor id";
1360 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1361 return "give device vendor id";
1362 case CEC_OPCODE_VENDOR_COMMAND
:
1363 return "vendor command";
1364 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1365 return "vendor command with id";
1366 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1367 return "vendor remote button down";
1368 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1369 return "vendor remote button up";
1370 case CEC_OPCODE_SET_OSD_STRING
:
1371 return "set osd string";
1372 case CEC_OPCODE_GIVE_OSD_NAME
:
1373 return "give osd name";
1374 case CEC_OPCODE_SET_OSD_NAME
:
1375 return "set osd name";
1376 case CEC_OPCODE_MENU_REQUEST
:
1377 return "menu request";
1378 case CEC_OPCODE_MENU_STATUS
:
1379 return "menu status";
1380 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1381 return "user control pressed";
1382 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1383 return "user control release";
1384 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1385 return "give device power status";
1386 case CEC_OPCODE_REPORT_POWER_STATUS
:
1387 return "report power status";
1388 case CEC_OPCODE_FEATURE_ABORT
:
1389 return "feature abort";
1390 case CEC_OPCODE_ABORT
:
1392 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1393 return "give audio status";
1394 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1395 return "give audio mode status";
1396 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1397 return "report audio status";
1398 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1399 return "set system audio mode";
1400 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1401 return "system audio mode request";
1402 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1403 return "system audio mode status";
1404 case CEC_OPCODE_SET_AUDIO_RATE
:
1405 return "set audio rate";
1406 case CEC_OPCODE_NONE
:
1413 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1417 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1419 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1426 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1428 // TODO this is a mask
1432 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1436 case CEC_VENDOR_SAMSUNG
:
1440 case CEC_VENDOR_PANASONIC
:
1442 case CEC_VENDOR_PIONEER
:
1444 case CEC_VENDOR_ONKYO
:
1446 case CEC_VENDOR_YAMAHA
:
1448 case CEC_VENDOR_PHILIPS
:
1450 case CEC_VENDOR_SONY
:
1452 case CEC_VENDOR_TOSHIBA
:
1459 const char *CCECProcessor::ToString(const cec_client_version version
)
1463 case CEC_CLIENT_VERSION_PRE_1_5
:
1465 case CEC_CLIENT_VERSION_1_5_0
:
1467 case CEC_CLIENT_VERSION_1_5_1
:
1469 case CEC_CLIENT_VERSION_1_5_2
:
1471 case CEC_CLIENT_VERSION_1_5_3
:
1473 case CEC_CLIENT_VERSION_1_6_0
:
1475 case CEC_CLIENT_VERSION_1_6_1
:
1477 case CEC_CLIENT_VERSION_1_6_2
:
1484 const char *CCECProcessor::ToString(const cec_server_version version
)
1488 case CEC_SERVER_VERSION_PRE_1_5
:
1490 case CEC_SERVER_VERSION_1_5_0
:
1492 case CEC_SERVER_VERSION_1_5_1
:
1494 case CEC_SERVER_VERSION_1_5_2
:
1496 case CEC_SERVER_VERSION_1_5_3
:
1498 case CEC_SERVER_VERSION_1_6_0
:
1500 case CEC_SERVER_VERSION_1_6_1
:
1502 case CEC_SERVER_VERSION_1_6_2
:
1509 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1511 if (!m_communication
&& strPort
)
1513 bool bReturn(false);
1514 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1515 CTimeout
timeout(10000);
1517 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
1519 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1525 bReturn
= comm
->StartBootloader();
1532 return m_communication
->StartBootloader();
1536 bool CCECProcessor::PingAdapter(void)
1538 return m_communication
->PingAdapter();
1541 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1543 if (destination
< CECDEVICE_BROADCAST
)
1544 m_busDevices
[destination
]->HandlePollFrom(initiator
);
1547 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1549 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1552 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1554 // stream path changes are sent by the TV
1555 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1558 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1560 bool bReinit(false);
1561 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1562 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1563 m_configuration
.clientVersion
= configuration
->clientVersion
;
1564 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString((cec_client_version
)configuration
->clientVersion
));
1566 // client version 1.5.0
1569 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1570 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1571 if (bDeviceTypeChanged
)
1572 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(configuration
->deviceTypes
[0]));
1574 bool bPhysicalAddressChanged(false);
1576 // autodetect address
1577 bool bPhysicalAutodetected(false);
1578 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1580 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1581 if (iPhysicalAddress
!= 0)
1584 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1586 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%x'", __FUNCTION__
, iPhysicalAddress
);
1587 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1588 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1589 m_configuration
.iHDMIPort
= 0;
1590 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1591 bPhysicalAutodetected
= true;
1596 if (!bPhysicalAutodetected
)
1598 if (configuration
->iPhysicalAddress
!= 0)
1599 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1600 if (bPhysicalAddressChanged
)
1602 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - physical address '%x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1603 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1607 bool bHdmiPortChanged(false);
1608 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1611 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1612 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1613 m_configuration
.baseDevice
= configuration
->baseDevice
;
1616 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1617 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1618 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1622 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1623 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1624 m_configuration
.iHDMIPort
= 0;
1627 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1630 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, configuration
->strDeviceName
);
1631 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1632 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1634 primary
->SetOSDName(m_configuration
.strDeviceName
);
1635 if (!bReinit
&& IsRunning())
1636 primary
->TransmitOSDName(CECDEVICE_TV
);
1639 // tv vendor id override
1640 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString((cec_vendor_id
)configuration
->tvVendor
));
1641 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1643 m_configuration
.tvVendor
= configuration
->tvVendor
;
1644 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1648 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1650 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1651 if (!bReinit
&& IsRunning())
1656 m_configuration
.bUseTVMenuLanguage
= configuration
->bUseTVMenuLanguage
;
1657 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1658 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1659 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1660 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1661 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1663 // client version 1.5.1
1664 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1665 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1667 // client version 1.6.0
1668 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1670 m_configuration
.bPowerOffDevicesOnStandby
= configuration
->bPowerOffDevicesOnStandby
;
1671 m_configuration
.bShutdownOnStandby
= configuration
->bShutdownOnStandby
;
1674 // client version 1.6.2
1675 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1677 memcpy(m_configuration
.strDeviceLanguage
, configuration
->strDeviceLanguage
, 3);
1680 // ensure that there is at least 1 device type set
1681 if (m_configuration
.deviceTypes
.IsEmpty())
1682 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1684 // persist the configuration
1686 m_communication
->PersistConfiguration(&m_configuration
);
1688 if (bReinit
|| m_configuration
.logicalAddresses
.IsEmpty())
1690 if (bDeviceTypeChanged
)
1691 return ChangeDeviceType(oldPrimaryType
, m_configuration
.deviceTypes
[0]);
1692 else if (bPhysicalAddressChanged
)
1693 return SetPhysicalAddress(m_configuration
.iPhysicalAddress
);
1695 return SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1697 else if (m_configuration
.bActivateSource
== 1 && IsRunning() && !IsActiveSource(m_configuration
.logicalAddresses
.primary
))
1699 // activate the source if we're not already the active source
1700 SetActiveSource(m_configuration
.deviceTypes
.types
[0]);
1706 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1708 // client version 1.5.0
1709 snprintf(configuration
->strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
1710 configuration
->deviceTypes
= m_configuration
.deviceTypes
;
1711 configuration
->bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
1712 configuration
->iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
1713 configuration
->baseDevice
= m_configuration
.baseDevice
;
1714 configuration
->iHDMIPort
= m_configuration
.iHDMIPort
;
1715 configuration
->clientVersion
= m_configuration
.clientVersion
;
1716 configuration
->serverVersion
= m_configuration
.serverVersion
;
1717 configuration
->tvVendor
= m_configuration
.tvVendor
;
1719 configuration
->bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
1720 configuration
->bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
1721 configuration
->bActivateSource
= m_configuration
.bActivateSource
;
1722 configuration
->wakeDevices
= m_configuration
.wakeDevices
;
1723 configuration
->powerOffDevices
= m_configuration
.powerOffDevices
;
1724 configuration
->bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
1725 configuration
->bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
1727 // client version 1.5.1
1728 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1729 configuration
->bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
1731 // client version 1.5.3
1732 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1733 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1735 // client version 1.6.0
1736 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1738 configuration
->iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
1739 configuration
->bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
1740 configuration
->bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
1743 // client version 1.6.2
1744 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1746 memcpy(configuration
->strDeviceLanguage
, m_configuration
.strDeviceLanguage
, 3);
1747 configuration
->iFirmwareBuildDate
= m_configuration
.iFirmwareBuildDate
;
1752 bool CCECProcessor::CanPersistConfiguration(void)
1754 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
1757 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1759 return m_communication
? m_communication
->PersistConfiguration(configuration
) : false;
1762 void CCECProcessor::RescanActiveDevices(void)
1764 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1765 m_busDevices
[iPtr
]->GetStatus(true);
1768 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = 10000 */)
1770 if (!OpenConnection(strPort
, 38400, iTimeoutMs
, false))
1773 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
1774 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1776 delete m_communication
;
1777 m_communication
= NULL
;
1781 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
1784 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1785 bReturn
&= m_busDevices
[iPtr
]->TransmitPendingActiveSourceCommands();