diff options
-rw-r--r-- | client/data_manager.c | 246 | ||||
-rw-r--r-- | client/data_manager.h | 51 |
2 files changed, 297 insertions, 0 deletions
diff --git a/client/data_manager.c b/client/data_manager.c new file mode 100644 index 00000000..723b9790 --- /dev/null +++ b/client/data_manager.c @@ -0,0 +1,246 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#include <glib.h> + +#include "src/connman.h" +#include "client/services.h" +#include "client/technology.h" +#include "client/data_manager.h" +#include "gdbus/gdbus.h" +#include "include/dbus.h" + +static void extract_manager_properties(DBusMessage *message) +{ + DBusMessageIter iter, array; + + dbus_message_iter_init(message, &iter); + dbus_message_iter_recurse(&iter, &array); + extract_properties(&array); +} + +int store_proxy_input(DBusConnection *connection, DBusMessage *message, + char *name, int num_args, char *argv[]) +{ + int i, j, k; + int error = 0; + gchar **servers = NULL; + gchar **excludes = NULL; + + for (i = 0; strcmp(argv[i], "excludes") != 0; i++) { + servers = g_try_realloc(servers, (i + 1) * sizeof(char *)); + if (servers == NULL) { + fprintf(stderr, "Could not allocate memory for list\n"); + return -ENOMEM; + } + servers[i] = g_strdup(argv[i]); + /* In case the user doesn't enter "excludes" */ + if (i + 1 == num_args) { + i++; + j = 0; + goto free_servers; + } + } + for (j = 0; j + (i + 1) != num_args; j++) { + excludes = g_try_realloc(excludes, (j + 1) * sizeof(char *)); + if (excludes == NULL) { + fprintf(stderr, "Could not allocate memory for list\n"); + return -ENOMEM; + } + excludes[j] = g_strdup(argv[j + (i + 1)]); + } + +free_servers: + error = set_proxy_manual(connection, message, name, servers, excludes, + i, j); + + for (k = 0; k < j - 1; k++) + g_free(excludes[k]); + g_free(excludes); + + for (k = 0; k < i - 1; k++) + g_free(servers[k]); + g_free(servers); + + return error; +} + +int connect_service(DBusConnection *connection, char *name) +{ + DBusMessage *message, *message_connect; + struct service_data service; + char *path; + const char *path_name; + DBusError err; + + message = get_message(connection, "GetServices"); + if (message == NULL) + return -ENOMEM; + + path_name = find_service(connection, message, name, &service); + if (path_name == NULL) + return -ENXIO; + + path = g_strdup_printf("/net/connman/service/%s", path_name); + message_connect = dbus_message_new_method_call("net.connman", path, + "net.connman.Service", + "Connect"); + if (message_connect == NULL) + return -ENOMEM; + + dbus_error_init(&err); + dbus_connection_send_with_reply_and_block(connection, message_connect, + -1, &err); + + if (dbus_error_is_set(&err)) { + printf("Connection failed; error: '%s'\n", err.message); + return -EINVAL; + } + + dbus_connection_send(connection, message_connect, NULL); + dbus_connection_flush(connection); + dbus_message_unref(message_connect); + g_free(path); + + return 0; +} + +int disconnect_service(DBusConnection *connection, char *name) +{ + DBusMessage *message, *message_disconnect; + struct service_data service; + char *path; + const char *path_name; + DBusError err; + + message = get_message(connection, "GetServices"); + if (message == NULL) + return -ENOMEM; + + path_name = find_service(connection, message, name, &service); + if (path_name == NULL) + return -ENXIO; + + path = g_strdup_printf("/net/connman/service/%s", path_name); + printf("%s\n", path); + message_disconnect = dbus_message_new_method_call("net.connman", path, + "net.connman.Service", + "Disconnect"); + if (message_disconnect == NULL) + return -ENOMEM; + + dbus_error_init(&err); + dbus_connection_send_with_reply_and_block(connection, + message_disconnect, + -1, &err); + + if (dbus_error_is_set(&err)) { + printf("Connection failed; error: '%s'\n", err.message); + return -EINVAL; + } + + dbus_connection_send(connection, message_disconnect, NULL); + dbus_connection_flush(connection); + dbus_message_unref(message_disconnect); + g_free(path); + + return 0; +} + +int set_manager(DBusConnection *connection, char *key, dbus_bool_t value) +{ + DBusMessage *message; + DBusMessageIter iter; + + message = dbus_message_new_method_call("net.connman", "/", + "net.connman.Manager", + "SetProperty"); + if (message == NULL) + return -ENOMEM; + + dbus_message_iter_init_append(message, &iter); + connman_dbus_property_append_basic(&iter, (const char *) key, + DBUS_TYPE_BOOLEAN, &value); + dbus_connection_send(connection, message, NULL); + dbus_connection_flush(connection); + dbus_message_unref(message); + + return 0; +} + +/* Call with any given function we want connman to respond to */ +DBusMessage *get_message(DBusConnection *connection, char *function) +{ + DBusMessage *message, *reply; + DBusError error; + + message = dbus_message_new_method_call(CONNMAN_SERVICE, + CONNMAN_MANAGER_PATH, + CONNMAN_MANAGER_INTERFACE, + function); + if (message == NULL) + return NULL; + + dbus_error_init(&error); + + reply = dbus_connection_send_with_reply_and_block(connection, + message, -1, &error); + if (dbus_error_is_set(&error) == TRUE) { + fprintf(stderr, "%s\n", error.message); + dbus_error_free(&error); + } else if (reply == NULL) + fprintf(stderr, "Failed to receive message\n"); + + dbus_message_unref(message); + + return reply; +} + +int list_properties(DBusConnection *connection, char *function, + char *service_name) +{ + DBusMessage *message; + + message = get_message(connection, function); + if (message == NULL) + return -ENOMEM; + + if (strcmp(function, "GetProperties") == 0) + extract_manager_properties(message); + + else if (strcmp(function, "GetServices") == 0 && service_name != NULL) + extract_services(message, service_name); + + else if (strcmp(function, "GetServices") == 0 && service_name == NULL) + get_services(message); + + else if (strcmp(function, "GetTechnologies") == 0) + extract_tech(message); + + dbus_message_unref(message); + + return 0; +} diff --git a/client/data_manager.h b/client/data_manager.h new file mode 100644 index 00000000..dead9e0a --- /dev/null +++ b/client/data_manager.h @@ -0,0 +1,51 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CLIENT_DATA_MANAGER_H +#define __CLIENT_DATA_MANAGER_H + +#include <dbus/dbus.h> + +#define SIGNAL_LISTEN_TIMEOUT 10 +#define CONNMAN_MANAGER_PATH "/" +#define CONNMAN_SERVICE "net.connman" + +struct signal_args { + DBusConnection *connection; + const char *signal_name; +}; + +struct proxy_input { + char *servers; + char *excludes; +}; + +DBusMessage *get_message(DBusConnection *connection, char *function); +int store_proxy_input(DBusConnection *connection, DBusMessage *message, + char *name, int num_args, char *argv[]); +int list_properties(DBusConnection *connection, char *function, + char *service_name); +int connect_service(DBusConnection *connection, char *name); +int disconnect_service(DBusConnection *connection, char *name); +int set_manager(DBusConnection *connection, char *key, dbus_bool_t value); +void listen_for_manager_signal(void *args); + +#endif |