updated dbus plugin
authorKevron Rees <kevron_m_rees@linux.intel.com>
Tue, 21 Aug 2012 21:29:42 +0000 (14:29 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Tue, 21 Aug 2012 21:29:42 +0000 (14:29 -0700)
16 files changed:
ambd/core.cpp
ambd/core.h
config
lib/abstractroutingengine.h
lib/vehicleproperty.h
plugins/dbus/CMakeLists.txt
plugins/dbus/abstractdbusinterface.cpp
plugins/dbus/abstractdbusinterface.h
plugins/dbus/abstractproperty.h
plugins/dbus/basicproperty.cpp [new file with mode: 0644]
plugins/dbus/basicproperty.h [new file with mode: 0644]
plugins/dbus/dbusinterfacemanager.cpp
plugins/dbus/dbusinterfacemanager.h
plugins/dbus/dbusplugin.cpp
plugins/dbus/dbusplugin.h
plugins/tcpsink/test/api.js

index db97d7c..ada8123 100644 (file)
@@ -46,7 +46,7 @@ void Core::setSupported(PropertyList supported, AbstractSource* source)
                        DebugOut()<<__FUNCTION__<<"() adding suport for property "<<VehicleProperty::name((*itr))<<endl;
                        mMasterPropertyList.push_back((*itr));
                }
-       }       
+       }
 }
 
 
@@ -99,7 +99,7 @@ void Core::updateProperty(VehicleProperty::Property property, boost::any value)
 {
        SinkList list = propertySinkMap[property];
        
-       DebugOut()<<__FUNCTION__<<"() there are "<<list.size()<<" sinks connected to property: "<<VehicleProperty::name(property)<<endl;
+       DebugOut()<<__FUNCTION__<<"() there are "<<list.size()<<" sinks connected to property: "<<property<<endl;
        
        for(SinkList::iterator itr = list.begin(); itr != list.end(); itr++)
        {
index 3be8e65..f8ac47c 100644 (file)
@@ -45,6 +45,7 @@ public:
        void setProperty(VehicleProperty::Property, boost::any);
        void subscribeToProperty(VehicleProperty::Property, AbstractSink* self);
        void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self);
+       PropertyList supported() { return mMasterPropertyList; }
     
 protected: 
        ~Core();
diff --git a/config b/config
index 757c65c..1f5911b 100644 (file)
--- a/config
+++ b/config
@@ -1,5 +1,5 @@
 {
        "sources" : [ "../plugins/examplesourceplugin.so" ],
-       "sinks": [ "../plugins/examplesinkplugin.so" ]
+       "sinks": [ "../plugins/examplesinkplugin.so", "../plugins/dbus/dbussinkplugin.so" ]
 }
 
index 627b803..a841d6d 100644 (file)
@@ -64,6 +64,7 @@ public:
        virtual void setProperty(VehicleProperty::Property, boost::any) = 0;
        virtual void subscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
        virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
+       virtual PropertyList supported() = 0;
 };
 
 #endif // ABSTRACTROUTINGENGINE_H
index c324b6c..921f163 100644 (file)
@@ -50,9 +50,12 @@ public:
 
                ThrottlePosition, /**< Throttle position 0-100% */
                WheelBrake, /**< Wheel brake position.  Engaged = true, Idle = false */
-               SteeringWheelAngle, /**< Steering wheel angle (0-360) */
+               SteeringWheelAngle, /**< Steering wheel angle (0-359) */
                TurnSignal, /**< 0=off, 1=right, 2=left, 3=hazard */
-               MachineGunTurretStatus /**< 0=off, 1=on */
+               MachineGunTurretStatus, /**< 0=off, 1=on */
+               AccelerationX, /**< Acceleration on the 'x' axis in 1/1000 gravitational acceleration "g-force" */
+               AccelerationY, /**< Acceleration on the 'y' axis in 1/1000 gravitational acceleration "g-force" */
+               AccelerationZ /**< Acceleration on the 'z' axis in 1/1000 gravitational acceleration "g-force" */
        };
 
        static std::string name(Property prop);
