dnsproxy: Fix unused variable warnings
[framework/connectivity/connman.git] / src / profile.c
index 3cf1e1e..a16336e 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2010  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
 #include <config.h>
 #endif
 
+#include <string.h>
+
 #include <glib.h>
 #include <gdbus.h>
 
 #include "connman.h"
 
-#define PROFILE_DEFAULT  "/profile/default"
-
-enum connman_service_state {
-       CONNMAN_SERVICE_STATE_UNKNOWN = 0,
-       CONNMAN_SERVICE_STATE_IDLE    = 1,
-};
+#define PROFILE_DEFAULT_IDENT  "default"
 
-struct connman_group {
+struct connman_profile {
+       char *ident;
        char *path;
-       char *type;
        char *name;
-       char *mode;
-       char *security;
-       connman_uint8_t strength;
-       connman_bool_t favorite;
-       enum connman_service_state state;
-       struct connman_network *network;
+       connman_bool_t offlinemode;
 };
 
-static GHashTable *groups = NULL;
+static struct connman_profile *default_profile = NULL;
 
 static DBusConnection *connection = NULL;
 
-static const char *state2string(enum connman_service_state state)
+static void offlinemode_changed(struct connman_profile *profile)
 {
-       switch (state) {
-       case CONNMAN_SERVICE_STATE_UNKNOWN:
-               break;
-       case CONNMAN_SERVICE_STATE_IDLE:
-               return "idle";
-       }
-
-       return NULL;
+       connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
+                               CONNMAN_MANAGER_INTERFACE, "OfflineMode",
+                               DBUS_TYPE_BOOLEAN, &profile->offlinemode);
 }
 
-static DBusMessage *get_properties(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
+connman_bool_t __connman_profile_get_offlinemode(void)
 {
-       struct connman_group *group = data;
-       DBusMessage *reply;
-       DBusMessageIter array, dict;
-       const char *str;
-
-       DBG("conn %p", conn);
-
-       reply = dbus_message_new_method_return(msg);
-       if (reply == NULL)
-               return NULL;
-
-       dbus_message_iter_init_append(reply, &array);
-
-       dbus_message_iter_open_container(&array, 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, &dict);
-
-       str = state2string(group->state);
-       if (str != NULL)
-               connman_dbus_dict_append_variant(&dict, "State",
-                                               DBUS_TYPE_STRING, &str);
-
-       if (group->type != NULL)
-               connman_dbus_dict_append_variant(&dict, "Type",
-                                       DBUS_TYPE_STRING, &group->type);
-
-       if (group->name != NULL)
-               connman_dbus_dict_append_variant(&dict, "Name",
-                                       DBUS_TYPE_STRING, &group->name);
+       if (default_profile == NULL)
+               return FALSE;
 
-       if (group->mode != NULL)
-               connman_dbus_dict_append_variant(&dict, "Mode",
-                                       DBUS_TYPE_STRING, &group->mode);
+       DBG("offlinemode %d", default_profile->offlinemode);
 
-       if (group->security != NULL)
-               connman_dbus_dict_append_variant(&dict, "Security",
-                                       DBUS_TYPE_STRING, &group->security);
+       return default_profile->offlinemode;
+}
 
-       if (group->strength > 0)
-               connman_dbus_dict_append_variant(&dict, "Strength",
-                                       DBUS_TYPE_BYTE, &group->strength);
+int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
+                                       connman_bool_t all_devices)
+{
+       DBG("offlinemode %d", offlinemode);
 
-       connman_dbus_dict_append_variant(&dict, "Favorite",
-                                       DBUS_TYPE_BOOLEAN, &group->favorite);
+       if (default_profile == NULL)
+               return -EINVAL;
 
-       dbus_message_iter_close_container(&array, &dict);
+       if (default_profile->offlinemode == offlinemode)
+               return -EALREADY;
 
-       return reply;
-}
+       default_profile->offlinemode = offlinemode;
+       offlinemode_changed(default_profile);
 
