sdbus-c++  0.8.3
High-level C++ D-Bus library based on systemd D-Bus implementation
StandardInterfaces.h
Go to the documentation of this file.
1 
27 #ifndef SDBUS_CXX_STANDARDINTERFACES_H_
28 #define SDBUS_CXX_STANDARDINTERFACES_H_
29 
30 #include <sdbus-c++/IObject.h>
31 #include <sdbus-c++/IProxy.h>
32 #include <sdbus-c++/Types.h>
33 #include <string>
34 #include <map>
35 #include <vector>
36 
37 namespace sdbus {
38 
39  // Proxy for peer
40  class Peer_proxy
41  {
42  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Peer";
43 
44  protected:
46  : proxy_(proxy)
47  {
48  }
49 
50  ~Peer_proxy() = default;
51 
52  public:
53  void Ping()
54  {
55  proxy_.callMethod("Ping").onInterface(INTERFACE_NAME);
56  }
57 
58  std::string GetMachineId()
59  {
60  std::string machineUUID;
61  proxy_.callMethod("GetMachineId").onInterface(INTERFACE_NAME).storeResultsTo(machineUUID);
62  return machineUUID;
63  }
64 
65  private:
66  sdbus::IProxy& proxy_;
67  };
68 
69  // Proxy for introspection
71  {
72  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Introspectable";
73 
74  protected:
76  : proxy_(proxy)
77  {
78  }
79 
80  ~Introspectable_proxy() = default;
81 
82  public:
83  std::string Introspect()
84  {
85  std::string xml;
86  proxy_.callMethod("Introspect").onInterface(INTERFACE_NAME).storeResultsTo(xml);
87  return xml;
88  }
89 
90  private:
91  sdbus::IProxy& proxy_;
92  };
93 
94  // Proxy for properties
96  {
97  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
98 
99  protected:
101  : proxy_(proxy)
102  {
103  proxy_
104  .uponSignal("PropertiesChanged")
105  .onInterface(INTERFACE_NAME)
106  .call([this]( const std::string& interfaceName
107  , const std::map<std::string, sdbus::Variant>& changedProperties
108  , const std::vector<std::string>& invalidatedProperties )
109  {
110  this->onPropertiesChanged(interfaceName, changedProperties, invalidatedProperties);
111  });
112  }
113 
114  ~Properties_proxy() = default;
115 
116  virtual void onPropertiesChanged( const std::string& interfaceName
117  , const std::map<std::string, sdbus::Variant>& changedProperties
118  , const std::vector<std::string>& invalidatedProperties ) = 0;
119 
120  public:
121  sdbus::Variant Get(const std::string& interfaceName, const std::string& propertyName)
122  {
123  return proxy_.getProperty(propertyName).onInterface(interfaceName);
124  }
125 
126  void Set(const std::string& interfaceName, const std::string& propertyName, const sdbus::Variant& value)
127  {
128  proxy_.setProperty(propertyName).onInterface(interfaceName).toValue(value);
129  }
130 
131  std::map<std::string, sdbus::Variant> GetAll(const std::string& interfaceName)
132  {
133  std::map<std::string, sdbus::Variant> props;
134  proxy_.callMethod("GetAll").onInterface(INTERFACE_NAME).withArguments(interfaceName).storeResultsTo(props);
135  return props;
136  }
137 
138  private:
139  sdbus::IProxy& proxy_;
140  };
141 
142  // Proxy for object manager
144  {
145  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
146 
147  protected:
149  : proxy_(proxy)
150  {
151  proxy_
152  .uponSignal("InterfacesAdded")
153  .onInterface(INTERFACE_NAME)
154  .call([this]( const sdbus::ObjectPath& objectPath
155  , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties )
156  {
157  this->onInterfacesAdded(objectPath, interfacesAndProperties);
158  });
159 
160  proxy_
161  .uponSignal("InterfacesRemoved")
162  .onInterface(INTERFACE_NAME)
163  .call([this]( const sdbus::ObjectPath& objectPath
164  , const std::vector<std::string>& interfaces )
165  {
166  this->onInterfacesRemoved(objectPath, interfaces);
167  });
168  }
169 
170  ~ObjectManager_proxy() = default;
171 
172  virtual void onInterfacesAdded( const sdbus::ObjectPath& objectPath
173  , const std::map<std::string, std::map<std::string, sdbus::Variant>>& interfacesAndProperties) = 0;
174  virtual void onInterfacesRemoved( const sdbus::ObjectPath& objectPath
175  , const std::vector<std::string>& interfaces) = 0;
176 
177  public:
178  std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> GetManagedObjects()
179  {
180  std::map<sdbus::ObjectPath, std::map<std::string, std::map<std::string, sdbus::Variant>>> objectsInterfacesAndProperties;
181  proxy_.callMethod("GetManagedObjects").onInterface(INTERFACE_NAME).storeResultsTo(objectsInterfacesAndProperties);
182  return objectsInterfacesAndProperties;
183  }
184 
185  private:
186  sdbus::IProxy& proxy_;
187  };
188 
189  // Adaptors for the above-listed standard D-Bus interfaces are not necessary because the functionality
190  // is provided by underlying libsystemd implementation. The exception is Properties_adaptor and
191  // ObjectManager_adaptor, which provide convenience functionality to emit signals.
192 
193  // Adaptor for properties
195  {
196  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.Properties";
197 
198  protected:
200  : object_(object)
201  {
202  }
203 
204  ~Properties_adaptor() = default;
205 
206  public:
207  void emitPropertiesChangedSignal(const std::string& interfaceName, const std::vector<std::string>& properties)
208  {
209  object_.emitPropertiesChangedSignal(interfaceName, properties);
210  }
211 
212  void emitPropertiesChangedSignal(const std::string& interfaceName)
213  {
214  object_.emitPropertiesChangedSignal(interfaceName);
215  }
216 
217  private:
218  sdbus::IObject& object_;
219  };
220 
221  // Adaptor for object manager
223  {
224  static constexpr const char* INTERFACE_NAME = "org.freedesktop.DBus.ObjectManager";
225 
226  protected:
228  : object_(object)
229  {
230  object_.addObjectManager();
231  }
232 
233  ~ObjectManager_adaptor() = default;
234 
235  public:
236  void emitInterfacesAddedSignal()
237  {
238  object_.emitInterfacesAddedSignal();
239  }
240 
241  void emitInterfacesAddedSignal(const std::vector<std::string>& interfaces)
242  {
243  object_.emitInterfacesAddedSignal(interfaces);
244  }
245 
246  void emitInterfacesRemovedSignal()
247  {
248  object_.emitInterfacesRemovedSignal();
249  }
250 
251  void emitInterfacesRemovedSignal(const std::vector<std::string>& interfaces)
252  {
253  object_.emitInterfacesRemovedSignal(interfaces);
254  }
255 
256  private:
257  sdbus::IObject& object_;
258  };
259 
260 }
261 
262 #endif /* SDBUS_CXX_STANDARDINTERFACES_H_ */
sdbus::IProxy
Definition: IProxy.h:63
sdbus::Introspectable_proxy
Definition: StandardInterfaces.h:70
Types.h
IProxy.h
IObject.h
sdbus::IProxy::uponSignal
SignalSubscriber uponSignal(const std::string &signalName)
Registers signal handler for a given signal of the proxied D-Bus object.
Definition: IProxy.h:342
sdbus::IProxy::setProperty
PropertySetter setProperty(const std::string &propertyName)
Sets value of a property of the proxied D-Bus object.
Definition: IProxy.h:352
sdbus::IObject
Definition: IObject.h:59
sdbus::IObject::emitInterfacesAddedSignal
virtual void emitInterfacesAddedSignal()=0
Emits InterfacesAdded signal on this object path.
sdbus::IProxy::getProperty
PropertyGetter getProperty(const std::string &propertyName)
Gets value of a property of the proxied D-Bus object.
Definition: IProxy.h:347
sdbus::IObject::emitInterfacesRemovedSignal
virtual void emitInterfacesRemovedSignal()=0
Emits InterfacesRemoved signal on this object path.
sdbus::IObject::emitPropertiesChangedSignal
virtual void emitPropertiesChangedSignal(const std::string &interfaceName, const std::vector< std::string > &propNames)=0
Emits PropertyChanged signal for specified properties under a given interface of this object path.
sdbus::ObjectManager_proxy
Definition: StandardInterfaces.h:143
sdbus::Properties_adaptor
Definition: StandardInterfaces.h:194
sdbus::Variant
Definition: Types.h:53
sdbus::ObjectManager_adaptor
Definition: StandardInterfaces.h:222
sdbus::IObject::addObjectManager
virtual void addObjectManager()=0
Adds an ObjectManager interface at the path of this D-Bus object.
sdbus::IProxy::callMethod
virtual MethodReply callMethod(const MethodCall &message, uint64_t timeout=0)=0
Calls method on the proxied D-Bus object.
sdbus::ObjectPath
Definition: Types.h:152
sdbus::Peer_proxy
Definition: StandardInterfaces.h:40
sdbus::Properties_proxy
Definition: StandardInterfaces.h:95