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();
216 if (m_iPhysicalAddress
!= 0)
218 m_busDevices
[m_logicalAddresses
.primary
]->m_iPhysicalAddress
= m_iPhysicalAddress
;
219 if ((bReturn
= m_busDevices
[m_logicalAddresses
.primary
]->TransmitPhysicalAddress()) == false)
220 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set the physical address to %4x", m_iPhysicalAddress
);
222 else if (m_iPhysicalAddress
== 0 && (bReturn
= SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true)) == false)
223 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort
, ToString(m_iBaseDevice
), (uint8_t)m_iBaseDevice
);
225 SetInitialised(bReturn
);
230 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
235 CLockObject
lock(m_mutex
);
236 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
239 /* create the processor thread */
242 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
247 if ((bReturn
= Initialise()) == false)
249 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
254 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
260 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
262 if (m_busDevices
[address
]->TryLogicalAddress())
264 m_logicalAddresses
.Set(address
);
271 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
273 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
274 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
275 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
276 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
279 bool CCECProcessor::FindLogicalAddressTuner(void)
281 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
282 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
283 TryLogicalAddress(CECDEVICE_TUNER2
) ||
284 TryLogicalAddress(CECDEVICE_TUNER3
) ||
285 TryLogicalAddress(CECDEVICE_TUNER4
);
288 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
290 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
291 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
292 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
293 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
296 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
298 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
299 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
302 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
304 bool bChanged(false);
306 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
308 CLockObject
lock(m_mutex
);
309 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
310 m_logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
312 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
314 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
317 if (m_types
.types
[iPtr
] == from
)
320 m_types
.types
[iPtr
] = to
;
322 else if (m_types
.types
[iPtr
] == to
&& bChanged
)
324 m_types
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
330 FindLogicalAddresses();
332 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
333 if (previousDevice
&& newDevice
)
335 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
336 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
338 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
339 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
341 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
342 cec_menu_language lang
;
343 lang
.device
= previousDevice
->GetLogicalAddress();
344 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
345 lang
.language
[iPtr
] = '?';
346 lang
.language
[3] = 0;
347 previousDevice
->SetMenuLanguage(lang
);
349 newDevice
->SetMenuState(previousDevice
->GetMenuState());
350 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
352 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
353 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
355 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
356 previousDevice
->SetPhysicalAddress(0xFFFF);
358 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
359 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
361 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
362 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
364 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
365 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
367 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
368 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
370 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
371 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
379 bool CCECProcessor::FindLogicalAddresses(void)
382 m_logicalAddresses
.Clear();
384 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
386 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
389 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
391 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
392 bReturn
&= FindLogicalAddressRecordingDevice();
393 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
394 bReturn
&= FindLogicalAddressTuner();
395 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
396 bReturn
&= FindLogicalAddressPlaybackDevice();
397 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
398 bReturn
&= FindLogicalAddressAudioSystem();
402 SetAckMask(m_logicalAddresses
.AckMask());
407 void CCECProcessor::ReplaceHandlers(void)
409 CLockObject
lock(m_mutex
);
410 if (!IsInitialised())
412 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
413 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
416 bool CCECProcessor::OnCommandReceived(const cec_command
&command
)
418 m_commandBuffer
.Push(command
);
422 void *CCECProcessor::Process(void)
425 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
427 while (!IsStopped() && m_communication
->IsOpen())
430 if (m_commandBuffer
.Pop(command
))
431 ParseCommand(command
);
433 m_controller
->CheckKeypressTimeout();
440 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
447 cec_logical_address addr
= m_logicalAddresses
.primary
;
449 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
451 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
453 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
455 addr
= (cec_logical_address
) iPtr
;
461 m_busDevices
[addr
]->SetActiveSource();
462 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
464 bReturn
= m_busDevices
[addr
]->TransmitImageViewOn() &&
465 m_busDevices
[addr
]->TransmitActiveSource();
469 m_busDevices
[addr
]->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
470 m_busDevices
[addr
]->TransmitMenuState(CECDEVICE_TV
);
473 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
474 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
475 m_busDevices
[addr
]->GetHandler()->SendDeckStatusUpdateOnActiveSource())
477 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
484 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
488 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
491 device
->SetActiveSource();
498 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
500 CLockObject
lock(m_mutex
);
501 m_iStandardLineTimeout
= iTimeout
;
504 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
506 CLockObject
lock(m_mutex
);
507 m_iRetryLineTimeout
= iTimeout
;
510 bool CCECProcessor::SetActiveView(void)
512 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
515 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
519 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
522 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
524 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
531 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
535 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
538 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
540 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
547 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
550 CLockObject
lock(m_mutex
);
552 m_iBaseDevice
= iBaseDevice
;
554 if (!IsRunning() && !bForce
)
557 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
559 uint16_t iPhysicalAddress(0);
560 if (iBaseDevice
> CECDEVICE_TV
)
563 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
567 if (iPhysicalAddress
< 0xffff)
569 if (iPhysicalAddress
== 0)
570 iPhysicalAddress
+= 0x1000 * iPort
;
571 else if (iPhysicalAddress
% 0x1000 == 0)
572 iPhysicalAddress
+= 0x100 * iPort
;
573 else if (iPhysicalAddress
% 0x100 == 0)
574 iPhysicalAddress
+= 0x10 * iPort
;
575 else if (iPhysicalAddress
% 0x10 == 0)
576 iPhysicalAddress
+= iPort
;
582 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
586 SetPhysicalAddress(iPhysicalAddress
);
592 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
594 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
596 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
602 bool CCECProcessor::TransmitInactiveSource(void)
607 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
608 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
612 void CCECProcessor::LogOutput(const cec_command
&data
)
615 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
617 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
619 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
620 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
621 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
624 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
626 CLockObject
lock(m_mutex
);
627 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
629 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
630 m_logicalAddresses
.primary
= iLogicalAddress
;
631 m_logicalAddresses
.Set(iLogicalAddress
);
632 return SetAckMask(m_logicalAddresses
.AckMask());
638 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
640 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
642 if (m_logicalAddresses
[iPtr
])
643 m_busDevices
[iPtr
]->SetMenuState(state
);
647 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
652 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
654 bool bSendActiveView(false);
656 cec_logical_addresses sendUpdatesTo
;
657 sendUpdatesTo
.Clear();
660 CLockObject
lock(m_mutex
);
661 m_iPhysicalAddress
= iPhysicalAddress
;
663 if (!m_logicalAddresses
.IsEmpty())
665 bool bWasActiveSource(false);
666 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
667 if (m_logicalAddresses
[iPtr
])
669 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
670 m_busDevices
[iPtr
]->SetInactiveSource();
671 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
673 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
676 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
681 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
682 if (sendUpdatesTo
[iPtr
])
683 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
691 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
693 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
696 CLockObject
lock(m_mutex
);
697 m_bMonitor
= bEnable
;
701 return SetAckMask(0);
703 return SetAckMask(m_logicalAddresses
.AckMask());
706 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
708 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
710 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
711 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
712 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
717 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
720 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
721 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
726 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
729 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
730 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
735 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
738 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
739 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
744 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
746 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
747 return m_busDevices
[m_logicalAddresses
.primary
];
749 CCECBusDevice
*device
= NULL
;
750 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
752 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
754 device
= m_busDevices
[iPtr
];
762 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
764 CCECBusDevice
*device
= NULL
;
766 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
768 if (m_busDevices
[iPtr
]->m_type
== type
&& m_logicalAddresses
[iPtr
])
770 device
= m_busDevices
[iPtr
];
778 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
780 CCECBusDevice
*device(NULL
);
781 cec_logical_address primary
= m_logicalAddresses
.primary
;
782 if (primary
!= CECDEVICE_UNKNOWN
)
783 device
= m_busDevices
[primary
];
787 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
789 return m_busDevices
[iAddress
]->GetCecVersion();
792 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
794 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
797 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
798 retVal
.device
= iAddress
;
803 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
805 if (m_busDevices
[iAddress
])
807 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
808 return (strcmp(language
->language
, "???") != 0);
813 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
815 if (m_busDevices
[iAddress
])
816 return m_busDevices
[iAddress
]->GetVendorId();
820 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
822 if (m_busDevices
[iAddress
])
823 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
827 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
829 if (m_busDevices
[iAddress
])
830 return m_busDevices
[iAddress
]->GetPowerStatus();
831 return CEC_POWER_STATUS_UNKNOWN
;
834 cec_logical_address
CCECProcessor::GetActiveSource(void)
836 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
838 if (m_busDevices
[iPtr
]->IsActiveSource())
839 return (cec_logical_address
)iPtr
;
842 return CECDEVICE_UNKNOWN
;
845 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
847 return m_busDevices
[iAddress
]->IsActiveSource();
850 bool CCECProcessor::Transmit(const cec_command
&data
)
852 cec_adapter_message_state
retVal(ADAPTER_MESSAGE_STATE_UNKNOWN
);
854 CLockObject
lock(m_mutex
);
856 m_iLastTransmission
= GetTimeMs();
857 if (!m_communication
)
859 uint8_t iMaxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
860 retVal
= m_communication
->Write(data
, iMaxTries
, m_iLineTimeout
, m_iRetryLineTimeout
);
863 /* set to "not present" on failed ack */
864 if (retVal
== ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
&&
865 data
.destination
!= CECDEVICE_BROADCAST
)
866 m_busDevices
[data
.destination
]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
868 return retVal
== ADAPTER_MESSAGE_STATE_SENT_ACKED
;
871 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
873 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
877 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
878 command
.parameters
.PushBack((uint8_t)opcode
);
879 command
.parameters
.PushBack((uint8_t)reason
);
884 void CCECProcessor::ParseCommand(const cec_command
&command
)
887 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
888 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
889 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
890 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
892 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
893 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
896 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
898 cec_logical_addresses addresses
;
900 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
902 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
903 addresses
.Set((cec_logical_address
) iPtr
);
908 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
910 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
913 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
915 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
917 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
924 uint16_t CCECProcessor::GetPhysicalAddress(void) const
926 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
927 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
931 bool CCECProcessor::SetAckMask(uint16_t iMask
)
933 return m_communication
->SetAckMask(iMask
);
936 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
938 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
941 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
943 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
946 const char *CCECProcessor::ToString(const cec_device_type type
)
950 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
951 return "audio system";
952 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
953 return "playback device";
954 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
955 return "recording device";
956 case CEC_DEVICE_TYPE_RESERVED
:
958 case CEC_DEVICE_TYPE_TUNER
:
960 case CEC_DEVICE_TYPE_TV
:
967 const char *CCECProcessor::ToString(const cec_menu_state state
)
971 case CEC_MENU_STATE_ACTIVATED
:
973 case CEC_MENU_STATE_DEACTIVATED
:
974 return "deactivated";
980 const char *CCECProcessor::ToString(const cec_version version
)
984 case CEC_VERSION_1_2
:
986 case CEC_VERSION_1_2A
:
988 case CEC_VERSION_1_3
:
990 case CEC_VERSION_1_3A
:
992 case CEC_VERSION_1_4
:
999 const char *CCECProcessor::ToString(const cec_power_status status
)
1003 case CEC_POWER_STATUS_ON
:
1005 case CEC_POWER_STATUS_STANDBY
:
1007 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1008 return "in transition from on to standby";
1009 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1010 return "in transition from standby to on";
1016 const char *CCECProcessor::ToString(const cec_logical_address address
)
1020 case CECDEVICE_AUDIOSYSTEM
:
1022 case CECDEVICE_BROADCAST
:
1024 case CECDEVICE_FREEUSE
:
1026 case CECDEVICE_PLAYBACKDEVICE1
:
1027 return "Playback 1";
1028 case CECDEVICE_PLAYBACKDEVICE2
:
1029 return "Playback 2";
1030 case CECDEVICE_PLAYBACKDEVICE3
:
1031 return "Playback 3";
1032 case CECDEVICE_RECORDINGDEVICE1
:
1033 return "Recorder 1";
1034 case CECDEVICE_RECORDINGDEVICE2
:
1035 return "Recorder 2";
1036 case CECDEVICE_RECORDINGDEVICE3
:
1037 return "Recorder 3";
1038 case CECDEVICE_RESERVED1
:
1039 return "Reserved 1";
1040 case CECDEVICE_RESERVED2
:
1041 return "Reserved 2";
1042 case CECDEVICE_TUNER1
:
1044 case CECDEVICE_TUNER2
:
1046 case CECDEVICE_TUNER3
:
1048 case CECDEVICE_TUNER4
:
1057 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1061 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1062 return "skip forward wind";
1063 case CEC_DECK_CONTROL_MODE_EJECT
:
1065 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1066 return "reverse rewind";
1067 case CEC_DECK_CONTROL_MODE_STOP
:
1074 const char *CCECProcessor::ToString(const cec_deck_info status
)
1078 case CEC_DECK_INFO_PLAY
:
1080 case CEC_DECK_INFO_RECORD
:
1082 case CEC_DECK_INFO_PLAY_REVERSE
:
1083 return "play reverse";
1084 case CEC_DECK_INFO_STILL
:
1086 case CEC_DECK_INFO_SLOW
:
1088 case CEC_DECK_INFO_SLOW_REVERSE
:
1089 return "slow reverse";
1090 case CEC_DECK_INFO_FAST_FORWARD
:
1091 return "fast forward";
1092 case CEC_DECK_INFO_FAST_REVERSE
:
1093 return "fast reverse";
1094 case CEC_DECK_INFO_NO_MEDIA
:
1096 case CEC_DECK_INFO_STOP
:
1098 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1099 return "info skip forward wind";
1100 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1101 return "info skip reverse rewind";
1102 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1103 return "info index search forward";
1104 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1105 return "info index search reverse";
1106 case CEC_DECK_INFO_OTHER_STATUS
:
1113 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1117 case CEC_OPCODE_ACTIVE_SOURCE
:
1118 return "active source";
1119 case CEC_OPCODE_IMAGE_VIEW_ON
:
1120 return "image view on";
1121 case CEC_OPCODE_TEXT_VIEW_ON
:
1122 return "text view on";
1123 case CEC_OPCODE_INACTIVE_SOURCE
:
1124 return "inactive source";
1125 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1126 return "request active source";
1127 case CEC_OPCODE_ROUTING_CHANGE
:
1128 return "routing change";
1129 case CEC_OPCODE_ROUTING_INFORMATION
:
1130 return "routing information";
1131 case CEC_OPCODE_SET_STREAM_PATH
:
1132 return "set stream path";
1133 case CEC_OPCODE_STANDBY
:
1135 case CEC_OPCODE_RECORD_OFF
:
1136 return "record off";
1137 case CEC_OPCODE_RECORD_ON
:
1139 case CEC_OPCODE_RECORD_STATUS
:
1140 return "record status";
1141 case CEC_OPCODE_RECORD_TV_SCREEN
:
1142 return "record tv screen";
1143 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1144 return "clear analogue timer";
1145 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1146 return "clear digital timer";
1147 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1148 return "clear external timer";
1149 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1150 return "set analogue timer";
1151 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1152 return "set digital timer";
1153 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1154 return "set external timer";
1155 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1156 return "set timer program title";
1157 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1158 return "timer cleared status";
1159 case CEC_OPCODE_TIMER_STATUS
:
1160 return "timer status";
1161 case CEC_OPCODE_CEC_VERSION
:
1162 return "cec version";
1163 case CEC_OPCODE_GET_CEC_VERSION
:
1164 return "get cec version";
1165 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1166 return "give physical address";
1167 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1168 return "get menu language";
1169 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1170 return "report physical address";
1171 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1172 return "set menu language";
1173 case CEC_OPCODE_DECK_CONTROL
:
1174 return "deck control";
1175 case CEC_OPCODE_DECK_STATUS
:
1176 return "deck status";
1177 case CEC_OPCODE_GIVE_DECK_STATUS
:
1178 return "give deck status";
1179 case CEC_OPCODE_PLAY
:
1181 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1182 return "give tuner status";
1183 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1184 return "select analogue service";
1185 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1186 return "set digital service";
1187 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1188 return "tuner device status";
1189 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1190 return "tuner step decrement";
1191 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1192 return "tuner step increment";
1193 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1194 return "device vendor id";
1195 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1196 return "give device vendor id";
1197 case CEC_OPCODE_VENDOR_COMMAND
:
1198 return "vendor command";
1199 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1200 return "vendor command with id";
1201 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1202 return "vendor remote button down";
1203 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1204 return "vendor remote button up";
1205 case CEC_OPCODE_SET_OSD_STRING
:
1206 return "set osd string";
1207 case CEC_OPCODE_GIVE_OSD_NAME
:
1208 return "give osd name";
1209 case CEC_OPCODE_SET_OSD_NAME
:
1210 return "set osd name";
1211 case CEC_OPCODE_MENU_REQUEST
:
1212 return "menu request";
1213 case CEC_OPCODE_MENU_STATUS
:
1214 return "menu status";
1215 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1216 return "user control pressed";
1217 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1218 return "user control release";
1219 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1220 return "give device power status";
1221 case CEC_OPCODE_REPORT_POWER_STATUS
:
1222 return "report power status";
1223 case CEC_OPCODE_FEATURE_ABORT
:
1224 return "feature abort";
1225 case CEC_OPCODE_ABORT
:
1227 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1228 return "give audio status";
1229 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1230 return "give audio mode status";
1231 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1232 return "report audio status";
1233 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1234 return "set system audio mode";
1235 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1236 return "system audio mode request";
1237 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1238 return "system audio mode status";
1239 case CEC_OPCODE_SET_AUDIO_RATE
:
1240 return "set audio rate";
1246 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1250 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1252 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1259 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1261 // TODO this is a mask
1265 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1269 case CEC_VENDOR_SAMSUNG
:
1273 case CEC_VENDOR_PANASONIC
:
1275 case CEC_VENDOR_PIONEER
:
1277 case CEC_VENDOR_ONKYO
:
1279 case CEC_VENDOR_YAMAHA
:
1281 case CEC_VENDOR_PHILIPS
:
1283 case CEC_VENDOR_SONY
:
1290 const char *CCECProcessor::ToString(const cec_client_version version
)
1294 case CEC_CLIENT_VERSION_PRE_1_5
:
1296 case CEC_CLIENT_VERSION_1_5_0
:
1303 void *CCECBusScan::Process(void)
1305 CCECBusDevice
*device(NULL
);
1306 uint8_t iCounter(0);
1308 while (!IsStopped())
1310 if (++iCounter
< 10)
1315 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1317 device
= m_processor
->m_busDevices
[iPtr
];
1319 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1323 device
->GetVendorId();
1327 device
->GetPowerStatus(true);
1335 void CCECBusScan::WaitUntilIdle(void)
1340 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1341 while (iWaitTime
> 0)
1344 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1348 bool CCECProcessor::StartBootloader(void)
1350 return m_communication
->StartBootloader();
1353 bool CCECProcessor::PingAdapter(void)
1355 return m_communication
->PingAdapter();
1358 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1360 m_busDevices
[initiator
]->HandlePoll(destination
);
1363 bool CCECProcessor::HandleReceiveFailed(cec_logical_address initiator
)
1365 return !m_busDevices
[initiator
]->HandleReceiveFailed();
1368 bool CCECProcessor::SetStreamPath(uint16_t iPhysicalAddress
)
1370 // stream path changes are sent by the TV
1371 return m_busDevices
[CECDEVICE_TV
]->GetHandler()->TransmitSetStreamPath(iPhysicalAddress
);
1374 bool CCECProcessor::GetCurrentConfiguration(libcec_configuration
*configuration
)
1376 configuration
->iPhysicalAddress
= m_iPhysicalAddress
;
1377 configuration
->iHDMIPort
= m_iHDMIPort
;
1378 configuration
->baseDevice
= m_iBaseDevice
;
1379 snprintf(configuration
->strDeviceName
, 13, "%s", m_strDeviceName
.c_str());
1380 configuration
->deviceTypes
= m_types
;
1384 bool CCECProcessor::CanPersistConfiguration(void)
1386 return m_communication
->GetFirmwareVersion() >= 2;
1389 bool CCECProcessor::PersistConfiguration(libcec_configuration
*configuration
)
1391 return m_communication
->PersistConfiguration(configuration
);