index d83a28b..a688c79 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)
-set(dbussinkplugin_sources dbusplugin.cpp abstractproperty.cpp abstractdbusinterface.cpp dbusinterfacemanager.cpp)
+set(dbussinkplugin_headers dbusplugin.h abstractproperty.h abstractdbusinterface.h dbusinterfacemanager.h accelerationproperty.h basicproperty.h)
+set(dbussinkplugin_sources dbusplugin.cpp abstractproperty.cpp abstractdbusinterface.cpp dbusinterfacemanager.cpp accelerationproperty.cpp basicproperty.cpp)
 
 add_library(dbussinkplugin MODULE ${dbussinkplugin_sources})
 set_target_properties(dbussinkplugin PROPERTIES PREFIX "")
index fba0e3a..9ee9d81 100644 (file)
@@ -25,11 +25,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 unordered_map<string, AbstractDBusInterface*> AbstractDBusInterface::interfaceMap;
 
-AbstractDBusInterface::AbstractDBusInterface(string interfaceName, string objectPath)
-: mInterfaceName(interfaceName), mObjectPath(objectPath)
+AbstractDBusInterface::AbstractDBusInterface(string interfaceName, string objectPath,
+                                                                                        GDBusConnection* connection)
+       : mInterfaceName(interfaceName), mObjectPath(objectPath), mConnection(connection)
 {
-       mConnection = nullptr;
-
        interfaceMap[interfaceName] = this;
        introspectionXml ="<node>" ;
        introspectionXml += "<interface name='"+ interfaceName + "' >";
@@ -40,8 +39,18 @@ void AbstractDBusInterface::addProperty(AbstractProperty* property)
        string nameToLower = property->name();
        boost::algorithm::to_lower<string>(nameToLower);
        
+       string access;
+
+       if(property->access() == AbstractProperty::Read)
+               access = "read";
+       else if(property->access() == AbstractProperty::Write)
+               access = "write";
+       else if(property->access() == AbstractProperty::ReadWrite)
+               access = "readwrite";
+       else throw -1; //FIXME: don't throw
+
        ///see which properties are supported:
-       introspectionXml +=     "<property type='"+ property->signature() + "' name='"+ property->name()+"' access='read' />"
+       introspectionXml +=     "<property type='"+ property->signature() + "' name='"+ property->name()+"' access='"+access+"' />"
        "<signal name='" + property->name() + "' >"
        "       <arg type='"+ property->signature() + "' name='" + nameToLower + "' direction='out' />"
        "</signal>";
@@ -49,9 +58,12 @@ void AbstractDBusInterface::addProperty(AbstractProperty* property)
        properties[property->name()] = property;
 }
 
