Files
NetworkManager/examples/C/glib/add-connection-gdbus.c
Thomas Haller 615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00

145 lines
4.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2011, 2014 Red Hat, Inc.
*/
/*
* The example shows how to call the AddConnection() D-Bus method to add a
* connection to the system settings service. It uses GDBus, plus a few defines
* from the NetworkManager headers. Contrast add-connection-libnm, which is
* higher level because it uses libnm.
*
* Compile with:
* gcc -Wall add-connection-gdbus.c -o add-connection-gdbus `pkg-config --cflags --libs libnm uuid`
*/
#include <gio/gio.h>
#include <uuid/uuid.h>
#include <nm-dbus-interface.h>
/* include NetworkManager.h for the defines, but we don't link against
* libnm. */
#include <NetworkManager.h>
/* copied from libnm-core/nm-utils.c */
char *
nm_utils_uuid_generate(void)
{
uuid_t uuid;
char *buf;
buf = g_malloc0(37);
uuid_generate_random(uuid);
uuid_unparse_lower(uuid, &buf[0]);
return buf;
}
static void
add_connection(GDBusProxy *proxy, const char *con_name)
{
GVariantBuilder connection_builder;
GVariantBuilder setting_builder;
char *uuid;
const char *new_con_path;
GVariant *ret;
GError *error = NULL;
/* Initialize connection GVariantBuilder */
g_variant_builder_init(&connection_builder, G_VARIANT_TYPE("a{sa{sv}}"));
/* Build up the 'connection' Setting */
g_variant_builder_init(&setting_builder, G_VARIANT_TYPE("a{sv}"));
uuid = nm_utils_uuid_generate();
g_variant_builder_add(&setting_builder,
"{sv}",
NM_SETTING_CONNECTION_UUID,
g_variant_new_string(uuid));
g_free(uuid);
g_variant_builder_add(&setting_builder,
"{sv}",
NM_SETTING_CONNECTION_ID,
g_variant_new_string(con_name));
g_variant_builder_add(&setting_builder,
"{sv}",
NM_SETTING_CONNECTION_TYPE,
g_variant_new_string(NM_SETTING_WIRED_SETTING_NAME));
g_variant_builder_add(&connection_builder,
"{sa{sv}}",
NM_SETTING_CONNECTION_SETTING_NAME,
&setting_builder);
/* Add the (empty) 'wired' Setting */
g_variant_builder_init(&setting_builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&connection_builder,
"{sa{sv}}",
NM_SETTING_WIRED_SETTING_NAME,
&setting_builder);
/* Build up the 'ipv4' Setting */
g_variant_builder_init(&setting_builder, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&setting_builder,
"{sv}",
NM_SETTING_IP_CONFIG_METHOD,
g_variant_new_string(NM_SETTING_IP4_CONFIG_METHOD_AUTO));
g_variant_builder_add(&connection_builder,
"{sa{sv}}",
NM_SETTING_IP4_CONFIG_SETTING_NAME,
&setting_builder);
/* Call AddConnection with the connection dictionary as argument.
* (g_variant_new() will consume the floating GVariant returned from
* &connection_builder, and g_dbus_proxy_call_sync() will consume the
* floating variant returned from g_variant_new(), so no cleanup is needed.
*/
ret = g_dbus_proxy_call_sync(proxy,
"AddConnection",
g_variant_new("(a{sa{sv}})", &connection_builder),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (ret) {
g_variant_get(ret, "(&o)", &new_con_path);
g_print("Added: %s\n", new_con_path);
g_variant_unref(ret);
} else {
g_dbus_error_strip_remote_error(error);
g_print("Error adding connection: %s\n", error->message);
g_clear_error(&error);
}
}
int
main(int argc, char *argv[])
{
GDBusProxy *proxy;
GError *error = NULL;
/* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */
proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
NM_DBUS_SERVICE,
NM_DBUS_PATH_SETTINGS,
NM_DBUS_INTERFACE_SETTINGS,
NULL,
&error);
if (!proxy) {
g_dbus_error_strip_remote_error(error);
g_print("Could not create NetworkManager D-Bus proxy: %s\n", error->message);
g_error_free(error);
return 1;
}
/* Add a connection */
add_connection(proxy, "__Test connection__");
g_object_unref(proxy);
return 0;
}