Skip to content

Commit

Permalink
Merge pull request #35 from BlueAndi/programArgumentsRefactoring
Browse files Browse the repository at this point in the history
Program arguments refactoring.
  • Loading branch information
gabryelreyes authored Nov 8, 2023
2 parents 839987d + 0a3f465 commit 4d19129
Showing 1 changed file with 65 additions and 42 deletions.
107 changes: 65 additions & 42 deletions lib/ArduinoNative/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,31 @@
* Types and classes
*****************************************************************************/

#ifndef UNIT_TEST

/** This type defines the possible program arguments. */
typedef struct
{
const char* socketServerPort; /**< Socket server port */
const char* name; /**< Robot name */

const char* robotName; /**< Robot name */
bool verbose; /**< Show verbose information */
} PrgArguments;

#endif

/******************************************************************************
* Prototypes
*****************************************************************************/

extern void setup();
extern void loop();

#ifndef UNIT_TEST

static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char** argv);
static void showPrgArguments(const PrgArguments& prgArgs);

#endif

/******************************************************************************
* Local Variables
Expand Down Expand Up @@ -108,10 +118,14 @@ static const int MAX_TIME_STEP = 10;
*/
static SimTime* gSimTime = nullptr;

/**
* Default port used for socket communications.
*/
static const char* SOCKET_SERVER_DEFAULT_PORT = "65432";
/** Program argument default value of the robot name. */
static const char* PRG_ARG_ROBOT_NAME_DEFAULT = "";

/** Default port used for socket communications. */
static const char* PRG_ARG_SOCKET_SERVER_PORT_DEFAULT = "65432";

/** Program argument default value of the verbose flag. */
static bool PRG_ARG_VERBOSE_DEFAULT = false;

/**
* Maximum number of socket connections.
Expand Down Expand Up @@ -171,6 +185,8 @@ extern int main(int argc, char** argv)
Keyboard& keyboard = Board::getInstance().getKeyboard();
PrgArguments prgArguments;

printf("\n*** Radon Ulzer ***\n");

/* Remove any buffering from stout and stderr to get the printed information immediately. */
(void)setvbuf(stdout, NULL, _IONBF, 0);
(void)setvbuf(stderr, NULL, _IONBF, 0);
Expand All @@ -179,14 +195,24 @@ extern int main(int argc, char** argv)

if (0 == status)
{
/* Show used arguments only in verbose mode. */
if (true == prgArguments.verbose)
{
showPrgArguments(prgArguments);
}

/* Initialize the socket server. */
if (false == gSocketStream.init(prgArguments.socketServerPort, SOCKET_SERVER_MAX_CONNECTIONS))
{
printf("Error initializing SocketServer.\n");
status = -1;
}
else
{
printf("SocketServer ready on port %s.\n", prgArguments.socketServerPort);
if (true == prgArguments.verbose)
{
printf("SocketServer ready on port %s.\n", prgArguments.socketServerPort);
}

/* Get simulation time handler. It will be used by millis() and delay(). */
gSimTime = &Board::getInstance().getSimTime();
Expand Down Expand Up @@ -247,34 +273,10 @@ extern void delay(unsigned long ms)
}
}

#endif

/******************************************************************************
* Local Functions
*****************************************************************************/

#ifdef UNIT_TEST

/**
* Handle the Arguments passed to the programm.
*
* @param[in] argc Program argument count
* @param[in] argv Program argument vector
*
* @returns 0 if handling was succesful. Otherwise, -1
*/
static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char** argv)
{
/* Not implemented. */
(void)prgArguments;
(void)argc;
(void)argv;

return 0;
}

#else

/**
* Handle the arguments passed to the programm.
* If a argument is not given via command line interface, its default value will be used.
Expand All @@ -293,21 +295,24 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char
int option = getopt(argc, argv, availableOptions);

/* Set default values */
prgArguments.socketServerPort = SOCKET_SERVER_DEFAULT_PORT;
prgArguments.name = nullptr;
prgArguments.socketServerPort = PRG_ARG_SOCKET_SERVER_PORT_DEFAULT;
prgArguments.robotName = PRG_ARG_ROBOT_NAME_DEFAULT;
prgArguments.verbose = PRG_ARG_VERBOSE_DEFAULT;

while ((-1 != option) && (0 == status))
{
switch (option)
{
case 'p': /* Port */
printf("Using Socket Client in Port \"%s\".\n", optarg);
case 'n': /* Name */
prgArguments.robotName = optarg;
break;

case 'p': /* SocketServer Port */
prgArguments.socketServerPort = optarg;
break;

case 'n': /* Name */
printf("Instance has been named \"%s\".\n", optarg);
prgArguments.name = optarg;
case 'v': /* Verbose */
prgArguments.verbose = true;
break;

case '?': /* Unknown */
Expand All @@ -317,18 +322,36 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char
/* fallthrough */

default: /* Default */
printf("Usage: %s <option(s)>\nOptions:\n", programName);
printf("\t-h\t\t\tShow this help message.\n"); /* Help */
printf("\t-p <PORT NUMBER>\tSet SocketServer port.\n"); /* Port */
printf("\t-n <NAME>\t\tSet instace name."); /* Name */
status = -1;
break;
}

option = getopt(argc, argv, availableOptions);
}

/* Does the user need help? */
if (0 > status)
{
printf("Usage: %s <option(s)>\nOptions:\n", programName);
printf("\t-h\t\t\tShow this help message.\n"); /* Help */
printf("\t-n <NAME>\t\tSet robot name."); /* Robot Name */
printf("\t-p <PORT NUMBER>\tSet SocketServer port."); /* SocketServer Port */
printf(" Default: %s\n", PRG_ARG_SOCKET_SERVER_PORT_DEFAULT); /* SocketServer port default value*/
}

return status;
}

/**
* Show program arguments on the console.
*
* @param[in] prgArgs Program arguments
*/
static void showPrgArguments(const PrgArguments& prgArgs)
{
printf("Robot name : %s\n", prgArgs.robotName);
printf("SocketServer Port : %s\n", prgArgs.socketServerPort);
/* Skip verbose flag. */
}

#endif

0 comments on commit 4d19129

Please sign in to comment.