diff mbox series

[6/9] Factorize reading of identities from config file

Message ID 20180906114041.21828-6-sbabic@denx.de
State Accepted
Headers show
Series [1/9] Use provided macro for exit() | expand

Commit Message

Stefano Babic Sept. 6, 2018, 11:40 a.m. UTC
identities are read up now by the Hawkbit connector. Factorize
it and put in common code to make it available to other servers.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/swupdate_settings.c | 36 ++++++++++++++++++++++++++++++++++++
 include/swupdate_settings.h |  4 ++++
 suricatta/server_hawkbit.c  | 33 ++-------------------------------
 3 files changed, 42 insertions(+), 31 deletions(-)
diff mbox series

Patch

diff --git a/corelib/swupdate_settings.c b/corelib/swupdate_settings.c
index ec726f2..f18a0e8 100644
--- a/corelib/swupdate_settings.c
+++ b/corelib/swupdate_settings.c
@@ -25,6 +25,7 @@ 
 #include "swupdate.h"
 #include "parselib.h"
 #include "swupdate_settings.h"
+#include "swupdate_dict.h"
 #include "compat.h"
 
 struct run_as {
@@ -125,3 +126,38 @@  int read_settings_user_id(const char *filename, const char *module, uid_t *useri
 
 	return 0;
 }
+
+
+/*
+ * Callback to be used to put a section of configuration
+ * into a dictionary
+ */
+int settings_into_dict(void *settings, void *data)
+{
+	void *elem;
+	int count, i;
+	char name[80], value[80];
+	struct dict *dictionary = (struct dict *) data;
+
+	count = get_array_length(LIBCFG_PARSER, settings);
+
+	for(i = 0; i < count; ++i) {
+		elem = get_elem_from_idx(LIBCFG_PARSER, settings, i);
+
+		if (!elem)
+			continue;
+
+		if(!(exist_field_string(LIBCFG_PARSER, elem, "name")))
+			continue;
+		if(!(exist_field_string(LIBCFG_PARSER, elem, "value")))
+			continue;
+
+		GET_FIELD_STRING(LIBCFG_PARSER, elem, "name", name);
+		GET_FIELD_STRING(LIBCFG_PARSER, elem, "value", value);
+		dict_set_value(dictionary, name, value);
+		TRACE("Identify for configData: %s --> %s\n",
+				name, value);
+	}
+
+	return 0;
+}
diff --git a/include/swupdate_settings.h b/include/swupdate_settings.h
index 1c06095..7101cc5 100644
--- a/include/swupdate_settings.h
+++ b/include/swupdate_settings.h
@@ -11,6 +11,7 @@ 
 #ifdef CONFIG_LIBCONFIG
 int read_module_settings(const char *filename, const char *module, settings_callback fcn, void *data);
 int read_settings_user_id(const char *filename, const char *module, uid_t *userid, gid_t *groupid);
+int settings_into_dict(void *settings, void *data);
 #else
 #include <unistd.h>
 static inline int read_module_settings(const char __attribute__ ((__unused__))*filename,
@@ -33,6 +34,9 @@  static inline int read_settings_user_id(const char __attribute__ ((__unused__))*
 
 	return 0;
 }
+
+static inline int settings_into_dict(void __attribute__ ((__unused__)) *settings,
+					void __attribute__ ((__unused__))*data);
 #endif
 
 #endif
diff --git a/suricatta/server_hawkbit.c b/suricatta/server_hawkbit.c
index ebcf7f1..31266c0 100644
--- a/suricatta/server_hawkbit.c
+++ b/suricatta/server_hawkbit.c
@@ -1556,35 +1556,6 @@  static int suricatta_settings(void *elem, void  __attribute__ ((__unused__)) *da
 
 }
 
-static int suricatta_configdata_settings(void *settings, void  __attribute__ ((__unused__)) *data)
-{
-	void *elem;
-	int count, i;
-	char name[80], value[80];
-
-	count = get_array_length(LIBCFG_PARSER, settings);
-
-	for(i = 0; i < count; ++i) {
-		elem = get_elem_from_idx(LIBCFG_PARSER, settings, i);
-
-		if (!elem)
-			continue;
-
-		if(!(exist_field_string(LIBCFG_PARSER, elem, "name")))
-			continue;
-		if(!(exist_field_string(LIBCFG_PARSER, elem, "value")))
-			continue;
-
-		GET_FIELD_STRING(LIBCFG_PARSER, elem, "name", name);
-		GET_FIELD_STRING(LIBCFG_PARSER, elem, "value", value);
-		dict_set_value(&server_hawkbit.configdata, name, value);
-		TRACE("Identify for configData: %s --> %s\n",
-				name, value);
-	}
-
-	return 0;
-}
-
 server_op_res_t server_start(char *fname, int argc, char *argv[])
 {
 	update_state_t update_state = STATE_NOT_AVAILABLE;
@@ -1597,8 +1568,8 @@  server_op_res_t server_start(char *fname, int argc, char *argv[])
 	if (fname) {
 		read_module_settings(fname, "suricatta", suricatta_settings,
 					NULL);
-		read_module_settings(fname, "identify", suricatta_configdata_settings,
-					NULL);
+		read_module_settings(fname, "identify", settings_into_dict,
+					&server_hawkbit.configdata);
 	}
 
 	if (loglevel >= DEBUGLEVEL) {