made mainloop pluggable and added qtmainloop plugin
[profile/ivi/automotive-message-broker.git] / lib / vehicleproperty.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
20 #include "vehicleproperty.h"
21 #include "listplusplus.h"
22 #include "debugout.h"
23 #include "mappropertytype.hpp"
24 #include <map>
25
26
27 #define REGISTERPROPERTY(property, defaultValue) \
28         registerPropertyPriv(property, []() { return new property ## Type(defaultValue); });
29
30 #define REGISTERPROPERTYWITHTYPE(property, type, defaultValue) \
31         registerPropertyPriv(property, []() { return new type(defaultValue); });
32
33 using namespace std;
34
35 std::map<VehicleProperty::Property, VehicleProperty::PropertyTypeFactoryCallback> VehicleProperty::registeredPropertyFactoryMap;
36
37 VehicleProperty* VehicleProperty::thereCanOnlyBeOne = nullptr;
38
39 const VehicleProperty::Property VehicleProperty::NoValue = "NoValue";
40 const VehicleProperty::Property VehicleProperty::VehicleSpeed = "VehicleSpeed";
41 const VehicleProperty::Property VehicleProperty::EngineSpeed = "EngineSpeed";
42 const VehicleProperty::Property VehicleProperty::TransmissionShiftPosition = "TransmissionShiftPosition";
43 const VehicleProperty::Property VehicleProperty::TransmissionGearPosition = "TransmissionGearPostion";
44 const VehicleProperty::Property VehicleProperty::TransmissionMode = "TransmissionMode";
45 const VehicleProperty::Property VehicleProperty::ThrottlePosition = "ThrottlePosition";
46 const VehicleProperty::Property VehicleProperty::WheelBrake = "WheelBrake";
47 const VehicleProperty::Property VehicleProperty::WheelBrakePressure = "WheelBrakePressure";
48 const VehicleProperty::Property VehicleProperty::SteeringWheelAngle = "SteeringWheelAngle";
49 const VehicleProperty::Property VehicleProperty::TurnSignal = "TurnSignal";
50 const VehicleProperty::Property VehicleProperty::ClutchStatus = "ClutchStatus";
51 const VehicleProperty::Property VehicleProperty::EngineOilPressure = "EngineOilPressure";
52 const VehicleProperty::Property VehicleProperty::EngineCoolantTemperature = "EngineCoolantTemperature";
53 const VehicleProperty::Property VehicleProperty::MachineGunTurretStatus = "MachineGunTurretStatus";
54 const VehicleProperty::Property VehicleProperty::AccelerationX = "AccelerationX";
55 const VehicleProperty::Property VehicleProperty::AccelerationY = "AccelerationY";
56 const VehicleProperty::Property VehicleProperty::AccelerationZ = "AccelerationZ";
57 const VehicleProperty::Property VehicleProperty::MassAirFlow = "MassAirFlow";
58 const VehicleProperty::Property VehicleProperty::ButtonEvent = "ButtonEvent";
59 const VehicleProperty::Property VehicleProperty::AirIntakeTemperature = "AirIntakeTemperature";
60 const VehicleProperty::Property VehicleProperty::BatteryVoltage = "BatteryVoltage";
61 const VehicleProperty::Property VehicleProperty::BatteryCurrent = "BatteryCurrent";
62 const VehicleProperty::Property VehicleProperty::InteriorTemperature = "InteriorTemperature";
63 const VehicleProperty::Property VehicleProperty::EngineOilTemperature = "EngineOilTemperature";
64 const VehicleProperty::Property VehicleProperty::VIN = "VIN";
65 const VehicleProperty::Property VehicleProperty::WMI = "WMI";
66 const VehicleProperty::Property VehicleProperty::TirePressureLeftFront = "TirePressureLeftFront";
67 const VehicleProperty::Property VehicleProperty::TirePressureRightFront = "TirePressureRightFront";
68 const VehicleProperty::Property VehicleProperty::TirePressureLeftRear = "TirePressureLeftRear";
69 const VehicleProperty::Property VehicleProperty::TirePressureRightRear = "TirePressureRightRear";
70 const VehicleProperty::Property VehicleProperty::TireTemperatureLeftFront = "TireTemperatureLeftFront";
71 const VehicleProperty::Property VehicleProperty::TireTemperatureRightFront = "TireTemperatureRightFront";
72 const VehicleProperty::Property VehicleProperty::TireTemperatureLeftRear = "TireTemperatureLeftRear";
73 const VehicleProperty::Property VehicleProperty::TireTemperatureRightRear = "TireTemperatureRightRear";
74 const VehicleProperty::Property VehicleProperty::VehiclePowerMode = "VehiclePowerMode";
75 const VehicleProperty::Property VehicleProperty::TripMeterA = "TripMeterA";
76 const VehicleProperty::Property VehicleProperty::TripMeterB = "TripMeterB";
77 const VehicleProperty::Property VehicleProperty::TripMeterC = "TripMeterC";
78 const VehicleProperty::Property VehicleProperty::CruiseControlActive = "CruiseControlActive";
79 const VehicleProperty::Property VehicleProperty::CruiseControlSpeed = "CruiseControlSpeed";
80 const VehicleProperty::Property VehicleProperty::LightHead = "LightHead";
81 const VehicleProperty::Property VehicleProperty::LightLeftTurn = "LightLeftTurn";
82 const VehicleProperty::Property VehicleProperty::LightRightTurn = "LightRightTurn";
83 const VehicleProperty::Property VehicleProperty::LightParking = "LightParking";
84 const VehicleProperty::Property VehicleProperty::LightFog = "LightFog";
85 const VehicleProperty::Property VehicleProperty::LightHazard= "LightHazard";
86 const VehicleProperty::Property VehicleProperty::LightHighBeam = "LightHighBeam";
87 const VehicleProperty::Property VehicleProperty::LightBrake= "LightBrake";
88 const VehicleProperty::Property VehicleProperty::InteriorLightDriver = "InteriorLightDriver";
89 const VehicleProperty::Property VehicleProperty::InteriorLightCenter = "InteriorLightCenter";
90 const VehicleProperty::Property VehicleProperty::InteriorLightPassenger = "InteriorLightPassenger";
91 const VehicleProperty::Property VehicleProperty::EngineLoad = "EngineLoad";
92 const VehicleProperty::Property VehicleProperty::Horn = "Horn";
93 const VehicleProperty::Property VehicleProperty::FuelLevel = "FuelLevel";
94 const VehicleProperty::Property VehicleProperty::FuelConsumption = "FuelConsumption";
95 const VehicleProperty::Property VehicleProperty::FuelRange = "FuelRange";
96 const VehicleProperty::Property VehicleProperty::FuelEconomy = "FuelEconomy";
97 const VehicleProperty::Property VehicleProperty::FuelAverageEconomy = "FuelAverageEconomy";
98 const VehicleProperty::Property VehicleProperty::FuelType = "FuelType";
99 const VehicleProperty::Property VehicleProperty::FuelPositionSide = "FuelPositionSide";
100 const VehicleProperty::Property VehicleProperty::EngineOilRemaining = "EngineOilRemaining";
101 const VehicleProperty::Property VehicleProperty::ExteriorBrightness = "ExteriorBrightness";
102 const VehicleProperty::Property VehicleProperty::Latitude = "Latitude";
103 const VehicleProperty::Property VehicleProperty::Longitude = "Longitude";
104 const VehicleProperty::Property VehicleProperty::Direction = "Direction";
105 const VehicleProperty::Property VehicleProperty::Altitude = "Altitude";
106 const VehicleProperty::Property VehicleProperty::VehicleWidth = "VehicleWidth";
107 const VehicleProperty::Property VehicleProperty::VehicleHeight = "VehicleHeight";
108 const VehicleProperty::Property VehicleProperty::VehicleLength = "VehicleLength";
109 const VehicleProperty::Property VehicleProperty::VehicleType = "VehicleType";
110 const VehicleProperty::Property VehicleProperty::DoorsPerRow = "DoorsPerRow";
111 const VehicleProperty::Property VehicleProperty::TransmissionGearType = "TransmissionGearType";
112 const VehicleProperty::Property VehicleProperty::FrontWheelRadius = "FrontWheelRadius";
113 const VehicleProperty::Property VehicleProperty::RearWheelRadius = "RearWheelRadius";
114 const VehicleProperty::Property VehicleProperty::WheelTrack = "WheelTrack";
115 const VehicleProperty::Property VehicleProperty::Odometer = "Odometer";
116 const VehicleProperty::Property VehicleProperty::TransmissionFluidLevel = "TransmissionFluidLevel";
117 const VehicleProperty::Property VehicleProperty::BrakeFluidLevel = "BrakeFluidLevel";
118 const VehicleProperty::Property VehicleProperty::WasherFluidLevel = "WasherFluidLevel";
119 const VehicleProperty::Property VehicleProperty::SecurityAlertStatus = "SecurityAlertStatus";
120 const VehicleProperty::Property VehicleProperty::ParkingBrakeStatus = "ParkingBrakeStatus";
121 const VehicleProperty::Property VehicleProperty::ParkingLightStatus = "ParkingLightStatus";
122 const VehicleProperty::Property VehicleProperty::HazardLightStatus = "HazardLightStatus";
123 const VehicleProperty::Property VehicleProperty::AirbagStatus = "AirbagStatus";
124 const VehicleProperty::Property VehicleProperty::AntilockBrakingSystem = "AntilockBrakingSystem";
125 const VehicleProperty::Property VehicleProperty::TractionControlSystem = "TractionControlSystem";
126 const VehicleProperty::Property VehicleProperty::VehicleTopSpeedLimit = "VehicleTopSpeedLimit";
127 const VehicleProperty::Property VehicleProperty::DoorStatus = "DoorStatus";
128 const VehicleProperty::Property VehicleProperty::DoorLockStatus = "DoorLockStatus";
129 const VehicleProperty::Property VehicleProperty::SeatBeltStatus = "SeatBeltStatus";
130 const VehicleProperty::Property VehicleProperty::WindowLockStatus = "WindowLockStatus";
131 const VehicleProperty::Property VehicleProperty::OccupantStatus = "OccupantStatus";
132 const VehicleProperty::Property VehicleProperty::ObstacleDistance = "ObstacleDistance";
133
134
135 std::list<VehicleProperty::Property> VehicleProperty::mCapabilities;
136 std::list<VehicleProperty::Property> VehicleProperty::mCustomProperties;
137
138 VehicleProperty::VehicleProperty()
139 {
140         registerPropertyPriv( VehicleSpeed, [](){ return new VehicleSpeedType(0); });
141         registerPropertyPriv(EngineSpeed, [](){ return new EngineSpeedType(0); });
142         registerPropertyPriv(TransmissionShiftPosition, [](){ return new TransmissionShiftPositionType(Transmission::Neutral); });
143         registerPropertyPriv(TransmissionGearPosition, [](){ return new TransmissionGearPositionType(Transmission::Neutral); });
144         REGISTERPROPERTY(TransmissionMode,Transmission::Normal);
145         registerPropertyPriv(ThrottlePosition, [](){ return new ThrottlePositionType(0); });
146         registerPropertyPriv(WheelBrake, [](){ return new WheelBrakeType(false); });
147         REGISTERPROPERTY(WheelBrakePressure,0);
148         registerPropertyPriv(SteeringWheelAngle, [](){ return new SteeringWheelAngleType(0); });
149         registerPropertyPriv(TurnSignal, [](){ return new TurnSignalType(TurnSignals::Off); });
150         registerPropertyPriv(ClutchStatus, [](){ return new ClutchStatusType(false); });
151         registerPropertyPriv(EngineOilPressure, [](){ return new EngineOilPressureType(0); });
152         registerPropertyPriv(EngineOilTemperature, [](){ return new EngineOilTemperatureType(0); });
153         REGISTERPROPERTY(EngineOilRemaining,0);
154         registerPropertyPriv(EngineCoolantTemperature, [](){ return new EngineCoolantTemperatureType(0); });
155         registerPropertyPriv(MachineGunTurretStatus, [](){ return new MachineGunTurretStatusType(false); });
156         registerPropertyPriv(AccelerationX, [](){ return new AccelerationType(0); });
157         registerPropertyPriv(AccelerationY, [](){ return new AccelerationType(0); });
158         registerPropertyPriv(AccelerationZ, [](){ return new AccelerationType(0); });
159         registerPropertyPriv(MassAirFlow, [](){ return new MassAirFlowType(0); });
160         registerPropertyPriv(ButtonEvent, [](){ return new ButtonEventType(ButtonEvents::NoButton); });
161         REGISTERPROPERTY(AirIntakeTemperature,0)
162         registerPropertyPriv(BatteryVoltage, [](){ return new BatteryVoltageType(0); });
163         REGISTERPROPERTY(BatteryCurrent,0);
164         registerPropertyPriv(InteriorTemperature, [](){ return new InteriorTemperatureType(0); });
165         registerPropertyPriv(VIN, [](){ return new VINType(""); });
166         registerPropertyPriv(WMI, [](){ return new WMIType(""); });
167         REGISTERPROPERTYWITHTYPE(TirePressureLeftFront, TirePressureType, 0);
168         REGISTERPROPERTYWITHTYPE(TirePressureRightFront, TirePressureType, 0);
169         REGISTERPROPERTYWITHTYPE(TirePressureLeftRear, TirePressureType, 0);
170         REGISTERPROPERTYWITHTYPE(TirePressureRightRear, TirePressureType, 0);
171         REGISTERPROPERTYWITHTYPE(TireTemperatureLeftFront,TireTemperatureType,0);
172         REGISTERPROPERTYWITHTYPE(TireTemperatureRightFront,TireTemperatureType,0);
173         REGISTERPROPERTYWITHTYPE(TireTemperatureLeftRear,TireTemperatureType,0);
174         REGISTERPROPERTYWITHTYPE(TireTemperatureRightRear,TireTemperatureType,0);
175         registerPropertyPriv( VehiclePowerMode,[](){ return new VehiclePowerModeType(Power::Off); } );
176         REGISTERPROPERTYWITHTYPE(TripMeterA,TripMeterType,0);
177         REGISTERPROPERTYWITHTYPE(TripMeterB,TripMeterType,0);
178         REGISTERPROPERTYWITHTYPE(TripMeterC,TripMeterType,0);
179         REGISTERPROPERTY(CruiseControlActive, false);
180         REGISTERPROPERTY(CruiseControlSpeed,0);
181         REGISTERPROPERTYWITHTYPE(LightHead, LightStatusType, false);
182         REGISTERPROPERTYWITHTYPE(LightLeftTurn,LightStatusType, false);
183         REGISTERPROPERTYWITHTYPE(LightRightTurn, LightStatusType, false);
184         REGISTERPROPERTYWITHTYPE(LightParking, LightStatusType, false);
185         REGISTERPROPERTYWITHTYPE(LightFog, LightStatusType, false);
186         REGISTERPROPERTYWITHTYPE(LightHazard, LightStatusType, false);
187         REGISTERPROPERTYWITHTYPE(LightHighBeam, LightStatusType, false);
188         REGISTERPROPERTYWITHTYPE(LightBrake, LightStatusType, false);
189         REGISTERPROPERTYWITHTYPE(InteriorLightDriver, LightStatusType, false);
190         REGISTERPROPERTYWITHTYPE(InteriorLightPassenger, LightStatusType, false);
191         REGISTERPROPERTYWITHTYPE(InteriorLightCenter, LightStatusType, false);
192         REGISTERPROPERTY(EngineLoad,0);
193         REGISTERPROPERTY(Horn, false);
194         REGISTERPROPERTY(FuelLevel, 0);
195         REGISTERPROPERTY(FuelRange, 0);
196         REGISTERPROPERTY(FuelConsumption, 0);
197         REGISTERPROPERTY(FuelEconomy, 0);
198         REGISTERPROPERTY(FuelAverageEconomy, 0);
199         REGISTERPROPERTY(FuelType, Fuel::Unknown);
200         REGISTERPROPERTY(FuelPositionSide, Fuel::UnknownPosition);
201         REGISTERPROPERTY(ExteriorBrightness, 0);
202         REGISTERPROPERTYWITHTYPE(VehicleWidth,VehicleSizeType,0);
203         REGISTERPROPERTYWITHTYPE(VehicleHeight,VehicleSizeType,0);
204         REGISTERPROPERTYWITHTYPE(VehicleLength,VehicleSizeType,0);
205         REGISTERPROPERTY(Latitude,0);
206         REGISTERPROPERTY(Longitude,0);
207         REGISTERPROPERTY(Altitude,0);
208         REGISTERPROPERTY(Direction,0);
209         REGISTERPROPERTY(VehicleType,Vehicle::Unknown);
210         registerPropertyPriv(DoorsPerRow,[]() { BasicPropertyType<uint16_t> d(0); return new DoorsPerRowType(&d); });
211         REGISTERPROPERTY(TransmissionGearType,Transmission::Unknown);
212         REGISTERPROPERTYWITHTYPE(FrontWheelRadius, WheelRadiusType, 0);
213         REGISTERPROPERTYWITHTYPE(RearWheelRadius, WheelRadiusType, 0);
214         REGISTERPROPERTY(WheelTrack,0);
215         REGISTERPROPERTY(Odometer,0);
216         REGISTERPROPERTY(TransmissionFluidLevel,0);
217         REGISTERPROPERTY(BrakeFluidLevel,0);
218         REGISTERPROPERTY(WasherFluidLevel,0);
219         REGISTERPROPERTY(SecurityAlertStatus,Security::Idle);
220         REGISTERPROPERTY(ParkingBrakeStatus,false);
221         REGISTERPROPERTY(ParkingLightStatus,false);
222         REGISTERPROPERTY(HazardLightStatus,false);
223         registerPropertyPriv(AirbagStatus,[]()
224         {
225                 BasicPropertyType<Airbag::Location> a(Airbag::Driver);
226                 BasicPropertyType<Airbag::Status> b(Airbag::Inactive);
227                 AirbagStatusType* t = new AirbagStatusType();
228                 t->append(a,b);
229
230                 return t;
231         });
232
233         REGISTERPROPERTY(AntilockBrakingSystem,false);
234         REGISTERPROPERTY(TractionControlSystem,false);
235         REGISTERPROPERTY(VehicleTopSpeedLimit,0);
236
237         registerPropertyPriv(DoorStatus,[]()
238         {
239                 DoorStatusType* t = new DoorStatusType();
240                 t->append(Door::Driver,Door::Closed);
241
242                 return t;
243         });
244
245         registerPropertyPriv(DoorLockStatus,[]()
246         {
247                 DoorLockStatusType* t = new DoorLockStatusType();
248                 t->append(Door::Driver,false);
249
250                 return t;
251         });
252
253         registerPropertyPriv(SeatBeltStatus,[]()
254         {
255                 SeatBeltStatusType* t = new SeatBeltStatusType();
256                 t->append(Seat::Driver,false);
257
258                 return t;
259         });
260
261         registerPropertyPriv(SeatBeltStatus,[]()
262         {
263                 SeatBeltStatusType* t = new SeatBeltStatusType();
264                 t->append(Seat::Driver,false);
265
266                 return t;
267         });
268
269         registerPropertyPriv(OccupantStatus,[]()
270         {
271                 OccupantStatusType* t = new OccupantStatusType();
272                 t->append(Seat::Driver,false);
273
274                 return t;
275         });
276
277         registerPropertyPriv(WindowLockStatus,[]()
278         {
279                 WindowLockStatusType* t = new WindowLockStatusType();
280                 t->append(Window::Driver,false);
281
282                 return t;
283         });
284
285 }
286
287 void VehicleProperty::factory()
288 {
289         if(!thereCanOnlyBeOne)
290                 thereCanOnlyBeOne = new VehicleProperty();
291 }
292
293 std::list<VehicleProperty::Property> VehicleProperty::capabilities()
294 {
295         return mCapabilities;
296 }
297
298 std::list<VehicleProperty::Property> VehicleProperty::customProperties()
299 {
300         return mCustomProperties;
301 }
302
303 AbstractPropertyType* VehicleProperty::getPropertyTypeForPropertyNameValue(VehicleProperty::Property name, std::string value)
304 {
305         if(registeredPropertyFactoryMap.count(name) > 0)
306         {
307                 VehicleProperty::PropertyTypeFactoryCallback cb = registeredPropertyFactoryMap[name];
308                 if ( cb != NULL )
309                 {
310                         AbstractPropertyType* type = cb();
311                         if(type == NULL)
312                                 throw std::runtime_error("Cannot return NULL in a PropertyTypeFactory");
313
314                         if(value != "" )
315                                 type->fromString(value);
316
317                         return type;
318                 }
319         }
320
321         return nullptr;
322 }
323
324 bool VehicleProperty::registerProperty(VehicleProperty::Property name, VehicleProperty::PropertyTypeFactoryCallback factory)
325 {
326
327         mCustomProperties.push_back(name);
328
329         return registerPropertyPriv(name, factory);
330 }
331
332 bool VehicleProperty::registerPropertyPriv(VehicleProperty::Property name, VehicleProperty::PropertyTypeFactoryCallback factory)
333 {
334         if(ListPlusPlus<Property>(&mCapabilities).contains(name))
335         {
336                 DebugOut(0)<<__FUNCTION__<<" ERROR: property '"<<name<<"'' already registered."<<endl;
337                 return false;
338         }
339
340         registeredPropertyFactoryMap[name] = factory;
341         mCapabilities.push_back(name);
342
343         return true;
344
345 }
346
347