71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "dbus_wrapper.h"
|
|
|
|
DBusError dbusError;
|
|
|
|
DBusConnection *conn;
|
|
|
|
void handleDbusError(DBusError *err, const char *message) {
|
|
if (dbus_error_is_set(err)) {
|
|
fprintf(stderr, "DBus Error - %s - %s\n", message, err->message);
|
|
dbus_error_free(err);
|
|
}
|
|
}
|
|
|
|
DBusConnection *dbusConnect(void) {
|
|
dbus_error_init(&dbusError);
|
|
|
|
if (NULL == conn) {
|
|
return conn;
|
|
}
|
|
|
|
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbusError);
|
|
if (dbus_error_is_set(&dbusError)) {
|
|
handleDbusError(&dbusError, "Could not connect");
|
|
dbus_error_free(&dbusError);
|
|
}
|
|
if (NULL == conn) {
|
|
return NULL;
|
|
}
|
|
return conn;
|
|
}
|
|
|
|
bool dbusCallMethod(DBusConnection *conn, const char *bus_name,
|
|
const char *path, const char *iface, const char *method) {
|
|
DBusMessage *msg;
|
|
DBusMessageIter args;
|
|
|
|
msg = dbus_message_new_method_call(bus_name, path, iface, method);
|
|
if (NULL == msg) {
|
|
handleDbusError(&dbusError, "Could not initialize message");
|
|
return false;
|
|
}
|
|
|
|
// append arguments
|
|
dbus_message_iter_init_append(msg, &args);
|
|
int val = 0;
|
|
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &val)) {
|
|
handleDbusError(&dbusError, "Could not append argument to method call");
|
|
return false;
|
|
}
|
|
|
|
if (!dbus_connection_send_with_reply_and_block(
|
|
conn, msg, -1, &dbusError)) { // -1 is default timeout
|
|
handleDbusError(&dbusError, "Could not send message");
|
|
dbus_message_unref(msg);
|
|
return false;
|
|
}
|
|
|
|
dbus_connection_flush(conn);
|
|
|
|
// free message
|
|
dbus_message_unref(msg);
|
|
|
|
dbus_connection_close(conn);
|
|
|
|
return true;
|
|
}
|