-static DBusMessage *connect_service(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
-{
-       return __connman_error_not_implemented(msg);
-}
+       if (all_devices)
+               __connman_device_set_offlinemode(offlinemode);
 
-static DBusMessage *disconnect_service(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
-{
-       return __connman_error_not_implemented(msg);
+       return 0;
 }
 
-static DBusMessage *remove_service(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
+int __connman_profile_save_default(void)
 {
-       return __connman_error_not_implemented(msg);
-}
+       DBG("");
 
-static DBusMessage *move_before(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
-{
-       return __connman_error_not_implemented(msg);
-}
+       if (default_profile != NULL)
+               __connman_storage_save_profile(default_profile);
 
-static DBusMessage *move_after(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
-{
-       return __connman_error_not_implemented(msg);
+       return 0;
 }
 
-static GDBusMethodTable service_methods[] = {
-       { "GetProperties", "",  "a{sv}", get_properties     },
-       { "Connect",       "",  "",      connect_service    },
-       { "Disconnect",    "",  "",      disconnect_service },
-       { "Remove",        "",  "",      remove_service     },
-       { "MoveBefore",    "o", "",      move_before        },
-       { "MoveAfter",     "o", "",      move_after         },
-       { },
-};
-
-static GDBusSignalTable service_signals[] = {
-       { "PropertyChanged", "sv" },
-       { },
-};
-
-static void free_group(gpointer data)
+const char *__connman_profile_active_ident(void)
 {
-       struct connman_group *group = data;
-
-       DBG("group %p", group);
-
-       g_dbus_unregister_interface(connection, group->path,
-                                               CONNMAN_SERVICE_INTERFACE);
+       DBG("");
 
-       g_free(group->security);
-       g_free(group->mode);
-       g_free(group->name);
-       g_free(group->type);
-       g_free(group->path);
-       g_free(group);
+       return PROFILE_DEFAULT_IDENT;
 }
 
-static struct connman_group *lookup_group(const char *name)
+const char *__connman_profile_active_path(void)
 {
-       struct connman_group *group;
-
-       DBG("name %s", name);
-
-       if (name == NULL)
-               return NULL;
-
-       group = g_hash_table_lookup(groups, name);
-       if (group != NULL)
-               goto done;
+       DBG("");
 
-       group = g_try_new0(struct connman_group, 1);
-       if (group == NULL)
+       if (default_profile == NULL)
                return NULL;
 
-       group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
-       group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
-
-       group->favorite = FALSE;
-
-       group->state = CONNMAN_SERVICE_STATE_IDLE;
-
-       g_hash_table_insert(groups, g_strdup(name), group);
-
-       g_dbus_register_interface(connection, group->path,
-                                       CONNMAN_SERVICE_INTERFACE,
-                                       service_methods, service_signals,
-                                                       NULL, group, NULL);
-
-done:
-       DBG("group %p", group);
-
-       return group;
+       return default_profile->path;
 }
 
-int __connman_profile_add_device(struct connman_device *device)
-{
-       struct connman_group *group;
-       char *name;
-
-       DBG("device %p", device);
-
-       name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
-                                       connman_device_get_index(device));
-       group = lookup_group(name);
-       g_free(name);
-
-       if (group == NULL)
-               return -EINVAL;
-
-       group->type = g_strdup(__connman_device_get_type(device));
-
-       return 0;
-}
+static guint changed_timeout = 0;
 
-int __connman_profile_remove_device(struct connman_device *device)
+static gboolean services_changed(gpointer user_data)
 {
-       struct connman_group *group;
-       char *name;
-
-       DBG("device %p", device);
+       changed_timeout = 0;
 
-       name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
-                                       connman_device_get_index(device));
-       group = lookup_group(name);
-       g_free(name);
+       if (default_profile == NULL)
+               return FALSE;
 
-       if (group == NULL)
-               return -EINVAL;
+       connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
+                               CONNMAN_MANAGER_INTERFACE, "Services",
+                               DBUS_TYPE_OBJECT_PATH, __connman_service_list,
+                               NULL);
 
-       return 0;
+       return FALSE;
 }
 
-int __connman_profile_add_network(struct connman_network *network)
+void __connman_profile_changed(gboolean delayed)
 {
-       struct connman_group *group;
-
-       DBG("network %p", network);
-
-       group = lookup_group(__connman_network_get_group(network));
-       if (group == NULL)
-               return -EINVAL;
-
-       g_free(group->type);
-       g_free(group->name);
-
-       group->type = g_strdup(__connman_network_get_type(network));
-       group->name = g_strdup(connman_network_get_string(network, "Name"));
+       DBG("");
 
-       group->strength = connman_network_get_uint8(network, "Strength");
+       if (changed_timeout > 0) {
+               g_source_remove(changed_timeout);
+               changed_timeout = 0;
+       }
 
-       if (group->network == NULL) {
-               group->network = network;
+       if (__connman_connection_update_gateway() == TRUE) {
+               services_changed(NULL);
+               return;
+       }
 
-               group->mode = g_strdup(connman_network_get_string(network,
-                                                               "WiFi.Mode"));
-               group->security = g_strdup(connman_network_get_string(network,
-                                                       "WiFi.Security"));
+       if (delayed == FALSE) {
+               services_changed(NULL);
+               return;
        }
 
-       return 0;
+       changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
 }
 
-int __connman_profile_remove_network(struct connman_network *network)
+static void free_profile(struct connman_profile *profile)
 {
-       struct connman_group *group;
-
-       DBG("network %p", network);
-
-       group = lookup_group(__connman_network_get_group(network));
-       if (group == NULL)
-               return -EINVAL;
-
-       if (group->network == network) {
-               g_free(group->security);
-               group->security = NULL;
-
-               g_free(group->mode);
-               group->mode = NULL;
-
-               group->network = NULL;
-       }
-
-       return 0;
+       g_free(profile->name);
+       g_free(profile->path);
+       g_free(profile->ident);
+       g_free(profile);
 }
 
-const char *__connman_profile_active(void)
+static int profile_init(void)
 {
        DBG("");
 
-       return PROFILE_DEFAULT;
-}
+       default_profile = g_try_new0(struct connman_profile, 1);
+       if (default_profile == NULL)
+               return -ENOMEM;
 
-void __connman_profile_list(DBusMessageIter *iter)
-{
-       const char *path = __connman_profile_active();
+       default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
+       default_profile->path = g_strdup_printf("/profile/%s",
+                                       PROFILE_DEFAULT_IDENT);
 
-       DBG("");
+       if (default_profile->ident == NULL || default_profile->path == NULL) {
+               free_profile(default_profile);
+               return -ENOMEM;
+       }
 
-       dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
-}
+       default_profile->name = g_strdup("Default");
 
-static void append_path(gpointer key, gpointer value, gpointer user_data)
-{
-       struct connman_group *group = value;
-       DBusMessageIter *iter = user_data;
+       __connman_storage_load_profile(default_profile);
 
-       dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
-                                                       &group->path);
-}
+       connman_info("Adding default profile");
 
-void __connman_profile_list_services(DBusMessageIter *iter)
-{
-       DBG("");
+       DBG("profile %p path %s", default_profile, default_profile->path);
 
-       g_hash_table_foreach(groups, append_path, iter);
+       return 0;
 }
 
-static void append_services(DBusMessageIter *dict)
+static int profile_load(struct connman_profile *profile)
 {
-       DBusMessageIter entry, value, iter;
-       const char *key = "Services";
+       GKeyFile *keyfile;
+       GError *error = NULL;
+       connman_bool_t offlinemode;
+       char *name;
 
-       dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
-                                                               NULL, &entry);
+       DBG("profile %p", profile);
 
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
+       keyfile = __connman_storage_open_profile(profile->ident);
+       if (keyfile == NULL)
+               return -EIO;
 
-       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
-               DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
-                                                               &value);
+       name = g_key_file_get_string(keyfile, "global", "Name", NULL);
+       if (name != NULL) {
+               g_free(profile->name);
+               profile->name = name;
+       }
 
-       dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
-                               DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
-       __connman_profile_list_services(&iter);
-       dbus_message_iter_close_container(&value, &iter);
+       offlinemode = g_key_file_get_boolean(keyfile, "global",
+                                               "OfflineMode", &error);
+       if (error == NULL)
+               profile->offlinemode = offlinemode;
+       g_clear_error(&error);
 
-       dbus_message_iter_close_container(&entry, &value);
+       __connman_storage_close_profile(profile->ident, keyfile, FALSE);
 
-       dbus_message_iter_close_container(dict, &entry);
+       return 0;
 }
 
