Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx> --- examples/show-gadgets.c | 41 +++++++++++++++++---------- include/usbg/usbg.h | 70 ++++++++++++++++++++++++++++++++++++++++------- src/usbg.c | 35 ++++++++++++++++++++---- 3 files changed, 117 insertions(+), 29 deletions(-) diff --git a/examples/show-gadgets.c b/examples/show-gadgets.c index 672eac3..26f4b65 100644 --- a/examples/show-gadgets.c +++ b/examples/show-gadgets.c @@ -30,20 +30,19 @@ void show_gadget(usbg_gadget *g) { char buf[USBG_MAX_STR_LENGTH]; + const char *name; int usbg_ret; usbg_gadget_attrs g_attrs; usbg_gadget_strs g_strs; - usbg_get_gadget_name(g, buf, USBG_MAX_STR_LENGTH); - usbg_ret = usbg_get_gadget_attrs(g, &g_attrs); - if (usbg_ret != USBG_SUCCESS) { - fprintf(stderr, "Error: %s : %s\n", usbg_error_name(usbg_ret), - usbg_strerror(usbg_ret)); + name = usbg_get_gadget_name(g); + if (!name) { + fprintf(stderr, "Unable to get gadget name\n"); return; } fprintf(stdout, "ID %04x:%04x '%s'\n", - g_attrs.idVendor, g_attrs.idProduct, buf); + g_attrs.idVendor, g_attrs.idProduct, name); usbg_get_gadget_udc(g, buf, USBG_MAX_STR_LENGTH); fprintf(stdout, " UDC\t\t\t%s\n", buf); @@ -70,12 +69,17 @@ void show_gadget(usbg_gadget *g) void show_function(usbg_function *f) { - char instance[USBG_MAX_STR_LENGTH]; + const char *instance; usbg_function_type type; int usbg_ret; usbg_function_attrs f_attrs; - usbg_get_function_instance(f, instance, USBG_MAX_STR_LENGTH); + instance = usbg_get_function_instance(f); + if (!instance) { + fprintf(stderr, "Unable to get function instance name\n"); + return; + } + type = usbg_get_function_type(f); usbg_ret = usbg_get_function_attrs(f, &f_attrs); if (usbg_ret != USBG_SUCCESS) { @@ -120,15 +124,20 @@ void show_config(usbg_config *c) { usbg_binding *b; usbg_function *f; - char buf[USBG_MAX_STR_LENGTH], instance[USBG_MAX_STR_LENGTH]; + const char *label, *instance, *bname; usbg_function_type type; usbg_config_attrs c_attrs; usbg_config_strs c_strs; int usbg_ret, id; - usbg_get_config_label(c, buf, USBG_MAX_STR_LENGTH); + label = usbg_get_config_label(c); + if (!label) { + fprintf(stderr, "Unable to get config label\n"); + return; + } + id = usbg_get_config_id(c); - fprintf(stdout, " Configuration: '%s' ID: %d\n", buf, id); + fprintf(stdout, " Configuration: '%s' ID: %d\n", label, id); usbg_ret = usbg_get_config_attrs(c, &c_attrs); if (usbg_ret != USBG_SUCCESS) { @@ -150,11 +159,15 @@ void show_config(usbg_config *c) fprintf(stdout, " configuration\t%s\n", c_strs.configuration); usbg_for_each_binding(b, c) { - usbg_get_binding_name(b, buf, USBG_MAX_STR_LENGTH); + bname = usbg_get_binding_name(b); f = usbg_get_binding_target(b); - usbg_get_function_instance(f, instance, USBG_MAX_STR_LENGTH); + instance = usbg_get_function_instance(f); type = usbg_get_function_type(f); - fprintf(stdout, " %s -> %s %s\n", buf, + if (!bname || !instance) { + fprintf(stderr, "Unable to get binding details\n"); + return; + } + fprintf(stdout, " %s -> %s %s\n", bname, usbg_get_function_type_str(type), instance); } } diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index fe0e019..3f5d561 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -255,6 +255,16 @@ extern int usbg_init(const char *configfs_path, usbg_state **state); extern void usbg_cleanup(usbg_state *s); /** + * @brief Get ConfigFS path + * @param s Pointer to state + * @return Path to configfs or NULL if error occurred + * @warning Returned buffer should not be edited! + * Returned string is valid as long as passed usbg_state is valid. + * For example path is valid unitill usbg_cleanup() call. + */ +extern const char *usbg_get_configfs_path(usbg_state *s); + +/** * @brief Get ConfigFS path length * @param s Pointer to state * @return Length of path or usbg_error if error occurred. @@ -262,13 +272,13 @@ extern void usbg_cleanup(usbg_state *s); extern size_t usbg_get_configfs_path_len(usbg_state *s); /** - * @brief Get ConfigFS path + * @brief Copy ConfigFS path to buffer * @param s Pointer to state * @param buf Buffer where path should be copied * @param len Length of given buffer * @return 0 on success or usbg_error if error occurred. */ -extern int usbg_get_configfs_path(usbg_state *s, char *buf, size_t len); +extern int usbg_cpy_configfs_path(usbg_state *s, char *buf, size_t len); /* USB gadget queries */ @@ -402,6 +412,16 @@ extern int usbg_set_gadget_attrs(usbg_gadget *g, extern int usbg_get_gadget_attrs(usbg_gadget *g, usbg_gadget_attrs *g_attrs); /** + * @brief Get gadget name + * @param g Pointer to gadget + * @return Gadget name or NULL if error occurred. + * @warning Returned buffer should not be edited! + * Returned string is valid as long as passed usbg_gadget is valid. + * For example gadget name is valid until someone remove gadget. + */ +extern const char *usbg_get_gadget_name(usbg_gadget *g); + +/** * @brief Get gadget name length * @param g Gadget which name length should be returned * @return Length of name string or usbg_error if error occurred. @@ -409,13 +429,13 @@ extern int usbg_get_gadget_attrs(usbg_gadget *g, usbg_gadget_attrs *g_attrs); extern size_t usbg_get_gadget_name_len(usbg_gadget *g); /** - * @brief Get gadget name + * @brief Copy gadget name * @param g Pointer to gadget * @param buf Buffer where name should be copied * @param len Length of given buffer * @return 0 on success or usbg_error if error occurred. */ -extern int usbg_get_gadget_name(usbg_gadget *g, char *buf, size_t len); +extern int usbg_cpy_gadget_name(usbg_gadget *g, char *buf, size_t len); /** * @brief Set the USB gadget vendor id @@ -553,6 +573,16 @@ extern int usbg_create_function(usbg_gadget *g, usbg_function_type type, usbg_function **f); /** + * @brief Get function instance name + * @param f Pointer to function + * @return instance name or NULL if error occurred. + * @warning Returned buffer should not be edited! + * Returned string is valid as long as passed usbg_function is valid. + * For example instance name is valid until someone remove this function. + */ +extern const char *usbg_get_function_instance(usbg_function *f); + +/** * @brief Get function instance name length * @param f function which name length should be returned * @return Length of name string or usbg_error if error occurred. @@ -560,13 +590,13 @@ extern int usbg_create_function(usbg_gadget *g, usbg_function_type type, extern size_t usbg_get_function_instance_len(usbg_function *f); /** - * @brief Get function instance name + * @brief Copy function instance name * @param f Pointer to function * @param buf Buffer where instance name should be copied * @param len Length of given buffer * @return 0 on success or usbg_error if error occurred. */ -extern int usbg_get_function_instance(usbg_function *f, char *buf, size_t len); +extern int usbg_cpy_function_instance(usbg_function *f, char *buf, size_t len); /** * @brief Get function type as a string @@ -592,6 +622,16 @@ extern int usbg_create_config(usbg_gadget *g, int id, const char *label, usbg_config_attrs *c_attrs, usbg_config_strs *c_strs, usbg_config **c); /** + * @brief Get config label + * @param c Pointer to config + * @return config label or NULL if error occurred. + * @warning Returned buffer should not be edited! + * Returned string is valid as long as passed usbg_config is valid. + * For example config label is valid until someone remove this function. + */ +extern const char *usbg_get_config_label(usbg_config *c); + +/** * @brief Get config label length * @param c Config which label length should be returned * @return Length of label or usbg_error if error occurred. @@ -599,13 +639,13 @@ extern int usbg_create_config(usbg_gadget *g, int id, const char *label, extern size_t usbg_get_config_label_len(usbg_config *c); /** - * @brief Get config label + * @brief Copy config label * @param c Pointer to config * @param buf Buffer where label should be copied * @param len Length of given buffer * @return 0 on success or usbg_error if error occurred. */ -extern int usbg_get_config_label(usbg_config *c, char *buf, size_t len); +extern int usbg_cpy_config_label(usbg_config *c, char *buf, size_t len); /** * @brief Get config id @@ -694,6 +734,16 @@ extern int usbg_add_config_function(usbg_config *c, const char *name, extern usbg_function *usbg_get_binding_target(usbg_binding *b); /** + * @brief Get binding name + * @param b Pointer to binding + * @return Binding name or NULL if error occurred. + * @warning Returned buffer should not be edited! + * Returned string is valid as long as passed usbg_binding is valid. + * For example binding name is valid until someone remove this binding. + */ +extern const char *usbg_get_binding_name(usbg_binding *b); + +/** * @brief Get binding name length * @param b Binding which name length should be returned * @return Length of name string or usbg_error if error occurred. @@ -701,13 +751,13 @@ extern usbg_function *usbg_get_binding_target(usbg_binding *b); extern size_t usbg_get_binding_name_len(usbg_binding *b); /** - * @brief Get binding name + * @brief Copy binding name * @param b Pointer to binding * @param buf Buffer where name should be copied * @param len Length of given buffer * @return 0 on success or usbg_error if error occurred. */ -extern int usbg_get_binding_name(usbg_binding *b, char *buf, size_t len); +extern int usbg_cpy_binding_name(usbg_binding *b, char *buf, size_t len); /* USB gadget setup and teardown */ diff --git a/src/usbg.c b/src/usbg.c index 785c01a..5f910f0 100644 --- a/src/usbg.c +++ b/src/usbg.c @@ -1316,12 +1316,17 @@ void usbg_cleanup(usbg_state *s) usbg_free_state(s); } +const char *usbg_get_configfs_path(usbg_state *s) +{ + return s ? s->path : NULL; +} + size_t usbg_get_configfs_path_len(usbg_state *s) { return s ? strlen(s->path) : USBG_ERROR_INVALID_PARAM; } -int usbg_get_configfs_path(usbg_state *s, char *buf, size_t len) +int usbg_cpy_configfs_path(usbg_state *s, char *buf, size_t len) { int ret = USBG_SUCCESS; if (s && buf) { @@ -1706,12 +1711,17 @@ int usbg_get_gadget_attrs(usbg_gadget *g, usbg_gadget_attrs *g_attrs) : USBG_ERROR_INVALID_PARAM; } +const char *usbg_get_gadget_name(usbg_gadget *g) +{ + return g ? g->name : NULL; +} + size_t usbg_get_gadget_name_len(usbg_gadget *g) { return g ? strlen(g->name) : USBG_ERROR_INVALID_PARAM; } -int usbg_get_gadget_name(usbg_gadget *g, char *buf, size_t len) +int usbg_cpy_gadget_name(usbg_gadget *g, char *buf, size_t len) { int ret = USBG_SUCCESS; if (g && buf) { @@ -2089,12 +2099,17 @@ out: return ret; } +const char *usbg_get_config_label(usbg_config *c) +{ + return c ? c->label : NULL; +} + size_t usbg_get_config_label_len(usbg_config *c) { return c ? strlen(c->label) : USBG_ERROR_INVALID_PARAM; } -int usbg_get_config_label(usbg_config *c, char *buf, size_t len) +int usbg_cpy_config_label(usbg_config *c, char *buf, size_t len) { int ret = USBG_SUCCESS; if (c && buf) { @@ -2112,12 +2127,17 @@ int usbg_get_config_id(usbg_config *c) return c ? c->id : USBG_ERROR_INVALID_PARAM; } +const char *usbg_get_function_instance(usbg_function *f) +{ + return f ? f->instance : NULL; +} + size_t usbg_get_function_instance_len(usbg_function *f) { return f ? strlen(f->instance) : USBG_ERROR_INVALID_PARAM; } -int usbg_get_function_instance(usbg_function *f, char *buf, size_t len) +int usbg_cpy_function_instance(usbg_function *f, char *buf, size_t len) { int ret = USBG_SUCCESS; if (f && buf) { @@ -2273,12 +2293,17 @@ usbg_function *usbg_get_binding_target(usbg_binding *b) return b ? b->target : NULL; } +const char *usbg_get_binding_name(usbg_binding *b) +{ + return b ? b->name : NULL; +} + size_t usbg_get_binding_name_len(usbg_binding *b) { return b ? strlen(b->name) : USBG_ERROR_INVALID_PARAM; } -int usbg_get_binding_name(usbg_binding *b, char *buf, size_t len) +int usbg_cpy_binding_name(usbg_binding *b, char *buf, size_t len) { int ret = USBG_SUCCESS; if (b && buf) { -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html