From f61f9520dba252eae01b7aab690fcf8fe797c28f Mon Sep 17 00:00:00 2001
From: frtabu <francois.taburet@nokia-bell-labs.com>
Date: Tue, 28 Jul 2020 20:08:48 +0200
Subject: [PATCH] implement queued commands in telnet server

---
 common/utils/telnetsrv/telnetsrv.c         | 21 +++++++-
 common/utils/telnetsrv/telnetsrv.h         | 16 ++++--
 openair1/SIMULATION/TOOLS/random_channel.c | 57 ++++++++++++++--------
 targets/ARCH/rfsimulator/simulator.c       |  6 ++-
 4 files changed, 73 insertions(+), 27 deletions(-)

diff --git a/common/utils/telnetsrv/telnetsrv.c b/common/utils/telnetsrv/telnetsrv.c
index 06a5d350c75..831d6032c8d 100644
--- a/common/utils/telnetsrv/telnetsrv.c
+++ b/common/utils/telnetsrv/telnetsrv.c
@@ -53,6 +53,7 @@
 #include <sys/resource.h>
 #include "common/utils/load_module_shlib.h"
 #include "common/config/config_userapi.h"
+#include "common/utils/threadPool/thread-pool.h"
 #include "executables/softmodem-common.h"
 #include <readline/history.h>
 
