cli_cmd.c 11.3 KB
Newer Older
1
/*******************************************************************************
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


11 12 13 14
    OpenAirInterface 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.
15

16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27 28 29 30 31 32

*******************************************************************************/

/*! \file cli_cmd.c
* \brief oai cli commands
* \author Navid Nikaein
33
* \date 2011 - 2014
34 35 36 37 38 39 40 41 42 43 44 45
* \version 0.1
* \warning This component can be runned only in user-space
* @ingroup util

*/


#include "cli.h"
#include "OCG.h"
#include "OCG_extern.h"
#include "log.h"
#include "log_extern.h"
alexandr's avatar
alexandr committed
46
#include "rrc_eNB_primitives.h"
47 48 49 50

extern cli_config *cli_cfg;

#define NEXT_OPT do {optv++;optc--; } while (0)
51 52 53 54 55 56 57 58
#define CHECK_OPTC do { if(!optc) {         \
      if((optc - 1) == 1){            \
  fprintf(stderr, "[CLI]Error parsing command line options!\n");  \
      } else {                \
  optv--;               \
  fprintf(stderr, "[CLI]You must provide a parameter when using the %s switch!\n", *optv); \
      }                 \
    } } while (0)
59 60 61


mapping log_comp[] = {
62 63 64 65 66 67
  {"phy", 2},
  {"mac", 3},
  {"emu", 4},
  {"ocg", 5},
  {"omg", 6},
  {NULL, -1}
68 69
};
mapping log_level[] = {
70 71 72 73 74
  {"error", 3},
  {"warn", 4},
  {"info", 6},
  {"debug", 7},
  {NULL, -1}
75 76 77
};

mapping log_flag[] = {
78 79 80 81 82
  {"none", 0x0},
  {"low", 0x04},
  {"med", 0x34},
  {"full", 0x74},
  {NULL, -1}
83 84 85 86 87 88
};

/* ************************ CLI COMMANDS ************************** */


/* The user wishes to quit using this program.  Just set DONE non-zero. */
89 90
int com_exit (char *arg)
{
91 92 93 94
  //set the xit value for the threads
  cli_cfg->exit = 1;
  cli_cfg->promptchar='\0';
  cli_cfg->prompt[0]='\0';
95
  snprintf(cli_cfg->prompt,200,"%s","oaicli");
96 97 98 99 100
  close(cli_cfg->cfd);
  return (0);
}

/* CLI error Function . */
101 102
void cli_error (char *caller)
{
103 104 105 106
  fprintf (stderr,"[CLI]an error running occured during the execution of %s function\n",
           caller);
}

107 108
int prompt (char *arg)
{
109
  // char buf[200];
110

111 112
  if (cli_help("prompt", arg) == OK)
    return 0;
113 114

  if (!arg || !*arg) { // no arg is provided, run get func
115 116 117
    snprintf(buffer,200,"%s\n", cli_cfg->prompt);
    send(cli_cfg->cfd, buffer, strlen(buffer), 0);
    // send(cli_cfg->cfd, cli_cfg->prompt, strlen(cli_cfg->prompt), 0);
118
  } else {// set func
119 120
    snprintf(cli_cfg->prompt,200,"%s",arg);
    sprintf( cli_cfg->prompt,"%s%c ", cli_prompt(),  cli_cfg->promptchar);
121

122
  }
123

124 125 126
  return (0);
}

127 128
int info (char *arg)
{
129 130 131 132 133 134 135

  sprintf(buffer, "CLI by user %s from host %s\n", username, host);
  send(cli_cfg->cfd,buffer, strlen(buffer),0);
  return 0;
}


136 137
int start (char *arg)
{
138 139 140 141
  // char buf[200];
  char* optv[20];
  int optc=0;

142

143 144
  if (cli_help("start", arg) == OK)
    return OK;
145 146

  if (valid_argument ("start", arg) == ERR) {
147
    start_usage("");
148 149
    return ERR;
  }
150

151 152 153 154 155 156
  optc = token_argument(arg, optv);
  process_argument(optc, optv);

  return (0);
}

