Skip to content

Commit

Permalink
Changed port number from int to string.
Browse files Browse the repository at this point in the history
Comes from command line as string.
Needed in Winsock as string.
Some formatting changes come from auto-format
  • Loading branch information
gabryelreyes committed Nov 7, 2023
1 parent fb93720 commit 839987d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
28 changes: 5 additions & 23 deletions lib/ArduinoNative/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/ArduinoNative/SocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h> /* definition of close */
#include <cstring> /* definition of memset for tests. */
#include <cstring> /* definition of memset for tests. */
#endif

/******************************************************************************
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions lib/ArduinoNative/SocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 839987d

Please sign in to comment.