@@ -512,7 +513,17 @@ int process_command(char *buf) {
       } else {
         for (k=0 ; telnetparams.CmdParsers[i].cmd[k].cmdfunc != NULL ; k++) {
           if (strncasecmp(cmd, telnetparams.CmdParsers[i].cmd[k].cmdname,sizeof(telnetparams.CmdParsers[i].cmd[k].cmdname)) == 0) {
-            telnetparams.CmdParsers[i].cmd[k].cmdfunc(cmdb, telnetparams.telnetdbg, client_printf);
+          	if (telnetparams.CmdParsers[i].cmd[k].qptr != NULL) {
+          		notifiedFIFO_elt_t *msg =newNotifiedFIFO_elt(sizeof(telnetsrv_qmsg_t),0,NULL,NULL);
+          		telnetsrv_qmsg_t *cmddata=NotifiedFifoData(msg);
+          		cmddata->cmdfunc=telnetparams.CmdParsers[i].cmd[k].cmdfunc;
+          	    cmddata->prnt=client_printf;
+          	    cmddata->debug=telnetparams.telnetdbg;
+          		cmddata->cmdbuff=strdup(cmdb);
+          		pushNotifiedFIFO(telnetparams.CmdParsers[i].cmd[k].qptr, msg);
+          	} else {
+              telnetparams.CmdParsers[i].cmd[k].cmdfunc(cmdb, telnetparams.telnetdbg, client_printf);
+            }
             rt= CMDSTATUS_FOUND;
           }
         } /* for k */
@@ -758,6 +769,7 @@ int telnetsrv_autoinit(void) {
 */
 int add_telnetcmd(char *modulename, telnetshell_vardef_t *var, telnetshell_cmddef_t *cmd) {
   int i;
+  notifiedFIFO_t *afifo=NULL;
 
   if( modulename == NULL || var == NULL || cmd == NULL) {
     fprintf(stderr,"[TELNETSRV] Telnet server, add_telnetcmd: invalid parameters\n");
@@ -769,6 +781,13 @@ int add_telnetcmd(char *modulename, telnetshell_vardef_t *var, telnetshell_cmdde
       strncpy(telnetparams.CmdParsers[i].module,modulename,sizeof(telnetparams.CmdParsers[i].module)-1);
       telnetparams.CmdParsers[i].cmd = cmd;
       telnetparams.CmdParsers[i].var = var;
+      if (cmd->cmdflags & TELNETSRV_CMDFLAG_PUSHINTPOOLQ) {
+      	  if (afifo == NULL) {
+      	  	  afifo = malloc(sizeof(notifiedFIFO_t));
+      	  	  initNotifiedFIFO(afifo);
+      	  }
+      	  cmd->qptr = afifo;
+      }
       printf("[TELNETSRV] Telnet server: module %i = %s added to shell\n",
              i,telnetparams.CmdParsers[i].module);
       break;
diff --git a/common/utils/telnetsrv/telnetsrv.h b/common/utils/telnetsrv/telnetsrv.h
index debb9f02ec9..32ec2b9666f 100644
--- a/common/utils/telnetsrv/telnetsrv.h
+++ b/common/utils/telnetsrv/telnetsrv.h
@@ -54,12 +54,24 @@
 typedef void(*telnet_printfunc_t)(const char* format, ...);
 typedef int(*cmdfunc_t)(char*, int, telnet_printfunc_t prnt);
 
+#define TELNETSRV_CMDFLAG_PUSHINTPOOLQ   (1<<0)    // ask the telnet server to push the command in a thread pool queue
 typedef struct cmddef {
     char cmdname[TELNET_CMD_MAXSIZE];
     char helpstr[TELNET_HELPSTR_SIZE];
     cmdfunc_t cmdfunc; 
+    unsigned int cmdflags;
+    void *qptr;
 } telnetshell_cmddef_t;
 
+/*----------------------------------------------------------------------------*/
+/* structure used to send a command via a message queue to enable */
+/* executing a command in a thread different from the telnet server thread */
+typedef struct telnetsrv_qmsg {
+  cmdfunc_t cmdfunc;
+  telnet_printfunc_t prnt;
+  int debug;
+  char *cmdbuff;
+} telnetsrv_qmsg_t;
 /*----------------------------------------------------------------------------*/
 /*structure to be used when adding a module to the telnet server */
 /* This is the first parameter of the add_telnetcmd function, which can be used   */
@@ -111,9 +123,6 @@ typedef struct {
 } telnetsrv_params_t;
 
 
-
-typedef int(*addcmdfunc_t)(char*, telnetshell_vardef_t*, telnetshell_cmddef_t*);
-
 typedef void(*settelnetmodule_t)(char *name, void *ptr); 
 
 /*-------------------------------------------------------------------------------------------*/
@@ -133,6 +142,7 @@ VT escape sequence definition, for smarter display....
 
 /*---------------------------------------------------------------------------------------------*/
 #define TELNET_ADDCMD_FNAME "add_telnetcmd"
+#define TELNET_ADDQUEUEDCMD_FNAME "add_telnetqueuedcmd"
 typedef int(*add_telnetcmd_func_t)(char *, telnetshell_vardef_t *, telnetshell_cmddef_t *);
 #ifdef TELNETSERVERCODE
 int add_telnetcmd(char *modulename, telnetshell_vardef_t *var, telnetshell_cmddef_t *cmd);
diff --git a/openair1/SIMULATION/TOOLS/random_channel.c b/openair1/SIMULATION/TOOLS/random_channel.c
index 0cc0ba55017..af266a6600b 100644
--- a/openair1/SIMULATION/TOOLS/random_channel.c
+++ b/openair1/SIMULATION/TOOLS/random_channel.c
@@ -46,7 +46,9 @@ static mapping channelmod_names[] = {
 
 static int channelmod_show_cmd(char *buff, int debug, telnet_printfunc_t prnt);
 static int channelmod_modify_cmd(char *buff, int debug, telnet_printfunc_t prnt);
+static int channelmod_print_help(char *buff, int debug, telnet_printfunc_t prnt);
 static telnetshell_cmddef_t channelmod_cmdarray[] = {
+  {"help","",channelmod_print_help},
   {"show","<predef,current>",channelmod_show_cmd},
   {"modify","<channelid> <param> <value>",channelmod_modify_cmd},  
   {"","",NULL},
@@ -286,7 +288,7 @@ channel_desc_t *new_channel_desc_scm(uint8_t nb_tx,
   channel_desc_t *chan_desc = (channel_desc_t *)malloc(sizeof(channel_desc_t));
   for(int i=0; i<max_chan;i++) {
   	  if (defined_channels[i] == NULL) {
-  	  	  defined_channels[i]=chan_desc;
+  	  	  defined_channels[i]=chan_desc;                             
   	  	  chan_desc->chan_idx=i;
   	      break;
   	  }
@@ -438,7 +440,7 @@ channel_desc_t *new_channel_desc_scm(uint8_t nb_tx,
       }
 
       break;
-
+                               
     case EPA:
       chan_desc->nb_taps        = 7;
       chan_desc->Td             = .410;
@@ -1504,11 +1506,18 @@ double N_RB2channel_bandwidth(uint16_t N_RB) {
       LOG_E(PHY,"Unknown N_PRB\n");
       return(-1);
   }
-
   return(channel_bandwidth);
-}
-static void display_channelmodel_help( telnet_printfunc_t prnt) {
-
+}   
+
+     
+static int channelmod_print_help(char *buff, int debug, telnet_printfunc_t prnt ) {
+	prnt("channelmod commands can be used to display or modify channel models parameters\n");
+	prnt("channelmod show predef: display predefined model algorithms available in oai\n");
+	prnt("channelmod show current: display the currently used models in the running executable\n");
+	prnt("channelmod modify <model index> <param name> <param value>: set the specified parameters in a current model to the given value\n");
+	prnt("                  <model index> specifies the model, the show current model command can be used to list the current models indexes\n");
+	prnt("                  <param name> can be one of \"riceanf\", \"aoa\", \"randaoa\", \"ploss\", \"offset\", \"forgetf\"\n");
+    return CMDSTATUS_FOUND;
 }
 
 
@@ -1522,10 +1531,10 @@ static void display_channelmodel(channel_desc_t *cd,int debug, telnet_printfunc_
 		 cd->ip, cd->nb_paths);		
 	for (int i=0; i<cd->nb_taps ; i++) {
 		prnt("taps: %i   lin. ampli. : %lf    delay: %lf \n",i,cd->amps[i], cd->delays[i]);
-	}
+	}                         
 }
-
-
+   
+  
 static int channelmod_show_cmd(char *buff, int debug, telnet_printfunc_t prnt) {
   char *subcmd=NULL; 
   int s = sscanf(buff,"%ms\n",&subcmd);
@@ -1543,14 +1552,14 @@ static int channelmod_show_cmd(char *buff, int debug, telnet_printfunc_t prnt) {
         }
       }
     } else {
-    	display_channelmodel_help(prnt);
+    	channelmod_print_help(buff, debug, prnt);
     }
   free(subcmd);
   }
   return CMDSTATUS_FOUND;
 }
 
-
+      
 
 static int channelmod_modify_cmd(char *buff, int debug, telnet_printfunc_t prnt) {
   char *param=NULL, *value=NULL; 
@@ -1569,15 +1578,21 @@ static int channelmod_modify_cmd(char *buff, int debug, telnet_printfunc_t prnt)
     if ( strcmp(param,"riceanf") == 0) {
         double dbl = atof(value);
         if (dbl <0 || dbl > 1)
-        	prnt("ricean factor range: 0 to 1, %lf is outof range\n",dbl);
+        	prnt("ERROR: ricean factor range: 0 to 1, %lf is outof range\n",dbl);
         else
         	defined_channels[cd_id]->ricean_factor=dbl;
     } else if ( strcmp(param,"aoa") == 0) {
         double dbl = atof(value);
         if (dbl <0 || dbl >6.28)
-        	prnt("angle of arrival range: 0 to 2*Pi,  %lf is outof range\n",dbl);
+        	prnt("ERROR: angle of arrival range: 0 to 2*Pi,  %lf is outof range\n",dbl);
         else
         	defined_channels[cd_id]->aoa=dbl;    
+    } else if ( strcmp(param,"randaoa") == 0) {
+        int i = atoi(value);
+        if (i!=0 && i!=1)
+        	prnt("ERROR: randaoa is a boolean, must be 0 or 1\n");
+        else
+        	defined_channels[cd_id]->random_aoa=i;
     } else if ( strcmp(param,"ploss") == 0) {
         double dbl = atof(value);
         defined_channels[cd_id]->path_loss_dB=dbl;     
@@ -1587,21 +1602,21 @@ static int channelmod_modify_cmd(char *buff, int debug, telnet_printfunc_t prnt)
     } else if ( strcmp(param,"forgetf") == 0) {
         double dbl = atof(value);
         if (dbl <0 || dbl > 1)
-        	prnt("forgetting factor range: 0 to 1 (disable variation), %lf is outof range\n",dbl);
+        	prnt("ERROR: forgetting factor range: 0 to 1 (disable variation), %lf is outof range\n",dbl);
         else
         	defined_channels[cd_id]->forgetting_factor=dbl;     
     } else {
- 
+         prnt("ERROR: %s, unknown channel parameter\n",param);
+         return CMDSTATUS_FOUND;  
     }
-  display_channelmodel(defined_channels[cd_id],debug,prnt);
-  free(param);
+  display_channelmodel(defined_channels[cd_id],debug,prnt);  
+  free(param);   
   free(value);
-  
+  random_channel(defined_channels[cd_id],false);                 
   }
-  
-  return CMDSTATUS_FOUND;
+  return CMDSTATUS_FOUND;           
 }
-
+   
 int modelid_fromname(char *modelname) {
   int modelid=map_str_to_int(channelmod_names,modelname);
   AssertFatal(modelid>0,
diff --git a/targets/ARCH/rfsimulator/simulator.c b/targets/ARCH/rfsimulator/simulator.c
index ff53ca8c46f..4b0c0a35e4a 100644
--- a/targets/ARCH/rfsimulator/simulator.c
+++ b/targets/ARCH/rfsimulator/simulator.c
@@ -82,7 +82,8 @@ extern RAN_CONTEXT_t RC;
     {"IQfile",                 "<file path to use when saving IQs>\n",  0,         strptr:&(saveF),                        defstrval:"/tmp/rfsimulator.iqs",TYPE_STRING,    0 },\
     {"modelname",              "<channel model name>\n",                0,         strptr:&(modelname),                    defstrval:"AWGN",                TYPE_STRING,    0 },\
     {"ploss",                  "<channel path loss in dB>\n",           0,         dblptr:&(rfsimulator->chan_pathloss),   defdblval:0,                     TYPE_DOUBLE,    0 },\
-    {"forgetfact",             "<channel forget factor ((0 to 1)>\n",   0,         dblptr:&(rfsimulator->chan_forgetfact), defdblval:0,                     TYPE_DOUBLE,    0 }\
+    {"forgetfact",             "<channel forget factor ((0 to 1)>\n",   0,         dblptr:&(rfsimulator->chan_forgetfact), defdblval:0,                     TYPE_DOUBLE,    0 },\
+    {"offset",                 "<channel offset in samps>\n",           0,         iptr:&(rfsimulator->chan_offset),       defintval:0,                     TYPE_INT,       0 }\
   };
 
 pthread_mutex_t Sockmutex;
@@ -118,6 +119,7 @@ typedef struct {
   int channelmod;
   double chan_pathloss;
   double chan_forgetfact;
+  int    chan_offset;
 } rfsimulator_state_t;
 
 
@@ -166,7 +168,7 @@ void allocCirBuf(rfsimulator_state_t *bridge, int sock) {
                                             bridge->sample_rate,
                                             bridge->tx_bw,
                                             bridge->chan_forgetfact, // forgetting_factor
-                                            0, // maybe used for TA
+                                            bridge->chan_offset, // maybe used for TA
                                             bridge->chan_pathloss); // path_loss in dB
     random_channel(ptr->channel_model,false);
   }
-- 
GitLab