2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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 "AdapterCommunication.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 "util/StdString.h"
45 #include "platform/timeutils.h"
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
52 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
53 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
54 m_lastInitiator(CECDEVICE_UNKNOWN
),
55 m_strDeviceName(strDeviceName
),
56 m_controller(controller
),
59 m_iStandardLineTimeout(3),
60 m_iRetryLineTimeout(3),
61 m_iLastTransmission(0)
63 m_communication
= new CAdapterCommunication(this);
64 m_logicalAddresses
.Clear();
65 m_logicalAddresses
.Set(iLogicalAddress
);
67 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
68 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
71 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
) :
73 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
74 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
75 m_strDeviceName(strDeviceName
),
77 m_controller(controller
),
79 m_iStandardLineTimeout(3),
80 m_iRetryLineTimeout(3),
81 m_iLastTransmission(0)
83 m_communication
= new CAdapterCommunication(this);
84 m_logicalAddresses
.Clear();
85 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
89 case CECDEVICE_AUDIOSYSTEM
:
90 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
92 case CECDEVICE_PLAYBACKDEVICE1
:
93 case CECDEVICE_PLAYBACKDEVICE2
:
94 case CECDEVICE_PLAYBACKDEVICE3
:
95 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
97 case CECDEVICE_RECORDINGDEVICE1
:
98 case CECDEVICE_RECORDINGDEVICE2
:
99 case CECDEVICE_RECORDINGDEVICE3
:
100 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
102 case CECDEVICE_TUNER1
:
103 case CECDEVICE_TUNER2
:
104 case CECDEVICE_TUNER3
:
105 case CECDEVICE_TUNER4
:
106 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
109 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
112 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
118 CCECProcessor::~CCECProcessor(void)
125 m_busScan
->StopThread();
129 m_startCondition
.Broadcast();
132 delete m_communication
;
133 m_communication
= NULL
;
135 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
136 delete m_busDevices
[iPtr
];
139 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
141 CLockObject
lock(&m_mutex
);
142 if (!m_communication
|| m_communication
->IsOpen())
144 m_controller
->AddLog(CEC_LOG_ERROR
, "connection already opened");
148 if (!m_communication
->Open(strPort
, iBaudRate
, iTimeoutMs
))
150 m_controller
->AddLog(CEC_LOG_ERROR
, "could not open a connection");
156 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
158 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
163 if (SetAckMask(m_logicalAddresses
.AckMask()) &&
164 SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true))
166 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
167 m_busScan
= new CCECBusScan(this);
168 m_busScan
->CreateThread(true);
173 m_controller
->AddLog(CEC_LOG_ERROR
, "failed to initialise the processor");
177 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
181 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
183 if (m_busDevices
[address
]->TryLogicalAddress())
185 /* only set our OSD name and active source for the primary device */
186 if (m_logicalAddresses
.IsEmpty())
188 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
189 m_busDevices
[address
]->m_bActiveSource
= true;
191 m_logicalAddresses
.Set(address
);
198 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
200 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
201 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
202 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
203 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
206 bool CCECProcessor::FindLogicalAddressTuner(void)
208 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
209 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
210 TryLogicalAddress(CECDEVICE_TUNER2
) ||
211 TryLogicalAddress(CECDEVICE_TUNER3
) ||
212 TryLogicalAddress(CECDEVICE_TUNER4
);
215 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
217 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
218 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
219 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
220 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
223 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
225 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
226 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
229 bool CCECProcessor::FindLogicalAddresses(void)
232 m_logicalAddresses
.Clear();
235 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
237 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
240 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
241 AddLog(CEC_LOG_DEBUG
, strLog
);
243 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
244 bReturn
&= FindLogicalAddressRecordingDevice();
245 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
246 bReturn
&= FindLogicalAddressTuner();
247 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
248 bReturn
&= FindLogicalAddressPlaybackDevice();
249 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
250 bReturn
&= FindLogicalAddressAudioSystem();
256 void *CCECProcessor::Process(void)
258 bool bParseFrame(false);
260 CCECAdapterMessage msg
;
262 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
264 CLockObject
lock(&m_mutex
);
265 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
266 m_startCondition
.Signal();
271 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPhysicalAddress();
272 CLockObject
lock(&m_mutex
);
274 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
275 m_startCondition
.Signal();
284 CLockObject
lock(&m_mutex
);
285 if (m_commandBuffer
.Pop(command
))
289 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
291 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
292 command
= m_currentframe
;
297 ParseCommand(command
);
302 m_controller
->CheckKeypressTimeout();
306 m_communication
->Close();
311 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
318 cec_logical_address addr
= m_logicalAddresses
.primary
;
320 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
322 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
324 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
326 addr
= (cec_logical_address
) iPtr
;
332 bReturn
= m_busDevices
[addr
]->TransmitActiveSource() &&
333 SetStreamPath(m_busDevices
[addr
]->GetPhysicalAddress(false));
335 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
336 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
338 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
344 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
346 CLockObject
lock(&m_mutex
);
347 m_iStandardLineTimeout
= iTimeout
;
350 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
352 CLockObject
lock(&m_mutex
);
353 m_iRetryLineTimeout
= iTimeout
;
356 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress
)
358 return SetStreamPath(m_busDevices
[iAddress
]->GetPhysicalAddress(false));
361 bool CCECProcessor::SetActiveView(void)
363 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
366 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
370 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
373 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
375 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
382 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
386 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
389 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
391 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
398 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
403 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
404 AddLog(CEC_LOG_DEBUG
, strLog
);
406 m_iBaseDevice
= iBaseDevice
;
408 if (!m_bStarted
&& !bForce
)
411 uint16_t iPhysicalAddress(0);
412 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
414 if (iPhysicalAddress
== 0)
416 else if (iPhysicalAddress
% 0x1000 == 0)
418 else if (iPhysicalAddress
% 0x100 == 0)
420 else if (iPhysicalAddress
% 0x10 == 0)
423 while(!bReturn
&& iPos
> 0)
425 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
426 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
427 AddLog(CEC_LOG_DEBUG
, strLog
);
428 if (PhysicalAddressInUse(iPhysicalAddress
))
430 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
431 AddLog(CEC_LOG_DEBUG
, strLog
);
432 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
436 strLog
.Format("physical address %4x is free", iPhysicalAddress
);
437 AddLog(CEC_LOG_DEBUG
, strLog
);
438 SetPhysicalAddress(iPhysicalAddress
);
446 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
448 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
450 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
456 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath
)
460 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
463 device
->SetActiveDevice();
470 bool CCECProcessor::TransmitInactiveSource(void)
475 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
476 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
480 void CCECProcessor::LogOutput(const cec_command
&data
)
483 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
485 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
487 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
488 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
489 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
492 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
494 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
497 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
498 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
499 m_logicalAddresses
.primary
= iLogicalAddress
;
500 m_logicalAddresses
.Set(iLogicalAddress
);
501 return SetAckMask(m_logicalAddresses
.AckMask());
507 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
509 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
511 if (m_logicalAddresses
[iPtr
])
512 m_busDevices
[iPtr
]->SetMenuState(state
);
516 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
521 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
523 if (!m_logicalAddresses
.IsEmpty())
525 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
526 if (m_logicalAddresses
[iPtr
])
527 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
528 return SetActiveView();
533 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
536 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
537 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
540 CLockObject
lock(&m_mutex
);
541 m_bMonitor
= bEnable
;
547 m_busScan
= new CCECBusScan(this);
548 m_busScan
->CreateThread(true);
555 m_busScan
->StopThread();
563 return SetAckMask(0);
565 return SetAckMask(m_logicalAddresses
.AckMask());
568 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
570 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
572 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
573 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
574 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
579 uint8_t CCECProcessor::VolumeUp(void)
582 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
583 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp();
588 uint8_t CCECProcessor::VolumeDown(void)
591 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
592 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown();
597 uint8_t CCECProcessor::MuteAudio(void)
600 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
601 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio();
606 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
608 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
609 return m_busDevices
[m_logicalAddresses
.primary
];
611 CCECBusDevice
*device
= NULL
;
612 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
614 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
616 device
= m_busDevices
[iPtr
];
624 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
626 CCECBusDevice
*device
= NULL
;
628 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
630 if (m_busDevices
[iPtr
]->m_type
== type
)
632 device
= m_busDevices
[iPtr
];
640 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
642 return m_busDevices
[iAddress
]->GetCecVersion();
645 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
647 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
650 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
651 retVal
.device
= iAddress
;
656 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
658 if (m_busDevices
[iAddress
])
660 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
661 return (strcmp(language
->language
, "???") != 0);
666 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
668 if (m_busDevices
[iAddress
])
669 return m_busDevices
[iAddress
]->GetVendorId();
673 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
675 if (m_busDevices
[iAddress
])
676 return m_busDevices
[iAddress
]->GetPowerStatus();
677 return CEC_POWER_STATUS_UNKNOWN
;
680 bool CCECProcessor::Transmit(const cec_command
&data
)
685 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
686 bReturn
= Transmit(output
);
688 /* set to "not present" on failed ack */
689 if (output
->is_error() && output
->reply
== MSGCODE_TRANSMIT_FAILED_ACK
&&
690 output
->destination() != CECDEVICE_BROADCAST
)
691 m_busDevices
[output
->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
697 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
700 CLockObject
lock(&m_mutex
);
702 m_iLastTransmission
= GetTimeMs();
703 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
708 if (output
->tries
> 0)
709 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
711 CLockObject
msgLock(&output
->mutex
);
712 if (!m_communication
|| !m_communication
->Write(output
))
716 output
->condition
.Wait(&output
->mutex
);
717 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
719 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
724 if (output
->transmit_timeout
> 0)
726 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
727 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
731 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
< output
->maxTries
);
734 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
739 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
741 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
745 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
746 command
.parameters
.PushBack((uint8_t)opcode
);
747 command
.parameters
.PushBack((uint8_t)reason
);
752 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
755 bool bTransmitSucceeded(false);
756 uint8_t iPacketsLeft(message
->size() / 4);
758 int64_t iNow
= GetTimeMs();
759 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
761 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
763 CCECAdapterMessage msg
;
765 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
771 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
773 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
774 m_lastInitiator
= msg
.initiator();
779 bError
= msg
.is_error();
780 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
781 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
782 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
790 message
->reply
= msg
.message();
791 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
795 switch(msg
.message())
797 case MSGCODE_COMMAND_ACCEPTED
:
798 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
799 if (iPacketsLeft
> 0)
802 case MSGCODE_TRANSMIT_SUCCEEDED
:
803 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
804 bTransmitSucceeded
= (iPacketsLeft
== 0);
805 bError
= !bTransmitSucceeded
;
806 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
809 // ignore other data while waiting
817 return bTransmitSucceeded
&& !bError
;
820 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
823 bool bIsError(msg
.is_error());
828 switch(msg
.message())
830 case MSGCODE_FRAME_START
:
832 m_currentframe
.Clear();
835 m_currentframe
.initiator
= msg
.initiator();
836 m_currentframe
.destination
= msg
.destination();
837 m_currentframe
.ack
= msg
.ack();
838 m_currentframe
.eom
= msg
.eom();
840 if (m_currentframe
.ack
== 0x1)
842 m_lastInitiator
= m_currentframe
.initiator
;
843 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
847 case MSGCODE_RECEIVE_FAILED
:
849 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
850 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
853 case MSGCODE_FRAME_DATA
:
857 m_currentframe
.PushBack(msg
[1]);
858 m_currentframe
.eom
= msg
.eom();
867 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
871 void CCECProcessor::ParseCommand(cec_command
&command
)
874 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
875 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
876 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
877 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
879 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
880 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
883 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
885 cec_logical_addresses addresses
;
887 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
889 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
890 addresses
.Set((cec_logical_address
) iPtr
);
895 bool CCECProcessor::IsActiveDevice(cec_logical_address address
)
897 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
900 bool CCECProcessor::IsActiveDeviceType(cec_device_type type
)
902 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
904 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
911 uint16_t CCECProcessor::GetPhysicalAddress(void) const
913 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
914 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
918 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
920 m_controller
->SetCurrentButton(iButtonCode
);
923 void CCECProcessor::AddCommand(const cec_command
&command
)
925 m_controller
->AddCommand(command
);
928 void CCECProcessor::AddKey(cec_keypress
&key
)
930 m_controller
->AddKey(key
);
933 void CCECProcessor::AddKey(void)
935 m_controller
->AddKey();
938 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
940 m_controller
->AddLog(level
, strMessage
);
943 bool CCECProcessor::SetAckMask(uint16_t iMask
)
947 strLog
.Format("setting ackmask to %2x", iMask
);
948 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
950 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
952 output
->push_back(MSGSTART
);
953 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
954 output
->push_escaped(iMask
>> 8);
955 output
->push_escaped((uint8_t)iMask
);
956 output
->push_back(MSGEND
);
958 if ((bReturn
= Transmit(output
)) == false)
959 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
966 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
)
968 return m_busDevices
[iDestination
]->TransmitKeypress(key
);
971 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
)
973 return m_busDevices
[iDestination
]->TransmitKeyRelease();
976 const char *CCECProcessor::ToString(const cec_menu_state state
)
980 case CEC_MENU_STATE_ACTIVATED
:
982 case CEC_MENU_STATE_DEACTIVATED
:
983 return "deactivated";
989 const char *CCECProcessor::ToString(const cec_version version
)
993 case CEC_VERSION_1_2
:
995 case CEC_VERSION_1_2A
:
997 case CEC_VERSION_1_3
:
999 case CEC_VERSION_1_3A
:
1001 case CEC_VERSION_1_4
:
1008 const char *CCECProcessor::ToString(const cec_power_status status
)
1012 case CEC_POWER_STATUS_ON
:
1014 case CEC_POWER_STATUS_STANDBY
:
1016 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1017 return "in transition from on to standby";
1018 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1019 return "in transition from standby to on";
1025 const char *CCECProcessor::ToString(const cec_logical_address address
)
1029 case CECDEVICE_AUDIOSYSTEM
:
1031 case CECDEVICE_BROADCAST
:
1033 case CECDEVICE_FREEUSE
:
1035 case CECDEVICE_PLAYBACKDEVICE1
:
1036 return "Playback 1";
1037 case CECDEVICE_PLAYBACKDEVICE2
:
1038 return "Playback 2";
1039 case CECDEVICE_PLAYBACKDEVICE3
:
1040 return "Playback 3";
1041 case CECDEVICE_RECORDINGDEVICE1
:
1042 return "Recorder 1";
1043 case CECDEVICE_RECORDINGDEVICE2
:
1044 return "Recorder 2";
1045 case CECDEVICE_RECORDINGDEVICE3
:
1046 return "Recorder 3";
1047 case CECDEVICE_RESERVED1
:
1048 return "Reserved 1";
1049 case CECDEVICE_RESERVED2
:
1050 return "Reserved 2";
1051 case CECDEVICE_TUNER1
:
1053 case CECDEVICE_TUNER2
:
1055 case CECDEVICE_TUNER3
:
1057 case CECDEVICE_TUNER4
:
1066 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1070 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1071 return "skip forward wind";
1072 case CEC_DECK_CONTROL_MODE_EJECT
:
1074 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1075 return "reverse rewind";
1076 case CEC_DECK_CONTROL_MODE_STOP
:
1083 const char *CCECProcessor::ToString(const cec_deck_info status
)
1087 case CEC_DECK_INFO_PLAY
:
1089 case CEC_DECK_INFO_RECORD
:
1091 case CEC_DECK_INFO_PLAY_REVERSE
:
1092 return "play reverse";
1093 case CEC_DECK_INFO_STILL
:
1095 case CEC_DECK_INFO_SLOW
:
1097 case CEC_DECK_INFO_SLOW_REVERSE
:
1098 return "slow reverse";
1099 case CEC_DECK_INFO_FAST_FORWARD
:
1100 return "fast forward";
1101 case CEC_DECK_INFO_FAST_REVERSE
:
1102 return "fast reverse";
1103 case CEC_DECK_INFO_NO_MEDIA
:
1105 case CEC_DECK_INFO_STOP
:
1107 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1108 return "info skip forward wind";
1109 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1110 return "info skip reverse rewind";
1111 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1112 return "info index search forward";
1113 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1114 return "info index search reverse";
1115 case CEC_DECK_INFO_OTHER_STATUS
:
1122 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1126 case CEC_OPCODE_ACTIVE_SOURCE
:
1127 return "active source";
1128 case CEC_OPCODE_IMAGE_VIEW_ON
:
1129 return "image view on";
1130 case CEC_OPCODE_TEXT_VIEW_ON
:
1131 return "text view on";
1132 case CEC_OPCODE_INACTIVE_SOURCE
:
1133 return "inactive source";
1134 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1135 return "request active source";
1136 case CEC_OPCODE_ROUTING_CHANGE
:
1137 return "routing change";
1138 case CEC_OPCODE_ROUTING_INFORMATION
:
1139 return "routing information";
1140 case CEC_OPCODE_SET_STREAM_PATH
:
1141 return "set stream path";
1142 case CEC_OPCODE_STANDBY
:
1144 case CEC_OPCODE_RECORD_OFF
:
1145 return "record off";
1146 case CEC_OPCODE_RECORD_ON
:
1148 case CEC_OPCODE_RECORD_STATUS
:
1149 return "record status";
1150 case CEC_OPCODE_RECORD_TV_SCREEN
:
1151 return "record tv screen";
1152 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1153 return "clear analogue timer";
1154 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1155 return "clear digital timer";
1156 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1157 return "clear external timer";
1158 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1159 return "set analogue timer";
1160 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1161 return "set digital timer";
1162 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1163 return "set external timer";
1164 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1165 return "set timer program title";
1166 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1167 return "timer cleared status";
1168 case CEC_OPCODE_TIMER_STATUS
:
1169 return "timer status";
1170 case CEC_OPCODE_CEC_VERSION
:
1171 return "cec version";
1172 case CEC_OPCODE_GET_CEC_VERSION
:
1173 return "get cec version";
1174 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1175 return "give physical address";
1176 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1177 return "get menu language";
1178 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1179 return "report physical address";
1180 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1181 return "set menu language";
1182 case CEC_OPCODE_DECK_CONTROL
:
1183 return "deck control";
1184 case CEC_OPCODE_DECK_STATUS
:
1185 return "deck status";
1186 case CEC_OPCODE_GIVE_DECK_STATUS
:
1187 return "give deck status";
1188 case CEC_OPCODE_PLAY
:
1190 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1191 return "give tuner status";
1192 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1193 return "select analogue service";
1194 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1195 return "set digital service";
1196 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1197 return "tuner device status";
1198 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1199 return "tuner step decrement";
1200 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1201 return "tuner step increment";
1202 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1203 return "device vendor id";
1204 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1205 return "give device vendor id";
1206 case CEC_OPCODE_VENDOR_COMMAND
:
1207 return "vendor command";
1208 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1209 return "vendor command with id";
1210 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1211 return "vendor remote button down";
1212 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1213 return "vendor remote button up";
1214 case CEC_OPCODE_SET_OSD_STRING
:
1215 return "set osd string";
1216 case CEC_OPCODE_GIVE_OSD_NAME
:
1217 return "give osd name";
1218 case CEC_OPCODE_SET_OSD_NAME
:
1219 return "set osd name";
1220 case CEC_OPCODE_MENU_REQUEST
:
1221 return "menu request";
1222 case CEC_OPCODE_MENU_STATUS
:
1223 return "menu status";
1224 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1225 return "user control pressed";
1226 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1227 return "user control release";
1228 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1229 return "give device power status";
1230 case CEC_OPCODE_REPORT_POWER_STATUS
:
1231 return "report power status";
1232 case CEC_OPCODE_FEATURE_ABORT
:
1233 return "feature abort";
1234 case CEC_OPCODE_ABORT
:
1236 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1237 return "give audio status";
1238 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1239 return "give audio mode status";
1240 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1241 return "report audio status";
1242 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1243 return "set system audio mode";
1244 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1245 return "system audio mode request";
1246 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1247 return "system audio mode status";
1248 case CEC_OPCODE_SET_AUDIO_RATE
:
1249 return "set audio rate";
1255 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1259 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1261 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1268 const char *CCECProcessor::ToString(const cec_audio_status status
)
1270 // TODO this is a mask
1274 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1278 case CEC_VENDOR_SAMSUNG
:
1282 case CEC_VENDOR_PANASONIC
:
1284 case CEC_VENDOR_PIONEER
:
1286 case CEC_VENDOR_ONKYO
:
1288 case CEC_VENDOR_YAMAHA
:
1290 case CEC_VENDOR_PHILIPS
:
1297 void *CCECBusScan::Process(void)
1299 CCECBusDevice
*device(NULL
);
1301 while (!IsStopped())
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 int64_t iWaitTime
= 3000 - (GetTimeMs() - m_processor
->GetLastTransmission());
1329 while (iWaitTime
> 0)
1332 iWaitTime
= 3000 - (GetTimeMs() - m_processor
->GetLastTransmission());
1336 bool CCECProcessor::StartBootloader(void)
1338 return m_communication
->StartBootloader();
1341 bool CCECProcessor::PingAdapter(void)
1343 return m_communication
->PingAdapter();