2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
33 #include "CECProcessor.h"
35 #include "AdapterCommunication.h"
36 #include "devices/CECBusDevice.h"
37 #include "devices/CECAudioSystem.h"
38 #include "devices/CECPlaybackDevice.h"
39 #include "devices/CECRecordingDevice.h"
40 #include "devices/CECTuner.h"
41 #include "devices/CECTV.h"
42 #include "implementations/CECCommandHandler.h"
44 #include "util/StdString.h"
45 #include "platform/timeutils.h"
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
52 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
53 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
54 m_lastInitiator(CECDEVICE_UNKNOWN
),
55 m_strDeviceName(strDeviceName
),
56 m_controller(controller
),
59 m_iStandardLineTimeout(3),
60 m_iRetryLineTimeout(3)
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)
81 m_communication
= new CAdapterCommunication(this);
82 m_logicalAddresses
.Clear();
83 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
87 case CECDEVICE_AUDIOSYSTEM
:
88 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
90 case CECDEVICE_PLAYBACKDEVICE1
:
91 case CECDEVICE_PLAYBACKDEVICE2
:
92 case CECDEVICE_PLAYBACKDEVICE3
:
93 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
95 case CECDEVICE_RECORDINGDEVICE1
:
96 case CECDEVICE_RECORDINGDEVICE2
:
97 case CECDEVICE_RECORDINGDEVICE3
:
98 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
100 case CECDEVICE_TUNER1
:
101 case CECDEVICE_TUNER2
:
102 case CECDEVICE_TUNER3
:
103 case CECDEVICE_TUNER4
:
104 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
107 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
110 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
116 CCECProcessor::~CCECProcessor(void)
120 m_busScan
->StopThread();
124 m_startCondition
.Broadcast();
127 delete m_communication
;
128 m_communication
= NULL
;
130 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
131 delete m_busDevices
[iPtr
];
134 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
136 CLockObject
lock(&m_mutex
);
137 if (!m_communication
|| m_communication
->IsOpen())
139 m_controller
->AddLog(CEC_LOG_ERROR
, "connection already opened");
143 if (!m_communication
->Open(strPort
, iBaudRate
, iTimeoutMs
))
145 m_controller
->AddLog(CEC_LOG_ERROR
, "could not open a connection");
151 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
153 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
158 if (SetAckMask(m_logicalAddresses
.AckMask()) &&
159 SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true))
161 m_controller
->AddLog(CEC_LOG_ERROR
, "processor thread started");
162 m_busScan
= new CCECBusScan(this);
163 m_busScan
->CreateThread(true);
168 m_controller
->AddLog(CEC_LOG_ERROR
, "failed to initialise the processor");
172 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
176 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
178 if (m_busDevices
[address
]->TryLogicalAddress())
180 /* only set our OSD name and active source for the primary device */
181 if (m_logicalAddresses
.IsEmpty())
183 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
184 m_busDevices
[address
]->m_bActiveSource
= true;
186 m_logicalAddresses
.Set(address
);
193 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
195 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
196 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
197 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
198 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
201 bool CCECProcessor::FindLogicalAddressTuner(void)
203 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
204 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
205 TryLogicalAddress(CECDEVICE_TUNER2
) ||
206 TryLogicalAddress(CECDEVICE_TUNER3
) ||
207 TryLogicalAddress(CECDEVICE_TUNER4
);
210 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
212 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
213 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
214 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
215 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
218 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
220 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
221 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
224 bool CCECProcessor::FindLogicalAddresses(void)
227 m_logicalAddresses
.Clear();
230 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
232 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
235 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
236 AddLog(CEC_LOG_DEBUG
, strLog
);
238 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
239 bReturn
&= FindLogicalAddressRecordingDevice();
240 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
241 bReturn
&= FindLogicalAddressTuner();
242 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
243 bReturn
&= FindLogicalAddressPlaybackDevice();
244 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
245 bReturn
&= FindLogicalAddressAudioSystem();
251 void *CCECProcessor::Process(void)
253 bool bParseFrame(false);
255 CCECAdapterMessage msg
;
257 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
259 CLockObject
lock(&m_mutex
);
260 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
261 m_startCondition
.Signal();
266 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPhysicalAddress();
267 CLockObject
lock(&m_mutex
);
269 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
270 m_startCondition
.Signal();
279 CLockObject
lock(&m_mutex
);
280 if (m_commandBuffer
.Pop(command
))
284 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
286 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
287 command
= m_currentframe
;
292 ParseCommand(command
);
297 m_controller
->CheckKeypressTimeout();
301 m_communication
->Close();
306 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
313 cec_logical_address addr
= m_logicalAddresses
.primary
;
315 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
317 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
319 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
321 addr
= (cec_logical_address
) iPtr
;
327 bReturn
= m_busDevices
[CECDEVICE_TV
]->PowerOn() &&
328 m_busDevices
[addr
]->TransmitActiveSource() &&
329 SetStreamPath(m_busDevices
[addr
]->GetPhysicalAddress(false));
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
);
340 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
342 CLockObject
lock(&m_mutex
);
343 m_iStandardLineTimeout
= iTimeout
;
346 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
348 CLockObject
lock(&m_mutex
);
349 m_iRetryLineTimeout
= iTimeout
;
352 bool CCECProcessor::SetActiveSource(cec_logical_address iAddress
)
354 return SetStreamPath(m_busDevices
[iAddress
]->GetPhysicalAddress(false));
357 bool CCECProcessor::SetActiveView(void)
359 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
362 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
366 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
369 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
371 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
378 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
382 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
385 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
387 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
394 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
399 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
400 AddLog(CEC_LOG_DEBUG
, strLog
);
402 m_iBaseDevice
= iBaseDevice
;
404 if (!m_bStarted
&& !bForce
)
407 uint16_t iPhysicalAddress(0);
408 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
410 if (iPhysicalAddress
== 0)
412 else if (iPhysicalAddress
% 0x1000 == 0)
414 else if (iPhysicalAddress
% 0x100 == 0)
416 else if (iPhysicalAddress
% 0x10 == 0)
419 while(!bReturn
&& iPos
> 0)
421 iPhysicalAddress
+= (uint16_t)(iPort
* iPos
);
422 strLog
.Format("checking physical address %4x", iPhysicalAddress
);
423 AddLog(CEC_LOG_DEBUG
, strLog
);
424 if (PhysicalAddressInUse(iPhysicalAddress
))
426 strLog
.Format("physical address %4x is in use", iPhysicalAddress
);
427 AddLog(CEC_LOG_DEBUG
, strLog
);
428 iPos
= (iPos
== 1) ? 0 : iPos
/ 0x10;
432 strLog
.Format("physical address %4x is free", iPhysicalAddress
);
433 AddLog(CEC_LOG_DEBUG
, strLog
);
434 SetPhysicalAddress(iPhysicalAddress
);
442 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
444 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
446 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
452 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath
)
456 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
459 device
->SetActiveDevice();
466 bool CCECProcessor::TransmitInactiveSource(void)
471 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
472 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
476 void CCECProcessor::LogOutput(const cec_command
&data
)
479 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
481 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
483 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
484 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
485 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
488 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
490 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
493 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
494 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
495 m_logicalAddresses
.primary
= iLogicalAddress
;
496 m_logicalAddresses
.Set(iLogicalAddress
);
497 return SetAckMask(m_logicalAddresses
.AckMask());
503 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
505 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
507 if (m_logicalAddresses
[iPtr
])
508 m_busDevices
[iPtr
]->SetMenuState(state
);
512 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
517 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
519 if (!m_logicalAddresses
.IsEmpty())
521 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
522 if (m_logicalAddresses
[iPtr
])
523 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
524 return SetActiveView();
529 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
532 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
533 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
536 CLockObject
lock(&m_mutex
);
537 m_bMonitor
= bEnable
;
543 m_busScan
= new CCECBusScan(this);
544 m_busScan
->CreateThread(true);
551 m_busScan
->StopThread();
559 return SetAckMask(0);
561 return SetAckMask(m_logicalAddresses
.AckMask());
564 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
566 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
568 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
569 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
570 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
575 uint8_t CCECProcessor::VolumeUp(bool bWait
/* = true */)
578 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
579 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bWait
);
584 uint8_t CCECProcessor::VolumeDown(bool bWait
/* = true */)
587 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
588 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bWait
);
593 uint8_t CCECProcessor::MuteAudio(bool bWait
/* = true */)
596 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM
))
597 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bWait
);
602 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
604 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
605 return m_busDevices
[m_logicalAddresses
.primary
];
607 CCECBusDevice
*device
= NULL
;
608 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
610 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
612 device
= m_busDevices
[iPtr
];
620 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
622 CCECBusDevice
*device
= NULL
;
624 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
626 if (m_busDevices
[iPtr
]->m_type
== type
)
628 device
= m_busDevices
[iPtr
];
636 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
638 return m_busDevices
[iAddress
]->GetCecVersion();
641 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
643 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
646 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
647 retVal
.device
= iAddress
;
652 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
654 if (m_busDevices
[iAddress
])
656 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
657 return (strcmp(language
->language
, "???") != 0);
662 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
664 if (m_busDevices
[iAddress
])
665 return m_busDevices
[iAddress
]->GetVendorId();
669 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
671 if (m_busDevices
[iAddress
])
672 return m_busDevices
[iAddress
]->GetPowerStatus();
673 return CEC_POWER_STATUS_UNKNOWN
;
676 bool CCECProcessor::Transmit(const cec_command
&data
)
681 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
682 bReturn
= Transmit(output
);
688 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
691 CLockObject
lock(&m_mutex
);
693 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
697 if (output
->tries
> 0)
698 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
700 CLockObject
msgLock(&output
->mutex
);
701 if (!m_communication
|| !m_communication
->Write(output
))
705 output
->condition
.Wait(&output
->mutex
);
706 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
708 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
713 if (output
->transmit_timeout
> 0)
715 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
716 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
720 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
<= output
->maxTries
);
723 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
728 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
730 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
734 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
735 command
.parameters
.PushBack((uint8_t)opcode
);
736 command
.parameters
.PushBack((uint8_t)reason
);
741 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
744 bool bTransmitSucceeded(false);
745 uint8_t iPacketsLeft(message
->size() / 4);
747 int64_t iNow
= GetTimeMs();
748 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
750 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
752 CCECAdapterMessage msg
;
754 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
760 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
762 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
763 m_lastInitiator
= msg
.initiator();
768 bError
= msg
.is_error();
769 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
770 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
771 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
779 message
->reply
= msg
.message();
780 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
784 switch(msg
.message())
786 case MSGCODE_COMMAND_ACCEPTED
:
787 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
788 if (iPacketsLeft
> 0)
791 case MSGCODE_TRANSMIT_SUCCEEDED
:
792 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
793 bTransmitSucceeded
= (iPacketsLeft
== 0);
794 bError
= !bTransmitSucceeded
;
795 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
798 // ignore other data while waiting
806 return bTransmitSucceeded
&& !bError
;
809 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
812 bool bIsError(msg
.is_error());
817 switch(msg
.message())
819 case MSGCODE_FRAME_START
:
821 m_currentframe
.Clear();
824 m_currentframe
.initiator
= msg
.initiator();
825 m_currentframe
.destination
= msg
.destination();
826 m_currentframe
.ack
= msg
.ack();
827 m_currentframe
.eom
= msg
.eom();
829 if (m_currentframe
.ack
== true)
831 m_lastInitiator
= m_currentframe
.initiator
;
832 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
836 case MSGCODE_RECEIVE_FAILED
:
838 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
839 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
842 case MSGCODE_FRAME_DATA
:
846 m_currentframe
.PushBack(msg
[1]);
847 m_currentframe
.eom
= msg
.eom();
856 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
860 void CCECProcessor::ParseCommand(cec_command
&command
)
863 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
864 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
865 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
866 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
868 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
869 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
872 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
874 cec_logical_addresses addresses
;
876 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
878 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
879 addresses
.Set((cec_logical_address
) iPtr
);
884 bool CCECProcessor::IsActiveDevice(cec_logical_address address
)
886 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
889 bool CCECProcessor::IsActiveDeviceType(cec_device_type type
)
891 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
893 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
900 uint16_t CCECProcessor::GetPhysicalAddress(void) const
902 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
903 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
907 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
909 m_controller
->SetCurrentButton(iButtonCode
);
912 void CCECProcessor::AddCommand(const cec_command
&command
)
914 m_controller
->AddCommand(command
);
917 void CCECProcessor::AddKey(cec_keypress
&key
)
919 m_controller
->AddKey(key
);
922 void CCECProcessor::AddKey(void)
924 m_controller
->AddKey();
927 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
929 m_controller
->AddLog(level
, strMessage
);
932 bool CCECProcessor::SetAckMask(uint16_t iMask
)
936 strLog
.Format("setting ackmask to %2x", iMask
);
937 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
939 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
941 output
->push_back(MSGSTART
);
942 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
943 output
->push_escaped(iMask
>> 8);
944 output
->push_escaped((uint8_t)iMask
);
945 output
->push_back(MSGEND
);
947 if ((bReturn
= Transmit(output
)) == false)
948 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
955 bool CCECProcessor::SendKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = false */)
957 return m_busDevices
[iDestination
]->SendKeypress(key
, bWait
);
960 bool CCECProcessor::SendKeyRelease(cec_logical_address iDestination
, bool bWait
/* = false */)
962 return m_busDevices
[iDestination
]->SendKeyRelease(bWait
);
965 const char *CCECProcessor::ToString(const cec_menu_state state
)
969 case CEC_MENU_STATE_ACTIVATED
:
971 case CEC_MENU_STATE_DEACTIVATED
:
972 return "deactivated";
978 const char *CCECProcessor::ToString(const cec_version version
)
982 case CEC_VERSION_1_2
:
984 case CEC_VERSION_1_2A
:
986 case CEC_VERSION_1_3
:
988 case CEC_VERSION_1_3A
:
990 case CEC_VERSION_1_4
:
997 const char *CCECProcessor::ToString(const cec_power_status status
)
1001 case CEC_POWER_STATUS_ON
:
1003 case CEC_POWER_STATUS_STANDBY
:
1005 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1006 return "in transition from on to standby";
1007 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1008 return "in transition from standby to on";
1014 const char *CCECProcessor::ToString(const cec_logical_address address
)
1018 case CECDEVICE_AUDIOSYSTEM
:
1020 case CECDEVICE_BROADCAST
:
1022 case CECDEVICE_FREEUSE
:
1024 case CECDEVICE_PLAYBACKDEVICE1
:
1025 return "Playback 1";
1026 case CECDEVICE_PLAYBACKDEVICE2
:
1027 return "Playback 2";
1028 case CECDEVICE_PLAYBACKDEVICE3
:
1029 return "Playback 3";
1030 case CECDEVICE_RECORDINGDEVICE1
:
1031 return "Recorder 1";
1032 case CECDEVICE_RECORDINGDEVICE2
:
1033 return "Recorder 2";
1034 case CECDEVICE_RECORDINGDEVICE3
:
1035 return "Recorder 3";
1036 case CECDEVICE_RESERVED1
:
1037 return "Reserved 1";
1038 case CECDEVICE_RESERVED2
:
1039 return "Reserved 2";
1040 case CECDEVICE_TUNER1
:
1042 case CECDEVICE_TUNER2
:
1044 case CECDEVICE_TUNER3
:
1046 case CECDEVICE_TUNER4
:
1055 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1059 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1060 return "skip forward wind";
1061 case CEC_DECK_CONTROL_MODE_EJECT
:
1063 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1064 return "reverse rewind";
1065 case CEC_DECK_CONTROL_MODE_STOP
:
1072 const char *CCECProcessor::ToString(const cec_deck_info status
)
1076 case CEC_DECK_INFO_PLAY
:
1078 case CEC_DECK_INFO_RECORD
:
1080 case CEC_DECK_INFO_PLAY_REVERSE
:
1081 return "play reverse";
1082 case CEC_DECK_INFO_STILL
:
1084 case CEC_DECK_INFO_SLOW
:
1086 case CEC_DECK_INFO_SLOW_REVERSE
:
1087 return "slow reverse";
1088 case CEC_DECK_INFO_FAST_FORWARD
:
1089 return "fast forward";
1090 case CEC_DECK_INFO_FAST_REVERSE
:
1091 return "fast reverse";
1092 case CEC_DECK_INFO_NO_MEDIA
:
1094 case CEC_DECK_INFO_STOP
:
1096 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1097 return "info skip forward wind";
1098 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1099 return "info skip reverse rewind";
1100 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1101 return "info index search forward";
1102 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1103 return "info index search reverse";
1104 case CEC_DECK_INFO_OTHER_STATUS
:
1111 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1115 case CEC_OPCODE_ACTIVE_SOURCE
:
1116 return "active source";
1117 case CEC_OPCODE_IMAGE_VIEW_ON
:
1118 return "image view on";
1119 case CEC_OPCODE_TEXT_VIEW_ON
:
1120 return "text view on";
1121 case CEC_OPCODE_INACTIVE_SOURCE
:
1122 return "inactive source";
1123 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1124 return "request active source";
1125 case CEC_OPCODE_ROUTING_CHANGE
:
1126 return "routing change";
1127 case CEC_OPCODE_ROUTING_INFORMATION
:
1128 return "routing information";
1129 case CEC_OPCODE_SET_STREAM_PATH
:
1130 return "set stream path";
1131 case CEC_OPCODE_STANDBY
:
1133 case CEC_OPCODE_RECORD_OFF
:
1134 return "record off";
1135 case CEC_OPCODE_RECORD_ON
:
1137 case CEC_OPCODE_RECORD_STATUS
:
1138 return "record status";
1139 case CEC_OPCODE_RECORD_TV_SCREEN
:
1140 return "record tv screen";
1141 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1142 return "clear analogue timer";
1143 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1144 return "clear digital timer";
1145 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1146 return "clear external timer";
1147 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1148 return "set analogue timer";
1149 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1150 return "set digital timer";
1151 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1152 return "set external timer";
1153 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1154 return "set timer program title";
1155 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1156 return "timer cleared status";
1157 case CEC_OPCODE_TIMER_STATUS
:
1158 return "timer status";
1159 case CEC_OPCODE_CEC_VERSION
:
1160 return "cec version";
1161 case CEC_OPCODE_GET_CEC_VERSION
:
1162 return "get cec version";
1163 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1164 return "give physical address";
1165 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1166 return "get menu language";
1167 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1168 return "report physical address";
1169 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1170 return "set menu language";
1171 case CEC_OPCODE_DECK_CONTROL
:
1172 return "deck control";
1173 case CEC_OPCODE_DECK_STATUS
:
1174 return "deck status";
1175 case CEC_OPCODE_GIVE_DECK_STATUS
:
1176 return "give deck status";
1177 case CEC_OPCODE_PLAY
:
1179 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1180 return "give tuner status";
1181 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1182 return "select analogue service";
1183 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1184 return "set digital service";
1185 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1186 return "tuner device status";
1187 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1188 return "tuner step decrement";
1189 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1190 return "tuner step increment";
1191 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1192 return "device vendor id";
1193 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1194 return "give device vendor id";
1195 case CEC_OPCODE_VENDOR_COMMAND
:
1196 return "vendor command";
1197 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1198 return "vendor command with id";
1199 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1200 return "vendor remote button down";
1201 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1202 return "vendor remote button up";
1203 case CEC_OPCODE_SET_OSD_STRING
:
1204 return "set osd string";
1205 case CEC_OPCODE_GIVE_OSD_NAME
:
1206 return "give osd name";
1207 case CEC_OPCODE_SET_OSD_NAME
:
1208 return "set osd name";
1209 case CEC_OPCODE_MENU_REQUEST
:
1210 return "menu request";
1211 case CEC_OPCODE_MENU_STATUS
:
1212 return "menu status";
1213 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1214 return "user control pressed";
1215 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1216 return "user control release";
1217 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1218 return "give device power status";
1219 case CEC_OPCODE_REPORT_POWER_STATUS
:
1220 return "report power status";
1221 case CEC_OPCODE_FEATURE_ABORT
:
1222 return "feature abort";
1223 case CEC_OPCODE_ABORT
:
1225 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1226 return "give audio status";
1227 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1228 return "give audio mode status";
1229 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1230 return "report audio status";
1231 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1232 return "set system audio mode";
1233 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1234 return "system audio mode request";
1235 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1236 return "system audio mode status";
1237 case CEC_OPCODE_SET_AUDIO_RATE
:
1238 return "set audio rate";
1244 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1248 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1250 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1257 const char *CCECProcessor::ToString(const cec_audio_status status
)
1259 // TODO this is a mask
1263 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1267 case CEC_VENDOR_SAMSUNG
:
1271 case CEC_VENDOR_PANASONIC
:
1273 case CEC_VENDOR_PIONEER
:
1275 case CEC_VENDOR_ONKYO
:
1277 case CEC_VENDOR_YAMAHA
:
1279 case CEC_VENDOR_PHILIPS
:
1286 void *CCECBusScan::Process(void)
1288 CCECBusDevice
*device(NULL
);
1290 while (!IsStopped())
1294 for (unsigned int iPtr
= 0; iPtr
< 15 && !IsStopped(); iPtr
++)
1296 device
= m_processor
->m_busDevices
[iPtr
];
1297 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1300 device
->GetVendorId();
1313 bool CCECProcessor::StartBootloader(void)
1315 return m_communication
->StartBootloader();
1318 bool CCECProcessor::PingAdapter(void)
1320 return m_communication
->PingAdapter();