From 7cea87ba2dd316cd6a31464db645e74fceba804a Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 8 Nov 2023 14:25:41 +0100 Subject: [PATCH 1/2] Program arguments refactoring. Added verbose option. Default values are defined and displayed on help text. Organized comments. --- lib/ArduinoNative/Arduino.cpp | 83 +++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/lib/ArduinoNative/Arduino.cpp b/lib/ArduinoNative/Arduino.cpp index 4e682d2b..3d37824d 100644 --- a/lib/ArduinoNative/Arduino.cpp +++ b/lib/ArduinoNative/Arduino.cpp @@ -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 @@ -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. @@ -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); @@ -179,6 +195,13 @@ 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"); @@ -186,7 +209,10 @@ extern int main(int argc, char** argv) } 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(); @@ -293,21 +319,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 */ @@ -317,10 +346,6 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char /* fallthrough */ default: /* Default */ - printf("Usage: %s \nOptions:\n", programName); - printf("\t-h\t\t\tShow this help message.\n"); /* Help */ - printf("\t-p \tSet SocketServer port.\n"); /* Port */ - printf("\t-n \t\tSet instace name."); /* Name */ status = -1; break; } @@ -328,7 +353,29 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char option = getopt(argc, argv, availableOptions); } + /* Does the user need help? */ + if (0 > status) + { + printf("Usage: %s \nOptions:\n", programName); + printf("\t-h\t\t\tShow this help message.\n"); /* Help */ + printf("\t-n \t\tSet robot name."); /* Robot Name */ + printf("\t-p \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 \ No newline at end of file From 0a3f465a1c12dc7a838c03203051191851fb6a0e Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 8 Nov 2023 14:34:34 +0100 Subject: [PATCH 2/2] Fixed tests. handleCommandLineArguments not define in Test environment --- lib/ArduinoNative/Arduino.cpp | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/lib/ArduinoNative/Arduino.cpp b/lib/ArduinoNative/Arduino.cpp index 3d37824d..5f7d9dfd 100644 --- a/lib/ArduinoNative/Arduino.cpp +++ b/lib/ArduinoNative/Arduino.cpp @@ -273,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.