From e4eca848c8d95f48652096284fd8e2752ed1a316 Mon Sep 17 00:00:00 2001 From: michele papalini Date: Thu, 9 Jan 2020 14:55:32 +0100 Subject: [HICN-469] execute hicn-light-control from remote Signed-off-by: michele papalini Change-Id: I2a23a5f7648614213b83e2409e2ff3784b083758 Signed-off-by: michele papalini --- .../controller/hicnLightControl_main.c | 53 +++++++++++++++------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c') diff --git a/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c b/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c index 739326669..f27eb3ba5 100644 --- a/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c +++ b/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c @@ -14,6 +14,7 @@ */ #include +#include #ifndef _WIN32 #include @@ -48,6 +49,8 @@ #include +#include + size_t commandOutputLen = 0; // preserve the number of structs composing // payload in case on not interactive call. @@ -155,12 +158,12 @@ static void _displayUsage(char *programName) { printf("\n"); } -static int _parseArgs(int argc, char *argv[], char **keystorePath, - char **keystorePassword, PARCList *commandList) { +static int _parseArgs(int argc, char *argv[], char **server_ip, + uint16_t *server_port, PARCList *commandList){ static struct option longFormOptions[] = { {"help", no_argument, 0, 'h'}, - {"keystore", required_argument, 0, 'k'}, - {"password", required_argument, 0, 'p'}, + {"server", required_argument, 0, 'S'}, + {"port", required_argument, 0, 'P'}, {0, 0, 0, 0}}; int c; @@ -169,7 +172,7 @@ static int _parseArgs(int argc, char *argv[], char **keystorePath, // getopt_long stores the option index here. int optionIndex = 0; - c = getopt_long(argc, argv, "hk:p:", longFormOptions, &optionIndex); + c = getopt_long(argc, argv, "hS:P:", longFormOptions, &optionIndex); // Detect the end of the options. if (c == -1) { @@ -177,22 +180,33 @@ static int _parseArgs(int argc, char *argv[], char **keystorePath, } switch (c) { - case 'k': - *keystorePath = optarg; - break; - - case 'p': - *keystorePassword = optarg; - break; - + case 'S': + { + *server_ip = optarg; + struct sockaddr_in sa; + int result = inet_pton(AF_INET, *server_ip, &(sa.sin_addr)); + //inet_pton() returns 1 on success + if(result != 1){ + return 0; + } + break; + } + case 'P': + { + char * port_str = optarg; + if(utils_IsNumber(port_str)){ + *server_port = (uint16_t) strtol(port_str, NULL, 10); + } else { + return 0; + } + break; + } case 'h': default: - _displayUsage(argv[0]); return 0; } } - // Any remaining parameters get put in the command list. if (optind < argc) { while (optind < argc) { parcList_Add(commandList, argv[optind]); @@ -311,14 +325,19 @@ int main(int argc, char *argv[]) { PARCList *commands = parcList(parcArrayList_Create(NULL), PARCArrayListAsPARCList); - if (!_parseArgs(argc, argv, NULL, NULL, commands)) { + char *server_ip = SRV_CTRL_IP; + uint16_t server_port = SRV_CTRL_PORT; + if (!_parseArgs(argc, argv, &server_ip, + &server_port, commands)) { + _displayUsage(argv[0]); parcList_Release(&commands); exit(EXIT_FAILURE); } ControlMainState mainState; mainState.controlState = - controlState_Create(&mainState, _writeAndReadMessage, true); + controlState_Create(&mainState, _writeAndReadMessage, true, + server_ip, server_port); controlState_RegisterCommand(mainState.controlState, controlRoot_HelpCreate(mainState.controlState)); -- cgit 1.2.3-korg