only export dbus properties that are supported
[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 <map>
22
23 #define REGISTERPROPERTY(property, defaultValue) \
24         registerProperty(property, []() { return new property ## Type(defaultValue); });
25
26 #define REGISTERPROPERTYWITHTYPE(property, type, defaultValue) \
27         registerProperty(property, []() { return new type(defaultValue); });
28
29 using namespace std;
30
31 std::map<VehicleProperty::Property, VehicleProperty::PropertyTypeFactoryCallback> VehicleProperty::registeredPropertyFactoryMap;
32
33 const VehicleProperty::Property VehicleProperty::NoValue = "NoValue";
34 const VehicleProperty::Property VehicleProperty::VehicleSpeed = "VehicleSpeed";
35 const VehicleProperty::Property VehicleProperty::EngineSpeed = "EngineSpeed";
36 const VehicleProperty::Property VehicleProperty::TransmissionShiftPosition = "TransmissionShiftPosition";
37 const VehicleProperty::Property VehicleProperty::TransmissionGearPosition = "TransmissionGearPostion";
38 const VehicleProperty::Property VehicleProperty::TransmissionMode = "TransmissionMode";
39 const VehicleProperty::Property VehicleProperty::ThrottlePosition = "ThrottlePosition";
40 const VehicleProperty::Property VehicleProperty::WheelBrake = "WheelBrake";
41 const VehicleProperty::Property VehicleProperty::SteeringWheelAngle = "SteeringWheelAngle";
42 const VehicleProperty::Property VehicleProperty::TurnSignal = "TurnSignal";
43 const VehicleProperty::Property VehicleProperty::ClutchStatus = "ClutchStatus";
44 const VehicleProperty::Property VehicleProperty::EngineOilPressure = "EngineOilPressure";
45 const VehicleProperty::Property VehicleProperty::EngineCoolantTemperature = "EngineCoolantTemperature";
46 const VehicleProperty::Property VehicleProperty::MachineGunTurretStatus = "MachineGunTurretStatus";
47 const VehicleProperty::Property VehicleProperty::AccelerationX = "AccelerationX";
48 const VehicleProperty::Property VehicleProperty::AccelerationY = "AccelerationY";
49 const VehicleProperty::Property VehicleProperty::AccelerationZ = "AccelerationZ";
50 const VehicleProperty::Property VehicleProperty::MassAirFlow = "MassAirFlow";
51 const VehicleProperty::Property VehicleProperty::ButtonEvent = "ButtonEvent";
52 const VehicleProperty::Property VehicleProperty::AirIntakeTemperature = "AirIntakeTemperature";
53 const VehicleProperty::Property VehicleProperty::BatteryVoltage = "BatteryVoltage";
54 const VehicleProperty::Property VehicleProperty::InteriorTemperature = "InteriorTemperature";
55 const VehicleProperty::Property VehicleProperty::EngineOilTemperature = "EngineOilTemperature";
56 const VehicleProperty::Property VehicleProperty::VIN = "VIN";
57 const VehicleProperty::Property VehicleProperty::WMI = "WMI";
58 const VehicleProperty::Property VehicleProperty::TirePressureLeftFront = "TirePressureLeftFront";
59 const VehicleProperty::Property VehicleProperty::TirePressureRightFront = "TirePressureRightFront";
60 const VehicleProperty::Property VehicleProperty::TirePressureLeftRear = "TirePressureLeftRear";
61 const VehicleProperty::Property VehicleProperty::TirePressureRightRear = "TirePressureRightRear";
62 const VehicleProperty::Property VehicleProperty::VehiclePowerMode = "VehiclePowerMode";
63 const VehicleProperty::Property VehicleProperty::TripMeterA = "TripMeterA";
64 const VehicleProperty::Property VehicleProperty::TripMeterB = "TripMeterB";
65 const VehicleProperty::Property VehicleProperty::TripMeterC = "TripMeterC";
66
67 std::list<VehicleProperty::Property> VehicleProperty::mCapabilities;
68
69 VehicleProperty::VehicleProperty()
70 {
71         registerProperty( VehicleSpeed, [](){ return new VehicleSpeedType(0); });
72         registerProperty(EngineSpeed, [](){ return new EngineSpeedType(0); });
73         registerProperty(TransmissionShiftPosition, [](){ return new TransmissionShiftPositionType(Transmission::Neutral); });
74         registerProperty(TransmissionGearPosition, [](){ return new TransmissionGearPositionType(Transmission::Neutral); });
75         REGISTERPROPERTY(TransmissionMode,Transmission::Normal);
76         registerProperty(ThrottlePosition, [](){ return new ThrottlePositionType(0); });
77         registerProperty(WheelBrake, [](){ return new WheelBrakeType(false); });
78         registerProperty(SteeringWheelAngle, [](){ return new SteeringWheelAngleType(0); });
79         registerProperty(TurnSignal, [](){ return new TurnSignalType(TurnSignals::Off); });
80         registerProperty(ClutchStatus, [](){ return new ClutchStatusType(false); });
81         registerProperty(EngineOilPressure, [](){ return new EngineOilPressureType(0); });
82         registerProperty(EngineCoolantTemperature, [](){ return new EngineCoolantTemperatureType(0); });
83         registerProperty(MachineGunTurretStatus, [](){ return new MachineGunTurretStatusType(false); });
84         registerProperty(AccelerationX, [](){ return new AccelerationType(0); });
85         registerProperty(AccelerationY, [](){ return new AccelerationType(0); });
86         registerProperty(AccelerationZ, [](){ return new AccelerationType(0); });
87         registerProperty(MassAirFlow, [](){ return new MassAirFlowType(0); });
88         registerProperty(ButtonEvent, [](){ return new ButtonEventType(ButtonEvents::NoButton); });
89         registerProperty(BatteryVoltage, [](){ return new BatteryVoltageType(0); });
90         registerProperty(InteriorTemperature, [](){ return new InteriorTemperatureType(0); });
91         registerProperty(EngineOilTemperature, [](){ return new EngineOilTemperatureType(0); });
92         registerProperty(VIN, [](){ return new VINType(""); });
93         registerProperty(WMI, [](){ return new WMIType(""); });
94         REGISTERPROPERTYWITHTYPE(TirePressureLeftFront, TirePressureType, 0);
95         REGISTERPROPERTYWITHTYPE(TirePressureRightFront, TirePressureType, 0);
96         REGISTERPROPERTYWITHTYPE(TirePressureLeftRear, TirePressureType, 0);
97         REGISTERPROPERTYWITHTYPE(TirePressureRightRear, TirePressureType, 0);
98         registerProperty( VehiclePowerMode,[](){ return new VehiclePowerModeType(Power::Off); } );
99         REGISTERPROPERTYWITHTYPE(TripMeterA,TripMeterType,0);
100         REGISTERPROPERTYWITHTYPE(TripMeterB,TripMeterType,0);
101         REGISTERPROPERTYWITHTYPE(TripMeterC,TripMeterType,0);
102 }
103
104 std::list<VehicleProperty::Property> VehicleProperty::capabilities()
105 {
106         return mCapabilities;
107 }
108
109 AbstractPropertyType* VehicleProperty::getPropertyTypeForPropertyNameValue(VehicleProperty::Property name, std::string value)
110 {
111
112         if(registeredPropertyFactoryMap.count(name) > 0)
113         {
114                 VehicleProperty::PropertyTypeFactoryCallback cb = registeredPropertyFactoryMap[name];
115                 if ( cb != NULL )
116                 {
117                         AbstractPropertyType* type = cb();
118                         if(type == NULL)
119                                 throw std::runtime_error("Cannot return NULL in a PropertyTypeFactory");
120
121                         type->fromString(value);
122
123                         return type;
124                 }
125
126         }
127
128         return nullptr;
129 }
130
131 void VehicleProperty::registerProperty(VehicleProperty::Property name, VehicleProperty::PropertyTypeFactoryCallback factory)
132 {
133         registeredPropertyFactoryMap[name] = factory;
134         mCapabilities.push_back(name);
135 }
136
137 VehicleProperty vehiclePropertyConstruct;