-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added testing and argument/config file/env parsing
- Loading branch information
1 parent
595ed65
commit b0ff8bf
Showing
13 changed files
with
317 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Oops, something went wrong.