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
),
60 m_communication
= new CAdapterCommunication(this);
61 m_logicalAddresses
.Clear();
62 m_logicalAddresses
.Set(iLogicalAddress
);
64 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
65 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
68 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
) :
70 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
71 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
72 m_strDeviceName(strDeviceName
),
74 m_controller(controller
),
77 m_communication
= new CAdapterCommunication(this);
78 m_logicalAddresses
.Clear();
79 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
83 case CECDEVICE_AUDIOSYSTEM
:
84 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
86 case CECDEVICE_PLAYBACKDEVICE1
:
87 case CECDEVICE_PLAYBACKDEVICE2
:
88 case CECDEVICE_PLAYBACKDEVICE3
:
89 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
91 case CECDEVICE_RECORDINGDEVICE1
:
92 case CECDEVICE_RECORDINGDEVICE2
:
93 case CECDEVICE_RECORDINGDEVICE3
:
94 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
96 case CECDEVICE_TUNER1
:
97 case CECDEVICE_TUNER2
:
98 case CECDEVICE_TUNER3
:
99 case CECDEVICE_TUNER4
:
100 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
103 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
106 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
112 CCECProcessor::~CCECProcessor(void)
116 m_busScan
->StopThread();
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 (SetAckMask(m_logicalAddresses
.AckMask()) &&
155 SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true))
157 m_controller
->AddLog(CEC_LOG_ERROR
, "processor thread started");
158 m_busScan
= new CCECBusScan(this);
159 m_busScan
->CreateThread(true);
164 m_controller
->AddLog(CEC_LOG_ERROR
, "failed to initialise the processor");
168 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
172 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
174 if (m_busDevices
[address
]->TryLogicalAddress())
176 /* only set our OSD name and active source for the primary device */
177 if (m_logicalAddresses
.IsEmpty())
179 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
180 m_busDevices
[address
]->m_bActiveSource
= true;
182 m_logicalAddresses
.Set(address
);
189 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
191 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
192 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
193 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
194 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
197 bool CCECProcessor::FindLogicalAddressTuner(void)
199 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
200 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
201 TryLogicalAddress(CECDEVICE_TUNER2
) ||
202 TryLogicalAddress(CECDEVICE_TUNER3
) ||
203 TryLogicalAddress(CECDEVICE_TUNER4
);
206 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
208 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
209 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
210 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
211 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
214 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
216 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
217 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
220 bool CCECProcessor::FindLogicalAddresses(void)
223 m_logicalAddresses
.Clear();
226 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
228 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
231 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
232 AddLog(CEC_LOG_DEBUG
, strLog
);
234 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
235 bReturn
&= FindLogicalAddressRecordingDevice();
236 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
237 bReturn
&= FindLogicalAddressTuner();
238 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
239 bReturn
&= FindLogicalAddressPlaybackDevice();
240 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
241 bReturn
&= FindLogicalAddressAudioSystem();
247 void *CCECProcessor::Process(void)
249 bool bParseFrame(false);
251 CCECAdapterMessage msg
;
253 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
255 CLockObject
lock(&m_mutex
);
256 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
257 m_startCondition
.Signal();
262 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPhysicalAddress();
263 CLockObject
lock(&m_mutex
);
265 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
266 m_startCondition
.Signal();
275 CLockObject
lock(&m_mutex
);
276 if (m_commandBuffer
.Pop(command
))
280 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
282 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
283 command
= m_currentframe
;
288 ParseCommand(command
);
293 m_controller
->CheckKeypressTimeout();
297 m_communication
->Close();
302 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
309 cec_logical_address addr
= m_logicalAddresses
.primary
;
311 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
313 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
315 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
317 addr
= (cec_logical_address
) iPtr
;
323 bReturn
= m_busDevices
[CECDEVICE_TV
]->PowerOn() &&
324 m_busDevices
[addr
]->TransmitActiveSource() &&
325 SetStreamPath(m_busDevices
[addr
]->GetPhysicalAddress(false));
327 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
328 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
330 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
336 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress
)
338 return SetStreamPath(m_busDevices
[iAddress
]->GetPhysicalAddress(false));
341 bool CCECProcessor::SetActiveView(void)
343 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
346 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
350 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
353 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
355 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
362 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
366 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
369 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
371 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
378 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
383 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
384 AddLog(CEC_LOG_DEBUG
, strLog
);
386 m_iBaseDevice
= iBaseDevice
;
388 if (!m_bStarted
&& !bForce
)
391 uint16_t iPhysicalAddress(0);
392 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
394 if (iPhysicalAddress
== 0)
396 else if (iPhysicalAddress
% 0x1000 == 0)
398 else if (iPhysicalAddress
% 0x100 == 0)
400 else if (iPhysicalAddress
% 0x10 == 0)
403 while(!bReturn
&& iPos
> 0)
405 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
406 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
407 AddLog(CEC_LOG_DEBUG
, strLog
);
408 if (PhysicalAddressInUse(iPhysicalAddress
))
410 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
411 AddLog(CEC_LOG_DEBUG
, strLog
);
412 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
416 strLog
.Format("physical address %4x is free", iPhysicalAddress
);
417 AddLog(CEC_LOG_DEBUG
, strLog
);
418 SetPhysicalAddress(iPhysicalAddress
);
426 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
428 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
430 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
436 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath
)
440 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
443 device
->SetActiveDevice();
450 bool CCECProcessor::TransmitInactiveSource(void)
455 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
456 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
460 void CCECProcessor::LogOutput(const cec_command
&data
)
463 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
465 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
467 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
468 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
469 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
472 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
474 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
477 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
478 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
479 m_logicalAddresses
.primary
= iLogicalAddress
;
480 m_logicalAddresses
.Set(iLogicalAddress
);
481 return SetAckMask(m_logicalAddresses
.AckMask());
487 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
489 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
491 if (m_logicalAddresses
[iPtr
])
492 m_busDevices
[iPtr
]->SetMenuState(state
);
496 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
501 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
503 if (!m_logicalAddresses
.IsEmpty())
505 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
506 if (m_logicalAddresses
[iPtr
])
507 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
508 return SetActiveView();
513 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
516 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
517 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
520 CLockObject
lock(&m_mutex
);
521 m_bMonitor
= bEnable
;
527 m_busScan
= new CCECBusScan(this);
528 m_busScan
->CreateThread(true);
535 m_busScan
->StopThread();
543 return SetAckMask(0);
545 return SetAckMask(m_logicalAddresses
.AckMask());
548 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
550 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
552 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
553 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
554 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
559 uint8_t CCECProcessor::VolumeUp(bool bWait
/* = true */)
562 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
563 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bWait
);
568 uint8_t CCECProcessor::VolumeDown(bool bWait
/* = true */)
571 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
572 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bWait
);
577 uint8_t CCECProcessor::MuteAudio(bool bWait
/* = true */)
580 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
581 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bWait
);
586 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
588 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
589 return m_busDevices
[m_logicalAddresses
.primary
];
591 CCECBusDevice
*device
= NULL
;
592 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
594 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
596 device
= m_busDevices
[iPtr
];
604 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
606 CCECBusDevice
*device
= NULL
;
608 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
610 if (m_busDevices
[iPtr
]->m_type
== type
)
612 device
= m_busDevices
[iPtr
];
620 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
622 return m_busDevices
[iAddress
]->GetCecVersion();
625 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
627 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
630 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
631 retVal
.device
= iAddress
;
636 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
638 if (m_busDevices
[iAddress
])
640 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
641 return (strcmp(language
->language
, "???") != 0);
646 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
648 if (m_busDevices
[iAddress
])
649 return m_busDevices
[iAddress
]->GetVendorId();
653 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
655 if (m_busDevices
[iAddress
])
656 return m_busDevices
[iAddress
]->GetPowerStatus();
657 return CEC_POWER_STATUS_UNKNOWN
;
660 bool CCECProcessor::Transmit(const cec_command
&data
)
665 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
666 bReturn
= Transmit(output
);
672 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
675 CLockObject
lock(&m_mutex
);
677 m_communication
->SetLineTimeout(3);
681 if (output
->tries
> 0)
682 m_communication
->SetLineTimeout(5);
684 CLockObject
msgLock(&output
->mutex
);
685 if (!m_communication
|| !m_communication
->Write(output
))
689 output
->condition
.Wait(&output
->mutex
);
690 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
692 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
697 if (output
->transmit_timeout
> 0)
699 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
700 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
704 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
<= output
->maxTries
);
707 m_communication
->SetLineTimeout(3);
712 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
714 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
718 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
719 command
.parameters
.PushBack((uint8_t)opcode
);
720 command
.parameters
.PushBack((uint8_t)reason
);
725 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
728 bool bTransmitSucceeded(false);
729 uint8_t iPacketsLeft(message
->size() / 4);
731 int64_t iNow
= GetTimeMs();
732 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
734 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
736 CCECAdapterMessage msg
;
738 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
744 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
746 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
747 m_lastInitiator
= msg
.initiator();
752 bError
= msg
.is_error();
753 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
754 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
755 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
763 message
->reply
= msg
.message();
764 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
768 switch(msg
.message())
770 case MSGCODE_COMMAND_ACCEPTED
:
771 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
772 if (iPacketsLeft
> 0)
775 case MSGCODE_TRANSMIT_SUCCEEDED
:
776 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
777 bTransmitSucceeded
= (iPacketsLeft
== 0);
778 bError
= !bTransmitSucceeded
;
779 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
782 // ignore other data while waiting
790 return bTransmitSucceeded
&& !bError
;
793 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
796 bool bIsError(msg
.is_error());
801 switch(msg
.message())
803 case MSGCODE_FRAME_START
:
805 m_currentframe
.Clear();
808 m_currentframe
.initiator
= msg
.initiator();
809 m_currentframe
.destination
= msg
.destination();
810 m_currentframe
.ack
= msg
.ack();
811 m_currentframe
.eom
= msg
.eom();
813 if (m_currentframe
.ack
== true)
815 m_lastInitiator
= m_currentframe
.initiator
;
816 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
820 case MSGCODE_RECEIVE_FAILED
:
822 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
823 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
826 case MSGCODE_FRAME_DATA
:
830 m_currentframe
.PushBack(msg
[1]);
831 m_currentframe
.eom
= msg
.eom();
840 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
844 void CCECProcessor::ParseCommand(cec_command
&command
)
847 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
848 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
849 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
850 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
852 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
853 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
856 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
858 cec_logical_addresses addresses
;
860 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
862 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
863 addresses
.Set((cec_logical_address
) iPtr
);
868 bool CCECProcessor::IsActiveDevice(cec_logical_address address
)
870 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
873 bool CCECProcessor::IsActiveDeviceType(cec_device_type type
)
875 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
877 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
884 uint16_t CCECProcessor::GetPhysicalAddress(void) const
886 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
887 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
891 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
893 m_controller
->SetCurrentButton(iButtonCode
);
896 void CCECProcessor::AddCommand(const cec_command
&command
)
898 m_controller
->AddCommand(command
);
901 void CCECProcessor::AddKey(cec_keypress
&key
)
903 m_controller
->AddKey(key
);
906 void CCECProcessor::AddKey(void)
908 m_controller
->AddKey();
911 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
913 m_controller
->AddLog(level
, strMessage
);
916 bool CCECProcessor::SetAckMask(uint16_t iMask
)
920 strLog
.Format("setting ackmask to %2x", iMask
);
921 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
923 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
925 output
->push_back(MSGSTART
);
926 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
927 output
->push_escaped(iMask
>> 8);
928 output
->push_escaped((uint8_t)iMask
);
929 output
->push_back(MSGEND
);
931 if ((bReturn
= Transmit(output
)) == false)
932 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
939 bool CCECProcessor::SendKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = false */)
941 return m_busDevices
[iDestination
]->SendKeypress(key
, bWait
);
944 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination
, bool bWait
/* = false */)
946 return m_busDevices
[iDestination
]->SendKeyRelease(bWait
);
949 const char *CCECProcessor::ToString(const cec_menu_state state
)
953 case CEC_MENU_STATE_ACTIVATED
:
955 case CEC_MENU_STATE_DEACTIVATED
:
956 return "deactivated";
962 const char *CCECProcessor::ToString(const cec_version version
)
966 case CEC_VERSION_1_2
:
968 case CEC_VERSION_1_2A
:
970 case CEC_VERSION_1_3
:
972 case CEC_VERSION_1_3A
:
974 case CEC_VERSION_1_4
:
981 const char *CCECProcessor::ToString(const cec_power_status status
)
985 case CEC_POWER_STATUS_ON
:
987 case CEC_POWER_STATUS_STANDBY
:
989 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
990 return "in transition from on to standby";
991 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
992 return "in transition from standby to on";
998 const char *CCECProcessor::ToString(const cec_logical_address address
)
1002 case CECDEVICE_AUDIOSYSTEM
:
1004 case CECDEVICE_BROADCAST
:
1006 case CECDEVICE_FREEUSE
:
1008 case CECDEVICE_PLAYBACKDEVICE1
:
1009 return "Playback 1";
1010 case CECDEVICE_PLAYBACKDEVICE2
:
1011 return "Playback 2";
1012 case CECDEVICE_PLAYBACKDEVICE3
:
1013 return "Playback 3";
1014 case CECDEVICE_RECORDINGDEVICE1
:
1015 return "Recorder 1";
1016 case CECDEVICE_RECORDINGDEVICE2
:
1017 return "Recorder 2";
1018 case CECDEVICE_RECORDINGDEVICE3
:
1019 return "Recorder 3";
1020 case CECDEVICE_RESERVED1
:
1021 return "Reserved 1";
1022 case CECDEVICE_RESERVED2
:
1023 return "Reserved 2";
1024 case CECDEVICE_TUNER1
:
1026 case CECDEVICE_TUNER2
:
1028 case CECDEVICE_TUNER3
:
1030 case CECDEVICE_TUNER4
:
1039 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1043 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1044 return "skip forward wind";
1045 case CEC_DECK_CONTROL_MODE_EJECT
:
1047 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1048 return "reverse rewind";
1049 case CEC_DECK_CONTROL_MODE_STOP
:
1056 const char *CCECProcessor::ToString(const cec_deck_info status
)
1060 case CEC_DECK_INFO_PLAY
:
1062 case CEC_DECK_INFO_RECORD
:
1064 case CEC_DECK_INFO_PLAY_REVERSE
:
1065 return "play reverse";
1066 case CEC_DECK_INFO_STILL
:
1068 case CEC_DECK_INFO_SLOW
:
1070 case CEC_DECK_INFO_SLOW_REVERSE
:
1071 return "slow reverse";
1072 case CEC_DECK_INFO_FAST_FORWARD
:
1073 return "fast forward";
1074 case CEC_DECK_INFO_FAST_REVERSE
:
1075 return "fast reverse";
1076 case CEC_DECK_INFO_NO_MEDIA
:
1078 case CEC_DECK_INFO_STOP
:
1080 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1081 return "info skip forward wind";
1082 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1083 return "info skip reverse rewind";
1084 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1085 return "info index search forward";
1086 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1087 return "info index search reverse";
1088 case CEC_DECK_INFO_OTHER_STATUS
:
1095 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1099 case CEC_OPCODE_ACTIVE_SOURCE
:
1100 return "active source";
1101 case CEC_OPCODE_IMAGE_VIEW_ON
:
1102 return "image view on";
1103 case CEC_OPCODE_TEXT_VIEW_ON
:
1104 return "text view on";
1105 case CEC_OPCODE_INACTIVE_SOURCE
:
1106 return "inactive source";
1107 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1108 return "request active source";
1109 case CEC_OPCODE_ROUTING_CHANGE
:
1110 return "routing change";
1111 case CEC_OPCODE_ROUTING_INFORMATION
:
1112 return "routing information";
1113 case CEC_OPCODE_SET_STREAM_PATH
:
1114 return "set stream path";
1115 case CEC_OPCODE_STANDBY
:
1117 case CEC_OPCODE_RECORD_OFF
:
1118 return "record off";
1119 case CEC_OPCODE_RECORD_ON
:
1121 case CEC_OPCODE_RECORD_STATUS
:
1122 return "record status";
1123 case CEC_OPCODE_RECORD_TV_SCREEN
:
1124 return "record tv screen";
1125 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1126 return "clear analogue timer";
1127 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1128 return "clear digital timer";
1129 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1130 return "clear external timer";
1131 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1132 return "set analogue timer";
1133 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1134 return "set digital timer";
1135 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1136 return "set external timer";
1137 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1138 return "set timer program title";
1139 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1140 return "timer cleared status";
1141 case CEC_OPCODE_TIMER_STATUS
:
1142 return "timer status";
1143 case CEC_OPCODE_CEC_VERSION
:
1144 return "cec version";
1145 case CEC_OPCODE_GET_CEC_VERSION
:
1146 return "get cec version";
1147 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1148 return "give physical address";
1149 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1150 return "get menu language";
1151 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1152 return "report physical address";
1153 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1154 return "set menu language";
1155 case CEC_OPCODE_DECK_CONTROL
:
1156 return "deck control";
1157 case CEC_OPCODE_DECK_STATUS
:
1158 return "deck status";
1159 case CEC_OPCODE_GIVE_DECK_STATUS
:
1160 return "give deck status";
1161 case CEC_OPCODE_PLAY
:
1163 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1164 return "give tuner status";
1165 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1166 return "select analogue service";
1167 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1168 return "set digital service";
1169 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1170 return "tuner device status";
1171 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1172 return "tuner step decrement";
1173 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1174 return "tuner step increment";
1175 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1176 return "device vendor id";
1177 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1178 return "give device vendor id";
1179 case CEC_OPCODE_VENDOR_COMMAND
:
1180 return "vendor command";
1181 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1182 return "vendor command with id";
1183 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1184 return "vendor remote button down";
1185 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1186 return "vendor remote button up";
1187 case CEC_OPCODE_SET_OSD_STRING
:
1188 return "set osd string";
1189 case CEC_OPCODE_GIVE_OSD_NAME
:
1190 return "give osd name";
1191 case CEC_OPCODE_SET_OSD_NAME
:
1192 return "set osd name";
1193 case CEC_OPCODE_MENU_REQUEST
:
1194 return "menu request";
1195 case CEC_OPCODE_MENU_STATUS
:
1196 return "menu status";
1197 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1198 return "user control pressed";
1199 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1200 return "user control release";
1201 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1202 return "give device power status";
1203 case CEC_OPCODE_REPORT_POWER_STATUS
:
1204 return "report power status";
1205 case CEC_OPCODE_FEATURE_ABORT
:
1206 return "feature abort";
1207 case CEC_OPCODE_ABORT
:
1209 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1210 return "give audio status";
1211 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1212 return "give audio mode status";
1213 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1214 return "report audio status";
1215 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1216 return "set system audio mode";
1217 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1218 return "system audio mode request";
1219 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1220 return "system audio mode status";
1221 case CEC_OPCODE_SET_AUDIO_RATE
:
1222 return "set audio rate";
1228 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1232 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1234 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1241 const char *CCECProcessor::ToString(const cec_audio_status status
)
1243 // TODO this is a mask
1247 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1251 case CEC_VENDOR_SAMSUNG
:
1255 case CEC_VENDOR_PANASONIC
:
1257 case CEC_VENDOR_PIONEER
:
1259 case CEC_VENDOR_ONKYO
:
1261 case CEC_VENDOR_YAMAHA
:
1263 case CEC_VENDOR_PHILIPS
:
1270 void *CCECBusScan::Process(void)
1272 CCECBusDevice
*device(NULL
);
1273 while (!IsStopped())
1275 for (unsigned int iPtr
= 0; iPtr
< 15 && !IsStopped(); iPtr
++)
1277 device
= m_processor
->m_busDevices
[iPtr
];
1278 if (device
&& device
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1281 device
->GetVendorId();
1290 bool CCECProcessor::StartBootloader(void)
1292 return m_communication
->StartBootloader();
1295 bool CCECProcessor::PingAdapter(void)
1297 return m_communication
->PingAdapter();