Front Haul Interface Library first seed code contribution
[o-du/phy.git] / fhi_lib / app / src / config.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 /**
21  * @brief
22  * @file
23  * @ingroup
24  * @author Intel Corporation
25  **/
26
27 #include "rte_common.h"
28 #include "config.h"
29
30 #include <rte_ethdev.h>
31
32 #define MAX_LINE_SIZE 512
33 /* Configuration file maximum supported line length */
34
35
36 #define KEY_APP_MODE        "appMode"
37 #define KEY_XRAN_MODE       "xranMode"
38 #define KEY_MU_NUMBER       "mu"
39 #define KEY_CC_PER_PORT_NUM "ccNum"
40 #define KEY_ANT_NUM         "antNum"
41 #define KEY_TTI_PERIOD      "ttiPeriod"
42 #define KEY_LLS_CU_MAC      "llsCUMac"
43 #define KEY_RU_MAC          "ruMac"
44
45 #define KEY_FILE_AxC0      "antC0"
46 #define KEY_FILE_AxC1      "antC1"
47 #define KEY_FILE_AxC2      "antC2"
48 #define KEY_FILE_AxC3      "antC3"
49 #define KEY_FILE_AxC4      "antC4"
50 #define KEY_FILE_AxC5      "antC5"
51 #define KEY_FILE_AxC6      "antC6"
52 #define KEY_FILE_AxC7      "antC7"
53 #define KEY_FILE_AxC8      "antC8"
54 #define KEY_FILE_AxC9      "antC9"
55 #define KEY_FILE_AxC10     "antC10"
56 #define KEY_FILE_AxC11     "antC11"
57 #define KEY_FILE_AxC12     "antC12"
58 #define KEY_FILE_AxC13     "antC13"
59 #define KEY_FILE_AxC14     "antC14"
60 #define KEY_FILE_AxC15     "antC15"
61
62 #define KEY_PRACH_ENABLE   "rachEanble"
63 #define KEY_PRACH_OFFSET   "rachOffset"
64 #define KEY_PRACH_CFG_IDX  "rachCfgIdx"
65
66 #define KEY_IQ_SWAP        "iqswap"
67 #define KEY_HTONS_SWAP     "nebyteorderswap"
68
69 #define KEY_TADV_CP_DL     "Tadv_cp_dl"
70 #define KEY_T2A_MIN_CP_DL  "T2a_min_cp_dl"
71 #define KEY_T2A_MAX_CP_DL  "T2a_max_cp_dl"
72 #define KEY_T2A_MIN_CP_UL  "T2a_min_cp_ul"
73 #define KEY_T2A_MAX_CP_UL  "T2a_max_cp_ul"
74 #define KEY_T2A_MIN_UP     "T2a_min_up"
75 #define KEY_T2A_MAX_UP     "T2a_max_up"
76 #define KEY_TA3_MIN        "Ta3_min"
77 #define KEY_TA3_MAX        "Ta3_max"
78 #define KEY_T1A_MIN_CP_DL  "T1a_min_cp_dl"
79 #define KEY_T1A_MAX_CP_DL  "T1a_max_cp_dl"
80 #define KEY_T1A_MIN_CP_UL  "T1a_min_cp_ul"
81 #define KEY_T1A_MAX_CP_UL  "T1a_max_cp_ul"
82 #define KEY_T1A_MIN_UP     "T1a_min_up"
83 #define KEY_T1A_MAX_UP     "T1a_max_up"
84 #define KEY_TA4_MIN        "Ta4_min"
85 #define KEY_TA4_MAX        "Ta4_max"
86
87
88 #define KEY_CP_ENABLE      "CPenable"
89 #define KEY_CP_VTAG        "c_plane_vlan_tag"
90 #define KEY_UP_VTAG        "u_plane_vlan_tag"
91 #define KEY_DEBUG_STOP     "debugStop"
92
93
94 /**
95  * Set runtime configuration parameters to their defaults.
96  *
97  * @todo Initialize missing parameters.
98  */
99 static void init_config(RuntimeConfig* config)
100 {
101     memset(config , 0, sizeof(RuntimeConfig));
102 }
103
104 /** - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - **/
105
106 static void trim(char* input)
107 {
108     uint32_t i;
109     for (i = 0; i<strlen(input); i++)
110         if (input[i] == ' ' || input[i] == '\n' || input[i] == '\t')
111             input[i] = '\0';
112 }
113
114 static int fillConfigStruct(RuntimeConfig *config, const char *key, const char *value)
115 {
116     int32_t parse_res = 0;
117
118     if (strcmp(key, KEY_APP_MODE) == 0){
119         config->appMode = atoi(value);
120     } else if (strcmp(key, KEY_XRAN_MODE) == 0) {
121         config->xranMode = atoi(value);
122     } else if (strcmp(key, KEY_CC_PER_PORT_NUM) == 0) {
123         config->numCC= atoi(value);
124     } else if (strcmp(key, KEY_MU_NUMBER) == 0) {
125         config->mu_number= atoi(value);
126     } else if (strcmp(key, KEY_ANT_NUM) == 0) {
127         config->numAxc = atoi(value);
128     } else if (strcmp(key, KEY_TTI_PERIOD) == 0) {
129         config->ttiPeriod = atoi(value);
130     } else if (strcmp(key, KEY_IQ_SWAP) == 0) {
131         config->iqswap = atoi(value);
132     } else if (strcmp(key, KEY_HTONS_SWAP) == 0) {
133         config->nebyteorderswap = atoi(value);
134     } else if (strcmp(key, KEY_LLS_CU_MAC) == 0) {
135         sscanf(value, "%02x:%02x:%02x:%02x:%02x:%02x", (uint32_t*)&config->lls_cu_addr.addr_bytes[0],
136                                            (uint32_t*)&config->lls_cu_addr.addr_bytes[1],
137                                            (uint32_t*)&config->lls_cu_addr.addr_bytes[2],
138                                            (uint32_t*)&config->lls_cu_addr.addr_bytes[3],
139                                            (uint32_t*)&config->lls_cu_addr.addr_bytes[4],
140                                            (uint32_t*)&config->lls_cu_addr.addr_bytes[5]);
141
142         printf("lls-CU MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
143             config->lls_cu_addr.addr_bytes[0],
144             config->lls_cu_addr.addr_bytes[1],
145             config->lls_cu_addr.addr_bytes[2],
146             config->lls_cu_addr.addr_bytes[3],
147             config->lls_cu_addr.addr_bytes[4],
148             config->lls_cu_addr.addr_bytes[5]);
149
150     } else if (strcmp(key, KEY_RU_MAC) == 0) {
151         sscanf(value, "%02x:%02x:%02x:%02x:%02x:%02x", (uint32_t*)&config->ru_addr.addr_bytes[0],
152                                            (uint32_t*)&config->ru_addr.addr_bytes[1],
153                                            (uint32_t*)&config->ru_addr.addr_bytes[2],
154                                            (uint32_t*)&config->ru_addr.addr_bytes[3],
155                                            (uint32_t*)&config->ru_addr.addr_bytes[4],
156                                            (uint32_t*)&config->ru_addr.addr_bytes[5]);
157
158         printf("RU MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
159             config->ru_addr.addr_bytes[0],
160             config->ru_addr.addr_bytes[1],
161             config->ru_addr.addr_bytes[2],
162             config->ru_addr.addr_bytes[3],
163             config->ru_addr.addr_bytes[4],
164             config->ru_addr.addr_bytes[5]);
165     } else if (strcmp(key, KEY_FILE_AxC0) == 0) {
166         strncpy(&config->ant_file[0][0], value, strlen(value));
167         printf("ant0: %s\n",config->ant_file[0]);
168     } else if (strcmp(key, KEY_FILE_AxC1) == 0) {
169         strncpy(&config->ant_file[1][0], value, strlen(value));
170         printf("ant1: %s\n",config->ant_file[1]);
171     } else if (strcmp(key, KEY_FILE_AxC2) == 0) {
172         strncpy(&config->ant_file[2][0], value, strlen(value));
173         printf("ant2: %s\n",config->ant_file[2]);
174     } else if (strcmp(key, KEY_FILE_AxC3) == 0) {
175         strncpy(&config->ant_file[3][0], value, strlen(value));
176         printf("ant3: %s\n",config->ant_file[3]);
177     } else if (strcmp(key, KEY_FILE_AxC4) == 0) {
178         strncpy(&config->ant_file[4][0], value, strlen(value));
179         printf("ant4: %s\n",config->ant_file[4]);
180     } else if (strcmp(key, KEY_FILE_AxC5) == 0) {
181         strncpy(&config->ant_file[5][0], value, strlen(value));
182         printf("ant5: %s\n",config->ant_file[5]);
183     } else if (strcmp(key, KEY_FILE_AxC6) == 0) {
184         strncpy(&config->ant_file[6][0], value, strlen(value));
185         printf("ant6: %s\n",config->ant_file[6]);
186     } else if (strcmp(key, KEY_FILE_AxC7) == 0) {
187         strncpy(&config->ant_file[7][0], value, strlen(value));
188         printf("ant7: %s\n",config->ant_file[7]);
189     } else if (strcmp(key, KEY_FILE_AxC8) == 0) {
190         strncpy(&config->ant_file[8][0], value, strlen(value));
191         printf("ant8: %s\n",config->ant_file[8]);
192     } else if (strcmp(key, KEY_FILE_AxC9) == 0) {
193         strncpy(&config->ant_file[9][0], value, strlen(value));
194         printf("ant9: %s\n",config->ant_file[9]);
195     } else if (strcmp(key, KEY_FILE_AxC10) == 0) {
196         strncpy(&config->ant_file[10][0], value, strlen(value));
197         printf("ant10: %s\n",config->ant_file[10]);
198     } else if (strcmp(key, KEY_FILE_AxC11) == 0) {
199         strncpy(&config->ant_file[11][0], value, strlen(value));
200         printf("ant11: %s\n",config->ant_file[11]);
201     } else if (strcmp(key, KEY_FILE_AxC12) == 0) {
202         strncpy(&config->ant_file[12][0], value, strlen(value));
203         printf("ant12: %s\n",config->ant_file[12]);
204     } else if (strcmp(key, KEY_FILE_AxC13) == 0) {
205         strncpy(&config->ant_file[13][0], value, strlen(value));
206         printf("ant13: %s\n",config->ant_file[13]);
207     } else if (strcmp(key, KEY_FILE_AxC14) == 0) {
208         strncpy(&config->ant_file[14][0], value, strlen(value));
209         printf("ant14: %s\n",config->ant_file[14]);
210     } else if (strcmp(key, KEY_FILE_AxC15) == 0) {
211         strncpy(&config->ant_file[15][0], value, strlen(value));
212         printf("ant15: %s\n",config->ant_file[15]);
213     } else if (strcmp(key, KEY_PRACH_ENABLE) == 0) {
214         config->enablePrach = atoi(value);
215         printf("Prach enable: %d\n",config->enablePrach);
216     } else if (strcmp(key, KEY_PRACH_OFFSET ) == 0) {
217         config->prachOffset = atoi(value);
218         printf("Prach Offset: %d\n",config->prachOffset);
219     } else if (strcmp(key, KEY_PRACH_CFG_IDX ) == 0) {
220         config->prachConfigIndex = atoi(value);
221         printf("Prach Conf Index: %d\n",config->prachConfigIndex);
222
223         /* timing */
224     } else if (strcmp(key, KEY_TADV_CP_DL ) == 0) {
225         config->Tadv_cp_dl = atoi(value);
226         printf("Tadv_cp_dl: %d\n",config->Tadv_cp_dl);
227     } else if (strcmp(key, KEY_T2A_MIN_CP_DL ) == 0) {
228         config->T2a_min_cp_dl = atoi(value);
229         printf("T2a_min_cp_dl: %d\n",config->T2a_min_cp_dl);
230     } else if (strcmp(key, KEY_T2A_MAX_CP_DL ) == 0) {
231         config->T2a_max_cp_dl = atoi(value);
232         printf("T2a_max_cp_dl: %d\n",config->T2a_max_cp_dl);
233     } else if (strcmp(key, KEY_T2A_MIN_CP_UL ) == 0) {
234         config->T2a_min_cp_ul = atoi(value);
235         printf("T2a_min_cp_ul: %d\n",config->T2a_min_cp_ul);
236     } else if (strcmp(key, KEY_T2A_MAX_CP_UL ) == 0) {
237         config->T2a_max_cp_ul = atoi(value);
238         printf("T2a_max_cp_ul: %d\n",config->T2a_max_cp_ul);
239     } else if (strcmp(key, KEY_T2A_MIN_UP ) == 0) {
240         config->T2a_min_up = atoi(value);
241         printf("T2a_min_up: %d\n",config->T2a_min_up);
242     } else if (strcmp(key, KEY_T2A_MAX_UP ) == 0) {
243         config->T2a_max_up = atoi(value);
244         printf("T2a_max_up: %d\n",config->T2a_max_up);
245     } else if (strcmp(key, KEY_TA3_MIN ) == 0) {
246         config->Ta3_min = atoi(value);
247         printf("Ta3_min: %d\n",config->Ta3_min);
248     } else if (strcmp(key, KEY_TA3_MAX ) == 0) {
249         config->Ta3_max = atoi(value);
250         printf("Ta3_max: %d\n",config->Ta3_max);
251     } else if (strcmp(key, KEY_T1A_MIN_CP_DL ) == 0) {
252         config->T1a_min_cp_dl = atoi(value);
253         printf("T1a_min_cp_dl: %d\n",config->T1a_min_cp_dl);
254     } else if (strcmp(key, KEY_T1A_MAX_CP_DL ) == 0) {
255         config->T1a_max_cp_dl = atoi(value);
256         printf("T1a_max_cp_dl: %d\n",config->T1a_max_cp_dl);
257     } else if (strcmp(key, KEY_T1A_MIN_CP_UL ) == 0) {
258         config->T1a_min_cp_ul = atoi(value);
259         printf("T1a_min_cp_ul: %d\n",config->T1a_min_cp_ul);
260     } else if (strcmp(key, KEY_T1A_MAX_CP_UL ) == 0) {
261         config->T1a_max_cp_ul = atoi(value);
262         printf("T1a_max_cp_ul: %d\n",config->T1a_max_cp_ul);
263     } else if (strcmp(key, KEY_T1A_MIN_UP ) == 0) {
264         config->T1a_min_up = atoi(value);
265         printf("T1a_min_up: %d\n",config->T1a_min_up);
266     } else if (strcmp(key, KEY_T1A_MAX_UP ) == 0) {
267         config->T1a_max_up = atoi(value);
268         printf("T1a_max_up: %d\n",config->T1a_max_up);
269     } else if (strcmp(key, KEY_TA4_MIN ) == 0) {
270         config->Ta4_min = atoi(value);
271         printf("Ta4_min: %d\n",config->Ta4_min);
272     } else if (strcmp(key, KEY_TA4_MAX ) == 0) {
273         config->Ta4_max = atoi(value);
274         printf("Ta4_max: %d\n",config->Ta4_max);
275         /* end of timing */
276     } else if (strcmp(key, KEY_CP_ENABLE ) == 0) {
277         config->enableCP = atoi(value);
278         printf("CPenable: %d\n",config->enableCP);
279     } else if (strcmp(key, KEY_DEBUG_STOP ) == 0) {
280         config->debugStop = atoi(value);
281         printf("debugStop: %d\n",config->debugStop);
282     } else if (strcmp(key, KEY_CP_VTAG ) == 0) {
283         config->cp_vlan_tag = atoi(value);
284         printf("cp_vlan_tag: %d\n",config->cp_vlan_tag);
285     } else if (strcmp(key, KEY_UP_VTAG ) == 0) {
286         config->up_vlan_tag = atoi(value);
287         printf("up_vlan_tag: %d\n",config->up_vlan_tag);
288     } else {
289         printf("Unsupported configuration key [%s]\n", key);
290         return -1;
291     }
292
293     return 0;
294 }
295
296 int parseConfigFile(char *filename, RuntimeConfig *config)
297 {
298     char inputLine[MAX_LINE_SIZE] = {0};
299     int inputLen = 0;
300     int i;
301     int lineNum = 0;
302     char key[MAX_LINE_SIZE] = {0};
303     char value[MAX_LINE_SIZE] = {0};
304     FILE *file = fopen(filename, "r");
305
306     if (NULL == file) {
307         //log_err("Error while opening config file from: %s", filename);
308         return -1;
309     }
310
311     init_config(config);
312
313     for (;;) {
314         if (fgets(inputLine, MAX_LINE_SIZE, file) == NULL) {
315             if (lineNum > 0) {
316                 printf("%d lines of config file has been read.\n", lineNum);
317                 break;
318             } else {
319                 printf("Configuration file reading error has occurred.\n");
320                 fclose(file);
321                 return -1;
322             }
323         }
324
325         if (inputLine[strlen(inputLine)-1] == '\n')
326             inputLine[strlen(inputLine)-1] == '\0';
327
328         lineNum++;
329         inputLen = strlen(inputLine);
330
331         for (i=0; i<inputLen; i++)
332             if (inputLine[i] == '#') {
333                 inputLine[i] = '\0';
334                 inputLen = i + 1;
335                 break;
336             }
337
338         for (i=0; i<inputLen; i++)
339             if (inputLine[i] == '=') {
340                 strncpy(key, inputLine, i);
341                 key[i] = '\0';
342                 trim(key);
343                 if ((i + 1 > inputLen - 1) || (i - 2 > inputLen)) {
344                     //log_err("Parsing config file error at line %d", lineNum);
345                     fclose(file);
346                     return -1;
347                 }
348                 strncpy(value, &inputLine[i+1], (sizeof(value) - 1));
349                 value[inputLen-i-2] = '\0';
350                 trim(value);
351
352                 if (strlen(key) == 0 || strlen(value) == 0) {
353                     printf("Parsing config file error at line %d", lineNum);
354                     fclose(file);
355                     return -1;
356                 }
357
358                 if (fillConfigStruct(config, key, value) != 0) {
359                     fclose(file);
360                     return -1;
361                 }
362
363                 break;
364             }
365
366         memset(&inputLine[0], 0, sizeof(MAX_LINE_SIZE));
367         memset(&key[0], 0, sizeof(MAX_LINE_SIZE));
368         memset(&value[0], 0, sizeof(MAX_LINE_SIZE));
369     }
370     fclose(file);
371
372     return 0;
373 }