Skip to content

Commit

Permalink
Merge pull request #33 from BlueAndi/portAsString
Browse files Browse the repository at this point in the history
Changed port number from int to string.
  • Loading branch information
BlueAndi authored Nov 8, 2023
2 parents fb93720 + c1d051f commit 5565d2e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 67 deletions.
127 changes: 66 additions & 61 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
{
uint16_t socketServerPort; /**< Socket server port */
const char* name; /**< Robot name */

const char* socketServerPort; /**< Socket server port */
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 uint16_t SOCKET_SERVER_DEFAULT_PORT = 65432U;
/** 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 %d.\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,39 +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 */
{
/* 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;
}
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 @@ -335,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.\n"); /* 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
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 5565d2e

Please sign in to comment.