dbus plugin updates
authorKevron Rees <kevron_m_rees@linux.intel.com>
Mon, 20 Aug 2012 18:03:23 +0000 (11:03 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Mon, 20 Aug 2012 18:03:23 +0000 (11:03 -0700)
CMakeLists.txt
plugins/dbus/CMakeLists.txt
plugins/dbus/abstractdbusinterface.cpp [new file with mode: 0644]
plugins/dbus/abstractdbusinterface.h [new file with mode: 0644]
plugins/dbus/abstractproperty.cpp [new file with mode: 0644]
plugins/dbus/abstractproperty.h [new file with mode: 0644]
plugins/dbus/dbusinterfacemanager.cpp [new file with mode: 0644]
plugins/dbus/dbusinterfacemanager.h [new file with mode: 0644]
plugins/dbus/dbusplugin.cpp
plugins/dbus/dbusplugin.h

index b003357..118f4cd 100644 (file)
@@ -21,14 +21,13 @@ find_path(libtool_INCLUDE_DIR ltdl.h DOC "Libtool headers")
 find_package(Boost REQUIRED)
 
 pkg_check_modules(glib REQUIRED glib-2.0)
-pkg_check_modules(gio REQUIRED gio-2.0)
 pkg_check_modules(json REQUIRED json-glib-1.0)
 
 add_definitions(-std=c++0x)
 add_definitions(-DDBusServiceName="org.automotive.message.broker")
 
 set(include_dirs ${libtool_INCLUDE_DIR} ${glib_INCLUDE_DIRS} ${gio_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${json_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/lib)
-set(link_libraries -lamb ${libtool_LIBRARY} ${glib_LIBRARIES} ${gio_LIBRARIES} ${json_LIBRARIES} -L${CMAKE_CURRENT_BINARY_DIR}/lib)
+set(link_libraries -lamb ${libtool_LIBRARY} ${glib_LIBRARIES} ${json_LIBRARIES} -L${CMAKE_CURRENT_BINARY_DIR}/lib)
 
 if(use_qtcore)
 
index ebc7786..d83a28b 100644 (file)
@@ -1,11 +1,13 @@
 
 include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs})
 
-set(dbussinkplugin_headers dbusplugin.h)
-set(dbussinkplugin_sources dbusplugin.cpp)
+pkg_check_modules(gio REQUIRED gio-2.0)
+
+set(dbussinkplugin_headers dbusplugin.h abstractproperty.h abstractdbusinterface.h dbusinterfacemanager.h)
+set(dbussinkplugin_sources dbusplugin.cpp abstractproperty.cpp abstractdbusinterface.cpp dbusinterfacemanager.cpp)
 
 add_library(dbussinkplugin MODULE ${dbussinkplugin_sources})
 set_target_properties(dbussinkplugin PROPERTIES PREFIX "")
-target_link_libraries(dbussinkplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
+target_link_libraries(dbussinkplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries} ${gio_LIBRARIES})
 
 
diff --git a/plugins/dbus/abstractdbusinterface.cpp b/plugins/dbus/abstractdbusinterface.cpp
new file mode 100644 (file)
index 0000000..fba0e3a
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+Copyright (C) 2012 Intel Corporation
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "abstractdbusinterface.h"
+#include "debugout.h"
+#include <boost/algorithm/string.hpp>
+#include <gio/gio.h>
+
+#include "abstractproperty.h"
+
+unordered_map<string, AbstractDBusInterface*> AbstractDBusInterface::interfaceMap;
+
+AbstractDBusInterface::AbstractDBusInterface(string interfaceName, string objectPath)
+: mInterfaceName(interfaceName), mObjectPath(objectPath)
+{
+       mConnection = nullptr;
+
+       interfaceMap[interfaceName] = this;
+       introspectionXml ="<node>" ;
+       introspectionXml += "<interface name='"+ interfaceName + "' >";
+}
+
+void AbstractDBusInterface::addProperty(AbstractProperty* property)
+{
+       string nameToLower = property->name();
+       boost::algorithm::to_lower<string>(nameToLower);
+       
+       ///see which properties are supported:
+       introspectionXml +=     "<property type='"+ property->signature() + "' name='"+ property->name()+"' access='read' />"
+       "<signal name='" + property->name() + "' >"
+       "       <arg type='"+ property->signature() + "' name='" + nameToLower + "' direction='out' />"
+       "</signal>";
+       
+       properties[property->name()] = property;
+}
+
+void AbstractDBusInterface::registerObject(GDBusConnection* connection)
+{
+       mConnection = connection;
+       
+       if(introspectionXml.empty())
+       {
+               cerr<<"no interface to export: "<<mInterfaceName<<endl;
+               throw -1;
+       }
+
+       introspectionXml += "</interface>";
+       introspectionXml += "</node>";
+       
+       GError* error=NULL;
+
+       GDBusNodeInfo* introspection = g_dbus_node_info_new_for_xml(introspectionXml.c_str(), &error);
+       
+       if(!introspection)
+       {
+               cerr<<"Error in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<endl;
+               cerr<<error->message<<endl;
+               cerr<<"probably bad xml:"<<endl;
+               cerr<<introspectionXml<<endl;
+               return;
+       }
+
+       GDBusInterfaceInfo* mInterfaceInfo = g_dbus_node_info_lookup_interface(introspection, mInterfaceName.c_str());
+       
+       const GDBusInterfaceVTable vtable = { NULL, AbstractDBusInterface::getProperty, AbstractDBusInterface::setProperty };
+       
+       guint regId = g_dbus_connection_register_object(connection, mObjectPath.c_str(), mInterfaceInfo, &vtable, NULL, NULL, &error);
+       
+       if(error) throw -1;
+       
+       g_assert(regId > 0);
+}
+
+void AbstractDBusInterface::updateValue(AbstractProperty *property)
+{
+       if(mConnection == nullptr)
+       {
+               return;
+       }
+
+       GError *error = NULL;
+       g_dbus_connection_emit_signal(mConnection, NULL, mObjectPath.c_str(), mInterfaceName.c_str(), property->name().c_str(), g_variant_new("(v)",property->toGVariant()), &error);
+
+       if(error)
+       {
+               throw -1;
+       }
+}
+
+GVariant* AbstractDBusInterface::getProperty(GDBusConnection* connection, const gchar* sender, const gchar* objectPath, const gchar* interfaceName, const gchar* propertyName, GError** error, gpointer userData)
+{
+       *error = NULL;
+       if(interfaceMap.count(interfaceName))
+       {
+               GVariant* value = interfaceMap[interfaceName]->getProperty(propertyName);
+               return value;
+       }
+       debugOut("No interface for" + string(interfaceName));
+       return nullptr;
+}
+
+gboolean AbstractDBusInterface::setProperty(GDBusConnection* connection, const gchar* sender, const gchar* objectPath, const gchar* interfaceName, const gchar* propertyName, GVariant* value, GError** error, gpointer userData)
+{
+       if(interfaceMap.count(interfaceName))
+       {
+               interfaceMap[interfaceName]->setProperty(propertyName, value);
+               return true;
+       }
+
+       return false;
+}
+
diff --git a/plugins/dbus/abstractdbusinterface.h b/plugins/dbus/abstractdbusinterface.h
new file mode 100644 (file)
index 0000000..ef8c8c1
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+Copyright (C) 2012 Intel Corporation
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef ABSTRACTDBUSINTERFACE_H
+#define ABSTRACTDBUSINTERFACE_H
+
+#include <string>
+#include <functional>
+#include <unordered_map>
+#include <gio/gio.h>
+#include <boost/any.hpp>
+
+class AbstractProperty;
+
+using namespace std;
+
+class AbstractDBusInterface
+{
+
+public:
+       AbstractDBusInterface(string interfaceName, string objectPath);
+
+       
+       void registerObject(GDBusConnection* connection);
+       
+       void addProperty(AbstractProperty* property);
+       virtual void updateValue(AbstractProperty* property);
+       
+protected:
+       
+       static GVariant *getProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
+                                                                const gchar *interfaceName, const gchar * propertyName, GError** error,
+                                                                gpointer userData);
+       static gboolean setProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
+                                                               const gchar *interfaceName, const gchar * propertyName, GVariant *value,
+                                                               GError** error, gpointer userData);
+    
+       virtual void setProperty(string propertyName, GVariant * value) = 0;
+       virtual GVariant * getProperty(string propertyName) = 0;
+       
+       unordered_map<string, AbstractProperty*> properties;
+
+private:
+       string mInterfaceName;
+       string mObjectPath;
+       string introspectionXml;
+       GDBusConnection * mConnection;
+       static unordered_map<string, AbstractDBusInterface*> interfaceMap;
+};
+
+#endif // ABSTRACTDBUSINTERFACE_H
diff --git a/plugins/dbus/abstractproperty.cpp b/plugins/dbus/abstractproperty.cpp
new file mode 100644 (file)
index 0000000..111f2bd
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+Copyright (C) 2012 Intel Corporation
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#include "abstractproperty.h"
+#include "abstractdbusinterface.h"
+
+AbstractProperty::AbstractProperty(string pn, string sig, Access access, AbstractDBusInterface* interface)
+       : mPropertyName(pn), mSignature(sig), mAccess(access), mInterface(interface)
+{
+       interface->addProperty(this);
+}
+
+void AbstractProperty::updateValue()
+{
+       mInterface->updateValue(this);
+}
diff --git a/plugins/dbus/abstractproperty.h b/plugins/dbus/abstractproperty.h
new file mode 100644 (file)
index 0000000..3830681
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+Copyright (C) 2012 Intel Corporation
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef ABSTRACTPROPERTY_H
+#define ABSTRACTPROPERTY_H
+
+#include <string>
+#include <functional>
+#include <boost/any.hpp>
+#include <glib.h>
+
+#include "debugout.h"
+
+class AbstractDBusInterface;
+
+using namespace std;
+
+typedef function<void (boost::any)> SetterFunc;
+
+class AbstractProperty
+{
+
+public:
+
+       enum Access {
+               Read,
+               Write,
+               ReadWrite
+       };
+
+       AbstractProperty(string propertyName, string signature, Access access, AbstractDBusInterface* interface);
+       
+       virtual void setSetterFunction(SetterFunc setterFunc)
+       {
+               mSetterFunc = setterFunc;
+       }
+       
+       virtual string signature()
+       {
+               return mSignature;
+       }
+       
+       virtual string name() 
+       {
+               return mPropertyName;
+       }
+
+       virtual Access access()
+       {
+               return mAccess;
+       }
+       
+       virtual GVariant* toGVariant() = 0;
+       virtual void fromGVariant(GVariant *value) = 0;
+
+       template<typename T>
+       void setValue(T val)
+       {
+               mValue = val;
+               updateValue();
+       }
+
+       template<typename T>
+       T value()
+       {
+               return boost::any_cast<T>(mValue);
+       }
+       
+protected: ///methods:
+
+       void updateValue();
+       
+protected:
+       
+       boost::any mValue;
+       string mPropertyName;
+       string mSignature;
+       SetterFunc mSetterFunc;
+       Access mAccess;
+       
+       AbstractDBusInterface* mInterface;
+};
+
+#endif // ABSTRACTPROPERTY_H
diff --git a/plugins/dbus/dbusinterfacemanager.cpp b/plugins/dbus/dbusinterfacemanager.cpp
new file mode 100644 (file)
index 0000000..c1802bf
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+Copyright (C) 2012 Intel Corporation
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "dbusinterfacemanager.h"
+#include <gio/gio.h>
+#include <string>
+
+using namespace std;
+
+static void
+on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+       //AbstractDBusInterface::iface()->registerObject(connection);
+}
+
+static void
+on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+}
+
+static void
+on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+}
+
+
+
+DBusInterfaceManager::DBusInterfaceManager()
+{
+       g_type_init();
+
+       ownerId = g_bus_own_name(G_BUS_TYPE_SESSION,
+                                       DBusServiceName,
+                                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                                       on_bus_acquired,
+                                       on_name_acquired,
+                                       on_name_lost,
+                                       this,
+                                       NULL);
+
+       ///TODO: instantiate other interfaces here!
+}
+
+DBusInterfaceManager::~DBusInterfaceManager()
+{
+       g_bus_unown_name(ownerId);
+}
+
+
diff --git a/plugins/dbus/dbusinterfacemanager.h b/plugins/dbus/dbusinterfacemanager.h
new file mode 100644 (file)
index 0000000..54f42c9
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+Copyright (C) 2012 Intel Corporation
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#ifndef DBUSINTERFACEMANAGER_H
+#define DBUSINTERFACEMANAGER_H
+
+class DBusInterfaceManager
+{
+
+public:
+       DBusInterfaceManager();
+       ~DBusInterfaceManager();
+    
+    
+private:
+       
+       unsigned int ownerId;
+
+};
+
+#endif // DBUSINTERFACEMANAGER_H
index e69de29..adbdb06 100644 (file)
@@ -0,0 +1,60 @@
+/*
+       Copyright (C) 2012  Intel Corporation
+
+       This library is free software; you can redistribute it and/or
+       modify it under the terms of the GNU Lesser General Public
+       License as published by the Free Software Foundation; either
+       version 2.1 of the License, or (at your option) any later version.
+
+       This library is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+       Lesser General Public License for more details.
+
+       You should have received a copy of the GNU Lesser General Public
+       License along with this library; if not, write to the Free Software
+       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+#include "dbusplugin.h".h"
+#include "abstractroutingengine.h"
+#include "debugout.h"
+
+extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine)
+{
+       return new DBusSinkManager(routingengine);
+}
+
+DBusSink::DBusSink(AbstractRoutingEngine* engine): AbstractSink(engine)
+{
+       routingEngine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
+       routingEngine->subscribeToProperty(VehicleProperty::VehicleSpeed, this);
+
+       AsyncPropertyRequest velocityRequest;
+       velocityRequest.property = VehicleProperty::VehicleSpeed;
+       velocityRequest.completed = [](AsyncPropertyReply* reply) { DebugOut()<<"Velocity Async request completed: "<<boost::any_cast<uint16_t>(reply->value)<<endl; };
+
+       AsyncPropertyReply* reply = routingEngine->getPropertyAsync(velocityRequest);
+}
+
+
+PropertyList DBusSink::subscriptions()
+{
+
+}
+
+void DBusSink::supportedChanged(PropertyList supportedProperties)
+{
+
+}
+
+void DBusSink::propertyChanged(VehicleProperty::Property property, boost::any value, std::string uuid)
+{
+       DebugOut()<<VehicleProperty::name(property)<<" value: "<<boost::any_cast<uint16_t>(value)<<endl;
+}
+
+std::string DBusSink::uuid()
+{
+       return "c2e6cafa-eef5-4b8a-99a0-0f2c9be1057d";
+}
index e69de29..36d9c6d 100644 (file)
@@ -0,0 +1,24 @@
+#include "abstractsink.h"
+
+
+class DBusSink : public AbstractSink
+{
+
+public:
+       DBusSink(AbstractRoutingEngine* engine);
+       virtual PropertyList subscriptions();
+       virtual void supportedChanged(PropertyList supportedProperties);
+       virtual void propertyChanged(VehicleProperty::Property property, boost::any value, std::string uuid);
+       virtual std::string uuid();
+};
+
+class DBusSinkManager: public AbstractSinkManager
+{
+public:
+       DBusSinkManager(AbstractRoutingEngine* engine)
+       :AbstractSinkManager(engine)
+       {
+               new DBusSink(engine);
+       }
+};
+