*
* 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"
+#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;
- struct connman_network *network;
+ connman_bool_t offlinemode;
};
-static GHashTable *groups = NULL;
+static struct connman_profile *default_profile = NULL;
static DBusConnection *connection = NULL;
-static DBusMessage *get_properties(DBusConnection *conn,
- DBusMessage *msg, void *data)
+static void offlinemode_changed(struct connman_profile *profile)
{
- struct connman_group *group = data;
- DBusMessage *reply;
- DBusMessageIter array, dict;
-
- 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);
-
- 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 (group->mode != NULL)
- connman_dbus_dict_append_variant(&dict, "Mode",
- DBUS_TYPE_STRING, &group->mode);
-
- if (group->security != NULL)
- connman_dbus_dict_append_variant(&dict, "Security",
- DBUS_TYPE_STRING, &group->security);
-
- if (group->strength > 0)
- connman_dbus_dict_append_variant(&dict, "Strength",
- DBUS_TYPE_BYTE, &group->strength);
-
- connman_dbus_dict_append_variant(&dict, "Favorite",
- DBUS_TYPE_BOOLEAN, &group->favorite);
-
- dbus_message_iter_close_container(&array, &dict);
-
- return reply;
+ connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
+ CONNMAN_MANAGER_INTERFACE, "OfflineMode",
+ DBUS_TYPE_BOOLEAN, &profile->offlinemode);
}
-static GDBusMethodTable service_methods[] = {
- { "GetProperties", "", "a{sv}", get_properties },
- { },
-};
-
-static void free_group(gpointer data)
+connman_bool_t __connman_profile_get_offlinemode(void)
{
- struct connman_group *group = data;
+ if (default_profile == NULL)
+ return FALSE;
- DBG("group %p", group);
+ DBG("offlinemode %d", default_profile->offlinemode);
- g_dbus_unregister_interface(connection, group->path,
- CONNMAN_SERVICE_INTERFACE);
-
- g_free(group->security);
- g_free(group->mode);
- g_free(group->name);
- g_free(group->type);
- g_free(group->path);
- g_free(group);
+ return default_profile->offlinemode;
}
-static struct connman_group *lookup_group(const char *name)
+int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
+ connman_bool_t all_devices)
{
- 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("offlinemode %d", offlinemode);
- group = g_try_new0(struct connman_group, 1);
- if (group == NULL)
- return NULL;
-
- group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
- group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
-
- group->favorite = FALSE;
+ if (default_profile == NULL)
+ return -EINVAL;
- g_hash_table_insert(groups, g_strdup(name), group);
+ if (default_profile->offlinemode == offlinemode)
+ return -EALREADY;
- g_dbus_register_interface(connection, group->path,
- CONNMAN_SERVICE_INTERFACE,
- service_methods,
- NULL, NULL, group, NULL);
+ default_profile->offlinemode = offlinemode;
+ offlinemode_changed(default_profile);
-done:
- DBG("group %p", group);
+ if (all_devices)
+ __connman_device_set_offlinemode(offlinemode);
- return group;
+ return 0;
}
-int __connman_profile_add_device(struct connman_device *device)
+int __connman_profile_save_default(void)
{
- 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;
+ DBG("");
- group->type = g_strdup(__connman_device_get_type(device));
+ if (default_profile != NULL)
+ __connman_storage_save_profile(default_profile);
return 0;
}
-int __connman_profile_remove_device(struct connman_device *device)
+const char *__connman_profile_active_ident(void)
{
- 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;
+ DBG("");
- return 0;
+ return PROFILE_DEFAULT_IDENT;
}
-int __connman_profile_add_network(struct connman_network *network)
+const char *__connman_profile_active_path(void)
{
- struct connman_group *group;
-
- DBG("network %p", network);
+ DBG("");
- group = lookup_group(__connman_network_get_group(network));
- if (group == NULL)
- return -EINVAL;
+ if (default_profile == NULL)
+ return NULL;
- g_free(group->type);
- g_free(group->name);
+ return default_profile->path;
+}
- group->type = g_strdup(__connman_network_get_type(network));
- group->name = g_strdup(connman_network_get_string(network, "Name"));
+static guint changed_timeout = 0;
- group->strength = connman_network_get_uint8(network, "Strength");
+static gboolean services_changed(gpointer user_data)
+{
+ changed_timeout = 0;
- if (group->network == NULL) {
- group->network = network;
+ if (default_profile == NULL)
+ return FALSE;
- group->mode = g_strdup(connman_network_get_string(network,
- "WiFi.Mode"));
- group->security = g_strdup(connman_network_get_string(network,
- "WiFi.Security"));
- }
+ 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_remove_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;
+ DBG("");
- if (group->network == network) {
- g_free(group->security);
- group->security = NULL;
+ if (changed_timeout > 0) {
+ g_source_remove(changed_timeout);
+ changed_timeout = 0;
+ }
- g_free(group->mode);
- group->mode = NULL;
+ if (__connman_connection_update_gateway() == TRUE) {
+ services_changed(NULL);
+ return;
+ }
- group->network = NULL;
+ if (delayed == FALSE) {
+ services_changed(NULL);
+ return;
}
- return 0;
+ changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
}
-const char *__connman_profile_active(void)
+static void free_profile(struct connman_profile *profile)
{
- DBG("");
-
- return PROFILE_DEFAULT;
+ g_free(profile->name);
+ g_free(profile->path);
+ g_free(profile->ident);
+ g_free(profile);
}
-void __connman_profile_list(DBusMessageIter *iter)
+static int profile_init(void)
{
- const char *path = __connman_profile_active();
-
DBG("");
- dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
-}
+ default_profile = g_try_new0(struct connman_profile, 1);
+ if (default_profile == NULL)
+ return -ENOMEM;
-static void append_path(gpointer key, gpointer value, gpointer user_data)
-{
- struct connman_group *group = value;
- DBusMessageIter *iter = user_data;
+ default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
+ default_profile->path = g_strdup_printf("/profile/%s",
+ PROFILE_DEFAULT_IDENT);
- dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
- &group->path);
-}
+ if (default_profile->ident == NULL || default_profile->path == NULL) {
+ free_profile(default_profile);
+ return -ENOMEM;
+ }
-void __connman_profile_list_services(DBusMessageIter *iter)
-{
- DBG("");
+ default_profile->name = g_strdup("Default");
+
+ __connman_storage_load_profile(default_profile);
+
+ connman_info("Adding default profile");
- g_hash_table_foreach(groups, append_path, iter);
+ DBG("profile %p path %s", default_profile, default_profile->path);
+
+ 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;
+ GKeyFile *keyfile;
- DBG("conn %p", conn);
+ DBG("profile %p", profile);
- 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);
+ 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);
}