2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "listplusplus.h"
26 using namespace std::placeholders;
30 static int PPSUpdate(void* data)
32 int* pps = (int*)data;
36 if(temp > 0 && temp != lastpps)
39 DebugOut(1)<<"Property updates per second: "<<temp<<endl;
46 :propertiesPerSecond(0)
48 g_timeout_add(1000,PPSUpdate,&propertiesPerSecond);
57 void Core::setSupported(PropertyList supported, AbstractSource* source)
60 if(!ListPlusPlus<AbstractSource*>(&mSources).contains(source))
61 mSources.push_back(source);
63 for(PropertyList::iterator itr = supported.begin(); itr != supported.end(); itr++)
65 if(!ListPlusPlus<VehicleProperty::Property>(&mMasterPropertyList).contains((*itr)))
67 DebugOut()<<__FUNCTION__<<"() adding support for property "<<(*itr)<<endl;
68 mMasterPropertyList.push_back((*itr));
72 /// tell all new sinks about the newly supported properties.
74 for(SinkList::iterator itr = mSinks.begin(); itr != mSinks.end(); itr++)
76 (*itr)->supportedChanged(mMasterPropertyList);
79 /// iterate through subscribed properties and resubscribe. This catches newly supported properties in the process.
81 for(map<VehicleProperty::Property, SinkList>::iterator itr = propertySinkMap.begin(); itr != propertySinkMap.end(); itr++)
83 VehicleProperty::Property property = (*itr).first;
85 for(SourceList::iterator source = mSources.begin(); source != mSources.end(); source++)
87 PropertyList properties = (*source)->supported();
89 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(property))
91 (*source)->subscribeToPropertyChanges(property);
98 void Core::updateSupported(PropertyList added, PropertyList removed)
101 /// add the newly supported to master list
103 for(PropertyList::iterator itr = added.begin(); itr != added.end(); itr++)
105 if(ListPlusPlus<VehicleProperty::Property>(&added).contains(*itr))
107 mMasterPropertyList.push_back(*itr);
111 /// removed no longer supported properties from master list.
113 for(PropertyList::iterator itr = removed.begin(); itr != removed.end(); itr++)
115 ListPlusPlus<VehicleProperty::Property>(&mMasterPropertyList).removeOne(*itr);
118 /// tell all new sinks about the newly supported properties.
120 for(SinkList::iterator itr = mSinks.begin(); itr != mSinks.end(); itr++)
122 (*itr)->supportedChanged(mMasterPropertyList);
125 /// iterate through subscribed properties and resubscribe. This catches newly supported properties in the process.
127 for(map<VehicleProperty::Property, SinkList>::iterator itr = propertySinkMap.begin(); itr != propertySinkMap.end(); itr++)
129 VehicleProperty::Property property = (*itr).first;
131 for(SourceList::iterator source = mSources.begin(); source != mSources.end(); source++)
133 PropertyList properties = (*source)->supported();
135 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(property))
137 (*source)->subscribeToPropertyChanges(property);
143 void Core::updateProperty(VehicleProperty::Property property, AbstractPropertyType *value)
145 SinkList list = propertySinkMap[property];
147 DebugOut()<<__FUNCTION__<<"() there are "<<list.size()<<" sinks connected to property: "<<property<<endl;
149 propertiesPerSecond++;
151 for(SinkList::iterator itr = list.begin(); itr != list.end(); itr++)
153 (*itr)->propertyChanged(property, value,(*itr)->uuid());
157 void Core::registerSink(AbstractSink *self)
159 if(!ListPlusPlus<AbstractSink*>(&mSinks).contains(self))
161 mSinks.push_back(self);
165 void Core::unregisterSink(AbstractSink *self)
167 if(ListPlusPlus<AbstractSink*>(&mSinks).contains(self))
169 ListPlusPlus<AbstractSink*>(&mSinks).removeOne(self);
174 AsyncPropertyReply *Core::getPropertyAsync(AsyncPropertyRequest request)
176 AsyncPropertyReply * reply = new AsyncPropertyReply(request);
178 for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
180 AbstractSource* src = (*itr);
181 PropertyList properties = src->supported();
182 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(request.property))
184 src->getPropertyAsync(reply);
191 AsyncRangePropertyReply *Core::getRangePropertyAsync(AsyncRangePropertyRequest request)
193 AsyncRangePropertyReply * reply = new AsyncRangePropertyReply(request);
195 for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
197 AbstractSource* src = (*itr);
198 PropertyList properties = src->supported();
199 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(request.property))
201 src->getRangePropertyAsync(reply);
208 AsyncPropertyReply * Core::setProperty(AsyncSetPropertyRequest request)
210 for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
212 AbstractSource* src = (*itr);
213 PropertyList properties = src->supported();
214 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(request.property))
216 return src->setProperty(request);
221 void Core::subscribeToProperty(VehicleProperty::Property property, AbstractSink* self)
223 printf("Subscribing\n");
224 if(!ListPlusPlus<VehicleProperty::Property>(&mMasterPropertyList).contains((property)))
226 DebugOut()<<__FUNCTION__<<"(): property not supported: "<<property<<endl;
230 if(propertySinkMap.find(property) == propertySinkMap.end())
232 propertySinkMap[property] = SinkList();
235 SinkList list = propertySinkMap[property];
237 if(!ListPlusPlus<AbstractSink*>(&list).contains(self))
239 propertySinkMap[property].push_back(self);
242 for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
244 AbstractSource* src = (*itr);
245 PropertyList properties = src->supported();
246 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(property))
248 src->subscribeToPropertyChanges(property);
253 void Core::unsubscribeToProperty(VehicleProperty::Property property, AbstractSink* self)
255 if(propertySinkMap.find(property) == propertySinkMap.end())
257 DebugOut()<<__FUNCTION__<<"property not supported: "<<property;
261 ListPlusPlus<AbstractSink*>(&propertySinkMap[property]).removeOne(self);
263 for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
265 AbstractSource* src = (*itr);
266 PropertyList properties = src->supported();
268 if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(property))
270 src->unsubscribeToPropertyChanges(property);