-static DBusMessage *profile_properties(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
+static int profile_save(struct connman_profile *profile)
 {
-       const char *name = "Default";
-       DBusMessage *reply;
-       DBusMessageIter array, dict;
-
-       DBG("conn %p", conn);
+       GKeyFile *keyfile;
 
-       reply = dbus_message_new_method_return(msg);
-       if (reply == NULL)
-               return NULL;
-
-       dbus_message_iter_init_append(reply, &array);
+       DBG("profile %p", profile);
 
-       dbus_message_iter_open_container(&array, 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, &dict);
+       keyfile = __connman_storage_open_profile(profile->ident);
+       if (keyfile == NULL)
+               return -EIO;
 
-       connman_dbus_dict_append_variant(&dict, "Name",
-                                               DBUS_TYPE_STRING, &name);
+       if (profile->name != NULL)
+               g_key_file_set_string(keyfile, "global",
+                                               "Name", profile->name);
 
-       append_services(&dict);
+       g_key_file_set_boolean(keyfile, "global",
+                                       "OfflineMode", profile->offlinemode);
 
-       dbus_message_iter_close_container(&array, &dict);
+       __connman_storage_close_profile(profile->ident, keyfile, TRUE);
 
-       return reply;
+       return 0;
 }
 
-static GDBusMethodTable profile_methods[] = {
-       { "GetProperties", "", "a{sv}", profile_properties },
-       { },
+static struct connman_storage profile_storage = {
+       .name           = "profile",
+       .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
+       .profile_init   = profile_init,
+       .profile_load   = profile_load,
+       .profile_save   = profile_save,
 };
 
-int __connman_profile_init(DBusConnection *conn)
+int __connman_profile_init(void)
 {
-       DBG("conn %p", conn);
+       DBG("");
 
-       connection = dbus_connection_ref(conn);
+       connection = connman_dbus_get_connection();
        if (connection == NULL)
                return -1;
 
-       groups = g_hash_table_new_full(g_str_hash, g_str_equal,
-                                                       g_free, free_group);
-
-       g_dbus_register_interface(connection, PROFILE_DEFAULT,
-                                               CONNMAN_PROFILE_INTERFACE,
-                                               profile_methods,
-                                               NULL, NULL, NULL, NULL);
+       if (connman_storage_register(&profile_storage) < 0)
+               connman_error("Failed to register profile storage");
 
        return 0;
 }
 
 void __connman_profile_cleanup(void)
 {
-       DBG("conn %p", connection);
-
-       g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
-                                               CONNMAN_PROFILE_INTERFACE);
-
-       g_hash_table_destroy(groups);
-       groups = NULL;
+       DBG("");
 
        if (connection == NULL)
                return;
 
+       connman_storage_unregister(&profile_storage);
+
        dbus_connection_unref(connection);
 }