archeus/packages/linux/dbus/libdbus/helpers.c

63 lines
2.3 KiB
C

#include "arc/linux/dbus/helpers.h"
#include "arc/linux/dbus/dbus.h"
#include <stdio.h>
#include <string.h>
typedef struct ARC_DBus {
DBusConnection *connection;
DBusError error;
const char *handle;
} ARC_DBus;
void ARC_DBusHelper_BusRequestName(ARC_DBus *dbus, const char *interface, const char *objectPath){
int32_t response = dbus_bus_request_name(dbus->connection, interface, DBUS_NAME_FLAG_REPLACE_EXISTING, &(dbus->error));
if(response == -1){
printf("Request name failed %s\n", (dbus->error).message);
dbus_error_free(&(dbus->error));
return;
}
}
void ARC_DBusHelper_BusAddMatch(ARC_DBus *dbus, const char *interface, const char *objectPath, const char *member){
//add response signal
const char *matchRuleCStr = "type='signal',interface='%s',path='%s',member='%s'";
uint32_t matchRuleLen = strlen(matchRuleCStr) + strlen(interface) + strlen(objectPath) + strlen(member);
char matchRule[matchRuleLen];
snprintf(matchRule, matchRuleLen, matchRuleCStr, interface, objectPath, member);
dbus_bus_add_match(dbus->connection, matchRule, &(dbus->error));
if(dbus_error_is_set(&(dbus->error))){
printf("failed to add match rule: %s\n", matchRuleCStr);
dbus_error_free(&(dbus->error));
}
}
void ARC_DBusHelper_HandleSignal(DBusMessage *message){
DBusError error;
dbus_error_init(&error);
uint32_t response;
DBusMessageIter argumentsIter;
dbus_message_iter_init(message, &argumentsIter);
dbus_message_iter_get_basic(&argumentsIter, &response);
printf("Response signal: %u\n", response);
dbus_error_free(&error);
}
void ARC_DBusHelper_AddStringVarientStringToMessageIterDictionary(DBusMessageIter *dictonaryIter, const char *stringKey, const char *varientStringValue){
//TODO: should add arc error checks here
DBusMessageIter keyIter;
dbus_message_iter_open_container(dictonaryIter, DBUS_TYPE_DICT_ENTRY, NULL, &keyIter);
dbus_message_iter_append_basic(&keyIter, DBUS_TYPE_STRING, &stringKey);
DBusMessageIter valueIter;
dbus_message_iter_open_container(&keyIter, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &valueIter);
dbus_message_iter_append_basic(&valueIter, DBUS_TYPE_STRING, &varientStringValue);
dbus_message_iter_close_container(&keyIter, &valueIter);
dbus_message_iter_close_container(dictonaryIter, &keyIter);
}