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
),
58 m_iStandardLineTimeout(3),
59 m_iRetryLineTimeout(3),
60 m_iLastTransmission(0)
62 m_communication
= new CAdapterCommunication(this);
63 m_logicalAddresses
.Clear();
64 m_logicalAddresses
.Set(iLogicalAddress
);
66 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
67 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
70 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
) :
72 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
73 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
74 m_strDeviceName(strDeviceName
),
76 m_controller(controller
),
78 m_iStandardLineTimeout(3),
79 m_iRetryLineTimeout(3),
80 m_iLastTransmission(0)
82 m_communication
= new CAdapterCommunication(this);
83 m_logicalAddresses
.Clear();
84 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
88 case CECDEVICE_AUDIOSYSTEM
:
89 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
91 case CECDEVICE_PLAYBACKDEVICE1
:
92 case CECDEVICE_PLAYBACKDEVICE2
:
93 case CECDEVICE_PLAYBACKDEVICE3
:
94 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
96 case CECDEVICE_RECORDINGDEVICE1
:
97 case CECDEVICE_RECORDINGDEVICE2
:
98 case CECDEVICE_RECORDINGDEVICE3
:
99 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
101 case CECDEVICE_TUNER1
:
102 case CECDEVICE_TUNER2
:
103 case CECDEVICE_TUNER3
:
104 case CECDEVICE_TUNER4
:
105 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
108 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
111 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
117 CCECProcessor::~CCECProcessor(void)
120 m_startCondition
.Broadcast();
123 delete m_communication
;
124 m_communication
= NULL
;
126 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
127 delete m_busDevices
[iPtr
];
130 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
132 CLockObject
lock(&m_mutex
);
133 if (!m_communication
|| m_communication
->IsOpen())
135 m_controller
->AddLog(CEC_LOG_ERROR
, "connection already opened");
139 if (!m_communication
->Open(strPort
, iBaudRate
, iTimeoutMs
))
141 m_controller
->AddLog(CEC_LOG_ERROR
, "could not open a connection");
147 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
149 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
154 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
156 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
162 /* only set our OSD name and active source for the primary device */
163 m_busDevices
[m_logicalAddresses
.primary
]->m_strDeviceName
= m_strDeviceName
;
164 m_busDevices
[m_logicalAddresses
.primary
]->m_bActiveSource
= true;
166 SetAckMask(m_logicalAddresses
.AckMask());
169 m_busDevices
[CECDEVICE_TV
]->GetVendorId();
171 if (SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true))
173 /* init the default handler */
174 if (m_busDevices
[CECDEVICE_TV
]->GetHandler()->GetVendorId() == CEC_VENDOR_UNKNOWN
)
175 m_busDevices
[CECDEVICE_TV
]->GetHandler()->InitHandler();
177 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
182 m_controller
->AddLog(CEC_LOG_ERROR
, "failed to initialise the processor");
186 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
190 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
192 if (m_busDevices
[address
]->TryLogicalAddress())
194 m_logicalAddresses
.Set(address
);
201 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
203 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
204 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
205 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
206 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
209 bool CCECProcessor::FindLogicalAddressTuner(void)
211 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
212 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
213 TryLogicalAddress(CECDEVICE_TUNER2
) ||
214 TryLogicalAddress(CECDEVICE_TUNER3
) ||
215 TryLogicalAddress(CECDEVICE_TUNER4
);
218 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
220 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
221 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
222 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
223 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
226 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
228 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
229 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
232 bool CCECProcessor::FindLogicalAddresses(void)
235 m_logicalAddresses
.Clear();
238 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
240 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
243 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
244 AddLog(CEC_LOG_DEBUG
, strLog
);
246 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
247 bReturn
&= FindLogicalAddressRecordingDevice();
248 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
249 bReturn
&= FindLogicalAddressTuner();
250 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
251 bReturn
&= FindLogicalAddressPlaybackDevice();
252 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
253 bReturn
&= FindLogicalAddressAudioSystem();
259 void *CCECProcessor::Process(void)
261 bool bParseFrame(false);
263 CCECAdapterMessage msg
;
266 CLockObject
lock(&m_mutex
);
268 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
269 m_startCondition
.Signal();
278 CLockObject
lock(&m_mutex
);
279 if (m_commandBuffer
.Pop(command
))
283 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
285 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
286 command
= m_currentframe
;
291 ParseCommand(command
);
296 m_controller
->CheckKeypressTimeout();
300 m_communication
->Close();
305 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
312 cec_logical_address addr
= m_logicalAddresses
.primary
;
314 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
316 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
318 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
320 addr
= (cec_logical_address
) iPtr
;
326 m_busDevices
[addr
]->SetActiveSource();
327 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
329 bReturn
= m_busDevices
[addr
]->TransmitActiveSource();
331 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
332 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
334 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
341 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
345 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
348 device
->SetActiveSource();
355 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
357 CLockObject
lock(&m_mutex
);
358 m_iStandardLineTimeout
= iTimeout
;
361 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
363 CLockObject
lock(&m_mutex
);
364 m_iRetryLineTimeout
= iTimeout
;
367 bool CCECProcessor::SetActiveView(void)
369 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
372 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
376 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
379 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
381 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
388 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
392 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
395 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
397 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
404 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
408 m_iBaseDevice
= iBaseDevice
;
410 if (!m_bStarted
&& !bForce
)
414 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
415 AddLog(CEC_LOG_DEBUG
, strLog
);
417 uint16_t iPhysicalAddress(0);
418 if (iBaseDevice
> CECDEVICE_TV
)
419 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
421 if (iPhysicalAddress
== 0xffff)
423 SetPhysicalAddress((uint16_t)iPort
* 0x1000);
429 if (iPhysicalAddress
== 0)
431 else if (iPhysicalAddress
% 0x1000 == 0)
433 else if (iPhysicalAddress
% 0x100 == 0)
435 else if (iPhysicalAddress
% 0x10 == 0)
438 while(!bReturn
&& iPos
> 0)
440 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
441 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
442 AddLog(CEC_LOG_DEBUG
, strLog
);
443 if (PhysicalAddressInUse(iPhysicalAddress
))
445 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
446 AddLog(CEC_LOG_DEBUG
, strLog
);
447 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
451 strLog
.Format("physical address %4x is free", iPhysicalAddress
);
452 AddLog(CEC_LOG_DEBUG
, strLog
);
453 SetPhysicalAddress(iPhysicalAddress
);
462 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
464 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
466 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
472 bool CCECProcessor::TransmitInactiveSource(void)
477 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
478 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
482 void CCECProcessor::LogOutput(const cec_command
&data
)
485 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
487 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
489 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
490 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
491 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
494 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
496 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
499 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
500 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
501 m_logicalAddresses
.primary
= iLogicalAddress
;
502 m_logicalAddresses
.Set(iLogicalAddress
);
503 return SetAckMask(m_logicalAddresses
.AckMask());
509 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
511 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
513 if (m_logicalAddresses
[iPtr
])
514 m_busDevices
[iPtr
]->SetMenuState(state
);
518 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
523 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
525 if (!m_logicalAddresses
.IsEmpty())
527 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
528 if (m_logicalAddresses
[iPtr
])
530 m_busDevices
[iPtr
]->SetInactiveSource();
531 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
532 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
534 return SetActiveView();
539 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
542 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
543 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
546 CLockObject
lock(&m_mutex
);
547 m_bMonitor
= bEnable
;
551 return SetAckMask(0);
553 return SetAckMask(m_logicalAddresses
.AckMask());
556 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
558 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
560 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
561 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
562 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
567 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
570 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
571 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
576 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
579 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
580 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
585 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
588 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
589 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
594 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
596 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
597 return m_busDevices
[m_logicalAddresses
.primary
];
599 CCECBusDevice
*device
= NULL
;
600 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
602 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
604 device
= m_busDevices
[iPtr
];
612 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
614 CCECBusDevice
*device
= NULL
;
616 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
618 if (m_busDevices
[iPtr
]->m_type
== type
)
620 device
= m_busDevices
[iPtr
];
628 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
630 CCECBusDevice
*device(NULL
);
631 cec_logical_address primary
= m_logicalAddresses
.primary
;
632 if (primary
!= CECDEVICE_UNKNOWN
)
633 device
= m_busDevices
[primary
];
637 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
639 return m_busDevices
[iAddress
]->GetCecVersion();
642 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
644 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
647 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
648 retVal
.device
= iAddress
;
653 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
655 if (m_busDevices
[iAddress
])
657 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
658 return (strcmp(language
->language
, "???") != 0);
663 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
665 if (m_busDevices
[iAddress
])
666 return m_busDevices
[iAddress
]->GetVendorId();
670 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
672 if (m_busDevices
[iAddress
])
673 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
677 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
679 if (m_busDevices
[iAddress
])
680 return m_busDevices
[iAddress
]->GetPowerStatus();
681 return CEC_POWER_STATUS_UNKNOWN
;
684 cec_logical_address
CCECProcessor::GetActiveSource(void)
686 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
688 if (m_busDevices
[iPtr
]->IsActiveSource())
689 return (cec_logical_address
)iPtr
;
692 return CECDEVICE_UNKNOWN
;
695 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
697 return m_busDevices
[iAddress
]->IsActiveSource();
700 bool CCECProcessor::Transmit(const cec_command
&data
)
705 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
707 /* set the number of retries */
708 if (data
.opcode
== CEC_OPCODE_NONE
)
709 output
->maxTries
= 1;
710 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
711 output
->maxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
713 bReturn
= Transmit(output
);
715 /* set to "not present" on failed ack */
716 if (output
->is_error() && output
->reply
== MSGCODE_TRANSMIT_FAILED_ACK
&&
717 output
->destination() != CECDEVICE_BROADCAST
)
718 m_busDevices
[output
->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
724 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
727 CLockObject
lock(&m_mutex
);
729 m_iLastTransmission
= GetTimeMs();
730 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
735 if (output
->tries
> 0)
736 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
738 CLockObject
msgLock(&output
->mutex
);
739 if (!m_communication
|| !m_communication
->Write(output
))
743 output
->condition
.Wait(&output
->mutex
);
744 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
746 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
751 if (output
->transmit_timeout
> 0)
753 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
754 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
758 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
< output
->maxTries
);
761 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
766 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
768 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
772 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
773 command
.parameters
.PushBack((uint8_t)opcode
);
774 command
.parameters
.PushBack((uint8_t)reason
);
779 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
782 bool bTransmitSucceeded(false);
783 uint8_t iPacketsLeft(message
->size() / 4);
785 int64_t iNow
= GetTimeMs();
786 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
788 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
790 CCECAdapterMessage msg
;
792 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
798 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
800 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
801 m_lastInitiator
= msg
.initiator();
806 bError
= msg
.is_error();
807 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
808 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
809 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
817 message
->reply
= msg
.message();
818 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
822 switch(msg
.message())
824 case MSGCODE_COMMAND_ACCEPTED
:
825 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
826 if (iPacketsLeft
> 0)
829 case MSGCODE_TRANSMIT_SUCCEEDED
:
830 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
831 bTransmitSucceeded
= (iPacketsLeft
== 0);
832 bError
= !bTransmitSucceeded
;
833 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
836 // ignore other data while waiting
844 return bTransmitSucceeded
&& !bError
;
847 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
850 bool bIsError(msg
.is_error());
855 switch(msg
.message())
857 case MSGCODE_FRAME_START
:
859 m_currentframe
.Clear();
862 m_currentframe
.initiator
= msg
.initiator();
863 m_currentframe
.destination
= msg
.destination();
864 m_currentframe
.ack
= msg
.ack();
865 m_currentframe
.eom
= msg
.eom();
867 if (m_currentframe
.ack
== 0x1)
869 m_lastInitiator
= m_currentframe
.initiator
;
870 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
874 case MSGCODE_RECEIVE_FAILED
:
876 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
877 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
880 case MSGCODE_FRAME_DATA
:
884 m_currentframe
.PushBack(msg
[1]);
885 m_currentframe
.eom
= msg
.eom();
894 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
898 void CCECProcessor::ParseCommand(cec_command
&command
)
901 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
902 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
903 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
904 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
906 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
907 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
910 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
912 cec_logical_addresses addresses
;
914 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
916 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
917 addresses
.Set((cec_logical_address
) iPtr
);
922 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
924 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
927 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
929 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
931 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
938 uint16_t CCECProcessor::GetPhysicalAddress(void) const
940 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
941 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
945 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
947 m_controller
->SetCurrentButton(iButtonCode
);
950 void CCECProcessor::AddCommand(const cec_command
&command
)
952 m_controller
->AddCommand(command
);
955 void CCECProcessor::AddKey(cec_keypress
&key
)
957 m_controller
->AddKey(key
);
960 void CCECProcessor::AddKey(void)
962 m_controller
->AddKey();
965 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
967 m_controller
->AddLog(level
, strMessage
);
970 bool CCECProcessor::SetAckMask(uint16_t iMask
)
974 strLog
.Format("setting ackmask to %2x", iMask
);
975 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
977 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
979 output
->push_back(MSGSTART
);
980 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
981 output
->push_escaped(iMask
>> 8);
982 output
->push_escaped((uint8_t)iMask
);
983 output
->push_back(MSGEND
);
985 if ((bReturn
= Transmit(output
)) == false)
986 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
993 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
995 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
998 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1000 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1003 const char *CCECProcessor::ToString(const cec_menu_state state
)
1007 case CEC_MENU_STATE_ACTIVATED
:
1009 case CEC_MENU_STATE_DEACTIVATED
:
1010 return "deactivated";
1016 const char *CCECProcessor::ToString(const cec_version version
)
1020 case CEC_VERSION_1_2
:
1022 case CEC_VERSION_1_2A
:
1024 case CEC_VERSION_1_3
:
1026 case CEC_VERSION_1_3A
:
1028 case CEC_VERSION_1_4
:
1035 const char *CCECProcessor::ToString(const cec_power_status status
)
1039 case CEC_POWER_STATUS_ON
:
1041 case CEC_POWER_STATUS_STANDBY
:
1043 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1044 return "in transition from on to standby";
1045 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1046 return "in transition from standby to on";
1052 const char *CCECProcessor::ToString(const cec_logical_address address
)
1056 case CECDEVICE_AUDIOSYSTEM
:
1058 case CECDEVICE_BROADCAST
:
1060 case CECDEVICE_FREEUSE
:
1062 case CECDEVICE_PLAYBACKDEVICE1
:
1063 return "Playback 1";
1064 case CECDEVICE_PLAYBACKDEVICE2
:
1065 return "Playback 2";
1066 case CECDEVICE_PLAYBACKDEVICE3
:
1067 return "Playback 3";
1068 case CECDEVICE_RECORDINGDEVICE1
:
1069 return "Recorder 1";
1070 case CECDEVICE_RECORDINGDEVICE2
:
1071 return "Recorder 2";
1072 case CECDEVICE_RECORDINGDEVICE3
:
1073 return "Recorder 3";
1074 case CECDEVICE_RESERVED1
:
1075 return "Reserved 1";
1076 case CECDEVICE_RESERVED2
:
1077 return "Reserved 2";
1078 case CECDEVICE_TUNER1
:
1080 case CECDEVICE_TUNER2
:
1082 case CECDEVICE_TUNER3
:
1084 case CECDEVICE_TUNER4
:
1093 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1097 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1098 return "skip forward wind";
1099 case CEC_DECK_CONTROL_MODE_EJECT
:
1101 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1102 return "reverse rewind";
1103 case CEC_DECK_CONTROL_MODE_STOP
:
1110 const char *CCECProcessor::ToString(const cec_deck_info status
)
1114 case CEC_DECK_INFO_PLAY
:
1116 case CEC_DECK_INFO_RECORD
:
1118 case CEC_DECK_INFO_PLAY_REVERSE
:
1119 return "play reverse";
1120 case CEC_DECK_INFO_STILL
:
1122 case CEC_DECK_INFO_SLOW
:
1124 case CEC_DECK_INFO_SLOW_REVERSE
:
1125 return "slow reverse";
1126 case CEC_DECK_INFO_FAST_FORWARD
:
1127 return "fast forward";
1128 case CEC_DECK_INFO_FAST_REVERSE
:
1129 return "fast reverse";
1130 case CEC_DECK_INFO_NO_MEDIA
:
1132 case CEC_DECK_INFO_STOP
:
1134 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1135 return "info skip forward wind";
1136 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1137 return "info skip reverse rewind";
1138 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1139 return "info index search forward";
1140 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1141 return "info index search reverse";
1142 case CEC_DECK_INFO_OTHER_STATUS
:
1149 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1153 case CEC_OPCODE_ACTIVE_SOURCE
:
1154 return "active source";
1155 case CEC_OPCODE_IMAGE_VIEW_ON
:
1156 return "image view on";
1157 case CEC_OPCODE_TEXT_VIEW_ON
:
1158 return "text view on";
1159 case CEC_OPCODE_INACTIVE_SOURCE
:
1160 return "inactive source";
1161 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1162 return "request active source";
1163 case CEC_OPCODE_ROUTING_CHANGE
:
1164 return "routing change";
1165 case CEC_OPCODE_ROUTING_INFORMATION
:
1166 return "routing information";
1167 case CEC_OPCODE_SET_STREAM_PATH
:
1168 return "set stream path";
1169 case CEC_OPCODE_STANDBY
:
1171 case CEC_OPCODE_RECORD_OFF
:
1172 return "record off";
1173 case CEC_OPCODE_RECORD_ON
:
1175 case CEC_OPCODE_RECORD_STATUS
:
1176 return "record status";
1177 case CEC_OPCODE_RECORD_TV_SCREEN
:
1178 return "record tv screen";
1179 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1180 return "clear analogue timer";
1181 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1182 return "clear digital timer";
1183 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1184 return "clear external timer";
1185 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1186 return "set analogue timer";
1187 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1188 return "set digital timer";
1189 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1190 return "set external timer";
1191 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1192 return "set timer program title";
1193 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1194 return "timer cleared status";
1195 case CEC_OPCODE_TIMER_STATUS
:
1196 return "timer status";
1197 case CEC_OPCODE_CEC_VERSION
:
1198 return "cec version";
1199 case CEC_OPCODE_GET_CEC_VERSION
:
1200 return "get cec version";
1201 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1202 return "give physical address";
1203 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1204 return "get menu language";
1205 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1206 return "report physical address";
1207 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1208 return "set menu language";
1209 case CEC_OPCODE_DECK_CONTROL
:
1210 return "deck control";
1211 case CEC_OPCODE_DECK_STATUS
:
1212 return "deck status";
1213 case CEC_OPCODE_GIVE_DECK_STATUS
:
1214 return "give deck status";
1215 case CEC_OPCODE_PLAY
:
1217 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1218 return "give tuner status";
1219 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1220 return "select analogue service";
1221 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1222 return "set digital service";
1223 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1224 return "tuner device status";
1225 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1226 return "tuner step decrement";
1227 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1228 return "tuner step increment";
1229 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1230 return "device vendor id";
1231 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1232 return "give device vendor id";
1233 case CEC_OPCODE_VENDOR_COMMAND
:
1234 return "vendor command";
1235 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1236 return "vendor command with id";
1237 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1238 return "vendor remote button down";
1239 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1240 return "vendor remote button up";
1241 case CEC_OPCODE_SET_OSD_STRING
:
1242 return "set osd string";
1243 case CEC_OPCODE_GIVE_OSD_NAME
:
1244 return "give osd name";
1245 case CEC_OPCODE_SET_OSD_NAME
:
1246 return "set osd name";
1247 case CEC_OPCODE_MENU_REQUEST
:
1248 return "menu request";
1249 case CEC_OPCODE_MENU_STATUS
:
1250 return "menu status";
1251 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1252 return "user control pressed";
1253 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1254 return "user control release";
1255 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1256 return "give device power status";
1257 case CEC_OPCODE_REPORT_POWER_STATUS
:
1258 return "report power status";
1259 case CEC_OPCODE_FEATURE_ABORT
:
1260 return "feature abort";
1261 case CEC_OPCODE_ABORT
:
1263 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1264 return "give audio status";
1265 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1266 return "give audio mode status";
1267 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1268 return "report audio status";
1269 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1270 return "set system audio mode";
1271 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1272 return "system audio mode request";
1273 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1274 return "system audio mode status";
1275 case CEC_OPCODE_SET_AUDIO_RATE
:
1276 return "set audio rate";
1282 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1286 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1288 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1295 const char *CCECProcessor::ToString(const cec_audio_status status
)
1297 // TODO this is a mask
1301 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1305 case CEC_VENDOR_SAMSUNG
:
1309 case CEC_VENDOR_PANASONIC
:
1311 case CEC_VENDOR_PIONEER
:
1313 case CEC_VENDOR_ONKYO
:
1315 case CEC_VENDOR_YAMAHA
:
1317 case CEC_VENDOR_PHILIPS
:
1324 void *CCECBusScan::Process(void)
1326 CCECBusDevice
*device(NULL
);
1327 uint8_t iCounter(0);
1329 while (!IsStopped())
1331 if (++iCounter
< 10)
1336 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1338 device
= m_processor
->m_busDevices
[iPtr
];
1340 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1344 device
->GetVendorId();
1348 device
->GetPowerStatus(true);
1356 void CCECBusScan::WaitUntilIdle(void)
1361 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1362 while (iWaitTime
> 0)
1365 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1369 bool CCECProcessor::StartBootloader(void)
1371 return m_communication
->StartBootloader();
1374 bool CCECProcessor::PingAdapter(void)
1376 return m_communication
->PingAdapter();