From 839987d1ee6ac0c8526361444e21db6c1e9c5761 Mon Sep 17 00:00:00 2001 From: gabryelreyes Date: Tue, 7 Nov 2023 17:04:50 +0100 Subject: [PATCH] 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);