Iterate over the Bluetooth Devices property
[connman] / src / storage.c
index e093dea..e0e3dc5 100644 (file)
 #include <config.h>
 #endif
 
-#include <sqlite3.h>
-
 #include "connman.h"
 
-static sqlite3 *db = NULL;
-
-static int create_tables(void)
+int __connman_storage_init(void)
 {
-       char *msg;
-       int err;
+       DBG("");
+
+       return 0;
+}
 
+void __connman_storage_cleanup(void)
+{
        DBG("");
+}
 
-       err = sqlite3_exec(db, "CREATE TABLE properties ("
-                                       "element TEXT NOT NULL,"
-                                       "name TEXT NOT NULL,"
-                                       "value TEXT NOT NULL,"
-                                       "PRIMARY KEY(element, name))",
-                                                       NULL, NULL, &msg);
-
-       if (err != SQLITE_OK) {
-               connman_error("SQL error: %s", msg);
-               sqlite3_free(msg);
-               return -1;
-       }
+static int do_load(GKeyFile *keyfile, struct connman_element *element)
+{
+       const gchar *value;
+
+       DBG("element %p name %s", element, element->name);
+
+       value = g_key_file_get_string(keyfile, element->path,
+                                               "Policy", NULL);
+       if (value != NULL)
+               element->policy = __connman_element_string2policy(value);
+
+       if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
+               element->remember = g_key_file_get_boolean(keyfile,
+                                       element->path, "Remember", NULL);
+
+       value = g_key_file_get_string(keyfile, element->path,
+                                               "WiFi.Security", NULL);
+       if (value != NULL)
+               connman_element_set_property(element,
+                               CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
+
+       value = g_key_file_get_string(keyfile, element->path,
+                                               "WiFi.Passphrase", NULL);
+       if (value != NULL)
+               connman_element_set_property(element,
+                               CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
 
        return 0;
 }
 
-int __connman_storage_init(void)
+int __connman_element_load(struct connman_element *element)
 {
-       int err;
+       GKeyFile *keyfile;
+       gchar *pathname, *data = NULL;
+       gsize length;
 
-       DBG("");
+       DBG("element %p name %s", element, element->name);
+
+       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
+       if (pathname == NULL)
+               return -ENOMEM;
 
-#if 0
-       if (!sqlite3_threadsafe()) {
-               connman_error("SQLite is missing thread support");
-               return -1;
+       keyfile = g_key_file_new();
+
+       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
+               g_free(pathname);
+               return -ENOENT;
        }
-#endif
 
-       err = sqlite3_open(STORAGEDIR "/config.db", &db);
-       if (err != SQLITE_OK) {
-               connman_error("Can't open database: %s", sqlite3_errmsg(db));
-               sqlite3_close(db);
-               return -1;
+       g_free(pathname);
+
+       if (g_key_file_load_from_data(keyfile, data, length,
+                                                       0, NULL) == FALSE) {
+               g_free(data);
+               return -EILSEQ;
        }
 
-       create_tables();
+       g_free(data);
+
+       do_load(keyfile, element);
+
+       g_key_file_free(keyfile);
 
        return 0;
 }
 
-void __connman_storage_cleanup(void)
+static void do_update(GKeyFile *keyfile, struct connman_element *element)
 {
-       DBG("");
+       GSList *list;
+       char *value;
+       const char *str;
 
-       sqlite3_close(db);
-}
+       DBG("element %p name %s", element, element->name);
 
-int __connman_element_load(struct connman_element *element)
-{
-       return 0;
+       g_key_file_set_string(keyfile, element->path, "Name", element->name);
+
+       str = __connman_element_policy2string(element->policy);
+       if (str != NULL)
+               g_key_file_set_string(keyfile, element->path, "Policy", str);
+
+       //g_key_file_set_boolean(keyfile, element->path, "Enabled",
+       //                                              element->enabled);
+
+       if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
+               g_key_file_set_boolean(keyfile, element->path, "Remember",
+                                                       element->remember);
+
+       __connman_element_lock(element);
+
+       for (list = element->properties; list; list = list->next) {
+               struct connman_property *property = list->data;
+
+               if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
+                       continue;
+
+               if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
+                       continue;
+
+               if (property->type == DBUS_TYPE_STRING)
+                       g_key_file_set_string(keyfile, element->path,
+                                       property->name, property->value);
+       }
+
+       __connman_element_unlock(element);
+
+       if (connman_element_get_value(element,
+                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
+               g_key_file_set_string(keyfile, element->path,
+                                               "WiFi.Security", value);
+
+       if (connman_element_get_value(element,
+                       CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
+               g_key_file_set_string(keyfile, element->path,
+                                               "WiFi.Passphrase", value);
 }
 
 int __connman_element_store(struct connman_element *element)
 {
-       char *sql, *msg;
+       GKeyFile *keyfile;
+       gchar *pathname, *data = NULL;
+       gsize length;
 
-       DBG("");
+       DBG("element %p name %s", element, element->name);
+
+       if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
+                               element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
+               return -EINVAL;
+
+       if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE ||
+                       element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
+               return -EINVAL;
+
+       pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
+       if (pathname == NULL)
+               return -ENOMEM;
+
+       keyfile = g_key_file_new();
 
-       if (element->priority > 0) {
-               sql = g_strdup_printf("INSERT INTO properties "
-                                               "VALUES ('%s','%s','%d')",
-                                               element->path, "Priority",
-                                                       element->priority);
+       if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
+               goto update;
 
-               if (sqlite3_exec(db, sql, NULL, NULL, &msg) != SQLITE_OK) {
-                       connman_error("SQL error: %s", msg);
-                       sqlite3_free(msg);
-               }
+       if (length > 0) {
+               if (g_key_file_load_from_data(keyfile, data, length,
+                                                       0, NULL) == FALSE)
+                       goto done;
        }
 
+       g_free(data);
+
+update:
+       do_update(keyfile, element);
+
+       data = g_key_file_to_data(keyfile, &length, NULL);
+
+       g_file_set_contents(pathname, data, length, NULL);
+
+done:
+       g_free(data);
+
+       g_key_file_free(keyfile);
+
+       g_free(pathname);
+
        return 0;
 }