157 158
int set(char *arg)
{
159 160 161 162 163
  char* optv[20];
  int  optc=0;

  if (cli_help("set", arg) == OK)
    return OK;
164 165

  if (valid_argument ("set", arg) == ERR) {
166
    set_usage( NULL );
167 168
    return ERR;
  }
169

170 171 172 173 174 175 176
  optc = token_argument(arg, optv);
  process_argument(optc, optv);
  return OK;

}
/* Return non-zero if ARG is a valid argument for CALLER, else print
   an error message and return zero. */
177 178 179
int valid_argument (char *caller, char *arg)
{

180
  if (!arg || !*arg)  {
181 182 183 184 185
    snprintf(buffer,200,"%s: argument required\n", caller);
    send(cli_cfg->cfd, buffer, strlen(buffer), 0);
    fprintf (stderr, "%s: argument required\n", caller);
    return (ERR);
  }
186 187 188 189

  return (OK);
}

190 191
int cli_help(char *caller, char * arg)
{
192 193

  command *command;
194

195 196
  if ((strcmp(arg, "-h") == 0 ) || (strcmp(arg, "?") == 0 ) || (strcmp(arg, "help") == 0 ) ||
      (strcmp(arg, "-help") == 0 ) ||(strcmp(arg, "--help") == 0 )) {
197

198 199 200
    command = find_command (caller); // we are sure that the command exists so no check is done
    return ((*(command->help))(caller)); // return zero after calling help func
  }
201

202 203 204
  return ERR; // no help is requested bu the user
}

205 206 207
int token_argument(char *arg, char* optv[])
{

208
  int tokc;
209

210 211
  tokc = 0;
  optv[tokc] = strtok(arg, " "); // get the first token
212

213 214
  while (optv[tokc]!=NULL) {
    tokc++;
215
    optv[tokc] = strtok(NULL, " ");
216
  }
217 218 219

  return tokc;

220 221
}

