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();
69 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
, uint16_t iPhysicalAddress
, cec_client_version clientVersion
) :
70 m_bInitialised(false),
71 m_iPhysicalAddress(iPhysicalAddress
),
72 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
73 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
74 m_strDeviceName(strDeviceName
),
76 m_communication(NULL
),
77 m_controller(controller
),
79 m_iStandardLineTimeout(3),
80 m_iRetryLineTimeout(3),
81 m_iLastTransmission(0),
82 m_clientVersion(clientVersion
)
84 m_logicalAddresses
.Clear();
88 void CCECProcessor::CreateBusDevices(void)
90 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
94 case CECDEVICE_AUDIOSYSTEM
:
95 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
97 case CECDEVICE_PLAYBACKDEVICE1
:
98 case CECDEVICE_PLAYBACKDEVICE2
:
99 case CECDEVICE_PLAYBACKDEVICE3
:
100 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
102 case CECDEVICE_RECORDINGDEVICE1
:
103 case CECDEVICE_RECORDINGDEVICE2
:
104 case CECDEVICE_RECORDINGDEVICE3
:
105 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
107 case CECDEVICE_TUNER1
:
108 case CECDEVICE_TUNER2
:
109 case CECDEVICE_TUNER3
:
110 case CECDEVICE_TUNER4
:
111 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
114 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
117 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
123 CCECProcessor::~CCECProcessor(void)
127 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
128 delete m_busDevices
[iPtr
];
131 void CCECProcessor::Close(void)
135 CLockObject
lock(m_mutex
);
138 m_communication
->Close();
139 delete m_communication
;
140 m_communication
= NULL
;
144 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
)
147 CLockObject
lock(m_mutex
);
150 CLibCEC::AddLog(CEC_LOG_WARNING
, "existing connection handler found, deleting it");
151 m_communication
->Close();
152 delete m_communication
;
155 m_communication
= new CUSBCECAdapterCommunication(this, strPort
, iBaudRate
);
157 /* check for an already opened connection */
158 if (m_communication
->IsOpen())
160 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
164 uint64_t iNow
= GetTimeMs();
165 uint64_t iTarget
= iTimeoutMs
> 0 ? iNow
+ iTimeoutMs
: iNow
+ CEC_DEFAULT_TRANSMIT_WAIT
;
167 /* open a new connection */
168 unsigned iConnectTry(0);
169 while (iNow
< iTarget
&& (bReturn
= m_communication
->Open(this, iTimeoutMs
)) == false)
171 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
177 CLibCEC::AddLog(CEC_LOG_NOTICE
, "connected to the CEC adapter. firmware version = %d, client version = %s", m_communication
->GetFirmwareVersion(), ToString(m_clientVersion
));
182 bool CCECProcessor::IsInitialised(void)
184 CLockObject
lock(m_mutex
);
185 return m_bInitialised
;
188 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
190 CLockObject
lock(m_mutex
);
191 m_bInitialised
= bSetTo
;
194 bool CCECProcessor::Initialise(void)
198 CLockObject
lock(m_mutex
);
199 if (!m_logicalAddresses
.IsEmpty())
200 m_logicalAddresses
.Clear();
202 if (!FindLogicalAddresses())
204 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
208 /* only set our OSD name for the primary device */
209 m_busDevices
[m_logicalAddresses
.primary
]->m_strDeviceName
= m_strDeviceName
;
212 /* get the vendor id from the TV, so we are using the correct handler */
213 m_busDevices
[CECDEVICE_TV
]->RequestVendorId();
215 if (m_iPhysicalAddress
!= 0)
217 m_busDevices
[m_logicalAddresses
.primary
]->m_iPhysicalAddress
= m_iPhysicalAddress
;
218 if ((bReturn
= m_busDevices
[m_logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
219 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_iPhysicalAddress
);
221 else if (m_iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true)) == false)
222 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort
, ToString(m_iBaseDevice
), (uint8_t)m_iBaseDevice
);
224 SetInitialised(bReturn
);
229 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
234 CLockObject
lock(m_mutex
);
235 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
238 /* create the processor thread */
241 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
246 if ((bReturn
= Initialise()) == false)
248 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
253 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
259 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
261 if (m_busDevices
[address
]->TryLogicalAddress())
263 m_logicalAddresses
.Set(address
);
270 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
272 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
273 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
274 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
275 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
278 bool CCECProcessor::FindLogicalAddressTuner(void)
280 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
281 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
282 TryLogicalAddress(CECDEVICE_TUNER2
) ||
283 TryLogicalAddress(CECDEVICE_TUNER3
) ||
284 TryLogicalAddress(CECDEVICE_TUNER4
);
287 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
289 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
290 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
291 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
292 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
295 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
297 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
298 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
301 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
303 bool bChanged(false);
305 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
307 CLockObject
lock(m_mutex
);
308 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
309 m_logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
311 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
313 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
316 if (m_types
.types
[iPtr
] == from
)
319 m_types
.types
[iPtr
] = to
;
321 else if (m_types
.types
[iPtr
] == to
&& bChanged
)
323 m_types
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
329 FindLogicalAddresses();
331 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
332 if (previousDevice
&& newDevice
)
334 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
335 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
337 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
338 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
340 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
341 cec_menu_language lang
;
342 lang
.device
= previousDevice
->GetLogicalAddress();
343 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
344 lang
.language
[iPtr
] = '?';
345 lang
.language
[3] = 0;
346 previousDevice
->SetMenuLanguage(lang
);
348 newDevice
->SetMenuState(previousDevice
->GetMenuState());
349 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
351 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
352 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
354 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
355 previousDevice
->SetPhysicalAddress(0xFFFF);
357 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
358 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
360 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
361 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
363 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
364 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
366 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
367 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
369 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
370 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
378 bool CCECProcessor::FindLogicalAddresses(void)
381 m_logicalAddresses
.Clear();
383 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
385 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
388 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
390 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
391 bReturn
&= FindLogicalAddressRecordingDevice();
392 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
393 bReturn
&= FindLogicalAddressTuner();
394 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
395 bReturn
&= FindLogicalAddressPlaybackDevice();
396 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
397 bReturn
&= FindLogicalAddressAudioSystem();
401 SetAckMask(m_logicalAddresses
.AckMask());
406 void CCECProcessor::ReplaceHandlers(void)
408 if (!IsInitialised())
410 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
411 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
414 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
416 m_commandBuffer
.Push(command
);
420 void *CCECProcessor::Process(void)
423 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
425 while (!IsStopped() && m_communication
->IsOpen())
428 if (m_commandBuffer
.Pop(command
))
429 ParseCommand(command
);
431 m_controller
->CheckKeypressTimeout();
438 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
445 cec_logical_address addr
= m_logicalAddresses
.primary
;
447 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
449 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
451 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
453 addr
= (cec_logical_address
) iPtr
;
459 m_busDevices
[addr
]->SetActiveSource();
460 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
462 bReturn
= m_busDevices
[addr
]->TransmitActiveSource();
466 m_busDevices
[addr
]->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
467 m_busDevices
[addr
]->TransmitMenuState(CECDEVICE_TV
);
470 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
471 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
472 m_busDevices
[addr
]->GetHandler()->SendDeckStatusUpdateOnActiveSource())
474 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
481 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
485 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
488 device
->SetActiveSource();
495 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
497 CLockObject
lock(m_mutex
);
498 m_iStandardLineTimeout
= iTimeout
;
501 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
503 CLockObject
lock(m_mutex
);
504 m_iRetryLineTimeout
= iTimeout
;
507 bool CCECProcessor::SetActiveView(void)
509 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
512 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
516 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
519 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
521 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
528 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
532 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
535 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
537 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
544 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
547 CLockObject
lock(m_mutex
);
549 m_iBaseDevice
= iBaseDevice
;
551 if (!IsRunning() && !bForce
)
554 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
556 uint16_t iPhysicalAddress(0);
557 if (iBaseDevice
> CECDEVICE_TV
)
560 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
564 if (iPhysicalAddress
< 0xffff)
566 if (iPhysicalAddress
== 0)
567 iPhysicalAddress
+= 0x1000 * iPort
;
568 else if (iPhysicalAddress
% 0x1000 == 0)
569 iPhysicalAddress
+= 0x100 * iPort
;
570 else if (iPhysicalAddress
% 0x100 == 0)
571 iPhysicalAddress
+= 0x10 * iPort
;
572 else if (iPhysicalAddress
% 0x10 == 0)
573 iPhysicalAddress
+= iPort
;
579 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
583 SetPhysicalAddress(iPhysicalAddress
);
589 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
591 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
593 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
599 bool CCECProcessor::TransmitInactiveSource(void)
604 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
605 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
609 void CCECProcessor::LogOutput(const cec_command
&data
)
612 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
614 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
616 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
617 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
618 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
621 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
623 CLockObject
lock(m_mutex
);
624 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
626 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
627 m_logicalAddresses
.primary
= iLogicalAddress
;
628 m_logicalAddresses
.Set(iLogicalAddress
);
629 return SetAckMask(m_logicalAddresses
.AckMask());
635 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
637 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
639 if (m_logicalAddresses
[iPtr
])
640 m_busDevices
[iPtr
]->SetMenuState(state
);
644 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
649 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
651 bool bSendActiveView(false);
653 cec_logical_addresses sendUpdatesTo
;
654 sendUpdatesTo
.Clear();
657 CLockObject
lock(m_mutex
);
658 m_iPhysicalAddress
= iPhysicalAddress
;
660 if (!m_logicalAddresses
.IsEmpty())
662 bool bWasActiveSource(false);
663 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
664 if (m_logicalAddresses
[iPtr
])
666 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
667 m_busDevices
[iPtr
]->SetInactiveSource();
668 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
670 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
673 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
678 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
679 if (sendUpdatesTo
[iPtr
])
680 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
688 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
690 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
693 CLockObject
lock(m_mutex
);
694 m_bMonitor
= bEnable
;
698 return SetAckMask(0);
700 return SetAckMask(m_logicalAddresses
.AckMask());
703 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
705 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
707 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
708 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
709 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
714 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
717 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
718 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
723 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
726 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
727 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
732 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
735 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
736 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
741 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
743 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
744 return m_busDevices
[m_logicalAddresses
.primary
];
746 CCECBusDevice
*device
= NULL
;
747 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
749 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
751 device
= m_busDevices
[iPtr
];
759 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
761 CCECBusDevice
*device
= NULL
;
763 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
765 if (m_busDevices
[iPtr
]->m_type
== type
&& m_logicalAddresses
[iPtr
])
767 device
= m_busDevices
[iPtr
];
775 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
777 CCECBusDevice
*device(NULL
);
778 cec_logical_address primary
= m_logicalAddresses
.primary
;
779 if (primary
!= CECDEVICE_UNKNOWN
)
780 device
= m_busDevices
[primary
];
784 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
786 return m_busDevices
[iAddress
]->GetCecVersion();
789 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
791 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
794 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
795 retVal
.device
= iAddress
;
800 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
802 if (m_busDevices
[iAddress
])
804 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
805 return (strcmp(language
->language
, "???") != 0);
810 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
812 if (m_busDevices
[iAddress
])
813 return m_busDevices
[iAddress
]->GetVendorId();
817 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
819 if (m_busDevices
[iAddress
])
820 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
824 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
826 if (m_busDevices
[iAddress
])
827 return m_busDevices
[iAddress
]->GetPowerStatus();
828 return CEC_POWER_STATUS_UNKNOWN
;
831 cec_logical_address
CCECProcessor::GetActiveSource(void)
833 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
835 if (m_busDevices
[iPtr
]->IsActiveSource())
836 return (cec_logical_address
)iPtr
;
839 return CECDEVICE_UNKNOWN
;
842 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
844 return m_busDevices
[iAddress
]->IsActiveSource();
847 bool CCECProcessor::Transmit(const cec_command
&data
)
849 cec_adapter_message_state
retVal(ADAPTER_MESSAGE_STATE_UNKNOWN
);
851 CLockObject
lock(m_mutex
);
853 m_iLastTransmission
= GetTimeMs();
854 if (!m_communication
)
856 uint8_t iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
857 retVal
= m_communication
->Write(data
, iMaxTries
, m_iLineTimeout
, m_iRetryLineTimeout
);
860 /* set to "not present" on failed ack */
861 if (retVal
== ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
&&
862 data
.destination
!= CECDEVICE_BROADCAST
)
863 m_busDevices
[data
.destination
]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
865 return retVal
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
868 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
870 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
874 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
875 command
.parameters
.PushBack((uint8_t)opcode
);
876 command
.parameters
.PushBack((uint8_t)reason
);
881 void CCECProcessor::ParseCommand(const cec_command
&command
)
884 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
885 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
886 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
887 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
889 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
890 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
893 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
895 cec_logical_addresses addresses
;
897 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
899 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
900 addresses
.Set((cec_logical_address
) iPtr
);
905 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
907 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
910 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
912 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
914 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
921 uint16_t CCECProcessor::GetPhysicalAddress(void) const
923 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
924 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
928 bool CCECProcessor::SetAckMask(uint16_t iMask
)
930 return m_communication
->SetAckMask(iMask
);
933 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
935 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
938 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
940 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
943 const char *CCECProcessor::ToString(const cec_device_type type
)
947 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
948 return "audio system";
949 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
950 return "playback device";
951 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
952 return "recording device";
953 case CEC_DEVICE_TYPE_RESERVED
:
955 case CEC_DEVICE_TYPE_TUNER
:
957 case CEC_DEVICE_TYPE_TV
:
964 const char *CCECProcessor::ToString(const cec_menu_state state
)
968 case CEC_MENU_STATE_ACTIVATED
:
970 case CEC_MENU_STATE_DEACTIVATED
:
971 return "deactivated";
977 const char *CCECProcessor::ToString(const cec_version version
)
981 case CEC_VERSION_1_2
:
983 case CEC_VERSION_1_2A
:
985 case CEC_VERSION_1_3
:
987 case CEC_VERSION_1_3A
:
989 case CEC_VERSION_1_4
:
996 const char *CCECProcessor::ToString(const cec_power_status status
)
1000 case CEC_POWER_STATUS_ON
:
1002 case CEC_POWER_STATUS_STANDBY
:
1004 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1005 return "in transition from on to standby";
1006 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1007 return "in transition from standby to on";
1013 const char *CCECProcessor::ToString(const cec_logical_address address
)
1017 case CECDEVICE_AUDIOSYSTEM
:
1019 case CECDEVICE_BROADCAST
:
1021 case CECDEVICE_FREEUSE
:
1023 case CECDEVICE_PLAYBACKDEVICE1
:
1024 return "Playback 1";
1025 case CECDEVICE_PLAYBACKDEVICE2
:
1026 return "Playback 2";
1027 case CECDEVICE_PLAYBACKDEVICE3
:
1028 return "Playback 3";
1029 case CECDEVICE_RECORDINGDEVICE1
:
1030 return "Recorder 1";
1031 case CECDEVICE_RECORDINGDEVICE2
:
1032 return "Recorder 2";
1033 case CECDEVICE_RECORDINGDEVICE3
:
1034 return "Recorder 3";
1035 case CECDEVICE_RESERVED1
:
1036 return "Reserved 1";
1037 case CECDEVICE_RESERVED2
:
1038 return "Reserved 2";
1039 case CECDEVICE_TUNER1
:
1041 case CECDEVICE_TUNER2
:
1043 case CECDEVICE_TUNER3
:
1045 case CECDEVICE_TUNER4
:
1054 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1058 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1059 return "skip forward wind";
1060 case CEC_DECK_CONTROL_MODE_EJECT
:
1062 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1063 return "reverse rewind";
1064 case CEC_DECK_CONTROL_MODE_STOP
:
1071 const char *CCECProcessor::ToString(const cec_deck_info status
)
1075 case CEC_DECK_INFO_PLAY
:
1077 case CEC_DECK_INFO_RECORD
:
1079 case CEC_DECK_INFO_PLAY_REVERSE
:
1080 return "play reverse";
1081 case CEC_DECK_INFO_STILL
:
1083 case CEC_DECK_INFO_SLOW
:
1085 case CEC_DECK_INFO_SLOW_REVERSE
:
1086 return "slow reverse";
1087 case CEC_DECK_INFO_FAST_FORWARD
:
1088 return "fast forward";
1089 case CEC_DECK_INFO_FAST_REVERSE
:
1090 return "fast reverse";
1091 case CEC_DECK_INFO_NO_MEDIA
:
1093 case CEC_DECK_INFO_STOP
:
1095 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1096 return "info skip forward wind";
1097 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1098 return "info skip reverse rewind";
1099 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1100 return "info index search forward";
1101 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1102 return "info index search reverse";
1103 case CEC_DECK_INFO_OTHER_STATUS
:
1110 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1114 case CEC_OPCODE_ACTIVE_SOURCE
:
1115 return "active source";
1116 case CEC_OPCODE_IMAGE_VIEW_ON
:
1117 return "image view on";
1118 case CEC_OPCODE_TEXT_VIEW_ON
:
1119 return "text view on";
1120 case CEC_OPCODE_INACTIVE_SOURCE
:
1121 return "inactive source";
1122 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1123 return "request active source";
1124 case CEC_OPCODE_ROUTING_CHANGE
:
1125 return "routing change";
1126 case CEC_OPCODE_ROUTING_INFORMATION
:
1127 return "routing information";
1128 case CEC_OPCODE_SET_STREAM_PATH
:
1129 return "set stream path";
1130 case CEC_OPCODE_STANDBY
:
1132 case CEC_OPCODE_RECORD_OFF
:
1133 return "record off";
1134 case CEC_OPCODE_RECORD_ON
:
1136 case CEC_OPCODE_RECORD_STATUS
:
1137 return "record status";
1138 case CEC_OPCODE_RECORD_TV_SCREEN
:
1139 return "record tv screen";
1140 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1141 return "clear analogue timer";
1142 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1143 return "clear digital timer";
1144 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1145 return "clear external timer";
1146 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1147 return "set analogue timer";
1148 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1149 return "set digital timer";
1150 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1151 return "set external timer";
1152 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1153 return "set timer program title";
1154 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1155 return "timer cleared status";
1156 case CEC_OPCODE_TIMER_STATUS
:
1157 return "timer status";
1158 case CEC_OPCODE_CEC_VERSION
:
1159 return "cec version";
1160 case CEC_OPCODE_GET_CEC_VERSION
:
1161 return "get cec version";
1162 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1163 return "give physical address";
1164 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1165 return "get menu language";
1166 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1167 return "report physical address";
1168 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1169 return "set menu language";
1170 case CEC_OPCODE_DECK_CONTROL
:
1171 return "deck control";
1172 case CEC_OPCODE_DECK_STATUS
:
1173 return "deck status";
1174 case CEC_OPCODE_GIVE_DECK_STATUS
:
1175 return "give deck status";
1176 case CEC_OPCODE_PLAY
:
1178 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1179 return "give tuner status";
1180 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1181 return "select analogue service";
1182 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1183 return "set digital service";
1184 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1185 return "tuner device status";
1186 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1187 return "tuner step decrement";
1188 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1189 return "tuner step increment";
1190 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1191 return "device vendor id";
1192 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1193 return "give device vendor id";
1194 case CEC_OPCODE_VENDOR_COMMAND
:
1195 return "vendor command";
1196 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1197 return "vendor command with id";
1198 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1199 return "vendor remote button down";
1200 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1201 return "vendor remote button up";
1202 case CEC_OPCODE_SET_OSD_STRING
:
1203 return "set osd string";
1204 case CEC_OPCODE_GIVE_OSD_NAME
:
1205 return "give osd name";
1206 case CEC_OPCODE_SET_OSD_NAME
:
1207 return "set osd name";
1208 case CEC_OPCODE_MENU_REQUEST
:
1209 return "menu request";
1210 case CEC_OPCODE_MENU_STATUS
:
1211 return "menu status";
1212 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1213 return "user control pressed";
1214 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1215 return "user control release";
1216 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1217 return "give device power status";
1218 case CEC_OPCODE_REPORT_POWER_STATUS
:
1219 return "report power status";
1220 case CEC_OPCODE_FEATURE_ABORT
:
1221 return "feature abort";
1222 case CEC_OPCODE_ABORT
:
1224 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1225 return "give audio status";
1226 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1227 return "give audio mode status";
1228 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1229 return "report audio status";
1230 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1231 return "set system audio mode";
1232 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1233 return "system audio mode request";
1234 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1235 return "system audio mode status";
1236 case CEC_OPCODE_SET_AUDIO_RATE
:
1237 return "set audio rate";
1243 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1247 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1249 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1256 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1258 // TODO this is a mask
1262 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1266 case CEC_VENDOR_SAMSUNG
:
1270 case CEC_VENDOR_PANASONIC
:
1272 case CEC_VENDOR_PIONEER
:
1274 case CEC_VENDOR_ONKYO
:
1276 case CEC_VENDOR_YAMAHA
:
1278 case CEC_VENDOR_PHILIPS
:
1280 case CEC_VENDOR_SONY
:
1287 const char *CCECProcessor::ToString(const cec_client_version version
)
1291 case CEC_CLIENT_VERSION_PRE_1_5
:
1293 case CEC_CLIENT_VERSION_1_5_0
:
1300 void *CCECBusScan::Process(void)
1302 CCECBusDevice
*device(NULL
);
1303 uint8_t iCounter(0);
1305 while (!IsStopped())
1307 if (++iCounter
< 10)
1312 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1314 device
= m_processor
->m_busDevices
[iPtr
];
1316 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1320 device
->GetVendorId();
1324 device
->GetPowerStatus(true);
1332 void CCECBusScan::WaitUntilIdle(void)
1337 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1338 while (iWaitTime
> 0)
1341 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1345 bool CCECProcessor::StartBootloader(void)
1347 return m_communication
->StartBootloader();
1350 bool CCECProcessor::PingAdapter(void)
1352 return m_communication
->PingAdapter();
1355 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1357 m_busDevices
[initiator
]->HandlePoll(destination
);
1360 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1362 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1365 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1367 // stream path changes are sent by the TV
1368 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1371 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1373 configuration
->iPhysicalAddress
= m_iPhysicalAddress
;
1374 configuration
->iHDMIPort
= m_iHDMIPort
;
1375 configuration
->baseDevice
= m_iBaseDevice
;
1376 snprintf(configuration
->strDeviceName
, 13, "%s", m_strDeviceName
.c_str());
1377 configuration
->deviceTypes
= m_types
;
1381 bool CCECProcessor::CanPersistConfiguration(void)
1383 return m_communication
->GetFirmwareVersion() >= 2;
1386 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1388 return m_communication
->PersistConfiguration(configuration
);