automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
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 
33 const uint getPid(const char *owner);
34 
35 typedef std::vector<VariantType*> PropertyDBusMap;
36 
38 {
39 
40 public:
41  AbstractDBusInterface(std::string interfaceName, std::string objectName, GDBusConnection* connection);
42 
43  virtual ~AbstractDBusInterface();
44 
45  void setDBusConnection(GDBusConnection* connection)
46  {
47  mConnection = connection;
48  }
49 
50  void registerObject();
51  void unregisterObject();
52 
53  void addProperty(VariantType* property);
54  virtual void updateValue(VariantType* property);
55 
56  static PropertyList implementedProperties() { return mimplementedProperties; }
57 
58  static std::list<AbstractDBusInterface *> getObjectsForProperty(std::string property);
59 
60  static list<AbstractDBusInterface*> interfaces();
61 
62  static std::vector<string> supportedInterfaces();
63 
64  std::string interfaceName() { return mInterfaceName; }
65  void setInterfaceName(const std::string & ifaceName)
66  {
67  mInterfaceName = ifaceName;
68  }
69 
70  bool implementsProperty(std::string property);
71 
77  bool hasPropertyDBus(std::string attributeName)
78  {
79 
80  for(auto i : propertyDBusMap)
81  {
82  if(i->name() == attributeName)
83  {
84  return true;
85  }
86  }
87 
88  return false;
89  }
90 
91  std::string objectPath() { return mObjectPath; }
92 
93  bool isSupported() { return supported; }
94 
95  double time() { return mTime; }
96 
97  VariantType* property(std::string propertyName)
98  {
99  if(properties.find(propertyName) != properties.end())
100  return properties[propertyName];
101  return nullptr;
102  }
103 
105 
106  void setObjectPath(std::string op)
107  {
108  if(objectMap.find(op) != objectMap.end())
109  objectMap.erase(op);
110 
111  mObjectPath = op;
112  objectMap[mObjectPath] = this;
113  }
114 
115  std::string objectName() { return mPropertyName; }
116 
117  Zone::Type zone() { return zoneFilter; }
118 
119  std::string source() { return mSourceFilter; }
120 
121  std::unordered_map<std::string, VariantType*> getProperties() { return properties; }
122 
123  bool isRegistered() { return regId > 0; }
124 
125 protected:
126 
127  void startRegistration();
128 
129  static GVariant *getProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
130  const gchar *interfaceName, const gchar * propertyName, GError** error,
131  gpointer userData);
132  static gboolean setProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
133  const gchar *interfaceName, const gchar * propertyName, GVariant *value,
134  GError** error, gpointer userData, std::function<void (bool, AsyncPropertyReply::Error)> callback);
135 
136  static void handleMyMethodCall(GDBusConnection *connection, const gchar *sender, const gchar *object_path,
137  const gchar *interface_name, const gchar *method_name, GVariant *parameters,
138  GDBusMethodInvocation *invocation, gpointer user_data);
139 
140  virtual void setProperty(std::string propertyName, GVariant * value, std::function<void (bool, AsyncPropertyReply::Error)> callback);
141  virtual GVariant * getProperty(std::string propertyName);
142 
143  void setTimeout(int timeout);
144 
145  std::unordered_map<std::string, VariantType*> properties;
146 
147  Zone::Type zoneFilter;
148 
149  PropertyDBusMap propertyDBusMap;
150 
151  bool supported;
152  double mTime;
153  std::string mSourceFilter;
154 
155 private:
156  std::string mInterfaceName;
157  std::string mObjectPath;
158  std::string mPropertyName;
159  std::string introspectionXml;
160  GDBusConnection * mConnection;
161  static std::unordered_map<std::string, AbstractDBusInterface*> objectMap;
162  static PropertyList mimplementedProperties;
163  guint regId;
164  int mTimeout;
165 
166 };
167 
168 #endif // ABSTRACTDBUSINTERFACE_H