+++ /dev/null
-/**
- @file dbus-helper.c
-
- Copyright (C) 2004 Nokia Corporation. All rights reserved.
- Copyright (C) 2003-2009, Jouni Malinen <j@w1.fi> and contributors.
- Copyright (C) 2009 Javier S. Pedro
-
- @author Janne Ylalehto <janne.ylalehto@nokia.com>
- @author Johan Hedberg <johan.hedberg@nokia.com>
- @author Jouni Malinen <j@w1.fi> and contributors
- @author Javier S. Pedro <javispedro@javispedro.com>
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 2 of the License, or (at your
- option) any later version.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-
-*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <glib.h>
-
-#define DBUS_API_SUBJECT_TO_CHANGE
-#include <dbus/dbus.h>
-#include "log.h"
-#include "dbus-helper.h"
-
-DBusMessage *new_dbus_signal(const char *path,
- const char *interface,
- const char *name,
- const char *destination) {
- DBusMessage *signal;
-
- signal = dbus_message_new_signal(path, interface, name);
- if (signal == NULL) {
- die("Out of memory during dbus_message_new_error()");
- }
-
- if (destination) {
- if (!dbus_message_set_destination(signal, destination)) {
- die("Out of memory during dbus_message_set_destination()");
- }
- }
-
- dbus_message_set_no_reply(signal, TRUE);
-
- return signal;
-}
-
-DBusMessage *new_dbus_method_call(const char *service,
- const char *path,
- const char *interface,
- const char *method) {
- DBusMessage *message;
-
- message = dbus_message_new_method_call(service, path, interface, method);
- if (message == NULL) {
- die("Out of memory during dbus_message_new_method_call()");
- }
-
- return message;
-}
-
-DBusMessage *new_dbus_method_return(DBusMessage *message) {
- DBusMessage *reply;
-
- reply = dbus_message_new_method_return(message);
- if (reply == NULL) {
- die("Out of memory during dbus_message_new_method_return()");
- }
-
- return reply;
-}
-
-DBusMessage *new_dbus_error(DBusMessage *message, const char *name) {
- DBusMessage *error;
-
- error = dbus_message_new_error(message, name, NULL);
- if (error == NULL) {
- die("Out of memory during dbus_message_new_error()");
- }
-
- return error;
-}
-
-int send_and_unref(DBusConnection *connection, DBusMessage *message) {
- if (!dbus_connection_send(connection, message, NULL)) {
- dbus_message_unref(message);
- return -1;
- }
-
- dbus_connection_flush(connection);
- dbus_message_unref(message);
-
- return 0;
-}
-
-int send_invalid_args(DBusConnection *connection, DBusMessage *message) {
- DBusMessage *reply;
-
- reply = new_dbus_error(message, DBUS_ERROR_INVALID_ARGS);
-
- return send_and_unref(connection, reply);
-}
-
-void append_dbus_args(DBusMessage *message, int first_arg_type, ...) {
- dbus_bool_t ret;
- va_list ap;
-
- va_start(ap, first_arg_type);
- ret = dbus_message_append_args_valist(message, first_arg_type, ap);
- va_end(ap);
-
- if (ret == FALSE) {
- die("dbus_message_append_args failed");
- }
-}
-
-/**
- * Start a dict in a dbus message. Should be paired with a call to
- * {@link wpa_dbus_dict_close_write}.
- *
- * @param iter A valid dbus message iterator
- * @param iter_dict (out) A dict iterator to pass to further dict functions
- * @return 0 on success, -1 on failure
- *
- */
-int dbus_dict_open_write(DBusMessageIter *iter,
- DBusMessageIter *iter_dict)
-{
- dbus_bool_t result;
-
- if (!iter || !iter_dict)
- return FALSE;
-
- result = dbus_message_iter_open_container(
- iter,
- DBUS_TYPE_ARRAY,
- DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
- DBUS_TYPE_STRING_AS_STRING
- DBUS_TYPE_VARIANT_AS_STRING
- DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
- iter_dict);
-
- return (result ? 0 : -1);
-}
-
-/**
- * End a dict element in a dbus message. Should be paired with
- * a call to {@link wpa_dbus_dict_open_write}.
- *
- * @param iter valid dbus message iterator, same as passed to
- * wpa_dbus_dict_open_write()
- * @param iter_dict a dbus dict iterator returned from
- * {@link wpa_dbus_dict_open_write}
- * @return 0 on success, -1 on failure
- *
- */
-int dbus_dict_close_write(DBusMessageIter *iter,
- DBusMessageIter *iter_dict)
-{
- if (!iter || !iter_dict)
- return FALSE;
-
- return dbus_message_iter_close_container(iter, iter_dict) ? 0 : -1;
-}
-
-static const char * _get_type_as_string_from_type(const int type)
-{
- switch(type) {
- case DBUS_TYPE_BYTE:
- return DBUS_TYPE_BYTE_AS_STRING;
- case DBUS_TYPE_BOOLEAN:
- return DBUS_TYPE_BOOLEAN_AS_STRING;
- case DBUS_TYPE_INT16:
- return DBUS_TYPE_INT16_AS_STRING;
- case DBUS_TYPE_UINT16:
- return DBUS_TYPE_UINT16_AS_STRING;
- case DBUS_TYPE_INT32:
- return DBUS_TYPE_INT32_AS_STRING;
- case DBUS_TYPE_UINT32:
- return DBUS_TYPE_UINT32_AS_STRING;
- case DBUS_TYPE_INT64:
- return DBUS_TYPE_INT64_AS_STRING;
- case DBUS_TYPE_UINT64:
- return DBUS_TYPE_UINT64_AS_STRING;
- case DBUS_TYPE_DOUBLE:
- return DBUS_TYPE_DOUBLE_AS_STRING;
- case DBUS_TYPE_STRING:
- return DBUS_TYPE_STRING_AS_STRING;
- case DBUS_TYPE_OBJECT_PATH:
- return DBUS_TYPE_OBJECT_PATH_AS_STRING;
- case DBUS_TYPE_ARRAY:
- return DBUS_TYPE_ARRAY_AS_STRING;
- default:
- return NULL;
- }
-}
-
-static int _dbus_add_dict_entry_start(
- DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
- const char *key, const int value_type)
-{
- if (!dbus_message_iter_open_container(iter_dict,
- DBUS_TYPE_DICT_ENTRY, NULL,
- iter_dict_entry))
- return FALSE;
-
- if (!dbus_message_iter_append_basic(iter_dict_entry, DBUS_TYPE_STRING,
- &key))
- return FALSE;
-
- return TRUE;
-}
-
-
-static dbus_bool_t _dbus_add_dict_entry_end(
- DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
- DBusMessageIter *iter_dict_val)
-{
- if (!dbus_message_iter_close_container(iter_dict_entry, iter_dict_val))
- return FALSE;
- if (!dbus_message_iter_close_container(iter_dict, iter_dict_entry))
- return FALSE;
-
- return TRUE;
-}
-
-static dbus_bool_t _dbus_add_dict_entry_basic(DBusMessageIter *iter_dict,
- const char *key,
- const int value_type,
- const void *value)
-{
- DBusMessageIter iter_dict_entry, iter_dict_val;
- const char *type_as_string = NULL;
-
- type_as_string = _get_type_as_string_from_type(value_type);
- if (!type_as_string)
- return FALSE;
-
- if (!_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
- key, value_type))
- return FALSE;
-
- if (!dbus_message_iter_open_container(&iter_dict_entry,
- DBUS_TYPE_VARIANT,
- type_as_string, &iter_dict_val))
- return FALSE;
-
- if (!dbus_message_iter_append_basic(&iter_dict_val, value_type, value))
- return FALSE;
-
- if (!_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
- &iter_dict_val))
- return FALSE;
-
- return TRUE;
-}
-
-/**
- * Add a string entry to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * {@link wpa_dbus_dict_open_write}
- * @param key The key of the dict item
- * @param value The string value
- * @return TRUE on success, FALSE on failure
- *
- */
-int dbus_dict_append_string(DBusMessageIter *iter_dict,
- const char *key, const char *value)
-{
- if (!key || !value)
- return FALSE;
- return _dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_STRING,
- &value) ? 0 : -1;
-}
-
-/**
- * Add a 32-bit signed integer to the dict.
- *
- * @param iter_dict A valid DBusMessageIter returned from
- * {@link wpa_dbus_dict_open_write}
- * @param key The key of the dict item
- * @param value The 32-bit signed integer value
- * @return TRUE on success, FALSE on failure
- *
- */
-int dbus_dict_append_int32(DBusMessageIter *iter_dict,
- const char *key,
- const dbus_int32_t value)
-{
- if (!key)
- return FALSE;
- return _dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT32,
- &value) ? 0 : -1;
-}
-