Thursday, September 7, 2023

BlueChi, dbus-devel and C client

 

dnf install gcc dbus-devel -y
gcc -o list_nodes list_nodes.c -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include/ -ldbus-1
#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
void handle_structure(DBusMessageIter *structIter) {
int typeInStruct;
while ((typeInStruct = dbus_message_iter_get_arg_type(structIter)) != DBUS_TYPE_INVALID) {
if (typeInStruct == DBUS_TYPE_STRING) {
char *value;
dbus_message_iter_get_basic(structIter, &value);
printf("%s\n", value);
} else if (typeInStruct == DBUS_TYPE_OBJECT_PATH) {
char *path;
dbus_message_iter_get_basic(structIter, &path);
printf("Path: %s\n", path);
}
dbus_message_iter_next(structIter);
}
printf("\n");
}
int main() {
DBusConnection *conn;
DBusError err;
DBusMessage *msg;
DBusMessageIter args;
dbus_error_init(&err);
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error: %s\n", err.message);
dbus_error_free(&err);
return 1;
}
msg = dbus_message_new_method_call(
"org.eclipse.bluechi",
"/org/eclipse/bluechi",
"org.eclipse.bluechi.Manager",
"ListNodes");
if (msg == NULL) {
fprintf(stderr, "Message Null\n");
return 1;
}
DBusMessage *reply = dbus_connection_send_with_reply_and_block(
conn,
msg,
-1,
&err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Send Error: %s\n", err.message);
dbus_error_free(&err);
return 1;
}
if (!reply) {
fprintf(stderr, "Reply Null\n");
return 1;
}
if (!dbus_message_iter_init(reply, &args)) {
fprintf(stderr, "Message has no arguments!\n");
} else {
int argType = dbus_message_iter_get_arg_type(&args);
if (argType == DBUS_TYPE_ARRAY) {
DBusMessageIter subArgs;
dbus_message_iter_recurse(&args, &subArgs);
while ((argType = dbus_message_iter_get_arg_type(&subArgs)) != DBUS_TYPE_INVALID) {
if (argType == DBUS_TYPE_STRUCT) {
DBusMessageIter structArgs;
dbus_message_iter_recurse(&subArgs, &structArgs);
handle_structure(&structArgs);
} else {
printf("Unexpected item: %d\n", argType);
}
dbus_message_iter_next(&subArgs);
}
} else {
fprintf(stderr, "Argument is not an array!\n");
}
}
dbus_message_unref(msg);
dbus_message_unref(reply);
dbus_connection_unref(conn);
return 0;
}

Bluechi and C++ client.

dnf install g++ dbus-devel -y
g++ -o list_nodes list_nodes.cpp
-I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include/ -ldbus-1


#include <dbus/dbus.h>

#include <iostream>


void handle_structure(DBusMessageIter *structIter) {

    int typeInStruct;

    while ((typeInStruct = dbus_message_iter_get_arg_type(

                structIter)) != DBUS_TYPE_INVALID) {

        if (typeInStruct == DBUS_TYPE_STRING) {

            char *value;

            dbus_message_iter_get_basic(structIter, &value);

            std::cout << "" << value << std::endl;

        } else if (typeInStruct == DBUS_TYPE_OBJECT_PATH) {

            char *path;

            dbus_message_iter_get_basic(structIter, &path);

            std::cout << "Path: " << path << std::endl;

        }

        dbus_message_iter_next(structIter);

    }

    std::cout << "" << std::endl;

}



int main() {

    DBusConnection *conn;

    DBusError err;

    DBusMessage *msg;

    DBusMessageIter args;


    dbus_error_init(&err);


    conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);

    if (dbus_error_is_set(&err)) {

        std::cerr << "Connection Error: " << err.message << std::endl;

        dbus_error_free(&err);

        return 1;

    }


    msg = dbus_message_new_method_call(

        "org.eclipse.bluechi",

        "/org/eclipse/bluechi",

        "org.eclipse.bluechi.Manager",

        "ListNodes");


    if (msg == nullptr) {

        std::cerr << "Message Null" << std::endl;

        return 1;

    }


    DBusMessage *reply = dbus_connection_send_with_reply_and_block(

        conn,

        msg,

        -1,

        &err);


    if (dbus_error_is_set(&err)) {

        std::cerr << "Send Error: " << err.message << std::endl;

        dbus_error_free(&err);

        return 1;

    }


    if (!reply) {

        std::cerr << "Reply Null" << std::endl;

        return 1;

    }


    if (!dbus_message_iter_init(reply, &args)) {

        std::cerr << "Message has no arguments!" << std::endl;

    } else {

        int argType = dbus_message_iter_get_arg_type(&args);


        if (argType == DBUS_TYPE_ARRAY) {

            DBusMessageIter subArgs;

            dbus_message_iter_recurse(&args, &subArgs);


            while ((argType = dbus_message_iter_get_arg_type(

                    &subArgs)) != DBUS_TYPE_INVALID) {

                if (argType == DBUS_TYPE_STRUCT) {

                    DBusMessageIter structArgs;

                    dbus_message_iter_recurse(&subArgs, &structArgs);

                    handle_structure(&structArgs);

                } else {

                    std::cout << "Unexpected item: " << argType << std::endl;

                }


                dbus_message_iter_next(&subArgs);

            }

        } else {

            std::cerr << "Argument is not an array!" << std::endl;

        }

    }


    dbus_message_unref(msg);

    dbus_message_unref(reply);

    dbus_connection_unref(conn);


    return 0;

}



Tuesday, September 5, 2023

Bluechi release v0.5.0

Highlights

  • New project name: BlueChi
  • License changes:
    • BlueChi project from GPL-2.0-or-later to LGPL-2.1-or-later
    • API examples to CC0-1.0
    • Typed python bindings package to CC0-1.0
  • New feature: freeze and thaw units
  • Improved configuration file parsing:
    • Increased maximum line length to 500
    • Added multi-line support
    • Clearer error messages when parsing fails
  • Typed python bindings: property annotation for D-Bus property added
  • Bug fixes:
    • heartbeat interval of 0 disables it instead of spamming the signal
    • disconnecting, anonymous node doesn't lead to a segfault anymore
    • some minor memory leaks have been fixed
  • Examples for using GoLang to access the D-Bus API have been added
  • Examples for using Rust to access the D-Bus API have been added
  • Added CLI option for all binaries to print the version
  • Changed the default log target to be journald

What's Changed

New Contributors

Full Changelogv0.4.0...v0.5.0

 https://github.com/containers/bluechi/releases/tag/v0.5.0