Add framework for configuration files
authorMarcel Holtmann <marcel@holtmann.org>
Mon, 14 Dec 2009 08:57:58 +0000 (09:57 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Mon, 14 Dec 2009 08:57:58 +0000 (09:57 +0100)
Makefile.am
src/config.c [new file with mode: 0644]
src/connman.h
src/main.c
src/storage.c

index 8ce2a6a..661fe1e 100644 (file)
@@ -51,7 +51,7 @@ src_connmand_SOURCES = $(gdbus_sources) $(builtin_sources) \
                        src/security.c src/resolver.c src/ipconfig.c \
                        src/ipv4.c src/dhcp.c src/rtnl.c src/inet.c \
                        src/utsname.c src/timeserver.c src/rfkill.c \
-                       src/wifi.c src/storage.c src/dbus.c
+                       src/wifi.c src/storage.c src/dbus.c src/config.c
 
 if UDEV
 src_connmand_SOURCES += src/udev.c
diff --git a/src/config.c b/src/config.c
new file mode 100644 (file)
index 0000000..f22d517
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2009  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
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib.h>
+
+#include "connman.h"
+
+struct connman_config {
+       char *ident;
+       char *name;
+       char *description;
+};
+
+static GHashTable *config_hash = NULL;
+
+static int load_config(struct connman_config *config)
+{
+       GKeyFile *keyfile;
+       char *str;
+
+       DBG("config %p", config);
+
+       keyfile = __connman_storage_open_config(config->ident);
+       if (keyfile == NULL)
+               return -EIO;
+
+       str = g_key_file_get_string(keyfile, "global", "Name", NULL);
+       if (str != NULL) {
+               g_free(config->name);
+               config->name = str;
+       }
+
+       str = g_key_file_get_string(keyfile, "global", "Description", NULL);
+       if (str != NULL) {
+               g_free(config->description);
+               config->description = str;
+       }
+
+       __connman_storage_close_config(config->ident, keyfile, FALSE);
+
+       return 0;
+}
+
+static void free_config(struct connman_config *config)
+{
+       g_free(config->description);
+       g_free(config->name);
+       g_free(config->ident);
+       g_free(config);
+}
+
+static void unregister_config(gpointer data)
+{
+       struct connman_config *config = data;
+
+       connman_info("Removing configuration %s", config->ident);
+
+       free_config(config);
+}
+
+static int create_config(const char *ident)
+{
+       struct connman_config *config;
+
+       DBG("ident %s", ident);
+
+       config = g_try_new0(struct connman_config, 1);
+       if (config == NULL)
+               return -ENOMEM;
+
+       config->ident = g_strdup(ident);
+
+       if (config->ident == NULL) {
+               free_config(config);
+               return -ENOMEM;
+       }
+
+       if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
+               free_config(config);
+               return -EEXIST;
+       }
+
+       g_hash_table_insert(config_hash, g_strdup(config->ident), config);
+
+       connman_info("Adding configuration %s", config->ident);
+
+       load_config(config);
+
+       return 0;
+}
+
+static int config_init(void)
+{
+       GDir *dir;
+       const gchar *file;
+
+       DBG("");
+
+       dir = g_dir_open(STORAGEDIR, 0, NULL);
+       if (dir != NULL) {
+               while ((file = g_dir_read_name(dir)) != NULL) {
+                       GString *str;
+                       gchar *ident;
+
+                       if (g_str_has_suffix(file, ".config") == FALSE)
+                               continue;
+
+                       ident = g_strrstr(file, ".config");
+                       if (ident == NULL)
+                               continue;
+
+                       str = g_string_new_len(file, ident - file);
+                       if (str == NULL)
+                               continue;
+
+                       ident = g_string_free(str, FALSE);
+
+                       if (connman_dbus_validate_ident(ident) == TRUE)
+                               create_config(ident);
+
+                       g_free(ident);
+               }
+
+               g_dir_close(dir);
+       }
+
+       return 0;
+}
+
+int __connman_config_init(void)
+{
+       DBG("");
+
+       config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                               g_free, unregister_config);
+
+       return config_init();
+}
+
+void __connman_config_cleanup(void)
+{
+       DBG("");
+
+       g_hash_table_destroy(config_hash);
+       config_hash = NULL;
+}
index 451b02e..bd61d25 100644 (file)
@@ -170,6 +170,11 @@ void __connman_storage_close_profile(const char *ident,
                                        GKeyFile *keyfile, gboolean save);
 void __connman_storage_delete_profile(const char *ident);
 
+GKeyFile *__connman_storage_open_config(const char *ident);
+void __connman_storage_close_config(const char *ident,
+                                       GKeyFile *keyfile, gboolean save);
+void __connman_storage_delete_config(const char *ident);
+
 int __connman_storage_init_profile(void);
 int __connman_storage_load_profile(struct connman_profile *profile);
 int __connman_storage_save_profile(struct connman_profile *profile);
@@ -316,6 +321,9 @@ const char *__connman_network_get_ident(struct connman_network *network);
 connman_bool_t __connman_network_get_weakness(struct connman_network *network);
 connman_bool_t __connman_network_get_connecting(struct connman_network *network);
 
+int __connman_config_init();
+void __connman_config_cleanup(void);
+
 #include <connman/profile.h>
 
 int __connman_profile_init();
index 327eb24..a66a65d 100644 (file)
@@ -209,6 +209,7 @@ int main(int argc, char *argv[])
        __connman_agent_init();
        __connman_manager_init(option_compat);
        __connman_profile_init();
+       __connman_config_init();
 
        __connman_resolver_init();
        __connman_ipconfig_init();
@@ -242,6 +243,7 @@ int main(int argc, char *argv[])
        __connman_ipconfig_cleanup();
        __connman_resolver_cleanup();
 
+       __connman_config_cleanup();
        __connman_profile_cleanup();
        __connman_manager_cleanup();
        __connman_agent_cleanup();
index e7959aa..89c6c14 100644 (file)
@@ -28,6 +28,7 @@
 #include "connman.h"
 
 #define PROFILE_SUFFIX "profile"
+#define CONFIG_SUFFIX  "config"
 
 static GSList *storage_list = NULL;
 
@@ -163,6 +164,22 @@ void __connman_storage_delete_profile(const char *ident)
        __connman_storage_delete(ident, PROFILE_SUFFIX);
 }
 
+GKeyFile *__connman_storage_open_config(const char *ident)
+{
+       return __connman_storage_open(ident, CONFIG_SUFFIX);
+}
+
+void __connman_storage_close_config(const char *ident,
+                                       GKeyFile *keyfile, gboolean save)
+{
+       __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
+}
+
+void __connman_storage_delete_config(const char *ident)
+{
+       __connman_storage_delete(ident, CONFIG_SUFFIX);
+}
+
 int __connman_storage_init_profile(void)
 {
        GSList *list;