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 #define CEC_PROCESSOR_SIGNAL_WAIT_TIME 1000
52 CCECProcessor::CCECProcessor(CLibCEC
*controller
, libcec_configuration
*configuration
) :
53 m_bConnectionOpened(false),
54 m_bInitialised(false),
55 m_communication(NULL
),
56 m_controller(controller
),
58 m_iStandardLineTimeout(3),
59 m_iRetryLineTimeout(3),
60 m_iLastTransmission(0)
63 m_configuration
.Clear();
64 m_configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
65 SetConfiguration(configuration
);
67 if (m_configuration
.tvVendor
!= CEC_VENDOR_UNKNOWN
)
68 m_busDevices
[CECDEVICE_TV
]->ReplaceHandler(false);
71 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
, uint16_t iPhysicalAddress
) :
72 m_bConnectionOpened(false),
73 m_bInitialised(false),
74 m_communication(NULL
),
75 m_controller(controller
),
77 m_iStandardLineTimeout(3),
78 m_iRetryLineTimeout(3),
79 m_iLastTransmission(0)
81 m_configuration
.Clear();
82 m_configuration
.serverVersion
= LIBCEC_VERSION_CURRENT
;
84 // client version < 1.5.0
85 m_configuration
.clientVersion
= (uint32_t)CEC_CLIENT_VERSION_PRE_1_5
;
86 snprintf(m_configuration
.strDeviceName
, 13, "%s", strDeviceName
);
87 m_configuration
.deviceTypes
= types
;
88 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
89 m_configuration
.baseDevice
= (cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
;
90 m_configuration
.iHDMIPort
= CEC_DEFAULT_HDMI_PORT
;
92 if (m_configuration
.deviceTypes
.IsEmpty())
93 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
97 void CCECProcessor::CreateBusDevices(void)
99 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
103 case CECDEVICE_AUDIOSYSTEM
:
104 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, CEC_INVALID_PHYSICAL_ADDRESS
);
106 case CECDEVICE_PLAYBACKDEVICE1
:
107 case CECDEVICE_PLAYBACKDEVICE2
:
108 case CECDEVICE_PLAYBACKDEVICE3
:
109 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, CEC_INVALID_PHYSICAL_ADDRESS
);
111 case CECDEVICE_RECORDINGDEVICE1
:
112 case CECDEVICE_RECORDINGDEVICE2
:
113 case CECDEVICE_RECORDINGDEVICE3
:
114 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, CEC_INVALID_PHYSICAL_ADDRESS
);
116 case CECDEVICE_TUNER1
:
117 case CECDEVICE_TUNER2
:
118 case CECDEVICE_TUNER3
:
119 case CECDEVICE_TUNER4
:
120 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, CEC_INVALID_PHYSICAL_ADDRESS
);
123 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, CEC_PHYSICAL_ADDRESS_TV
);
126 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, CEC_INVALID_PHYSICAL_ADDRESS
);
132 CCECProcessor::~CCECProcessor(void)
136 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
138 delete m_busDevices
[iPtr
];
139 m_busDevices
[iPtr
] = NULL
;
143 void CCECProcessor::Close(void)
146 SetInitialised(false);
151 CLockObject
lock(m_mutex
);
152 bClose
= m_bConnectionOpened
;
153 m_bConnectionOpened
= false;
156 if (bClose
&& m_communication
)
158 delete m_communication
;
159 m_communication
= NULL
;
163 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
, bool bStartListening
/* = true */)
169 CLockObject
lock(m_mutex
);
170 if (m_bConnectionOpened
)
172 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
175 m_communication
= new CUSBCECAdapterCommunication(this, strPort
, iBaudRate
);
176 m_bConnectionOpened
= (m_communication
!= NULL
);
179 CTimeout
timeout(iTimeoutMs
> 0 ? iTimeoutMs
: CEC_DEFAULT_TRANSMIT_WAIT
);
181 /* open a new connection */
182 unsigned iConnectTry(0);
183 while (timeout
.TimeLeft() > 0 && (bReturn
= m_communication
->Open((timeout
.TimeLeft() / CEC_CONNECT_TRIES
), false, bStartListening
)) == false)
185 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
186 m_communication
->Close();
187 CEvent::Sleep(CEC_DEFAULT_CONNECT_RETRY_WAIT
);
192 m_configuration
.iFirmwareVersion
= m_communication
->GetFirmwareVersion();
193 m_configuration
.iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
195 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
);
196 if (m_configuration
.iFirmwareBuildDate
!= CEC_DEFAULT_FIRMWARE_BUILD_DATE
)
198 time_t buildTime
= (time_t)m_configuration
.iFirmwareBuildDate
;
199 strLog
.AppendFormat(", firmware build date: %s", asctime(gmtime(&buildTime
)));
200 strLog
= strLog
.Left((int)strLog
.length() - 1); // strip \n added by asctime
201 strLog
.append(" +0000");
203 CLibCEC::AddLog(CEC_LOG_NOTICE
, strLog
);
206 if (m_configuration
.bGetSettingsFromROM
== 1)
208 libcec_configuration config
;
210 m_communication
->GetConfiguration(&config
);
212 CLockObject
lock(m_mutex
);
213 if (!config
.deviceTypes
.IsEmpty())
214 m_configuration
.deviceTypes
= config
.deviceTypes
;
215 if (config
.iPhysicalAddress
> 0)
216 m_configuration
.iPhysicalAddress
= config
.iPhysicalAddress
;
217 snprintf(m_configuration
.strDeviceName
, 13, "%s", config
.strDeviceName
);
223 bool CCECProcessor::IsInitialised(void)
225 CLockObject
lock(m_threadMutex
);
226 return m_bInitialised
;
229 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
231 CLockObject
lock(m_mutex
);
232 m_bInitialised
= bSetTo
;
235 bool CCECProcessor::Initialise(void)
239 CLockObject
lock(m_mutex
);
240 if (!m_configuration
.logicalAddresses
.IsEmpty())
241 m_configuration
.logicalAddresses
.Clear();
243 if (!FindLogicalAddresses())
245 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
249 /* only set our OSD name for the primary device */
250 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_strDeviceName
= m_configuration
.strDeviceName
;
252 /* make the primary device the active source if the option is set */
253 if (m_configuration
.bActivateSource
== 1)
254 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_bActiveSource
= true;
256 /* set the default menu language for devices we control */
257 cec_menu_language language
;
258 language
.device
= m_configuration
.logicalAddresses
.primary
;
259 memcpy(language
.language
, m_configuration
.strDeviceLanguage
, 3);
260 language
.language
[3] = 0;
262 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
264 if (m_configuration
.logicalAddresses
[iPtr
])
266 language
.device
= (cec_logical_address
) iPtr
;
267 m_busDevices
[iPtr
]->SetMenuLanguage(language
);
272 /* get the vendor id from the TV, so we are using the correct handler */
273 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
275 if (m_configuration
.iPhysicalAddress
!= 0)
277 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_configuration
.iPhysicalAddress
);
278 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->m_iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
279 if ((bReturn
= m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
280 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_configuration
.iPhysicalAddress
);
282 else if (m_configuration
.iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
, true)) == false)
283 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
);
285 if (bReturn
&& m_configuration
.bActivateSource
== 1)
286 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->ActivateSource();
288 SetInitialised(bReturn
);
290 CLibCEC::ConfigurationChanged(m_configuration
);
295 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = CEC_SERIAL_DEFAULT_BAUDRATE */, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
300 CLockObject
lock(m_mutex
);
301 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
304 /* create the processor thread */
307 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
312 if ((bReturn
= Initialise()) == false)
314 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
319 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
325 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
327 if (m_busDevices
[address
]->TryLogicalAddress())
329 m_configuration
.logicalAddresses
.Set(address
);
336 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
338 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
339 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
340 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
341 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
344 bool CCECProcessor::FindLogicalAddressTuner(void)
346 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
347 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
348 TryLogicalAddress(CECDEVICE_TUNER2
) ||
349 TryLogicalAddress(CECDEVICE_TUNER3
) ||
350 TryLogicalAddress(CECDEVICE_TUNER4
);
353 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
355 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
356 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
357 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
358 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
361 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
363 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
364 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
367 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
369 bool bChanged(false);
371 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
373 CLockObject
lock(m_mutex
);
374 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
375 m_configuration
.logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
377 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
379 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
382 if (m_configuration
.deviceTypes
.types
[iPtr
] == from
)
385 m_configuration
.deviceTypes
.types
[iPtr
] = to
;
387 else if (m_configuration
.deviceTypes
.types
[iPtr
] == to
&& bChanged
)
389 m_configuration
.deviceTypes
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
395 FindLogicalAddresses();
397 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
398 if (previousDevice
&& newDevice
)
400 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
401 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
403 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
404 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
406 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
408 newDevice
->SetMenuState(previousDevice
->GetMenuState());
409 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
411 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
412 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
414 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress());
415 previousDevice
->SetPhysicalAddress(CEC_INVALID_PHYSICAL_ADDRESS
);
417 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
418 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
420 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
421 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
423 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
424 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
426 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
427 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
429 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
430 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
438 bool CCECProcessor::FindLogicalAddresses(void)
441 m_configuration
.logicalAddresses
.Clear();
443 if (m_configuration
.deviceTypes
.IsEmpty())
445 CLibCEC::AddLog(CEC_LOG_ERROR
, "no device types set");
449 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
451 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
454 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_configuration
.deviceTypes
.types
[iPtr
]);
456 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
457 bReturn
&= FindLogicalAddressRecordingDevice();
458 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
459 bReturn
&= FindLogicalAddressTuner();
460 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
461 bReturn
&= FindLogicalAddressPlaybackDevice();
462 if (m_configuration
.deviceTypes
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
463 bReturn
&= FindLogicalAddressAudioSystem();
467 SetAckMask(m_configuration
.logicalAddresses
.AckMask());
472 void CCECProcessor::ReplaceHandlers(void)
474 if (!IsInitialised())
476 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
477 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
480 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
482 return m_inBuffer
.Push(command
);
485 void *CCECProcessor::Process(void)
487 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
492 while (!IsStopped() && m_communication
->IsOpen())
494 if (m_inBuffer
.Pop(command
, CEC_PROCESSOR_SIGNAL_WAIT_TIME
))
495 ParseCommand(command
);
501 m_controller
->CheckKeypressTimeout();
508 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
515 cec_logical_address addr
= m_configuration
.logicalAddresses
.primary
;
517 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
519 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
521 if (m_configuration
.logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
523 addr
= (cec_logical_address
) iPtr
;
529 m_busDevices
[addr
]->SetActiveSource();
530 if (m_busDevices
[addr
]->GetPhysicalAddress() != CEC_INVALID_PHYSICAL_ADDRESS
)
531 bReturn
= m_busDevices
[addr
]->ActivateSource();
536 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
540 // suppress polls when searching for a device
541 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
544 device
->SetActiveSource();
549 CLibCEC::AddLog(CEC_LOG_DEBUG
, "device with PA '%04x' not found", iStreamPath
);
555 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
557 CLockObject
lock(m_mutex
);
558 m_iStandardLineTimeout
= iTimeout
;
561 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
563 CLockObject
lock(m_mutex
);
564 m_iRetryLineTimeout
= iTimeout
;
567 bool CCECProcessor::SetActiveView(void)
569 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
570 return SetActiveSource(m_configuration
.deviceTypes
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_configuration
.deviceTypes
[0]);
573 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
577 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
580 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
582 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
589 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
593 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
596 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
598 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
605 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
609 // limit the HDMI port range to 1-15
616 CLockObject
lock(m_mutex
);
617 m_configuration
.baseDevice
= iBaseDevice
;
618 m_configuration
.iHDMIPort
= iPort
;
621 if (!IsRunning() && !bForce
)
624 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
626 uint16_t iPhysicalAddress(0);
627 if (iBaseDevice
> CECDEVICE_TV
)
629 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
632 if (iPhysicalAddress
<= CEC_MAX_PHYSICAL_ADDRESS
)
634 if (iPhysicalAddress
== 0)
635 iPhysicalAddress
+= 0x1000 * iPort
;
636 else if (iPhysicalAddress
% 0x1000 == 0)
637 iPhysicalAddress
+= 0x100 * iPort
;
638 else if (iPhysicalAddress
% 0x100 == 0)
639 iPhysicalAddress
+= 0x10 * iPort
;
640 else if (iPhysicalAddress
% 0x10 == 0)
641 iPhysicalAddress
+= iPort
;
647 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
649 SetPhysicalAddress(iPhysicalAddress
);
654 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
656 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
658 if (m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
664 bool CCECProcessor::TransmitInactiveSource(void)
669 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
670 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitInactiveSource();
674 void CCECProcessor::LogOutput(const cec_command
&data
)
677 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
679 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
681 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
682 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
683 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
686 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
688 CLockObject
lock(m_mutex
);
689 if (m_configuration
.logicalAddresses
.primary
!= iLogicalAddress
)
691 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
692 m_configuration
.logicalAddresses
.primary
= iLogicalAddress
;
693 m_configuration
.logicalAddresses
.Set(iLogicalAddress
);
694 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
700 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
702 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
704 if (m_configuration
.logicalAddresses
[iPtr
])
705 m_busDevices
[iPtr
]->SetMenuState(state
);
709 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
714 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
716 bool bSendActiveView(false);
718 cec_logical_addresses sendUpdatesTo
;
719 sendUpdatesTo
.Clear();
722 CLockObject
lock(m_mutex
);
723 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
724 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting physical address to '%4x'", iPhysicalAddress
);
726 if (!m_configuration
.logicalAddresses
.IsEmpty())
728 bool bWasActiveSource(false);
729 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
730 if (m_configuration
.logicalAddresses
[iPtr
])
732 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
733 m_busDevices
[iPtr
]->SetInactiveSource();
734 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
736 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
739 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
744 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
745 if (sendUpdatesTo
[iPtr
])
746 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
753 libcec_configuration config
;
755 CLockObject
lock(m_mutex
);
756 config
= m_configuration
;
759 PersistConfiguration(&config
);
760 CLibCEC::ConfigurationChanged(config
);
766 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
768 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
771 CLockObject
lock(m_mutex
);
772 m_bMonitor
= bEnable
;
776 return SetAckMask(0);
778 return SetAckMask(m_configuration
.logicalAddresses
.AckMask());
781 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
783 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
785 return m_configuration
.logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
786 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
787 m_busDevices
[m_configuration
.logicalAddresses
.primary
]->TransmitPoll(iAddress
);
792 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
795 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
796 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
801 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
804 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
805 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
810 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
813 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
814 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
819 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
821 CCECBusDevice
*device(NULL
);
824 if (iPhysicalAddress
== CEC_INVALID_PHYSICAL_ADDRESS
)
827 // check each device until we found a match
828 for (unsigned int iPtr
= 0; !device
&& iPtr
< 16; iPtr
++)
830 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bSuppressUpdate
) == iPhysicalAddress
)
831 device
= m_busDevices
[iPtr
];
837 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
839 CCECBusDevice
*device
= NULL
;
841 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
843 if (m_busDevices
[iPtr
]->m_type
== type
&& m_configuration
.logicalAddresses
[iPtr
])
845 device
= m_busDevices
[iPtr
];
853 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
855 CCECBusDevice
*device(NULL
);
856 cec_logical_address primary
= m_configuration
.logicalAddresses
.primary
;
857 if (primary
!= CECDEVICE_UNKNOWN
)
858 device
= m_busDevices
[primary
];
862 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
864 return m_busDevices
[iAddress
]->GetCecVersion();
867 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
869 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
872 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
873 retVal
.device
= iAddress
;
878 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
880 if (m_busDevices
[iAddress
])
882 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
883 return (strcmp(language
->language
, "???") != 0);
888 CStdString
CCECProcessor::GetDeviceName(void) const
891 strName
= m_configuration
.strDeviceName
;
895 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
897 if (m_busDevices
[iAddress
])
898 return m_busDevices
[iAddress
]->GetVendorId();
902 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
904 if (m_busDevices
[iAddress
])
905 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
909 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
911 if (m_busDevices
[iAddress
])
912 return m_busDevices
[iAddress
]->GetPowerStatus();
913 return CEC_POWER_STATUS_UNKNOWN
;
916 cec_logical_address
CCECProcessor::GetActiveSource(bool bRequestActiveSource
/* = true */)
918 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
920 if (m_busDevices
[iPtr
]->IsActiveSource())
921 return (cec_logical_address
)iPtr
;
924 if (bRequestActiveSource
&& m_configuration
.logicalAddresses
.primary
!= CECDEVICE_UNKNOWN
)
926 CCECBusDevice
*primary
= m_busDevices
[m_configuration
.logicalAddresses
.primary
];
928 primary
->RequestActiveSource();
930 return GetActiveSource(false);
933 return CECDEVICE_UNKNOWN
;
936 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
938 return iAddress
>= CECDEVICE_TV
&& iAddress
< CECDEVICE_BROADCAST
?
939 m_busDevices
[iAddress
]->IsActiveSource() :
943 bool CCECProcessor::Transmit(const cec_command
&data
)
945 if (m_configuration
.logicalAddresses
[(uint8_t)data
.destination
])
947 CLibCEC::AddLog(CEC_LOG_WARNING
, "not sending data to myself!");
951 uint8_t iMaxTries(0);
953 CLockObject
lock(m_mutex
);
957 m_iLastTransmission
= GetTimeMs();
958 if (!m_communication
|| !m_communication
->IsOpen())
960 CLibCEC::AddLog(CEC_LOG_ERROR
, "cannot transmit command: connection closed");
963 iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
968 uint8_t iLineTimeout
= m_iStandardLineTimeout
;
969 cec_adapter_message_state adapterState
= ADAPTER_MESSAGE_STATE_UNKNOWN
;
971 while (bRetry
&& ++iTries
< iMaxTries
)
973 if (m_busDevices
[data
.initiator
]->IsUnsupportedFeature(data
.opcode
))
976 adapterState
= m_communication
->Write(data
, bRetry
, iLineTimeout
);
977 iLineTimeout
= m_iRetryLineTimeout
;
980 return adapterState
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
983 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
985 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
989 cec_command::Format(command
, m_configuration
.logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
990 command
.parameters
.PushBack((uint8_t)opcode
);
991 command
.parameters
.PushBack((uint8_t)reason
);
996 void CCECProcessor::ParseCommand(const cec_command
&command
)
999 dataStr
.Format(">> %1x%1x", command
.initiator
, command
.destination
);
1000 if (command
.opcode_set
== 1)
1001 dataStr
.AppendFormat(":%02x", command
.opcode
);
1002 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
1003 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
1004 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
1006 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
1007 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
1010 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
1012 cec_logical_addresses addresses
;
1014 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1016 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1017 addresses
.Set((cec_logical_address
) iPtr
);
1022 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
1024 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
1027 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
1029 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1031 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1038 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1040 if (!m_configuration
.logicalAddresses
.IsEmpty() && m_busDevices
[m_configuration
.logicalAddresses
.primary
])
1041 return m_busDevices
[m_configuration
.logicalAddresses
.primary
]->GetPhysicalAddress();
1045 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1047 return m_communication
? m_communication
->SetAckMask(iMask
) : false;
1050 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1052 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1055 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1057 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1060 bool CCECProcessor::EnablePhysicalAddressDetection(void)
1062 CLibCEC::AddLog(CEC_LOG_WARNING
, "deprecated method %s called", __FUNCTION__
);
1063 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1064 if (iPhysicalAddress
!= 0)
1066 m_configuration
.bAutodetectAddress
= 1;
1067 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1068 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1069 m_configuration
.iHDMIPort
= 0;
1070 return SetPhysicalAddress(iPhysicalAddress
);
1075 bool CCECProcessor::StandbyDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1077 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1080 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1082 if (m_configuration
.powerOffDevices
[iPtr
])
1084 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - putting '%s' in standby mode", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1085 bReturn
&= m_busDevices
[iPtr
]->Standby();
1091 return m_busDevices
[address
]->Standby();
1094 bool CCECProcessor::PowerOnDevices(cec_logical_address address
/* = CECDEVICE_BROADCAST */)
1096 if (address
== CECDEVICE_BROADCAST
&& m_configuration
.clientVersion
>= CEC_CLIENT_VERSION_1_5_0
)
1099 for (uint8_t iPtr
= 0; iPtr
<= 0xF; iPtr
++)
1101 if (m_configuration
.wakeDevices
[iPtr
])
1103 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - powering on '%s'", __FUNCTION__
, ToString((cec_logical_address
)iPtr
));
1104 bReturn
&= m_busDevices
[iPtr
]->PowerOn();
1110 return m_busDevices
[address
]->PowerOn();
1113 const char *CCECProcessor::ToString(const cec_device_type type
)
1117 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1118 return "audio system";
1119 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1120 return "playback device";
1121 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1122 return "recording device";
1123 case CEC_DEVICE_TYPE_RESERVED
:
1125 case CEC_DEVICE_TYPE_TUNER
:
1127 case CEC_DEVICE_TYPE_TV
:
1134 const char *CCECProcessor::ToString(const cec_menu_state state
)
1138 case CEC_MENU_STATE_ACTIVATED
:
1140 case CEC_MENU_STATE_DEACTIVATED
:
1141 return "deactivated";
1147 const char *CCECProcessor::ToString(const cec_version version
)
1151 case CEC_VERSION_1_2
:
1153 case CEC_VERSION_1_2A
:
1155 case CEC_VERSION_1_3
:
1157 case CEC_VERSION_1_3A
:
1159 case CEC_VERSION_1_4
:
1166 const char *CCECProcessor::ToString(const cec_power_status status
)
1170 case CEC_POWER_STATUS_ON
:
1172 case CEC_POWER_STATUS_STANDBY
:
1174 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1175 return "in transition from on to standby";
1176 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1177 return "in transition from standby to on";
1183 const char *CCECProcessor::ToString(const cec_logical_address address
)
1187 case CECDEVICE_AUDIOSYSTEM
:
1189 case CECDEVICE_BROADCAST
:
1191 case CECDEVICE_FREEUSE
:
1193 case CECDEVICE_PLAYBACKDEVICE1
:
1194 return "Playback 1";
1195 case CECDEVICE_PLAYBACKDEVICE2
:
1196 return "Playback 2";
1197 case CECDEVICE_PLAYBACKDEVICE3
:
1198 return "Playback 3";
1199 case CECDEVICE_RECORDINGDEVICE1
:
1200 return "Recorder 1";
1201 case CECDEVICE_RECORDINGDEVICE2
:
1202 return "Recorder 2";
1203 case CECDEVICE_RECORDINGDEVICE3
:
1204 return "Recorder 3";
1205 case CECDEVICE_RESERVED1
:
1206 return "Reserved 1";
1207 case CECDEVICE_RESERVED2
:
1208 return "Reserved 2";
1209 case CECDEVICE_TUNER1
:
1211 case CECDEVICE_TUNER2
:
1213 case CECDEVICE_TUNER3
:
1215 case CECDEVICE_TUNER4
:
1224 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1228 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1229 return "skip forward wind";
1230 case CEC_DECK_CONTROL_MODE_EJECT
:
1232 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1233 return "reverse rewind";
1234 case CEC_DECK_CONTROL_MODE_STOP
:
1241 const char *CCECProcessor::ToString(const cec_deck_info status
)
1245 case CEC_DECK_INFO_PLAY
:
1247 case CEC_DECK_INFO_RECORD
:
1249 case CEC_DECK_INFO_PLAY_REVERSE
:
1250 return "play reverse";
1251 case CEC_DECK_INFO_STILL
:
1253 case CEC_DECK_INFO_SLOW
:
1255 case CEC_DECK_INFO_SLOW_REVERSE
:
1256 return "slow reverse";
1257 case CEC_DECK_INFO_FAST_FORWARD
:
1258 return "fast forward";
1259 case CEC_DECK_INFO_FAST_REVERSE
:
1260 return "fast reverse";
1261 case CEC_DECK_INFO_NO_MEDIA
:
1263 case CEC_DECK_INFO_STOP
:
1265 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1266 return "info skip forward wind";
1267 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1268 return "info skip reverse rewind";
1269 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1270 return "info index search forward";
1271 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1272 return "info index search reverse";
1273 case CEC_DECK_INFO_OTHER_STATUS
:
1280 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1284 case CEC_OPCODE_ACTIVE_SOURCE
:
1285 return "active source";
1286 case CEC_OPCODE_IMAGE_VIEW_ON
:
1287 return "image view on";
1288 case CEC_OPCODE_TEXT_VIEW_ON
:
1289 return "text view on";
1290 case CEC_OPCODE_INACTIVE_SOURCE
:
1291 return "inactive source";
1292 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1293 return "request active source";
1294 case CEC_OPCODE_ROUTING_CHANGE
:
1295 return "routing change";
1296 case CEC_OPCODE_ROUTING_INFORMATION
:
1297 return "routing information";
1298 case CEC_OPCODE_SET_STREAM_PATH
:
1299 return "set stream path";
1300 case CEC_OPCODE_STANDBY
:
1302 case CEC_OPCODE_RECORD_OFF
:
1303 return "record off";
1304 case CEC_OPCODE_RECORD_ON
:
1306 case CEC_OPCODE_RECORD_STATUS
:
1307 return "record status";
1308 case CEC_OPCODE_RECORD_TV_SCREEN
:
1309 return "record tv screen";
1310 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1311 return "clear analogue timer";
1312 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1313 return "clear digital timer";
1314 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1315 return "clear external timer";
1316 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1317 return "set analogue timer";
1318 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1319 return "set digital timer";
1320 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1321 return "set external timer";
1322 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1323 return "set timer program title";
1324 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1325 return "timer cleared status";
1326 case CEC_OPCODE_TIMER_STATUS
:
1327 return "timer status";
1328 case CEC_OPCODE_CEC_VERSION
:
1329 return "cec version";
1330 case CEC_OPCODE_GET_CEC_VERSION
:
1331 return "get cec version";
1332 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1333 return "give physical address";
1334 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1335 return "get menu language";
1336 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1337 return "report physical address";
1338 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1339 return "set menu language";
1340 case CEC_OPCODE_DECK_CONTROL
:
1341 return "deck control";
1342 case CEC_OPCODE_DECK_STATUS
:
1343 return "deck status";
1344 case CEC_OPCODE_GIVE_DECK_STATUS
:
1345 return "give deck status";
1346 case CEC_OPCODE_PLAY
:
1348 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1349 return "give tuner status";
1350 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1351 return "select analogue service";
1352 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1353 return "set digital service";
1354 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1355 return "tuner device status";
1356 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1357 return "tuner step decrement";
1358 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1359 return "tuner step increment";
1360 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1361 return "device vendor id";
1362 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1363 return "give device vendor id";
1364 case CEC_OPCODE_VENDOR_COMMAND
:
1365 return "vendor command";
1366 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1367 return "vendor command with id";
1368 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1369 return "vendor remote button down";
1370 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1371 return "vendor remote button up";
1372 case CEC_OPCODE_SET_OSD_STRING
:
1373 return "set osd string";
1374 case CEC_OPCODE_GIVE_OSD_NAME
:
1375 return "give osd name";
1376 case CEC_OPCODE_SET_OSD_NAME
:
1377 return "set osd name";
1378 case CEC_OPCODE_MENU_REQUEST
:
1379 return "menu request";
1380 case CEC_OPCODE_MENU_STATUS
:
1381 return "menu status";
1382 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1383 return "user control pressed";
1384 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1385 return "user control release";
1386 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1387 return "give device power status";
1388 case CEC_OPCODE_REPORT_POWER_STATUS
:
1389 return "report power status";
1390 case CEC_OPCODE_FEATURE_ABORT
:
1391 return "feature abort";
1392 case CEC_OPCODE_ABORT
:
1394 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1395 return "give audio status";
1396 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1397 return "give audio mode status";
1398 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1399 return "report audio status";
1400 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1401 return "set system audio mode";
1402 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1403 return "system audio mode request";
1404 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1405 return "system audio mode status";
1406 case CEC_OPCODE_SET_AUDIO_RATE
:
1407 return "set audio rate";
1408 case CEC_OPCODE_NONE
:
1415 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1419 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1421 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1428 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1430 // TODO this is a mask
1434 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1438 case CEC_VENDOR_SAMSUNG
:
1442 case CEC_VENDOR_PANASONIC
:
1444 case CEC_VENDOR_PIONEER
:
1446 case CEC_VENDOR_ONKYO
:
1448 case CEC_VENDOR_YAMAHA
:
1450 case CEC_VENDOR_PHILIPS
:
1452 case CEC_VENDOR_SONY
:
1454 case CEC_VENDOR_TOSHIBA
:
1461 const char *CCECProcessor::ToString(const cec_client_version version
)
1465 case CEC_CLIENT_VERSION_PRE_1_5
:
1467 case CEC_CLIENT_VERSION_1_5_0
:
1469 case CEC_CLIENT_VERSION_1_5_1
:
1471 case CEC_CLIENT_VERSION_1_5_2
:
1473 case CEC_CLIENT_VERSION_1_5_3
:
1475 case CEC_CLIENT_VERSION_1_6_0
:
1477 case CEC_CLIENT_VERSION_1_6_1
:
1479 case CEC_CLIENT_VERSION_1_6_2
:
1486 const char *CCECProcessor::ToString(const cec_server_version version
)
1490 case CEC_SERVER_VERSION_PRE_1_5
:
1492 case CEC_SERVER_VERSION_1_5_0
:
1494 case CEC_SERVER_VERSION_1_5_1
:
1496 case CEC_SERVER_VERSION_1_5_2
:
1498 case CEC_SERVER_VERSION_1_5_3
:
1500 case CEC_SERVER_VERSION_1_6_0
:
1502 case CEC_SERVER_VERSION_1_6_1
:
1504 case CEC_SERVER_VERSION_1_6_2
:
1511 bool CCECProcessor::StartBootloader(const char *strPort
/* = NULL */)
1513 if (!m_communication
&& strPort
)
1515 bool bReturn(false);
1516 IAdapterCommunication
*comm
= new CUSBCECAdapterCommunication(this, strPort
);
1517 CTimeout
timeout(CEC_DEFAULT_CONNECT_TIMEOUT
);
1519 while (timeout
.TimeLeft() > 0 && (bReturn
= comm
->Open(timeout
.TimeLeft() / CEC_CONNECT_TRIES
, true)) == false)
1521 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
1523 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
1527 bReturn
= comm
->StartBootloader();
1534 return m_communication
->StartBootloader();
1538 bool CCECProcessor::PingAdapter(void)
1540 return m_communication
->PingAdapter();
1543 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1545 if (destination
< CECDEVICE_BROADCAST
)
1546 m_busDevices
[destination
]->HandlePollFrom(initiator
);
1549 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1551 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1554 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1556 // stream path changes are sent by the TV
1557 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1560 bool CCECProcessor::SetConfiguration(const libcec_configuration
*configuration
)
1562 bool bReinit(false);
1563 CCECBusDevice
*primary
= IsRunning() ? GetPrimaryDevice() : NULL
;
1564 cec_device_type oldPrimaryType
= primary
? primary
->GetType() : CEC_DEVICE_TYPE_RECORDING_DEVICE
;
1565 m_configuration
.clientVersion
= configuration
->clientVersion
;
1566 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using client version '%s'", __FUNCTION__
, ToString((cec_client_version
)configuration
->clientVersion
));
1568 // client version 1.5.0
1571 bool bDeviceTypeChanged
= IsRunning () && m_configuration
.deviceTypes
!= configuration
->deviceTypes
;
1572 m_configuration
.deviceTypes
= configuration
->deviceTypes
;
1573 if (bDeviceTypeChanged
)
1574 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using primary device type '%s'", __FUNCTION__
, ToString(configuration
->deviceTypes
[0]));
1576 bool bPhysicalAddressChanged(false);
1578 // autodetect address
1579 bool bPhysicalAutodetected(false);
1580 if (IsRunning() && configuration
->bAutodetectAddress
== 1)
1582 uint16_t iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1583 if (iPhysicalAddress
!= 0)
1586 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - autodetected physical address '%4x'", __FUNCTION__
, iPhysicalAddress
);
1588 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using physical address '%x'", __FUNCTION__
, iPhysicalAddress
);
1589 bPhysicalAddressChanged
= (m_configuration
.iPhysicalAddress
!= iPhysicalAddress
);
1590 m_configuration
.iPhysicalAddress
= iPhysicalAddress
;
1591 m_configuration
.iHDMIPort
= 0;
1592 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1593 bPhysicalAutodetected
= true;
1598 if (!bPhysicalAutodetected
)
1600 if (configuration
->iPhysicalAddress
!= 0)
1601 bPhysicalAddressChanged
= IsRunning() && m_configuration
.iPhysicalAddress
!= configuration
->iPhysicalAddress
;
1602 if (bPhysicalAddressChanged
)
1604 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - physical address '%x'", __FUNCTION__
, configuration
->iPhysicalAddress
);
1605 m_configuration
.iPhysicalAddress
= configuration
->iPhysicalAddress
;
1609 bool bHdmiPortChanged(false);
1610 if (!bPhysicalAutodetected
&& !bPhysicalAddressChanged
)
1613 bHdmiPortChanged
= IsRunning() && m_configuration
.baseDevice
!= configuration
->baseDevice
;
1614 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using base device '%x'", __FUNCTION__
, (int)configuration
->baseDevice
);
1615 m_configuration
.baseDevice
= configuration
->baseDevice
;
1618 bHdmiPortChanged
|= IsRunning() && m_configuration
.iHDMIPort
!= configuration
->iHDMIPort
;
1619 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using HDMI port '%d'", __FUNCTION__
, configuration
->iHDMIPort
);
1620 m_configuration
.iHDMIPort
= configuration
->iHDMIPort
;
1624 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - resetting HDMI port and base device to defaults", __FUNCTION__
);
1625 m_configuration
.baseDevice
= CECDEVICE_UNKNOWN
;
1626 m_configuration
.iHDMIPort
= 0;
1629 bReinit
= bPhysicalAddressChanged
|| bHdmiPortChanged
|| bDeviceTypeChanged
;
1632 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - using OSD name '%s'", __FUNCTION__
, configuration
->strDeviceName
);
1633 snprintf(m_configuration
.strDeviceName
, 13, "%s", configuration
->strDeviceName
);
1634 if (primary
&& !primary
->GetOSDName().Equals(m_configuration
.strDeviceName
))
1636 primary
->SetOSDName(m_configuration
.strDeviceName
);
1637 if (!bReinit
&& IsRunning())
1638 primary
->TransmitOSDName(CECDEVICE_TV
);
1641 // tv vendor id override
1642 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - vendor id '%s'", __FUNCTION__
, ToString((cec_vendor_id
)configuration
->tvVendor
));
1643 if (m_configuration
.tvVendor
!= configuration
->tvVendor
)
1645 m_configuration
.tvVendor
= configuration
->tvVendor
;
1646 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)m_configuration
.tvVendor
);
1650 if (m_configuration
.wakeDevices
!= configuration
->wakeDevices
)
1652 m_configuration
.wakeDevices
= configuration
->wakeDevices
;
1653 if (!bReinit
&& IsRunning())
1658 m_configuration
.bUseTVMenuLanguage
= configuration
->bUseTVMenuLanguage
;
1659 m_configuration
.bActivateSource
= configuration
->bActivateSource
;
1660 m_configuration
.bGetSettingsFromROM
= configuration
->bGetSettingsFromROM
;
1661 m_configuration
.powerOffDevices
= configuration
->powerOffDevices
;
1662 m_configuration
.bPowerOffScreensaver
= configuration
->bPowerOffScreensaver
;
1663 m_configuration
.bPowerOffOnStandby
= configuration
->bPowerOffOnStandby
;
1665 // client version 1.5.1
1666 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1667 m_configuration
.bSendInactiveSource
= configuration
->bSendInactiveSource
;
1669 // client version 1.6.0
1670 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1672 m_configuration
.bPowerOffDevicesOnStandby
= configuration
->bPowerOffDevicesOnStandby
;
1673 m_configuration
.bShutdownOnStandby
= configuration
->bShutdownOnStandby
;
1676 // client version 1.6.2
1677 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1679 memcpy(m_configuration
.strDeviceLanguage
, configuration
->strDeviceLanguage
, 3);
1682 // ensure that there is at least 1 device type set
1683 if (m_configuration
.deviceTypes
.IsEmpty())
1684 m_configuration
.deviceTypes
.Add(CEC_DEVICE_TYPE_RECORDING_DEVICE
);
1686 // persist the configuration
1688 m_communication
->PersistConfiguration(&m_configuration
);
1690 if (bReinit
|| m_configuration
.logicalAddresses
.IsEmpty())
1692 if (bDeviceTypeChanged
)
1693 return ChangeDeviceType(oldPrimaryType
, m_configuration
.deviceTypes
[0]);
1694 else if (bPhysicalAddressChanged
)
1695 return SetPhysicalAddress(m_configuration
.iPhysicalAddress
);
1697 return SetHDMIPort(m_configuration
.baseDevice
, m_configuration
.iHDMIPort
);
1699 else if (m_configuration
.bActivateSource
== 1 && IsRunning() && !IsActiveSource(m_configuration
.logicalAddresses
.primary
))
1701 // activate the source if we're not already the active source
1702 SetActiveSource(m_configuration
.deviceTypes
.types
[0]);
1708 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1710 // client version 1.5.0
1711 snprintf(configuration
->strDeviceName
, 13, "%s", m_configuration
.strDeviceName
);
1712 configuration
->deviceTypes
= m_configuration
.deviceTypes
;
1713 configuration
->bAutodetectAddress
= m_configuration
.bAutodetectAddress
;
1714 configuration
->iPhysicalAddress
= m_configuration
.iPhysicalAddress
;
1715 configuration
->baseDevice
= m_configuration
.baseDevice
;
1716 configuration
->iHDMIPort
= m_configuration
.iHDMIPort
;
1717 configuration
->clientVersion
= m_configuration
.clientVersion
;
1718 configuration
->serverVersion
= m_configuration
.serverVersion
;
1719 configuration
->tvVendor
= m_configuration
.tvVendor
;
1721 configuration
->bGetSettingsFromROM
= m_configuration
.bGetSettingsFromROM
;
1722 configuration
->bUseTVMenuLanguage
= m_configuration
.bUseTVMenuLanguage
;
1723 configuration
->bActivateSource
= m_configuration
.bActivateSource
;
1724 configuration
->wakeDevices
= m_configuration
.wakeDevices
;
1725 configuration
->powerOffDevices
= m_configuration
.powerOffDevices
;
1726 configuration
->bPowerOffScreensaver
= m_configuration
.bPowerOffScreensaver
;
1727 configuration
->bPowerOffOnStandby
= m_configuration
.bPowerOffOnStandby
;
1729 // client version 1.5.1
1730 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_1
)
1731 configuration
->bSendInactiveSource
= m_configuration
.bSendInactiveSource
;
1733 // client version 1.5.3
1734 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_5_3
)
1735 configuration
->logicalAddresses
= m_configuration
.logicalAddresses
;
1737 // client version 1.6.0
1738 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_0
)
1740 configuration
->iFirmwareVersion
= m_configuration
.iFirmwareVersion
;
1741 configuration
->bPowerOffDevicesOnStandby
= m_configuration
.bPowerOffDevicesOnStandby
;
1742 configuration
->bShutdownOnStandby
= m_configuration
.bShutdownOnStandby
;
1745 // client version 1.6.2
1746 if (configuration
->clientVersion
>= CEC_CLIENT_VERSION_1_6_2
)
1748 memcpy(configuration
->strDeviceLanguage
, m_configuration
.strDeviceLanguage
, 3);
1749 configuration
->iFirmwareBuildDate
= m_configuration
.iFirmwareBuildDate
;
1754 bool CCECProcessor::CanPersistConfiguration(void)
1756 return m_communication
? m_communication
->GetFirmwareVersion() >= 2 : false;
1759 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1761 return m_communication
? m_communication
->PersistConfiguration(configuration
) : false;
1764 void CCECProcessor::RescanActiveDevices(void)
1766 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1767 m_busDevices
[iPtr
]->GetStatus(true);
1770 bool CCECProcessor::GetDeviceInformation(const char *strPort
, libcec_configuration
*config
, uint32_t iTimeoutMs
/* = CEC_DEFAULT_CONNECT_TIMEOUT */)
1772 if (!OpenConnection(strPort
, CEC_SERIAL_DEFAULT_BAUDRATE
, iTimeoutMs
, false))
1775 config
->iFirmwareVersion
= m_communication
->GetFirmwareVersion();
1776 config
->iPhysicalAddress
= m_communication
->GetPhysicalAddress();
1777 config
->iFirmwareBuildDate
= m_communication
->GetFirmwareBuildDate();
1782 bool CCECProcessor::TransmitPendingActiveSourceCommands(void)
1785 for (unsigned int iPtr
= 0; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
1786 bReturn
&= m_busDevices
[iPtr
]->TransmitPendingActiveSourceCommands();