test/throughput/throughput_cli_parser.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "gos.h"
#include "throughput_cli_parser.h"
#define THROUGHPUT_START_SERVER_STRING "-s"
#define THROUGHPUT_TIME_STRING "-t"
#define THROUGHPUT_PORT_STRING "-p"
#define THROUGHPUT_UPDATE_STRING "-i"
#define THROUGHPUT_IP_STRING "-ip"
#define THROUGHPUT_UDP_STRING "-u"
#define STR_TO_UINT_NO_SCALE 1
#define THROUGHPUT_SECONDS_TO_MS 1000
#define THROUGHPUT_MAX_PORT ((uint32_t) 0xFFFF)
gos_result_t search_match(int argc, char **argv, char *match, uint8_t *index)
{
for (uint8_t i = 0; i < argc; i++)
{
if (strcmp(argv[i], match) == 0)
{
if(NULL != index)
{
// i returns the match string not the value. i + 1 is the value of the argument
if((i + 1) >= argc)
{
}
*index = i + 1;
}
result = GOS_SUCCESS;
break;
}
}
return result;
}
gos_result_t get_arg_uint(int argc, char **argv, char *search,
uint32_t max, uint32_t *result, uint16_t scale)
{
gos_result_t search_result;
uint8_t index;
// Find the string
search_result = search_match(argc, argv, search, &index);
if (search_result == GOS_SUCCESS)
{
intmax_t int_result;
if(GOS_SUCCESS == str_parse_int(argv[index], &int_result, 0, max))
{
// Convert Kilobytes to bytes or seconds to ms
*result = (uint32_t)int_result * scale;
}
else
{
search_result = GOS_CMD_RESULT_PARSE_ERROR;
}
}
return search_result;
}
gos_result_t get_arg_float(int argc, char **argv, char *search, float *result)
{
gos_result_t search_result;
uint8_t index;
// Find the string
search_result = search_match(argc, argv, search, &index);
if (search_result != GOS_SUCCESS)
{
*result = 0;
return search_result;
}
*result = str_to_float(argv[index]);
return GOS_SUCCESS;
}
gos_result_t get_arg_str(int argc, char **argv, char *search, char *result)
{
gos_result_t search_result;
uint8_t index;
// Find the string
search_result = search_match(argc, argv, search, &index);
if (search_result == GOS_SUCCESS)
{
strcpy(result, argv[index]);
}
return search_result;
}
gos_result_t throughput_process_args(int argc, char **argv, throughput_context_t *throughput)
{
float temp = 0;
gos_result_t result;
// Add time
result = get_arg_float(argc, argv, THROUGHPUT_TIME_STRING, &temp);
if (result != GOS_SUCCESS || temp == 0)
{
}
throughput->settings.time_s = (uint32_t)(temp * THROUGHPUT_SECONDS_TO_MS);
// Add interval
result = get_arg_float(argc, argv, THROUGHPUT_UPDATE_STRING, &temp);
if (result != GOS_SUCCESS)
{
throughput->settings.update_interval_ms = 0;
}
else if (temp == 0)
{
GOS_LOG("Invalid interval specified. Overriding interval with 0");
throughput->settings.update_interval_ms = 0;
}
else
{
throughput->settings.update_interval_ms = (uint32_t)(temp * THROUGHPUT_SECONDS_TO_MS);
}
GOS_LOG("interval = %ld", throughput->settings.update_interval_ms);
// Add Port
result = get_arg_uint(argc, argv, THROUGHPUT_PORT_STRING, THROUGHPUT_MAX_PORT,
(uint32_t*)&throughput->settings.port,
STR_TO_UINT_NO_SCALE);
if (result != GOS_SUCCESS)
{
}
// Process IP Address for clients
if (throughput->type == CLIENT)
{
result = get_arg_str(argc, argv, THROUGHPUT_IP_STRING, throughput->settings.ip);
if(result != GOS_SUCCESS)
{
}
}
// Add protocol. Defaults to TCP
result = search_match(argc, argv, THROUGHPUT_UDP_STRING, NULL);
if (result != GOS_SUCCESS)
{
throughput->settings.proto = THROUGHPUT_TCP;
}
else
{
throughput->settings.proto = THROUGHPUT_UDP;
}
return GOS_SUCCESS;
}
context_type_t process_session_type(int argc, char **argv)
{
context_type_t result = CLIENT;
if (GOS_SUCCESS == search_match(argc, argv, THROUGHPUT_START_SERVER_STRING, NULL))
{
result = SERVER;
}
return result;
}