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 { 117e62be329SEd 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 162a8b4eac4SEd Tanous void send(const 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 196*f95fe4ccSVernon Mauery dbus_variant get_property(const std::string& property_name) { 197*f95fe4ccSVernon Mauery auto property = properties_map.find(property_name); 198*f95fe4ccSVernon Mauery if (property == properties_map.end()) { 199*f95fe4ccSVernon Mauery // TODO(ed) property not found error 200*f95fe4ccSVernon Mauery throw std::runtime_error("property not found"); 201*f95fe4ccSVernon Mauery } else { 202*f95fe4ccSVernon Mauery return property->second; 203*f95fe4ccSVernon Mauery } 204*f95fe4ccSVernon Mauery } 205*f95fe4ccSVernon Mauery 206377e76abSEd Tanous template <typename VALUE_TYPE> 207377e76abSEd Tanous void set_property(const std::string& property_name, const VALUE_TYPE value, 208377e76abSEd Tanous UpdateType update_mode = UpdateType::VALUE_CHANGE_ONLY) { 209377e76abSEd Tanous // Construct a change vector of length 1. if this set_properties is ever 210377e76abSEd Tanous // templated for any type, we could probably swap with with a 211377e76abSEd Tanous // std::array<pair, 1> 212377e76abSEd Tanous std::vector<std::pair<std::string, dbus_variant>> v; 213377e76abSEd Tanous v.emplace_back(property_name, value); 214377e76abSEd Tanous set_properties(v, update_mode); 215377e76abSEd Tanous } 216377e76abSEd Tanous 217377e76abSEd Tanous void set_properties( 218377e76abSEd Tanous const std::vector<std::pair<std::string, dbus_variant>>& v, 219377e76abSEd Tanous const UpdateType update_mode = UpdateType::VALUE_CHANGE_ONLY) { 220377e76abSEd Tanous // TODO(ed) generalize this interface for all "map like" types, basically 221377e76abSEd Tanous // anything that will return a const iterator of std::pair<string, 222377e76abSEd Tanous // variant> 223377e76abSEd Tanous std::vector<std::pair<std::string, dbus_variant>> updates; 224377e76abSEd Tanous updates.reserve(v.size()); 225377e76abSEd Tanous 226377e76abSEd Tanous if (update_mode == UpdateType::FORCE) { 227377e76abSEd Tanous updates = v; 228377e76abSEd Tanous } else { 229377e76abSEd Tanous for (auto& property : v) { 230377e76abSEd Tanous auto property_map_it = properties_map.find(property.first); 231377e76abSEd Tanous if (property_map_it != properties_map.end()) { 232377e76abSEd Tanous // Property exists in map 233377e76abSEd Tanous if (property_map_it->second != property.second) { 234377e76abSEd Tanous properties_map[property.first] = property.second; 235377e76abSEd Tanous // if value has changed since last set 236377e76abSEd Tanous updates.emplace_back(*property_map_it); 237377e76abSEd Tanous } 238377e76abSEd Tanous } else { 239377e76abSEd Tanous // property doesn't exist, must be new 240377e76abSEd Tanous properties_map[property.first] = property.second; 241377e76abSEd Tanous updates.emplace_back(property.first, property.second); 242377e76abSEd Tanous } 243377e76abSEd Tanous } 244377e76abSEd Tanous } 245377e76abSEd Tanous 2462277cd81SFeist, James dbus::endpoint endpoint("org.freedesktop.DBus", object_name, 247377e76abSEd Tanous "org.freedesktop.DBus.Properties"); 248377e76abSEd Tanous 249377e76abSEd Tanous auto m = dbus::message::new_signal(endpoint, "PropertiesChanged"); 250377e76abSEd Tanous 251377e76abSEd Tanous static const std::vector<std::string> empty; 252377e76abSEd Tanous m.pack(get_interface_name(), updates, empty); 253377e76abSEd Tanous // TODO(ed) make sure this doesn't block 254377e76abSEd Tanous conn->async_send( 255377e76abSEd Tanous m, [](const boost::system::error_code ec, dbus::message r) {}); 256377e76abSEd Tanous } 257377e76abSEd Tanous 258e3b0bf5bSEd Tanous void register_method(std::shared_ptr<DbusMethod> method) { 259377e76abSEd Tanous dbus_methods.emplace(method->name, method); 260377e76abSEd Tanous } 261377e76abSEd Tanous 262377e76abSEd Tanous template <typename Handler> 263e3b0bf5bSEd Tanous void register_method(const std::string& name, Handler method) { 264377e76abSEd Tanous dbus_methods.emplace(name, 265377e76abSEd Tanous new LambdaDbusMethod<Handler>(name, conn, method)); 266377e76abSEd Tanous } 267377e76abSEd Tanous 268e3b0bf5bSEd Tanous template <typename Handler> 269e3b0bf5bSEd Tanous void register_method(const std::string& name, 270e3b0bf5bSEd Tanous const std::vector<std::string>& input_arg_names, 271e3b0bf5bSEd Tanous const std::vector<std::string>& output_arg_names, 272e3b0bf5bSEd Tanous Handler method) { 273e3b0bf5bSEd Tanous dbus_methods.emplace( 274e3b0bf5bSEd Tanous name, new LambdaDbusMethod<Handler>(name, input_arg_names, 275e3b0bf5bSEd Tanous output_arg_names, conn, method)); 276e3b0bf5bSEd Tanous } 277e3b0bf5bSEd Tanous 278e3b0bf5bSEd Tanous template <typename... Args> 279a8b4eac4SEd Tanous std::shared_ptr<DbusTemplateSignal<Args...>> register_signal( 280e3b0bf5bSEd Tanous const std::string& name, const std::vector<std::string> arg_names) { 281a8b4eac4SEd Tanous auto sig = std::make_shared<DbusTemplateSignal<Args...>>( 282a8b4eac4SEd Tanous name, object_name, interface_name, arg_names, conn); 283a8b4eac4SEd Tanous dbus_signals.emplace(name, sig); 284a8b4eac4SEd Tanous return sig; 285e3b0bf5bSEd Tanous } 286e3b0bf5bSEd Tanous 287377e76abSEd Tanous void call(dbus::message& m) { 288377e76abSEd Tanous std::string method_name = m.get_member(); 289377e76abSEd Tanous auto method = dbus_methods.find(method_name); 290377e76abSEd Tanous if (method != dbus_methods.end()) { 291377e76abSEd Tanous method->second->call(m); 292377e76abSEd Tanous } // TODO(ed) send something when method doesn't exist? 293377e76abSEd Tanous } 294377e76abSEd Tanous 295377e76abSEd Tanous std::string object_name; 296377e76abSEd Tanous std::string interface_name; 297377e76abSEd Tanous boost::container::flat_map<std::string, std::shared_ptr<DbusMethod>> 298377e76abSEd Tanous dbus_methods; 299377e76abSEd Tanous boost::container::flat_map<std::string, std::shared_ptr<DbusSignal>> 300377e76abSEd Tanous dbus_signals; 301377e76abSEd Tanous boost::container::flat_map<std::string, dbus_variant> properties_map; 302377e76abSEd Tanous std::shared_ptr<dbus::connection> conn; 303377e76abSEd Tanous }; 304377e76abSEd Tanous 305377e76abSEd Tanous class DbusObject { 306377e76abSEd Tanous public: 307377e76abSEd Tanous DbusObject(std::shared_ptr<dbus::connection> conn, std::string object_name) 308377e76abSEd Tanous : object_name(std::move(object_name)), conn(conn) { 309377e76abSEd Tanous properties_iface = add_interface("org.freedesktop.DBus.Properties"); 310377e76abSEd Tanous 311377e76abSEd Tanous properties_iface->register_method( 312e3b0bf5bSEd Tanous "Get", {"interface_name", "properties_name"}, {"value"}, 313e3b0bf5bSEd Tanous [&](const std::string& interface_name, 314377e76abSEd Tanous const std::string& property_name) { 315377e76abSEd Tanous auto interface_it = interfaces.find(interface_name); 316377e76abSEd Tanous if (interface_it == interfaces.end()) { 317377e76abSEd Tanous // Interface not found error 318377e76abSEd Tanous throw std::runtime_error("interface not found"); 319377e76abSEd Tanous } else { 320377e76abSEd Tanous auto& properties_map = interface_it->second->get_properties_map(); 321377e76abSEd Tanous auto property = properties_map.find(property_name); 322377e76abSEd Tanous if (property == properties_map.end()) { 323377e76abSEd Tanous // TODO(ed) property not found error 324377e76abSEd Tanous throw std::runtime_error("property not found"); 325377e76abSEd Tanous } else { 326377e76abSEd Tanous return std::tuple<dbus_variant>(property->second); 327377e76abSEd Tanous } 328377e76abSEd Tanous } 329377e76abSEd Tanous }); 330377e76abSEd Tanous 331e3b0bf5bSEd Tanous properties_iface->register_method( 332e3b0bf5bSEd Tanous "GetAll", {"interface_name"}, {"properties"}, 333e3b0bf5bSEd Tanous [&](const std::string& interface_name) { 334377e76abSEd Tanous auto interface_it = interfaces.find(interface_name); 335377e76abSEd Tanous if (interface_it == interfaces.end()) { 336377e76abSEd Tanous // Interface not found error 337377e76abSEd Tanous throw std::runtime_error("interface not found"); 338377e76abSEd Tanous } else { 339377e76abSEd Tanous std::vector<std::pair<std::string, dbus_variant>> v; 34093f7fc83SEd Tanous for (auto& element : interface_it->second->get_properties_map()) { 341377e76abSEd Tanous v.emplace_back(element.first, element.second); 342377e76abSEd Tanous } 343e3b0bf5bSEd Tanous return std::tuple< 344e3b0bf5bSEd Tanous std::vector<std::pair<std::string, dbus_variant>>>(v); 345377e76abSEd Tanous } 346377e76abSEd Tanous }); 347377e76abSEd Tanous properties_iface->register_method( 348e3b0bf5bSEd Tanous "Set", {"interface_name", "properties_name", "value"}, {}, 349377e76abSEd Tanous [&](const std::string& interface_name, const std::string& property_name, 350377e76abSEd Tanous const dbus_variant& value) { 351377e76abSEd Tanous auto interface_it = interfaces.find(interface_name); 352377e76abSEd Tanous if (interface_it == interfaces.end()) { 353377e76abSEd Tanous // Interface not found error 354377e76abSEd Tanous throw std::runtime_error("interface not found"); 355377e76abSEd Tanous } else { 356377e76abSEd Tanous // Todo, the set propery (signular) interface should support 357a04118e8SEd Tanous // handing a variant. The below is expensive 358377e76abSEd Tanous std::vector<std::pair<std::string, dbus_variant>> v; 359377e76abSEd Tanous v.emplace_back(property_name, value); 360377e76abSEd Tanous interface_it->second->set_properties(v); 361377e76abSEd Tanous return std::tuple<>(); 362377e76abSEd Tanous } 363377e76abSEd Tanous }); 364e3b0bf5bSEd Tanous 365e3b0bf5bSEd Tanous properties_iface->register_signal< 366e3b0bf5bSEd Tanous std::string, std::vector<std::pair<std::string, dbus_variant>>, 367e3b0bf5bSEd Tanous std::vector<std::string>>( 368e3b0bf5bSEd Tanous "PropertiesChanged", 369e3b0bf5bSEd Tanous {"interface_name", "changed_properties", "invalidated_properties"}); 370377e76abSEd Tanous } 371377e76abSEd Tanous 372377e76abSEd Tanous std::shared_ptr<DbusInterface> add_interface(const std::string& name) { 373377e76abSEd Tanous auto x = std::make_shared<DbusInterface>(name, conn); 374377e76abSEd Tanous register_interface(x); 375377e76abSEd Tanous return x; 376377e76abSEd Tanous } 377377e76abSEd Tanous 378377e76abSEd Tanous void register_interface(std::shared_ptr<DbusInterface>& interface) { 379377e76abSEd Tanous interfaces[interface->get_interface_name()] = interface; 380377e76abSEd Tanous interface->object_name = object_name; 381377e76abSEd Tanous const static dbus::endpoint endpoint("", object_name, 382377e76abSEd Tanous "org.freedesktop.DBus.ObjectManager"); 383377e76abSEd Tanous 384e3b0bf5bSEd Tanous auto m = message::new_signal(endpoint, "InterfacesAdded"); 385377e76abSEd Tanous typedef std::vector<std::pair<std::string, dbus_variant>> properties_dict; 386377e76abSEd Tanous std::vector<std::pair<std::string, properties_dict>> sig; 387377e76abSEd Tanous sig.emplace_back(interface->get_interface_name(), properties_dict()); 388377e76abSEd Tanous auto& prop_dict = sig.back().second; 389377e76abSEd Tanous for (auto& property : interface->get_properties_map()) { 390377e76abSEd Tanous prop_dict.emplace_back(property); 391377e76abSEd Tanous } 392377e76abSEd Tanous 393377e76abSEd Tanous m.pack(object_name, sig); 394f76e5e0bSFeist, James 395f76e5e0bSFeist, James conn->send(m, std::chrono::seconds(0)); 396377e76abSEd Tanous } 397377e76abSEd Tanous 398377e76abSEd Tanous auto get_interfaces() { return interfaces; } 399377e76abSEd Tanous 400377e76abSEd Tanous void call(dbus::message& m) { 401377e76abSEd Tanous auto interface = interfaces.find(m.get_interface()); 402377e76abSEd Tanous if (interface != interfaces.end()) { 403377e76abSEd Tanous interface->second->call(m); 404377e76abSEd Tanous } // TODO(ed) send something when interface doesn't exist? 405377e76abSEd Tanous } 406377e76abSEd Tanous 407377e76abSEd Tanous std::string object_name; 408377e76abSEd Tanous std::shared_ptr<dbus::connection> conn; 409377e76abSEd Tanous 410377e76abSEd Tanous // dbus::filter properties_filter; 411377e76abSEd Tanous std::shared_ptr<DbusInterface> properties_iface; 412377e76abSEd Tanous 413e3b0bf5bSEd Tanous std::shared_ptr<DbusInterface> object_manager_iface; 414e3b0bf5bSEd Tanous 415377e76abSEd Tanous std::function<void(boost::system::error_code, message)> callback; 416377e76abSEd Tanous boost::container::flat_map<std::string, std::shared_ptr<DbusInterface>> 417377e76abSEd Tanous interfaces; 418377e76abSEd Tanous }; 419377e76abSEd Tanous 420377e76abSEd Tanous class DbusObjectServer { 421377e76abSEd Tanous public: 422377e76abSEd Tanous DbusObjectServer(std::shared_ptr<dbus::connection>& conn) : conn(conn) { 423377e76abSEd Tanous introspect_filter = 424377e76abSEd Tanous std::make_unique<dbus::filter>(conn, [](dbus::message m) { 425377e76abSEd Tanous if (m.get_type() != "method_call") { 426377e76abSEd Tanous return false; 427377e76abSEd Tanous } 428377e76abSEd Tanous if (m.get_interface() != "org.freedesktop.DBus.Introspectable") { 429377e76abSEd Tanous return false; 430377e76abSEd Tanous } 431377e76abSEd Tanous if (m.get_member() != "Introspect") { 432377e76abSEd Tanous return false; 433377e76abSEd Tanous }; 434377e76abSEd Tanous return true; 435377e76abSEd Tanous }); 436377e76abSEd Tanous 437377e76abSEd Tanous introspect_filter->async_dispatch( 438377e76abSEd Tanous [&](const boost::system::error_code ec, dbus::message m) { 439377e76abSEd Tanous on_introspect(ec, m); 440377e76abSEd Tanous }); 441377e76abSEd Tanous 442377e76abSEd Tanous object_manager_filter = 443377e76abSEd Tanous std::make_unique<dbus::filter>(conn, [](dbus::message m) { 444377e76abSEd Tanous 445377e76abSEd Tanous if (m.get_type() != "method_call") { 446377e76abSEd Tanous return false; 447377e76abSEd Tanous } 448377e76abSEd Tanous if (m.get_interface() != "org.freedesktop.DBus.ObjectManager") { 449377e76abSEd Tanous return false; 450377e76abSEd Tanous } 451377e76abSEd Tanous if (m.get_member() != "GetManagedObjects") { 452377e76abSEd Tanous return false; 453377e76abSEd Tanous }; 454377e76abSEd Tanous return true; 455377e76abSEd Tanous }); 456377e76abSEd Tanous 457377e76abSEd Tanous object_manager_filter->async_dispatch( 458377e76abSEd Tanous [&](const boost::system::error_code ec, dbus::message m) { 459377e76abSEd Tanous on_get_managed_objects(ec, m); 460377e76abSEd Tanous }); 461377e76abSEd Tanous 462377e76abSEd Tanous method_filter = std::make_unique<dbus::filter>(conn, [](dbus::message m) { 463377e76abSEd Tanous 464377e76abSEd Tanous if (m.get_type() != "method_call") { 465377e76abSEd Tanous return false; 466377e76abSEd Tanous } 467377e76abSEd Tanous return true; 468377e76abSEd Tanous }); 469377e76abSEd Tanous 470377e76abSEd Tanous method_filter->async_dispatch( 471377e76abSEd Tanous [&](const boost::system::error_code ec, dbus::message m) { 472377e76abSEd Tanous on_method_call(ec, m); 473377e76abSEd Tanous }); 474377e76abSEd Tanous }; 475377e76abSEd Tanous 476377e76abSEd Tanous std::shared_ptr<dbus::connection>& get_connection() { return conn; } 477377e76abSEd Tanous void on_introspect(const boost::system::error_code ec, dbus::message m) { 478377e76abSEd Tanous auto xml = get_xml_for_path(m.get_path()); 479e3b0bf5bSEd Tanous std::cout << "path: " << m.get_path() << "\n" << xml << "\n"; 480377e76abSEd Tanous auto ret = dbus::message::new_return(m); 481377e76abSEd Tanous ret.pack(xml); 482377e76abSEd Tanous conn->async_send( 483377e76abSEd Tanous ret, [](const boost::system::error_code ec, dbus::message r) {}); 484377e76abSEd Tanous 485377e76abSEd Tanous introspect_filter->async_dispatch( 486377e76abSEd Tanous [&](const boost::system::error_code ec, dbus::message m) { 487377e76abSEd Tanous on_introspect(ec, m); 488377e76abSEd Tanous }); 489377e76abSEd Tanous } 490377e76abSEd Tanous 491377e76abSEd Tanous void on_method_call(const boost::system::error_code ec, dbus::message m) { 492377e76abSEd Tanous std::cout << "on method call\n"; 493377e76abSEd Tanous if (ec) { 494377e76abSEd Tanous std::cerr << "on_method_call error: " << ec << "\n"; 495377e76abSEd Tanous } else { 496377e76abSEd Tanous auto path = m.get_path(); 497377e76abSEd Tanous // TODO(ed) objects should be a map 498377e76abSEd Tanous for (auto& object : objects) { 499377e76abSEd Tanous if (object->object_name == path) { 500377e76abSEd Tanous object->call(m); 501377e76abSEd Tanous break; 502377e76abSEd Tanous } 503377e76abSEd Tanous } 504377e76abSEd Tanous } 505377e76abSEd Tanous method_filter->async_dispatch( 506377e76abSEd Tanous [&](const boost::system::error_code ec, dbus::message m) { 507377e76abSEd Tanous on_method_call(ec, m); 508377e76abSEd Tanous }); 509377e76abSEd Tanous } 510377e76abSEd Tanous 511377e76abSEd Tanous void on_get_managed_objects(const boost::system::error_code ec, 512377e76abSEd Tanous dbus::message m) { 513377e76abSEd Tanous typedef std::vector<std::pair<std::string, dbus::dbus_variant>> 514377e76abSEd Tanous properties_dict; 515377e76abSEd Tanous 516377e76abSEd Tanous typedef std::vector<std::pair<std::string, properties_dict>> 517377e76abSEd Tanous interfaces_dict; 518377e76abSEd Tanous 519377e76abSEd Tanous std::vector<std::pair<std::string, interfaces_dict>> dict; 520377e76abSEd Tanous 521377e76abSEd Tanous for (auto& object : objects) { 522377e76abSEd Tanous interfaces_dict i; 523377e76abSEd Tanous for (auto& interface : object->get_interfaces()) { 524377e76abSEd Tanous properties_dict p; 525377e76abSEd Tanous 526377e76abSEd Tanous for (auto& property : interface.second->get_properties_map()) { 527377e76abSEd Tanous p.push_back(property); 528377e76abSEd Tanous } 529377e76abSEd Tanous 530377e76abSEd Tanous i.emplace_back(interface.second->get_interface_name(), std::move(p)); 531377e76abSEd Tanous } 532377e76abSEd Tanous dict.emplace_back(object->object_name, std::move(i)); 533377e76abSEd Tanous } 534377e76abSEd Tanous auto ret = dbus::message::new_return(m); 535377e76abSEd Tanous ret.pack(dict); 536377e76abSEd Tanous conn->async_send( 537377e76abSEd Tanous ret, [](const boost::system::error_code ec, dbus::message r) {}); 538377e76abSEd Tanous 539377e76abSEd Tanous object_manager_filter->async_dispatch( 540377e76abSEd Tanous [&](const boost::system::error_code ec, dbus::message m) { 541377e76abSEd Tanous on_get_managed_objects(ec, m); 542377e76abSEd Tanous }); 543377e76abSEd Tanous } 544377e76abSEd Tanous 545377e76abSEd Tanous std::shared_ptr<DbusObject> add_object(const std::string& name) { 546377e76abSEd Tanous auto x = std::make_shared<DbusObject>(conn, name); 547377e76abSEd Tanous register_object(x); 548377e76abSEd Tanous return x; 549377e76abSEd Tanous } 550377e76abSEd Tanous 551377e76abSEd Tanous void register_object(std::shared_ptr<DbusObject> object) { 552377e76abSEd Tanous objects.emplace_back(object); 553377e76abSEd Tanous } 554377e76abSEd Tanous 555377e76abSEd Tanous std::string get_xml_for_path(const std::string& path) { 556377e76abSEd Tanous std::string newpath(path); 557377e76abSEd Tanous 558377e76abSEd Tanous if (newpath == "/") { 559377e76abSEd Tanous newpath.assign(""); 560377e76abSEd Tanous } 561377e76abSEd Tanous 562377e76abSEd Tanous boost::container::flat_set<std::string> node_names; 563377e76abSEd Tanous std::string xml( 564377e76abSEd Tanous "<!DOCTYPE node PUBLIC " 565377e76abSEd Tanous "\"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" " 566377e76abSEd Tanous "\"http://www.freedesktop.org/standards/dbus/1.0/" 567377e76abSEd Tanous "introspect.dtd\">\n<node>"); 568377e76abSEd Tanous for (auto& object : objects) { 569377e76abSEd Tanous std::string& object_name = object->object_name; 570377e76abSEd Tanous // exact match 571377e76abSEd Tanous if (object->object_name == newpath) { 572377e76abSEd Tanous xml += 573377e76abSEd Tanous " <interface name=\"org.freedesktop.DBus.Peer\">" 574377e76abSEd Tanous " <method name=\"Ping\"/>" 575377e76abSEd Tanous " <method name=\"GetMachineId\">" 576377e76abSEd Tanous " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>" 577377e76abSEd Tanous " </method>" 578377e76abSEd Tanous " </interface>"; 579377e76abSEd Tanous 580377e76abSEd Tanous xml += 581377e76abSEd Tanous " <interface name=\"org.freedesktop.DBus.ObjectManager\">" 582377e76abSEd Tanous " <method name=\"GetManagedObjects\">" 583377e76abSEd Tanous " <arg type=\"a{oa{sa{sv}}}\" " 584377e76abSEd Tanous " name=\"object_paths_interfaces_and_properties\" " 585377e76abSEd Tanous " direction=\"out\"/>" 586377e76abSEd Tanous " </method>" 587377e76abSEd Tanous " <signal name=\"InterfacesAdded\">" 588377e76abSEd Tanous " <arg type=\"o\" name=\"object_path\"/>" 589377e76abSEd Tanous " <arg type=\"a{sa{sv}}\" " 590377e76abSEd Tanous "name=\"interfaces_and_properties\"/>" 591377e76abSEd Tanous " </signal>" 592377e76abSEd Tanous " <signal name=\"InterfacesRemoved\">" 593377e76abSEd Tanous " <arg type=\"o\" name=\"object_path\"/>" 594377e76abSEd Tanous " <arg type=\"as\" name=\"interfaces\"/>" 595377e76abSEd Tanous " </signal>" 596377e76abSEd Tanous " </interface>"; 597377e76abSEd Tanous 598e3b0bf5bSEd Tanous xml += 599e3b0bf5bSEd Tanous "<interface name=\"org.freedesktop.DBus.Introspectable\">" 600e3b0bf5bSEd Tanous " <method name=\"Introspect\">" 601e3b0bf5bSEd Tanous " <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>" 602e3b0bf5bSEd Tanous " </method>" 603e3b0bf5bSEd Tanous "</interface>"; 604e3b0bf5bSEd Tanous 605377e76abSEd Tanous for (auto& interface_pair : object->interfaces) { 606377e76abSEd Tanous xml += "<interface name=\""; 607377e76abSEd Tanous xml += interface_pair.first; 608377e76abSEd Tanous xml += "\">"; 609377e76abSEd Tanous for (auto& method : interface_pair.second->get_methods()) { 610377e76abSEd Tanous xml += "<method name=\""; 611377e76abSEd Tanous xml += method.first; 612377e76abSEd Tanous xml += "\">"; 613377e76abSEd Tanous for (auto& arg : method.second->get_args()) { 614377e76abSEd Tanous xml += "<arg name=\""; 615377e76abSEd Tanous xml += arg.name; 616377e76abSEd Tanous xml += "\" type=\""; 617377e76abSEd Tanous xml += arg.type; 618377e76abSEd Tanous xml += "\" direction=\""; 619377e76abSEd Tanous xml += arg.direction; 620377e76abSEd Tanous xml += "\"/>"; 621377e76abSEd Tanous } 622377e76abSEd Tanous xml += "</method>"; 623377e76abSEd Tanous } 624377e76abSEd Tanous 625377e76abSEd Tanous for (auto& signal : interface_pair.second->get_signals()) { 626377e76abSEd Tanous xml += "<signal name=\""; 627377e76abSEd Tanous xml += signal.first; 628377e76abSEd Tanous xml += "\">"; 629377e76abSEd Tanous for (auto& arg : signal.second->get_args()) { 630377e76abSEd Tanous xml += "<arg name=\""; 631377e76abSEd Tanous xml += arg.name; 632377e76abSEd Tanous xml += "\" type=\""; 633377e76abSEd Tanous xml += arg.type; 634377e76abSEd Tanous xml += "\"/>"; 635377e76abSEd Tanous } 636377e76abSEd Tanous 637377e76abSEd Tanous xml += "</signal>"; 638377e76abSEd Tanous } 639377e76abSEd Tanous 640377e76abSEd Tanous for (auto& property : interface_pair.second->get_properties_map()) { 641377e76abSEd Tanous xml += "<property name=\""; 642377e76abSEd Tanous xml += property.first; 643377e76abSEd Tanous xml += "\" type=\""; 644377e76abSEd Tanous 645377e76abSEd Tanous std::string type = std::string(boost::apply_visitor( 646377e76abSEd Tanous [&](auto val) { 647377e76abSEd Tanous static const auto constexpr sig = 648377e76abSEd Tanous element_signature<decltype(val)>::code; 649377e76abSEd Tanous return &sig[0]; 650377e76abSEd Tanous }, 651377e76abSEd Tanous property.second)); 652377e76abSEd Tanous xml += type; 653e3b0bf5bSEd Tanous xml += "\" access=\""; 654377e76abSEd Tanous // TODO direction can be readwrite, read, or write. Need to 655377e76abSEd Tanous // make this configurable 656377e76abSEd Tanous xml += "readwrite"; 657377e76abSEd Tanous xml += "\"/>"; 658377e76abSEd Tanous } 659377e76abSEd Tanous xml += "</interface>"; 660377e76abSEd Tanous } 661377e76abSEd Tanous } else if (boost::starts_with(object_name, newpath)) { 662377e76abSEd Tanous auto slash_index = object_name.find("/", newpath.size() + 1); 663377e76abSEd Tanous auto subnode = object_name.substr(newpath.size() + 1, 664377e76abSEd Tanous slash_index - newpath.size() - 1); 665377e76abSEd Tanous if (node_names.find(subnode) == node_names.end()) { 666377e76abSEd Tanous node_names.insert(subnode); 667377e76abSEd Tanous xml += "<node name=\""; 668377e76abSEd Tanous xml += subnode; 669377e76abSEd Tanous xml += "\">"; 670377e76abSEd Tanous xml += "</node>"; 671377e76abSEd Tanous } 672377e76abSEd Tanous } 673377e76abSEd Tanous } 674377e76abSEd Tanous xml += "</node>"; 675377e76abSEd Tanous return xml; 676377e76abSEd Tanous } 677377e76abSEd Tanous 678377e76abSEd Tanous private: 679377e76abSEd Tanous std::shared_ptr<dbus::connection> conn; 680377e76abSEd Tanous std::vector<std::shared_ptr<DbusObject>> objects; 681377e76abSEd Tanous std::unique_ptr<dbus::filter> introspect_filter; 682377e76abSEd Tanous std::unique_ptr<dbus::filter> object_manager_filter; 683377e76abSEd Tanous std::unique_ptr<dbus::filter> method_filter; 684377e76abSEd Tanous }; 685377e76abSEd Tanous } 686