Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build*
doc
tags
CMakeUserPresets.json
CMakeLists.txt.user
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_subdirectory(tests)
endif(BUILD_TESTS)
if(BUILD_EXAMPLES)
include(CTest)
add_subdirectory(examples)
endif(BUILD_EXAMPLES)
if(BUILD_DOCS)
Expand All @@ -106,7 +105,6 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
)
set(CPACK_PACKAGE_FILE_NAME ${cpack_file_name})
include(CPack)
include(CPackIFW)
cpack_add_component(${PROJECT_NAME})
endif(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)

Expand Down
29 changes: 14 additions & 15 deletions examples/connmanctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <ranges>
#include <sstream>
#include <string>

Expand All @@ -19,7 +20,7 @@ auto main() -> int {
bool connecting = false;
Connman connman;
const auto manager = connman.manager();
manager->onRequestInputPassphrase([&](auto service) -> auto {
manager->onRequestInputPassphrase([&](const auto& service) -> auto {
const auto name = service->properties().getName();

std::string passphrase;
Expand All @@ -46,14 +47,13 @@ auto main() -> int {
}
}
// Trim whitespace
line.erase(line.begin(),
std::find_if(line.begin(), line.end(), [](int character) {
line.erase(line.begin(), std::ranges::find_if(line, [](int character) {
return std::isspace(character) == 0;
}));
line.erase(std::find_if(line.rbegin(), line.rend(),
[](int character) {
return std::isspace(character) == 0;
})
line.erase(std::ranges::find_if(std::ranges::reverse_view(line),
[](int character) {
return std::isspace(character) == 0;
})
.base(),
line.end());

Expand Down Expand Up @@ -118,9 +118,8 @@ auto main() -> int {
<< service->objPath() << "\n";
}
} else {
auto iterator = std::find_if(
services.begin(), services.end(),
[&arg](const auto& service) {
auto iterator = std::ranges::find_if(
services, [&arg](const auto& service) {
return service->objPath() == arg ||
service->properties().getName() == arg;
});
Expand Down Expand Up @@ -225,8 +224,8 @@ auto main() -> int {
}
const bool connect = (cmd == "connect");
const auto services = manager->services();
auto iterator = std::find_if(
services.begin(), services.end(), [&arg](const auto& service) {
auto iterator =
std::ranges::find_if(services, [&arg](const auto& service) {
return service->objPath() == arg ||
service->properties().getName() == arg;
});
Expand Down Expand Up @@ -279,8 +278,8 @@ auto main() -> int {
continue;
}
const auto services = manager->services();
auto iterator = std::find_if(
services.begin(), services.end(), [&arg](const auto& service) {
auto iterator =
std::ranges::find_if(services, [&arg](const auto& service) {
return service->objPath() == arg ||
service->properties().getName() == arg;
});
Expand Down Expand Up @@ -311,4 +310,4 @@ auto main() -> int {
}
std::cout << "Exiting.\n";
return 0;
}
}
4 changes: 2 additions & 2 deletions include/amarula/dbus/connman/gagent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <gio/gio.h>
#include <glib.h>

#include <amarula/dbus/gdbus.hpp>
#include <functional>
#include <string>
#include <utility>
Expand All @@ -15,8 +16,7 @@ class Agent {
GDBusConnection *connection_{nullptr};
std::string path_{"/net/amarula/gconnman/agent"};

explicit Agent(GDBusConnection *connection,
const std::string &path = std::string());
explicit Agent(DBus *dbus, const std::string &path = std::string());

using RequestInputCallback =
std::function<GVariant *(const gchar *service, GVariant *fields)>;
Expand Down
4 changes: 3 additions & 1 deletion include/amarula/dbus/connman/gmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ class Manager : public DBusProxy<ManaProperties> {
void onTechnologiesChanged(OnTechListChangedCallback callback);
void onServicesChanged(OnServListChangedCallback callback);

auto internalAgentPath() const -> std::string { return agent_->path_; };
[[nodiscard]] auto internalAgentPath() const -> std::string {
return agent_->path_;
};

private:
enum class InputType : std::uint8_t {
Expand Down
2 changes: 2 additions & 0 deletions include/amarula/dbus/gdbus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DBus {
unsigned int pending_calls_{0};
GDBusConnection* connection_ = nullptr;
GMainLoop* loop_{nullptr};
GMainContext* ctx_{nullptr};

static auto on_loop_started(gpointer user_data) -> gboolean;

Expand All @@ -35,6 +36,7 @@ class DBus {
void onAnyAsyncStart();

[[nodiscard]] auto connection() const { return connection_; }
[[nodiscard]] auto context() { return ctx_; }
};

} // namespace Amarula::DBus::G
170 changes: 138 additions & 32 deletions include/amarula/dbus/gproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
}

protected:
template <typename Callback>
void connectSignal(const std::string& signal_name, Callback callback,
gpointer user_data) {
struct Data {
GDBusProxy* proxy;
std::string signal_name;
Callback callback;
gpointer user_data;
};

auto* data = new Data{proxy_, signal_name, callback, user_data};

g_main_context_invoke_full(
dbus_->context(), G_PRIORITY_DEFAULT,
[](gpointer user_data) -> gboolean {
auto* data = static_cast<Data*>(user_data);

g_signal_connect(data->proxy, data->signal_name.c_str(),
G_CALLBACK(data->callback), data->user_data);

return G_SOURCE_REMOVE;
},
data,
[](gpointer user_data) { delete static_cast<Data*>(user_data); });
}

void updateProperties(GVariant* properties) {
GVariantIter* iter = g_variant_iter_new(properties);
GVariant* prop = nullptr;
Expand Down Expand Up @@ -135,7 +161,7 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {

void getProperties(PropertiesCallback callback = nullptr) {
auto data = prepareCallback(std::move(callback));
callMethod(proxy_, nullptr, "GetProperties", nullptr,
callMethod(nullptr, "GetProperties", nullptr,
&DBusProxy::get_property_cb, data.release());
}

Expand Down Expand Up @@ -176,23 +202,71 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
dbus_->onAnyAsyncDone();
}

explicit DBusProxy(DBus* dbus, const gchar* name, const gchar* obj_path,
const gchar* interface_name)
explicit DBusProxy(DBus* dbus, const std::string& name,
const std::string& obj_path,
const std::string& interface_name)
: dbus_{dbus} {
GError* err = nullptr;

proxy_ = g_dbus_proxy_new_sync(dbus_->connection(),
G_DBUS_PROXY_FLAGS_NONE, nullptr, name,
obj_path, interface_name, nullptr, &err);
if (proxy_ == nullptr) {
std::string const msg =
"Failed to create proxy: " + std::string(err->message);
g_error_free(err);
throw std::runtime_error(msg);
struct Data {
DBusProxy* proxy;
std::string name;
std::string obj_path;
std::string interface_name;
std::mutex mtx;
std::condition_variable cv;
bool done{false};
std::string error;
};

auto data = Data{this, name, obj_path, interface_name};

g_main_context_invoke_full(
dbus_->context(), G_PRIORITY_HIGH,
[](gpointer user_data) -> gboolean {
auto* data = static_cast<Data*>(user_data);

GError* err = nullptr;

auto flags = static_cast<GDBusProxyFlags>(
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
Comment on lines +230 to +231
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES and G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS prevents the proxy from loading initial properties and subscribing to D-Bus signals. However, the class relies on the g-signal::PropertyChanged signal (which maps to D-Bus signals) to update its internal state. With these flags set, the proxy will not receive any updates, and the props_ member will remain uninitialized or empty. If the goal was to avoid blocking during construction, consider that g_dbus_proxy_new_sync is already a blocking call.

G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION);

data->proxy->proxy_ = g_dbus_proxy_new_sync(
data->proxy->dbus_->connection(), flags, nullptr,
data->name.c_str(), data->obj_path.c_str(),
data->interface_name.c_str(), nullptr, &err);

if (data->proxy->proxy_ == nullptr) {
data->error =
"Failed to create proxy: " + std::string(err->message);
g_error_free(err);
} else {
g_signal_connect(
data->proxy->proxy_, "g-signal::PropertyChanged",
G_CALLBACK(&DBusProxy::on_properties_changed_cb),
data->proxy);
}

return G_SOURCE_REMOVE;
},
&data,
[](gpointer user_data) {
auto* data = static_cast<Data*>(user_data);
{
std::lock_guard<std::mutex> const lock(data->mtx);
data->done = true;
}
data->cv.notify_all();
});
{
std::unique_lock<std::mutex> lock(data.mtx);
data.cv.wait(lock, [&] { return data.done; });
}

if (!data.error.empty()) {
throw std::runtime_error(data.error);
}
g_signal_connect(proxy_, "g-signal::PropertyChanged",
G_CALLBACK(&DBusProxy::on_properties_changed_cb),
this);
}

template <typename T>
Expand Down Expand Up @@ -226,28 +300,60 @@ class DBusProxy : public std::enable_shared_from_this<DBusProxy<Properties>> {
self->template executeCallBack<PropertiesSetCallback>(counter, success);
}

static void setProperty(GDBusProxy* proxy, const gchar* arg_name,
GVariant* arg_value, GCancellable* cancellable,
GAsyncReadyCallback callback, gpointer user_data
void setProperty(const gchar* arg_name, GVariant* arg_value,
GCancellable* cancellable, GAsyncReadyCallback callback,
gpointer user_data

) {
std::array<GVariant*, 2> tuple_elements{
g_variant_new_string(arg_name), g_variant_new_variant(arg_value)};

GVariant* parameters = g_variant_new_tuple(tuple_elements.data(), 2);
g_dbus_proxy_call(proxy, "SetProperty", parameters,
G_DBUS_CALL_FLAGS_NONE, -1, cancellable, callback,
user_data);
}

static void callMethod(GDBusProxy* proxy, GCancellable* cancellable,
const gchar* arg_name, GVariant* parameters,
GAsyncReadyCallback callback, gpointer user_data) {
g_dbus_proxy_call(
proxy, arg_name,
(parameters != nullptr) ? parameters
: g_variant_new_tuple(nullptr, 0),
G_DBUS_CALL_FLAGS_NONE, -1, cancellable, callback, user_data);
callMethod(cancellable, "SetProperty", parameters, callback, user_data);
}

void callMethod(GCancellable* cancellable, const std::string& arg_name,
GVariant* parameters, GAsyncReadyCallback callback,
gpointer user_data) {
struct Data {
GDBusProxy* proxy;
std::string arg_name;
GVariant* parameters;
GCancellable* cancellable;
GAsyncReadyCallback callback;
gpointer user_data;
};

auto data = std::make_unique<Data>(
Data{proxy_, arg_name,
(parameters != nullptr)
? g_variant_ref_sink(parameters)
: g_variant_ref_sink(g_variant_new_tuple(nullptr, 0)),
(cancellable != nullptr)
? static_cast<GCancellable*>(g_object_ref(cancellable))
: nullptr,
callback, user_data});

g_main_context_invoke_full(
dbus_->context(), G_PRIORITY_DEFAULT,
[](gpointer user_data) -> gboolean {
auto* data = static_cast<Data*>(user_data);

g_dbus_proxy_call(data->proxy, data->arg_name.c_str(),
data->parameters, G_DBUS_CALL_FLAGS_NONE, -1,
data->cancellable, data->callback,
data->user_data);

return G_SOURCE_REMOVE;
},
data.release(),
[](gpointer user_data) {
std::unique_ptr<Data> data(static_cast<Data*>(user_data));
g_variant_unref(data->parameters);
if (data->cancellable != nullptr) {
g_object_unref(data->cancellable);
}
});
}
};

Expand Down
Loading
Loading