From 6c588589ac96d27f4f3933996fcbe3ba0d3150f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Leroy?= <frederic.leroy@b-com.com>
Date: Wed, 24 Aug 2016 10:38:12 +0200
Subject: [PATCH] refactor(conf2uedata): move parser functions to
 conf_parser.[c|h]

---
 cmake_targets/nas_sim_tools/CMakeLists.txt |   1 +
 openair3/NAS/TOOLS/conf2uedata.c           | 109 +-------------------
 openair3/NAS/TOOLS/conf2uedata.h           |  10 --
 openair3/NAS/TOOLS/conf_emm.h              |   2 +-
 openair3/NAS/TOOLS/conf_parser.c           | 110 +++++++++++++++++++++
 openair3/NAS/TOOLS/conf_parser.h           |  12 +++
 openair3/NAS/TOOLS/conf_usim.c             |   1 -
 7 files changed, 125 insertions(+), 120 deletions(-)
 create mode 100644 openair3/NAS/TOOLS/conf_parser.c
 create mode 100644 openair3/NAS/TOOLS/conf_parser.h

diff --git a/cmake_targets/nas_sim_tools/CMakeLists.txt b/cmake_targets/nas_sim_tools/CMakeLists.txt
index 966c928a2b..025c35c441 100644
--- a/cmake_targets/nas_sim_tools/CMakeLists.txt
+++ b/cmake_targets/nas_sim_tools/CMakeLists.txt
@@ -22,6 +22,7 @@ set(conf2uedata_SRC
     ${OPENAIR_DIR}/openair3/NAS/TOOLS/conf_usim.c
     ${OPENAIR_DIR}/openair3/NAS/TOOLS/conf_network.c
     ${OPENAIR_DIR}/openair3/NAS/TOOLS/conf_user_plmn.c
+    ${OPENAIR_DIR}/openair3/NAS/TOOLS/conf_parser.c
     ${OPENAIR_DIR}/openair3/NAS/TOOLS/fs.c
     ${OPENAIR_DIR}/openair3/NAS/TOOLS/display.c
     ${OPENAIR_DIR}/openair3/NAS/UE/API/USIM/usim_api.c
diff --git a/openair3/NAS/TOOLS/conf2uedata.c b/openair3/NAS/TOOLS/conf2uedata.c
index 338d1b11ef..b162979dc2 100644
--- a/openair3/NAS/TOOLS/conf2uedata.c
+++ b/openair3/NAS/TOOLS/conf2uedata.c
@@ -4,13 +4,8 @@
 #include <getopt.h>
 
 #include "conf2uedata.h"
-#include "memory.h"
-#include "utils.h"
 #include "display.h"
-#include "fs.h"
-#include "conf_emm.h"
-#include "conf_user_data.h"
-#include "conf_usim.h"
+#include "conf_parser.h"
 
 int main(int argc, char**argv) {
 	int option;
@@ -56,108 +51,6 @@ int main(int argc, char**argv) {
 	exit(0);
 }
 
-bool parse_config_file(const char *output_dir, const char *conf_filename) {
-	int rc = true;
-    int ret;
-    int ue_nb = 0;
-    config_setting_t *root_setting = NULL;
-    config_setting_t *ue_setting = NULL;
-    config_setting_t *all_plmn_setting = NULL;
-    char user[10];
-    config_t cfg;
-
-	networks_t networks;;
-
-    ret = get_config_from_file(conf_filename, &cfg);
-    if (ret == false) {
-        exit(1);
-    }
-
-    root_setting = config_root_setting(&cfg);
-    ue_nb = config_setting_length(root_setting) - 1;
-
-    all_plmn_setting = config_setting_get_member(root_setting, PLMN);
-    if (all_plmn_setting == NULL) {
-        printf("NO PLMN SECTION...EXITING...\n");
-        return (false);
-    }
-
-    if ( parse_plmns(all_plmn_setting, &networks) == false ) {
-        return false;
-    }
-
-    for (int i = 0; i < ue_nb; i++) {
-	    emm_nvdata_t emm_data;
-
-	    user_nvdata_t user_data;
-	    user_data_conf_t user_data_conf;
-
-	    usim_data_t usim_data;
-	    usim_data_conf_t usim_data_conf;
-
-		user_plmns_t user_plmns;
-
-        sprintf(user, "%s%d", UE, i);
-
-        ue_setting = config_setting_get_member(root_setting, user);
-        if (ue_setting == NULL) {
-            printf("Check UE%d settings\n", i);
-            return false;
-        }
-
-        if ( parse_user_plmns_conf(ue_setting, i, &user_plmns, &usim_data_conf.hplmn, networks) == false ) {
-            return false;
-        }
-
-        rc = parse_ue_user_data(ue_setting, i, &user_data_conf);
-        if (rc != true) {
-            printf("Problem in USER section for UE%d. EXITING...\n", i);
-            return false;
-        }
-        gen_user_data(&user_data_conf, &user_data);
-        write_user_data(output_dir, i, &user_data);
-
-        rc = parse_ue_sim_param(ue_setting, i, &usim_data_conf);
-        if (rc != true) {
-            printf("Problem in SIM section for UE%d. EXITING...\n", i);
-            return false;
-        }
-        gen_usim_data(&usim_data_conf, &usim_data, &user_plmns, networks);
-        write_usim_data(output_dir, i, &usim_data);
-
-        gen_emm_data(&emm_data, usim_data_conf.hplmn, usim_data_conf.msin,
-                     user_plmns.equivalents_home.size, networks);
-        write_emm_data(output_dir, i, &emm_data);
-
-		user_plmns_free(&user_plmns);
-
-     }
-    free(networks.items);
-	networks.size=0;
-    config_destroy(&cfg);
-	return(true);
-}
-
-bool get_config_from_file(const char *filename, config_t *config) {
-    config_init(config);
-    if (filename == NULL) {
-        // XXX write error message ?
-        return(false);
-    }
-
-    /* Read the file. If there is an error, report it and exit. */
-    if (!config_read_file(config, filename)) {
-        fprintf(stderr, "Cant read config file '%s': %s\n", filename,
-                config_error_text(config));
-        if ( config_error_type(config) == CONFIG_ERR_PARSE ) {
-            fprintf(stderr, "This is line %d\n", config_error_line(config));
-        }
-        config_destroy(config);
-        return (false);
-    }
-    return true;
-}
-
 /*
  * Displays command line usage
  */
diff --git a/openair3/NAS/TOOLS/conf2uedata.h b/openair3/NAS/TOOLS/conf2uedata.h
index b97116eaa9..ed56a22718 100644
--- a/openair3/NAS/TOOLS/conf2uedata.h
+++ b/openair3/NAS/TOOLS/conf2uedata.h
@@ -1,16 +1,6 @@
 #ifndef _CONF2UEDATA_H
 #define _CONF2UEDATA_H
 
-#include <libconfig.h>
-
-#include "usim_api.h"
-#include "conf_network.h"
-
-#define UE "UE"
-
-bool get_config_from_file(const char *filename, config_t *config);
-bool parse_config_file(const char *output_dir, const char *filename);
-
 void _display_usage(void);
 
 #endif // _CONF2UEDATA_H
diff --git a/openair3/NAS/TOOLS/conf_emm.h b/openair3/NAS/TOOLS/conf_emm.h
index 32506acdb5..a7b97c5da2 100644
--- a/openair3/NAS/TOOLS/conf_emm.h
+++ b/openair3/NAS/TOOLS/conf_emm.h
@@ -1,8 +1,8 @@
 #ifndef _CONF_EMM_H
 #define _CONF_EMM_H
 
-#include "conf2uedata.h"
 #include "emmData.h"
+#include "conf_network.h"
 
 void gen_emm_data(emm_nvdata_t *emm_data, const char *hplmn, const char *msin, int ehplmn_count, const networks_t networks);
 bool write_emm_data(const char *directory, int user_id, emm_nvdata_t *emm_data);
diff --git a/openair3/NAS/TOOLS/conf_parser.c b/openair3/NAS/TOOLS/conf_parser.c
new file mode 100644
index 0000000000..816c5c6ec9
--- /dev/null
+++ b/openair3/NAS/TOOLS/conf_parser.c
@@ -0,0 +1,110 @@
+#include "conf_parser.h"
+
+#include "conf_network.h"
+#include "conf_emm.h"
+#include "conf_usim.h"
+#include "conf_user_data.h"
+#include "conf_user_plmn.h"
+
+bool parse_config_file(const char *output_dir, const char *conf_filename) {
+	int rc = true;
+    int ret;
+    int ue_nb = 0;
+    config_setting_t *root_setting = NULL;
+    config_setting_t *ue_setting = NULL;
+    config_setting_t *all_plmn_setting = NULL;
+    char user[10];
+    config_t cfg;
+
+	networks_t networks;;
+
+    ret = get_config_from_file(conf_filename, &cfg);
+    if (ret == false) {
+        exit(1);
+    }
+
+    root_setting = config_root_setting(&cfg);
+    ue_nb = config_setting_length(root_setting) - 1;
+
+    all_plmn_setting = config_setting_get_member(root_setting, PLMN);
+    if (all_plmn_setting == NULL) {
+        printf("NO PLMN SECTION...EXITING...\n");
+        return (false);
+    }
+
+    if ( parse_plmns(all_plmn_setting, &networks) == false ) {
+        return false;
+    }
+
+    for (int i = 0; i < ue_nb; i++) {
+	    emm_nvdata_t emm_data;
+
+	    user_nvdata_t user_data;
+	    user_data_conf_t user_data_conf;
+
+	    usim_data_t usim_data;
+	    usim_data_conf_t usim_data_conf;
+
+		user_plmns_t user_plmns;
+
+        sprintf(user, "%s%d", UE, i);
+
+        ue_setting = config_setting_get_member(root_setting, user);
+        if (ue_setting == NULL) {
+            printf("Check UE%d settings\n", i);
+            return false;
+        }
+
+        if ( parse_user_plmns_conf(ue_setting, i, &user_plmns, &usim_data_conf.hplmn, networks) == false ) {
+            return false;
+        }
+
+        rc = parse_ue_user_data(ue_setting, i, &user_data_conf);
+        if (rc != true) {
+            printf("Problem in USER section for UE%d. EXITING...\n", i);
+            return false;
+        }
+        gen_user_data(&user_data_conf, &user_data);
+        write_user_data(output_dir, i, &user_data);
+
+        rc = parse_ue_sim_param(ue_setting, i, &usim_data_conf);
+        if (rc != true) {
+            printf("Problem in SIM section for UE%d. EXITING...\n", i);
+            return false;
+        }
+        gen_usim_data(&usim_data_conf, &usim_data, &user_plmns, networks);
+        write_usim_data(output_dir, i, &usim_data);
+
+        gen_emm_data(&emm_data, usim_data_conf.hplmn, usim_data_conf.msin,
+                     user_plmns.equivalents_home.size, networks);
+        write_emm_data(output_dir, i, &emm_data);
+
+		user_plmns_free(&user_plmns);
+
+     }
+    free(networks.items);
+	networks.size=0;
+    config_destroy(&cfg);
+	return(true);
+}
+
+bool get_config_from_file(const char *filename, config_t *config) {
+    config_init(config);
+    if (filename == NULL) {
+        // XXX write error message ?
+        return(false);
+    }
+
+    /* Read the file. If there is an error, report it and exit. */
+    if (!config_read_file(config, filename)) {
+        fprintf(stderr, "Cant read config file '%s': %s\n", filename,
+                config_error_text(config));
+        if ( config_error_type(config) == CONFIG_ERR_PARSE ) {
+            fprintf(stderr, "This is line %d\n", config_error_line(config));
+        }
+        config_destroy(config);
+        return (false);
+    }
+    return true;
+}
+
diff --git a/openair3/NAS/TOOLS/conf_parser.h b/openair3/NAS/TOOLS/conf_parser.h
new file mode 100644
index 0000000000..4e160ab73a
--- /dev/null
+++ b/openair3/NAS/TOOLS/conf_parser.h
@@ -0,0 +1,12 @@
+#ifndef _CONF_PARSER_H
+#define _CONF_PARSER_H
+
+#include <stdbool.h>
+#include <libconfig.h>
+
+#define UE "UE"
+
+bool get_config_from_file(const char *filename, config_t *config);
+bool parse_config_file(const char *output_dir, const char *filename);
+
+#endif
diff --git a/openair3/NAS/TOOLS/conf_usim.c b/openair3/NAS/TOOLS/conf_usim.c
index 5418f01932..5004363275 100644
--- a/openair3/NAS/TOOLS/conf_usim.c
+++ b/openair3/NAS/TOOLS/conf_usim.c
@@ -5,7 +5,6 @@
 #include "utils.h"
 #include "conf_emm.h"
 #include "fs.h"
-#include "conf2uedata.h"
 #include "conf_usim.h"
 
 bool parse_ue_sim_param(config_setting_t *ue_setting, int user_id, usim_data_conf_t *u) {
-- 
GitLab