From a76452265e0d73fed9d2ad591863b231df30292d Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 2 Jul 2008 20:53:11 +0200 Subject: [PATCH] Add skeleton for new storage support using SQLite3 --- configure.in | 5 +++ src/Makefile.am | 8 ++-- src/connman.h | 6 +++ src/element.c | 4 ++ src/main.c | 4 ++ src/storage.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 src/storage.c diff --git a/configure.in b/configure.in index 042f85b..8abdd13 100644 --- a/configure.in +++ b/configure.in @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 1ab7d7a..fb5d90c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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)"\" diff --git a/src/connman.h b/src/connman.h index 05bfe36..5f64286 100644 --- a/src/connman.h +++ b/src/connman.h @@ -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 int __connman_iface_init(DBusConnection *conn, const char *interface); diff --git a/src/element.c b/src/element.c index c21b505..58ba06b 100644 --- a/src/element.c +++ b/src/element.c @@ -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, diff --git a/src/main.c b/src/main.c index 10f9fe6..9e237cc 100644 --- a/src/main.c +++ b/src/main.c @@ -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 index 0000000..e093dea --- /dev/null +++ b/src/storage.c @@ -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 +#endif + +#include + +#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; +} -- 2.7.4