2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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/
34 #include "CECDeviceMap.h"
36 #include "CECAudioSystem.h"
37 #include "CECPlaybackDevice.h"
38 #include "CECRecordingDevice.h"
41 #include "lib/CECProcessor.h"
42 #include "lib/CECTypeUtils.h"
47 CCECDeviceMap::CCECDeviceMap(CCECProcessor
*processor
) :
48 m_processor(processor
),
49 m_iActiveSource(CEC_INVALID_PHYSICAL_ADDRESS
)
51 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
<= CECDEVICE_BROADCAST
; iPtr
++)
55 case CECDEVICE_AUDIOSYSTEM
:
56 m_busDevices
.insert(make_pair
<cec_logical_address
, CCECBusDevice
*>((cec_logical_address
)iPtr
, new CCECAudioSystem(processor
, (cec_logical_address
) iPtr
)));
58 case CECDEVICE_PLAYBACKDEVICE1
:
59 case CECDEVICE_PLAYBACKDEVICE2
:
60 case CECDEVICE_PLAYBACKDEVICE3
:
61 m_busDevices
.insert(make_pair
<cec_logical_address
, CCECBusDevice
*>((cec_logical_address
)iPtr
, new CCECPlaybackDevice(processor
, (cec_logical_address
) iPtr
)));
63 case CECDEVICE_RECORDINGDEVICE1
:
64 case CECDEVICE_RECORDINGDEVICE2
:
65 case CECDEVICE_RECORDINGDEVICE3
:
66 m_busDevices
.insert(make_pair
<cec_logical_address
, CCECBusDevice
*>((cec_logical_address
)iPtr
, new CCECRecordingDevice(processor
, (cec_logical_address
) iPtr
)));
68 case CECDEVICE_TUNER1
:
69 case CECDEVICE_TUNER2
:
70 case CECDEVICE_TUNER3
:
71 case CECDEVICE_TUNER4
:
72 m_busDevices
.insert(make_pair
<cec_logical_address
, CCECBusDevice
*>((cec_logical_address
)iPtr
, new CCECTuner(processor
, (cec_logical_address
) iPtr
)));
75 m_busDevices
.insert(make_pair
<cec_logical_address
, CCECBusDevice
*>((cec_logical_address
)iPtr
, new CCECTV(processor
, (cec_logical_address
) iPtr
)));
78 m_busDevices
.insert(make_pair
<cec_logical_address
, CCECBusDevice
*>((cec_logical_address
)iPtr
, new CCECBusDevice(processor
, (cec_logical_address
) iPtr
)));
83 CCECDeviceMap::~CCECDeviceMap(void)
88 CECDEVICEMAP::iterator
CCECDeviceMap::Begin(void)
90 return m_busDevices
.begin();
93 CECDEVICEMAP::iterator
CCECDeviceMap::End(void)
95 return m_busDevices
.end();
98 void CCECDeviceMap::ResetDeviceStatus(void)
100 for (CECDEVICEMAP::iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
101 it
->second
->ResetDeviceStatus();
104 CCECBusDevice
*CCECDeviceMap::operator[] (cec_logical_address iAddress
) const
109 CCECBusDevice
*CCECDeviceMap::operator[] (uint8_t iAddress
) const
114 CCECBusDevice
*CCECDeviceMap::At(cec_logical_address iAddress
) const
116 return At((uint8_t) iAddress
);
119 CCECBusDevice
*CCECDeviceMap::At(uint8_t iAddress
) const
121 CECDEVICEMAP::const_iterator it
= m_busDevices
.find((cec_logical_address
)iAddress
);
122 if (it
!= m_busDevices
.end())
127 void CCECDeviceMap::Clear(void)
129 for (CECDEVICEMAP::iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
131 m_busDevices
.clear();
134 CCECBusDevice
*CCECDeviceMap::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bSuppressUpdate
/* = true */)
136 CCECBusDevice
*device(NULL
);
138 // check each device until we found a match
139 for (CECDEVICEMAP::iterator it
= m_busDevices
.begin(); !device
&& it
!= m_busDevices
.end(); it
++)
141 if (it
->second
->GetPhysicalAddress(m_processor
->GetLogicalAddress(), bSuppressUpdate
) == iPhysicalAddress
)
148 void CCECDeviceMap::Get(CECDEVICEVEC
&devices
) const
150 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
151 devices
.push_back(it
->second
);
154 void CCECDeviceMap::GetByLogicalAddresses(CECDEVICEVEC
&devices
, const cec_logical_addresses
&addresses
)
156 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
158 if (addresses
.IsSet(it
->first
))
159 devices
.push_back(it
->second
);
163 void CCECDeviceMap::GetByType(const cec_device_type type
, CECDEVICEVEC
&devices
) const
165 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
166 if (it
->second
->GetType() == type
)
167 devices
.push_back(it
->second
);
170 void CCECDeviceMap::GetLibCECControlled(CECDEVICEVEC
&devices
) const
172 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
173 if (it
->second
->IsHandledByLibCEC())
174 devices
.push_back(it
->second
);
177 void CCECDeviceMap::GetActive(CECDEVICEVEC
&devices
) const
179 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
181 cec_bus_device_status status
= it
->second
->GetStatus();
182 if (status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
||
183 status
== CEC_DEVICE_STATUS_PRESENT
)
184 devices
.push_back(it
->second
);
188 void CCECDeviceMap::GetPowerOffDevices(const libcec_configuration
&configuration
, CECDEVICEVEC
&devices
) const
190 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
192 if (configuration
.powerOffDevices
[(uint8_t)it
->first
])
193 devices
.push_back(it
->second
);
197 void CCECDeviceMap::GetWakeDevices(const libcec_configuration
&configuration
, CECDEVICEVEC
&devices
) const
199 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
201 if (configuration
.wakeDevices
[(uint8_t)it
->first
])
202 devices
.push_back(it
->second
);
206 CCECBusDevice
*CCECDeviceMap::GetActiveSource(void) const
208 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
210 if (m_iActiveSource
!= CEC_INVALID_PHYSICAL_ADDRESS
&& !it
->second
->IsActiveSource() &&
211 it
->second
->GetCurrentPowerStatus() == CEC_POWER_STATUS_ON
&&
212 m_iActiveSource
== it
->second
->GetCurrentPhysicalAddress())
213 it
->second
->MarkAsActiveSource();
214 if (it
->second
->IsActiveSource())
220 void CCECDeviceMap::FilterLibCECControlled(CECDEVICEVEC
&devices
)
222 CECDEVICEVEC newDevices
;
223 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
225 if ((*it
)->IsHandledByLibCEC())
226 newDevices
.push_back(*it
);
228 devices
= newDevices
;
231 void CCECDeviceMap::FilterActive(CECDEVICEVEC
&devices
)
233 CECDEVICEVEC newDevices
;
234 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
236 cec_bus_device_status status
= (*it
)->GetCurrentStatus();
237 if (status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
||
238 status
== CEC_DEVICE_STATUS_PRESENT
)
239 newDevices
.push_back(*it
);
241 devices
= newDevices
;
244 void CCECDeviceMap::FilterTypes(const cec_device_type_list
&types
, CECDEVICEVEC
&devices
)
246 cec_device_type_list
t(types
);//silly, but needed to retain abi
247 CECDEVICEVEC newDevices
;
248 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
250 if (t
.IsSet((*it
)->GetType()))
251 newDevices
.push_back(*it
);
253 devices
= newDevices
;
256 void CCECDeviceMap::FilterType(const cec_device_type type
, CECDEVICEVEC
&devices
)
258 CECDEVICEVEC newDevices
;
259 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
261 if ((*it
)->GetType() == type
)
262 newDevices
.push_back(*it
);
264 devices
= newDevices
;
267 cec_logical_addresses
CCECDeviceMap::ToLogicalAddresses(const CECDEVICEVEC
&devices
)
269 cec_logical_addresses addresses
;
271 for (CECDEVICEVEC::const_iterator it
= devices
.begin(); it
!= devices
.end(); it
++)
272 addresses
.Set((*it
)->GetLogicalAddress());
276 void CCECDeviceMap::GetChildrenOf(CECDEVICEVEC
& devices
, CCECBusDevice
* device
) const
282 uint16_t iPA
= device
->GetCurrentPhysicalAddress();
284 for (CECDEVICEMAP::const_iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
286 uint16_t iCurrentPA
= it
->second
->GetCurrentPhysicalAddress();
287 if (CCECTypeUtils::PhysicalAddressIsIncluded(iPA
, iCurrentPA
))
288 devices
.push_back(it
->second
);
292 void CCECDeviceMap::SetActiveSource(uint16_t iPhysicalAddress
)
294 m_iActiveSource
= iPhysicalAddress
;
297 uint16_t CCECDeviceMap::GetActiveSourceAddress(void) const
299 return m_iActiveSource
;
302 void CCECDeviceMap::SignalAll(cec_opcode opcode
)
304 for (CECDEVICEMAP::iterator it
= m_busDevices
.begin(); it
!= m_busDevices
.end(); it
++)
305 it
->second
->SignalOpcode(opcode
);