attempt to support custom properties in dbus. will seg fault
authorKevron Rees <kevron_m_rees@linux.intel.com>
Fri, 11 Jan 2013 01:25:36 +0000 (17:25 -0800)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Fri, 11 Jan 2013 01:25:36 +0000 (17:25 -0800)
16 files changed:
lib/abstractpropertytype.h
lib/vehicleproperty.cpp
lib/vehicleproperty.h
plugins/dbus/CMakeLists.txt
plugins/dbus/abstractdbusinterface.cpp
plugins/dbus/abstractproperty.cpp
plugins/dbus/abstractproperty.h
plugins/dbus/basicproperty.h
plugins/dbus/custompropertyinterface.cpp [new file with mode: 0644]
plugins/dbus/custompropertyinterface.h [new file with mode: 0644]
plugins/dbus/dbusinterfacemanager.cpp
plugins/dbus/dbusplugin.cpp
plugins/dbus/varianttype.cpp [new file with mode: 0644]
plugins/dbus/varianttype.h [new file with mode: 0644]
plugins/obd2plugin/obd2source.cpp
plugins/obd2plugin/obd2source.h

index 3e1d8ac..fda5540 100644 (file)
@@ -26,6 +26,7 @@
 #include <boost/lexical_cast.hpp>
 #include <boost/utility.hpp>
 #include <type_traits>
+#include <glibmm/variant.h>
 #include "timestamp.h"
 
 class AbstractPropertyType
@@ -37,6 +38,10 @@ public:
 
        virtual void fromString(std::string)= 0;
 
+       virtual Glib::VariantBase* toVariant() = 0;
+
+       virtual void fromVariant(Glib::VariantBase*) = 0;
+
        virtual AbstractPropertyType* copy() = 0;
 
        double timestamp;
@@ -119,8 +124,22 @@ public:
                return stream.str();
        }
 
+       Glib::VariantBase* toVariant()
+       {
+               serializeVariant<T>(value<T>());
+
+               return &mVariant;
+       }
+
+       void fromVariant(Glib::VariantBase *v)
+       {
+               setValue(deserializeVariant<T>(v));
+       }
+
 private:
 
+       Glib::VariantBase mVariant;
+
        template <class N>
        void serialize(std::string val,  typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
        {
@@ -140,6 +159,33 @@ private:
                stream>>someTemp;
                setValue(someTemp);
        }
+
+       template <class N>
+       void serializeVariant(T val, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
+       {
+               mVariant = Glib::VariantBase(Glib::Variant<gint16>::create((int)val).gobj());
+       }
+
+       template <class N>
+       void serializeVariant(T val, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
+       {
+               //mVariant = Glib::Variant<int>::create(0);
+               mVariant = Glib::Variant<T>::create(val);
+       }
+
+       template <class N>
+       T deserializeVariant(Glib::VariantBase* v, typename std::enable_if<std::is_enum<N>::value, N>::type* = 0)
+       {
+               return (T)((Glib::Variant<int>::cast_dynamic<Glib::Variant<int> >(*v)).get());
+       }
+
+       template <class N>
+       T deserializeVariant(Glib::VariantBase* v, typename std::enable_if<!std::is_enum<N>::value, N>::type* = 0)
+       {
+               return Glib::VariantBase::cast_dynamic<Glib::Variant<T> >(*v).get();
+       }
+
+
 };
 
 class StringPropertyType: public AbstractPropertyType
@@ -178,6 +224,21 @@ public:
                return value<std::string>();
        }
 
+       Glib::VariantBase* toVariant()
+       {
+               mVariant = Glib::Variant<std::string>::create(toString());
+
+               return &mVariant;
+       }
+
+       void fromVariant(Glib::VariantBase *v)
+       {
+               setValue(std::string(v->print()));
+       }
+
+private:
+
+       Glib::Variant<std::string> mVariant;
 };
 
 #endif
index d60ccc8..32d57b3 100644 (file)
 
 
 #define REGISTERPROPERTY(property, defaultValue) \
