2 Copyright (C) 2012 Intel Corporation
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.
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.
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
19 #include "abstractdbusinterface.h"
21 #include <boost/algorithm/string.hpp>
24 #include "abstractproperty.h"
26 unordered_map<string, AbstractDBusInterface*> AbstractDBusInterface::interfaceMap;
28 AbstractDBusInterface::AbstractDBusInterface(string interfaceName, string objectPath,
29 GDBusConnection* connection)
30 : mInterfaceName(interfaceName), mObjectPath(objectPath), mConnection(connection)
32 interfaceMap[interfaceName] = this;
36 void AbstractDBusInterface::addProperty(AbstractProperty* property)
38 string nameToLower = property->name();
39 boost::algorithm::to_lower<string>(nameToLower);
43 if(property->access() == AbstractProperty::Read)
45 else if(property->access() == AbstractProperty::Write)
47 else if(property->access() == AbstractProperty::ReadWrite)
49 else throw -1; //FIXME: don't throw
51 ///see which properties are supported:
52 introspectionXml += "<property type='"+ property->signature() + "' name='"+ property->name()+"' access='"+access+"' />"
53 "<signal name='" + property->name() + "' >"
54 " <arg type='"+ property->signature() + "' name='" + nameToLower + "' direction='out' />"
57 properties[property->name()] = property;
60 void AbstractDBusInterface::registerObject()
64 throw std::runtime_error("forgot to call setDBusConnection on AbstractDBusInterface");
67 if(introspectionXml.empty())
69 cerr<<"no interface to export: "<<mInterfaceName<<endl;
73 introspectionXml += "</interface>";
74 introspectionXml += "</node>";
78 GDBusNodeInfo* introspection = g_dbus_node_info_new_for_xml(introspectionXml.c_str(), &error);
82 cerr<<"Error in "<<__FILE__<<" - "<<__FUNCTION__<<":"<<__LINE__<<endl;
83 cerr<<error->message<<endl;
84 cerr<<"probably bad xml:"<<endl;
85 cerr<<introspectionXml<<endl;
89 GDBusInterfaceInfo* mInterfaceInfo = g_dbus_node_info_lookup_interface(introspection, mInterfaceName.c_str());
91 const GDBusInterfaceVTable vtable = { NULL, AbstractDBusInterface::getProperty, AbstractDBusInterface::setProperty };
93 regId = g_dbus_connection_register_object(mConnection, mObjectPath.c_str(), mInterfaceInfo, &vtable, NULL, NULL, &error);
100 void AbstractDBusInterface::unregisterObject()
102 g_dbus_connection_unregister_object(mConnection, regId);
105 void AbstractDBusInterface::updateValue(AbstractProperty *property)
107 if(mConnection == nullptr)
112 GError *error = NULL;
113 g_dbus_connection_emit_signal(mConnection, NULL, mObjectPath.c_str(), mInterfaceName.c_str(), property->name().c_str(), g_variant_new("(v)",property->toGVariant()), &error);
121 void AbstractDBusInterface::startRegistration()
124 introspectionXml ="<node>" ;
125 introspectionXml += "<interface name='"+ mInterfaceName + "' >";
128 GVariant* AbstractDBusInterface::getProperty(GDBusConnection* connection, const gchar* sender, const gchar* objectPath, const gchar* interfaceName, const gchar* propertyName, GError** error, gpointer userData)
131 if(interfaceMap.count(interfaceName))
133 GVariant* value = interfaceMap[interfaceName]->getProperty(propertyName);
136 debugOut("No interface for" + string(interfaceName));
140 gboolean AbstractDBusInterface::setProperty(GDBusConnection* connection, const gchar* sender, const gchar* objectPath, const gchar* interfaceName, const gchar* propertyName, GVariant* value, GError** error, gpointer userData)
142 if(interfaceMap.count(interfaceName))
144 interfaceMap[interfaceName]->setProperty(propertyName, value);
151 void AbstractDBusInterface::setProperty(string propertyName, GVariant *value)
153 if(properties.count(propertyName))
155 properties[propertyName]->fromGVariant(value);
163 GVariant *AbstractDBusInterface::getProperty(string propertyName)
165 if(properties.count(propertyName))
166 return properties[propertyName]->toGVariant();