From 854a1d51bff0808cd217a64f220da5b27f58cc54 Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Mon, 20 Aug 2012 11:03:23 -0700 Subject: [PATCH] dbus plugin updates --- CMakeLists.txt | 3 +- plugins/dbus/CMakeLists.txt | 8 ++- plugins/dbus/abstractdbusinterface.cpp | 127 +++++++++++++++++++++++++++++++++ plugins/dbus/abstractdbusinterface.h | 66 +++++++++++++++++ plugins/dbus/abstractproperty.cpp | 32 +++++++++ plugins/dbus/abstractproperty.h | 99 +++++++++++++++++++++++++ plugins/dbus/dbusinterfacemanager.cpp | 64 +++++++++++++++++ plugins/dbus/dbusinterfacemanager.h | 37 ++++++++++ plugins/dbus/dbusplugin.cpp | 60 ++++++++++++++++ plugins/dbus/dbusplugin.h | 24 +++++++ 10 files changed, 515 insertions(+), 5 deletions(-) create mode 100644 plugins/dbus/abstractdbusinterface.cpp create mode 100644 plugins/dbus/abstractdbusinterface.h create mode 100644 plugins/dbus/abstractproperty.cpp create mode 100644 plugins/dbus/abstractproperty.h create mode 100644 plugins/dbus/dbusinterfacemanager.cpp create mode 100644 plugins/dbus/dbusinterfacemanager.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b003357..118f4cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/plugins/dbus/CMakeLists.txt b/plugins/dbus/CMakeLists.txt index ebc7786..d83a28b 100644 --- a/plugins/dbus/CMakeLists.txt +++ b/plugins/dbus/CMakeLists.txt @@ -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 index 0000000..fba0e3a --- /dev/null +++ b/plugins/dbus/abstractdbusinterface.cpp @@ -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 +#include + +#include "abstractproperty.h" + +unordered_map AbstractDBusInterface::interfaceMap; + +AbstractDBusInterface::AbstractDBusInterface(string interfaceName, string objectPath) +: mInterfaceName(interfaceName), mObjectPath(objectPath) +{ + mConnection = nullptr; + + interfaceMap[interfaceName] = this; + introspectionXml ="" ; + introspectionXml += ""; +} + +void AbstractDBusInterface::addProperty(AbstractProperty* property) +{ + string nameToLower = property->name(); + boost::algorithm::to_lower(nameToLower); + + ///see which properties are supported: + introspectionXml += "" + "" + " " + ""; + + properties[property->name()] = property; +} + +void AbstractDBusInterface::registerObject(GDBusConnection* connection) +{ + mConnection = connection; + + if(introspectionXml.empty()) + { + cerr<<"no interface to export: "<message< 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 index 0000000..ef8c8c1 --- /dev/null +++ b/plugins/dbus/abstractdbusinterface.h @@ -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 +#include +#include +#include +#include + +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 properties; + +private: + string mInterfaceName; + string mObjectPath; + string introspectionXml; + GDBusConnection * mConnection; + static unordered_map interfaceMap; +}; + +#endif // ABSTRACTDBUSINTERFACE_H diff --git a/plugins/dbus/abstractproperty.cpp b/plugins/dbus/abstractproperty.cpp new file mode 100644 index 0000000..111f2bd --- /dev/null +++ b/plugins/dbus/abstractproperty.cpp @@ -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 index 0000000..3830681 --- /dev/null +++ b/plugins/dbus/abstractproperty.h @@ -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 +#include +#include +#include + +#include "debugout.h" + +class AbstractDBusInterface; + +using namespace std; + +typedef function 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 + void setValue(T val) + { + mValue = val; + updateValue(); + } + + template + T value() + { + return boost::any_cast(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 index 0000000..c1802bf --- /dev/null +++ b/plugins/dbus/dbusinterfacemanager.cpp @@ -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 +#include + +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 index 0000000..54f42c9 --- /dev/null +++ b/plugins/dbus/dbusinterfacemanager.h @@ -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 diff --git a/plugins/dbus/dbusplugin.cpp b/plugins/dbus/dbusplugin.cpp index e69de29..adbdb06 100644 --- a/plugins/dbus/dbusplugin.cpp +++ b/plugins/dbus/dbusplugin.cpp @@ -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: "<(reply->value)<getPropertyAsync(velocityRequest); +} + + +PropertyList DBusSink::subscriptions() +{ + +} + +void DBusSink::supportedChanged(PropertyList supportedProperties) +{ + +} + +void DBusSink::propertyChanged(VehicleProperty::Property property, boost::any value, std::string uuid) +{ + DebugOut()<(value)<