a21b2b43254149cf51408d43c90365a81bbe6a4a
[deb_libcec.git] / src / lib / devices / CECDeviceMap.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33 #include "env.h"
34 #include "CECDeviceMap.h"
35
36 #include "CECAudioSystem.h"
37 #include "CECPlaybackDevice.h"
38 #include "CECRecordingDevice.h"
39 #include "CECTuner.h"
40 #include "CECTV.h"
41 #include "lib/CECProcessor.h"
42
43 using namespace std;
44 using namespace CEC;
45
46 CCECDeviceMap::CCECDeviceMap(CCECProcessor *processor) :
47 m_processor(processor)
48 {
49 for (uint8_t iPtr = CECDEVICE_TV; iPtr <= CECDEVICE_BROADCAST; iPtr++)
50 {
51 switch(iPtr)
52 {
53 case CECDEVICE_AUDIOSYSTEM:
54 m_busDevices.insert(make_pair<cec_logical_address, CCECBusDevice *>((cec_logical_address)iPtr, new CCECAudioSystem(processor, (cec_logical_address) iPtr)));
55 break;
56 case CECDEVICE_PLAYBACKDEVICE1:
57 case CECDEVICE_PLAYBACKDEVICE2:
58 case CECDEVICE_PLAYBACKDEVICE3:
59 m_busDevices.insert(make_pair<cec_logical_address, CCECBusDevice *>((cec_logical_address)iPtr, new CCECPlaybackDevice(processor, (cec_logical_address) iPtr)));
60 break;
61 case CECDEVICE_RECORDINGDEVICE1:
62 case CECDEVICE_RECORDINGDEVICE2:
63 case CECDEVICE_RECORDINGDEVICE3:
64 m_busDevices.insert(make_pair<cec_logical_address, CCECBusDevice *>((cec_logical_address)iPtr, new CCECRecordingDevice(processor, (cec_logical_address) iPtr)));
65 break;
66 case CECDEVICE_TUNER1:
67 case CECDEVICE_TUNER2:
68 case CECDEVICE_TUNER3:
69 case CECDEVICE_TUNER4:
70 m_busDevices.insert(make_pair<cec_logical_address, CCECBusDevice *>((cec_logical_address)iPtr, new CCECTuner(processor, (cec_logical_address) iPtr)));
71 break;
72 case CECDEVICE_TV:
73 m_busDevices.insert(make_pair<cec_logical_address, CCECBusDevice *>((cec_logical_address)iPtr, new CCECTV(processor, (cec_logical_address) iPtr)));
74 break;
75 default:
76 m_busDevices.insert(make_pair<cec_logical_address, CCECBusDevice *>((cec_logical_address)iPtr, new CCECBusDevice(processor, (cec_logical_address) iPtr)));
77 break;
78 }
79 }
80 }
81 CCECDeviceMap::~CCECDeviceMap(void)
82 {
83 Clear();
84 }
85
86 CECDEVICEMAP::iterator CCECDeviceMap::Begin(void)
87 {
88 return m_busDevices.begin();
89 }
90
91 CECDEVICEMAP::iterator CCECDeviceMap::End(void)
92 {
93 return m_busDevices.end();
94 }
95
96 void CCECDeviceMap::ResetDeviceStatus(void)
97 {
98 for (CECDEVICEMAP::iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
99 it->second->ResetDeviceStatus();
100 }
101
102 CCECBusDevice *CCECDeviceMap::operator[] (cec_logical_address iAddress) const
103 {
104 return At(iAddress);
105 }
106
107 CCECBusDevice *CCECDeviceMap::operator[] (uint8_t iAddress) const
108 {
109 return At(iAddress);
110 }
111
112 CCECBusDevice *CCECDeviceMap::At(cec_logical_address iAddress) const
113 {
114 return At((uint8_t) iAddress);
115 }
116
117 CCECBusDevice *CCECDeviceMap::At(uint8_t iAddress) const
118 {
119 CECDEVICEMAP::const_iterator it = m_busDevices.find((cec_logical_address)iAddress);
120 if (it != m_busDevices.end())
121 return it->second;
122 return NULL;
123 }
124
125 void CCECDeviceMap::Clear(void)
126 {
127 for (CECDEVICEMAP::iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
128 delete it->second;
129 m_busDevices.clear();
130 }
131
132 CCECBusDevice *CCECDeviceMap::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate /* = true */)
133 {
134 CCECBusDevice *device(NULL);
135
136 // check each device until we found a match
137 for (CECDEVICEMAP::iterator it = m_busDevices.begin(); !device && it != m_busDevices.end(); it++)
138 {
139 if (it->second->GetPhysicalAddress(m_processor->GetLogicalAddress(), bSuppressUpdate) == iPhysicalAddress)
140 device = it->second;
141 }
142
143 return device;
144 }
145
146 void CCECDeviceMap::Get(CECDEVICEVEC &devices) const
147 {
148 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
149 devices.push_back(it->second);
150 }
151
152 void CCECDeviceMap::GetByLogicalAddresses(CECDEVICEVEC &devices, const cec_logical_addresses &addresses)
153 {
154 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
155 {
156 if (addresses.IsSet(it->first))
157 devices.push_back(it->second);
158 }
159 }
160
161 void CCECDeviceMap::GetByType(const cec_device_type type, CECDEVICEVEC &devices) const
162 {
163 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
164 if (it->second->GetType() == type)
165 devices.push_back(it->second);
166 }
167
168 void CCECDeviceMap::GetLibCECControlled(CECDEVICEVEC &devices) const
169 {
170 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
171 if (it->second->IsHandledByLibCEC())
172 devices.push_back(it->second);
173 }
174
175 void CCECDeviceMap::GetActive(CECDEVICEVEC &devices) const
176 {
177 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
178 {
179 cec_bus_device_status status = it->second->GetStatus();
180 if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC ||
181 status == CEC_DEVICE_STATUS_PRESENT)
182 devices.push_back(it->second);
183 }
184 }
185
186 void CCECDeviceMap::GetPowerOffDevices(const libcec_configuration &configuration, CECDEVICEVEC &devices) const
187 {
188 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
189 {
190 if (configuration.powerOffDevices[(uint8_t)it->first])
191 devices.push_back(it->second);
192 }
193 }
194
195 void CCECDeviceMap::GetWakeDevices(const libcec_configuration &configuration, CECDEVICEVEC &devices) const
196 {
197 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
198 {
199 if (configuration.wakeDevices[(uint8_t)it->first])
200 devices.push_back(it->second);
201 }
202 }
203
204 CCECBusDevice *CCECDeviceMap::GetActiveSource(void) const
205 {
206 for (CECDEVICEMAP::const_iterator it = m_busDevices.begin(); it != m_busDevices.end(); it++)
207 {
208 if (it->second->IsActiveSource())
209 return it->second;
210 }
211 return NULL;
212 }
213
214 void CCECDeviceMap::FilterLibCECControlled(CECDEVICEVEC &devices)
215 {
216 CECDEVICEVEC newDevices;
217 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
218 {
219 if ((*it)->IsHandledByLibCEC())
220 newDevices.push_back(*it);
221 }
222 devices = newDevices;
223 }
224
225 void CCECDeviceMap::FilterActive(CECDEVICEVEC &devices)
226 {
227 CECDEVICEVEC newDevices;
228 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
229 {
230 cec_bus_device_status status = (*it)->GetCurrentStatus();
231 if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC ||
232 status == CEC_DEVICE_STATUS_PRESENT)
233 newDevices.push_back(*it);
234 }
235 devices = newDevices;
236 }
237
238 void CCECDeviceMap::FilterTypes(const cec_device_type_list &types, CECDEVICEVEC &devices)
239 {
240 cec_device_type_list t(types);//silly, but needed to retain abi
241 CECDEVICEVEC newDevices;
242 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
243 {
244 if (t.IsSet((*it)->GetType()))
245 newDevices.push_back(*it);
246 }
247 devices = newDevices;
248 }
249
250 void CCECDeviceMap::FilterType(const cec_device_type type, CECDEVICEVEC &devices)
251 {
252 CECDEVICEVEC newDevices;
253 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
254 {
255 if ((*it)->GetType() == type)
256 newDevices.push_back(*it);
257 }
258 devices = newDevices;
259 }
260
261 cec_logical_addresses CCECDeviceMap::ToLogicalAddresses(const CECDEVICEVEC &devices)
262 {
263 cec_logical_addresses addresses;
264 addresses.Clear();
265 for (CECDEVICEVEC::const_iterator it = devices.begin(); it != devices.end(); it++)
266 addresses.Set((*it)->GetLogicalAddress());
267 return addresses;
268 }