-       registerProperty(property, []() { return new property ## Type(defaultValue); });
+       registerPropertyPriv(property, []() { return new property ## Type(defaultValue); });
 
 #define REGISTERPROPERTYWITHTYPE(property, type, defaultValue) \
-       registerProperty(property, []() { return new type(defaultValue); });
+       registerPropertyPriv(property, []() { return new type(defaultValue); });
 
 using namespace std;
 
@@ -94,34 +94,35 @@ const VehicleProperty::Property VehicleProperty::FuelAverageEconomy = "FuelAvera
 const VehicleProperty::Property VehicleProperty::EngineOilRemaining = "EngineOilRemaining";
 
 std::list<VehicleProperty::Property> VehicleProperty::mCapabilities;
+std::list<VehicleProperty::Property> VehicleProperty::mCustomProperties;
 
 VehicleProperty::VehicleProperty()
 {
-       registerProperty( VehicleSpeed, [](){ return new VehicleSpeedType(0); });
-       registerProperty(EngineSpeed, [](){ return new EngineSpeedType(0); });
-       registerProperty(TransmissionShiftPosition, [](){ return new TransmissionShiftPositionType(Transmission::Neutral); });
-       registerProperty(TransmissionGearPosition, [](){ return new TransmissionGearPositionType(Transmission::Neutral); });
+       registerPropertyPriv( VehicleSpeed, [](){ return new VehicleSpeedType(0); });
+       registerPropertyPriv(EngineSpeed, [](){ return new EngineSpeedType(0); });
+       registerPropertyPriv(TransmissionShiftPosition, [](){ return new TransmissionShiftPositionType(Transmission::Neutral); });
+       registerPropertyPriv(TransmissionGearPosition, [](){ return new TransmissionGearPositionType(Transmission::Neutral); });
        REGISTERPROPERTY(TransmissionMode,Transmission::Normal);
-       registerProperty(ThrottlePosition, [](){ return new ThrottlePositionType(0); });
-       registerProperty(WheelBrake, [](){ return new WheelBrakeType(false); });
-       registerProperty(SteeringWheelAngle, [](){ return new SteeringWheelAngleType(0); });
-       registerProperty(TurnSignal, [](){ return new TurnSignalType(TurnSignals::Off); });
-       registerProperty(ClutchStatus, [](){ return new ClutchStatusType(false); });
-       registerProperty(EngineOilPressure, [](){ return new EngineOilPressureType(0); });
-       registerProperty(EngineOilTemperature, [](){ return new EngineOilTemperatureType(0); });
+       registerPropertyPriv(ThrottlePosition, [](){ return new ThrottlePositionType(0); });
+       registerPropertyPriv(WheelBrake, [](){ return new WheelBrakeType(false); });
+       registerPropertyPriv(SteeringWheelAngle, [](){ return new SteeringWheelAngleType(0); });
+       registerPropertyPriv(TurnSignal, [](){ return new TurnSignalType(TurnSignals::Off); });
+       registerPropertyPriv(ClutchStatus, [](){ return new ClutchStatusType(false); });
+       registerPropertyPriv(EngineOilPressure, [](){ return new EngineOilPressureType(0); });
+       registerPropertyPriv(EngineOilTemperature, [](){ return new EngineOilTemperatureType(0); });
        REGISTERPROPERTY(EngineOilRemaining,0);
-       registerProperty(EngineCoolantTemperature, [](){ return new EngineCoolantTemperatureType(0); });
-       registerProperty(MachineGunTurretStatus, [](){ return new MachineGunTurretStatusType(false); });
-       registerProperty(AccelerationX, [](){ return new AccelerationType(0); });
-       registerProperty(AccelerationY, [](){ return new AccelerationType(0); });
-       registerProperty(AccelerationZ, [](){ return new AccelerationType(0); });
-       registerProperty(MassAirFlow, [](){ return new MassAirFlowType(0); });
-       registerProperty(ButtonEvent, [](){ return new ButtonEventType(ButtonEvents::NoButton); });
+       registerPropertyPriv(EngineCoolantTemperature, [](){ return new EngineCoolantTemperatureType(0); });
+       registerPropertyPriv(MachineGunTurretStatus, [](){ return new MachineGunTurretStatusType(false); });
+       registerPropertyPriv(AccelerationX, [](){ return new AccelerationType(0); });
+       registerPropertyPriv(AccelerationY, [](){ return new AccelerationType(0); });
+       registerPropertyPriv(AccelerationZ, [](){ return new AccelerationType(0); });
+       registerPropertyPriv(MassAirFlow, [](){ return new MassAirFlowType(0); });
+       registerPropertyPriv(ButtonEvent, [](){ return new ButtonEventType(ButtonEvents::NoButton); });
        REGISTERPROPERTY(AirIntakeTemperature,0)
-       registerProperty(BatteryVoltage, [](){ return new BatteryVoltageType(0); });
-       registerProperty(InteriorTemperature, [](){ return new InteriorTemperatureType(0); });
-       registerProperty(VIN, [](){ return new VINType(""); });
-       registerProperty(WMI, [](){ return new WMIType(""); });
+       registerPropertyPriv(BatteryVoltage, [](){ return new BatteryVoltageType(0); });
+       registerPropertyPriv(InteriorTemperature, [](){ return new InteriorTemperatureType(0); });
+       registerPropertyPriv(VIN, [](){ return new VINType(""); });
+       registerPropertyPriv(WMI, [](){ return new WMIType(""); });
        REGISTERPROPERTYWITHTYPE(TirePressureLeftFront, TirePressureType, 0);
        REGISTERPROPERTYWITHTYPE(TirePressureRightFront, TirePressureType, 0);
        REGISTERPROPERTYWITHTYPE(TirePressureLeftRear, TirePressureType, 0);
@@ -130,7 +131,7 @@ VehicleProperty::VehicleProperty()
        REGISTERPROPERTYWITHTYPE(TireTemperatureRightFront,TireTemperatureType,0);
        REGISTERPROPERTYWITHTYPE(TireTemperatureLeftRear,TireTemperatureType,0);
        REGISTERPROPERTYWITHTYPE(TireTemperatureRightRear,TireTemperatureType,0);
-       registerProperty( VehiclePowerMode,[](){ return new VehiclePowerModeType(Power::Off); } );
+       registerPropertyPriv( VehiclePowerMode,[](){ return new VehiclePowerModeType(Power::Off); } );
        REGISTERPROPERTYWITHTYPE(TripMeterA,TripMeterType,0);
        REGISTERPROPERTYWITHTYPE(TripMeterB,TripMeterType,0);
        REGISTERPROPERTYWITHTYPE(TripMeterC,TripMeterType,0);
@@ -163,6 +164,11 @@ std::list<VehicleProperty::Property> VehicleProperty::capabilities()
        return mCapabilities;
 }
 
+std::list<VehicleProperty::Property> VehicleProperty::customProperties()
+{
+       return mCustomProperties;
+}
+
 AbstractPropertyType* VehicleProperty::getPropertyTypeForPropertyNameValue(VehicleProperty::Property name, std::string value)
 {
        if(registeredPropertyFactoryMap.count(name) > 0)
@@ -185,6 +191,14 @@ AbstractPropertyType* VehicleProperty::getPropertyTypeForPropertyNameValue(Vehic
 
 bool VehicleProperty::registerProperty(VehicleProperty::Property name, VehicleProperty::PropertyTypeFactoryCallback factory)
 {
+
+       mCustomProperties.push_back(name);
+
+       return registerPropertyPriv(name, factory);
+}
+
+bool VehicleProperty::registerPropertyPriv(VehicleProperty::Property name, VehicleProperty::PropertyTypeFactoryCallback factory)
+{
        if(ListPlusPlus<Property>(&mCapabilities).contains(name))
        {
                DebugOut(0)<<__FUNCTION__<<" ERROR: property '"<<name<<"'' already registered."<<endl;
@@ -195,6 +209,7 @@ bool VehicleProperty::registerProperty(VehicleProperty::Property name, VehiclePr
        mCapabilities.push_back(name);
 
        return true;
+
 }
 
 VehicleProperty vehiclePropertyConstruct;
index 7789aae..2e475e4 100644 (file)
@@ -235,14 +235,14 @@ public:
        static const Property TirePressureRightFront;
        static const Property TirePressureLeftRear;
        static const Property TirePressureRightRear;
-       typedef BasicPropertyType<float> TirePressureType;
+       typedef BasicPropertyType<double> TirePressureType;
 
        /**< Tire temperature in degrees C */
        static const Property TireTemperatureLeftFront;
        static const Property TireTemperatureRightFront;
        static const Property TireTemperatureLeftRear;
        static const Property TireTemperatureRightRear;
-       typedef BasicPropertyType<float> TireTemperatureType;
+       typedef BasicPropertyType<double> TireTemperatureType;
        
        /**< Vehicle Power Mode.
         *@see Power::PowerModes
@@ -296,6 +296,7 @@ public:
        typedef BasicPropertyType<uint16_t> FuelAverageEconomyType;
 
        static std::list<VehicleProperty::Property> capabilities();
+       static std::list<VehicleProperty::Property> customProperties();
 
        /*! getPropertyTypeForPropertyNameValue returns an AbstractPropertyType* for the property name
          * with the value specified by 'value'.  Ownership of the returned AbstractPropertyType* is
@@ -323,10 +324,14 @@ public:
         */
        static bool registerProperty(Property name, PropertyTypeFactoryCallback factory);
 
+
+
 private:
+       static bool registerPropertyPriv(Property name, PropertyTypeFactoryCallback factory);
 
        static std::map<Property, PropertyTypeFactoryCallback> registeredPropertyFactoryMap;
        static std::list<VehicleProperty::Property> mCapabilities;
+       static std::list<VehicleProperty::Property> mCustomProperties;
 };
 
 typedef std::list<VehicleProperty::Property> PropertyList;
index 4e9745b..eb4060f 100644 (file)
@@ -3,8 +3,8 @@ include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs})
 
 pkg_check_modules(gio REQUIRED gio-2.0)
 
-set(dbussinkplugin_headers dbusplugin.h abstractproperty.h abstractdbusinterface.h dbusinterfacemanager.h basicproperty.h properties.h)
-set(dbussinkplugin_sources dbusplugin.cpp abstractproperty.cpp abstractdbusinterface.cpp dbusinterfacemanager.cpp basicproperty.cpp properties.cpp)
+set(dbussinkplugin_headers dbusplugin.h abstractproperty.h abstractdbusinterface.h dbusinterfacemanager.h basicproperty.h properties.h varianttype.h custompropertyinterface.h)
+set(dbussinkplugin_sources dbusplugin.cpp abstractproperty.cpp abstractdbusinterface.cpp dbusinterfacemanager.cpp basicproperty.cpp properties.cpp varianttype.cpp custompropertyinterface.cpp)
 
 add_library(dbussinkplugin MODULE ${dbussinkplugin_sources})
 set_target_properties(dbussinkplugin PROPERTIES PREFIX "")
index dc5d42e..aad3fd2 100644 (file)
@@ -113,7 +113,8 @@ void AbstractDBusInterface::updateValue(AbstractProperty *property)
        GError *error = NULL;
 
        GVariant **params = g_new(GVariant*,2);
-       params[0] = g_variant_new("v",property->toGVariant());
+       GVariant *val = g_variant_ref(property->toGVariant());
+       params[0] = g_variant_new("v",val);
        params[1] = g_variant_new("d",property->timestamp());
 
        GVariant *tuple_variant = g_variant_new_tuple(params,2);
@@ -143,6 +144,7 @@ GVariant* AbstractDBusInterface::getProperty(GDBusConnection* connection, const
        {
                GVariant* value = interfaceMap[interfaceName]->getProperty(propertyName);
                return value;
+
        }
        debugOut("No interface for" + string(interfaceName));
        return nullptr;
index 5109387..1d417b6 100644 (file)
@@ -21,7 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #include "abstractdbusinterface.h"
 
 AbstractProperty::AbstractProperty(string pn, string sig, Access access, AbstractDBusInterface* interface)
-       : mPropertyName(pn), mSignature(sig), mAccess(access), mInterface(interface), mTimestamp(0)
+       : mPropertyName(pn), mSignature(sig), mAccess(access), mInterface(interface), mTimestamp(0),mValue(NULL)
 {
 
 }
index 2f98072..5e5f289 100644 (file)
@@ -74,24 +74,32 @@ public:
                return mTimestamp;
        }
 
-       void setValue(boost::any val, double timestmp)
+       virtual void setValue(AbstractPropertyType* val)
        {
-               mValue = val;
-               mTimestamp = timestmp;
+               mValue = val->copy();
+               mAnyValue = val->anyValue();
+               mTimestamp = val->timestamp;
                updateValue();
        }
 
        template<typename T>
        void setValue(T val)
        {
-               mValue = val;
+               mAnyValue = val;
                updateValue();
        }
 
+
+
        template<typename T>
-       T value()
+       T anyValue()
+       {
+               return boost::any_cast<T>(mAnyValue);
+       }
+
+       AbstractPropertyType* value()
        {
-               return boost::any_cast<T>(mValue);
+               return mValue;
        }
        
 protected: ///methods:
@@ -100,13 +108,13 @@ protected: ///methods:
        
 protected:
        
-       boost::any mValue;
+       boost::any mAnyValue;
        string mPropertyName;
        string mSignature;
        SetterFunc mSetterFunc;
        Access mAccess;
        double mTimestamp;
-       
+       AbstractPropertyType* mValue;
        AbstractDBusInterface* mInterface;
 };
 
index cbd79e7..ba83f20 100644 (file)
@@ -40,7 +40,7 @@ public:
 
        T value()
        {
-               return AbstractProperty::value<T>();
+               return AbstractProperty::anyValue<T>();
        }
 
        virtual GVariant* toGVariant()
diff --git a/plugins/dbus/custompropertyinterface.cpp b/plugins/dbus/custompropertyinterface.cpp
new file mode 100644 (file)
index 0000000..ceeb9fd
--- /dev/null
@@ -0,0 +1,17 @@
+#include "custompropertyinterface.h"
+#include "vehicleproperty.h"
+#include "varianttype.h"
+
+CustomPropertyInterface::CustomPropertyInterface(AbstractRoutingEngine *re, GDBusConnection *connection)
+       :DBusSink("org.automotive.custom","/org/automotive/custom", re, connection, map<string, string>())
+{
+       PropertyList list = VehicleProperty::customProperties();
+       for (auto itr = list.begin(); itr != list.end(); itr++)
+       {
+               VehicleProperty::Property prop = *itr;
+
+               propertyDBusMap[prop] = new VariantType(re, prop, VariantType::ReadWrite, this);
+       }
+
+       supportedChanged(re->supported());
+}
diff --git a/plugins/dbus/custompropertyinterface.h b/plugins/dbus/custompropertyinterface.h
new file mode 100644 (file)
index 0000000..c6a8207
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef CUSTOMPROPERTYINTERFACE_H_
+#define CUSTOMPROPERTYINTERFACE_H_
+
+#include "dbusplugin.h"
+
+class CustomPropertyInterface: DBusSink
+{
+public:
+       CustomPropertyInterface(AbstractRoutingEngine* re, GDBusConnection* connection);
+};
+
+
+#endif
index 211a02b..f33aacb 100644 (file)
@@ -25,6 +25,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ///properties:
 #include "accelerationproperty.h"
 #include "properties.h"
+#include "custompropertyinterface.h"
+
 
 #define ConstructProperty(property) \
        new property(iface->re, connection);
@@ -50,6 +52,8 @@ on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_d
        ConstructProperty(HornProperty);
        ConstructProperty(FuelProperty);
        ConstructProperty(EngineOilProperty);
+       ConstructProperty(CustomPropertyInterface);
+
 }
 
 static void
index fc85c6e..2009af7 100644 (file)
@@ -60,7 +60,7 @@ void DBusSink::propertyChanged(VehicleProperty::Property property, AbstractPrope
                return;
 
        AbstractProperty* prop = propertyDBusMap[property];
-       prop->setValue(value->anyValue(),value->timestamp);
+       prop->setValue(value);
 }
 
 std::string DBusSink::uuid()
diff --git a/plugins/dbus/varianttype.cpp b/plugins/dbus/varianttype.cpp
new file mode 100644 (file)
index 0000000..6cfcd02
--- /dev/null
@@ -0,0 +1,20 @@
+#include "varianttype.h"
+
+VariantType::VariantType(AbstractRoutingEngine* re, std::string propertyName,  Access access, AbstractDBusInterface *interface)
+       :AbstractProperty(propertyName, "v", access, interface)
+{
+//VehicleProperty::getPropertyTypeForPropertyNameValue(propertyName,"")->toVariant()->get_type_string()
+}
+
+GVariant *VariantType::toGVariant()
+{
+       if(!value())
+               return g_variant_new("v",g_variant_new_int16(0));
+
+       return value()->toVariant()->gobj();
+}
+
+void VariantType::fromGVariant(GVariant *value)
+{
+
+}
diff --git a/plugins/dbus/varianttype.h b/plugins/dbus/varianttype.h
new file mode 100644 (file)
index 0000000..8214eec
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _VARIANT_TYPE_H_
+#define _VARIANT_TYPE_H_
+
+#include "abstractproperty.h"
+#include "abstractroutingengine.h"
+
+class VariantType: public AbstractProperty
+{
+public:
+
+       VariantType(AbstractRoutingEngine* re,string propertyName, Access access, AbstractDBusInterface* interface);
+       GVariant* toGVariant();
+       void fromGVariant(GVariant *value);
+
+private:
+       AbstractRoutingEngine* routingEngine;
+};
+
+#endif
index 5f49470..06e2491 100644 (file)
@@ -34,7 +34,7 @@ AbstractRoutingEngine *m_re;
 
 //std::list<ObdPid*> Obd2Amb::supportedPidsList;
 Obd2Amb *obd2AmbInstance = new Obd2Amb;
-
+VehicleProperty::Property Obd2Connected = "Obd2Connected";
 int calledPersecond = 0;
 
 bool sendElmCommand(obdLib *obd,std::string command)
@@ -443,12 +443,14 @@ static int updateProperties(/*gpointer retval,*/ gpointer data)
                StatusMessage *reply = (StatusMessage*)retval;
                if (reply->statusStr == "disconnected")
                {
-                       //TODO: This is where we update Obd2Connected property
-                       //src->updateProperty("Obd2Connected",true);
+
+                       BasicPropertyType<bool> val(false);
+                       src->updateProperty(Obd2Connected,&val);
                }
                else if (reply->statusStr == "connected")
                {
-                       //TODO: This is where we update Obd2Connected property
+                       BasicPropertyType<bool> val(true);
+                       src->updateProperty(Obd2Connected,&val);
                }
        }
        while(gpointer retval = g_async_queue_try_pop(src->responseQueue))
@@ -610,9 +612,9 @@ void OBD2Source::setConfiguration(map<string, string> config)
 }
 
 OBD2Source::OBD2Source(AbstractRoutingEngine *re, map<string, string> config)
-       : AbstractSource(re, config), Obd2Connect("Obd2Connect")
+       : AbstractSource(re, config)
 {
-       VehicleProperty::registerProperty(Obd2Connect,[](){ return new Obd2ConnectType(false); });
+       VehicleProperty::registerProperty(Obd2Connected,[](){ return new Obd2ConnectType(false); });
        clientConnected = false;
        m_re = re;  
 
@@ -625,7 +627,7 @@ OBD2Source::OBD2Source(AbstractRoutingEngine *re, map<string, string> config)
                m_supportedProperties.push_back((*itr)->property);
        }
 
-       m_supportedProperties.push_back(Obd2Connect);
+       m_supportedProperties.push_back(Obd2Connected);
 
        re->setSupported(supported(), this);
        /*if (openPort(std::string("/dev/pts/7"),115200))
index a6f55a4..a224dab 100644 (file)
@@ -178,7 +178,6 @@ public:
 private:
        PropertyList m_supportedProperties;
        GMutex *threadQueueMutex;
-       VehicleProperty::Property Obd2Connect;
        typedef BasicPropertyType<bool> Obd2ConnectType;
 
 };