From 16eb45092772d73d83e1c1562b67db1293e62392 Mon Sep 17 00:00:00 2001
From: oai <francois.taburet@nokia-bell-labs.com>
Date: Fri, 29 Sep 2017 16:15:04 +0200
Subject: [PATCH] Add generic shared lib loader

---
 cmake_targets/CMakeLists.txt          |  9 ++++
 cmake_targets/build_oai               | 23 ++++++++-
 common/config/config_cmdline.c        | 70 ++++++++++++---------------
 common/utils/load_module_shlib.c      | 52 ++++++++++++++++++++
 common/utils/load_module_shlib.h      | 11 +++++
 common/utils/telnetsrv/CMakeLists.txt |  3 +-
 common/utils/telnetsrv/load_telnet.c  | 45 -----------------
 common/utils/telnetsrv/load_telnet.h  |  5 --
 common/utils/telnetsrv/telnetsrv.h    |  4 +-
 targets/RT/USER/lte-softmodem.c       |  7 ++-
 targets/RT/USER/lte-softmodem.h       |  6 ++-
 11 files changed, 138 insertions(+), 97 deletions(-)
 create mode 100644 common/utils/load_module_shlib.c
 create mode 100644 common/utils/load_module_shlib.h
 delete mode 100644 common/utils/telnetsrv/load_telnet.c
 delete mode 100644 common/utils/telnetsrv/load_telnet.h

diff --git a/cmake_targets/CMakeLists.txt b/cmake_targets/CMakeLists.txt
index e3e3e79913..3cdd2b9ccd 100644
--- a/cmake_targets/CMakeLists.txt
+++ b/cmake_targets/CMakeLists.txt
@@ -502,6 +502,10 @@ set (CONFIG_LIBCONFIG_SOURCES
    )   
 add_library(params_libconfig MODULE ${CONFIG_LIBCONFIG_SOURCES} )
 target_link_libraries(params_libconfig config)  
+# shared library loader
+set (SHLIB_LOADER_SOURCES
+    ${OPENAIR_DIR}/common/utils/load_module_shlib.c
+)
 # include RF devices / transport protocols library modules
 ######################################################################
 
