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)
121 m_startCondition
.Broadcast();
124 delete m_communication
;
125 m_communication
= NULL
;
127 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
128 delete m_busDevices
[iPtr
];
131 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
133 CLockObject
lock(&m_mutex
);
134 if (!m_communication
|| m_communication
->IsOpen())
136 m_controller
->AddLog(CEC_LOG_ERROR
, "connection already opened");
140 if (!m_communication
->Open(strPort
, iBaudRate
, iTimeoutMs
))
142 m_controller
->AddLog(CEC_LOG_ERROR
, "could not open a connection");
148 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
150 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
156 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
158 if (SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true))
160 /* init the default handler */
161 if (m_busDevices
[CECDEVICE_TV
]->GetHandler()->GetVendorId() == CEC_VENDOR_UNKNOWN
)
162 m_busDevices
[CECDEVICE_TV
]->GetHandler()->InitHandler();
164 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
165 m_busScan
= new CCECBusScan(this);
166 m_busScan
->CreateThread(true);
171 m_controller
->AddLog(CEC_LOG_ERROR
, "failed to initialise the processor");
175 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
179 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
181 if (m_busDevices
[address
]->TryLogicalAddress())
183 m_logicalAddresses
.Set(address
);
190 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
192 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
193 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
194 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
195 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
198 bool CCECProcessor::FindLogicalAddressTuner(void)
200 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
201 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
202 TryLogicalAddress(CECDEVICE_TUNER2
) ||
203 TryLogicalAddress(CECDEVICE_TUNER3
) ||
204 TryLogicalAddress(CECDEVICE_TUNER4
);
207 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
209 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
210 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
211 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
212 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
215 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
217 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
218 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
221 bool CCECProcessor::FindLogicalAddresses(void)
224 m_logicalAddresses
.Clear();
227 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
229 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
232 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
233 AddLog(CEC_LOG_DEBUG
, strLog
);
235 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
236 bReturn
&= FindLogicalAddressRecordingDevice();
237 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
238 bReturn
&= FindLogicalAddressTuner();
239 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
240 bReturn
&= FindLogicalAddressPlaybackDevice();
241 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
242 bReturn
&= FindLogicalAddressAudioSystem();
248 void *CCECProcessor::Process(void)
250 bool bParseFrame(false);
252 CCECAdapterMessage msg
;
254 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
256 CLockObject
lock(&m_mutex
);
257 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
258 m_startCondition
.Signal();
263 /* only set our OSD name and active source for the primary device */
264 m_busDevices
[m_logicalAddresses
.primary
]->m_strDeviceName
= m_strDeviceName
;
265 m_busDevices
[m_logicalAddresses
.primary
]->m_bActiveSource
= true;
267 SetAckMask(m_logicalAddresses
.AckMask());
269 CLockObject
lock(&m_mutex
);
271 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
272 m_startCondition
.Signal();
281 CLockObject
lock(&m_mutex
);
282 if (m_commandBuffer
.Pop(command
))
286 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
288 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
289 command
= m_currentframe
;
294 ParseCommand(command
);
299 m_controller
->CheckKeypressTimeout();
304 m_busScan
->StopThread();
310 m_communication
->Close();
315 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
322 cec_logical_address addr
= m_logicalAddresses
.primary
;
324 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
326 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
328 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
330 addr
= (cec_logical_address
) iPtr
;
336 m_busDevices
[addr
]->SetActiveSource();
337 bReturn
= m_busDevices
[addr
]->TransmitActiveSource();
339 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
340 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
342 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
348 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
352 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
355 device
->SetActiveSource();
362 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
364 CLockObject
lock(&m_mutex
);
365 m_iStandardLineTimeout
= iTimeout
;
368 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
370 CLockObject
lock(&m_mutex
);
371 m_iRetryLineTimeout
= iTimeout
;
374 bool CCECProcessor::SetActiveView(void)
376 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
379 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
383 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
386 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
388 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
395 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
399 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
402 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
404 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
411 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
415 m_iBaseDevice
= iBaseDevice
;
417 if (!m_bStarted
&& !bForce
)
421 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
422 AddLog(CEC_LOG_DEBUG
, strLog
);
424 uint16_t iPhysicalAddress(0);
425 if (iBaseDevice
> CECDEVICE_TV
)
426 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
428 if (iPhysicalAddress
== 0xffff)
430 SetPhysicalAddress((uint16_t)iPort
* 0x1000);
436 if (iPhysicalAddress
== 0)
438 else if (iPhysicalAddress
% 0x1000 == 0)
440 else if (iPhysicalAddress
% 0x100 == 0)
442 else if (iPhysicalAddress
% 0x10 == 0)
445 while(!bReturn
&& iPos
> 0)
447 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
448 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
449 AddLog(CEC_LOG_DEBUG
, strLog
);
450 if (PhysicalAddressInUse(iPhysicalAddress
))
452 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
453 AddLog(CEC_LOG_DEBUG
, strLog
);
454 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
458 strLog
.Format("physical address %4x is free", iPhysicalAddress
);
459 AddLog(CEC_LOG_DEBUG
, strLog
);
460 SetPhysicalAddress(iPhysicalAddress
);
469 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
471 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
473 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
479 bool CCECProcessor::TransmitInactiveSource(void)
484 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
485 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
489 void CCECProcessor::LogOutput(const cec_command
&data
)
492 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
494 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
496 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
497 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
498 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
501 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
503 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
506 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
507 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
508 m_logicalAddresses
.primary
= iLogicalAddress
;
509 m_logicalAddresses
.Set(iLogicalAddress
);
510 return SetAckMask(m_logicalAddresses
.AckMask());
516 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
518 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
520 if (m_logicalAddresses
[iPtr
])
521 m_busDevices
[iPtr
]->SetMenuState(state
);
525 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
530 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
532 if (!m_logicalAddresses
.IsEmpty())
534 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
535 if (m_logicalAddresses
[iPtr
])
537 m_busDevices
[iPtr
]->SetInactiveSource();
538 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
539 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
541 return SetActiveView();
546 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
549 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
550 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
553 CLockObject
lock(&m_mutex
);
554 m_bMonitor
= bEnable
;
560 m_busScan
= new CCECBusScan(this);
561 m_busScan
->CreateThread(true);
568 m_busScan
->StopThread();
576 return SetAckMask(0);
578 return SetAckMask(m_logicalAddresses
.AckMask());
581 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
583 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
585 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
586 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
587 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
592 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
595 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
596 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
601 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
604 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
605 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
610 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
613 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
614 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
619 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
621 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
622 return m_busDevices
[m_logicalAddresses
.primary
];
624 CCECBusDevice
*device
= NULL
;
625 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
627 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
629 device
= m_busDevices
[iPtr
];
637 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
639 CCECBusDevice
*device
= NULL
;
641 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
643 if (m_busDevices
[iPtr
]->m_type
== type
)
645 device
= m_busDevices
[iPtr
];
653 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
655 return m_busDevices
[iAddress
]->GetCecVersion();
658 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
660 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
663 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
664 retVal
.device
= iAddress
;
669 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
671 if (m_busDevices
[iAddress
])
673 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
674 return (strcmp(language
->language
, "???") != 0);
679 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
681 if (m_busDevices
[iAddress
])
682 return m_busDevices
[iAddress
]->GetVendorId();
686 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
688 if (m_busDevices
[iAddress
])
689 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
693 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
695 if (m_busDevices
[iAddress
])
696 return m_busDevices
[iAddress
]->GetPowerStatus();
697 return CEC_POWER_STATUS_UNKNOWN
;
700 cec_logical_address
CCECProcessor::GetActiveSource(void)
702 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
704 if (m_busDevices
[iPtr
]->IsActiveSource())
705 return (cec_logical_address
)iPtr
;
708 return CECDEVICE_UNKNOWN
;
711 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
713 return m_busDevices
[iAddress
]->IsActiveSource();
716 bool CCECProcessor::Transmit(const cec_command
&data
)
721 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
723 /* set the number of retries */
724 if (data
.opcode
== CEC_OPCODE_NONE
)
725 output
->maxTries
= 1;
726 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
727 output
->maxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
729 bReturn
= Transmit(output
);
731 /* set to "not present" on failed ack */
732 if (output
->is_error() && output
->reply
== MSGCODE_TRANSMIT_FAILED_ACK
&&
733 output
->destination() != CECDEVICE_BROADCAST
)
734 m_busDevices
[output
->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
740 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
743 CLockObject
lock(&m_mutex
);
745 m_iLastTransmission
= GetTimeMs();
746 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
751 if (output
->tries
> 0)
752 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
754 CLockObject
msgLock(&output
->mutex
);
755 if (!m_communication
|| !m_communication
->Write(output
))
759 output
->condition
.Wait(&output
->mutex
);
760 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
762 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
767 if (output
->transmit_timeout
> 0)
769 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
770 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
774 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
< output
->maxTries
);
777 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
782 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
784 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
788 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
789 command
.parameters
.PushBack((uint8_t)opcode
);
790 command
.parameters
.PushBack((uint8_t)reason
);
795 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
798 bool bTransmitSucceeded(false);
799 uint8_t iPacketsLeft(message
->size() / 4);
801 int64_t iNow
= GetTimeMs();
802 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
804 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
806 CCECAdapterMessage msg
;
808 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
814 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
816 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
817 m_lastInitiator
= msg
.initiator();
822 bError
= msg
.is_error();
823 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
824 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
825 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
833 message
->reply
= msg
.message();
834 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
838 switch(msg
.message())
840 case MSGCODE_COMMAND_ACCEPTED
:
841 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
842 if (iPacketsLeft
> 0)
845 case MSGCODE_TRANSMIT_SUCCEEDED
:
846 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
847 bTransmitSucceeded
= (iPacketsLeft
== 0);
848 bError
= !bTransmitSucceeded
;
849 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
852 // ignore other data while waiting
860 return bTransmitSucceeded
&& !bError
;
863 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
866 bool bIsError(msg
.is_error());
871 switch(msg
.message())
873 case MSGCODE_FRAME_START
:
875 m_currentframe
.Clear();
878 m_currentframe
.initiator
= msg
.initiator();
879 m_currentframe
.destination
= msg
.destination();
880 m_currentframe
.ack
= msg
.ack();
881 m_currentframe
.eom
= msg
.eom();
883 if (m_currentframe
.ack
== 0x1)
885 m_lastInitiator
= m_currentframe
.initiator
;
886 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
890 case MSGCODE_RECEIVE_FAILED
:
892 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
893 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
896 case MSGCODE_FRAME_DATA
:
900 m_currentframe
.PushBack(msg
[1]);
901 m_currentframe
.eom
= msg
.eom();
910 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
914 void CCECProcessor::ParseCommand(cec_command
&command
)
917 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
918 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
919 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
920 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
922 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
923 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
926 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
928 cec_logical_addresses addresses
;
930 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
932 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
933 addresses
.Set((cec_logical_address
) iPtr
);
938 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
940 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
943 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
945 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
947 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
954 uint16_t CCECProcessor::GetPhysicalAddress(void) const
956 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
957 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
961 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
963 m_controller
->SetCurrentButton(iButtonCode
);
966 void CCECProcessor::AddCommand(const cec_command
&command
)
968 m_controller
->AddCommand(command
);
971 void CCECProcessor::AddKey(cec_keypress
&key
)
973 m_controller
->AddKey(key
);
976 void CCECProcessor::AddKey(void)
978 m_controller
->AddKey();
981 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
983 m_controller
->AddLog(level
, strMessage
);
986 bool CCECProcessor::SetAckMask(uint16_t iMask
)
990 strLog
.Format("setting ackmask to %2x", iMask
);
991 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
993 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
995 output
->push_back(MSGSTART
);
996 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
997 output
->push_escaped(iMask
>> 8);
998 output
->push_escaped((uint8_t)iMask
);
999 output
->push_back(MSGEND
);
1001 if ((bReturn
= Transmit(output
)) == false)
1002 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
1009 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1011 return m_busDevices
[iDestination
]->TransmitKeypress(key
);
1014 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1016 return m_busDevices
[iDestination
]->TransmitKeyRelease();
1019 const char *CCECProcessor::ToString(const cec_menu_state state
)
1023 case CEC_MENU_STATE_ACTIVATED
:
1025 case CEC_MENU_STATE_DEACTIVATED
:
1026 return "deactivated";
1032 const char *CCECProcessor::ToString(const cec_version version
)
1036 case CEC_VERSION_1_2
:
1038 case CEC_VERSION_1_2A
:
1040 case CEC_VERSION_1_3
:
1042 case CEC_VERSION_1_3A
:
1044 case CEC_VERSION_1_4
:
1051 const char *CCECProcessor::ToString(const cec_power_status status
)
1055 case CEC_POWER_STATUS_ON
:
1057 case CEC_POWER_STATUS_STANDBY
:
1059 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1060 return "in transition from on to standby";
1061 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1062 return "in transition from standby to on";
1068 const char *CCECProcessor::ToString(const cec_logical_address address
)
1072 case CECDEVICE_AUDIOSYSTEM
:
1074 case CECDEVICE_BROADCAST
:
1076 case CECDEVICE_FREEUSE
:
1078 case CECDEVICE_PLAYBACKDEVICE1
:
1079 return "Playback 1";
1080 case CECDEVICE_PLAYBACKDEVICE2
:
1081 return "Playback 2";
1082 case CECDEVICE_PLAYBACKDEVICE3
:
1083 return "Playback 3";
1084 case CECDEVICE_RECORDINGDEVICE1
:
1085 return "Recorder 1";
1086 case CECDEVICE_RECORDINGDEVICE2
:
1087 return "Recorder 2";
1088 case CECDEVICE_RECORDINGDEVICE3
:
1089 return "Recorder 3";
1090 case CECDEVICE_RESERVED1
:
1091 return "Reserved 1";
1092 case CECDEVICE_RESERVED2
:
1093 return "Reserved 2";
1094 case CECDEVICE_TUNER1
:
1096 case CECDEVICE_TUNER2
:
1098 case CECDEVICE_TUNER3
:
1100 case CECDEVICE_TUNER4
:
1109 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1113 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1114 return "skip forward wind";
1115 case CEC_DECK_CONTROL_MODE_EJECT
:
1117 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1118 return "reverse rewind";
1119 case CEC_DECK_CONTROL_MODE_STOP
:
1126 const char *CCECProcessor::ToString(const cec_deck_info status
)
1130 case CEC_DECK_INFO_PLAY
:
1132 case CEC_DECK_INFO_RECORD
:
1134 case CEC_DECK_INFO_PLAY_REVERSE
:
1135 return "play reverse";
1136 case CEC_DECK_INFO_STILL
:
1138 case CEC_DECK_INFO_SLOW
:
1140 case CEC_DECK_INFO_SLOW_REVERSE
:
1141 return "slow reverse";
1142 case CEC_DECK_INFO_FAST_FORWARD
:
1143 return "fast forward";
1144 case CEC_DECK_INFO_FAST_REVERSE
:
1145 return "fast reverse";
1146 case CEC_DECK_INFO_NO_MEDIA
:
1148 case CEC_DECK_INFO_STOP
:
1150 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1151 return "info skip forward wind";
1152 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1153 return "info skip reverse rewind";
1154 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1155 return "info index search forward";
1156 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1157 return "info index search reverse";
1158 case CEC_DECK_INFO_OTHER_STATUS
:
1165 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1169 case CEC_OPCODE_ACTIVE_SOURCE
:
1170 return "active source";
1171 case CEC_OPCODE_IMAGE_VIEW_ON
:
1172 return "image view on";
1173 case CEC_OPCODE_TEXT_VIEW_ON
:
1174 return "text view on";
1175 case CEC_OPCODE_INACTIVE_SOURCE
:
1176 return "inactive source";
1177 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1178 return "request active source";
1179 case CEC_OPCODE_ROUTING_CHANGE
:
1180 return "routing change";
1181 case CEC_OPCODE_ROUTING_INFORMATION
:
1182 return "routing information";
1183 case CEC_OPCODE_SET_STREAM_PATH
:
1184 return "set stream path";
1185 case CEC_OPCODE_STANDBY
:
1187 case CEC_OPCODE_RECORD_OFF
:
1188 return "record off";
1189 case CEC_OPCODE_RECORD_ON
:
1191 case CEC_OPCODE_RECORD_STATUS
:
1192 return "record status";
1193 case CEC_OPCODE_RECORD_TV_SCREEN
:
1194 return "record tv screen";
1195 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1196 return "clear analogue timer";
1197 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1198 return "clear digital timer";
1199 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1200 return "clear external timer";
1201 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1202 return "set analogue timer";
1203 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1204 return "set digital timer";
1205 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1206 return "set external timer";
1207 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1208 return "set timer program title";
1209 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1210 return "timer cleared status";
1211 case CEC_OPCODE_TIMER_STATUS
:
1212 return "timer status";
1213 case CEC_OPCODE_CEC_VERSION
:
1214 return "cec version";
1215 case CEC_OPCODE_GET_CEC_VERSION
:
1216 return "get cec version";
1217 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1218 return "give physical address";
1219 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1220 return "get menu language";
1221 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1222 return "report physical address";
1223 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1224 return "set menu language";
1225 case CEC_OPCODE_DECK_CONTROL
:
1226 return "deck control";
1227 case CEC_OPCODE_DECK_STATUS
:
1228 return "deck status";
1229 case CEC_OPCODE_GIVE_DECK_STATUS
:
1230 return "give deck status";
1231 case CEC_OPCODE_PLAY
:
1233 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1234 return "give tuner status";
1235 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1236 return "select analogue service";
1237 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1238 return "set digital service";
1239 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1240 return "tuner device status";
1241 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1242 return "tuner step decrement";
1243 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1244 return "tuner step increment";
1245 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1246 return "device vendor id";
1247 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1248 return "give device vendor id";
1249 case CEC_OPCODE_VENDOR_COMMAND
:
1250 return "vendor command";
1251 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1252 return "vendor command with id";
1253 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1254 return "vendor remote button down";
1255 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1256 return "vendor remote button up";
1257 case CEC_OPCODE_SET_OSD_STRING
:
1258 return "set osd string";
1259 case CEC_OPCODE_GIVE_OSD_NAME
:
1260 return "give osd name";
1261 case CEC_OPCODE_SET_OSD_NAME
:
1262 return "set osd name";
1263 case CEC_OPCODE_MENU_REQUEST
:
1264 return "menu request";
1265 case CEC_OPCODE_MENU_STATUS
:
1266 return "menu status";
1267 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1268 return "user control pressed";
1269 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1270 return "user control release";
1271 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1272 return "give device power status";
1273 case CEC_OPCODE_REPORT_POWER_STATUS
:
1274 return "report power status";
1275 case CEC_OPCODE_FEATURE_ABORT
:
1276 return "feature abort";
1277 case CEC_OPCODE_ABORT
:
1279 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1280 return "give audio status";
1281 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1282 return "give audio mode status";
1283 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1284 return "report audio status";
1285 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1286 return "set system audio mode";
1287 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1288 return "system audio mode request";
1289 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1290 return "system audio mode status";
1291 case CEC_OPCODE_SET_AUDIO_RATE
:
1292 return "set audio rate";
1298 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1302 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1304 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1311 const char *CCECProcessor::ToString(const cec_audio_status status
)
1313 // TODO this is a mask
1317 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1321 case CEC_VENDOR_SAMSUNG
:
1325 case CEC_VENDOR_PANASONIC
:
1327 case CEC_VENDOR_PIONEER
:
1329 case CEC_VENDOR_ONKYO
:
1331 case CEC_VENDOR_YAMAHA
:
1333 case CEC_VENDOR_PHILIPS
:
1340 void *CCECBusScan::Process(void)
1342 CCECBusDevice
*device(NULL
);
1343 uint8_t iCounter(0);
1345 while (!IsStopped())
1347 if (++iCounter
< 10)
1352 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1354 device
= m_processor
->m_busDevices
[iPtr
];
1356 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1360 device
->GetVendorId();
1364 device
->GetPowerStatus(true);
1372 void CCECBusScan::WaitUntilIdle(void)
1377 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1378 while (iWaitTime
> 0)
1381 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1385 bool CCECProcessor::StartBootloader(void)
1387 return m_communication
->StartBootloader();
1390 bool CCECProcessor::PingAdapter(void)
1392 return m_communication
->PingAdapter();