222 223 224
int process_argument(int optc, char* optv[])
{

225 226
  int index;
  int state;
227 228
  long value;
  float value1;
229
  int comp=0, level=0, flag=0x34, interval=0;
230

231
  while (optc > 0) {
232 233

    /* add an RB */
234
    if ((strcmp(*optv, "ue") == 0) || (strcmp(*optv, "UE") == 0) ) {
235 236 237

      NEXT_OPT;
      CHECK_OPTC;
238
      index = atoi (*optv);
239 240 241 242 243 244 245 246 247

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        state = atoi (*optv);
        printf("[CLI] UE %d state %d\n", index, state);
        oai_emulation.info.cli_start_ue[index]=state;
      } else
        return ERR;
248
    }
249

250
    if ((strcmp(*optv, "enb") == 0) || (strcmp(*optv, "eNB") == 0) ) {
251 252 253

      NEXT_OPT;
      CHECK_OPTC;
254
      index = atoi (*optv);
255

256
      if (optc > 0) {
257 258 259 260 261 262 263
        NEXT_OPT;
        CHECK_OPTC;
        state = atoi (*optv);
        printf("[CLI] eNB %d state %d\n", index, state);
        oai_emulation.info.cli_start_enb[index]= state;
      } else
        return ERR;
264
    }
265

alexandr's avatar
alexandr committed
266 267
    // Handover CLI

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    // Set hysteresis value
    if ((strcmp(*optv, "hys") == 0) || (strcmp(*optv, "HYS") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
        printf("[CLI] Hysteresis is set to %ld for eNB %d\n",value,index);
        set_hys(index,value);
      } else
        return ERR;
    }

    // Set time to trigger value
    if ((strcmp(*optv, "ttt_ms") == 0) || (strcmp(*optv, "TTT_ms") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
        printf("[CLI] Time to trigger is set to %ld for eNB %d\n",value,index);
        set_ttt_ms(index,value);
      } else
        return ERR;
    }

alexandr's avatar
alexandr committed
302 303 304 305 306 307 308 309 310 311 312
    // Set ofn value
    if ((strcmp(*optv, "ofn") == 0) || (strcmp(*optv, "OFN") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
313
        printf("[CLI] OFN is set to %ld for eNB %d\n",value,index);
alexandr's avatar
alexandr committed
314 315 316 317
        set_ofn(index,value);
      } else
        return ERR;
    }
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    // Set ocn value
    if ((strcmp(*optv, "ocn") == 0) || (strcmp(*optv, "OCN") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
        printf("[CLI] OCN is set to %ld for eNB %d\n",value,index);
        set_ocn(index,value);
      } else
        return ERR;
    }

    // Set ofs value
    if ((strcmp(*optv, "ofs") == 0) || (strcmp(*optv, "OFS") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
        printf("[CLI] OFS is set to %ld for eNB %d\n",value,index);
        set_ofs(index,value);
      } else
        return ERR;
    }

    // Set ocs value
    if ((strcmp(*optv, "ocs") == 0) || (strcmp(*optv, "OCS") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
        printf("[CLI] OCS is set to %ld for eNB %d\n",value,index);
        set_ocs(index,value);
      } else
        return ERR;
    }

    // Set off value
    if ((strcmp(*optv, "off") == 0) || (strcmp(*optv, "OFF") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value = atoi (*optv);
        printf("[CLI] OFF is set to %ld for eNB %d\n",value,index);
        set_off(index,value);
      } else
        return ERR;
    }

    // Set L3 filtering coefficient RSRP value
    if ((strcmp(*optv, "rsrp_filter_coeff") == 0) || (strcmp(*optv, "RSRP_FILTER_COEFF") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value1 = atoi (*optv);
        printf("[CLI] L3 RSRP filtering coefficient is set to %f for eNB %d\n",value1,index);
        set_rsrp_filter_coeff(index,value);
      } else
        return ERR;
    }

    // Set L3 filtering coefficient RSRQ value
    if ((strcmp(*optv, "rsrq_filter_coeff") == 0) || (strcmp(*optv, "RSRQ_FILTER_COEFF") == 0) ) {

      NEXT_OPT;
      CHECK_OPTC;
      index = atoi (*optv);

      if (optc > 0) {
        NEXT_OPT;
        CHECK_OPTC;
        value1 = atoi (*optv);
        printf("[CLI] L3 RSRQ filtering coefficient is set to %f for eNB %d\n",value1,index);
        set_rsrq_filter_coeff(index,value);
      } else
        return ERR;
    }
alexandr's avatar
alexandr committed
419

420 421
    // fixme
    if ((strcmp(*optv, "log") == 0) || (strcmp(*optv, "LOG") == 0) ) {
422 423 424

      NEXT_OPT;
      CHECK_OPTC;
425
      comp = map_str_to_int(log_comp, *optv);
426 427
      NEXT_OPT;

428
      if (optc > 0) {
429 430 431 432 433
        CHECK_OPTC;
        level = map_str_to_int(log_level, *optv);
      } else
        return ERR;

434
      NEXT_OPT;
435

436
      if (optc > 0) {
437 438
        CHECK_OPTC;
        flag = map_str_to_int (log_flag, *optv);
439
      }
440

441
      NEXT_OPT;
442

443
      if (optc > 0) {
444 445
        CHECK_OPTC;
        interval = atoi (*optv);
446
      }
447 448 449

      printf("[CLI] OMG set log for comp %d level %d flag 0x%x interval %d\n",
             comp, level, flag, interval);
450 451
      set_comp_log(comp, level, flag, interval);
    }
452

453 454
    NEXT_OPT;
  }
455

456 457 458 459
  return OK;
}


460 461
int prompt_usage(char *arg)
{
462 463
  sprintf(buffer, "Usage: prompt [value]\n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
464

465 466
  return 0;
}
467 468
int start_usage(char *arg)
{
469 470 471 472 473 474
  sprintf(buffer, "Usage: start [enb|UE] [enb_index][0: stop, 1: start] \n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
  sprintf(buffer, "Example to start enb 0: start enb 0 1 \n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
  sprintf(buffer, "Example to stop ue &: start ue 1 0 \n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
475

476 477 478
  return 0;
}

479
int set_usage( char *arg )
480
{
481
  (void)(arg); // unused
482 483 484 485 486 487
  sprintf(buffer, "Usage: set log [comp] [level:debug,info,warn,error][flag:none,low,med,full][intervale: 1-100] \n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
  sprintf(buffer, "Example 1: set log omg debug\n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
  sprintf(buffer, "Example 1: set log omg info med 10\n");
  send(cli_cfg->cfd, buffer, strlen(buffer), 0);
488

489 490
  return 0;
}