xref: /openbmc/boost-dbus/include/dbus/properties.hpp (revision e62be32939e67e9ce2e982015d8875a21cb8d435)
1377e76abSEd Tanous #include <dbus/connection.hpp>
2377e76abSEd Tanous #include <dbus/filter.hpp>
3377e76abSEd Tanous #include <dbus/match.hpp>
4377e76abSEd Tanous #include <functional>
5377e76abSEd Tanous #include <tuple>
6377e76abSEd Tanous #include <type_traits>
7377e76abSEd Tanous #include <boost/algorithm/string/predicate.hpp>
8377e76abSEd Tanous #include <boost/container/flat_map.hpp>
9377e76abSEd Tanous #include <boost/container/flat_set.hpp>
10377e76abSEd Tanous 
11377e76abSEd Tanous namespace dbus {
12377e76abSEd Tanous struct DbusArgument {
13377e76abSEd Tanous   DbusArgument(const std::string& direction, const std::string& name,
14377e76abSEd Tanous                const std::string& type)
15377e76abSEd Tanous       : direction(direction), name(name), type(type){};
16377e76abSEd Tanous   std::string direction;
17377e76abSEd Tanous   std::string name;
18377e76abSEd Tanous   std::string type;
19377e76abSEd Tanous };
20377e76abSEd Tanous 
21377e76abSEd Tanous class DbusMethod {
22377e76abSEd Tanous  public:
23e3b0bf5bSEd Tanous   DbusMethod(const std::string& name, std::shared_ptr<dbus::connection>& conn)
24377e76abSEd Tanous       : name(name), conn(conn){};
25377e76abSEd Tanous   virtual void call(dbus::message& m){};
26377e76abSEd Tanous   virtual std::vector<DbusArgument> get_args() { return {}; };
27377e76abSEd Tanous   std::string name;
28377e76abSEd Tanous   std::shared_ptr<dbus::connection> conn;
29377e76abSEd Tanous };
30377e76abSEd Tanous 
31377e76abSEd Tanous enum class UpdateType { VALUE_CHANGE_ONLY, FORCE };
32377e76abSEd Tanous 
33377e76abSEd Tanous // Base case for when I == the size of the tuple args.  Does nothing, as we
34377e76abSEd Tanous // should be done
357a440136SEd Tanous template <std::size_t TupleIndex = 0, typename... Tp>
367a440136SEd Tanous inline typename std::enable_if<TupleIndex == sizeof...(Tp), void>::type
377a440136SEd Tanous arg_types(bool in, std::tuple<Tp...>& t, std::vector<DbusArgument>& v,
38e3b0bf5bSEd Tanous           const std::vector<std::string>* arg_names = nullptr) {}
39377e76abSEd Tanous 
40377e76abSEd Tanous // Case for when I < the size of tuple args.  Unpacks the tuple type into the
41377e76abSEd Tanous // dbusargument object and names it appropriately.
427a440136SEd Tanous template <std::size_t TupleIndex = 0, typename... Tp>
43377e76abSEd Tanous     inline typename std::enable_if <
447a440136SEd Tanous     TupleIndex<sizeof...(Tp), void>::type arg_types(
45e3b0bf5bSEd Tanous         bool in, std::tuple<Tp...>& t, std::vector<DbusArgument>& v,
46e3b0bf5bSEd Tanous         const std::vector<std::string>* arg_names = nullptr) {
477a440136SEd Tanous   typedef typename std::tuple_element<TupleIndex, std::tuple<Tp...>>::type
487a440136SEd Tanous       element_type;
49377e76abSEd Tanous   auto constexpr sig = element_signature<element_type>::code;
50e3b0bf5bSEd Tanous   std::string name;
51e3b0bf5bSEd Tanous   std::string direction;
527a440136SEd Tanous   if (arg_names == nullptr || arg_names->size() <= TupleIndex) {
53377e76abSEd Tanous     if (in) {
547a440136SEd Tanous       name = "arg_" + std::to_string(TupleIndex);
55377e76abSEd Tanous     } else {
567a440136SEd Tanous       name = "out_" + std::to_string(TupleIndex);
57377e76abSEd Tanous     }
58e3b0bf5bSEd Tanous   } else {
597a440136SEd Tanous     name = (*arg_names)[TupleIndex];
60e3b0bf5bSEd Tanous   }
61e3b0bf5bSEd Tanous   v.emplace_back(in ? "in" : "out", name, &sig[0]);
62e3b0bf5bSEd Tanous 
637a440136SEd Tanous   arg_types<TupleIndex + 1, Tp...>(in, t, v, arg_names);
64e3b0bf5bSEd Tanous }
65e3b0bf5bSEd Tanous 
66e3b0bf5bSEd Tanous // Special case for handling raw arguments returned from handlers.  Because they
67e3b0bf5bSEd Tanous // don't get stored in a tuple, special handling is neccesary
68e3b0bf5bSEd Tanous template <typename Element>
69e3b0bf5bSEd Tanous void arg_types(bool in, Element& t, std::vector<DbusArgument>& v,
70e3b0bf5bSEd Tanous                const std::vector<std::string>* arg_names = nullptr) {
71e3b0bf5bSEd Tanous   auto constexpr sig = element_signature<Element>::code;
72e3b0bf5bSEd Tanous   std::string name;
73e3b0bf5bSEd Tanous   if (arg_names == nullptr || arg_names->size() < 1) {
74e3b0bf5bSEd Tanous     name.assign("arg_0");
75e3b0bf5bSEd Tanous   } else {
76e3b0bf5bSEd Tanous     name = (*arg_names)[0];
77e3b0bf5bSEd Tanous   }
78e3b0bf5bSEd Tanous 
79e3b0bf5bSEd Tanous   v.emplace_back(in ? "in" : "out", name, &sig[0]);
80377e76abSEd Tanous }
81377e76abSEd Tanous 
82377e76abSEd Tanous template <typename Handler>
83377e76abSEd Tanous class LambdaDbusMethod : public DbusMethod {
84377e76abSEd Tanous  public:
85377e76abSEd Tanous   typedef function_traits<Handler> traits;
86377e76abSEd Tanous   typedef typename traits::decayed_arg_types InputTupleType;
87e3b0bf5bSEd Tanous   typedef typename traits::result_type ResultType;
88e3b0bf5bSEd Tanous   LambdaDbusMethod(const std::string name,
89e3b0bf5bSEd Tanous                    std::shared_ptr<dbus::connection>& conn, Handler h)
90e3b0bf5bSEd Tanous       : DbusMethod(name, conn), h(std::move(h)) {
91e3b0bf5bSEd Tanous     InputTupleType t;
92e3b0bf5bSEd Tanous     arg_types(true, t, args);
93e3b0bf5bSEd Tanous 
94e3b0bf5bSEd Tanous     ResultType o;
95e3b0bf5bSEd Tanous     arg_types(false, o, args);
96e3b0bf5bSEd Tanous   }
97e3b0bf5bSEd Tanous 
98e3b0bf5bSEd Tanous   LambdaDbusMethod(const std::string& name,
99e3b0bf5bSEd Tanous                    const std::vector<std::string>& input_arg_names,
100e3b0bf5bSEd Tanous                    const std::vector<std::string>& output_arg_names,
101e3b0bf5bSEd Tanous                    std::shared_ptr<dbus::connection>& conn, Handler h)
102e3b0bf5bSEd Tanous       : DbusMethod(name, conn), h(std::move(h)) {
103e3b0bf5bSEd Tanous     InputTupleType t;
104e3b0bf5bSEd Tanous     arg_types(true, t, args, &input_arg_names);
105e3b0bf5bSEd Tanous 
106e3b0bf5bSEd Tanous     ResultType o;
107e3b0bf5bSEd Tanous     arg_types(false, o, args, &output_arg_names);
108e3b0bf5bSEd Tanous   }
109377e76abSEd Tanous   void call(dbus::message& m) override {
110377e76abSEd Tanous     InputTupleType input_args;
111a04118e8SEd Tanous     if (unpack_into_tuple(input_args, m) == false) {
112a04118e8SEd Tanous       auto err = dbus::message::new_error(m, DBUS_ERROR_INVALID_ARGS, "");
113a04118e8SEd Tanous       conn->send(err, std::chrono::seconds(0));
114a04118e8SEd Tanous       return;
115a04118e8SEd Tanous     }
116a04118e8SEd Tanous     try {
117*e62be329SEd Tanous       ResultType r = apply(h, input_args);
118377e76abSEd Tanous       auto ret = dbus::message::new_return(m);
119a04118e8SEd Tanous       if (pack_tuple_into_msg(r, ret) == false) {
120a04118e8SEd Tanous         auto err = dbus::message::new_error(
121a04118e8SEd Tanous             m, DBUS_ERROR_FAILED, "Handler had issue when packing response");
122a04118e8SEd Tanous         conn->send(err, std::chrono::seconds(0));
123a04118e8SEd Tanous         return;
124a04118e8SEd Tanous       }
125377e76abSEd Tanous       conn->send(ret, std::chrono::seconds(0));
126a04118e8SEd Tanous     } catch (...) {
127a04118e8SEd Tanous       auto err = dbus::message::new_error(
128a04118e8SEd Tanous           m, DBUS_ERROR_FAILED,
129a04118e8SEd Tanous           "Handler threw exception while handling request.");
130a04118e8SEd Tanous       conn->send(err, std::chrono::seconds(0));
131a04118e8SEd Tanous       return;
132a04118e8SEd Tanous     }
133377e76abSEd Tanous   };
134377e76abSEd Tanous 
135e3b0bf5bSEd Tanous   std::vector<DbusArgument> get_args() override { return args; };
136377e76abSEd Tanous   Handler h;
137e3b0bf5bSEd Tanous   std::vector<DbusArgument> args;
138377e76abSEd Tanous };
139377e76abSEd Tanous 
140377e76abSEd Tanous class DbusSignal {
141377e76abSEd Tanous  public:
142e3b0bf5bSEd Tanous   DbusSignal(){};
143e3b0bf5bSEd Tanous   virtual std::vector<DbusArgument> get_args() { return {}; }
144e3b0bf5bSEd Tanous };
145377e76abSEd Tanous 
146e3b0bf5bSEd Tanous template <typename... Args>
147e3b0bf5bSEd Tanous class DbusTemplateSignal : public DbusSignal {
148e3b0bf5bSEd Tanous  public:
149e3b0bf5bSEd Tanous   DbusTemplateSignal(const std::string& name, const std::string& object_name,
150e3b0bf5bSEd Tanous                      const std::string& interface_name,
151e3b0bf5bSEd Tanous                      const std::vector<std::string>& names,
152e3b0bf5bSEd Tanous                      std::shared_ptr<dbus::connection>& conn)
153e3b0bf5bSEd Tanous       : DbusSignal(),
154e3b0bf5bSEd Tanous         name(name),
155e3b0bf5bSEd Tanous         object_name(object_name),
156e3b0bf5bSEd Tanous         interface_name(interface_name),
157e3b0bf5bSEd Tanous         conn(conn) {
158e3b0bf5bSEd Tanous     std::tuple<Args...> tu;
159e3b0bf5bSEd Tanous     arg_types(true, tu, args, &names);
160e3b0bf5bSEd Tanous   };
161e3b0bf5bSEd Tanous 
162e3b0bf5bSEd Tanous   void send(Args&...) {
163e3b0bf5bSEd Tanous     dbus::endpoint endpoint("", object_name, interface_name);
164e3b0bf5bSEd Tanous     auto m = dbus::message::new_signal(endpoint, name);
165e3b0bf5bSEd Tanous     conn->send(m, std::chrono::seconds(0));
166e3b0bf5bSEd Tanous   }
167e3b0bf5bSEd Tanous 
168e3b0bf5bSEd Tanous   std::vector<DbusArgument> get_args() override { return args; };
169e3b0bf5bSEd Tanous 
170e3b0bf5bSEd Tanous   std::vector<DbusArgument> args;
171e3b0bf5bSEd Tanous   std::string name;
172e3b0bf5bSEd Tanous   std::string object_name;
173e3b0bf5bSEd Tanous   std::string interface_name;
174e3b0bf5bSEd Tanous   std::shared_ptr<dbus::connection> conn;
175377e76abSEd Tanous };
176377e76abSEd Tanous 
177377e76abSEd Tanous class DbusInterface {
178377e76abSEd Tanous  public:
179377e76abSEd Tanous   DbusInterface(std::string interface_name,
180377e76abSEd Tanous                 std::shared_ptr<dbus::connection>& conn)
181377e76abSEd Tanous       : interface_name(std::move(interface_name)), conn(conn) {}
182377e76abSEd Tanous   virtual boost::container::flat_map<std::string, std::shared_ptr<DbusSignal>>
183377e76abSEd Tanous   get_signals() {
184377e76abSEd Tanous     return dbus_signals;
185377e76abSEd Tanous   };
186377e76abSEd Tanous   virtual boost::container::flat_map<std::string, std::shared_ptr<DbusMethod>>
187377e76abSEd Tanous   get_methods() {
188377e76abSEd Tanous     return dbus_methods;
189377e76abSEd Tanous   };
190377e76abSEd Tanous   virtual std::string get_interface_name() { return interface_name; };
191377e76abSEd Tanous   virtual const boost::container::flat_map<std::string, dbus_variant>
192377e76abSEd Tanous   get_properties_map() {
193377e76abSEd Tanous     return properties_map;
194377e76abSEd Tanous   };
195377e76abSEd Tanous 
196377e76abSEd Tanous   template <typename VALUE_TYPE>
197377e76abSEd Tanous   void set_property(const std::string& property_name, const VALUE_TYPE value,
198377e76abSEd Tanous                     UpdateType update_mode = UpdateType::VALUE_CHANGE_ONLY) {
199377e76abSEd Tanous     // Construct a change vector of length 1.  if this set_properties is ever
200377e76abSEd Tanous     // templated for any type, we could probably swap with with a
201377e76abSEd Tanous     // std::array<pair, 1>
202377e76abSEd Tanous     std::vector<std::pair<std::string, dbus_variant>> v;
203377e76abSEd Tanous     v.emplace_back(property_name, value);
204377e76abSEd Tanous     set_properties(v, update_mode);
205377e76abSEd Tanous   }
206377e76abSEd Tanous 
207377e76abSEd Tanous   void set_properties(
208377e76abSEd Tanous       const std::vector<std::pair<std::string, dbus_variant>>& v,
209377e76abSEd Tanous       const UpdateType update_mode = UpdateType::VALUE_CHANGE_ONLY) {
210377e76abSEd Tanous     // TODO(ed) generalize this interface for all "map like" types, basically
211377e76abSEd Tanous     // anything that will return a const iterator of std::pair<string,
212377e76abSEd Tanous     // variant>
213377e76abSEd Tanous     std::vector<std::pair<std::string, dbus_variant>> updates;
214377e76abSEd Tanous     updates.reserve(v.size());
215377e76abSEd Tanous 
216377e76abSEd Tanous     if (update_mode == UpdateType::FORCE) {
217377e76abSEd Tanous       updates = v;
218377e76abSEd Tanous     } else {
219377e76abSEd Tanous       for (auto& property : v) {
220377e76abSEd Tanous         auto property_map_it = properties_map.find(property.first);
221377e76abSEd Tanous         if (property_map_it != properties_map.end()) {
222377e76abSEd Tanous           // Property exists in map
223377e76abSEd Tanous           if (property_map_it->second != property.second) {
224377e76abSEd Tanous             properties_map[property.first] = property.second;
225377e76abSEd Tanous             // if value has changed since last set
226377e76abSEd Tanous             updates.emplace_back(*property_map_it);
227377e76abSEd Tanous           }
228377e76abSEd Tanous         } else {
229377e76abSEd Tanous           // property doesn't exist, must be new
230377e76abSEd Tanous           properties_map[property.first] = property.second;
231377e76abSEd Tanous           updates.emplace_back(property.first, property.second);
232377e76abSEd Tanous         }
233377e76abSEd Tanous       }
234377e76abSEd Tanous     }
235377e76abSEd Tanous 
236377e76abSEd Tanous     const static dbus::endpoint endpoint("org.freedesktop.DBus", object_name,
237377e76abSEd Tanous                                          "org.freedesktop.DBus.Properties");
238377e76abSEd Tanous 
239377e76abSEd Tanous     auto m = dbus::message::new_signal(endpoint, "PropertiesChanged");
240377e76abSEd Tanous 
241377e76abSEd Tanous     static const std::vector<std::string> empty;
242377e76abSEd Tanous     m.pack(get_interface_name(), updates, empty);
243377e76abSEd Tanous     // TODO(ed) make sure this doesn't block
244377e76abSEd Tanous     conn->async_send(
245377e76abSEd Tanous         m, [](const boost::system::error_code ec, dbus::message r) {});
246377e76abSEd Tanous   }
247377e76abSEd Tanous 
248e3b0bf5bSEd Tanous   void register_method(std::shared_ptr<DbusMethod> method) {
249377e76abSEd Tanous     dbus_methods.emplace(method->name, method);
250377e76abSEd Tanous   }
251377e76abSEd Tanous 
252377e76abSEd Tanous   template <typename Handler>
253e3b0bf5bSEd Tanous   void register_method(const std::string& name, Handler method) {
254377e76abSEd Tanous     dbus_methods.emplace(name,
255377e76abSEd Tanous                          new LambdaDbusMethod<Handler>(name, conn, method));
256377e76abSEd Tanous   }
257377e76abSEd Tanous 
258e3b0bf5bSEd Tanous   template <typename Handler>
259e3b0bf5bSEd Tanous   void register_method(const std::string& name,
260e3b0bf5bSEd Tanous                        const std::vector<std::string>& input_arg_names,
261e3b0bf5bSEd Tanous                        const std::vector<std::string>& output_arg_names,
262e3b0bf5bSEd Tanous                        Handler method) {
263e3b0bf5bSEd Tanous     dbus_methods.emplace(
264e3b0bf5bSEd Tanous         name, new LambdaDbusMethod<Handler>(name, input_arg_names,
265e3b0bf5bSEd Tanous                                             output_arg_names, conn, method));
266e3b0bf5bSEd Tanous   }
267e3b0bf5bSEd Tanous 
268e3b0bf5bSEd Tanous   template <typename... Args>
269e3b0bf5bSEd Tanous   std::shared_ptr<DbusSignal> register_signal(
270e3b0bf5bSEd Tanous       const std::string& name, const std::vector<std::string> arg_names) {
271e3b0bf5bSEd Tanous     auto it = dbus_signals.emplace(
272e3b0bf5bSEd Tanous         name, new DbusTemplateSignal<Args...>(name, object_name, interface_name,
273e3b0bf5bSEd Tanous                                               arg_names, conn));
274e3b0bf5bSEd Tanous     return it.first->second;
275e3b0bf5bSEd Tanous   }
276e3b0bf5bSEd Tanous 
277377e76abSEd Tanous   void call(dbus::message& m) {
278377e76abSEd Tanous     std::string method_name = m.get_member();
279377e76abSEd Tanous     auto method = dbus_methods.find(method_name);
280377e76abSEd Tanous     if (method != dbus_methods.end()) {
281377e76abSEd Tanous       method->second->call(m);
282377e76abSEd Tanous     }  // TODO(ed) send something when method doesn't exist?
283377e76abSEd Tanous   }
284377e76abSEd Tanous 
285377e76abSEd Tanous   std::string object_name;
286377e76abSEd Tanous   std::string interface_name;
287377e76abSEd Tanous   boost::container::flat_map<std::string, std::shared_ptr<DbusMethod>>
288377e76abSEd Tanous       dbus_methods;
289377e76abSEd Tanous   boost::container::flat_map<std::string, std::shared_ptr<DbusSignal>>
290377e76abSEd Tanous       dbus_signals;
291377e76abSEd Tanous   boost::container::flat_map<std::string, dbus_variant> properties_map;
292377e76abSEd Tanous   std::shared_ptr<dbus::connection> conn;
293377e76abSEd Tanous };
294377e76abSEd Tanous 
295377e76abSEd Tanous class DbusObject {
296377e76abSEd Tanous  public:
297377e76abSEd Tanous   DbusObject(std::shared_ptr<dbus::connection> conn, std::string object_name)
298377e76abSEd Tanous       : object_name(std::move(object_name)), conn(conn) {
299377e76abSEd Tanous     properties_iface = add_interface("org.freedesktop.DBus.Properties");
300377e76abSEd Tanous 
301377e76abSEd Tanous     properties_iface->register_method(
302e3b0bf5bSEd Tanous         "Get", {"interface_name", "properties_name"}, {"value"},
303e3b0bf5bSEd Tanous         [&](const std::string& interface_name,
304377e76abSEd Tanous             const std::string& property_name) {
305377e76abSEd Tanous           auto interface_it = interfaces.find(interface_name);
306377e76abSEd Tanous           if (interface_it == interfaces.end()) {
307377e76abSEd Tanous             // Interface not found error
308377e76abSEd Tanous             throw std::runtime_error("interface not found");
309377e76abSEd Tanous           } else {
310377e76abSEd Tanous             auto& properties_map = interface_it->second->get_properties_map();
311377e76abSEd Tanous             auto property = properties_map.find(property_name);
312377e76abSEd Tanous             if (property == properties_map.end()) {
313377e76abSEd Tanous               // TODO(ed) property not found error
314377e76abSEd Tanous               throw std::runtime_error("property not found");
315377e76abSEd Tanous             } else {
316377e76abSEd Tanous               return std::tuple<dbus_variant>(property->second);
317377e76abSEd Tanous             }
318377e76abSEd Tanous           }
319377e76abSEd Tanous         });
320377e76abSEd Tanous 
321e3b0bf5bSEd Tanous     properties_iface->register_method(
322e3b0bf5bSEd Tanous         "GetAll", {"interface_name"}, {"properties"},
323e3b0bf5bSEd Tanous         [&](const std::string& interface_name) {
324377e76abSEd Tanous           auto interface_it = interfaces.find(interface_name);
325377e76abSEd Tanous           if (interface_it == interfaces.end()) {
326377e76abSEd Tanous             // Interface not found error
327377e76abSEd Tanous             throw std::runtime_error("interface not found");
328377e76abSEd Tanous           } else {
329377e76abSEd Tanous             std::vector<std::pair<std::string, dbus_variant>> v;
330377e76abSEd Tanous             for (auto& element : properties_iface->get_properties_map()) {
331377e76abSEd Tanous               v.emplace_back(element.first, element.second);
332377e76abSEd Tanous             }
333e3b0bf5bSEd Tanous             return std::tuple<
334e3b0bf5bSEd Tanous                 std::vector<std::pair<std::string, dbus_variant>>>(v);
335377e76abSEd Tanous           }
336377e76abSEd Tanous         });
337377e76abSEd Tanous     properties_iface->register_method(
338e3b0bf5bSEd Tanous         "Set", {"interface_name", "properties_name", "value"}, {},
339377e76abSEd Tanous         [&](const std::string& interface_name, const std::string& property_name,
340377e76abSEd Tanous             const dbus_variant& value) {
341377e76abSEd Tanous           auto interface_it = interfaces.find(interface_name);
342377e76abSEd Tanous           if (interface_it == interfaces.end()) {
343377e76abSEd Tanous             // Interface not found error
344377e76abSEd Tanous             throw std::runtime_error("interface not found");
345377e76abSEd Tanous           } else {
346377e76abSEd Tanous             // Todo, the set propery (signular) interface should support
347a04118e8SEd Tanous             // handing a variant.  The below is expensive
348377e76abSEd Tanous             std::vector<std::pair<std::string, dbus_variant>> v;
349377e76abSEd Tanous             v.emplace_back(property_name, value);
350377e76abSEd Tanous             interface_it->second->set_properties(v);
351377e76abSEd Tanous             return std::tuple<>();
352377e76abSEd Tanous           }
353377e76abSEd Tanous         });
354e3b0bf5bSEd Tanous 
355e3b0bf5bSEd Tanous     properties_iface->register_signal<
356e3b0bf5bSEd Tanous         std::string, std::vector<std::pair<std::string, dbus_variant>>,
357e3b0bf5bSEd Tanous         std::vector<std::string>>(
358e3b0bf5bSEd Tanous         "PropertiesChanged",
359e3b0bf5bSEd Tanous         {"interface_name", "changed_properties", "invalidated_properties"});
360377e76abSEd Tanous   }
361377e76abSEd Tanous 
362377e76abSEd Tanous   std::shared_ptr<DbusInterface> add_interface(const std::string& name) {
363377e76abSEd Tanous     auto x = std::make_shared<DbusInterface>(name, conn);
364377e76abSEd Tanous     register_interface(x);
365377e76abSEd Tanous     return x;
366377e76abSEd Tanous   }
367377e76abSEd Tanous 
368377e76abSEd Tanous   void register_interface(std::shared_ptr<DbusInterface>& interface) {
369377e76abSEd Tanous     interfaces[interface->get_interface_name()] = interface;
370377e76abSEd Tanous     interface->object_name = object_name;
371377e76abSEd Tanous     const static dbus::endpoint endpoint("", object_name,
372377e76abSEd Tanous                                          "org.freedesktop.DBus.ObjectManager");
373377e76abSEd Tanous 
374e3b0bf5bSEd Tanous     auto m = message::new_signal(endpoint, "InterfacesAdded");
375377e76abSEd Tanous     typedef std::vector<std::pair<std::string, dbus_variant>> properties_dict;
376377e76abSEd Tanous     std::vector<std::pair<std::string, properties_dict>> sig;
377377e76abSEd Tanous     sig.emplace_back(interface->get_interface_name(), properties_dict());
378377e76abSEd Tanous     auto& prop_dict = sig.back().second;
379377e76abSEd Tanous     for (auto& property : interface->get_properties_map()) {
380377e76abSEd Tanous       prop_dict.emplace_back(property);
381377e76abSEd Tanous     }
382377e76abSEd Tanous 
383377e76abSEd Tanous     m.pack(object_name, sig);
384e3b0bf5bSEd Tanous     // TODO(ed)
385e3b0bf5bSEd Tanous     // conn->send(m, std::chrono::seconds(0));
386377e76abSEd Tanous   }
387377e76abSEd Tanous 
388377e76abSEd Tanous   auto get_interfaces() { return interfaces; }
389377e76abSEd Tanous 
390377e76abSEd Tanous   void call(dbus::message& m) {
391377e76abSEd Tanous     auto interface = interfaces.find(m.get_interface());
392377e76abSEd Tanous     if (interface != interfaces.end()) {
393377e76abSEd Tanous       interface->second->call(m);
394377e76abSEd Tanous     }  // TODO(ed) send something when interface doesn't exist?
395377e76abSEd Tanous   }
396377e76abSEd Tanous 
397377e76abSEd Tanous   std::string object_name;
398377e76abSEd Tanous   std::shared_ptr<dbus::connection> conn;
399377e76abSEd Tanous 
400377e76abSEd Tanous   // dbus::filter properties_filter;
401377e76abSEd Tanous   std::shared_ptr<DbusInterface> properties_iface;
402377e76abSEd Tanous 
403e3b0bf5bSEd Tanous   std::shared_ptr<DbusInterface> object_manager_iface;
404e3b0bf5bSEd Tanous 
405377e76abSEd Tanous   std::function<void(boost::system::error_code, message)> callback;
406377e76abSEd Tanous   boost::container::flat_map<std::string, std::shared_ptr<DbusInterface>>
407377e76abSEd Tanous       interfaces;
408377e76abSEd Tanous };
409377e76abSEd Tanous 
410377e76abSEd Tanous class DbusObjectServer {
411377e76abSEd Tanous  public:
412377e76abSEd Tanous   DbusObjectServer(std::shared_ptr<dbus::connection>& conn) : conn(conn) {
413377e76abSEd Tanous     introspect_filter =
414377e76abSEd Tanous         std::make_unique<dbus::filter>(conn, [](dbus::message m) {
415377e76abSEd Tanous           if (m.get_type() != "method_call") {
416377e76abSEd Tanous             return false;
417377e76abSEd Tanous           }
418377e76abSEd Tanous           if (m.get_interface() != "org.freedesktop.DBus.Introspectable") {
419377e76abSEd Tanous             return false;
420377e76abSEd Tanous           }
421377e76abSEd Tanous           if (m.get_member() != "Introspect") {
422377e76abSEd Tanous             return false;
423377e76abSEd Tanous           };
424377e76abSEd Tanous           return true;
425377e76abSEd Tanous         });
426377e76abSEd Tanous 
427377e76abSEd Tanous     introspect_filter->async_dispatch(
428377e76abSEd Tanous         [&](const boost::system::error_code ec, dbus::message m) {
429377e76abSEd Tanous           on_introspect(ec, m);
430377e76abSEd Tanous         });
431377e76abSEd Tanous 
432377e76abSEd Tanous     object_manager_filter =
433377e76abSEd Tanous         std::make_unique<dbus::filter>(conn, [](dbus::message m) {
434377e76abSEd Tanous 
435377e76abSEd Tanous           if (m.get_type() != "method_call") {
436377e76abSEd Tanous             return false;
437377e76abSEd Tanous           }
438377e76abSEd Tanous           if (m.get_interface() != "org.freedesktop.DBus.ObjectManager") {
439377e76abSEd Tanous             return false;
440377e76abSEd Tanous           }
441377e76abSEd Tanous           if (m.get_member() != "GetManagedObjects") {
442377e76abSEd Tanous             return false;
443377e76abSEd Tanous           };
444377e76abSEd Tanous           return true;
445377e76abSEd Tanous         });
446377e76abSEd Tanous 
447377e76abSEd Tanous     object_manager_filter->async_dispatch(
448377e76abSEd Tanous         [&](const boost::system::error_code ec, dbus::message m) {
449377e76abSEd Tanous           on_get_managed_objects(ec, m);
450377e76abSEd Tanous         });
451377e76abSEd Tanous 
452377e76abSEd Tanous     method_filter = std::make_unique<dbus::filter>(conn, [](dbus::message m) {
453377e76abSEd Tanous 
454377e76abSEd Tanous       if (m.get_type() != "method_call") {
455377e76abSEd Tanous         return false;
456377e76abSEd Tanous       }
457377e76abSEd Tanous       return true;
458377e76abSEd Tanous     });
459377e76abSEd Tanous 
460377e76abSEd Tanous     method_filter->async_dispatch(
461377e76abSEd Tanous         [&](const boost::system::error_code ec, dbus::message m) {
462377e76abSEd Tanous           on_method_call(ec, m);
463377e76abSEd Tanous         });
464377e76abSEd Tanous   };
465377e76abSEd Tanous 
466377e76abSEd Tanous   std::shared_ptr<dbus::connection>& get_connection() { return conn; }
467377e76abSEd Tanous   void on_introspect(const boost::system::error_code ec, dbus::message m) {
468377e76abSEd Tanous     auto xml = get_xml_for_path(m.get_path());
469e3b0bf5bSEd Tanous     std::cout << "path: " << m.get_path() << "\n" << xml << "\n";
470377e76abSEd Tanous     auto ret = dbus::message::new_return(m);
471377e76abSEd Tanous     ret.pack(xml);
472377e76abSEd Tanous     conn->async_send(
473377e76abSEd Tanous         ret, [](const boost::system::error_code ec, dbus::message r) {});
474377e76abSEd Tanous 
475377e76abSEd Tanous     introspect_filter->async_dispatch(
476377e76abSEd Tanous         [&](const boost::system::error_code ec, dbus::message m) {
477377e76abSEd Tanous           on_introspect(ec, m);
478377e76abSEd Tanous         });
479377e76abSEd Tanous   }
480377e76abSEd Tanous 
481377e76abSEd Tanous   void on_method_call(const boost::system::error_code ec, dbus::message m) {
482377e76abSEd Tanous     std::cout << "on method call\n";
483377e76abSEd Tanous     if (ec) {
484377e76abSEd Tanous       std::cerr << "on_method_call error: " << ec << "\n";
485377e76abSEd Tanous     } else {
486377e76abSEd Tanous       auto path = m.get_path();
487377e76abSEd Tanous       // TODO(ed) objects should be a map
488377e76abSEd Tanous       for (auto& object : objects) {
489377e76abSEd Tanous         if (object->object_name == path) {
490377e76abSEd Tanous           object->call(m);
491377e76abSEd Tanous           break;
492377e76abSEd Tanous         }
493377e76abSEd Tanous       }
494377e76abSEd Tanous     }
495377e76abSEd Tanous     method_filter->async_dispatch(
496377e76abSEd Tanous         [&](const boost::system::error_code ec, dbus::message m) {
497377e76abSEd Tanous           on_method_call(ec, m);
498377e76abSEd Tanous         });
499377e76abSEd Tanous   }
500377e76abSEd Tanous 
501377e76abSEd Tanous   void on_get_managed_objects(const boost::system::error_code ec,
502377e76abSEd Tanous                               dbus::message m) {
503377e76abSEd Tanous     typedef std::vector<std::pair<std::string, dbus::dbus_variant>>
504377e76abSEd Tanous         properties_dict;
505377e76abSEd Tanous 
506377e76abSEd Tanous     typedef std::vector<std::pair<std::string, properties_dict>>
507377e76abSEd Tanous         interfaces_dict;
508377e76abSEd Tanous 
509377e76abSEd Tanous     std::vector<std::pair<std::string, interfaces_dict>> dict;
510377e76abSEd Tanous 
511377e76abSEd Tanous     for (auto& object : objects) {
512377e76abSEd Tanous       interfaces_dict i;
513377e76abSEd Tanous       for (auto& interface : object->get_interfaces()) {
514377e76abSEd Tanous         properties_dict p;
515377e76abSEd Tanous 
516377e76abSEd Tanous         for (auto& property : interface.second->get_properties_map()) {
517377e76abSEd Tanous           p.push_back(property);
518377e76abSEd Tanous         }
519377e76abSEd Tanous 
520377e76abSEd Tanous         i.emplace_back(interface.second->get_interface_name(), std::move(p));
521377e76abSEd Tanous       }
522377e76abSEd Tanous       dict.emplace_back(object->object_name, std::move(i));
523377e76abSEd Tanous     }
524377e76abSEd Tanous     auto ret = dbus::message::new_return(m);
525377e76abSEd Tanous     ret.pack(dict);
526377e76abSEd Tanous     conn->async_send(
527377e76abSEd Tanous         ret, [](const boost::system::error_code ec, dbus::message r) {});
528377e76abSEd Tanous 
529377e76abSEd Tanous     object_manager_filter->async_dispatch(
530377e76abSEd Tanous         [&](const boost::system::error_code ec, dbus::message m) {
531377e76abSEd Tanous           on_get_managed_objects(ec, m);
532377e76abSEd Tanous         });
533377e76abSEd Tanous   }
534377e76abSEd Tanous 
535377e76abSEd Tanous   std::shared_ptr<DbusObject> add_object(const std::string& name) {
536377e76abSEd Tanous     auto x = std::make_shared<DbusObject>(conn, name);
537377e76abSEd Tanous     register_object(x);
538377e76abSEd Tanous     return x;
539377e76abSEd Tanous   }
540377e76abSEd Tanous 
541377e76abSEd Tanous   void register_object(std::shared_ptr<DbusObject> object) {
542377e76abSEd Tanous     objects.emplace_back(object);
543377e76abSEd Tanous   }
544377e76abSEd Tanous 
545377e76abSEd Tanous   std::string get_xml_for_path(const std::string& path) {
546377e76abSEd Tanous     std::string newpath(path);
547377e76abSEd Tanous 
548377e76abSEd Tanous     if (newpath == "/") {
549377e76abSEd Tanous       newpath.assign("");
550377e76abSEd Tanous     }
551377e76abSEd Tanous 
552377e76abSEd Tanous     boost::container::flat_set<std::string> node_names;
553377e76abSEd Tanous     std::string xml(
554377e76abSEd Tanous         "<!DOCTYPE node PUBLIC "
555377e76abSEd Tanous         "\"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" "
556377e76abSEd Tanous         "\"http://www.freedesktop.org/standards/dbus/1.0/"
557377e76abSEd Tanous         "introspect.dtd\">\n<node>");
558377e76abSEd Tanous     for (auto& object : objects) {
559377e76abSEd Tanous       std::string& object_name = object->object_name;
560377e76abSEd Tanous       // exact match
561377e76abSEd Tanous       if (object->object_name == newpath) {
562377e76abSEd Tanous         xml +=
563377e76abSEd Tanous             "  <interface name=\"org.freedesktop.DBus.Peer\">"
564377e76abSEd Tanous             "    <method name=\"Ping\"/>"
565377e76abSEd Tanous             "    <method name=\"GetMachineId\">"
566377e76abSEd Tanous             "      <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>"
567377e76abSEd Tanous             "    </method>"
568377e76abSEd Tanous             "  </interface>";
569377e76abSEd Tanous 
570377e76abSEd Tanous         xml +=
571377e76abSEd Tanous             "  <interface name=\"org.freedesktop.DBus.ObjectManager\">"
572377e76abSEd Tanous             "    <method name=\"GetManagedObjects\">"
573377e76abSEd Tanous             "      <arg type=\"a{oa{sa{sv}}}\" "
574377e76abSEd Tanous             "           name=\"object_paths_interfaces_and_properties\" "
575377e76abSEd Tanous             "           direction=\"out\"/>"
576377e76abSEd Tanous             "    </method>"
577377e76abSEd Tanous             "    <signal name=\"InterfacesAdded\">"
578377e76abSEd Tanous             "      <arg type=\"o\" name=\"object_path\"/>"
579377e76abSEd Tanous             "      <arg type=\"a{sa{sv}}\" "
580377e76abSEd Tanous             "name=\"interfaces_and_properties\"/>"
581377e76abSEd Tanous             "    </signal>"
582377e76abSEd Tanous             "    <signal name=\"InterfacesRemoved\">"
583377e76abSEd Tanous             "      <arg type=\"o\" name=\"object_path\"/>"
584377e76abSEd Tanous             "      <arg type=\"as\" name=\"interfaces\"/>"
585377e76abSEd Tanous             "    </signal>"
586377e76abSEd Tanous             "  </interface>";
587377e76abSEd Tanous 
588e3b0bf5bSEd Tanous         xml +=
589e3b0bf5bSEd Tanous             "<interface name=\"org.freedesktop.DBus.Introspectable\">"
590e3b0bf5bSEd Tanous             "    <method name=\"Introspect\">"
591e3b0bf5bSEd Tanous             "        <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>"
592e3b0bf5bSEd Tanous             "    </method>"
593e3b0bf5bSEd Tanous             "</interface>";
594e3b0bf5bSEd Tanous 
595377e76abSEd Tanous         for (auto& interface_pair : object->interfaces) {
596377e76abSEd Tanous           xml += "<interface name=\"";
597377e76abSEd Tanous           xml += interface_pair.first;
598377e76abSEd Tanous           xml += "\">";
599377e76abSEd Tanous           for (auto& method : interface_pair.second->get_methods()) {
600377e76abSEd Tanous             xml += "<method name=\"";
601377e76abSEd Tanous             xml += method.first;
602377e76abSEd Tanous             xml += "\">";
603377e76abSEd Tanous             for (auto& arg : method.second->get_args()) {
604377e76abSEd Tanous               xml += "<arg name=\"";
605377e76abSEd Tanous               xml += arg.name;
606377e76abSEd Tanous               xml += "\" type=\"";
607377e76abSEd Tanous               xml += arg.type;
608377e76abSEd Tanous               xml += "\" direction=\"";
609377e76abSEd Tanous               xml += arg.direction;
610377e76abSEd Tanous               xml += "\"/>";
611377e76abSEd Tanous             }
612377e76abSEd Tanous             xml += "</method>";
613377e76abSEd Tanous           }
614377e76abSEd Tanous 
615377e76abSEd Tanous           for (auto& signal : interface_pair.second->get_signals()) {
616377e76abSEd Tanous             xml += "<signal name=\"";
617377e76abSEd Tanous             xml += signal.first;
618377e76abSEd Tanous             xml += "\">";
619377e76abSEd Tanous             for (auto& arg : signal.second->get_args()) {
620377e76abSEd Tanous               xml += "<arg name=\"";
621377e76abSEd Tanous               xml += arg.name;
622377e76abSEd Tanous               xml += "\" type=\"";
623377e76abSEd Tanous               xml += arg.type;
624377e76abSEd Tanous               xml += "\"/>";
625377e76abSEd Tanous             }
626377e76abSEd Tanous 
627377e76abSEd Tanous             xml += "</signal>";
628377e76abSEd Tanous           }
629377e76abSEd Tanous 
630377e76abSEd Tanous           for (auto& property : interface_pair.second->get_properties_map()) {
631377e76abSEd Tanous             xml += "<property name=\"";
632377e76abSEd Tanous             xml += property.first;
633377e76abSEd Tanous             xml += "\" type=\"";
634377e76abSEd Tanous 
635377e76abSEd Tanous             std::string type = std::string(boost::apply_visitor(
636377e76abSEd Tanous                 [&](auto val) {
637377e76abSEd Tanous                   static const auto constexpr sig =
638377e76abSEd Tanous                       element_signature<decltype(val)>::code;
639377e76abSEd Tanous                   return &sig[0];
640377e76abSEd Tanous                 },
641377e76abSEd Tanous                 property.second));
642377e76abSEd Tanous             xml += type;
643e3b0bf5bSEd Tanous             xml += "\" access=\"";
644377e76abSEd Tanous             // TODO direction can be readwrite, read, or write.  Need to
645377e76abSEd Tanous             // make this configurable
646377e76abSEd Tanous             xml += "readwrite";
647377e76abSEd Tanous             xml += "\"/>";
648377e76abSEd Tanous           }
649377e76abSEd Tanous           xml += "</interface>";
650377e76abSEd Tanous         }
651377e76abSEd Tanous       } else if (boost::starts_with(object_name, newpath)) {
652377e76abSEd Tanous         auto slash_index = object_name.find("/", newpath.size() + 1);
653377e76abSEd Tanous         auto subnode = object_name.substr(newpath.size() + 1,
654377e76abSEd Tanous                                           slash_index - newpath.size() - 1);
655377e76abSEd Tanous         if (node_names.find(subnode) == node_names.end()) {
656377e76abSEd Tanous           node_names.insert(subnode);
657377e76abSEd Tanous           xml += "<node name=\"";
658377e76abSEd Tanous           xml += subnode;
659377e76abSEd Tanous           xml += "\">";
660377e76abSEd Tanous           xml += "</node>";
661377e76abSEd Tanous         }
662377e76abSEd Tanous       }
663377e76abSEd Tanous     }
664377e76abSEd Tanous     xml += "</node>";
665377e76abSEd Tanous     return xml;
666377e76abSEd Tanous   }
667377e76abSEd Tanous 
668377e76abSEd Tanous  private:
669377e76abSEd Tanous   std::shared_ptr<dbus::connection> conn;
670377e76abSEd Tanous   std::vector<std::shared_ptr<DbusObject>> objects;
671377e76abSEd Tanous   std::unique_ptr<dbus::filter> introspect_filter;
672377e76abSEd Tanous   std::unique_ptr<dbus::filter> object_manager_filter;
673377e76abSEd Tanous   std::unique_ptr<dbus::filter> method_filter;
674377e76abSEd Tanous };
675377e76abSEd Tanous }
676