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 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
339 bReturn
= m_busDevices
[addr
]->TransmitActiveSource();
341 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
342 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
344 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
351 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
355 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
358 device
->SetActiveSource();
365 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
367 CLockObject
lock(&m_mutex
);
368 m_iStandardLineTimeout
= iTimeout
;
371 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
373 CLockObject
lock(&m_mutex
);
374 m_iRetryLineTimeout
= iTimeout
;
377 bool CCECProcessor::SetActiveView(void)
379 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
382 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
386 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
389 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
391 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
398 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
402 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
405 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
407 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
414 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
418 m_iBaseDevice
= iBaseDevice
;
420 if (!m_bStarted
&& !bForce
)
424 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
425 AddLog(CEC_LOG_DEBUG
, strLog
);
427 uint16_t iPhysicalAddress(0);
428 if (iBaseDevice
> CECDEVICE_TV
)
429 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
431 if (iPhysicalAddress
== 0xffff)
433 SetPhysicalAddress((uint16_t)iPort
* 0x1000);
439 if (iPhysicalAddress
== 0)
441 else if (iPhysicalAddress
% 0x1000 == 0)
443 else if (iPhysicalAddress
% 0x100 == 0)
445 else if (iPhysicalAddress
% 0x10 == 0)
448 while(!bReturn
&& iPos
> 0)
450 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
451 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
452 AddLog(CEC_LOG_DEBUG
, strLog
);
453 if (PhysicalAddressInUse(iPhysicalAddress
))
455 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
456 AddLog(CEC_LOG_DEBUG
, strLog
);
457 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
461 strLog
.Format("physical address %4x is free", iPhysicalAddress
);
462 AddLog(CEC_LOG_DEBUG
, strLog
);
463 SetPhysicalAddress(iPhysicalAddress
);
472 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
474 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
476 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
482 bool CCECProcessor::TransmitInactiveSource(void)
487 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
488 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
492 void CCECProcessor::LogOutput(const cec_command
&data
)
495 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
497 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
499 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
500 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
501 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
504 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
506 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
509 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
510 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
511 m_logicalAddresses
.primary
= iLogicalAddress
;
512 m_logicalAddresses
.Set(iLogicalAddress
);
513 return SetAckMask(m_logicalAddresses
.AckMask());
519 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
521 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
523 if (m_logicalAddresses
[iPtr
])
524 m_busDevices
[iPtr
]->SetMenuState(state
);
528 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
533 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
535 if (!m_logicalAddresses
.IsEmpty())
537 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
538 if (m_logicalAddresses
[iPtr
])
540 m_busDevices
[iPtr
]->SetInactiveSource();
541 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
542 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
544 return SetActiveView();
549 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
552 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
553 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
556 CLockObject
lock(&m_mutex
);
557 m_bMonitor
= bEnable
;
563 m_busScan
= new CCECBusScan(this);
564 m_busScan
->CreateThread(true);
571 m_busScan
->StopThread();
579 return SetAckMask(0);
581 return SetAckMask(m_logicalAddresses
.AckMask());
584 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
586 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
588 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
589 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
590 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
595 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
598 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
599 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
604 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
607 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
608 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
613 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
616 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
617 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
622 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
624 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
625 return m_busDevices
[m_logicalAddresses
.primary
];
627 CCECBusDevice
*device
= NULL
;
628 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
630 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
632 device
= m_busDevices
[iPtr
];
640 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
642 CCECBusDevice
*device
= NULL
;
644 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
646 if (m_busDevices
[iPtr
]->m_type
== type
)
648 device
= m_busDevices
[iPtr
];
656 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
658 return m_busDevices
[iAddress
]->GetCecVersion();
661 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
663 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
666 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
667 retVal
.device
= iAddress
;
672 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
674 if (m_busDevices
[iAddress
])
676 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
677 return (strcmp(language
->language
, "???") != 0);
682 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
684 if (m_busDevices
[iAddress
])
685 return m_busDevices
[iAddress
]->GetVendorId();
689 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
691 if (m_busDevices
[iAddress
])
692 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
696 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
698 if (m_busDevices
[iAddress
])
699 return m_busDevices
[iAddress
]->GetPowerStatus();
700 return CEC_POWER_STATUS_UNKNOWN
;
703 cec_logical_address
CCECProcessor::GetActiveSource(void)
705 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
707 if (m_busDevices
[iPtr
]->IsActiveSource())
708 return (cec_logical_address
)iPtr
;
711 return CECDEVICE_UNKNOWN
;
714 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
716 return m_busDevices
[iAddress
]->IsActiveSource();
719 bool CCECProcessor::Transmit(const cec_command
&data
)
724 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
726 /* set the number of retries */
727 if (data
.opcode
== CEC_OPCODE_NONE
)
728 output
->maxTries
= 1;
729 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
730 output
->maxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
732 bReturn
= Transmit(output
);
734 /* set to "not present" on failed ack */
735 if (output
->is_error() && output
->reply
== MSGCODE_TRANSMIT_FAILED_ACK
&&
736 output
->destination() != CECDEVICE_BROADCAST
)
737 m_busDevices
[output
->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
743 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
746 CLockObject
lock(&m_mutex
);
748 m_iLastTransmission
= GetTimeMs();
749 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
754 if (output
->tries
> 0)
755 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
757 CLockObject
msgLock(&output
->mutex
);
758 if (!m_communication
|| !m_communication
->Write(output
))
762 output
->condition
.Wait(&output
->mutex
);
763 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
765 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
770 if (output
->transmit_timeout
> 0)
772 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
773 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
777 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
< output
->maxTries
);
780 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
785 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
787 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
791 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
792 command
.parameters
.PushBack((uint8_t)opcode
);
793 command
.parameters
.PushBack((uint8_t)reason
);
798 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
801 bool bTransmitSucceeded(false);
802 uint8_t iPacketsLeft(message
->size() / 4);
804 int64_t iNow
= GetTimeMs();
805 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
807 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
809 CCECAdapterMessage msg
;
811 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
817 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
819 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
820 m_lastInitiator
= msg
.initiator();
825 bError
= msg
.is_error();
826 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
827 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
828 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
836 message
->reply
= msg
.message();
837 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
841 switch(msg
.message())
843 case MSGCODE_COMMAND_ACCEPTED
:
844 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
845 if (iPacketsLeft
> 0)
848 case MSGCODE_TRANSMIT_SUCCEEDED
:
849 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
850 bTransmitSucceeded
= (iPacketsLeft
== 0);
851 bError
= !bTransmitSucceeded
;
852 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
855 // ignore other data while waiting
863 return bTransmitSucceeded
&& !bError
;
866 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
869 bool bIsError(msg
.is_error());
874 switch(msg
.message())
876 case MSGCODE_FRAME_START
:
878 m_currentframe
.Clear();
881 m_currentframe
.initiator
= msg
.initiator();
882 m_currentframe
.destination
= msg
.destination();
883 m_currentframe
.ack
= msg
.ack();
884 m_currentframe
.eom
= msg
.eom();
886 if (m_currentframe
.ack
== 0x1)
888 m_lastInitiator
= m_currentframe
.initiator
;
889 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
893 case MSGCODE_RECEIVE_FAILED
:
895 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
896 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
899 case MSGCODE_FRAME_DATA
:
903 m_currentframe
.PushBack(msg
[1]);
904 m_currentframe
.eom
= msg
.eom();
913 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
917 void CCECProcessor::ParseCommand(cec_command
&command
)
920 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
921 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
922 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
923 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
925 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
926 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
929 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
931 cec_logical_addresses addresses
;
933 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
935 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
936 addresses
.Set((cec_logical_address
) iPtr
);
941 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
943 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
946 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
948 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
950 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
957 uint16_t CCECProcessor::GetPhysicalAddress(void) const
959 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
960 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
964 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
966 m_controller
->SetCurrentButton(iButtonCode
);
969 void CCECProcessor::AddCommand(const cec_command
&command
)
971 m_controller
->AddCommand(command
);
974 void CCECProcessor::AddKey(cec_keypress
&key
)
976 m_controller
->AddKey(key
);
979 void CCECProcessor::AddKey(void)
981 m_controller
->AddKey();
984 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
986 m_controller
->AddLog(level
, strMessage
);
989 bool CCECProcessor::SetAckMask(uint16_t iMask
)
993 strLog
.Format("setting ackmask to %2x", iMask
);
994 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
996 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
998 output
->push_back(MSGSTART
);
999 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
1000 output
->push_escaped(iMask
>> 8);
1001 output
->push_escaped((uint8_t)iMask
);
1002 output
->push_back(MSGEND
);
1004 if ((bReturn
= Transmit(output
)) == false)
1005 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
1012 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1014 return m_busDevices
[iDestination
]->TransmitKeypress(key
);
1017 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1019 return m_busDevices
[iDestination
]->TransmitKeyRelease();
1022 const char *CCECProcessor::ToString(const cec_menu_state state
)
1026 case CEC_MENU_STATE_ACTIVATED
:
1028 case CEC_MENU_STATE_DEACTIVATED
:
1029 return "deactivated";
1035 const char *CCECProcessor::ToString(const cec_version version
)
1039 case CEC_VERSION_1_2
:
1041 case CEC_VERSION_1_2A
:
1043 case CEC_VERSION_1_3
:
1045 case CEC_VERSION_1_3A
:
1047 case CEC_VERSION_1_4
:
1054 const char *CCECProcessor::ToString(const cec_power_status status
)
1058 case CEC_POWER_STATUS_ON
:
1060 case CEC_POWER_STATUS_STANDBY
:
1062 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1063 return "in transition from on to standby";
1064 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1065 return "in transition from standby to on";
1071 const char *CCECProcessor::ToString(const cec_logical_address address
)
1075 case CECDEVICE_AUDIOSYSTEM
:
1077 case CECDEVICE_BROADCAST
:
1079 case CECDEVICE_FREEUSE
:
1081 case CECDEVICE_PLAYBACKDEVICE1
:
1082 return "Playback 1";
1083 case CECDEVICE_PLAYBACKDEVICE2
:
1084 return "Playback 2";
1085 case CECDEVICE_PLAYBACKDEVICE3
:
1086 return "Playback 3";
1087 case CECDEVICE_RECORDINGDEVICE1
:
1088 return "Recorder 1";
1089 case CECDEVICE_RECORDINGDEVICE2
:
1090 return "Recorder 2";
1091 case CECDEVICE_RECORDINGDEVICE3
:
1092 return "Recorder 3";
1093 case CECDEVICE_RESERVED1
:
1094 return "Reserved 1";
1095 case CECDEVICE_RESERVED2
:
1096 return "Reserved 2";
1097 case CECDEVICE_TUNER1
:
1099 case CECDEVICE_TUNER2
:
1101 case CECDEVICE_TUNER3
:
1103 case CECDEVICE_TUNER4
:
1112 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1116 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1117 return "skip forward wind";
1118 case CEC_DECK_CONTROL_MODE_EJECT
:
1120 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1121 return "reverse rewind";
1122 case CEC_DECK_CONTROL_MODE_STOP
:
1129 const char *CCECProcessor::ToString(const cec_deck_info status
)
1133 case CEC_DECK_INFO_PLAY
:
1135 case CEC_DECK_INFO_RECORD
:
1137 case CEC_DECK_INFO_PLAY_REVERSE
:
1138 return "play reverse";
1139 case CEC_DECK_INFO_STILL
:
1141 case CEC_DECK_INFO_SLOW
:
1143 case CEC_DECK_INFO_SLOW_REVERSE
:
1144 return "slow reverse";
1145 case CEC_DECK_INFO_FAST_FORWARD
:
1146 return "fast forward";
1147 case CEC_DECK_INFO_FAST_REVERSE
:
1148 return "fast reverse";
1149 case CEC_DECK_INFO_NO_MEDIA
:
1151 case CEC_DECK_INFO_STOP
:
1153 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1154 return "info skip forward wind";
1155 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1156 return "info skip reverse rewind";
1157 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1158 return "info index search forward";
1159 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1160 return "info index search reverse";
1161 case CEC_DECK_INFO_OTHER_STATUS
:
1168 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1172 case CEC_OPCODE_ACTIVE_SOURCE
:
1173 return "active source";
1174 case CEC_OPCODE_IMAGE_VIEW_ON
:
1175 return "image view on";
1176 case CEC_OPCODE_TEXT_VIEW_ON
:
1177 return "text view on";
1178 case CEC_OPCODE_INACTIVE_SOURCE
:
1179 return "inactive source";
1180 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1181 return "request active source";
1182 case CEC_OPCODE_ROUTING_CHANGE
:
1183 return "routing change";
1184 case CEC_OPCODE_ROUTING_INFORMATION
:
1185 return "routing information";
1186 case CEC_OPCODE_SET_STREAM_PATH
:
1187 return "set stream path";
1188 case CEC_OPCODE_STANDBY
:
1190 case CEC_OPCODE_RECORD_OFF
:
1191 return "record off";
1192 case CEC_OPCODE_RECORD_ON
:
1194 case CEC_OPCODE_RECORD_STATUS
:
1195 return "record status";
1196 case CEC_OPCODE_RECORD_TV_SCREEN
:
1197 return "record tv screen";
1198 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1199 return "clear analogue timer";
1200 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1201 return "clear digital timer";
1202 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1203 return "clear external timer";
1204 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1205 return "set analogue timer";
1206 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1207 return "set digital timer";
1208 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1209 return "set external timer";
1210 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1211 return "set timer program title";
1212 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1213 return "timer cleared status";
1214 case CEC_OPCODE_TIMER_STATUS
:
1215 return "timer status";
1216 case CEC_OPCODE_CEC_VERSION
:
1217 return "cec version";
1218 case CEC_OPCODE_GET_CEC_VERSION
:
1219 return "get cec version";
1220 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1221 return "give physical address";
1222 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1223 return "get menu language";
1224 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1225 return "report physical address";
1226 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1227 return "set menu language";
1228 case CEC_OPCODE_DECK_CONTROL
:
1229 return "deck control";
1230 case CEC_OPCODE_DECK_STATUS
:
1231 return "deck status";
1232 case CEC_OPCODE_GIVE_DECK_STATUS
:
1233 return "give deck status";
1234 case CEC_OPCODE_PLAY
:
1236 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1237 return "give tuner status";
1238 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1239 return "select analogue service";
1240 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1241 return "set digital service";
1242 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1243 return "tuner device status";
1244 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1245 return "tuner step decrement";
1246 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1247 return "tuner step increment";
1248 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1249 return "device vendor id";
1250 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1251 return "give device vendor id";
1252 case CEC_OPCODE_VENDOR_COMMAND
:
1253 return "vendor command";
1254 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1255 return "vendor command with id";
1256 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1257 return "vendor remote button down";
1258 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1259 return "vendor remote button up";
1260 case CEC_OPCODE_SET_OSD_STRING
:
1261 return "set osd string";
1262 case CEC_OPCODE_GIVE_OSD_NAME
:
1263 return "give osd name";
1264 case CEC_OPCODE_SET_OSD_NAME
:
1265 return "set osd name";
1266 case CEC_OPCODE_MENU_REQUEST
:
1267 return "menu request";
1268 case CEC_OPCODE_MENU_STATUS
:
1269 return "menu status";
1270 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1271 return "user control pressed";
1272 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1273 return "user control release";
1274 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1275 return "give device power status";
1276 case CEC_OPCODE_REPORT_POWER_STATUS
:
1277 return "report power status";
1278 case CEC_OPCODE_FEATURE_ABORT
:
1279 return "feature abort";
1280 case CEC_OPCODE_ABORT
:
1282 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1283 return "give audio status";
1284 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1285 return "give audio mode status";
1286 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1287 return "report audio status";
1288 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1289 return "set system audio mode";
1290 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1291 return "system audio mode request";
1292 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1293 return "system audio mode status";
1294 case CEC_OPCODE_SET_AUDIO_RATE
:
1295 return "set audio rate";
1301 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1305 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1307 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1314 const char *CCECProcessor::ToString(const cec_audio_status status
)
1316 // TODO this is a mask
1320 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1324 case CEC_VENDOR_SAMSUNG
:
1328 case CEC_VENDOR_PANASONIC
:
1330 case CEC_VENDOR_PIONEER
:
1332 case CEC_VENDOR_ONKYO
:
1334 case CEC_VENDOR_YAMAHA
:
1336 case CEC_VENDOR_PHILIPS
:
1343 void *CCECBusScan::Process(void)
1345 CCECBusDevice
*device(NULL
);
1346 uint8_t iCounter(0);
1348 while (!IsStopped())
1350 if (++iCounter
< 10)
1355 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1357 device
= m_processor
->m_busDevices
[iPtr
];
1359 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1363 device
->GetVendorId();
1367 device
->GetPowerStatus(true);
1375 void CCECBusScan::WaitUntilIdle(void)
1380 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1381 while (iWaitTime
> 0)
1384 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1388 bool CCECProcessor::StartBootloader(void)
1390 return m_communication
->StartBootloader();
1393 bool CCECProcessor::PingAdapter(void)
1395 return m_communication
->PingAdapter();