fixed frequency option in dbus plugin
[profile/ivi/automotive-message-broker.git] / plugins / dbus / dbusinterfacemanager.cpp
1 /*
2 Copyright (C) 2012 Intel Corporation
3
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.
8
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.
13
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
17 */
18
19 #include "dbusinterfacemanager.h"
20
21
22 #include <gio/gio.h>
23 #include <string>
24
25 #include "listplusplus.h"
26 #include "automotivemanager.h"
27
28 ///properties:
29 #include "runningstatus.h"
30 #include "custompropertyinterface.h"
31 #include "uncategorizedproperty.h"
32 #include "environmentproperties.h"
33 #include "vehicleinfo.h"
34 #include "maintenance.h"
35 #include "parking.h"
36 #include "drivingsafety.h"
37 #include "personalization.h"
38
39 std::map<std::string, std::map<Zone::Type, bool> > getUniqueSourcesList(AbstractRoutingEngine *re, PropertyList implementedProperties)
40 {
41         std::map<std::string, std::map<Zone::Type, bool>> uniqueSourcesList;
42
43         for(auto itr = implementedProperties.begin(); itr != implementedProperties.end(); itr++)
44         {
45                 VehicleProperty::Property property = *itr;
46                 std::list<std::string> sources = re->sourcesForProperty(property);
47
48                 for(auto itr2 = sources.begin(); itr2 != sources.end(); itr2++)
49                 {
50                         std::string source = *itr2;
51
52                         PropertyInfo info = re->getPropertyInfo(property,source);
53
54                         std::map<Zone::Type, bool> uniqueZoneList;
55
56                         if(uniqueSourcesList.find(source) != uniqueSourcesList.end())
57                         {
58                                 uniqueZoneList = uniqueSourcesList[source];
59                         }
60
61                         std::list<Zone::Type> zoneList = info.zones();
62
63                         if(!zoneList.size())
64                         {
65                                 uniqueZoneList[Zone::None] = true;
66                         }
67
68                         for(auto zoneItr = zoneList.begin(); zoneItr != zoneList.end(); zoneItr++)
69                         {
70                                 uniqueZoneList[*zoneItr] = true;
71                         }
72
73                         uniqueSourcesList[source] = uniqueZoneList;
74                 }
75         }
76
77         return uniqueSourcesList;
78 }
79
80 template <typename T>
81 void exportProperty(AbstractRoutingEngine *re, GDBusConnection *connection)
82 {
83         T* t = new T(re, connection);
84
85         /// check if we need more than one instance:
86
87         PropertyList implementedProperties = t->wantsProperties();
88
89         std::map<std::string, std::map<Zone::Type, bool> > uniqueSourcesList = getUniqueSourcesList(re, implementedProperties);
90
91         delete t;
92
93         for(auto itr = uniqueSourcesList.begin(); itr != uniqueSourcesList.end(); itr++)
94         {
95                 std::map<Zone::Type, bool> zones = (*itr).second;
96
97                 std::string source = (*itr).first;
98
99                 std::string objectPath = "/" + source;
100
101                 boost::algorithm::erase_all(objectPath, "-");
102
103                 for(auto zoneItr = zones.begin(); zoneItr != zones.end(); zoneItr++)
104                 {
105                         Zone::Type zone = (*zoneItr).first;
106                         T* t = new T(re, connection);
107                         std::stringstream fullobjectPath;
108                         fullobjectPath<< objectPath << "/" << zone << "/" <<t->objectName();
109                         t->setObjectPath(fullobjectPath.str());
110                         t->setSourceFilter(source);
111                         t->setZoneFilter(zone);
112                         t->supportedChanged(re->supported());
113                 }
114
115         }
116 }
117
118 template <typename T>
119 void exportProperty(VehicleProperty::Property prop, AbstractRoutingEngine *re, GDBusConnection *connection)
120 {
121         T* t = new T(prop, re, connection);
122
123         /// check if we need more than one instance:
124
125         PropertyList implementedProperties = t->wantsProperties();
126
127         std::map<std::string, std::map<Zone::Type, bool> > uniqueSourcesList = getUniqueSourcesList(re, implementedProperties);
128
129         delete t;
130
131         for(auto itr = uniqueSourcesList.begin(); itr != uniqueSourcesList.end(); itr++)
132         {
133                 std::map<Zone::Type, bool> zones = (*itr).second;
134
135                 std::string source = (*itr).first;
136
137                 std::string objectPath = "/" + source;
138
139                 boost::algorithm::erase_all(objectPath, "-");
140
141                 for(auto zoneItr = zones.begin(); zoneItr != zones.end(); zoneItr++)
142                 {
143                         Zone::Type zone = (*zoneItr).first;
144                         T* t = new T(prop, re, connection);
145                         std::stringstream fullobjectPath;
146                         fullobjectPath<< objectPath << "/" << zone << "/" <<t->objectName();
147                         t->setObjectPath(fullobjectPath.str());
148                         t->setSourceFilter(source);
149                         t->setZoneFilter(zone);
150                         t->supportedChanged(re->supported());
151                 }
152
153         }
154 }
155
156 static void
157 on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
158 {
159         DBusInterfaceManager* iface = static_cast<DBusInterfaceManager*>(user_data);
160
161         iface->connection = connection;
162
163         new AutomotiveManager(connection);
164
165         /// properties:
166         exportProperty<AccelerationProperty>(iface->re,connection);
167         exportProperty<VehicleSpeedProperty>(iface->re, connection);
168         exportProperty<TireProperty>(iface->re, connection);
169         exportProperty<EngineSpeedProperty>(iface->re, connection);
170         exportProperty<VehiclePowerModeProperty>(iface->re, connection);
171         exportProperty<TripMeterProperty>(iface->re, connection);
172         exportProperty<TransmissionProperty>(iface->re, connection);
173         exportProperty<CruiseControlProperty>(iface->re, connection);
174         exportProperty<WheelBrakeProperty>(iface->re, connection);
175         exportProperty<LightStatusProperty>(iface->re, connection);
176         exportProperty<HornProperty>(iface->re, connection);
177         exportProperty<FuelProperty>(iface->re, connection);
178         exportProperty<EngineOilProperty>(iface->re, connection);
179         exportProperty<ExteriorBrightnessProperty>(iface->re, connection);
180         exportProperty<Temperature>(iface->re, connection);
181         exportProperty<RainSensor>(iface->re, connection);
182         exportProperty<WindshieldWiper>(iface->re, connection);
183         exportProperty<HVACProperty>(iface->re, connection);
184         exportProperty<WindowStatusProperty>(iface->re, connection);
185         exportProperty<Sunroof>(iface->re, connection);
186         exportProperty<ConvertibleRoof>(iface->re, connection);
187         exportProperty<VehicleId>(iface->re, connection);
188         exportProperty<VehicleTypeProperty>(iface->re, connection);
189         exportProperty<FuelInfoProperty>(iface->re, connection);
190         exportProperty<SizeProperty>(iface->re, connection);
191         exportProperty<DoorsProperty>(iface->re, connection);
192         exportProperty<WheelInformationProperty>(iface->re, connection);
193         exportProperty<OdometerProperty>(iface->re, connection);
194         exportProperty<FluidProperty>(iface->re, connection);
195         exportProperty<BatteryProperty>(iface->re, connection);
196         exportProperty<SecurityAlertProperty>(iface->re, connection);
197         exportProperty<ParkingBrakeProperty>(iface->re, connection);
198         exportProperty<ParkingLightProperty>(iface->re, connection);
199         exportProperty<HazardLightProperty>(iface->re, connection);
200         exportProperty<LocationProperty>(iface->re, connection);
201         exportProperty<AntilockBrakingSystemProperty>(iface->re, connection);
202         exportProperty<TractionControlSystemProperty>(iface->re, connection);
203         exportProperty<VehicleTopSpeedLimitProperty>(iface->re, connection);
204         exportProperty<AirbagStatusProperty>(iface->re, connection);
205         exportProperty<DoorStatusProperty>(iface->re, connection);
206         exportProperty<SeatBeltStatusProperty>(iface->re, connection);
207         exportProperty<OccupantStatusProperty>(iface->re, connection);
208         exportProperty<ObstacleDistanceProperty>(iface->re, connection);
209         exportProperty<SeatPostionProperty>(iface->re, connection);
210         exportProperty<SteeringWheelPositionProperty>(iface->re, connection);
211         exportProperty<MirrorSettingProperty>(iface->re, connection);
212
213         iface->registerCustomTypes();
214 }
215
216 static void
217 on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
218 {
219
220 }
221
222 static void
223 on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data)
224 {
225
226         DebugOut(DebugOut::Error)<<"DBus: Lost bus name"<<endl;
227
228         if(!connection){
229                 DebugOut(DebugOut::Error)<<"DBus: Connection could not be established."<<endl;
230                 throw std::runtime_error("Could not establish DBus connection.");
231         }
232 }
233
234
235
236 DBusInterfaceManager::DBusInterfaceManager(AbstractRoutingEngine* engine,std::map<std::string,std::string> config)
237         :AbstractSink(engine,config),re(engine), connection(nullptr)
238 {
239         DBusSink::dbusConfig = config;
240         ownerId = g_bus_own_name(G_BUS_TYPE_SYSTEM,
241                                         DBusServiceName,
242                                         G_BUS_NAME_OWNER_FLAGS_NONE,
243                                         on_bus_acquired,
244                                         on_name_acquired,
245                                         on_name_lost,
246                                         this,
247                                         NULL);
248
249 }
250
251 DBusInterfaceManager::~DBusInterfaceManager()
252 {
253         g_bus_unown_name(ownerId);
254 }
255
256 void DBusInterfaceManager::supportedChanged(const PropertyList &supportedProperties)
257 {
258         DebugOut()<<"supported Properties: "<<supportedProperties.size()<<endl;
259         if(!connection)
260         {
261                 return;
262         }
263
264         registerCustomTypes();
265 }
266
267 void DBusInterfaceManager::registerCustomTypes()
268 {
269         PropertyList list = VehicleProperty::customProperties();
270         PropertyList implemented = AbstractDBusInterface::implementedProperties();
271
272         for (auto itr = list.begin(); itr != list.end(); itr++)
273         {
274                 VehicleProperty::Property prop = *itr;
275
276                 if(!contains(implemented, prop))
277                 {
278                         exportProperty<CustomPropertyInterface>(prop, re, connection);
279                 }
280         }
281
282         /// Create objects for unimplemented properties:
283
284         implemented = AbstractDBusInterface::implementedProperties();
285
286         PropertyList capabilitiesList = VehicleProperty::capabilities();
287
288         for (auto itr = capabilitiesList.begin(); itr != capabilitiesList.end(); itr++)
289         {
290                 VehicleProperty::Property prop = *itr;
291
292                 if(!contains(implemented, prop))
293                 {
294                         exportProperty<UncategorizedPropertyInterface>(prop, re, connection);
295                 }
296         }
297 }
298
299