From 839987d1ee6ac0c8526361444e21db6c1e9c5761 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Tue, 7 Nov 2023 17:04:50 +0100 Subject: [PATCH 1/5] Changed port number from int to string. Comes from command line as string. Needed in Winsock as string. Some formatting changes come from auto-format --- lib/ArduinoNative/Arduino.cpp | 28 +++++----------------------- lib/ArduinoNative/SocketServer.cpp | 6 +++--- lib/ArduinoNative/SocketServer.h | 5 ++--- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/lib/ArduinoNative/Arduino.cpp b/lib/ArduinoNative/Arduino.cpp index f692fb07..4e682d2b 100644 --- a/lib/ArduinoNative/Arduino.cpp +++ b/lib/ArduinoNative/Arduino.cpp @@ -64,7 +64,7 @@ /** This type defines the possible program arguments. */ typedef struct { - uint16_t socketServerPort; /**< Socket server port */ + const char* socketServerPort; /**< Socket server port */ const char* name; /**< Robot name */ } PrgArguments; @@ -111,7 +111,7 @@ static SimTime* gSimTime = nullptr; /** * Default port used for socket communications. */ -static const uint16_t SOCKET_SERVER_DEFAULT_PORT = 65432U; +static const char* SOCKET_SERVER_DEFAULT_PORT = "65432"; /** * Maximum number of socket connections. @@ -186,7 +186,7 @@ extern int main(int argc, char** argv) } else { - printf("SocketServer ready on port %d.\n", prgArguments.socketServerPort); + 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(); @@ -301,27 +301,9 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char switch (option) { case 'p': /* Port */ - { - /* Parse Port Number */ - char* p; /* End Pointer*/ - errno = 0; /* Reset Error Register */ - long parsedValue = strtol(optarg, &p, 10); /* Long value parsed from string. */ - - if (('\0' == *p) && /* Make sure the string is completely read. */ - (0 == errno) && /* No Errors were produced. */ - (UINT16_MAX >= parsedValue) && /* No overflow of uint16_t to allow direct casting. */ - (0U <= parsedValue)) /* No negative values. */ - { - prgArguments.socketServerPort = parsedValue; - } - else - { - printf("Error parsing port argument.\n"); - status = -1; - } - + printf("Using Socket Client in Port \"%s\".\n", optarg); + prgArguments.socketServerPort = optarg; break; - } case 'n': /* Name */ printf("Instance has been named \"%s\".\n", optarg); diff --git a/lib/ArduinoNative/SocketServer.cpp b/lib/ArduinoNative/SocketServer.cpp index 584b0d52..833b3ef1 100644 --- a/lib/ArduinoNative/SocketServer.cpp +++ b/lib/ArduinoNative/SocketServer.cpp @@ -52,7 +52,7 @@ #include #include #include /* definition of close */ -#include /* definition of memset for tests. */ +#include /* definition of memset for tests. */ #endif /****************************************************************************** @@ -126,7 +126,7 @@ SocketServer::~SocketServer() } } -bool SocketServer::init(uint16_t port, uint8_t maxConnections) +bool SocketServer::init(const char* port, uint8_t maxConnections) { int result; struct addrinfo hints; @@ -158,7 +158,7 @@ bool SocketServer::init(uint16_t port, uint8_t maxConnections) #endif /* Resolve the server address and port */ - result = getaddrinfo(nullptr, std::to_string(port).c_str(), &hints, &addrInfo); + result = getaddrinfo(nullptr, port, &hints, &addrInfo); if (0 != result) { printf("getaddrinfo failed with error: %d\n", result); diff --git a/lib/ArduinoNative/SocketServer.h b/lib/ArduinoNative/SocketServer.h index 109620d8..e507c6b1 100644 --- a/lib/ArduinoNative/SocketServer.h +++ b/lib/ArduinoNative/SocketServer.h @@ -68,7 +68,7 @@ class SocketServer : public Stream * @param[in] maxConnections Number of maxConnections allowed. * @returns true if server has been succesfully set-up. */ - bool init(uint16_t port, uint8_t maxConnections); + bool init(const char* port, uint8_t maxConnections); /** * Print argument to the Output Stream. @@ -189,12 +189,11 @@ class SocketServer : public Stream void process(); private: - /** Struct for Implementation of PIMPL Idiom. */ struct SocketServerImpl; /** SocketServer Members. PIMPL Idiom. */ - SocketServerImpl *m_members; + SocketServerImpl* m_members; /* Not allowed. */ SocketServer(const SocketServer& srv); From 7cea87ba2dd316cd6a31464db645e74fceba804a Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 8 Nov 2023 14:25:41 +0100 Subject: [PATCH 2/5] 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 3/5] 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. From 917c24312eae207ba124ca880f890ff85cb91d01 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 8 Nov 2023 21:21:54 +0100 Subject: [PATCH 4/5] Fixed text printed to console --- lib/ArduinoNative/Arduino.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ArduinoNative/Arduino.cpp b/lib/ArduinoNative/Arduino.cpp index 5f7d9dfd..236a4b95 100644 --- a/lib/ArduinoNative/Arduino.cpp +++ b/lib/ArduinoNative/Arduino.cpp @@ -334,8 +334,8 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char { 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("\t-n \t\tSet robot name.\n"); /* Robot Name */ + printf("\t-p \tSet SocketServer port.\n"); /* SocketServer Port */ printf(" Default: %s\n", PRG_ARG_SOCKET_SERVER_PORT_DEFAULT); /* SocketServer port default value*/ } @@ -349,8 +349,8 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char */ static void showPrgArguments(const PrgArguments& prgArgs) { - printf("Robot name : %s\n", prgArgs.robotName); - printf("SocketServer Port : %s\n", prgArgs.socketServerPort); + printf("Robot name : %s\n", prgArgs.robotName); + printf("SocketServer Port: %s\n", prgArgs.socketServerPort); /* Skip verbose flag. */ } From c1d051f299ebf48f8c386883e08df2751cfb3a88 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Wed, 8 Nov 2023 21:41:01 +0100 Subject: [PATCH 5/5] Removed extra carriage return --- lib/ArduinoNative/Arduino.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ArduinoNative/Arduino.cpp b/lib/ArduinoNative/Arduino.cpp index 236a4b95..d7a6f09d 100644 --- a/lib/ArduinoNative/Arduino.cpp +++ b/lib/ArduinoNative/Arduino.cpp @@ -335,7 +335,7 @@ static int handleCommandLineArguments(PrgArguments& prgArguments, int argc, char printf("Usage: %s \nOptions:\n", programName); printf("\t-h\t\t\tShow this help message.\n"); /* Help */ printf("\t-n \t\tSet robot name.\n"); /* Robot Name */ - printf("\t-p \tSet SocketServer port.\n"); /* SocketServer Port */ + printf("\t-p \tSet SocketServer port."); /* SocketServer Port */ printf(" Default: %s\n", PRG_ARG_SOCKET_SERVER_PORT_DEFAULT); /* SocketServer port default value*/ }