1 /******************************************************************************
3 * Copyright (c) 2020 Intel.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 *******************************************************************************/
20 * @brief This file has utilities to parse the parameters passed in through the command
21 * line and configure the application based on these parameters
24 * @author Intel Corporation
28 #include "aux_cline.h"
38 typedef struct _CLINE_KEY_TABLE_
45 #define CLINE_MAXKEYS (256)
46 #define CLINE_MAX_STRING_LENGTH (128)
47 #define CLINE_MAXKEYSIZE (2048)
49 static CLINE_KEY_TABLE PhyAppKeys[CLINE_MAXKEYS];
50 static uint32_t pPhyCfgNumEntries = 0;
54 //-------------------------------------------------------------------------------------------
55 /** @ingroup group_source_auxlib_cline
59 * @return 0 if AUX_SUCCESS
62 * Initialize Cline Interface
65 //-------------------------------------------------------------------------------------------
69 pPhyCfgNumEntries = 0;
70 for (i = 0; i < CLINE_MAXKEYS; i++)
72 PhyAppKeys[i].name = NULL;
73 PhyAppKeys[i].value = NULL;
81 //-------------------------------------------------------------------------------------------
82 /** @ingroup group_source_auxlib_cline
84 * @param[in] *name String to search for
85 * @param[out] *value Pointer to location where number needs to be stored
86 * @param[in] deflt Default value to put into the value field if string is not found
88 * @return 0 if AUX_SUCCESS
91 * This funtion searchs for a string from the phycfg.xml file and returns the number that
92 * was associated in file
95 //-------------------------------------------------------------------------------------------
96 uint32_t cline_set_int(const char *name, int *value, int deflt)
100 for (i = 0; i < pPhyCfgNumEntries; i++)
102 if (PhyAppKeys[i].name)
104 if (strcasecmp(name, PhyAppKeys[i].name) == 0)
106 char *p1 = PhyAppKeys[i].value;
107 if (strstr(p1, "0x") || strstr(p1, "0X"))
110 if (cline_covert_hex_2_dec(p1, &core) != AUX_SUCCESS)
112 printf("cline_set_int Failed (%s)\n", p1);
119 *value = (int)strtol(PhyAppKeys[i].value, NULL, 0);
120 if ((*value == 0 && errno == EINVAL) || (*value == INT_MAX && errno == ERANGE))
131 // End of list reached
133 printf("incomming setting in XML: param \"%s\" is not found!\n", name);
145 //-------------------------------------------------------------------------------------------
146 /** @ingroup group_source_auxlib_cline
148 * @param[in] *name String to search for
149 * @param[out] *value Pointer to location where number needs to be stored
150 * @param[in] deflt Default value to put into the value field if string is not found
152 * @return 0 if AUX_SUCCESS
155 * This funtion searchs for a string from the phycfg.xml file and returns the number that
156 * was associated in file
159 //-------------------------------------------------------------------------------------------
160 uint32_t cline_set_uint64(const char *name, uint64_t *value, uint64_t deflt)
164 for (i = 0; i < pPhyCfgNumEntries; i++)
166 if (PhyAppKeys[i].name)
168 if (strcasecmp(name, PhyAppKeys[i].name) == 0)
170 if (strstr(PhyAppKeys[i].value, "0x") || strstr(PhyAppKeys[i].value, "0X"))
172 if (cline_covert_hex_2_dec(PhyAppKeys[i].value, value) != AUX_SUCCESS)
174 printf("cline_covert_hex_2_dec Failed (%s)\n", PhyAppKeys[i].value);
180 *value = strtoull(PhyAppKeys[i].value, NULL, 0);
182 if ((*value == 0 && errno == EINVAL) || (*value == LONG_MAX && errno == ERANGE))
192 // End of list reached
194 printf("incomming setting in XML: param \"%s\" is not found!\n", name);
203 //-------------------------------------------------------------------------------------------
204 /** @ingroup group_source_auxlib_cline
206 * @param[in] *pStr String to search for
207 * @param[out] *pDst Pointer to location where number needs to be stored
209 * @return 0 if AUX_SUCCESS
212 * This funtion takes a char string as input and converts it to a unit61_t
215 //-------------------------------------------------------------------------------------------
216 uint32_t cline_covert_hex_2_dec(char *pStr, uint64_t *pDst)
221 uint64_t value = 0, mult = 1, nibble_val = 0;
229 printf("String Length is invalid: %p [%d]\n", pStr, len);
232 for (i = len - 1; i >= 0; i--)
235 if ((nibble >= '0') && (nibble <= '9'))
237 nibble_val = nibble - '0';
239 else if ((nibble >= 'A') && (nibble <= 'F'))
241 nibble_val = nibble - 'A' + 10;
243 else if ((nibble >= 'a') && (nibble <= 'f'))
245 nibble_val = nibble - 'a' + 10;
249 printf("String is invalid: %p[%d] %c\n", pStr, i, nibble);
253 value += (nibble_val * mult);
264 //-------------------------------------------------------------------------------------------
265 /** @ingroup group_source_auxlib_cline
267 * @param[in] *name String to search for
268 * @param[out] *core Pointer to location where core id needs to be stored
269 * @param[out] *priority Pointer to location where priority needs to be stored
270 * @param[out] *policy Pointer to location where policy needs to be stored
272 * @return 0 if AUX_SUCCESS
275 * This funtion searchs for a string from the phycfg.xml file stores all thread related info into
279 //-------------------------------------------------------------------------------------------
280 uint32_t cline_set_thread_info(const char *name, uint64_t *core, int *priority, int *policy)
287 for (i = 0; i < pPhyCfgNumEntries; i++)
289 if (PhyAppKeys[i].name)
291 if (strcasecmp(name, PhyAppKeys[i].name) == 0)
293 p1 = (char*)PhyAppKeys[i].value;
294 p2 = strstr(p1, ",");
299 p3 = strstr(p2, ",");
305 if (strstr(p1, "0x") || strstr(p1, "0X"))
307 if (cline_covert_hex_2_dec(p1, core) != AUX_SUCCESS)
309 printf("cline_covert_hex_2_dec Failed (%s)\n", p1);
315 *core = strtoull(p1, NULL, 0);
317 *priority = strtol(p2, NULL, 0);
318 sched = strtol(p3, NULL, 0);
320 *policy = (sched ? SCHED_RR : SCHED_FIFO);
322 //print_info_log("%s %ld %d %d\n", name, *core, *priority, *policy);
328 printf("p3 is null %s\n", p2);
333 printf("p2 is null %s\n", p1);
336 printf("%s FAIL1\n", name);
342 // End of list reached
343 printf("incomming setting in XML: param \"%s\" is not found!\n", name);
345 printf("cline_set_thread_info: %s FAIL2\n", name);
350 printf("cline_set_thread_info: %s FAIL3\n", name);
357 //-------------------------------------------------------------------------------------------
358 /** @ingroup group_source_auxlib_cline
360 * @param[in] *name String to search for
361 * @param[in] maxLen Max lenth of output array
362 * @param[out] *dataOut Pointer to the data array filled by each int element of the input string
363 * @param[out] *outLen Filled length of the array
365 * @return 0 if AUX_SUCCESS
368 * This funtion searchs for a string from the phycfg.xml file stores all int value to output array
371 //-------------------------------------------------------------------------------------------
372 uint32_t cline_set_int_array(const char *name, int maxLen, int *dataOut, int *outLen)
378 for (i = 0; i < pPhyCfgNumEntries; i++)
380 if (PhyAppKeys[i].name)
382 if (strcasecmp(name, PhyAppKeys[i].name) == 0)
384 p1 = (char*)PhyAppKeys[i].value;
385 while(*outLen < maxLen)
387 p2 = strstr(p1, ",");
392 dataOut[*outLen] = strtol(p1, NULL, 0);
393 //printf("\ngranularity %d in idx %d",dataOut[*outLen],*outLen);
399 dataOut[*outLen] = strtol(p1, NULL, 0);
400 //printf("\ngranularity %d in idx %d",dataOut[*outLen],*outLen);
410 //printf("cline_set_int_array: Could not find %s\n", name);
417 //-------------------------------------------------------------------------------------------
418 /** @ingroup group_source_auxlib_cline
420 * @param[in] *name String to search for
421 * @param[out] *value Pointer to location where string needs to be stored
422 * @param[in] deflt Default value to put into the value field if string is not found
424 * @return 0 if AUX_SUCCESS
427 * This funtion searchs for a string from the phycfg.xml file and returns the string that
428 * was associated in file
431 //-------------------------------------------------------------------------------------------
432 uint32_t cline_set_str(const char *name, char *value, const char *deflt)
436 for (i = 0; i < CLINE_MAXKEYS; i++)
438 if (PhyAppKeys[i].name)
440 if (strcasecmp(name, PhyAppKeys[i].name) == 0)
442 strcpy(value, PhyAppKeys[i].value);
448 // End of list reached
449 strcpy(value, deflt);
460 //-------------------------------------------------------------------------------------------
461 /** @ingroup group_source_auxlib_cline
463 * @param[in] *pString Pointer to string that needs to be parsed
465 * @return 0 if AUX_SUCCESS
468 * This function takes a line from phycfg.xml and parses it and if valid fields are found,
469 * stores the xml tag and the value associated with the tag into a global structure.
472 //-------------------------------------------------------------------------------------------
473 int cline_parse_line(char *pString)
475 char *stringLocal, *ptr1, *ptr2, *ptr3;
476 // char stringName[CLINE_MAX_STRING_LENGTH] = "", stringValue[CLINE_MAX_STRING_LENGTH] = "";
477 char *stringName, *stringValue;
482 ptr1 = ptr2 = ptr3 = NULL;
483 uint32_t strLen = strlen(pString);
486 stringLocal = (char *)malloc(strLen + 1);
487 if (stringLocal == NULL)
489 printf("Cannot allocate stringLocal of size %d\n", (strLen + 1));
494 // Dont Destroy Original String
496 strcpy(stringLocal, pString);
500 if (strlen(stringLocal) <= 2) // Probably line feed
511 ptr2 = strstr(ptr1, "<");
516 printf("no begin at parameters string");
520 // See if this is a comment
522 ptr3 = strstr(ptr2, "!--");
532 ptr3 = strstr(ptr2, ">");
537 printf("no ending at parameters string");
545 strLen = strlen(ptr2 + 1);
548 stringName = (char *)malloc(strLen + 1);
549 if (stringName == NULL)
553 printf("Cannot allocate stringName of size %d\n", (strLen + 1));
559 strcpy(stringName, ptr2 + 1);
567 ptr2 = strstr(ptr1, "<");
572 ptr3 = strstr(ptr2, ">");
579 printf("no ending at parameters string");
588 strLen = strlen(ptr1);
591 stringValue = (char *)malloc(strLen + 1);
592 if (stringValue == NULL)
598 printf("Cannot allocate stringValue of size %d\n", (strLen + 1));
602 strcpy(stringValue, ptr1);
606 printf("Found String: %s with Value: %s\n", stringName, stringValue);
613 len = strlen(stringName);
616 PhyAppKeys[pPhyCfgNumEntries].name = (char *) malloc(len+1);
617 if (PhyAppKeys[pPhyCfgNumEntries].name == NULL)
625 printf("Cannot allocate PhyAppKeys[pPhyCfgNumEntries].name of size %d\n", (strLen + 1));
629 strcpy(PhyAppKeys[pPhyCfgNumEntries].name, stringName);
636 len = strlen(stringValue);
639 PhyAppKeys[pPhyCfgNumEntries].value = (char *)malloc(len + 1);
640 if (PhyAppKeys[pPhyCfgNumEntries].value == NULL)
648 printf("Cannot allocate PhyAppKeys[pPhyCfgNumEntries].value of size %d\n", (strLen + 1));
652 strcpy(PhyAppKeys[pPhyCfgNumEntries].value, stringValue);
671 //-------------------------------------------------------------------------------------------
672 /** @ingroup group_source_auxlib_cline
679 * This function prints all the tags and values found in the phycfg.xml file after parsing
682 //-------------------------------------------------------------------------------------------
683 void cline_print_info(void)
688 for (i = 0; i < pPhyCfgNumEntries; i++)
690 if (PhyAppKeys[i].name)
692 printf(" --%s=%s\n", PhyAppKeys[i].name, PhyAppKeys[i].value);
702 //-------------------------------------------------------------------------------------------
703 /** @ingroup group_source_auxlib_cline
705 * @param[in] argc Number of command line params
706 * @param[in] *argv[] Array of command line params
707 * @param[in] pString String to search for
708 * @param[out] *pDest Location where to store the payload
710 * @return 0 if AUX_SUCCESS
713 * This function looks for a string passed in from the command line and returns the immediate
714 * next parameter passed after this.
717 //-------------------------------------------------------------------------------------------
718 uint32_t cline_get_string(int argc, char *argv[], char* pString, char *pDest)
720 uint32_t ret = AUX_FAILURE;
721 int i = 1, length = (int)strlen(pString);
722 char *filename = NULL;
724 //print_info_log("Searching for string: %s. Length of string: %d\n", pString, length);
727 if (strstr(argv[i], pString) != NULL)
729 filename = strstr(argv[i], pString);
730 filename += (length + 1);
732 //print_info_log("Found %s: Val = %s\n", pString, filename);
733 strcpy(pDest, filename);