Add skeleton for new storage support using SQLite3
authorMarcel Holtmann <marcel@holtmann.org>
Wed, 2 Jul 2008 18:53:11 +0000 (20:53 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Wed, 2 Jul 2008 18:53:11 +0000 (20:53 +0200)
configure.in
src/Makefile.am
src/connman.h
src/element.c
src/main.c
src/storage.c [new file with mode: 0644]

index 042f85b..8abdd13 100644 (file)
@@ -74,6 +74,11 @@ PKG_CHECK_MODULES(HAL, hal >= 0.5.8, dummy=yes,
 AC_SUBST(HAL_CFLAGS)
 AC_SUBST(HAL_LIBS)
 
+PKG_CHECK_MODULES(SQLITE, sqlite3, dummy=yes,
+                               AC_MSG_ERROR(sqlite3 is required))
+AC_SUBST(SQLITE_CFLAGS)
+AC_SUBST(SQLITE_LIBS)
+
 AC_OUTPUT(Makefile include/Makefile src/Makefile doc/Makefile
                        test/Makefile plugins/Makefile scripts/Makefile
                        scripts/fi.epitest.hostap.WPASupplicant.service
index 1ab7d7a..fb5d90c 100644 (file)
@@ -12,11 +12,12 @@ DISTCLEANFILES = $(service_DATA)
 sbin_PROGRAMS = connmand
 
 connmand_SOURCES = main.c connman.h log.c plugin.c element.c \
-                                               manager.c agent.c \
+                                       storage.c manager.c agent.c \
                        iface.c iface-storage.c iface-helper.c iface-inet.c \
                                        network.c rtnl.c dhcp.c resolver.c
 
-connmand_LDADD = @HAL_LIBS@ @GDBUS_LIBS@ @GMODULE_LIBS@ @GTHREAD_LIBS@
+connmand_LDADD = @HAL_LIBS@ @SQLITE_LIBS@ \
+                       @GDBUS_LIBS@ @GMODULE_LIBS@ @GTHREAD_LIBS@
  
 statedir = $(localstatedir)/run/connman
 
@@ -28,7 +29,8 @@ else
 plugindir = $(libdir)/connman/plugins
 endif
 
-AM_CFLAGS = @GTHREAD_CFLAGS@ @GMODULE_CFLAGS@ @GDBUS_CFLAGS@ @HAL_CFLAGS@ \
+AM_CFLAGS = @GTHREAD_CFLAGS@ @GMODULE_CFLAGS@ @GDBUS_CFLAGS@ \
+                               @SQLITE_CFLAGS@ @HAL_CFLAGS@ \
                        -DSTATEDIR=\""$(statedir)"\" \
                        -DSTORAGEDIR=\""$(storagedir)\"" \
                        -DPLUGINDIR=\""$(plugindir)"\"
index 05bfe36..5f64286 100644 (file)
@@ -27,6 +27,9 @@
 #define NM_PATH       "/org/freedesktop/NetworkManager"
 #define NM_INTERFACE  NM_SERVICE
 
+int __connman_storage_init(void);
+void __connman_storage_cleanup(void);
+
 int __connman_manager_init(DBusConnection *conn, gboolean compat);
 void __connman_manager_cleanup(void);
 
@@ -58,6 +61,9 @@ void __connman_element_list(enum connman_element_type type,
 const char *__connman_element_type2string(enum connman_element_type type);
 const char *__connman_element_subtype2string(enum connman_element_subtype type);
 
+int __connman_element_load(struct connman_element *element);
+int __connman_element_store(struct connman_element *element);
+
 #include <connman/iface.h>
 
 int __connman_iface_init(DBusConnection *conn, const char *interface);
index c21b505..58ba06b 100644 (file)
@@ -457,6 +457,8 @@ int connman_element_register(struct connman_element *element,
        if (connman_element_ref(element) == NULL)
                return -1;
 
+       __connman_element_load(element);
+
        g_static_rw_lock_writer_lock(&element_lock);
 
        if (parent) {
@@ -502,6 +504,8 @@ int connman_element_register(struct connman_element *element,
 
        g_static_rw_lock_writer_unlock(&element_lock);
 
+       __connman_element_store(element);
+
        g_dbus_register_interface(connection, element->path,
                                        CONNMAN_ELEMENT_INTERFACE,
                                        element_methods, NULL, NULL,
index 10f9fe6..9e237cc 100644 (file)
@@ -127,6 +127,8 @@ int main(int argc, char *argv[])
 
        __connman_log_init(option_detach, option_debug);
 
+       __connman_storage_init();
+
        __connman_element_init(conn);
 
        __connman_agent_init(conn);
@@ -152,6 +154,8 @@ int main(int argc, char *argv[])
 
        __connman_element_cleanup();
 
+       __connman_storage_cleanup();
+
        __connman_log_cleanup();
 
        g_dbus_cleanup_connection(conn);
diff --git a/src/storage.c b/src/storage.c
new file mode 100644 (file)
index 0000000..e093dea
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2008  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 <sqlite3.h>
+
+#include "connman.h"
+
+static sqlite3 *db = NULL;
+
+static int create_tables(void)
+{
+       char *msg;
+       int err;
+
+       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;
+       }
+
+       return 0;
+}
+
+int __connman_storage_init(void)
+{
+       int err;
+
+       DBG("");
+
+#if 0
+       if (!sqlite3_threadsafe()) {
+               connman_error("SQLite is missing thread support");
+               return -1;
+       }
+#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;
+       }
+
+       create_tables();
+
+       return 0;
+}
+
+void __connman_storage_cleanup(void)
+{
+       DBG("");
+
+       sqlite3_close(db);
+}
+
+int __connman_element_load(struct connman_element *element)
+{
+       return 0;
+}
+
+int __connman_element_store(struct connman_element *element)
+{
+       char *sql, *msg;
+
+       DBG("");
+
+       if (element->priority > 0) {
+               sql = g_strdup_printf("INSERT INTO properties "
+                                               "VALUES ('%s','%s','%d')",
+                                               element->path, "Priority",
+                                                       element->priority);
+
+               if (sqlite3_exec(db, sql, NULL, NULL, &msg) != SQLITE_OK) {
+                       connman_error("SQL error: %s", msg);
+                       sqlite3_free(msg);
+               }
+       }
+
+       return 0;
+}