-void AbstractDBusInterface::registerObject(GDBusConnection* connection)
+void AbstractDBusInterface::registerObject()
 {
-       mConnection = connection;
+       if(!mConnection)
+       {
+               throw std::runtime_error("forgot to call setDBusConnection on AbstractDBusInterface");
+       }
        
        if(introspectionXml.empty())
        {
@@ -79,7 +91,7 @@ void AbstractDBusInterface::registerObject(GDBusConnection* connection)
        
        const GDBusInterfaceVTable vtable = { NULL, AbstractDBusInterface::getProperty, AbstractDBusInterface::setProperty };
        
-       guint regId = g_dbus_connection_register_object(connection, mObjectPath.c_str(), mInterfaceInfo, &vtable, NULL, NULL, &error);
+       guint regId = g_dbus_connection_register_object(mConnection, mObjectPath.c_str(), mInterfaceInfo, &vtable, NULL, NULL, &error);
        
        if(error) throw -1;
        
@@ -125,3 +137,23 @@ gboolean AbstractDBusInterface::setProperty(GDBusConnection* connection, const g
        return false;
 }
 
+void AbstractDBusInterface::setProperty(string propertyName, GVariant *value)
+{
+       if(properties.count(propertyName))
+       {
+               properties[propertyName]->fromGVariant(value);
+       }
+       else
+       {
+               throw -1;
+       }
+}
+
+GVariant *AbstractDBusInterface::getProperty(string propertyName)
+{
+       if(properties.count(propertyName))
+               return properties[propertyName]->toGVariant();
+       else
+               throw -1;
+}
+
index ef8c8c1..b786e4f 100644 (file)
@@ -33,10 +33,15 @@ class AbstractDBusInterface
 {
 
 public:
-       AbstractDBusInterface(string interfaceName, string objectPath);
+       AbstractDBusInterface(string interfaceName, string objectPath, GDBusConnection* connection);
 
        
-       void registerObject(GDBusConnection* connection);
+       void setDBusConnection(GDBusConnection* connection)
+       {
+               mConnection = connection;
+       }
+
+       void registerObject();
        
        void addProperty(AbstractProperty* property);
        virtual void updateValue(AbstractProperty* property);
@@ -50,8 +55,8 @@ protected:
                                                                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;
+       virtual void setProperty(string propertyName, GVariant * value);
+       virtual GVariant * getProperty(string propertyName);
        
        unordered_map<string, AbstractProperty*> properties;
 
index 3830681..8feae3e 100644 (file)
@@ -68,6 +68,11 @@ public:
        virtual GVariant* toGVariant() = 0;
        virtual void fromGVariant(GVariant *value) = 0;
 
+       void setValue(boost::any val)
+       {
+               mValue = val;
+       }
+
        template<typename T>
        void setValue(T val)
        {
diff --git a/plugins/dbus/basicproperty.cpp b/plugins/dbus/basicproperty.cpp
new file mode 100644 (file)
index 0000000..a778026
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+       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 "basicproperty.h"
+
+
+
+
+
diff --git a/plugins/dbus/basicproperty.h b/plugins/dbus/basicproperty.h
new file mode 100644 (file)
index 0000000..818cd08
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+       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 _BASICPROPERTY_H_
+#define _BASICPROPERTY_H_
+
+#include "abstractproperty.h"
+
+template <typename T>
+class BasicProperty: public AbstractProperty
+{
+public:
+       BasicProperty(string propertyName, string signature, Access access, AbstractDBusInterface *interface)
+               :AbstractProperty(propertyName,signature,access,interface)
+       {
+
+       }
+
+       void setValue(T val)
+       {
+               AbstractProperty::setValue<T>(val);
+       }
+
+       T value()
+       {
+               return AbstractProperty::value<T>();
+       }
+
+       virtual GVariant* toGVariant()
+       {
+               return g_variant_new(signature().c_str(), value());
+       }
+
+       virtual void fromGVariant(GVariant *value)
+       {
+
+       }
+};
+
+#endif
index c1802bf..84e2f06 100644 (file)
@@ -17,15 +17,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
 #include "dbusinterfacemanager.h"
+
+
 #include <gio/gio.h>
 #include <string>
 
+///properties:
+#include "accelerationproperty.h"
+
 using namespace std;
 
 static void
 on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
 {
-       //AbstractDBusInterface::iface()->registerObject(connection);
+       DBusInterfaceManager* iface = static_cast<DBusInterfaceManager*>(user_data);
+       AbstractDBusInterface* acceleration = new AccelerationPropertyInterface(iface->re, connection);
+
 }
 
 static void
@@ -40,7 +47,8 @@ on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data
 
 
 
-DBusInterfaceManager::DBusInterfaceManager()
+DBusInterfaceManager::DBusInterfaceManager(AbstractRoutingEngine* engine)
+       :re(engine)
 {
        g_type_init();
 
index 54f42c9..45804fe 100644 (file)
@@ -20,18 +20,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #ifndef DBUSINTERFACEMANAGER_H
 #define DBUSINTERFACEMANAGER_H
 
+class AbstractRoutingEngine;
+
 class DBusInterfaceManager
 {
 
 public:
-       DBusInterfaceManager();
+       DBusInterfaceManager(AbstractRoutingEngine* re);
        ~DBusInterfaceManager();
-    
+
+       AbstractRoutingEngine* re;
     
 private:
        
        unsigned int ownerId;
 
+
 };
 
 #endif // DBUSINTERFACEMANAGER_H
index adbdb06..f39b76e 100644 (file)
@@ -17,8 +17,9 @@
 */
 
 
-#include "dbusplugin.h".h"
+#include "dbusplugin.h"
 #include "abstractroutingengine.h"
+#include "dbusinterfacemanager.h"
 #include "debugout.h"
 
 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine)
@@ -26,35 +27,26 @@ 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()
+DBusSink::DBusSink(AbstractRoutingEngine* engine)
+       : AbstractSink(engine)
 {
 
 }
 
-void DBusSink::supportedChanged(PropertyList supportedProperties)
+void DBusSink::propertyChanged(VehicleProperty::Property property, boost::any value, string uuid)
 {
+       AbstractProperty* prop = propertyDBusMap[property];
 
+       prop->setValue(value);
 }
 
-void DBusSink::propertyChanged(VehicleProperty::Property property, boost::any value, std::string uuid)
+std::string DBusSink::uuid()
 {
-       DebugOut()<<VehicleProperty::name(property)<<" value: "<<boost::any_cast<uint16_t>(value)<<endl;
+       return "c2e6cafa-eef5-4b8a-99a0-0f2c9be1057d";
 }
 
-std::string DBusSink::uuid()
+DBusSinkManager::DBusSinkManager(AbstractRoutingEngine *engine)
+       :AbstractSinkManager(engine)
 {
-       return "c2e6cafa-eef5-4b8a-99a0-0f2c9be1057d";
+       DBusInterfaceManager* manager = new DBusInterfaceManager(engine);
 }
index 36d9c6d..abb7416 100644 (file)
@@ -1,24 +1,47 @@
+/*
+       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 DBUSSINK_H_
+#define DBUSSINK_H_
+
 #include "abstractsink.h"
+#include "abstractproperty.h"
+#include <map>
 
+typedef std::map<VehicleProperty::Property, AbstractProperty*> PropertyDBusMap;
 
 class DBusSink : public AbstractSink
 {
 
 public:
        DBusSink(AbstractRoutingEngine* engine);
-       virtual PropertyList subscriptions();
-       virtual void supportedChanged(PropertyList supportedProperties);
+       virtual void supportedChanged(PropertyList supportedProperties) = 0;
        virtual void propertyChanged(VehicleProperty::Property property, boost::any value, std::string uuid);
        virtual std::string uuid();
+
+protected:
+       PropertyDBusMap propertyDBusMap;
 };
 
 class DBusSinkManager: public AbstractSinkManager
 {
 public:
-       DBusSinkManager(AbstractRoutingEngine* engine)
-       :AbstractSinkManager(engine)
-       {
-               new DBusSink(engine);
-       }
+       DBusSinkManager(AbstractRoutingEngine* engine);
 };
 
+#endif
index fbc1151..3958619 100644 (file)
@@ -50,7 +50,7 @@ function Vehicle()
         {\r
             PRINT.pass("The browser is websocket capable");\r
 \r
-            this.socket = new WebSocket("ws://localhost:23000/echo");\r
+            this.socket = new WebSocket("ws://localhost:7681");\r
             this.socket.onopen = function()\r
             {\r
                 PRINT.pass("Connection OPEN");\r
@@ -60,10 +60,13 @@ function Vehicle()
             {\r
                 self.receive(e.data);\r
             };\r
-            this.socket.onclose = function()\r
+            this.socket.onclose = function(e)\r
             {\r
-                PRINT.fail("Connection CLOSED");\r
+                PRINT.fail("Connection CLOSED: " + e.reason + " code: " + e.code);\r
             };\r
+            this.socket.onerror = function(evt) {\r
+               alert(evt);\r
+            }\r
         }\r
         else\r
         {\r