diff options
author | Krzysztof Opasiak <k.opasiak@samsung.com> | 2017-03-03 17:05:20 +0100 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2017-03-03 17:48:44 +0100 |
commit | 651177558b7be6d91bf12420bfd8b2fd42727755 (patch) | |
tree | 85f497c5e8a8cc7759f81aded1bb1a20c3d29d7b | |
parent | a91dd9d037b17109e86225d9e7a81eed1cbd5f2d (diff) | |
download | libusbg-651177558b7be6d91bf12420bfd8b2fd42727755.tar.gz libusbg-651177558b7be6d91bf12420bfd8b2fd42727755.tar.bz2 libusbg-651177558b7be6d91bf12420bfd8b2fd42727755.zip |
libusbgx: Fix compilation without libconfig
After refactorization we lost ability to build libusbgx
without libconfig. Let's restore this.
Basic concept is that the whole code related to libconfig usage
should be under #ifdef or compiled only if libconfig support
is enabled.
To prevent future problems of that kind, let's create a header
which is included when compiling without libconfig. Inside it we
like:
struct s {
.import = usbg_get_config_node_int,
.export = usbg_set_config_node_int,
};
are still valid, but all tries to call this functions directly end up
in compilation error. This helps us identify all pieces of code
which should be placed under suitable #ifdef.
Based on initial pull request "Compilation fixes. #4" by:
Bent Bisballe Nyeng (github: aasimon) <deva@aasimon.org>
Reported-by: Bent Bisballe Nyeng (github: aasimon) <deva@aasimon.org>
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | include/usbg/usbg_internal.h | 46 | ||||
-rw-r--r-- | include/usbg/usbg_internal_libconfig.h | 54 | ||||
-rw-r--r-- | include/usbg/usbg_internal_none.h | 38 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/function/hid.c | 9 | ||||
-rw-r--r-- | src/usbg_common.c | 174 | ||||
-rw-r--r-- | src/usbg_common_libconfig.c | 189 |
7 files changed, 296 insertions, 216 deletions
diff --git a/include/usbg/usbg_internal.h b/include/usbg/usbg_internal.h index edfdccb..f0cca68 100644 --- a/include/usbg/usbg_internal.h +++ b/include/usbg/usbg_internal.h @@ -19,20 +19,15 @@ #include <malloc.h> #include <sys/types.h> #ifdef HAS_LIBCONFIG -#include <libconfig.h> +#include "usbg_internal_libconfig.h" +#else +#include "usbg_internal_none.h" #endif - #ifdef __cplusplus extern "C" { #endif -#ifndef HAS_LIBCONFIG - typedef struct _should_not_be_used config_t; - typedef struct _should_not_be_used config_setting_t; - void config_destroy(config_t *config); -#endif - /** * @file include/usbg/usbg_internal.h */ @@ -376,42 +371,11 @@ int usbg_get_dev(const char *path, const char *name, const char *attr, * above 0 when found suitable value */ typedef int (*usbg_import_node_func)(config_setting_t *root, - const char *node_name, void *val); + const char *node_name, void *val); /* return 0 on success, usbg_error otherwise */ typedef int (*usbg_export_node_func)(config_setting_t *root, - const char *node_name, void *val); - -int usbg_get_config_node_int(config_setting_t *root, - const char *node_name, void *val); - -int usbg_get_config_node_bool(config_setting_t *root, - const char *node_name, void *val); - -int usbg_get_config_node_string(config_setting_t *root, - const char *node_name, void *val); - -int usbg_get_config_node_ether_addr(config_setting_t *root, - const char *node_name, void *val); - -int usbg_set_config_node_int(config_setting_t *root, - const char *node_name, void *val); - -int usbg_set_config_node_int_hex(config_setting_t *root, - const char *node_name, void *val); - -int usbg_set_config_node_bool(config_setting_t *root, - const char *node_name, void *val); - -int usbg_set_config_node_string(config_setting_t *root, - const char *node_name, void *val); - -int usbg_set_config_node_ether_addr(config_setting_t *root, - const char *node_name, void *val); - -int usbg_set_config_node_dev(config_setting_t *root, - const char *node_name, void *val); - + const char *node_name, void *val); #ifdef __cplusplus } diff --git a/include/usbg/usbg_internal_libconfig.h b/include/usbg/usbg_internal_libconfig.h new file mode 100644 index 0000000..ac51758 --- /dev/null +++ b/include/usbg/usbg_internal_libconfig.h @@ -0,0 +1,54 @@ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +#ifndef USBG_INTERNAL_LIBCONFIG_H +#define USBG_INTERNAL_LIBCONFIG_H + +#include <libconfig.h> +#ifdef __cplusplus +extern "C" { +#endif + +int usbg_get_config_node_int(config_setting_t *root, + const char *node_name, void *val); + +int usbg_get_config_node_bool(config_setting_t *root, + const char *node_name, void *val); + +int usbg_get_config_node_string(config_setting_t *root, + const char *node_name, void *val); + +int usbg_get_config_node_ether_addr(config_setting_t *root, + const char *node_name, void *val); + +int usbg_set_config_node_int(config_setting_t *root, + const char *node_name, void *val); + +int usbg_set_config_node_int_hex(config_setting_t *root, + const char *node_name, void *val); + +int usbg_set_config_node_bool(config_setting_t *root, + const char *node_name, void *val); + +int usbg_set_config_node_string(config_setting_t *root, + const char *node_name, void *val); + +int usbg_set_config_node_ether_addr(config_setting_t *root, + const char *node_name, void *val); + +int usbg_set_config_node_dev(config_setting_t *root, + const char *node_name, void *val); + +#ifdef __cplusplus +} +#endif + +#endif /* USBG_INTERNAL_LIBCONFIG_H */ diff --git a/include/usbg/usbg_internal_none.h b/include/usbg/usbg_internal_none.h new file mode 100644 index 0000000..a4aed92 --- /dev/null +++ b/include/usbg/usbg_internal_none.h @@ -0,0 +1,38 @@ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +#ifndef USBG_INTERNAL_LIBCONFIG_H +#define USBG_INTERNAL_LIBCONFIG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define usbg_get_config_node_int NULL +#define usbg_get_config_node_bool NULL +#define usbg_get_config_node_string NULL +#define usbg_get_config_node_ether_addr NULL +#define usbg_set_config_node_int NULL +#define usbg_set_config_node_int_hex NULL +#define usbg_set_config_node_bool NULL +#define usbg_set_config_node_string NULL +#define usbg_set_config_node_ether_addr NULL +#define usbg_set_config_node_dev NULL + +typedef struct _should_not_be_used config_t; +typedef struct _should_not_be_used config_setting_t; +void config_destroy(config_t *config); + +#ifdef __cplusplus +} +#endif + +#endif /* USBG_INTERNAL_LIBCONFIG_H */ diff --git a/src/Makefile.am b/src/Makefile.am index 98aebcd..1302af0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = std-options subdir-objects lib_LTLIBRARIES = libusbgx.la libusbgx_la_SOURCES = usbg.c usbg_error.c usbg_common.c function/ether.c function/ffs.c function/midi.c function/ms.c function/phonet.c function/serial.c function/loopback.c function/hid.c if TEST_GADGET_SCHEMES -libusbgx_la_SOURCES += usbg_schemes_libconfig.c +libusbgx_la_SOURCES += usbg_schemes_libconfig.c usbg_common_libconfig.c else libusbgx_la_SOURCES += usbg_schemes_none.c endif diff --git a/src/function/hid.c b/src/function/hid.c index a60333a..43f4ac8 100644 --- a/src/function/hid.c +++ b/src/function/hid.c @@ -44,6 +44,7 @@ struct usbg_f_hid { .export = usbg_set_config_node_dev, \ } +#ifdef HAS_LIBCONFIG static int hid_get_report(const char *path, const char *name, const char *attr, void *val) { @@ -159,6 +160,14 @@ static int hid_set_config_node_report(config_setting_t *root, return 0; } +#else + +#define hid_get_report NULL +#define hid_set_report NULL +#define hid_get_config_node_report NULL +#define hid_set_config_node_report NULL + +#endif #define HID_RD_ATTR(_name) \ { \ diff --git a/src/usbg_common.c b/src/usbg_common.c index d3040c8..de9e40d 100644 --- a/src/usbg_common.c +++ b/src/usbg_common.c @@ -359,180 +359,6 @@ int usbg_get_dev(const char *path, const char *name, const char *attr, return 0; } -int usbg_get_config_node_int(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - - node = config_setting_get_member(root, node_name); - if (!node) - return 0; - - if (!usbg_config_is_int(node)) - return USBG_ERROR_INVALID_TYPE; - - *(int *)val = config_setting_get_int(node); - - return 1; -} - -int usbg_get_config_node_bool(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - bool *value = val; - int ret; - - node = config_setting_get_member(root, node_name); - if (!node) - return 0; - - ret = config_setting_type(node); - switch (ret) { - case CONFIG_TYPE_INT: - *value = !!config_setting_get_int(node); - break; - case CONFIG_TYPE_BOOL: - *value = config_setting_get_bool(node); - break; - default: - return USBG_ERROR_INVALID_TYPE; - } - - return 0; -} - -int usbg_get_config_node_string(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - - node = config_setting_get_member(root, node_name); - if (!node) - return 0; - - if (!usbg_config_is_string(node)) - return USBG_ERROR_INVALID_TYPE; - - *(const char **)val = config_setting_get_string(node); - - return 1; -} - -int usbg_get_config_node_ether_addr(config_setting_t *root, - const char *node_name, void *val) -{ - const char *str_addr; - struct ether_addr *addr; - int ret; - - ret = usbg_get_config_node_string(root, node_name, &str_addr); - /* if not found or error */ - if (ret == 0 || ret < 0) - return ret; - - addr = ether_aton_r(str_addr, val); - - return addr ? 1 : USBG_ERROR_INVALID_VALUE; -} - -int usbg_set_config_node_int(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - int ret = 0; - - node = config_setting_add(root, node_name, CONFIG_TYPE_INT); - if (!node) - return USBG_ERROR_NO_MEM; - - ret = config_setting_set_int(node, *(int *)val); - - return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; -} - -int usbg_set_config_node_int_hex(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - int ret = 0; - - node = config_setting_add(root, node_name, CONFIG_TYPE_INT); - if (!node) - return USBG_ERROR_NO_MEM; - - ret = config_setting_set_format(node, CONFIG_FORMAT_HEX); - if (ret != CONFIG_TRUE) - return USBG_ERROR_OTHER_ERROR; - - ret = config_setting_set_int(node, *(int *)val); - - return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; -} - -int usbg_set_config_node_bool(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - int ret = 0; - - node = config_setting_add(root, node_name, CONFIG_TYPE_BOOL); - if (!node) - return USBG_ERROR_NO_MEM; - - ret = config_setting_set_bool(node, *(bool *)val); - - return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; -} - -int usbg_set_config_node_string(config_setting_t *root, - const char *node_name, void *val) -{ - config_setting_t *node; - int ret = 0; - - node = config_setting_add(root, node_name, CONFIG_TYPE_STRING); - if (!node) - return USBG_ERROR_NO_MEM; - - ret = config_setting_set_string(node, *(char **)val); - - return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; -} - -int usbg_set_config_node_ether_addr(config_setting_t *root, - const char *node_name, void *val) -{ - char str_addr[USBG_MAX_STR_LENGTH]; - char *ptr = str_addr; - - usbg_ether_ntoa_r(val, str_addr); - return usbg_set_config_node_string(root, node_name, &ptr); -} - -int usbg_set_config_node_dev(config_setting_t *root, - const char *node_name, void *val) -{ - dev_t *dev = (dev_t *)val; - config_setting_t *node; - int tmp; - int ret = 0; - - node = config_setting_add(root, node_name, CONFIG_TYPE_GROUP); - if (!node) - return USBG_ERROR_NO_MEM; - - tmp = major(*dev); - ret = usbg_set_config_node_int(node, "major", &tmp); - if (ret) - return ret; - - tmp = minor(*dev); - ret = usbg_set_config_node_int(node, "minor", &tmp); - - return ret; -} - void usbg_cleanup_function(struct usbg_function *f) { free(f->path); diff --git a/src/usbg_common_libconfig.c b/src/usbg_common_libconfig.c new file mode 100644 index 0000000..d2576d7 --- /dev/null +++ b/src/usbg_common_libconfig.c @@ -0,0 +1,189 @@ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +#include "usbg/usbg.h" +#include "usbg/usbg_internal.h" +#include "usbg/usbg_internal_libconfig.h" + +int usbg_get_config_node_int(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + + node = config_setting_get_member(root, node_name); + if (!node) + return 0; + + if (!usbg_config_is_int(node)) + return USBG_ERROR_INVALID_TYPE; + + *(int *)val = config_setting_get_int(node); + + return 1; +} + +int usbg_get_config_node_bool(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + bool *value = val; + int ret; + + node = config_setting_get_member(root, node_name); + if (!node) + return 0; + + ret = config_setting_type(node); + switch (ret) { + case CONFIG_TYPE_INT: + *value = !!config_setting_get_int(node); + break; + case CONFIG_TYPE_BOOL: + *value = config_setting_get_bool(node); + break; + default: + return USBG_ERROR_INVALID_TYPE; + } + + return 0; +} + +int usbg_get_config_node_string(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + + node = config_setting_get_member(root, node_name); + if (!node) + return 0; + + if (!usbg_config_is_string(node)) + return USBG_ERROR_INVALID_TYPE; + + *(const char **)val = config_setting_get_string(node); + + return 1; +} + +int usbg_get_config_node_ether_addr(config_setting_t *root, + const char *node_name, void *val) +{ + const char *str_addr; + struct ether_addr *addr; + int ret; + + ret = usbg_get_config_node_string(root, node_name, &str_addr); + /* if not found or error */ + if (ret == 0 || ret < 0) + return ret; + + addr = ether_aton_r(str_addr, val); + + return addr ? 1 : USBG_ERROR_INVALID_VALUE; +} + +int usbg_set_config_node_int(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + int ret = 0; + + node = config_setting_add(root, node_name, CONFIG_TYPE_INT); + if (!node) + return USBG_ERROR_NO_MEM; + + ret = config_setting_set_int(node, *(int *)val); + + return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; +} + +int usbg_set_config_node_int_hex(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + int ret = 0; + + node = config_setting_add(root, node_name, CONFIG_TYPE_INT); + if (!node) + return USBG_ERROR_NO_MEM; + + ret = config_setting_set_format(node, CONFIG_FORMAT_HEX); + if (ret != CONFIG_TRUE) + return USBG_ERROR_OTHER_ERROR; + + ret = config_setting_set_int(node, *(int *)val); + + return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; +} + +int usbg_set_config_node_bool(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + int ret = 0; + + node = config_setting_add(root, node_name, CONFIG_TYPE_BOOL); + if (!node) + return USBG_ERROR_NO_MEM; + + ret = config_setting_set_bool(node, *(bool *)val); + + return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; +} + +int usbg_set_config_node_string(config_setting_t *root, + const char *node_name, void *val) +{ + config_setting_t *node; + int ret = 0; + + node = config_setting_add(root, node_name, CONFIG_TYPE_STRING); + if (!node) + return USBG_ERROR_NO_MEM; + + ret = config_setting_set_string(node, *(char **)val); + + return ret == CONFIG_TRUE ? 0 : USBG_ERROR_OTHER_ERROR; +} + +int usbg_set_config_node_ether_addr(config_setting_t *root, + const char *node_name, void *val) +{ + char str_addr[USBG_MAX_STR_LENGTH]; + char *ptr = str_addr; + + usbg_ether_ntoa_r(val, str_addr); + return usbg_set_config_node_string(root, node_name, &ptr); +} + +int usbg_set_config_node_dev(config_setting_t *root, + const char *node_name, void *val) +{ + dev_t *dev = (dev_t *)val; + config_setting_t *node; + int tmp; + int ret = 0; + + node = config_setting_add(root, node_name, CONFIG_TYPE_GROUP); + if (!node) + return USBG_ERROR_NO_MEM; + + tmp = major(*dev); + ret = usbg_set_config_node_int(node, "major", &tmp); + if (ret) + return ret; + + tmp = minor(*dev); + ret = usbg_set_config_node_int(node, "minor", &tmp); + + return ret; +} + |