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
),
61 m_communication
= new CAdapterCommunication(this);
62 m_logicalAddresses
.Clear();
63 m_logicalAddresses
.Set(iLogicalAddress
);
65 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
66 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
69 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
) :
71 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
72 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
73 m_strDeviceName(strDeviceName
),
75 m_controller(controller
),
79 m_communication
= new CAdapterCommunication(this);
80 m_logicalAddresses
.Clear();
81 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
85 case CECDEVICE_AUDIOSYSTEM
:
86 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
88 case CECDEVICE_PLAYBACKDEVICE1
:
89 case CECDEVICE_PLAYBACKDEVICE2
:
90 case CECDEVICE_PLAYBACKDEVICE3
:
91 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
93 case CECDEVICE_RECORDINGDEVICE1
:
94 case CECDEVICE_RECORDINGDEVICE2
:
95 case CECDEVICE_RECORDINGDEVICE3
:
96 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
98 case CECDEVICE_TUNER1
:
99 case CECDEVICE_TUNER2
:
100 case CECDEVICE_TUNER3
:
101 case CECDEVICE_TUNER4
:
102 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
105 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
108 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
114 CCECProcessor::~CCECProcessor(void)
118 m_busScan
->StopThread();
122 m_startCondition
.Broadcast();
125 delete m_communication
;
126 m_communication
= NULL
;
128 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
129 delete m_busDevices
[iPtr
];
132 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
134 CLockObject
lock(&m_mutex
);
135 if (!m_communication
|| m_communication
->IsOpen())
137 m_controller
->AddLog(CEC_LOG_ERROR
, "connection already opened");
141 if (!m_communication
->Open(strPort
, iBaudRate
, iTimeoutMs
))
143 m_controller
->AddLog(CEC_LOG_ERROR
, "could not open a connection");
149 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
151 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
156 if (SetAckMask(m_logicalAddresses
.AckMask()) &&
157 SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true))
159 m_busScan
= new CCECBusScan(this);
160 m_busScan
->CreateThread(true);
165 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
170 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
172 if (m_busDevices
[address
]->TryLogicalAddress())
174 /* only set our OSD name and active source for the primary device */
175 if (m_logicalAddresses
.IsEmpty())
177 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
178 m_busDevices
[address
]->m_bActiveSource
= true;
180 m_logicalAddresses
.Set(address
);
187 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
189 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
190 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
191 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
192 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
195 bool CCECProcessor::FindLogicalAddressTuner(void)
197 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
198 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
199 TryLogicalAddress(CECDEVICE_TUNER2
) ||
200 TryLogicalAddress(CECDEVICE_TUNER3
) ||
201 TryLogicalAddress(CECDEVICE_TUNER4
);
204 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
206 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
207 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
208 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
209 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
212 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
214 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
215 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
218 bool CCECProcessor::FindLogicalAddresses(void)
221 m_logicalAddresses
.Clear();
224 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
226 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
229 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
230 AddLog(CEC_LOG_DEBUG
, strLog
);
232 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
233 bReturn
&= FindLogicalAddressRecordingDevice();
234 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
235 bReturn
&= FindLogicalAddressTuner();
236 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
237 bReturn
&= FindLogicalAddressPlaybackDevice();
238 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
239 bReturn
&= FindLogicalAddressAudioSystem();
245 bool CCECProcessor::SetLineTimeout(uint8_t iTimeout
)
247 bool bReturn(m_iLineTimeout
!= iTimeout
);
251 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
253 output
->push_back(MSGSTART
);
254 output
->push_escaped(MSGCODE_TRANSMIT_IDLETIME
);
255 output
->push_escaped(iTimeout
);
256 output
->push_back(MSGEND
);
258 if ((bReturn
= Transmit(output
)) == false)
259 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the idletime");
266 void *CCECProcessor::Process(void)
268 bool bParseFrame(false);
270 CCECAdapterMessage msg
;
272 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
274 CLockObject
lock(&m_mutex
);
275 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
276 m_startCondition
.Signal();
281 CLockObject
lock(&m_mutex
);
283 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
284 m_startCondition
.Signal();
293 CLockObject
lock(&m_mutex
);
294 if (m_commandBuffer
.Pop(command
))
298 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
300 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
301 command
= m_currentframe
;
306 ParseCommand(command
);
311 m_controller
->CheckKeypressTimeout();
315 m_communication
->Close();
320 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
327 cec_logical_address addr
= m_logicalAddresses
.primary
;
329 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
331 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
333 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
335 addr
= (cec_logical_address
) iPtr
;
341 return SetStreamPath(m_busDevices
[addr
]->GetPhysicalAddress(false)) &&
342 m_busDevices
[addr
]->TransmitActiveSource();
345 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress
)
347 return SetStreamPath(m_busDevices
[iAddress
]->GetPhysicalAddress(false));
350 bool CCECProcessor::SetActiveView(void)
352 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
355 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
359 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
362 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
364 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
371 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
375 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
378 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
380 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
387 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
392 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
393 AddLog(CEC_LOG_DEBUG
, strLog
);
395 m_iBaseDevice
= iBaseDevice
;
397 if (!m_bStarted
&& !bForce
)
400 uint16_t iPhysicalAddress(0);
401 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
403 if (iPhysicalAddress
== 0)
405 else if (iPhysicalAddress
% 0x1000 == 0)
407 else if (iPhysicalAddress
% 0x100 == 0)
409 else if (iPhysicalAddress
% 0x10 == 0)
412 while(!bReturn
&& iPos
> 0)
414 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
415 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
416 AddLog(CEC_LOG_DEBUG
, strLog
);
417 if (CheckPhysicalAddress(iPhysicalAddress
))
419 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
420 AddLog(CEC_LOG_DEBUG
, strLog
);
421 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
425 SetPhysicalAddress(iPhysicalAddress
);
433 bool CCECProcessor::CheckPhysicalAddress(uint16_t iPhysicalAddress
)
435 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
437 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
443 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath
)
447 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
450 device
->SetActiveDevice();
457 bool CCECProcessor::TransmitInactiveSource(void)
462 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
463 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
467 void CCECProcessor::LogOutput(const cec_command
&data
)
470 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
472 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
474 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
475 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
476 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
479 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
481 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
484 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
485 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
486 m_logicalAddresses
.primary
= iLogicalAddress
;
487 m_logicalAddresses
.Set(iLogicalAddress
);
488 return SetAckMask(m_logicalAddresses
.AckMask());
494 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
496 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
498 if (m_logicalAddresses
[iPtr
])
499 m_busDevices
[iPtr
]->SetMenuState(state
);
503 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
508 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
510 if (!m_logicalAddresses
.IsEmpty())
512 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
513 if (m_logicalAddresses
[iPtr
])
514 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
515 return SetActiveView();
520 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
523 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
524 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
527 CLockObject
lock(&m_mutex
);
528 m_bMonitor
= bEnable
;
534 m_busScan
= new CCECBusScan(this);
535 m_busScan
->CreateThread(true);
542 m_busScan
->StopThread();
550 return SetAckMask(0);
552 return SetAckMask(m_logicalAddresses
.AckMask());
555 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
557 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
559 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
560 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
561 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
566 uint8_t CCECProcessor::VolumeUp(bool bWait
/* = true */)
569 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
570 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bWait
);
575 uint8_t CCECProcessor::VolumeDown(bool bWait
/* = true */)
578 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
579 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bWait
);
584 uint8_t CCECProcessor::MuteAudio(bool bWait
/* = true */)
587 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
588 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bWait
);
593 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
595 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
596 return m_busDevices
[m_logicalAddresses
.primary
];
598 CCECBusDevice
*device
= NULL
;
599 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
601 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
603 device
= m_busDevices
[iPtr
];
611 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
613 CCECBusDevice
*device
= NULL
;
615 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
617 if (m_busDevices
[iPtr
]->m_type
== type
)
619 device
= m_busDevices
[iPtr
];
627 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
629 return m_busDevices
[iAddress
]->GetCecVersion();
632 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
634 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
637 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
638 retVal
.device
= iAddress
;
643 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
645 if (m_busDevices
[iAddress
])
647 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
648 return (strcmp(language
->language
, "???") != 0);
653 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
655 if (m_busDevices
[iAddress
])
656 return m_busDevices
[iAddress
]->GetVendorId();
660 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
662 if (m_busDevices
[iAddress
])
663 return m_busDevices
[iAddress
]->GetPowerStatus();
664 return CEC_POWER_STATUS_UNKNOWN
;
667 bool CCECProcessor::Transmit(const cec_command
&data
)
672 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
673 bReturn
= Transmit(output
);
679 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
682 CLockObject
lock(&m_mutex
);
688 if (output
->tries
> 0)
691 CLockObject
msgLock(&output
->mutex
);
692 if (!m_communication
|| !m_communication
->Write(output
))
696 output
->condition
.Wait(&output
->mutex
);
697 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
699 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
704 if (output
->transmit_timeout
> 0)
706 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
707 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
711 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
<= output
->maxTries
);
719 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
721 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
725 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
726 command
.parameters
.PushBack((uint8_t)opcode
);
727 command
.parameters
.PushBack((uint8_t)reason
);
732 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
735 bool bTransmitSucceeded(false);
736 uint8_t iPacketsLeft(message
->size() / 4);
738 int64_t iNow
= GetTimeMs();
739 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
741 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
743 CCECAdapterMessage msg
;
745 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
751 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
753 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
754 m_lastInitiator
= msg
.initiator();
759 bError
= msg
.is_error();
760 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
761 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
762 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
770 message
->reply
= msg
.message();
771 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
775 switch(msg
.message())
777 case MSGCODE_COMMAND_ACCEPTED
:
778 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
779 if (iPacketsLeft
> 0)
782 case MSGCODE_TRANSMIT_SUCCEEDED
:
783 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
784 bTransmitSucceeded
= (iPacketsLeft
== 0);
785 bError
= !bTransmitSucceeded
;
786 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
789 // ignore other data while waiting
797 return bTransmitSucceeded
&& !bError
;
800 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
803 bool bIsError(msg
.is_error());
808 switch(msg
.message())
810 case MSGCODE_FRAME_START
:
812 m_currentframe
.Clear();
815 m_currentframe
.initiator
= msg
.initiator();
816 m_currentframe
.destination
= msg
.destination();
817 m_currentframe
.ack
= msg
.ack();
818 m_currentframe
.eom
= msg
.eom();
820 if (m_currentframe
.ack
== true)
822 m_lastInitiator
= m_currentframe
.initiator
;
823 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
827 case MSGCODE_RECEIVE_FAILED
:
829 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
830 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
833 case MSGCODE_FRAME_DATA
:
837 m_currentframe
.PushBack(msg
[1]);
838 m_currentframe
.eom
= msg
.eom();
847 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
851 void CCECProcessor::ParseCommand(cec_command
&command
)
854 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
855 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
856 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
857 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
859 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
860 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
863 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
865 cec_logical_addresses addresses
;
867 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
869 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
870 addresses
.Set((cec_logical_address
) iPtr
);
875 bool CCECProcessor::IsActiveDevice(cec_logical_address address
)
877 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
880 bool CCECProcessor::IsActiveDeviceType(cec_device_type type
)
882 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
884 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
891 uint16_t CCECProcessor::GetPhysicalAddress(void) const
893 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
894 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
898 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
900 m_controller
->SetCurrentButton(iButtonCode
);
903 void CCECProcessor::AddCommand(const cec_command
&command
)
905 m_controller
->AddCommand(command
);
908 void CCECProcessor::AddKey(cec_keypress
&key
)
910 m_controller
->AddKey(key
);
913 void CCECProcessor::AddKey(void)
915 m_controller
->AddKey();
918 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
920 m_controller
->AddLog(level
, strMessage
);
923 bool CCECProcessor::SetAckMask(uint16_t iMask
)
927 strLog
.Format("setting ackmask to %2x", iMask
);
928 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
930 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
932 output
->push_back(MSGSTART
);
933 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
934 output
->push_escaped(iMask
>> 8);
935 output
->push_escaped((uint8_t)iMask
);
936 output
->push_back(MSGEND
);
938 if ((bReturn
= Transmit(output
)) == false)
939 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
946 bool CCECProcessor::SendKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = false */)
948 return m_busDevices
[iDestination
]->SendKeypress(key
, bWait
);
951 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination
, bool bWait
/* = false */)
953 return m_busDevices
[iDestination
]->SendKeyRelease(bWait
);
956 const char *CCECProcessor::ToString(const cec_menu_state state
)
960 case CEC_MENU_STATE_ACTIVATED
:
962 case CEC_MENU_STATE_DEACTIVATED
:
963 return "deactivated";
969 const char *CCECProcessor::ToString(const cec_version version
)
973 case CEC_VERSION_1_2
:
975 case CEC_VERSION_1_2A
:
977 case CEC_VERSION_1_3
:
979 case CEC_VERSION_1_3A
:
981 case CEC_VERSION_1_4
:
988 const char *CCECProcessor::ToString(const cec_power_status status
)
992 case CEC_POWER_STATUS_ON
:
994 case CEC_POWER_STATUS_STANDBY
:
996 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
997 return "in transition from on to standby";
998 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
999 return "in transition from standby to on";
1005 const char *CCECProcessor::ToString(const cec_logical_address address
)
1009 case CECDEVICE_AUDIOSYSTEM
:
1011 case CECDEVICE_BROADCAST
:
1013 case CECDEVICE_FREEUSE
:
1015 case CECDEVICE_PLAYBACKDEVICE1
:
1016 return "Playback 1";
1017 case CECDEVICE_PLAYBACKDEVICE2
:
1018 return "Playback 2";
1019 case CECDEVICE_PLAYBACKDEVICE3
:
1020 return "Playback 3";
1021 case CECDEVICE_RECORDINGDEVICE1
:
1022 return "Recorder 1";
1023 case CECDEVICE_RECORDINGDEVICE2
:
1024 return "Recorder 2";
1025 case CECDEVICE_RECORDINGDEVICE3
:
1026 return "Recorder 3";
1027 case CECDEVICE_RESERVED1
:
1028 return "Reserved 1";
1029 case CECDEVICE_RESERVED2
:
1030 return "Reserved 2";
1031 case CECDEVICE_TUNER1
:
1033 case CECDEVICE_TUNER2
:
1035 case CECDEVICE_TUNER3
:
1037 case CECDEVICE_TUNER4
:
1046 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1050 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1051 return "skip forward wind";
1052 case CEC_DECK_CONTROL_MODE_EJECT
:
1054 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1055 return "reverse rewind";
1056 case CEC_DECK_CONTROL_MODE_STOP
:
1063 const char *CCECProcessor::ToString(const cec_deck_info status
)
1067 case CEC_DECK_INFO_PLAY
:
1069 case CEC_DECK_INFO_RECORD
:
1071 case CEC_DECK_INFO_PLAY_REVERSE
:
1072 return "play reverse";
1073 case CEC_DECK_INFO_STILL
:
1075 case CEC_DECK_INFO_SLOW
:
1077 case CEC_DECK_INFO_SLOW_REVERSE
:
1078 return "slow reverse";
1079 case CEC_DECK_INFO_FAST_FORWARD
:
1080 return "fast forward";
1081 case CEC_DECK_INFO_FAST_REVERSE
:
1082 return "fast reverse";
1083 case CEC_DECK_INFO_NO_MEDIA
:
1085 case CEC_DECK_INFO_STOP
:
1087 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1088 return "info skip forward wind";
1089 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1090 return "info skip reverse rewind";
1091 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1092 return "info index search forward";
1093 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1094 return "info index search reverse";
1095 case CEC_DECK_INFO_OTHER_STATUS
:
1102 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1106 case CEC_OPCODE_ACTIVE_SOURCE
:
1107 return "active source";
1108 case CEC_OPCODE_IMAGE_VIEW_ON
:
1109 return "image view on";
1110 case CEC_OPCODE_TEXT_VIEW_ON
:
1111 return "text view on";
1112 case CEC_OPCODE_INACTIVE_SOURCE
:
1113 return "inactive source";
1114 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1115 return "request active source";
1116 case CEC_OPCODE_ROUTING_CHANGE
:
1117 return "routing change";
1118 case CEC_OPCODE_ROUTING_INFORMATION
:
1119 return "routing information";
1120 case CEC_OPCODE_SET_STREAM_PATH
:
1121 return "set stream path";
1122 case CEC_OPCODE_STANDBY
:
1124 case CEC_OPCODE_RECORD_OFF
:
1125 return "record off";
1126 case CEC_OPCODE_RECORD_ON
:
1128 case CEC_OPCODE_RECORD_STATUS
:
1129 return "record status";
1130 case CEC_OPCODE_RECORD_TV_SCREEN
:
1131 return "record tv screen";
1132 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1133 return "clear analogue timer";
1134 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1135 return "clear digital timer";
1136 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1137 return "clear external timer";
1138 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1139 return "set analogue timer";
1140 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1141 return "set digital timer";
1142 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1143 return "set external timer";
1144 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1145 return "set timer program title";
1146 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1147 return "timer cleared status";
1148 case CEC_OPCODE_TIMER_STATUS
:
1149 return "timer status";
1150 case CEC_OPCODE_CEC_VERSION
:
1151 return "cec version";
1152 case CEC_OPCODE_GET_CEC_VERSION
:
1153 return "get cec version";
1154 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1155 return "give physical address";
1156 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1157 return "get menu language";
1158 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1159 return "report physical address";
1160 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1161 return "set menu language";
1162 case CEC_OPCODE_DECK_CONTROL
:
1163 return "deck control";
1164 case CEC_OPCODE_DECK_STATUS
:
1165 return "deck status";
1166 case CEC_OPCODE_GIVE_DECK_STATUS
:
1167 return "give deck status";
1168 case CEC_OPCODE_PLAY
:
1170 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1171 return "give tuner status";
1172 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1173 return "select analogue service";
1174 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1175 return "set digital service";
1176 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1177 return "tuner device status";
1178 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1179 return "tuner step decrement";
1180 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1181 return "tuner step increment";
1182 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1183 return "device vendor id";
1184 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1185 return "give device vendor id";
1186 case CEC_OPCODE_VENDOR_COMMAND
:
1187 return "vendor command";
1188 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1189 return "vendor command with id";
1190 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1191 return "vendor remote button down";
1192 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1193 return "vendor remote button up";
1194 case CEC_OPCODE_SET_OSD_STRING
:
1195 return "set osd string";
1196 case CEC_OPCODE_GIVE_OSD_NAME
:
1197 return "give osd name";
1198 case CEC_OPCODE_SET_OSD_NAME
:
1199 return "set osd name";
1200 case CEC_OPCODE_MENU_REQUEST
:
1201 return "menu request";
1202 case CEC_OPCODE_MENU_STATUS
:
1203 return "menu status";
1204 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1205 return "user control pressed";
1206 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1207 return "user control release";
1208 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1209 return "give device power status";
1210 case CEC_OPCODE_REPORT_POWER_STATUS
:
1211 return "report power status";
1212 case CEC_OPCODE_FEATURE_ABORT
:
1213 return "feature abort";
1214 case CEC_OPCODE_ABORT
:
1216 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1217 return "give audio status";
1218 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1219 return "give audio mode status";
1220 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1221 return "report audio status";
1222 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1223 return "set system audio mode";
1224 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1225 return "system audio mode request";
1226 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1227 return "system audio mode status";
1228 case CEC_OPCODE_SET_AUDIO_RATE
:
1229 return "set audio rate";
1235 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1239 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1241 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1248 const char *CCECProcessor::ToString(const cec_audio_status status
)
1250 // TODO this is a mask
1254 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1258 case CEC_VENDOR_SAMSUNG
:
1262 case CEC_VENDOR_PANASONIC
:
1264 case CEC_VENDOR_PIONEER
:
1266 case CEC_VENDOR_ONKYO
:
1268 case CEC_VENDOR_YAMAHA
:
1270 case CEC_VENDOR_PHILIPS
:
1277 void *CCECBusScan::Process(void)
1279 CCECBusDevice
*device(NULL
);
1280 while (!IsStopped())
1282 for (unsigned int iPtr
= 0; iPtr
< 15 && !IsStopped(); iPtr
++)
1284 device
= m_processor
->m_busDevices
[iPtr
];
1285 if (device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1288 device
->GetVendorId();
1297 bool CCECProcessor::StartBootloader(void)
1299 return m_communication
->StartBootloader();
1302 bool CCECProcessor::PingAdapter(void)
1304 return m_communication
->PingAdapter();