Skip to content

Commit

Permalink
added testing and argument/config file/env parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
strangesast committed Nov 5, 2020
1 parent 595ed65 commit b0ff8bf
Show file tree
Hide file tree
Showing 13 changed files with 317 additions and 82 deletions.
14 changes: 7 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
version: '3.7'
services:
base:
image: fwlib
fwlib:
image: strangesast/fwlib
build:
context: .
dockerfile: i386.Dockerfile
fwlib:
image: fwlib-test
dockerfile: Dockerfile
example:
image: strangesast/fwlib-example
network_mode: host
build:
context: .
dockerfile: test.Dockerfile
dockerfile: example/Dockerfile
depends_on:
- base
- fwlib
12 changes: 7 additions & 5 deletions example/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
add_executable(fanuc_example main.c)

#add_library(fwlib32 SHARED IMPORTED)
# include header file (fwlib32.h) directory
target_include_directories(fanuc_example PRIVATE ${CMAKE_SOURCE_DIR}/../)
# also requires renaming / linking libfwlib32-$platform-$arch.so.$version to libfwlib32.so at runtime

# the following lines are required to run demo without registering fwlib with dd
# but may require that libfwlib32*.so is in the same location / same filesystem when
# run. when enabled, this breaks the docker build but it's useful for development.
# this also requires renaming / linking libfwlib32-$platform-$arch.so.$version (system
# dependent) to libfwlib32.so at runtime
#add_library(fwlib32 SHARED IMPORTED)
#set_target_properties(fwlib32 PROPERTIES
# IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../libfwlib32-linux-x86.so.1.0.5
#)
#set(CMAKE_INSTALL_RPATH "${CMAKE_SOURCE_DIR}/../libfwlib32-linux-x86.so.1.0.5")

