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
, const libcec_configuration
*configuration
) :
51 m_bInitialised(false),
52 m_iPhysicalAddress(configuration
->iPhysicalAddress
),
53 m_iHDMIPort(configuration
->iHDMIPort
),
54 m_iBaseDevice(configuration
->baseDevice
),
55 m_strDeviceName(configuration
->strDeviceName
),
56 m_types(configuration
->deviceTypes
),
57 m_communication(NULL
),
58 m_controller(controller
),
60 m_iStandardLineTimeout(3),
61 m_iRetryLineTimeout(3),
62 m_iLastTransmission(0),
63 m_clientVersion(configuration
->clientVersion
)
65 m_logicalAddresses
.Clear();
67 if (configuration
->tvVendor
!= CEC_VENDOR_UNKNOWN
)
69 m_busDevices
[CECDEVICE_TV
]->SetVendorId((uint64_t)configuration
->tvVendor
);
70 m_busDevices
[CECDEVICE_TV
]->ReplaceHandler(false);
74 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
, uint16_t iPhysicalAddress
, cec_client_version clientVersion
) :
75 m_bInitialised(false),
76 m_iPhysicalAddress(iPhysicalAddress
),
77 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
78 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
79 m_strDeviceName(strDeviceName
),
81 m_communication(NULL
),
82 m_controller(controller
),
84 m_iStandardLineTimeout(3),
85 m_iRetryLineTimeout(3),
86 m_iLastTransmission(0),
87 m_clientVersion(clientVersion
)
89 m_logicalAddresses
.Clear();
93 void CCECProcessor::CreateBusDevices(void)
95 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
99 case CECDEVICE_AUDIOSYSTEM
:
100 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
102 case CECDEVICE_PLAYBACKDEVICE1
:
103 case CECDEVICE_PLAYBACKDEVICE2
:
104 case CECDEVICE_PLAYBACKDEVICE3
:
105 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
107 case CECDEVICE_RECORDINGDEVICE1
:
108 case CECDEVICE_RECORDINGDEVICE2
:
109 case CECDEVICE_RECORDINGDEVICE3
:
110 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
112 case CECDEVICE_TUNER1
:
113 case CECDEVICE_TUNER2
:
114 case CECDEVICE_TUNER3
:
115 case CECDEVICE_TUNER4
:
116 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
119 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
122 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
128 CCECProcessor::~CCECProcessor(void)
132 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
133 delete m_busDevices
[iPtr
];
136 void CCECProcessor::Close(void)
140 CLockObject
lock(m_mutex
);
143 m_communication
->Close();
144 delete m_communication
;
145 m_communication
= NULL
;
149 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
)
152 CLockObject
lock(m_mutex
);
155 CLibCEC::AddLog(CEC_LOG_WARNING
, "existing connection handler found, deleting it");
156 m_communication
->Close();
157 delete m_communication
;
160 m_communication
= new CUSBCECAdapterCommunication(this, strPort
, iBaudRate
);
162 /* check for an already opened connection */
163 if (m_communication
->IsOpen())
165 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
169 uint64_t iNow
= GetTimeMs();
170 uint64_t iTarget
= iTimeoutMs
> 0 ? iNow
+ iTimeoutMs
: iNow
+ CEC_DEFAULT_TRANSMIT_WAIT
;
172 /* open a new connection */
173 unsigned iConnectTry(0);
174 while (iNow
< iTarget
&& (bReturn
= m_communication
->Open(this, iTimeoutMs
)) == false)
176 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
182 CLibCEC::AddLog(CEC_LOG_NOTICE
, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication
->GetFirmwareVersion(), ToString(m_clientVersion
));
187 bool CCECProcessor::IsInitialised(void)
189 CLockObject
lock(m_mutex
);
190 return m_bInitialised
;
193 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
195 CLockObject
lock(m_mutex
);
196 m_bInitialised
= bSetTo
;
199 bool CCECProcessor::Initialise(void)
203 CLockObject
lock(m_mutex
);
204 if (!m_logicalAddresses
.IsEmpty())
205 m_logicalAddresses
.Clear();
207 if (!FindLogicalAddresses())
209 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
213 /* only set our OSD name for the primary device */
214 m_busDevices
[m_logicalAddresses
.primary
]->m_strDeviceName
= m_strDeviceName
;
217 /* get the vendor id from the TV, so we are using the correct handler */
218 m_busDevices
[CECDEVICE_TV
]->RequestVendorId();
220 if (m_iPhysicalAddress
!= 0)
222 CLibCEC::AddLog(CEC_LOG_NOTICE
, "setting the physical address to %4x", m_iPhysicalAddress
);
223 m_busDevices
[m_logicalAddresses
.primary
]->m_iPhysicalAddress
= m_iPhysicalAddress
;
224 if ((bReturn
= m_busDevices
[m_logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
225 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_iPhysicalAddress
);
227 else if (m_iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true)) == false)
228 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort
, ToString(m_iBaseDevice
), (uint8_t)m_iBaseDevice
);
230 SetInitialised(bReturn
);
235 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
240 CLockObject
lock(m_mutex
);
241 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
244 /* create the processor thread */
247 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
252 if ((bReturn
= Initialise()) == false)
254 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
259 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
265 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
267 if (m_busDevices
[address
]->TryLogicalAddress())
269 m_logicalAddresses
.Set(address
);
276 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
278 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
279 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
280 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
281 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
284 bool CCECProcessor::FindLogicalAddressTuner(void)
286 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
287 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
288 TryLogicalAddress(CECDEVICE_TUNER2
) ||
289 TryLogicalAddress(CECDEVICE_TUNER3
) ||
290 TryLogicalAddress(CECDEVICE_TUNER4
);
293 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
295 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
296 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
297 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
298 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
301 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
303 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
304 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
307 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
309 bool bChanged(false);
311 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
313 CLockObject
lock(m_mutex
);
314 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
315 m_logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
317 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
319 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
322 if (m_types
.types
[iPtr
] == from
)
325 m_types
.types
[iPtr
] = to
;
327 else if (m_types
.types
[iPtr
] == to
&& bChanged
)
329 m_types
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
335 FindLogicalAddresses();
337 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
338 if (previousDevice
&& newDevice
)
340 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
341 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
343 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
344 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
346 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
347 cec_menu_language lang
;
348 lang
.device
= previousDevice
->GetLogicalAddress();
349 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
350 lang
.language
[iPtr
] = '?';
351 lang
.language
[3] = 0;
352 previousDevice
->SetMenuLanguage(lang
);
354 newDevice
->SetMenuState(previousDevice
->GetMenuState());
355 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
357 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
358 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
360 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
361 previousDevice
->SetPhysicalAddress(0xFFFF);
363 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
364 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
366 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
367 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
369 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
370 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
372 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
373 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
375 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
376 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
384 bool CCECProcessor::FindLogicalAddresses(void)
387 m_logicalAddresses
.Clear();
389 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
391 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
394 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
396 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
397 bReturn
&= FindLogicalAddressRecordingDevice();
398 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
399 bReturn
&= FindLogicalAddressTuner();
400 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
401 bReturn
&= FindLogicalAddressPlaybackDevice();
402 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
403 bReturn
&= FindLogicalAddressAudioSystem();
407 SetAckMask(m_logicalAddresses
.AckMask());
412 void CCECProcessor::ReplaceHandlers(void)
414 if (!IsInitialised())
416 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
417 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
420 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
422 m_commandBuffer
.Push(command
);
426 void *CCECProcessor::Process(void)
429 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
431 while (!IsStopped() && m_communication
->IsOpen())
434 if (m_commandBuffer
.Pop(command
))
435 ParseCommand(command
);
437 m_controller
->CheckKeypressTimeout();
444 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
451 cec_logical_address addr
= m_logicalAddresses
.primary
;
453 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
455 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
457 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
459 addr
= (cec_logical_address
) iPtr
;
465 m_busDevices
[addr
]->SetActiveSource();
466 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
467 bReturn
= m_busDevices
[addr
]->ActivateSource();
472 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
476 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
479 device
->SetActiveSource();
486 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
488 CLockObject
lock(m_mutex
);
489 m_iStandardLineTimeout
= iTimeout
;
492 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
494 CLockObject
lock(m_mutex
);
495 m_iRetryLineTimeout
= iTimeout
;
498 bool CCECProcessor::SetActiveView(void)
500 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
503 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
507 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
510 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
512 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
519 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
523 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
526 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
528 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
535 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
538 CLockObject
lock(m_mutex
);
540 m_iBaseDevice
= iBaseDevice
;
542 if (!IsRunning() && !bForce
)
545 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
547 uint16_t iPhysicalAddress(0);
548 if (iBaseDevice
> CECDEVICE_TV
)
551 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
555 if (iPhysicalAddress
< 0xffff)
557 if (iPhysicalAddress
== 0)
558 iPhysicalAddress
+= 0x1000 * iPort
;
559 else if (iPhysicalAddress
% 0x1000 == 0)
560 iPhysicalAddress
+= 0x100 * iPort
;
561 else if (iPhysicalAddress
% 0x100 == 0)
562 iPhysicalAddress
+= 0x10 * iPort
;
563 else if (iPhysicalAddress
% 0x10 == 0)
564 iPhysicalAddress
+= iPort
;
570 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
574 SetPhysicalAddress(iPhysicalAddress
);
580 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
582 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
584 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
590 bool CCECProcessor::TransmitInactiveSource(void)
595 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
596 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
600 void CCECProcessor::LogOutput(const cec_command
&data
)
603 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
605 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
607 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
608 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
609 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
612 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
614 CLockObject
lock(m_mutex
);
615 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
617 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
618 m_logicalAddresses
.primary
= iLogicalAddress
;
619 m_logicalAddresses
.Set(iLogicalAddress
);
620 return SetAckMask(m_logicalAddresses
.AckMask());
626 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
628 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
630 if (m_logicalAddresses
[iPtr
])
631 m_busDevices
[iPtr
]->SetMenuState(state
);
635 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
640 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
642 bool bSendActiveView(false);
644 cec_logical_addresses sendUpdatesTo
;
645 sendUpdatesTo
.Clear();
648 CLockObject
lock(m_mutex
);
649 m_iPhysicalAddress
= iPhysicalAddress
;
651 if (!m_logicalAddresses
.IsEmpty())
653 bool bWasActiveSource(false);
654 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
655 if (m_logicalAddresses
[iPtr
])
657 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
658 m_busDevices
[iPtr
]->SetInactiveSource();
659 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
661 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
664 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
669 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
670 if (sendUpdatesTo
[iPtr
])
671 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
679 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
681 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
684 CLockObject
lock(m_mutex
);
685 m_bMonitor
= bEnable
;
689 return SetAckMask(0);
691 return SetAckMask(m_logicalAddresses
.AckMask());
694 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
696 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
698 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
699 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
700 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
705 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
708 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
709 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
714 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
717 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
718 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
723 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
726 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
727 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
732 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
734 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
735 return m_busDevices
[m_logicalAddresses
.primary
];
737 CCECBusDevice
*device
= NULL
;
738 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
740 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
742 device
= m_busDevices
[iPtr
];
750 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
752 CCECBusDevice
*device
= NULL
;
754 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
756 if (m_busDevices
[iPtr
]->m_type
== type
&& m_logicalAddresses
[iPtr
])
758 device
= m_busDevices
[iPtr
];
766 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
768 CCECBusDevice
*device(NULL
);
769 cec_logical_address primary
= m_logicalAddresses
.primary
;
770 if (primary
!= CECDEVICE_UNKNOWN
)
771 device
= m_busDevices
[primary
];
775 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
777 return m_busDevices
[iAddress
]->GetCecVersion();
780 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
782 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
785 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
786 retVal
.device
= iAddress
;
791 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
793 if (m_busDevices
[iAddress
])
795 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
796 return (strcmp(language
->language
, "???") != 0);
801 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
803 if (m_busDevices
[iAddress
])
804 return m_busDevices
[iAddress
]->GetVendorId();
808 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
810 if (m_busDevices
[iAddress
])
811 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
815 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
817 if (m_busDevices
[iAddress
])
818 return m_busDevices
[iAddress
]->GetPowerStatus();
819 return CEC_POWER_STATUS_UNKNOWN
;
822 cec_logical_address
CCECProcessor::GetActiveSource(void)
824 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
826 if (m_busDevices
[iPtr
]->IsActiveSource())
827 return (cec_logical_address
)iPtr
;
830 return CECDEVICE_UNKNOWN
;
833 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
835 return m_busDevices
[iAddress
]->IsActiveSource();
838 bool CCECProcessor::Transmit(const cec_command
&data
)
840 cec_adapter_message_state
retVal(ADAPTER_MESSAGE_STATE_UNKNOWN
);
842 CLockObject
lock(m_mutex
);
844 m_iLastTransmission
= GetTimeMs();
845 if (!m_communication
)
847 uint8_t iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
848 retVal
= m_communication
->Write(data
, iMaxTries
, m_iLineTimeout
, m_iRetryLineTimeout
);
851 /* set to "not present" on failed ack */
852 if (retVal
== ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
&&
853 data
.destination
!= CECDEVICE_BROADCAST
)
854 m_busDevices
[data
.destination
]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
856 return retVal
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
859 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
861 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
865 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
866 command
.parameters
.PushBack((uint8_t)opcode
);
867 command
.parameters
.PushBack((uint8_t)reason
);
872 void CCECProcessor::ParseCommand(const cec_command
&command
)
875 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
876 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
877 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
878 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
880 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
881 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
884 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
886 cec_logical_addresses addresses
;
888 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
890 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
891 addresses
.Set((cec_logical_address
) iPtr
);
896 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
898 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
901 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
903 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
905 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
912 uint16_t CCECProcessor::GetPhysicalAddress(void) const
914 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
915 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
919 bool CCECProcessor::SetAckMask(uint16_t iMask
)
921 return m_communication
->SetAckMask(iMask
);
924 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
926 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
929 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
931 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
934 const char *CCECProcessor::ToString(const cec_device_type type
)
938 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
939 return "audio system";
940 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
941 return "playback device";
942 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
943 return "recording device";
944 case CEC_DEVICE_TYPE_RESERVED
:
946 case CEC_DEVICE_TYPE_TUNER
:
948 case CEC_DEVICE_TYPE_TV
:
955 const char *CCECProcessor::ToString(const cec_menu_state state
)
959 case CEC_MENU_STATE_ACTIVATED
:
961 case CEC_MENU_STATE_DEACTIVATED
:
962 return "deactivated";
968 const char *CCECProcessor::ToString(const cec_version version
)
972 case CEC_VERSION_1_2
:
974 case CEC_VERSION_1_2A
:
976 case CEC_VERSION_1_3
:
978 case CEC_VERSION_1_3A
:
980 case CEC_VERSION_1_4
:
987 const char *CCECProcessor::ToString(const cec_power_status status
)
991 case CEC_POWER_STATUS_ON
:
993 case CEC_POWER_STATUS_STANDBY
:
995 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
996 return "in transition from on to standby";
997 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
998 return "in transition from standby to on";
1004 const char *CCECProcessor::ToString(const cec_logical_address address
)
1008 case CECDEVICE_AUDIOSYSTEM
:
1010 case CECDEVICE_BROADCAST
:
1012 case CECDEVICE_FREEUSE
:
1014 case CECDEVICE_PLAYBACKDEVICE1
:
1015 return "Playback 1";
1016 case CECDEVICE_PLAYBACKDEVICE2
:
1017 return "Playback 2";
1018 case CECDEVICE_PLAYBACKDEVICE3
:
1019 return "Playback 3";
1020 case CECDEVICE_RECORDINGDEVICE1
:
1021 return "Recorder 1";
1022 case CECDEVICE_RECORDINGDEVICE2
:
1023 return "Recorder 2";
1024 case CECDEVICE_RECORDINGDEVICE3
:
1025 return "Recorder 3";
1026 case CECDEVICE_RESERVED1
:
1027 return "Reserved 1";
1028 case CECDEVICE_RESERVED2
:
1029 return "Reserved 2";
1030 case CECDEVICE_TUNER1
:
1032 case CECDEVICE_TUNER2
:
1034 case CECDEVICE_TUNER3
:
1036 case CECDEVICE_TUNER4
:
1045 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1049 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1050 return "skip forward wind";
1051 case CEC_DECK_CONTROL_MODE_EJECT
:
1053 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1054 return "reverse rewind";
1055 case CEC_DECK_CONTROL_MODE_STOP
:
1062 const char *CCECProcessor::ToString(const cec_deck_info status
)
1066 case CEC_DECK_INFO_PLAY
:
1068 case CEC_DECK_INFO_RECORD
:
1070 case CEC_DECK_INFO_PLAY_REVERSE
:
1071 return "play reverse";
1072 case CEC_DECK_INFO_STILL
:
1074 case CEC_DECK_INFO_SLOW
:
1076 case CEC_DECK_INFO_SLOW_REVERSE
:
1077 return "slow reverse";
1078 case CEC_DECK_INFO_FAST_FORWARD
:
1079 return "fast forward";
1080 case CEC_DECK_INFO_FAST_REVERSE
:
1081 return "fast reverse";
1082 case CEC_DECK_INFO_NO_MEDIA
:
1084 case CEC_DECK_INFO_STOP
:
1086 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1087 return "info skip forward wind";
1088 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1089 return "info skip reverse rewind";
1090 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1091 return "info index search forward";
1092 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1093 return "info index search reverse";
1094 case CEC_DECK_INFO_OTHER_STATUS
:
1101 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1105 case CEC_OPCODE_ACTIVE_SOURCE
:
1106 return "active source";
1107 case CEC_OPCODE_IMAGE_VIEW_ON
:
1108 return "image view on";
1109 case CEC_OPCODE_TEXT_VIEW_ON
:
1110 return "text view on";
1111 case CEC_OPCODE_INACTIVE_SOURCE
:
1112 return "inactive source";
1113 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1114 return "request active source";
1115 case CEC_OPCODE_ROUTING_CHANGE
:
1116 return "routing change";
1117 case CEC_OPCODE_ROUTING_INFORMATION
:
1118 return "routing information";
1119 case CEC_OPCODE_SET_STREAM_PATH
:
1120 return "set stream path";
1121 case CEC_OPCODE_STANDBY
:
1123 case CEC_OPCODE_RECORD_OFF
:
1124 return "record off";
1125 case CEC_OPCODE_RECORD_ON
:
1127 case CEC_OPCODE_RECORD_STATUS
:
1128 return "record status";
1129 case CEC_OPCODE_RECORD_TV_SCREEN
:
1130 return "record tv screen";
1131 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1132 return "clear analogue timer";
1133 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1134 return "clear digital timer";
1135 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1136 return "clear external timer";
1137 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1138 return "set analogue timer";
1139 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1140 return "set digital timer";
1141 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1142 return "set external timer";
1143 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1144 return "set timer program title";
1145 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1146 return "timer cleared status";
1147 case CEC_OPCODE_TIMER_STATUS
:
1148 return "timer status";
1149 case CEC_OPCODE_CEC_VERSION
:
1150 return "cec version";
1151 case CEC_OPCODE_GET_CEC_VERSION
:
1152 return "get cec version";
1153 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1154 return "give physical address";
1155 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1156 return "get menu language";
1157 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1158 return "report physical address";
1159 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1160 return "set menu language";
1161 case CEC_OPCODE_DECK_CONTROL
:
1162 return "deck control";
1163 case CEC_OPCODE_DECK_STATUS
:
1164 return "deck status";
1165 case CEC_OPCODE_GIVE_DECK_STATUS
:
1166 return "give deck status";
1167 case CEC_OPCODE_PLAY
:
1169 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1170 return "give tuner status";
1171 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1172 return "select analogue service";
1173 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1174 return "set digital service";
1175 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1176 return "tuner device status";
1177 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1178 return "tuner step decrement";
1179 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1180 return "tuner step increment";
1181 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1182 return "device vendor id";
1183 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1184 return "give device vendor id";
1185 case CEC_OPCODE_VENDOR_COMMAND
:
1186 return "vendor command";
1187 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1188 return "vendor command with id";
1189 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1190 return "vendor remote button down";
1191 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1192 return "vendor remote button up";
1193 case CEC_OPCODE_SET_OSD_STRING
:
1194 return "set osd string";
1195 case CEC_OPCODE_GIVE_OSD_NAME
:
1196 return "give osd name";
1197 case CEC_OPCODE_SET_OSD_NAME
:
1198 return "set osd name";
1199 case CEC_OPCODE_MENU_REQUEST
:
1200 return "menu request";
1201 case CEC_OPCODE_MENU_STATUS
:
1202 return "menu status";
1203 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1204 return "user control pressed";
1205 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1206 return "user control release";
1207 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1208 return "give device power status";
1209 case CEC_OPCODE_REPORT_POWER_STATUS
:
1210 return "report power status";
1211 case CEC_OPCODE_FEATURE_ABORT
:
1212 return "feature abort";
1213 case CEC_OPCODE_ABORT
:
1215 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1216 return "give audio status";
1217 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1218 return "give audio mode status";
1219 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1220 return "report audio status";
1221 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1222 return "set system audio mode";
1223 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1224 return "system audio mode request";
1225 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1226 return "system audio mode status";
1227 case CEC_OPCODE_SET_AUDIO_RATE
:
1228 return "set audio rate";
1234 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1238 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1240 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1247 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1249 // TODO this is a mask
1253 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1257 case CEC_VENDOR_SAMSUNG
:
1261 case CEC_VENDOR_PANASONIC
:
1263 case CEC_VENDOR_PIONEER
:
1265 case CEC_VENDOR_ONKYO
:
1267 case CEC_VENDOR_YAMAHA
:
1269 case CEC_VENDOR_PHILIPS
:
1271 case CEC_VENDOR_SONY
:
1278 const char *CCECProcessor::ToString(const cec_client_version version
)
1282 case CEC_CLIENT_VERSION_PRE_1_5
:
1284 case CEC_CLIENT_VERSION_1_5_0
:
1291 void *CCECBusScan::Process(void)
1293 CCECBusDevice
*device(NULL
);
1294 uint8_t iCounter(0);
1296 while (!IsStopped())
1298 if (++iCounter
< 10)
1303 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1305 device
= m_processor
->m_busDevices
[iPtr
];
1307 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1311 device
->GetVendorId();
1315 device
->GetPowerStatus(true);
1323 void CCECBusScan::WaitUntilIdle(void)
1328 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1329 while (iWaitTime
> 0)
1332 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1336 bool CCECProcessor::StartBootloader(void)
1338 return m_communication
->StartBootloader();
1341 bool CCECProcessor::PingAdapter(void)
1343 return m_communication
->PingAdapter();
1346 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1348 m_busDevices
[initiator
]->HandlePoll(destination
);
1351 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1353 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1356 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1358 // stream path changes are sent by the TV
1359 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1362 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1364 configuration
->iPhysicalAddress
= m_iPhysicalAddress
;
1365 configuration
->iHDMIPort
= m_iHDMIPort
;
1366 configuration
->baseDevice
= m_iBaseDevice
;
1367 snprintf(configuration
->strDeviceName
, 13, "%s", m_strDeviceName
.c_str());
1368 configuration
->deviceTypes
= m_types
;
1372 bool CCECProcessor::CanPersistConfiguration(void)
1374 return m_communication
->GetFirmwareVersion() >= 2;
1377 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1379 return m_communication
->PersistConfiguration(configuration
);