openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

cmd_api.c (3547B)


      1 /*
      2  * 
      3  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      4  * 
      5  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      6  *
      7  * File:        cmd_api.c
      8  * Purpose:     API mode handler
      9  */
     10 
     11 #include "bcm/port.h"
     12 #include "sal/core/alloc.h"
     13 #include "sal/core/libc.h"
     14 #include "sal/appl/io.h"
     15 #include "shared/util.h"
     16 #include "appl/diag/shell.h"
     17 #include "appl/diag/system.h"
     18 #include "appl/diag/parse.h"
     19 #include "appl/diag/cmd_cint.h"
     20 #include "sal/appl/editline/editline.h"
     21 #include "shared/util.h"
     22 #include "context.h"
     23 #include "completion.h"
     24 #include "api_mode.h"
     25 
     26 #include <cint_porting.h>
     27 #include <cint_internal.h>
     28 #include <cint_ast.h>
     29 #include <cint_eval_asts.h>
     30 #include <cint_eval_ast_print.h>
     31 
     32 #include <appl/diag/api_mode.h>
     33 
     34 typedef enum api_mode_state_e {
     35     ApiModeOff,
     36     ApiModeOn,
     37     ApiModeOnly
     38 } api_mode_state_t;
     39 
     40 STATIC api_mode_state_t api_mode_state;
     41 
     42 STATIC int
     43 api_mode_initialize(void)
     44 {
     45     /* init CINT */
     46     interp.extended_events = 1; /* enable extended event reporting */
     47     BCM_IF_ERROR_RETURN(cmd_cint_initialize());
     48     BCM_IF_ERROR_RETURN((api_mode_context_initialize()));
     49     BCM_IF_ERROR_RETURN((api_mode_completion_initialize()));
     50 
     51     return BCM_E_NONE;
     52 }
     53 
     54 STATIC int
     55 api_mode_uninitialize(void)
     56 {
     57     BCM_IF_ERROR_RETURN((api_mode_completion_uninitialize()));
     58     BCM_IF_ERROR_RETURN((api_mode_context_uninitialize()));
     59 
     60     return BCM_E_NONE;
     61 }
     62 
     63 
     64 char cmd_api_usage[] =
     65     "api [on|off|only]\n"
     66 #ifndef COMPILER_STRING_CONST_LIMIT
     67     "\ton   - enable API mode, fall through to shell\n"
     68     "\toff  - disable API mode\n"
     69     "\tonly - enable API mode, do not fall though to shell\n"
     70 #endif
     71     ;
     72 
     73 /*
     74  * Function:    cmd_api
     75  * Purpose: Set shell mode to API
     76  * Returns: CMD_USAGE/CMD_FAIL/CMD_OK.
     77  */
     78 cmd_result_t
     79 cmd_api(int unit, args_t *args)
     80 {
     81     cmd_result_t rv = CMD_FAIL;
     82     char *subcmd;
     83 
     84     if ((subcmd = ARG_GET(args)) == NULL) {
     85         sal_printf("API command mode is %s\n",
     86                    (api_mode_state != ApiModeOff) ? "on" : "off");
     87         rv = CMD_OK;
     88     } else if (sal_strcasecmp(subcmd, "ON") == 0) {
     89         if (BCM_SUCCESS(api_mode_initialize())) {
     90             api_mode_state = ApiModeOn;
     91             rv = CMD_OK;
     92         }
     93     } else if (sal_strcasecmp(subcmd, "OFF") == 0) {
     94         (void) api_mode_uninitialize();
     95         api_mode_state = ApiModeOff;
     96         rv = CMD_OK;
     97     } else if (sal_strcasecmp(subcmd, "ONLY") == 0) {
     98         if (BCM_SUCCESS(api_mode_initialize())) {
     99             api_mode_state = ApiModeOnly;
    100             rv = CMD_OK;
    101         }
    102     } else {
    103         sal_printf("API subcommand '%s' not found\n", subcmd);
    104         rv = CMD_USAGE;
    105     }
    106     return rv;
    107 }
    108 
    109 cmd_result_t
    110 diag_api_mode_process_command(int u, char *s)
    111 {
    112     int rv = API_MODE_E_FAIL;
    113 
    114     if (api_mode_state != ApiModeOff) {
    115         rv = api_mode_process_command(u, s);
    116         (void)var_set_integer("?", rv, TRUE, FALSE);
    117         if (api_mode_state == ApiModeOnly) {
    118             /* ignore returned rv so only API mode commands are run */
    119             if (API_MODE_FAILURE(rv)) {
    120                 sal_printf("Error %d\n", rv);
    121             }
    122             rv = API_MODE_E_NONE;
    123         }
    124     }
    125 
    126     return (API_MODE_FAILURE(rv)) ? CMD_FAIL : CMD_OK;
    127 }
    128 
    129 void
    130 diag_api_mode_completion_enable(int flag)
    131 {
    132     if (api_mode_state != ApiModeOff) {
    133         if (flag) {
    134             (void)api_mode_completion_initialize();
    135         } else {
    136             (void)api_mode_completion_uninitialize();
    137         }
    138     }
    139 }