Update to odulow per maintenance bronze
[o-du/phy.git] / fapi_5g / source / utils / nr5g_fapi_cmd.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief This file has the command line interface for the testmac application
21  * @file nr5g_fapi_cmd.c
22  * @ingroup group_source_utils
23  * @author Intel Corporation
24  **/
25
26 #include "nr5g_fapi_std.h"
27 #include "nr5g_fapi_framework.h"
28 #include "nr5g_fapi_wls.h"
29
30 #define NUM_CMDS 5
31 #define CMD_SIZE 32
32 #define ORAN_5G_FAPI_LOGO "ORAN_5G_FAPI>"
33
34 enum {
35     NR5G_FAPI_CMGR_HELP = 0,
36     NR5G_FAPI_CMGR_EXIT,
37     NR5G_FAPI_CMGR_VERS,
38     NR5G_FAPI_CMGR_WLS_STATS,
39     NR5G_FAPI_CMGR_NULL,
40 };
41
42 static char nr5g_fapi_cmd_registry[NUM_CMDS][CMD_SIZE] = {
43     {"help"}, {"exit"}, {"version"}, {"show wls stats"}
44 };
45
46 static char *nr5g_fapi_cmgr_char_get(
47     void)
48 {
49     static char str[CMD_SIZE + 2];
50     int ch, n = 0;
51
52     while ((ch = getchar()) != EOF && n < CMD_SIZE && ch != '\n') {
53         str[n] = ch;
54         ++n;
55     }
56
57     if (n > 0 && n < CMD_SIZE) {
58         str[n] = (char)0;
59         return str;
60     } else {
61         return NULL;
62     }
63 }
64
65 static int nr5g_fapi_cmgr_type(
66     char *cmd)
67 {
68     int len = 0, i;
69     for (i = 0; i < NUM_CMDS; i++) {
70         nr5g_fapi_cmd_registry[i][sizeof(nr5g_fapi_cmd_registry[i]) - 1] = 0;
71         len = strlen(nr5g_fapi_cmd_registry[i]);
72         if (len < CMD_SIZE) {
73             if (!strncmp(cmd, nr5g_fapi_cmd_registry[i], len)) {
74                 return i;
75             }
76         }
77     }
78
79     return NR5G_FAPI_CMGR_NULL;
80 }
81
82 //-------------------------------------------------------------------------------------------
83 /** @ingroup group_testmac
84  *
85  *  @param[in]   paramc Number of command line parameters
86  *  @param[in]   params Pointer to structure that has the command line parameter
87  *  @param[in]   *cmd_param Pointer to additional parameters
88  *
89  *  @return  0 if SUCCESS
90  *
91  *  @description
92  *  This function prints the oran_5g_fapi version to the oran_5g_fapi console
93  *
94 **/
95 //-------------------------------------------------------------------------------------------
96 static void nr5g_fapi_cmd_print_version(
97     )
98 {
99     //printf("\n ORAN_5G_FAPI Version: \n");
100 }
101
102 //-------------------------------------------------------------------------------------------
103 /** @ingroup group_testmac
104  *
105  *  @param[in]   paramc Number of command line parameters
106  *  @param[in]   params Pointer to structure that has the command line parameter
107  *  @param[in]   *cmd_param Pointer to additional parameters
108  *
109  *  @return  0 if SUCCESS
110  *
111  *  @description
112  *  This function exits the testmac application
113  *
114 **/
115 //-------------------------------------------------------------------------------------------
116 static void nr5g_fapi_cmd_exit(
117     p_nr5g_fapi_phy_ctx_t p_phy_ctx)
118 {
119     p_phy_ctx->process_exit = 1;
120     printf("Exitting App...\n");
121     exit(0);
122 }
123
124 //-------------------------------------------------------------------------------------------
125 /** @ingroup group_testmac
126  *
127  *  @param[in]   paramc Number of command line parameters
128  *  @param[in]   params Pointer to structure that has the command line parameter
129  *  @param[in]   *cmd_param Pointer to additional parameters
130  *
131  *  @return  0 if SUCCESS
132  *
133  *  @description
134  *  This function lists all the pre-registered function and associated commands on screen.
135  *  The commands are sorted alphabetically.
136  *
137 **/
138 //-------------------------------------------------------------------------------------------
139 static void nr5g_fapi_cmd_help(
140     )
141 {
142
143 }
144
145 //-------------------------------------------------------------------------------------------
146 /** @ingroup group_testmac
147  *
148  *  @param[in]   paramc Number of command line parameters
149  *  @param[in]   params Pointer to structure that has the command line parameter
150  *  @param[in]   *cmd_param Pointer to additional parameters
151  *
152  *  @return  0 if SUCCESS
153  *
154  *  @description
155  *  This function lists all the pre-registered function and associated commands on screen.
156  *  The commands are sorted alphabetically.
157  *
158 **/
159 //-------------------------------------------------------------------------------------------
160 static void nr5g_fapi_cmd_wls_stats(
161     )
162 {
163     nr5g_fapi_wls_print_stats();
164 }
165
166 //-------------------------------------------------------------------------------------------
167 /** @ingroup group_testmac
168  *
169  *  @param   void
170  *
171  *  @return  0 if SUCCESS
172  *
173  *  @description
174  *  This function initializes all the pre-defined commands with associated functions for the testmac
175  *  application. It iscalled on system bootup
176  *
177 **/
178 //-------------------------------------------------------------------------------------------
179 void nr5g_fapi_cmgr(
180     void *config)
181 {
182     static int process_exit = 1;
183     char *cmd;
184     int cmd_type;
185     UNUSED(config);
186     //p_nr5g_fapi_cfg_t cfg = (p_nr5g_fapi_cfg_t) config; // start up config
187     p_nr5g_fapi_phy_ctx_t p_phy_ctx = nr5g_fapi_get_nr5g_fapi_phy_ctx();
188
189     fflush(stdout);
190     printf("\n");
191     while (process_exit) {
192         printf("%s", ORAN_5G_FAPI_LOGO);
193
194         cmd = nr5g_fapi_cmgr_char_get();
195         if (!cmd)
196             continue;
197
198         cmd_type = nr5g_fapi_cmgr_type(cmd);
199         switch (cmd_type) {
200             case NR5G_FAPI_CMGR_VERS:
201                 nr5g_fapi_cmd_print_version();
202
203                 break;
204             case NR5G_FAPI_CMGR_HELP:
205                 nr5g_fapi_cmd_help();
206                 break;
207
208             case NR5G_FAPI_CMGR_EXIT:
209                 nr5g_fapi_cmd_exit(p_phy_ctx);
210                 break;
211
212             case NR5G_FAPI_CMGR_WLS_STATS:
213                 nr5g_fapi_cmd_wls_stats();
214                 break;
215
216             case NR5G_FAPI_CMGR_NULL:
217             default:
218                 printf("Warning: command (%s) not present\n", cmd);
219                 break;
220         }
221     }
222 }