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
2 changes: 2 additions & 0 deletions .github/workflows/code-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
steps:
- name: Checkout target repo
uses: actions/checkout@v4
with:
ref: develop

# --- C/C++ ---
- name: Install dependencies
Expand Down
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
2 changes: 0 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
add_compile_definitions(LCM_LOG_DEFAULT_ENABLED=1)

if(BUILD_CONNMAN)
add_executable(connmanctl_dbus connmanctl.cpp)
target_link_libraries(connmanctl_dbus PRIVATE GConnmanDbus)
Expand Down
30 changes: 15 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 @@ -17,9 +18,10 @@ auto main() -> int {
std::mutex cin_mutex;
std::condition_variable cin_cv;
bool connecting = false;
Amarula::Log::enable(true);
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 +48,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 +119,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 +225,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 +279,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 +311,4 @@ auto main() -> int {
}
std::cout << "Exiting.\n";
return 0;
}
}
6 changes: 3 additions & 3 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 @@ -12,11 +13,10 @@ class Connman;
class Agent {
GDBusNodeInfo *node_info_;
guint registration_id_{0};
GDBusConnection *connection_{nullptr};
DBus *dbus_;
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
2 changes: 1 addition & 1 deletion include/amarula/dbus/connman/gconnman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Connman {
auto operator=(const Connman&) -> Connman& = delete;
Connman(Connman&&) = delete;
auto operator=(Connman&&) -> Connman& = delete;
~Connman() { dbus_.reset(); }
~Connman();

[[nodiscard]] auto clock() const { return clock_; }
[[nodiscard]] auto manager() const { return manager_; }
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
3 changes: 3 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 @@ -33,8 +34,10 @@ class DBus {
void start();
void onAnyAsyncDone();
void onAnyAsyncStart();
void stop();

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

} // namespace Amarula::DBus::G
179 changes: 147 additions & 32 deletions include/amarula/dbus/gproxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,47 @@ 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;
std::mutex mtx;
std::condition_variable cv;
bool done{false};
};

Data 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) {
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; });
}
}
Comment thread
EddyTheCo marked this conversation as resolved.

void updateProperties(GVariant* properties) {
GVariantIter* iter = g_variant_iter_new(properties);
GVariant* prop = nullptr;
Expand Down Expand Up @@ -135,7 +176,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 +217,65 @@ 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;

data->proxy->proxy_ = g_dbus_proxy_new_sync(
data->proxy->dbus_->connection(), G_DBUS_PROXY_FLAGS_NONE,
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 +309,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