@@ -1767,6 +1771,7 @@ add_executable(lte-softmodem
   ${XFORMS_SOURCE_SOFTMODEM}
   ${T_SOURCE}
   ${CONFIG_SOURCES}
+  ${SHLIB_LOADER_SOURCES}
   )
 
 target_link_libraries (lte-softmodem -ldl 
@@ -1802,6 +1807,7 @@ add_executable(lte-softmodem-nos1
   ${XFORMS_SOURCE_SOFTMODEM}
   ${T_SOURCE}
   ${CONFIG_SOURCES}
+  ${SHLIB_LOADER_SOURCES}
   )
 target_link_libraries (lte-softmodem-nos1
   -Wl,--start-group
@@ -1890,6 +1896,7 @@ add_executable(oaisim
   ${XFORMS_SOURCE}
   ${T_SOURCE}
   ${CONFIG_SOURCES}
+  ${SHLIB_LOADER_SOURCES}
 )
 
 
@@ -1933,6 +1940,8 @@ add_executable(oaisim_nos1
   ${OPENAIR_DIR}/common/utils/system.c
   ${XFORMS_SOURCE}
   ${T_SOURCE}
+  ${CONFIG_SOURCES}
+  ${SHLIB_LOADER_SOURCES}
 )
 target_include_directories(oaisim_nos1 PUBLIC  ${OPENAIR_TARGETS}/SIMU/USER)
 target_link_libraries (oaisim_nos1
diff --git a/cmake_targets/build_oai b/cmake_targets/build_oai
index 7b612c8ef3..01d94da73e 100755
--- a/cmake_targets/build_oai
+++ b/cmake_targets/build_oai
@@ -345,7 +345,11 @@ function main() {
             BUILD_ECLIPSE=1
             CMAKE_CMD="$CMAKE_CMD"' -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT=TRUE -G"Eclipse CDT4 - Unix Makefiles"'
             echo_info "Enabling build eclipse project support"
-            shift 1;;			
+            shift 1;;
+       --build-telnetsrv)
+            BUILD_TELNETSRV=1
+            echo_info "Build embedded telnet server"
+            shift ;;			
         -h | --help)
             print_help
             exit 1;;
@@ -811,7 +815,22 @@ function main() {
 	 rrh_gw $dbin/rrh_gw
 
   fi
-  
+  # Telnet server compilation
+  #####################
+  if [ "$BUILD_TELNETSRV" = "1" ] ; then
+
+     telnetsrv_build_dir=telnetsrv
+     mkdir -p $DIR/$telnetsrv_build_dir/build
+     cd $DIR/$telnetsrv_build_dir/build   
+     echo_info "Compiling telnet server library ..."
+    
+     [ "$CLEAN" = "1" ] && rm -rf $DIR/$telnetsrv_build_dir 
+     cmake_file=$OPENAIR_DIR/common/utils/$telnetsrv_build_dir/CMakeLists.txt   
+     cd $DIR/$telnetsrv_build_dir/build
+     eval  "$CMAKE_CMD $OPENAIR_DIR/common/utils/$telnetsrv_build_dir/"
+     make
+
+  fi  
   # build RF device and transport protocol libraries
   #####################################
   if [ "$eNB" = "1" -o "$UE" = "1" -o  "$RRH" = "1" ] ; then
diff --git a/common/config/config_cmdline.c b/common/config/config_cmdline.c
index 170f8963cf..98c75c1c14 100644
--- a/common/config/config_cmdline.c
+++ b/common/config/config_cmdline.c
@@ -37,24 +37,16 @@
 
 int processoption(paramdef_t *cfgoptions, char *value)
 {
-int argok=1;
 char *tmpval = value;
-int optisset;
+int optisset=0;
 char defbool[2]="1";
-     if (value == NULL) {
-     	 argok=0; 
-     } else if ( value[0] == '-') {
-     	 argok = 0;
-     }
-     if ((cfgoptions->paramflags &PARAMFLAG_BOOL) == 0) { /* not a boolean, argument required */
-	if (argok == 0) {
+
+     if ( ((cfgoptions->paramflags &PARAMFLAG_BOOL) == 0) && value == NULL ) { /* not a boolean, argument required */
 	    fprintf(stderr,"[CONFIG] command line, option %s requires an argument\n",cfgoptions->optname);
 	    return 0;
-	} 
-     } else {        /* boolean value */
-         tmpval = defbool;
+     } else {        /* boolean value option without argument, set value to true*/
+            tmpval = defbool;
      }
-     
      switch(cfgoptions->type)
        {
        	case TYPE_STRING:
@@ -107,7 +99,7 @@ char defbool[2]="1";
           cfgoptions->paramflags = cfgoptions->paramflags |  PARAMFLAG_PARAMSET;
        }
        
-    return argok;
+    return optisset;
 }
 
 int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
@@ -115,6 +107,7 @@ int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
 char **p = config_get_if()->argv;
 int c = config_get_if()->argc;
 int j;
+char *pp;
 char *cfgpath; 
  
   j = (prefix ==NULL) ? 0 : strlen(prefix); 
@@ -131,32 +124,31 @@ char *cfgpath;
         if (strcmp(*p, "-h") == 0 || strcmp(*p, "--help") == 0 ) {
             config_printhelp(cfgoptions,numoptions);
         }
-        for(int i=0;i<numoptions;i++) {
-            if ( ( cfgoptions[i].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
-              continue;
-             }
-	    if (prefix != NULL) {
-               sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
-	    } else {
-	       sprintf(cfgpath,"%s",cfgoptions[i].optname);
-	    }
-            if ( ((strlen(*p) == 2) && (strcmp(*p + 1,cfgpath) == 0))  || 
-                 ((strlen(*p) > 2) && (strcmp(*p + 2,cfgpath ) == 0 )) ) {
-
-               if (c > 1) {
-                  p++;
-                  c--;
-                  j = processoption(&(cfgoptions[i]), *p);
-                  if ( j== 0) {
-                      c++;
-                      p--;
-                  }
-               } else {
-                  j = processoption(&(cfgoptions[i]), NULL);
-               }
+        if (*p[0] == '-') {
+          
+    	    for(int i=0;i<numoptions;i++) {
+    		if ( ( cfgoptions[i].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
+    		  continue;
+    		 }
+    		if (prefix != NULL) {
+    		   sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
+    		} else {
+    		   sprintf(cfgpath,"%s",cfgoptions[i].optname);
+    		}
+    		if ( ((strlen(*p) == 2) && (strcmp(*p + 1,cfgpath) == 0))  || 
+    		     ((strlen(*p) > 2) && (strcmp(*p + 2,cfgpath ) == 0 )) ) {
+    		   pp = *(p+1);
+    		   if ( ( pp != NULL )   &&  (pp[0]!= '-') ) {
+    		      p++;
+    		      c--;
+    		      j += processoption(&(cfgoptions[i]), pp);
+    		   } else {
+    		      j += processoption(&(cfgoptions[i]), NULL);
+    		   }
 
-            }
-         }   	     
+    		}
+    	     } /* for */
+         } /* if (*p[0] == '-') */  	     
    	 p++;
          c--;  
     }   /* fin du while */
diff --git a/common/utils/load_module_shlib.c b/common/utils/load_module_shlib.c
new file mode 100644
index 0000000000..27e7ba8b09
--- /dev/null
+++ b/common/utils/load_module_shlib.c
@@ -0,0 +1,52 @@
+
+/* FT NOKBLF:
+ *  this source is to be linked with the program using the telnet server, it looks for
+ *  the telnet server dynamic library, possibly loads it and calls the telnet server
+ *  init functions
+*/
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <dlfcn.h>
+#include "openair1/PHY/defs.h"
+#define LOAD_MODULE_SHLIB_MAIN
+#include "load_module_shlib.h"
+int load_module_shlib(char *modname)
+{
+   void *lib_handle;
+   initfunc_t fpi;
+   char *tmpstr;
+   int ret=0;
+ 
+   tmpstr = malloc(strlen(modname)+16);
+   if (tmpstr == NULL) {
+      fprintf(stderr,"[LOADER] %s %d malloc error loading module %s, %s\n",__FILE__, __LINE__, modname, strerror(errno));
+      return -1; 
+   }
+   sprintf(tmpstr,"lib%s.so",modname);
+   lib_handle = dlopen(tmpstr, RTLD_LAZY|RTLD_NODELETE|RTLD_GLOBAL);
+   if (!lib_handle) {
+      printf("[LOADER] library %s is not loaded: %s\n", tmpstr,dlerror());
+      ret = -1;
+   } else {
+      sprintf(tmpstr,"init_%s",modname);
+      fpi = dlsym(lib_handle,tmpstr);
+
+      if (fpi != NULL )
+         {
+	 fpi();
+	 }
+      else
+         {
+         fprintf(stderr,"[LOADER] %s %d %s function not found %s\n",__FILE__, __LINE__, dlerror(),tmpstr);
+         ret =  -1;
+         }
+    } 
+	  	 
+   if (tmpstr != NULL) free(tmpstr);
+   if (lib_handle != NULL) dlclose(lib_handle); 
+   return ret;	       
+}
diff --git a/common/utils/load_module_shlib.h b/common/utils/load_module_shlib.h
new file mode 100644
index 0000000000..2db1d17ec0
--- /dev/null
+++ b/common/utils/load_module_shlib.h
@@ -0,0 +1,11 @@
+#ifndef LOAD_SHLIB_H
+#define LOAD_SHLIB_H
+
+
+typedef int(*initfunc_t)(void);
+#ifdef LOAD_MODULE_SHLIB_MAIN
+#else
+extern int load_module_shlib(char *modname);
+#endif
+
+#endif
diff --git a/common/utils/telnetsrv/CMakeLists.txt b/common/utils/telnetsrv/CMakeLists.txt
index 6e6c21c2db..52fb4887b1 100644
--- a/common/utils/telnetsrv/CMakeLists.txt
+++ b/common/utils/telnetsrv/CMakeLists.txt
@@ -8,8 +8,9 @@ ELSE()
 ENDIF()
 
 
-set(APPROOT . )
+
 set(OPENAIR_DIR $ENV{OPENAIR_DIR})
+set(APPROOT ${OPENAIR_DIR}/common/utils/telnetsrv )
 set(OPENAIR_BUILD_DIR $ENV{OPENAIR_DIR}/cmake_targets)
 set(OPENAIR1_DIR $ENV{OPENAIR1_DIR})
 set(OPENAIR2_DIR $ENV{OPENAIR2_DIR})
diff --git a/common/utils/telnetsrv/load_telnet.c b/common/utils/telnetsrv/load_telnet.c
deleted file mode 100644
index 850c025313..0000000000
--- a/common/utils/telnetsrv/load_telnet.c
+++ /dev/null
@@ -1,45 +0,0 @@
-
-/* FT NOKBLF:
- *  this source is to be linked with the program using the telnet server, it looks for
- *  the telnet server dynamic library, possibly loads it and calls the telnet server
- *  init functions
-*/
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <dlfcn.h>
-#include "telnetsrv.h"
-#include "openair1/PHY/defs.h"
-
-int load_telnet(void)
-{
-   void *lib_handle;
-   initfunc_t fpi;
-
- 
-   lib_handle = dlopen(TELNETSRV_SHAREDLIB, RTLD_LAZY|RTLD_NODELETE|RTLD_GLOBAL);
-   if (!lib_handle) 
-   {
-      printf("[TELNETSRV] telnet server is not loaded: %s\n", dlerror());
-      return -1;
-   } 
-   
-   fpi = dlsym(lib_handle,"init_telnetsrv");
-
-     if (fpi != NULL )
-         {
-	 fpi(cfgfile);
-	 }
-      else
-         {
-         fprintf(stderr,"[TELNETSRV] %s %d Telnet server init function not found %s\n",__FILE__, __LINE__, dlerror());
-         return -1;
-         } 
-	  	 
-
-   dlclose(lib_handle); 
-   return 0;	       
-}
diff --git a/common/utils/telnetsrv/load_telnet.h b/common/utils/telnetsrv/load_telnet.h
deleted file mode 100644
index 73543fe38a..0000000000
--- a/common/utils/telnetsrv/load_telnet.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#ifndef TELNET_LOAD_H
-#define TELNET_LOAD_H
-#include "telnetsrv.h"
-extern int load_telnet(void);
-#endif
diff --git a/common/utils/telnetsrv/telnetsrv.h b/common/utils/telnetsrv/telnetsrv.h
index 5663b44d67..f54557e751 100644
--- a/common/utils/telnetsrv/telnetsrv.h
+++ b/common/utils/telnetsrv/telnetsrv.h
@@ -1,7 +1,7 @@
 #ifndef TELNETSRV_H
 #define TELNETSRV_H
 
-#define TELNETSRV_SHAREDLIB "libtelnetsrv.so"
+#define TELNETSRV_MODNAME  "telnetsrv"
 
 #define TELNET_PORT               9090
 #define TELNET_MAX_MSGLENGTH      2048
@@ -80,7 +80,7 @@ typedef struct {
 
 
 typedef int(*addcmdfunc_t)(char*, telnetshell_vardef_t*, telnetshell_cmddef_t*);
-typedef int(*initfunc_t)(char *cfgfile);
+
 typedef void(*settelnetmodule_t)(char *name, void *ptr); 
 
 /*-------------------------------------------------------------------------------------------*/
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index c076c3d9b7..69765e80c5 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -50,7 +50,7 @@
 #include "PHY/defs.h"
 #include "common/ran_context.h"
 #include "common/config/config_userapi.h"
-#include "common/utils/telnetsrv/load_telnet.h"
+#include "common/utils/load_module_shlib.h"
 #undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
 //#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all
 
@@ -559,6 +559,7 @@ static void get_options(void) {
   int dumpframe;
   uint32_t online_log_messages;
   uint32_t glog_level, glog_verbosity;
+  uint32_t start_telnetsrv;
 
   paramdef_t cmdline_params[] =CMDLINE_PARAMS_DESC ;
   paramdef_t cmdline_logparams[] =CMDLINE_LOGPARAMS_DESC ;
@@ -586,6 +587,10 @@ static void get_options(void) {
   if(config_isparamset(cmdline_logparams,CMDLINE_GLOGLEVEL_IDX)) {
       set_glog(-1, glog_verbosity);
   }
+  if (start_telnetsrv) {
+     load_module_shlib("telnetsrv");
+  }
+
   
   if (UE_flag > 0) {
      paramdef_t cmdline_uemodeparams[] =CMDLINE_UEMODEPARAMS_DESC;
diff --git a/targets/RT/USER/lte-softmodem.h b/targets/RT/USER/lte-softmodem.h
index 6f86d0e62d..9b62b30e36 100644
--- a/targets/RT/USER/lte-softmodem.h
+++ b/targets/RT/USER/lte-softmodem.h
@@ -172,6 +172,7 @@ extern int16_t dlsch_demod_shift;
 #define CONFIG_HLP_FLOG          "Enable online log \n"
 #define CONFIG_HLP_LOGL          "Set the global log level, valide options: (9:trace, 8/7:debug, 6:info, 4:warn, 3:error)\n"
 #define CONFIG_HLP_LOGV          "Set the global log verbosity \n"
+#define CONFIG_HLP_TELN          "Start embedded telnet server \n"
 /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
 /*                                            command line parameters for LOG utility                                                                                        */
 /*   optname                     helpstr                paramflags                      XXXptr                  defXXXval                            type           numelt   */
@@ -179,12 +180,13 @@ extern int16_t dlsch_demod_shift;
 #define CMDLINE_LOGPARAMS_DESC {  \
 {"R" ,  		  	 CONFIG_HLP_FLOG,	0,                uptr:&online_log_messages,		defintval:1,			   TYPE_INT,	  0},			   \
 {"g" ,  		  	 CONFIG_HLP_LOGL,	0,		  uptr:&glog_level,			defintval:0,			   TYPE_UINT,     0},			   \
-{"G" ,  		  	 CONFIG_HLP_LOGV,	0,		  uptr:&glog_verbosity,		        defintval:0,			   TYPE_UINT16,   0},			   \
+{"G" ,                           CONFIG_HLP_LOGV,	0,		  uptr:&glog_verbosity,		        defintval:0,			   TYPE_UINT16,   0},			   \
+{"telnetsrv",    		 CONFIG_HLP_TELN,	PARAMFLAG_BOOL,	  uptr:&start_telnetsrv,		defintval:0,			   TYPE_UINT,     0},			   \
 }
 #define CMDLINE_ONLINELOG_IDX     0 
 #define CMDLINE_GLOGLEVEL_IDX     1
 #define CMDLINE_GLOGVERBO_IDX     2              
-
+#define CMDLINE_STARTTELN_IDX     3
 
 
 extern int T_port;
-- 
GitLab