gboolean rtdbus_send_message_with_reply(DBusMessage *message) { DBusError derror; DBusMessage *reply; gboolean result = TRUE; const char *reply_msg = NULL; dbus_error_init(&derror); reply = dbus_connection_send_with_reply_and_block(rtdbus_connection, message, -1, &derror); UNREF_LOG(dbus_message_unref(message)); if (!reply) { rtdbus_whinge(&derror, _("Unable to send/get reply to D-BUS message")); return FALSE; } if (!dbus_message_get_args(reply, &derror, DBUS_TYPE_STRING, &reply_msg, DBUS_TYPE_INVALID)) { rtdbus_whinge(&derror, _("Unable to get D-BUS reply message")); result = FALSE; } if (strcmp(reply_msg, "OK")) { result = FALSE; g_warning("%s", reply_msg); } UNREF_LOG(dbus_message_unref(reply)); return result; }
gboolean rtdbus_add_signal_rule_and_filter( const char *path, const char *interface, DBusHandleMessageFunction filter_fn) { DBusError derror; char *match_rule; dbus_error_init(&derror); match_rule = g_strdup_printf( "type='signal',path='%s',interface='%s'", path, interface); dbus_bus_add_match(rtdbus_connection, match_rule, &derror); if (dbus_error_is_set(&derror)) { rtdbus_whinge(&derror, _("Unable to add D-BUS signal match rule")); return FALSE; } if (!dbus_connection_add_filter(rtdbus_connection, filter_fn, NULL, NULL)) { rtdbus_whinge(&derror, _("Unable to install D-BUS message filter")); return FALSE; } return TRUE; }
DBusMessage *rtdbus_method_new( const char *bus_name, const char *object_path, const char *interface, const char *method_name, int first_arg_type, ...) { DBusMessage *message; DBusError derror; /* No point in doing anything if D-BUS has broken */ if (!rtdbus_connection) return NULL; dbus_error_init(&derror); message = dbus_message_new_method_call(bus_name, object_path, interface, method_name); if (message) { va_list ap; va_start(ap, first_arg_type); message = rtdbus_append_args_valist(message, first_arg_type, ap); va_end(ap); } else { rtdbus_whinge(&derror, _("Unable to create D-BUS method call message")); } return message; }
gboolean rtdbus_add_rule_and_filter(const char *match_rule, DBusHandleMessageFunction filter_fn, void *user_data) { DBusError derror; dbus_error_init(&derror); dbus_bus_add_match(rtdbus_connection, match_rule, &derror); if (dbus_error_is_set(&derror)) { rtdbus_whinge(&derror, _("Unable to add D-BUS match rule")); return FALSE; } if (!dbus_connection_add_filter(rtdbus_connection, filter_fn, user_data, NULL)) { rtdbus_whinge(&derror, _("Unable to install D-BUS message filter")); return FALSE; } return TRUE; }
gboolean rtdbus_start_service(const char *name, const char *object_path, DBusObjectPathMessageFunction method_handler, gboolean replace) { DBusError derror; DBusObjectPathVTable vtable = { NULL, method_handler, NULL, NULL, NULL, NULL }; guint flags; int result; dbus_error_init(&derror); flags = DBUS_NAME_FLAG_ALLOW_REPLACEMENT | DBUS_NAME_FLAG_DO_NOT_QUEUE; if (replace) flags |= DBUS_NAME_FLAG_REPLACE_EXISTING; result = dbus_bus_request_name(rtdbus_connection, name, flags, &derror); switch (result) { case -1: rtdbus_whinge(&derror, _("Unable to start D-BUS service")); rtdbus_shutdown(); break; case DBUS_REQUEST_NAME_REPLY_EXISTS: return TRUE; default: if (!dbus_connection_register_object_path(rtdbus_connection, object_path, &vtable, NULL)) { /* Currently complaining about handler already being registered * and returning FALSE without setting derror, but still seems * to work OK, so ignore FALSE if derror not set. */ if (dbus_error_is_set(&derror)) { rtdbus_whinge(&derror, _("Unable to listen for D-BUS method calls")); rtdbus_shutdown(); } } break; } return FALSE; }