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