message(STATUS "${CMAKE_SYSTEM_PROCESSOR}")

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
else()
set_target_properties(fanuc_example PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
endif()


target_link_libraries(fanuc_example m fwlib32 pthread)
target_link_libraries(fanuc_example m fwlib32 pthread config)
98 changes: 73 additions & 25 deletions example/src/config.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,93 @@
#include <libconfig.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./config.h"

extern const char *deviceIP;
extern int devicePort;
const char DEFAULT_IP[] = "127.0.0.1";

int read_config(const char *cfg_file) {
static struct option options[] = {{"ip", optional_argument, NULL, 'h'},
{"port", optional_argument, NULL, 'p'},
{"config", optional_argument, NULL, 'c'},
{NULL, 0, NULL, 0}};

int set_config_defaults(struct config *conf) {
strcpy(conf->ip, "127.0.0.1");
conf->port = 8193;

return 0;
}

int read_arg_config(int argc, char *argv[], struct config *conf) {
int c;
int i = 0;
int tmp;

while ((c = getopt_long(argc, argv, "hp", options, &i)) != -1) {
switch (c) {
case 'h':
strcpy(conf->ip, optarg);
break;

case 'p':
if ((tmp = atoi(optarg)) != 0) {
conf->port = tmp;
}
break;
case 'c':
if (read_file_config(optarg, conf)) {
return 1;
}
break;

case '?':
/* getopt_long already printed an error message. */
break;
}
}

return 0;
}

int read_env_config(struct config *conf) {
int iTmp;
char *pTmp;
bool config_avail = true;

if ((pTmp = getenv("DEVICE_IP")) != NULL) {
strcpy(conf->ip, pTmp);
}

if (((pTmp = getenv("DEVICE_PORT")) != NULL) && (iTmp = atoi(pTmp)) > 0) {
conf->port = iTmp;
}

return 0;
}

int read_file_config(const char *cfg_file, struct config *conf) {
config_t cfg;

config_init(&cfg);
const char *tmp;

if (!config_read_file(&cfg, cfg_file)) {
config_avail = false;
if (config_read_file(&cfg, cfg_file) != CONFIG_TRUE) {
fprintf(stderr, "unable to read config file \"%s\"\n", cfg_file);
config_destroy(&cfg);
return 1;
}

if ((pTmp = getenv("DEVICE_IP")) != NULL) {
deviceIP = pTmp;
} else if (config_avail && config_lookup_string(&cfg, "ip", &deviceIP)) {
// set by libconfig
} else {
// use default
deviceIP = DEFAULT_IP;
}

if (((pTmp = getenv("DEVICE_PORT")) != NULL) && (iTmp = atoi(pTmp)) > 0) {
devicePort = iTmp;
} else if (config_avail && config_lookup_int(&cfg, "port", &devicePort)) {
// set by libconfig
} else {
// use default
devicePort = 8193;
if (config_lookup_string(&cfg, "ip", &tmp) == CONFIG_TRUE) {
strcpy(conf->ip, tmp);
}
config_lookup_int(&cfg, "port", &conf->port);

config_destroy(&cfg);

return 0;
}

int read_config(int argc, char *argv[], struct config *conf) {
set_config_defaults(conf);
read_env_config(conf);
read_arg_config(argc, argv, conf);

return 0;
}
17 changes: 17 additions & 0 deletions example/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <getopt.h>
#include <libconfig.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct config {
char ip[100];
int port;
} Config;

int set_config_defaults(struct config *conf);
int read_config(int argc, char *argv[], struct config *conf);
int read_arg_config(int argc, char *argv[], struct config *conf);
int read_env_config(struct config *conf);
int read_file_config(const char *cfg_file, struct config *conf);
30 changes: 11 additions & 19 deletions example/src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "./config.c"
#include "fwlib32.h"

unsigned short libh;
Expand All @@ -12,26 +14,16 @@ void cleanup() {
}

int main(int argc, char *argv[]) {
char ip[100] = "127.0.0.1";
int port = 8193;
unsigned long cncIDs[4];
char cncID[36];
Config conf;

if (argc > 1) {
if (strlen(argv[1]) >= sizeof(ip)) {
printf("ip too long: %s\n", argv[1]);
exit(EXIT_FAILURE);
return 1;
}
strcpy(ip, argv[1]);
}

if (argc > 2) {
int tmp;
tmp = atoi(argv[2]);
if (tmp > 0 && tmp < 65535) {
port = tmp;
}
if (read_config(argc, argv, &conf)) {
fprintf(
stderr,
"%s --config=<path_to_config> --port=<device port> --ip=<device ip>",
argv[0]);
return 1;
}

if (cnc_startupprocess(0, "focas.log") != EW_OK) {
Expand All @@ -40,8 +32,8 @@ int main(int argc, char *argv[]) {
return 1;
}

printf("connecting to machine at %s:%d...\n", ip, port);
if (cnc_allclibhndl3(ip, port, 10, &libh) != EW_OK) {
printf("connecting to machine at %s:%d...\n", conf.ip, conf.port);
if (cnc_allclibhndl3(conf.ip, conf.port, 10, &libh) != EW_OK) {
fprintf(stderr, "Failed to connect to cnc!\n");
exit(EXIT_FAILURE);
return 1;
Expand Down
12 changes: 9 additions & 3 deletions example/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@

macro(package_add_test TESTNAME FILES LIBRARIES)
add_executable(${TESTNAME} ${FILES})

add_test(NAME ${TESTNAME} COMMAND ${TESTNAME})
target_link_libraries(${TESTNAME} ${LIBRARIES})

add_test(NAME ${TESTNAME} COMMAND ${TESTNAME} WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/test")
if (NOT "${LIBRARIES}" STREQUAL "")
target_link_libraries(${TESTNAME} ${LIBRARIES})
endif()
endmacro()

package_add_test(test_defaults_config test_defaults_config.c config)
package_add_test(test_file_config test_file_config.c config)
package_add_test(test_env_config test_env_config.c config)
package_add_test(test_arg_config test_arg_config.c config)
package_add_test(test_config test_config.c config)
41 changes: 41 additions & 0 deletions example/test/test_arg_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>

#include "../src/config.c"

int add(int a, int b) { return a + b; }

int main(int argc, char *argv[]) {
char *test_args[] = {"test_arg_config", "--port=8193", "--ip=127.0.0.1"};
int ret;
Config conf = {.ip = "0.0.0.0", .port = 1234};

if (strcmp(conf.ip, "0.0.0.0") != 0) {
fprintf(stderr, "unexpected null value for deviceIP\n");
exit(1);
}

if (conf.port != 1234) {
fprintf(stderr, "unexpected null value for devicePort\n");
exit(1);
}

ret = read_arg_config(3, test_args, &conf);
if (ret != 0) {
fprintf(stderr, "read_arg_config failed!\n");
exit(1);
}

if (strcmp(conf.ip, "127.0.0.1") != 0) {
fprintf(stderr, "unexpected device ip \"%s\"\n", conf.ip);
exit(1);
}

if (conf.port != 8193) {
fprintf(stderr, "unexpected device port \"%d\"\n", conf.port);
exit(1);
}

exit(0);
}
33 changes: 11 additions & 22 deletions example/test/test_config.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
#include <libconfig.h>
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>

#include "../src/config.c"

int devicePort;
const char *deviceIP = NULL;

int add(int a, int b) { return a + b; }

int main(int argc, char *argv[]) {
char *test_args[] = {"test_arg_config", "--ip=123.456.789.1",
"--config=test_config.cfg"};
int ret;
char config_file[] = "test_config.cfg";
Config conf;

if (deviceIP != NULL) {
fprintf(stderr, "unexpected null value for deviceIP\n");
exit(1);
}

if (devicePort != 0) {
fprintf(stderr, "unexpected null value for devicePort\n");
exit(1);
}

ret = read_config(config_file);
// setenv("", "", 1);
ret = read_config(2, test_args, &conf);
if (ret != 0) {
fprintf(stderr, "read_config failed!\n");
exit(1);
}

if (strcmp(deviceIP, "127.0.0.1") != 0) {
fprintf(stderr, "unexpected device ip\n");
if (strcmp(conf.ip, "123.456.789.1") != 0) {
fprintf(stderr, "unexpected null value for deviceIP\n");
exit(1);
}

if (devicePort != 8193) {
fprintf(stderr, "unexpected device port");
if (conf.port != 8193) {
fprintf(stderr, "unexpected null value for devicePort\n");
exit(1);
}

exit(0);
Expand Down
7 changes: 7 additions & 0 deletions example/test/test_config.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ip = "127.0.0.1"
port = 8193
quote = "Criticism may not be agreeable, but it is necessary."
" It fulfils the same function as pain in the human"
" body. It calls attention to an unhealthy state of"
" things.\n"
"\t--Winston Churchill";
Loading

0 comments on commit b0ff8bf

Please sign in to comment.