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 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
));
194 if (m_configuration
.bGetSettingsFromROM
== 1)
196 libcec_configuration config
;
198 m_communication
->GetConfiguration(&config
);
200 CLockObject
lock(m_mutex
);
201 if (!config
.deviceTypes
.IsEmpty())
202 m_configuration
.deviceTypes
= config
.deviceTypes
;
203 if (config
.iPhysicalAddress
> 0)
204 m_configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
205 snprintf(m_configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
211 bool CCECProcessor::IsInitialised(void)
213 CLockObject
lock(m_mutex
);
214 return m_bInitialised
;
217 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
219 CLockObject
lock(m_mutex
);
220 m_bInitialised
= bSetTo
;
223 bool CCECProcessor::Initialise(void)
227 CLockObject
lock(m_mutex
);
228 if (!m_configuration
.logicalAddresses
.IsEmpty())
229 m_configuration
.logicalAddresses
.Clear();
231 if (!FindLogicalAddresses())
233 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
237 /* only set our OSD name for the primary device */
238 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
240 /* make the primary device the active source if the option is set */
241 if (m_configuration
.bActivateSource
== 1)
242 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
245 /* get the vendor id from the TV, so we are using the correct handler */
246 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
248 if (m_configuration
.iPhysicalAddress
!= 0)
250 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
251 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
252 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
253 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
255 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
256 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
);
258 if (bReturn
&& m_configuration
.bActivateSource
== 1)
259 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
261 SetInitialised(bReturn
);
263 CLibCEC::ConfigurationChanged(m_configuration
);
268 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
273 CLockObject
lock(m_mutex
);
274 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
277 /* create the processor thread */
280 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
285 if ((bReturn
= Initialise()) == false)
287 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
292 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
298 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
300 if (m_busDevices
[address
]->TryLogicalAddress())
302 m_configuration
.logicalAddresses
.Set(address
);
309 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
311 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
312 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
313 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
314 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
317 bool CCECProcessor::FindLogicalAddressTuner(void)
319 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
320 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
321 TryLogicalAddress(CECDEVICE_TUNER2
) ||
322 TryLogicalAddress(CECDEVICE_TUNER3
) ||
323 TryLogicalAddress(CECDEVICE_TUNER4
);
326 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
328 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
329 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
330 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
331 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
334 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
336 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
337 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
340 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
342 bool bChanged(false);
344 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
346 CLockObject
lock(m_mutex
);
347 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
348 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
350 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
352 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
355 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
358 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
360 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
362 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
368 FindLogicalAddresses();
370 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
371 if (previousDevice
&& newDevice
)
373 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
374 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
376 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
377 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
379 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
380 cec_menu_language lang
;
381 lang
.device
= previousDevice
->GetLogicalAddress();
382 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
383 lang
.language
[iPtr
] = '?';
384 lang
.language
[3] = 0;
385 previousDevice
->SetMenuLanguage(lang
);
387 newDevice
->SetMenuState(previousDevice
->GetMenuState());
388 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
390 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
391 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
393 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
394 previousDevice
->SetPhysicalAddress(0xFFFF);
396 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
397 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
399 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
400 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
402 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
403 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
405 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
406 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
408 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
409 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
417 bool CCECProcessor::FindLogicalAddresses(void)
420 m_configuration
.logicalAddresses
.Clear();
422 if (m_configuration
.deviceTypes
.IsEmpty())
424 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
428 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
430 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
433 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
435 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
436 bReturn
&= FindLogicalAddressRecordingDevice();
437 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
438 bReturn
&= FindLogicalAddressTuner();
439 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
440 bReturn
&= FindLogicalAddressPlaybackDevice();
441 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
442 bReturn
&= FindLogicalAddressAudioSystem();
446 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
451 void CCECProcessor::ReplaceHandlers(void)
453 if (!IsInitialised())
455 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
456 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
459 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
461 return m_inBuffer
.Push(command
);
464 void *CCECProcessor::Process(void)
466 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
471 while (!IsStopped() && m_communication
->IsOpen())
473 if (m_inBuffer
.Pop(command
, 500))
474 ParseCommand(command
);
480 m_controller
->CheckKeypressTimeout();
487 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
494 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
496 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
498 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
500 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
502 addr
= (cec_logical_address
) iPtr
;
508 m_busDevices
[addr
]->SetActiveSource();
509 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
510 bReturn
= m_busDevices
[addr
]->ActivateSource();
515 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
519 // suppress polls when searching for a device
520 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
, false, true);
523 device
->SetActiveSource();
530 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
532 CLockObject
lock(m_mutex
);
533 m_iStandardLineTimeout
= iTimeout
;
536 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
538 CLockObject
lock(m_mutex
);
539 m_iRetryLineTimeout
= iTimeout
;
542 bool CCECProcessor::SetActiveView(void)
544 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
545 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
548 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
552 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
555 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
557 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
564 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
568 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
571 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
573 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
580 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
584 // limit the HDMI port range to 1-15
591 CLockObject
lock(m_mutex
);
592 m_configuration
.baseDevice
= iBaseDevice
;
593 m_configuration
.iHDMIPort
= iPort
;
596 if (!IsRunning() && !bForce
)
599 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
601 uint16_t iPhysicalAddress(0);
602 if (iBaseDevice
> CECDEVICE_TV
)
604 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
607 if (iPhysicalAddress
< 0xffff)
609 if (iPhysicalAddress
== 0)
610 iPhysicalAddress
+= 0x1000 * iPort
;
611 else if (iPhysicalAddress
% 0x1000 == 0)
612 iPhysicalAddress
+= 0x100 * iPort
;
613 else if (iPhysicalAddress
% 0x100 == 0)
614 iPhysicalAddress
+= 0x10 * iPort
;
615 else if (iPhysicalAddress
% 0x10 == 0)
616 iPhysicalAddress
+= iPort
;
622 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
624 SetPhysicalAddress(iPhysicalAddress
);
629 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
631 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
633 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
639 bool CCECProcessor::TransmitInactiveSource(void)
644 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
645 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
649 void CCECProcessor::LogOutput(const cec_command
&data
)
652 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
654 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
656 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
657 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
658 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
661 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
663 CLockObject
lock(m_mutex
);
664 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
666 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
667 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
668 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
669 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
675 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
677 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
679 if (m_configuration
.logicalAddresses
[iPtr
])
680 m_busDevices
[iPtr
]->SetMenuState(state
);
684 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
689 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
691 bool bSendActiveView(false);
693 cec_logical_addresses sendUpdatesTo
;
694 sendUpdatesTo
.Clear();
697 CLockObject
lock(m_mutex
);
698 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
699 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
701 if (!m_configuration
.logicalAddresses
.IsEmpty())
703 bool bWasActiveSource(false);
704 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
705 if (m_configuration
.logicalAddresses
[iPtr
])
707 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
708 m_busDevices
[iPtr
]->SetInactiveSource();
709 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
711 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
714 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
719 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
720 if (sendUpdatesTo
[iPtr
])
721 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
728 libcec_configuration config
;
730 CLockObject
lock(m_mutex
);
731 config
= m_configuration
;
734 PersistConfiguration(&config
);
735 CLibCEC::ConfigurationChanged(config
);
741 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
743 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
746 CLockObject
lock(m_mutex
);
747 m_bMonitor
= bEnable
;
751 return SetAckMask(0);
753 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
756 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
758 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
760 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
761 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
762 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
767 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
770 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
771 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
776 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
779 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
780 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
785 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
788 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
789 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
794 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */, bool bSuppressPoll
/* = false */) const
796 if (m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
797 return m_busDevices
[m_configuration
.logicalAddresses
.primary
];
799 CCECBusDevice
*device
= NULL
;
800 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
802 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
, bSuppressPoll
) == iPhysicalAddress
)
804 device
= m_busDevices
[iPtr
];
812 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
814 CCECBusDevice
*device
= NULL
;
816 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
818 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
820 device
= m_busDevices
[iPtr
];
828 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
830 CCECBusDevice
*device(NULL
);
831 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
832 if (primary
!= CECDEVICE_UNKNOWN
)
833 device
= m_busDevices
[primary
];
837 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
839 return m_busDevices
[iAddress
]->GetCecVersion();
842 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
844 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
847 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
848 retVal
.device
= iAddress
;
853 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
855 if (m_busDevices
[iAddress
])
857 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
858 return (strcmp(language
->language
, "???") != 0);
863 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
865 if (m_busDevices
[iAddress
])
866 return m_busDevices
[iAddress
]->GetVendorId();
870 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
872 if (m_busDevices
[iAddress
])
873 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
877 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
879 if (m_busDevices
[iAddress
])
880 return m_busDevices
[iAddress
]->GetPowerStatus();
881 return CEC_POWER_STATUS_UNKNOWN
;
884 cec_logical_address
CCECProcessor::GetActiveSource(void)
886 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
888 if (m_busDevices
[iPtr
]->IsActiveSource())
889 return (cec_logical_address
)iPtr
;
892 return CECDEVICE_UNKNOWN
;
895 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
897 return iAddress
> CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
?
898 m_busDevices
[iAddress
]->IsActiveSource() :
902 bool CCECProcessor::Transmit(const cec_command
&data
)
904 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
906 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
910 uint8_t iMaxTries(0);
912 CLockObject
lock(m_mutex
);
916 m_iLastTransmission
= GetTimeMs();
917 if (!m_communication
|| !m_communication
->IsOpen())
919 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
922 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
925 return m_communication
->Write(data
, iMaxTries
, m_iStandardLineTimeout
, m_iRetryLineTimeout
)
926 == ADAPTER_MESSAGE_STATE_SENT_ACKED
;
929 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
931 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
935 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
936 command
.parameters
.PushBack((uint8_t)opcode
);
937 command
.parameters
.PushBack((uint8_t)reason
);
942 void CCECProcessor::ParseCommand(const cec_command
&command
)
945 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
946 if (command
.opcode_set
== 1)
947 dataStr
.AppendFormat(":%02x", command
.opcode
);
948 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
949 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
950 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
952 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
953 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
956 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
958 cec_logical_addresses addresses
;
960 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
962 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
963 addresses
.Set((cec_logical_address
) iPtr
);
968 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
970 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
973 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
975 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
977 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
984 uint16_t CCECProcessor::GetPhysicalAddress(void) const
986 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
987 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress(false);
991 bool CCECProcessor::SetAckMask(uint16_t iMask
)
993 return m_communication
->SetAckMask(iMask
);
996 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
998 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1001 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1003 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1006 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1008 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
1009 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1010 if (iPhysicalAddress
!= 0)
1012 m_configuration
.bAutodetectAddress
= 1;
1013 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1014 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1015 m_configuration
.iHDMIPort
= 0;
1016 return SetPhysicalAddress(iPhysicalAddress
);
1021 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1023 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1026 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1028 if (m_configuration
.powerOffDevices
[iPtr
])
1029 bReturn
&= m_busDevices
[iPtr
]->Standby();
1034 return m_busDevices
[address
]->Standby();
1037 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1039 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1042 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1044 if (m_configuration
.powerOffDevices
[iPtr
])
1045 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1050 return m_busDevices
[address
]->PowerOn();
1053 const char *CCECProcessor::ToString(const cec_device_type type
)
1057 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1058 return "audio system";
1059 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1060 return "playback device";
1061 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1062 return "recording device";
1063 case CEC_DEVICE_TYPE_RESERVED
:
1065 case CEC_DEVICE_TYPE_TUNER
:
1067 case CEC_DEVICE_TYPE_TV
:
1074 const char *CCECProcessor::ToString(const cec_menu_state state
)
1078 case CEC_MENU_STATE_ACTIVATED
:
1080 case CEC_MENU_STATE_DEACTIVATED
:
1081 return "deactivated";
1087 const char *CCECProcessor::ToString(const cec_version version
)
1091 case CEC_VERSION_1_2
:
1093 case CEC_VERSION_1_2A
:
1095 case CEC_VERSION_1_3
:
1097 case CEC_VERSION_1_3A
:
1099 case CEC_VERSION_1_4
:
1106 const char *CCECProcessor::ToString(const cec_power_status status
)
1110 case CEC_POWER_STATUS_ON
:
1112 case CEC_POWER_STATUS_STANDBY
:
1114 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1115 return "in transition from on to standby";
1116 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1117 return "in transition from standby to on";
1123 const char *CCECProcessor::ToString(const cec_logical_address address
)
1127 case CECDEVICE_AUDIOSYSTEM
:
1129 case CECDEVICE_BROADCAST
:
1131 case CECDEVICE_FREEUSE
:
1133 case CECDEVICE_PLAYBACKDEVICE1
:
1134 return "Playback 1";
1135 case CECDEVICE_PLAYBACKDEVICE2
:
1136 return "Playback 2";
1137 case CECDEVICE_PLAYBACKDEVICE3
:
1138 return "Playback 3";
1139 case CECDEVICE_RECORDINGDEVICE1
:
1140 return "Recorder 1";
1141 case CECDEVICE_RECORDINGDEVICE2
:
1142 return "Recorder 2";
1143 case CECDEVICE_RECORDINGDEVICE3
:
1144 return "Recorder 3";
1145 case CECDEVICE_RESERVED1
:
1146 return "Reserved 1";
1147 case CECDEVICE_RESERVED2
:
1148 return "Reserved 2";
1149 case CECDEVICE_TUNER1
:
1151 case CECDEVICE_TUNER2
:
1153 case CECDEVICE_TUNER3
:
1155 case CECDEVICE_TUNER4
:
1164 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1168 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1169 return "skip forward wind";
1170 case CEC_DECK_CONTROL_MODE_EJECT
:
1172 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1173 return "reverse rewind";
1174 case CEC_DECK_CONTROL_MODE_STOP
:
1181 const char *CCECProcessor::ToString(const cec_deck_info status
)
1185 case CEC_DECK_INFO_PLAY
:
1187 case CEC_DECK_INFO_RECORD
:
1189 case CEC_DECK_INFO_PLAY_REVERSE
:
1190 return "play reverse";
1191 case CEC_DECK_INFO_STILL
:
1193 case CEC_DECK_INFO_SLOW
:
1195 case CEC_DECK_INFO_SLOW_REVERSE
:
1196 return "slow reverse";
1197 case CEC_DECK_INFO_FAST_FORWARD
:
1198 return "fast forward";
1199 case CEC_DECK_INFO_FAST_REVERSE
:
1200 return "fast reverse";
1201 case CEC_DECK_INFO_NO_MEDIA
:
1203 case CEC_DECK_INFO_STOP
:
1205 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1206 return "info skip forward wind";
1207 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1208 return "info skip reverse rewind";
1209 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1210 return "info index search forward";
1211 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1212 return "info index search reverse";
1213 case CEC_DECK_INFO_OTHER_STATUS
:
1220 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1224 case CEC_OPCODE_ACTIVE_SOURCE
:
1225 return "active source";
1226 case CEC_OPCODE_IMAGE_VIEW_ON
:
1227 return "image view on";
1228 case CEC_OPCODE_TEXT_VIEW_ON
:
1229 return "text view on";
1230 case CEC_OPCODE_INACTIVE_SOURCE
:
1231 return "inactive source";
1232 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1233 return "request active source";
1234 case CEC_OPCODE_ROUTING_CHANGE
:
1235 return "routing change";
1236 case CEC_OPCODE_ROUTING_INFORMATION
:
1237 return "routing information";
1238 case CEC_OPCODE_SET_STREAM_PATH
:
1239 return "set stream path";
1240 case CEC_OPCODE_STANDBY
:
1242 case CEC_OPCODE_RECORD_OFF
:
1243 return "record off";
1244 case CEC_OPCODE_RECORD_ON
:
1246 case CEC_OPCODE_RECORD_STATUS
:
1247 return "record status";
1248 case CEC_OPCODE_RECORD_TV_SCREEN
:
1249 return "record tv screen";
1250 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1251 return "clear analogue timer";
1252 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1253 return "clear digital timer";
1254 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1255 return "clear external timer";
1256 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1257 return "set analogue timer";
1258 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1259 return "set digital timer";
1260 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1261 return "set external timer";
1262 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1263 return "set timer program title";
1264 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1265 return "timer cleared status";
1266 case CEC_OPCODE_TIMER_STATUS
:
1267 return "timer status";
1268 case CEC_OPCODE_CEC_VERSION
:
1269 return "cec version";
1270 case CEC_OPCODE_GET_CEC_VERSION
:
1271 return "get cec version";
1272 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1273 return "give physical address";
1274 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1275 return "get menu language";
1276 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1277 return "report physical address";
1278 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1279 return "set menu language";
1280 case CEC_OPCODE_DECK_CONTROL
:
1281 return "deck control";
1282 case CEC_OPCODE_DECK_STATUS
:
1283 return "deck status";
1284 case CEC_OPCODE_GIVE_DECK_STATUS
:
1285 return "give deck status";
1286 case CEC_OPCODE_PLAY
:
1288 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1289 return "give tuner status";
1290 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1291 return "select analogue service";
1292 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1293 return "set digital service";
1294 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1295 return "tuner device status";
1296 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1297 return "tuner step decrement";
1298 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1299 return "tuner step increment";
1300 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1301 return "device vendor id";
1302 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1303 return "give device vendor id";
1304 case CEC_OPCODE_VENDOR_COMMAND
:
1305 return "vendor command";
1306 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1307 return "vendor command with id";
1308 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1309 return "vendor remote button down";
1310 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1311 return "vendor remote button up";
1312 case CEC_OPCODE_SET_OSD_STRING
:
1313 return "set osd string";
1314 case CEC_OPCODE_GIVE_OSD_NAME
:
1315 return "give osd name";
1316 case CEC_OPCODE_SET_OSD_NAME
:
1317 return "set osd name";
1318 case CEC_OPCODE_MENU_REQUEST
:
1319 return "menu request";
1320 case CEC_OPCODE_MENU_STATUS
:
1321 return "menu status";
1322 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1323 return "user control pressed";
1324 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1325 return "user control release";
1326 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1327 return "give device power status";
1328 case CEC_OPCODE_REPORT_POWER_STATUS
:
1329 return "report power status";
1330 case CEC_OPCODE_FEATURE_ABORT
:
1331 return "feature abort";
1332 case CEC_OPCODE_ABORT
:
1334 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1335 return "give audio status";
1336 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1337 return "give audio mode status";
1338 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1339 return "report audio status";
1340 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1341 return "set system audio mode";
1342 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1343 return "system audio mode request";
1344 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1345 return "system audio mode status";
1346 case CEC_OPCODE_SET_AUDIO_RATE
:
1347 return "set audio rate";
1348 case CEC_OPCODE_NONE
:
1355 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1359 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1361 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1368 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1370 // TODO this is a mask
1374 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1378 case CEC_VENDOR_SAMSUNG
:
1382 case CEC_VENDOR_PANASONIC
:
1384 case CEC_VENDOR_PIONEER
:
1386 case CEC_VENDOR_ONKYO
:
1388 case CEC_VENDOR_YAMAHA
:
1390 case CEC_VENDOR_PHILIPS
:
1392 case CEC_VENDOR_SONY
:
1394 case CEC_VENDOR_TOSHIBA
:
1401 const char *CCECProcessor::ToString(const cec_client_version version
)
1405 case CEC_CLIENT_VERSION_PRE_1_5
:
1407 case CEC_CLIENT_VERSION_1_5_0
:
1409 case CEC_CLIENT_VERSION_1_5_1
:
1411 case CEC_CLIENT_VERSION_1_5_2
:
1413 case CEC_CLIENT_VERSION_1_5_3
:
1415 case CEC_CLIENT_VERSION_1_6_0
:
1417 case CEC_CLIENT_VERSION_1_6_1
:
1424 const char *CCECProcessor::ToString(const cec_server_version version
)
1428 case CEC_SERVER_VERSION_PRE_1_5
:
1430 case CEC_SERVER_VERSION_1_5_0
:
1432 case CEC_SERVER_VERSION_1_5_1
:
1434 case CEC_SERVER_VERSION_1_5_2
:
1436 case CEC_SERVER_VERSION_1_5_3
:
1438 case CEC_SERVER_VERSION_1_6_0
:
1440 case CEC_SERVER_VERSION_1_6_1
:
1447 void *CCECBusScan::Process(void)
1449 CCECBusDevice
*device(NULL
);
1450 uint8_t iCounter(0);
1452 while (!IsStopped())
1454 if (++iCounter
< 10)
1459 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1461 device
= m_processor
->m_busDevices
[iPtr
];
1463 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1467 device
->GetVendorId();
1471 device
->GetPowerStatus(true);
1479 void CCECBusScan::WaitUntilIdle(void)
1484 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1485 while (iWaitTime
> 0)
1488 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1492 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1494 if (!m_communication
&& strPort
)
1496 bool bReturn(false);
1497 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1498 CTimeout
timeout(10000);
1500 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
1502 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1508 bReturn
= comm
->StartBootloader();
1515 return m_communication
->StartBootloader();
1519 bool CCECProcessor::PingAdapter(void)
1521 return m_communication
->PingAdapter();
1524 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1526 if (destination
< CECDEVICE_BROADCAST
)
1527 m_busDevices
[destination
]->HandlePollFrom(initiator
);
1530 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1532 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1535 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1537 // stream path changes are sent by the TV
1538 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1541 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1543 bool bReinit(false);
1544 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1545 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1546 m_configuration
.clientVersion
= configuration
->clientVersion
;
1547 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString((cec_client_version
)configuration
->clientVersion
));
1549 // client version 1.5.0
1552 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1553 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1554 if (bDeviceTypeChanged
)
1555 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(configuration
->deviceTypes
[0]));
1557 bool bPhysicalAddressChanged(false);
1559 // autodetect address
1560 bool bPhysicalAutodetected(false);
1561 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1563 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1564 if (iPhysicalAddress
!= 0)
1567 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1569 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%x'", __FUNCTION__
, iPhysicalAddress
);
1570 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1571 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1572 m_configuration
.iHDMIPort
= 0;
1573 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1574 bPhysicalAutodetected
= true;
1579 if (!bPhysicalAutodetected
)
1581 if (configuration
->iPhysicalAddress
!= 0)
1582 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1583 if (bPhysicalAddressChanged
)
1585 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - physical address '%x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1586 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1590 bool bHdmiPortChanged(false);
1591 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1594 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1595 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1596 m_configuration
.baseDevice
= configuration
->baseDevice
;
1599 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1600 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1601 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1605 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1606 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1607 m_configuration
.iHDMIPort
= 0;
1610 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1613 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, configuration
->strDeviceName
);
1614 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1615 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1617 primary
->SetOSDName(m_configuration
.strDeviceName
);
1618 if (!bReinit
&& IsRunning())
1619 primary
->TransmitOSDName(CECDEVICE_TV
);
1622 // tv vendor id override
1623 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString((cec_vendor_id
)configuration
->tvVendor
));
1624 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1626 m_configuration
.tvVendor
= configuration
->tvVendor
;
1627 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1631 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1633 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1634 if (!bReinit
&& IsRunning())
1639 m_configuration
.bUseTVMenuLanguage
= configuration
->bUseTVMenuLanguage
;
1640 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1641 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1642 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1643 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1644 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1646 // client version 1.5.1
1647 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1648 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1650 // client version 1.6.0
1651 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1653 m_configuration
.bPowerOffDevicesOnStandby
= configuration
->bPowerOffDevicesOnStandby
;
1654 m_configuration
.bShutdownOnStandby
= configuration
->bShutdownOnStandby
;
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
->GetFirmwareVersion() >= 2;
1728 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1730 return m_communication
->PersistConfiguration(configuration
);
1733 void CCECProcessor::RescanActiveDevices(void)
1735 for (unsigned int iPtr
= 0; iPtr
< 16; 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
;