033bb4db508c344b302a481da65fa47d81055f08
[profile/ivi/automotive-message-broker.git] / plugins / dbus / abstractdbusinterface.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
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.
8
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.
13
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
17 */
18
19 #ifndef ABSTRACTDBUSINTERFACE_H
20 #define ABSTRACTDBUSINTERFACE_H
21
22 #include <string>
23 #include <functional>
24 #include <unordered_map>
25 #include <list>
26 #include <gio/gio.h>
27 #include <boost/any.hpp>
28 #include <nullptr.h>
29 #include "abstractpropertytype.h"
30 #include <abstractroutingengine.h>
31 #include "varianttype.h"
32 #include "dbussignaller.h"
33
34 const uint getPid(const char *owner);
35
36 typedef std::vector<VariantType*> PropertyDBusMap;
37
38 class AbstractDBusInterface
39 {
40
41 public:
42         AbstractDBusInterface(std::string interfaceName, std::string objectName, GDBusConnection* connection);
43
44         virtual ~AbstractDBusInterface();
45
46         void setDBusConnection(GDBusConnection* connection)
47         {
48                 mConnection = connection;
49         }
50
51         void registerObject();
52         void unregisterObject();
53
54         void addProperty(AbstractProperty* property);
55         virtual void updateValue(AbstractProperty* property);
56
57         static PropertyList implementedProperties() { return mimplementedProperties; }
58
59         static std::list<AbstractDBusInterface *> getObjectsForProperty(std::string property);
60
61         static list<AbstractDBusInterface*> interfaces();
62
63         static std::vector<string> supportedInterfaces();
64
65         std::string interfaceName() { return mInterfaceName; }
66
67         bool implementsProperty(std::string property);
68
69         /*!
70          * \brief hasPropertyDBus
71          * \param attributeName, name of DBus property
72          * \return true if attributeName is supported by this interface
73          */
74         bool hasPropertyDBus(std::string attributeName)
75         {
76
77                 for(auto i : propertyDBusMap)
78                 {
79                         if(i->dbusName() == attributeName)
80                         {
81                                 return true;
82                         }
83                 }
84
85                 return false;
86         }
87
88         std::string objectPath() { return mObjectPath; }
89
90         bool isSupported() { return supported; }
91
92         double time() { return mTime; }
93
94         AbstractProperty* property(std::string propertyName)
95         {
96                 if(properties.find(propertyName) != properties.end())
97                         return properties[propertyName];
98                 return nullptr;
99         }
100
101         AbstractRoutingEngine* re;
102
103         void setObjectPath(std::string op)
104         {
105                 if(objectMap.find(op) != objectMap.end())
106                         objectMap.erase(op);
107
108                 mObjectPath = op;
109                 objectMap[mObjectPath] = this;
110         }
111
112         std::string objectName() { return mPropertyName; }
113
114         Zone::Type zone() { return zoneFilter; }
115
116         std::string source() { return mSourceFilter; }
117
118         std::unordered_map<std::string, AbstractProperty*> getProperties() { return properties; }
119
120         bool isRegistered() { return regId > 0; }
121
122 protected:
123
124         void startRegistration();
125
126         static GVariant *getProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
127                                                                  const gchar *interfaceName, const gchar * propertyName, GError** error,
128                                                                  gpointer userData);
129         static gboolean setProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
130                                                                 const gchar *interfaceName, const gchar * propertyName, GVariant *value,
131                                                                 GError** error, gpointer userData);
132
133         virtual void setProperty(std::string propertyName, GVariant * value);
134         virtual GVariant * getProperty(std::string propertyName);
135
136         void setTimeout(int timeout);
137
138         std::unordered_map<std::string, AbstractProperty*> properties;
139
140         Zone::Type zoneFilter;
141
142         PropertyDBusMap propertyDBusMap;
143
144         bool supported;
145         double mTime;
146         std::string mSourceFilter;
147
148 private:
149         std::string mInterfaceName;
150         std::string mObjectPath;
151         std::string mPropertyName;
152         std::string introspectionXml;
153         GDBusConnection * mConnection;
154         static std::unordered_map<std::string, AbstractDBusInterface*> objectMap;
155         static PropertyList mimplementedProperties;
156         guint regId;
157         int mTimeout;
158
159 };
160
161 #endif // ABSTRACTDBUSINTERFACE_H