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_bInitialised(false),
53 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
54 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
55 m_lastInitiator(CECDEVICE_UNKNOWN
),
56 m_strDeviceName(strDeviceName
),
57 m_controller(controller
),
59 m_iStandardLineTimeout(3),
60 m_iRetryLineTimeout(3),
61 m_iLastTransmission(0)
63 m_communication
= new CAdapterCommunication(this);
64 m_logicalAddresses
.Clear();
65 m_logicalAddresses
.Set(iLogicalAddress
);
67 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
68 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
71 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
) :
73 m_bInitialised(false),
74 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
75 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
76 m_strDeviceName(strDeviceName
),
78 m_controller(controller
),
80 m_iStandardLineTimeout(3),
81 m_iRetryLineTimeout(3),
82 m_iLastTransmission(0)
84 m_communication
= new CAdapterCommunication(this);
85 m_logicalAddresses
.Clear();
86 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
90 case CECDEVICE_AUDIOSYSTEM
:
91 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
93 case CECDEVICE_PLAYBACKDEVICE1
:
94 case CECDEVICE_PLAYBACKDEVICE2
:
95 case CECDEVICE_PLAYBACKDEVICE3
:
96 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
98 case CECDEVICE_RECORDINGDEVICE1
:
99 case CECDEVICE_RECORDINGDEVICE2
:
100 case CECDEVICE_RECORDINGDEVICE3
:
101 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
103 case CECDEVICE_TUNER1
:
104 case CECDEVICE_TUNER2
:
105 case CECDEVICE_TUNER3
:
106 case CECDEVICE_TUNER4
:
107 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
110 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
113 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
119 CCECProcessor::~CCECProcessor(void)
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 */)
137 CLockObject
lock(&m_mutex
);
139 /* check for an already opened connection */
140 if (!m_communication
|| m_communication
->IsOpen())
142 m_controller
->AddLog(CEC_LOG_ERROR
, "connection already opened");
146 /* open a new connection */
147 if (!m_communication
->Open(strPort
, iBaudRate
, iTimeoutMs
))
149 m_controller
->AddLog(CEC_LOG_ERROR
, "could not open a connection");
153 /* create the processor thread */
154 if (!CreateThread() || !m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
156 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
161 /* find the logical address for the adapter */
162 bReturn
= m_logicalAddresses
.IsEmpty() ? FindLogicalAddresses() : true;
164 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
166 /* set the physical address for the adapter */
169 /* only set our OSD name for the primary device */
170 m_busDevices
[m_logicalAddresses
.primary
]->m_strDeviceName
= m_strDeviceName
;
172 /* get the vendor id from the TV, so we are using the correct handler */
173 m_busDevices
[CECDEVICE_TV
]->RequestVendorId();
176 bReturn
= SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true);
181 m_bInitialised
= true;
182 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
186 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
193 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
195 if (m_busDevices
[address
]->TryLogicalAddress())
197 m_logicalAddresses
.Set(address
);
204 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
206 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
207 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
208 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
209 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
212 bool CCECProcessor::FindLogicalAddressTuner(void)
214 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
215 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
216 TryLogicalAddress(CECDEVICE_TUNER2
) ||
217 TryLogicalAddress(CECDEVICE_TUNER3
) ||
218 TryLogicalAddress(CECDEVICE_TUNER4
);
221 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
223 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
224 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
225 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
226 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
229 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
231 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
232 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
235 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
237 bool bChanged(false);
240 strLog
.Format("changing device type '%s' into '%s'", ToString(from
), ToString(to
));
241 AddLog(CEC_LOG_NOTICE
, strLog
);
243 CLockObject
lock(&m_mutex
);
244 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
245 m_logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
247 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
249 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
252 if (m_types
.types
[iPtr
] == from
)
255 m_types
.types
[iPtr
] = to
;
257 else if (m_types
.types
[iPtr
] == to
&& bChanged
)
259 m_types
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
265 FindLogicalAddresses();
267 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
268 if (previousDevice
&& newDevice
)
270 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
271 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_UNKNOWN
);
273 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
274 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
276 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
277 cec_menu_language lang
;
278 lang
.device
= previousDevice
->GetLogicalAddress();
279 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
280 lang
.language
[iPtr
] = '?';
281 lang
.language
[3] = 0;
282 previousDevice
->SetMenuLanguage(lang
);
284 newDevice
->SetMenuState(previousDevice
->GetMenuState());
285 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
287 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
288 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
290 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
291 previousDevice
->SetPhysicalAddress(0xFFFF);
293 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
294 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
296 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
297 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
299 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
300 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
302 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
303 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
305 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
306 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
314 bool CCECProcessor::FindLogicalAddresses(void)
317 m_logicalAddresses
.Clear();
320 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
322 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
325 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
326 AddLog(CEC_LOG_DEBUG
, strLog
);
328 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
329 bReturn
&= FindLogicalAddressRecordingDevice();
330 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
331 bReturn
&= FindLogicalAddressTuner();
332 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
333 bReturn
&= FindLogicalAddressPlaybackDevice();
334 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
335 bReturn
&= FindLogicalAddressAudioSystem();
339 SetAckMask(m_logicalAddresses
.AckMask());
344 void CCECProcessor::ReplaceHandlers(void)
346 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
347 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
350 void *CCECProcessor::Process(void)
352 bool bParseFrame(false);
354 CCECAdapterMessage msg
;
357 CLockObject
lock(&m_mutex
);
359 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
360 m_startCondition
.Signal();
370 CLockObject
lock(&m_mutex
);
371 if (m_commandBuffer
.Pop(command
))
375 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
377 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
378 command
= m_currentframe
;
383 ParseCommand(command
);
388 m_controller
->CheckKeypressTimeout();
392 m_communication
->Close();
397 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
404 cec_logical_address addr
= m_logicalAddresses
.primary
;
406 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
408 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
410 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
412 addr
= (cec_logical_address
) iPtr
;
418 m_busDevices
[addr
]->SetActiveSource();
419 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
420 bReturn
= m_busDevices
[addr
]->GetHandler()->ActivateSource();
425 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
429 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
432 device
->SetActiveSource();
439 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
441 CLockObject
lock(&m_mutex
);
442 m_iStandardLineTimeout
= iTimeout
;
445 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
447 CLockObject
lock(&m_mutex
);
448 m_iRetryLineTimeout
= iTimeout
;
451 bool CCECProcessor::SetActiveView(void)
453 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
456 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
460 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
463 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
465 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
472 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
476 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
479 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
481 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
488 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
492 CLockObject
lock(&m_mutex
);
494 m_iBaseDevice
= iBaseDevice
;
496 if (!m_bStarted
&& !bForce
)
501 strLog
.Format("setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
502 AddLog(CEC_LOG_DEBUG
, strLog
);
504 uint16_t iPhysicalAddress(0);
505 if (iBaseDevice
> CECDEVICE_TV
)
506 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
508 if (iPhysicalAddress
< 0xffff)
510 if (iPhysicalAddress
== 0)
511 iPhysicalAddress
+= 0x1000 * iPort
;
512 else if (iPhysicalAddress
% 0x1000 == 0)
513 iPhysicalAddress
+= 0x100 * iPort
;
514 else if (iPhysicalAddress
% 0x100 == 0)
515 iPhysicalAddress
+= 0x10 * iPort
;
516 else if (iPhysicalAddress
% 0x10 == 0)
517 iPhysicalAddress
+= iPort
;
519 SetPhysicalAddress(iPhysicalAddress
);
524 m_controller
->AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
529 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
531 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
533 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
539 bool CCECProcessor::TransmitInactiveSource(void)
544 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
545 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
549 void CCECProcessor::LogOutput(const cec_command
&data
)
552 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
554 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
556 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
557 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
558 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
561 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
563 CLockObject
lock(&m_mutex
);
564 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
567 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
568 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
569 m_logicalAddresses
.primary
= iLogicalAddress
;
570 m_logicalAddresses
.Set(iLogicalAddress
);
571 return SetAckMask(m_logicalAddresses
.AckMask());
577 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
579 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
581 if (m_logicalAddresses
[iPtr
])
582 m_busDevices
[iPtr
]->SetMenuState(state
);
586 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
591 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
593 bool bWasActiveSource(false);
594 CLockObject
lock(&m_mutex
);
595 if (!m_logicalAddresses
.IsEmpty())
597 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
598 if (m_logicalAddresses
[iPtr
])
600 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
601 m_busDevices
[iPtr
]->SetInactiveSource();
602 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
604 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
607 return bWasActiveSource
&& bSendUpdate
? SetActiveView() : true;
612 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
615 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
616 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
619 CLockObject
lock(&m_mutex
);
620 m_bMonitor
= bEnable
;
624 return SetAckMask(0);
626 return SetAckMask(m_logicalAddresses
.AckMask());
629 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
631 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
633 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
634 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
635 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
640 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
643 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
644 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
649 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
652 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
653 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
658 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
661 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
662 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
667 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
669 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
670 return m_busDevices
[m_logicalAddresses
.primary
];
672 CCECBusDevice
*device
= NULL
;
673 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
675 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
677 device
= m_busDevices
[iPtr
];
685 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
687 CCECBusDevice
*device
= NULL
;
689 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
691 if (m_busDevices
[iPtr
]->m_type
== type
&& m_logicalAddresses
[iPtr
])
693 device
= m_busDevices
[iPtr
];
701 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
703 CCECBusDevice
*device(NULL
);
704 cec_logical_address primary
= m_logicalAddresses
.primary
;
705 if (primary
!= CECDEVICE_UNKNOWN
)
706 device
= m_busDevices
[primary
];
710 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
712 return m_busDevices
[iAddress
]->GetCecVersion();
715 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
717 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
720 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
721 retVal
.device
= iAddress
;
726 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
728 if (m_busDevices
[iAddress
])
730 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
731 return (strcmp(language
->language
, "???") != 0);
736 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
738 if (m_busDevices
[iAddress
])
739 return m_busDevices
[iAddress
]->GetVendorId();
743 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
745 if (m_busDevices
[iAddress
])
746 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
750 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
752 if (m_busDevices
[iAddress
])
753 return m_busDevices
[iAddress
]->GetPowerStatus();
754 return CEC_POWER_STATUS_UNKNOWN
;
757 cec_logical_address
CCECProcessor::GetActiveSource(void)
759 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
761 if (m_busDevices
[iPtr
]->IsActiveSource())
762 return (cec_logical_address
)iPtr
;
765 return CECDEVICE_UNKNOWN
;
768 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
770 return m_busDevices
[iAddress
]->IsActiveSource();
773 bool CCECProcessor::Transmit(const cec_command
&data
)
778 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
780 /* set the number of retries */
781 if (data
.opcode
== CEC_OPCODE_NONE
)
782 output
->maxTries
= 1;
783 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
784 output
->maxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
786 bReturn
= Transmit(output
);
788 /* set to "not present" on failed ack */
789 if (output
->is_error() && output
->reply
== MSGCODE_TRANSMIT_FAILED_ACK
&&
790 output
->destination() != CECDEVICE_BROADCAST
)
791 m_busDevices
[output
->destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
797 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
800 CLockObject
lock(&m_mutex
);
802 m_iLastTransmission
= GetTimeMs();
803 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
808 if (output
->tries
> 0)
809 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
811 CLockObject
msgLock(&output
->mutex
);
812 if (!m_communication
|| !m_communication
->Write(output
))
816 output
->condition
.Wait(&output
->mutex
);
817 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
819 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
824 if (output
->transmit_timeout
> 0)
826 if ((bReturn
= WaitForTransmitSucceeded(output
)) == false)
827 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
831 }while (output
->transmit_timeout
> 0 && output
->needs_retry() && ++output
->tries
< output
->maxTries
);
834 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
839 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
841 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
845 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
846 command
.parameters
.PushBack((uint8_t)opcode
);
847 command
.parameters
.PushBack((uint8_t)reason
);
852 bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage
*message
)
855 bool bTransmitSucceeded(false);
856 uint8_t iPacketsLeft(message
->size() / 4);
858 int64_t iNow
= GetTimeMs();
859 int64_t iTargetTime
= iNow
+ message
->transmit_timeout
;
861 while (!bTransmitSucceeded
&& !bError
&& (message
->transmit_timeout
== 0 || iNow
< iTargetTime
))
863 CCECAdapterMessage msg
;
865 if (!m_communication
->Read(msg
, message
->transmit_timeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
871 if (msg
.message() == MSGCODE_FRAME_START
&& msg
.ack())
873 m_busDevices
[msg
.initiator()]->GetHandler()->HandlePoll(msg
.initiator(), msg
.destination());
874 m_lastInitiator
= msg
.initiator();
879 bError
= msg
.is_error();
880 if (msg
.message() == MSGCODE_RECEIVE_FAILED
&&
881 m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
882 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed())
890 message
->reply
= msg
.message();
891 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
895 switch(msg
.message())
897 case MSGCODE_COMMAND_ACCEPTED
:
898 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
899 if (iPacketsLeft
> 0)
902 case MSGCODE_TRANSMIT_SUCCEEDED
:
903 m_controller
->AddLog(CEC_LOG_DEBUG
, msg
.ToString());
904 bTransmitSucceeded
= (iPacketsLeft
== 0);
905 bError
= !bTransmitSucceeded
;
906 message
->reply
= MSGCODE_TRANSMIT_SUCCEEDED
;
909 // ignore other data while waiting
917 return bTransmitSucceeded
&& !bError
;
920 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
923 bool bIsError(msg
.is_error());
928 switch(msg
.message())
930 case MSGCODE_FRAME_START
:
932 m_currentframe
.Clear();
935 m_currentframe
.initiator
= msg
.initiator();
936 m_currentframe
.destination
= msg
.destination();
937 m_currentframe
.ack
= msg
.ack();
938 m_currentframe
.eom
= msg
.eom();
940 if (m_currentframe
.ack
== 0x1)
942 m_lastInitiator
= m_currentframe
.initiator
;
943 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
947 case MSGCODE_RECEIVE_FAILED
:
949 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
950 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
953 case MSGCODE_FRAME_DATA
:
957 m_currentframe
.PushBack(msg
[1]);
958 m_currentframe
.eom
= msg
.eom();
967 m_controller
->AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
971 void CCECProcessor::ParseCommand(cec_command
&command
)
974 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
975 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
976 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
977 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
979 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
980 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
983 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
985 cec_logical_addresses addresses
;
987 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
989 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
990 addresses
.Set((cec_logical_address
) iPtr
);
995 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
997 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
1000 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
1002 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1004 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1011 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1013 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
1014 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
1018 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
1020 m_controller
->SetCurrentButton(iButtonCode
);
1023 void CCECProcessor::AddCommand(const cec_command
&command
)
1025 m_controller
->AddCommand(command
);
1028 void CCECProcessor::AddKey(cec_keypress
&key
)
1030 m_controller
->AddKey(key
);
1033 void CCECProcessor::AddKey(void)
1035 m_controller
->AddKey();
1038 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
1040 m_controller
->AddLog(level
, strMessage
);
1043 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1045 bool bReturn(false);
1047 strLog
.Format("setting ackmask to %2x", iMask
);
1048 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
1050 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
1052 output
->push_back(MSGSTART
);
1053 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
1054 output
->push_escaped(iMask
>> 8);
1055 output
->push_escaped((uint8_t)iMask
);
1056 output
->push_back(MSGEND
);
1058 if ((bReturn
= Transmit(output
)) == false)
1059 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");
1066 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1068 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1071 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1073 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1076 const char *CCECProcessor::ToString(const cec_device_type type
)
1080 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1081 return "audio system";
1082 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1083 return "playback device";
1084 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1085 return "recording device";
1086 case CEC_DEVICE_TYPE_RESERVED
:
1088 case CEC_DEVICE_TYPE_TUNER
:
1090 case CEC_DEVICE_TYPE_TV
:
1097 const char *CCECProcessor::ToString(const cec_menu_state state
)
1101 case CEC_MENU_STATE_ACTIVATED
:
1103 case CEC_MENU_STATE_DEACTIVATED
:
1104 return "deactivated";
1110 const char *CCECProcessor::ToString(const cec_version version
)
1114 case CEC_VERSION_1_2
:
1116 case CEC_VERSION_1_2A
:
1118 case CEC_VERSION_1_3
:
1120 case CEC_VERSION_1_3A
:
1122 case CEC_VERSION_1_4
:
1129 const char *CCECProcessor::ToString(const cec_power_status status
)
1133 case CEC_POWER_STATUS_ON
:
1135 case CEC_POWER_STATUS_STANDBY
:
1137 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1138 return "in transition from on to standby";
1139 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1140 return "in transition from standby to on";
1146 const char *CCECProcessor::ToString(const cec_logical_address address
)
1150 case CECDEVICE_AUDIOSYSTEM
:
1152 case CECDEVICE_BROADCAST
:
1154 case CECDEVICE_FREEUSE
:
1156 case CECDEVICE_PLAYBACKDEVICE1
:
1157 return "Playback 1";
1158 case CECDEVICE_PLAYBACKDEVICE2
:
1159 return "Playback 2";
1160 case CECDEVICE_PLAYBACKDEVICE3
:
1161 return "Playback 3";
1162 case CECDEVICE_RECORDINGDEVICE1
:
1163 return "Recorder 1";
1164 case CECDEVICE_RECORDINGDEVICE2
:
1165 return "Recorder 2";
1166 case CECDEVICE_RECORDINGDEVICE3
:
1167 return "Recorder 3";
1168 case CECDEVICE_RESERVED1
:
1169 return "Reserved 1";
1170 case CECDEVICE_RESERVED2
:
1171 return "Reserved 2";
1172 case CECDEVICE_TUNER1
:
1174 case CECDEVICE_TUNER2
:
1176 case CECDEVICE_TUNER3
:
1178 case CECDEVICE_TUNER4
:
1187 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1191 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1192 return "skip forward wind";
1193 case CEC_DECK_CONTROL_MODE_EJECT
:
1195 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1196 return "reverse rewind";
1197 case CEC_DECK_CONTROL_MODE_STOP
:
1204 const char *CCECProcessor::ToString(const cec_deck_info status
)
1208 case CEC_DECK_INFO_PLAY
:
1210 case CEC_DECK_INFO_RECORD
:
1212 case CEC_DECK_INFO_PLAY_REVERSE
:
1213 return "play reverse";
1214 case CEC_DECK_INFO_STILL
:
1216 case CEC_DECK_INFO_SLOW
:
1218 case CEC_DECK_INFO_SLOW_REVERSE
:
1219 return "slow reverse";
1220 case CEC_DECK_INFO_FAST_FORWARD
:
1221 return "fast forward";
1222 case CEC_DECK_INFO_FAST_REVERSE
:
1223 return "fast reverse";
1224 case CEC_DECK_INFO_NO_MEDIA
:
1226 case CEC_DECK_INFO_STOP
:
1228 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1229 return "info skip forward wind";
1230 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1231 return "info skip reverse rewind";
1232 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1233 return "info index search forward";
1234 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1235 return "info index search reverse";
1236 case CEC_DECK_INFO_OTHER_STATUS
:
1243 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1247 case CEC_OPCODE_ACTIVE_SOURCE
:
1248 return "active source";
1249 case CEC_OPCODE_IMAGE_VIEW_ON
:
1250 return "image view on";
1251 case CEC_OPCODE_TEXT_VIEW_ON
:
1252 return "text view on";
1253 case CEC_OPCODE_INACTIVE_SOURCE
:
1254 return "inactive source";
1255 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1256 return "request active source";
1257 case CEC_OPCODE_ROUTING_CHANGE
:
1258 return "routing change";
1259 case CEC_OPCODE_ROUTING_INFORMATION
:
1260 return "routing information";
1261 case CEC_OPCODE_SET_STREAM_PATH
:
1262 return "set stream path";
1263 case CEC_OPCODE_STANDBY
:
1265 case CEC_OPCODE_RECORD_OFF
:
1266 return "record off";
1267 case CEC_OPCODE_RECORD_ON
:
1269 case CEC_OPCODE_RECORD_STATUS
:
1270 return "record status";
1271 case CEC_OPCODE_RECORD_TV_SCREEN
:
1272 return "record tv screen";
1273 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1274 return "clear analogue timer";
1275 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1276 return "clear digital timer";
1277 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1278 return "clear external timer";
1279 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1280 return "set analogue timer";
1281 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1282 return "set digital timer";
1283 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1284 return "set external timer";
1285 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1286 return "set timer program title";
1287 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1288 return "timer cleared status";
1289 case CEC_OPCODE_TIMER_STATUS
:
1290 return "timer status";
1291 case CEC_OPCODE_CEC_VERSION
:
1292 return "cec version";
1293 case CEC_OPCODE_GET_CEC_VERSION
:
1294 return "get cec version";
1295 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1296 return "give physical address";
1297 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1298 return "get menu language";
1299 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1300 return "report physical address";
1301 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1302 return "set menu language";
1303 case CEC_OPCODE_DECK_CONTROL
:
1304 return "deck control";
1305 case CEC_OPCODE_DECK_STATUS
:
1306 return "deck status";
1307 case CEC_OPCODE_GIVE_DECK_STATUS
:
1308 return "give deck status";
1309 case CEC_OPCODE_PLAY
:
1311 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1312 return "give tuner status";
1313 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1314 return "select analogue service";
1315 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1316 return "set digital service";
1317 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1318 return "tuner device status";
1319 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1320 return "tuner step decrement";
1321 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1322 return "tuner step increment";
1323 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1324 return "device vendor id";
1325 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1326 return "give device vendor id";
1327 case CEC_OPCODE_VENDOR_COMMAND
:
1328 return "vendor command";
1329 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1330 return "vendor command with id";
1331 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1332 return "vendor remote button down";
1333 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1334 return "vendor remote button up";
1335 case CEC_OPCODE_SET_OSD_STRING
:
1336 return "set osd string";
1337 case CEC_OPCODE_GIVE_OSD_NAME
:
1338 return "give osd name";
1339 case CEC_OPCODE_SET_OSD_NAME
:
1340 return "set osd name";
1341 case CEC_OPCODE_MENU_REQUEST
:
1342 return "menu request";
1343 case CEC_OPCODE_MENU_STATUS
:
1344 return "menu status";
1345 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1346 return "user control pressed";
1347 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1348 return "user control release";
1349 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1350 return "give device power status";
1351 case CEC_OPCODE_REPORT_POWER_STATUS
:
1352 return "report power status";
1353 case CEC_OPCODE_FEATURE_ABORT
:
1354 return "feature abort";
1355 case CEC_OPCODE_ABORT
:
1357 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1358 return "give audio status";
1359 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1360 return "give audio mode status";
1361 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1362 return "report audio status";
1363 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1364 return "set system audio mode";
1365 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1366 return "system audio mode request";
1367 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1368 return "system audio mode status";
1369 case CEC_OPCODE_SET_AUDIO_RATE
:
1370 return "set audio rate";
1376 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1380 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1382 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1389 const char *CCECProcessor::ToString(const cec_audio_status status
)
1391 // TODO this is a mask
1395 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1399 case CEC_VENDOR_SAMSUNG
:
1403 case CEC_VENDOR_PANASONIC
:
1405 case CEC_VENDOR_PIONEER
:
1407 case CEC_VENDOR_ONKYO
:
1409 case CEC_VENDOR_YAMAHA
:
1411 case CEC_VENDOR_PHILIPS
:
1418 void *CCECBusScan::Process(void)
1420 CCECBusDevice
*device(NULL
);
1421 uint8_t iCounter(0);
1423 while (!IsStopped())
1425 if (++iCounter
< 10)
1430 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1432 device
= m_processor
->m_busDevices
[iPtr
];
1434 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1438 device
->GetVendorId();
1442 device
->GetPowerStatus(true);
1450 void CCECBusScan::WaitUntilIdle(void)
1455 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1456 while (iWaitTime
> 0)
1459 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1463 bool CCECProcessor::StartBootloader(void)
1465 return m_communication
->StartBootloader();
1468 bool CCECProcessor::PingAdapter(void)
1470 return m_communication
->PingAdapter();