Hi,
On Fri, 2012-09-07 at 17:26 -0700, Tudor Marcu wrote:
> The data manager connects/disconnects with services, lists multiple properties
> of connman, and sets manager properties. The function get_message is important
> as it is used to send any method request to connman, and returns the reply
> that
> several functions use. This file should mainly cover setting global properties
> (i.e offlinemode), or handling connections.
> ---
> client/data_manager.c | 250
> +++++++++++++++++++++++++++++++++++++++++++++++++
> client/data_manager.h | 52 ++++++++++
> 2 files changed, 302 insertions(+)
> create mode 100644 client/data_manager.c
> create mode 100644 client/data_manager.h
>
> diff --git a/client/data_manager.c b/client/data_manager.c
> new file mode 100644
> index 0000000..1ab75bb
> --- /dev/null
> +++ b/client/data_manager.c
> @@ -0,0 +1,250 @@
> +/*
> + *
> + * 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 "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;
> +
> + gchar **servers = g_try_malloc(0);
> + gchar **excludes = g_try_malloc(0);
Just set servers and excludes to NULL.
> +
> + for (i = 0; strcmp(argv[i+5], "excludes"); i++) {
Instead of adding 5 to i, is it possible to give argv[i+5] as input to
this function? Also please spell out the for statement condition, it
seems to be 'strcmp(...) != 0' now.
> + 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+5]);
> + /* In case the user doesn't enter "excludes" */
> + if (i + 6 == num_args) {
> + i++;
> + j = 0;
> + goto free_servers;
> + }
> + }
> + for (j = 0; j + (i + 6) != 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 + 6)]);
> + }
> +
> +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);
This frees excludes, but the individual g_strdup'ed entries in
excludes[] are lost. Please use e.g. g_strfreev.
> +
> + for (k = 0; k < i - 1; k++)
> + g_free(servers[k]);
> + g_free(servers);
Same here.
> +
> + if (error != 0)
> + return error;
> + return 0;
Set error to 0 when it is declared and then just return error; here.
> +}
> +
> +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;
> +}
> +
Extra empty line end of file, please remove.
> diff --git a/client/data_manager.h b/client/data_manager.h
> new file mode 100644
> index 0000000..e5d55a4
> --- /dev/null
> +++ b/client/data_manager.h
> @@ -0,0 +1,52 @@
> +/*
> + *
> + * 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
> +
Extra empty line end of file, please remove.
Cheers,
Patrik
_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman