[dbus] add overload 'value' for custom dbus property types
[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 #include <unordered_set>
28
29 #include <dbusexport.h>
30 #include <ambplugin.h>
31
32 ///properties:
33 #include "runningstatus.h"
34 #include "custompropertyinterface.h"
35 #include "uncategorizedproperty.h"
36 #include "environmentproperties.h"
37 #include "vehicleinfo.h"
38 #include "maintenance.h"
39 #include "parking.h"
40 #include "drivingsafety.h"
41 #include "personalization.h"
42
43 extern "C" void create(AbstractRoutingEngine* routingengine, map<string, string> config)
44 {
45         auto plugin = new AmbPlugin<DBusInterfaceManager>(routingengine, config);
46         plugin->init();
47 }
48
49 static void
50 on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
51 {
52         DBusInterfaceManager* iface = static_cast<DBusInterfaceManager*>(user_data);
53
54         iface->connection = std::shared_ptr<GDBusConnection>(connection, [=](GDBusConnection* conn){
55                 amb::traits<GDBusConnection>::delete_functor functor;
56                 functor(conn);
57         });
58
59         iface->setValue(iface->dbusConnected, true);
60
61         new AutomotiveManager(connection);
62
63         iface->registerTypes();
64 }
65
66 static void
67 on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
68 {
69
70 }
71
72 static void
73 on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data)
74 {
75         DebugOut(DebugOut::Error)<<"DBus: Lost bus name"<<endl;
76
77         DBusInterfaceManager* iface = static_cast<DBusInterfaceManager*>(user_data);
78
79         iface->setValue(iface->dbusConnected, false);
80
81         if(!connection){
82                 DebugOut(DebugOut::Error)<<"DBus: Connection could not be established."<<endl;
83                 throw std::runtime_error("Could not establish DBus connection.");
84         }
85 }
86
87
88
89 DBusInterfaceManager::DBusInterfaceManager(AbstractRoutingEngine * engine, std::map<std::string,std::string> config, AbstractSource & parent)
90         :AmbPluginImpl(engine, config, parent), connection(nullptr)
91 {
92         DBusSink::dbusConfig = config;
93         dbusConnected = addPropertySupport(Zone::None, []() { return new BasicPropertyType<bool>(DBusConnected, false); });
94
95         ownerId = g_bus_own_name(G_BUS_TYPE_SYSTEM,
96                                                          DBusServiceName,
97                                                          G_BUS_NAME_OWNER_FLAGS_NONE,
98                                                          on_bus_acquired,
99                                                          on_name_acquired,
100                                                          on_name_lost,
101                                                          this,
102                                                          nullptr);
103
104 }
105
106 DBusInterfaceManager::~DBusInterfaceManager()
107 {
108         g_bus_unown_name(ownerId);
109 }
110
111 void DBusInterfaceManager::supportedChanged(const PropertyList &supportedProperties)
112 {
113         DebugOut()<<"supported Properties: "<<supportedProperties.size()<<endl;
114         if(!connection)
115         {
116                 return;
117         }
118
119         registerTypes();
120 }
121
122 void DBusInterfaceManager::registerTypes()
123 {
124         /// properties:
125         auto re = routingEngine;
126
127         auto exporter = amb::Exporter::instance();
128
129         exporter->connection = connection;
130
131         exporter->exportProperty<AccelerationProperty>(re);
132         exporter->exportProperty<VehicleSpeedProperty>(re);
133         exporter->exportProperty<TireProperty>(re);
134         exporter->exportProperty<EngineSpeedProperty>(re);
135         exporter->exportProperty<VehiclePowerModeProperty>(re);
136         exporter->exportProperty<TripMeterProperty>(re);
137         exporter->exportProperty<TransmissionProperty>(re);
138         exporter->exportProperty<CruiseControlProperty>(re);
139         exporter->exportProperty<BrakeOperation>(re);
140         exporter->exportProperty<LightStatusProperty>(re);
141         exporter->exportProperty<HornProperty>(re);
142         exporter->exportProperty<FuelProperty>(re);
143         exporter->exportProperty<EngineOilProperty>(re);
144         exporter->exportProperty<ExteriorBrightnessProperty>(re);
145         exporter->exportProperty<Temperature>(re);
146         exporter->exportProperty<RainSensor>(re);
147         exporter->exportProperty<ClimateControlProperty>(re);
148         exporter->exportProperty<WindowStatusProperty>(re);
149         exporter->exportProperty<DefrostProperty>(re);
150         exporter->exportProperty<Sunroof>(re);
151         exporter->exportProperty<ConvertibleRoof>(re);
152         exporter->exportProperty<VehicleId>(re);
153         exporter->exportProperty<VehicleTypeProperty>(re);
154         exporter->exportProperty<FuelInfoProperty>(re);
155         exporter->exportProperty<SizeProperty>(re);
156         exporter->exportProperty<DoorsProperty>(re);
157         exporter->exportProperty<WheelInformationProperty>(re);
158         exporter->exportProperty<OdometerProperty>(re);
159         exporter->exportProperty<FluidProperty>(re);
160         exporter->exportProperty<BatteryStatusProperty>(re);
161         exporter->exportProperty<ParkingBrakeProperty>(re);
162         exporter->exportProperty<HazardLightProperty>(re);
163         exporter->exportProperty<LocationProperty>(re);
164         exporter->exportProperty<AntilockBrakingSystemProperty>(re);
165         exporter->exportProperty<TractionControlSystemProperty>(re);
166         exporter->exportProperty<VehicleTopSpeedLimitProperty>(re);
167         exporter->exportProperty<AirbagStatusProperty>(re);
168         exporter->exportProperty<DoorStatusProperty>(re);
169         exporter->exportProperty<SeatBeltStatusProperty>(re);
170         exporter->exportProperty<OccupantStatusProperty>(re);
171         exporter->exportProperty<ObstacleDistanceProperty>(re);
172         exporter->exportProperty<SteeringWheelPositionProperty>(re);
173         exporter->exportProperty<SteeringWheel>(re);
174         exporter->exportProperty<ThrottlePosition>(re);
175         exporter->exportProperty<EngineCoolant>(re);
176         exporter->exportProperty<NightMode>(re);
177         exporter->exportProperty<DrivingMode>(re);
178         exporter->exportProperty<PowertrainTorque>(re);
179         exporter->exportProperty<AcceleratorPedalPosition>(re);
180         exporter->exportProperty<Chime>(re);
181         exporter->exportProperty<WheelTick>(re);
182         exporter->exportProperty<IgnitionTime>(re);
183         exporter->exportProperty<YawRate>(re);
184         exporter->exportProperty<TransmissionClutch>(re);
185         exporter->exportProperty<TransmissionOil>(re);
186         exporter->exportProperty<BrakeMaintenance>(re);
187         exporter->exportProperty<WasherFluid>(re);
188         exporter->exportProperty<MalfunctionIndicator>(re);
189         exporter->exportProperty<Diagnostics>(re);
190         exporter->exportProperty<MirrorProperty>(re);
191         exporter->exportProperty<SeatAdjustment>(re);
192         exporter->exportProperty<DriveMode>(re);
193         exporter->exportProperty<VehicleSound>(re);
194         exporter->exportProperty<ElectronicStabilityControl>(re);
195         exporter->exportProperty<ChildSafetyLock>(re);
196         exporter->exportProperty<SeatProperty>(re);
197         exporter->exportProperty<DoorProperty>(re);
198         exporter->exportProperty<WindshieldWiperStatus>(re);
199         exporter->exportProperty<SideWindowStatusProperty>(re);
200         exporter->exportProperty<AtmosphericPressure>(re);
201         exporter->exportProperty<LaneDepartureStatus>(re);
202         exporter->exportProperty<AlarmStatus>(re);
203
204         PropertyList list = VehicleProperty::customProperties();
205         PropertyList implemented = AbstractDBusInterface::implementedProperties();
206
207         for (auto prop : list)
208         {
209                 if(!contains(implemented, prop))
210                 {
211                         exporter->exportProperty<CustomPropertyInterface>(prop, re);
212                 }
213         }
214
215         /// Create objects for unimplemented properties:
216
217         implemented = AbstractDBusInterface::implementedProperties();
218
219         PropertyList capabilitiesList = VehicleProperty::capabilities();
220
221         for (auto itr = capabilitiesList.begin(); itr != capabilitiesList.end(); itr++)
222         {
223                 VehicleProperty::Property prop = *itr;
224
225                 if(!contains(implemented, prop))
226                 {
227                         exporter->exportProperty<UncategorizedPropertyInterface>(prop, re);
228                 }
229         }
230 }