Skip to content

Instantly share code, notes, and snippets.

@brentalanmiller
Created June 28, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentalanmiller/e774b3bbb53e9f08f22a9fe05f95b0aa to your computer and use it in GitHub Desktop.
Save brentalanmiller/e774b3bbb53e9f08f22a9fe05f95b0aa to your computer and use it in GitHub Desktop.
(Broken) example of querying NetworkManager connection settings using dbus-cpp
#include <iostream>
#include <core/dbus/dbus.h>
#include <core/dbus/asio/executor.h>
struct NetworkManager {
static const std::string &default_path() {
static const std::string s{"/org/freedesktop/NetworkManager"};
return s;
}
static const std::string &name() {
static const std::string s{"org.freedesktop.NetworkManager"};
return s;
}
};
struct Settings {
static const std::string &default_path() {
static const std::string s{"/org/freedesktop/NetworkManager/Settings"};
return s;
}
static const std::string &name() {
static const std::string s{"org.freedesktop.NetworkManager.Settings"};
return s;
}
struct ListConnections {
typedef Settings Interface;
static const std::string &name() {
static const std::string s{"ListConnections"};
return s;
}
static const std::chrono::milliseconds default_timeout() {
return std::chrono::seconds{10};
}
typedef void ArgumentType;
typedef std::vector<core::dbus::types::ObjectPath> ResultType;
};
};
struct Connection {
static const std::string &default_path() {
static const std::string s{"/org/freedesktop/NetworkManager/Settings/Connection"};
return s;
}
static const std::string &name() {
static const std::string s{"org.freedesktop.NetworkManager.Settings.Connection"};
return s;
}
struct GetSettings {
typedef Connection Interface;
static const std::string &name() {
static const std::string s{"GetSettings"};
return s;
}
static const std::chrono::milliseconds default_timeout() { return std::chrono::seconds{10}; }
typedef void ArgumentType;
//This is what dbus-cppc suggested the type should be
typedef std::vector<std::pair<std::string, std::vector<std::pair<std::string, core::dbus::types::Variant>>>> ResultType;
};
};
int main() {
core::dbus::Bus::Ptr system_bus = std::make_shared<core::dbus::Bus>(core::dbus::WellKnownBus::system);
system_bus->install_executor(core::dbus::asio::make_executor(system_bus));
std::thread t {std::bind(&core::dbus::Bus::run, system_bus)};
auto nm_service = core::dbus::Service::use_service(system_bus, core::dbus::traits::Service<NetworkManager>::interface_name());
auto settings_obj = nm_service->object_for_path(core::dbus::types::ObjectPath(Settings::default_path()));
auto connection_list_result = settings_obj->invoke_method_synchronously<Settings::ListConnections, Settings::ListConnections::ResultType>();
std::for_each(connection_list_result.value().begin(), connection_list_result.value().end(), [nm_service](const core::dbus::types::ObjectPath &path) {
std::cout << "connection path: " << path << std::endl;
auto connection_obj = nm_service->object_for_path(core::dbus::types::ObjectPath(path));
//Exception gets raised here
auto settings_list = connection_obj->invoke_method_synchronously<Connection::GetSettings, Connection::GetSettings::ResultType>();
// std::for_each(settings_list.value().begin(), settings_list.value().end(), [](const std::pair<std::string, std::vector<std::pair<std::string, core::dbus::types::Variant>>> &setting) {
//// std::cout << "\t" << setting.first << std::endl;
// });
});
system_bus->stop();
if (t.joinable()) {
t.join();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment