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