From: Krzysztof Opasiak Date: Wed, 29 Oct 2014 14:26:12 +0000 (+0100) Subject: libusbgx: Extract internal header file X-Git-Tag: libusbgx-v0.1.0~144 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c2f0e176395cd15b745de5fc0727206bb36c5d78;p=platform%2Fupstream%2Flibusbg.git libusbgx: Extract internal header file Signed-off-by: Krzysztof Opasiak --- diff --git a/include/usbg/usbg_internal.h b/include/usbg/usbg_internal.h new file mode 100644 index 0000000..4c0f449 --- /dev/null +++ b/include/usbg/usbg_internal.h @@ -0,0 +1,130 @@ +/* + * 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 +#include +#include +#include + +struct usbg_state +{ + char *path; + + TAILQ_HEAD(ghead, usbg_gadget) gadgets; + TAILQ_HEAD(uhead, usbg_udc) udcs; + config_t *last_failed_import; +}; + +struct usbg_gadget +{ + char *name; + char *path; + + TAILQ_ENTRY(usbg_gadget) gnode; + TAILQ_HEAD(chead, usbg_config) configs; + TAILQ_HEAD(fhead, usbg_function) functions; + usbg_state *parent; + config_t *last_failed_import; + usbg_udc *udc; +}; + +struct usbg_config +{ + TAILQ_ENTRY(usbg_config) cnode; + TAILQ_HEAD(bhead, usbg_binding) bindings; + usbg_gadget *parent; + + char *name; + char *path; + char *label; + int id; +}; + +struct usbg_function +{ + TAILQ_ENTRY(usbg_function) fnode; + usbg_gadget *parent; + + char *name; + char *path; + char *instance; + /* Only for internal library usage */ + char *label; + usbg_function_type type; +}; + +struct usbg_binding +{ + TAILQ_ENTRY(usbg_binding) bnode; + usbg_config *parent; + usbg_function *target; + + char *name; + char *path; +}; + +struct usbg_udc +{ + TAILQ_ENTRY(usbg_udc) unode; + usbg_state *parent; + usbg_gadget *gadget; + + char *name; +}; + +#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array)) + +#define ARRAY_SIZE_SENTINEL(array, size) \ + static void __attribute__ ((unused)) array##_size_sentinel() \ + { \ + char array##_smaller_than_expected[ \ + (int)(ARRAY_SIZE(array) - size)] \ + __attribute__ ((unused)); \ + \ + char array##_larger_than_expected[ \ + (int)(size - ARRAY_SIZE(array))] \ + __attribute__ ((unused)); \ + } + +#define ERROR(msg, ...) do {\ + fprintf(stderr, "%s() "msg" \n", \ + __func__, ##__VA_ARGS__);\ + fflush(stderr);\ + } while (0) + +#define ERRORNO(msg, ...) do {\ + fprintf(stderr, "%s() %s: "msg" \n", \ + __func__, strerror(errno), ##__VA_ARGS__);\ + fflush(stderr);\ + } while (0) + +/* Insert in string order */ +#define INSERT_TAILQ_STRING_ORDER(HeadPtr, HeadType, NameField, ToInsert, NodeField) \ + do { \ + if (TAILQ_EMPTY((HeadPtr)) || \ + (strcmp((ToInsert)->NameField, TAILQ_FIRST((HeadPtr))->NameField) < 0)) \ + TAILQ_INSERT_HEAD((HeadPtr), (ToInsert), NodeField); \ + else if (strcmp((ToInsert)->NameField, TAILQ_LAST((HeadPtr), HeadType)->NameField) > 0) \ + TAILQ_INSERT_TAIL((HeadPtr), (ToInsert), NodeField); \ + else { \ + typeof(ToInsert) _cur; \ + TAILQ_FOREACH(_cur, (HeadPtr), NodeField) { \ + if (strcmp((ToInsert)->NameField, _cur->NameField) > 0) \ + continue; \ + TAILQ_INSERT_BEFORE(_cur, (ToInsert), NodeField); \ + } \ + } \ + } while (0) + +#define STRINGS_DIR "strings" +#define CONFIGS_DIR "configs" +#define FUNCTIONS_DIR "functions" diff --git a/src/usbg.c b/src/usbg.c index 978d359..0293f23 100644 --- a/src/usbg.c +++ b/src/usbg.c @@ -16,7 +16,7 @@ #include #include -#include + #include #include #include @@ -28,93 +28,12 @@ #include #include -#define STRINGS_DIR "strings" -#define CONFIGS_DIR "configs" -#define FUNCTIONS_DIR "functions" +#include "usbg/usbg_internal.h" /** * @file usbg.c - * @todo Handle buffer overflows */ -struct usbg_state -{ - char *path; - - TAILQ_HEAD(ghead, usbg_gadget) gadgets; - TAILQ_HEAD(uhead, usbg_udc) udcs; - config_t *last_failed_import; -}; - -struct usbg_gadget -{ - char *name; - char *path; - - TAILQ_ENTRY(usbg_gadget) gnode; - TAILQ_HEAD(chead, usbg_config) configs; - TAILQ_HEAD(fhead, usbg_function) functions; - usbg_state *parent; - config_t *last_failed_import; - usbg_udc *udc; -}; - -struct usbg_config -{ - TAILQ_ENTRY(usbg_config) cnode; - TAILQ_HEAD(bhead, usbg_binding) bindings; - usbg_gadget *parent; - - char *name; - char *path; - char *label; - int id; -}; - -struct usbg_function -{ - TAILQ_ENTRY(usbg_function) fnode; - usbg_gadget *parent; - - char *name; - char *path; - char *instance; - /* Only for internal library usage */ - char *label; - usbg_function_type type; -}; - -struct usbg_binding -{ - TAILQ_ENTRY(usbg_binding) bnode; - usbg_config *parent; - usbg_function *target; - - char *name; - char *path; -}; - -struct usbg_udc -{ - TAILQ_ENTRY(usbg_udc) unode; - usbg_state *parent; - usbg_gadget *gadget; - - char *name; -}; -#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array)) - -#define ARRAY_SIZE_SENTINEL(array, size) \ - static void __attribute__ ((unused)) array##_size_sentinel() \ - { \ - char array##_smaller_than_expected[ \ - (int)(ARRAY_SIZE(array) - size)] \ - __attribute__ ((unused)); \ - \ - char array##_larger_than_expected[ \ - (int)(size - ARRAY_SIZE(array))] \ - __attribute__ ((unused)); \ - } /** * @var function_names @@ -150,36 +69,6 @@ const char *gadget_attr_names[] = ARRAY_SIZE_SENTINEL(gadget_attr_names, USBG_GADGET_ATTR_MAX); -#define ERROR(msg, ...) do {\ - fprintf(stderr, "%s() "msg" \n", \ - __func__, ##__VA_ARGS__);\ - fflush(stderr);\ - } while (0) - -#define ERRORNO(msg, ...) do {\ - fprintf(stderr, "%s() %s: "msg" \n", \ - __func__, strerror(errno), ##__VA_ARGS__);\ - fflush(stderr);\ - } while (0) - -/* Insert in string order */ -#define INSERT_TAILQ_STRING_ORDER(HeadPtr, HeadType, NameField, ToInsert, NodeField) \ - do { \ - if (TAILQ_EMPTY((HeadPtr)) || \ - (strcmp((ToInsert)->NameField, TAILQ_FIRST((HeadPtr))->NameField) < 0)) \ - TAILQ_INSERT_HEAD((HeadPtr), (ToInsert), NodeField); \ - else if (strcmp((ToInsert)->NameField, TAILQ_LAST((HeadPtr), HeadType)->NameField) > 0) \ - TAILQ_INSERT_TAIL((HeadPtr), (ToInsert), NodeField); \ - else { \ - typeof(ToInsert) _cur; \ - TAILQ_FOREACH(_cur, (HeadPtr), NodeField) { \ - if (strcmp((ToInsert)->NameField, _cur->NameField) > 0) \ - continue; \ - TAILQ_INSERT_BEFORE(_cur, (ToInsert), NodeField); \ - } \ - } \ - } while (0) - static int usbg_translate_error(int error) { int ret;