From 7c85291a51a754c59d8a8d3f0fec2df404f4cad6 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Wed, 19 Feb 2014 14:39:35 +0100 Subject: [PATCH] libusbgx: Separate config attrs and strs form configuration. Configuration, its attributes and strings are all logically independent so should be placed in separate structures. Signed-off-by: Krzysztof Opasiak [Port from libusbg and update description] Signed-off-by: Krzysztof Opasiak --- examples/show-gadgets.c | 6 +++--- include/usbg/usbg.h | 32 +++++++++++++++++++++++++------- src/usbg.c | 39 ++++++++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/examples/show-gadgets.c b/examples/show-gadgets.c index 5b6cb61..b9c5948 100644 --- a/examples/show-gadgets.c +++ b/examples/show-gadgets.c @@ -80,9 +80,9 @@ void show_config(struct config *c) struct binding *b; fprintf(stdout, " Configuration '%s'\n", c->name); - fprintf(stdout, " MaxPower\t\t%d\n", c->maxpower); - fprintf(stdout, " bmAttributes\t0x%02x\n", c->bmattrs); - fprintf(stdout, " configuration\t%s\n", c->str_cfg); + fprintf(stdout, " MaxPower\t\t%d\n", c->attrs.bMaxPower); + fprintf(stdout, " bmAttributes\t0x%02x\n", c->attrs.bmAttributes); + fprintf(stdout, " configuration\t%s\n", c->strs.configuration); usbg_for_each_binding(b, c) fprintf(stdout, " %s -> %s\n", b->name,b->target->name); } diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index d787cdc..9b5cedf 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -99,6 +99,25 @@ struct gadget }; /** + * @struct config_attrs + * @brief USB configuration attributes + */ +struct config_attrs +{ + uint8_t bmAttributes; + uint8_t bMaxPower; +}; + +/** + * @struct config_strs + * @brief USB configuration strings + */ +struct config_strs +{ + char configuration[USBG_MAX_STR_LENGTH]; +}; + +/** * @struct config * @brief USB gadget configuration attributes */ @@ -110,9 +129,8 @@ struct config char name[USBG_MAX_NAME_LENGTH]; char path[USBG_MAX_PATH_LENGTH]; - int maxpower; - int bmattrs; - char str_cfg[USBG_MAX_STR_LENGTH]; + struct config_attrs attrs; + struct config_strs strs; }; /** @@ -461,16 +479,16 @@ extern struct config *usbg_create_config(struct gadget *g, char *name); /** * @brief Set the configuration maximum power * @param c Pointer to config - * @param maxpower Maximum power (in 2 mA units) + * @param bMaxPower Maximum power (in 2 mA units) */ -extern void usbg_set_config_max_power(struct config *c, int maxpower); +extern void usbg_set_config_max_power(struct config *c, int bMaxPower); /** * @brief Set the configuration bitmap attributes * @param c Pointer to config - * @param bmattrs Configuration characteristics + * @param bmAttributes Configuration characteristics */ -extern void usbg_set_config_bm_attrs(struct config *c, int bmattrs); +extern void usbg_set_config_bm_attrs(struct config *c, int bmAttributes); /** * @brief Set the configuration string diff --git a/src/usbg.c b/src/usbg.c index 6b48d68..87121d0 100644 --- a/src/usbg.c +++ b/src/usbg.c @@ -265,11 +265,22 @@ static int usbg_parse_functions(char *path, struct gadget *g) return 0; } -static void usbg_parse_config_attrs(struct config *c) +static void usbg_parse_config_attrs(char *path, char *name, + struct config_attrs *c_attrs) { - c->maxpower = usbg_read_dec(c->path, c->name, "MaxPower"); - c->bmattrs = usbg_read_hex(c->path, c->name, "bmAttributes"); - usbg_read_string(c->path, c->name, "strings/0x409/configuration", c->str_cfg); + c_attrs->bMaxPower = usbg_read_dec(path, name, "MaxPower"); + c_attrs->bmAttributes = usbg_read_hex(path, name, "bmAttributes"); +} + +static void usbg_parse_config_strs(char *path, char *name, + struct config_strs *c_attrs) +{ + /* Hardcoded to US English right now*/ + int lang = LANG_US_ENG; + char spath[USBG_MAX_PATH_LENGTH]; + + sprintf(spath, "%s/%s/%s/0x%x", path, name, STRINGS_DIR, lang); + usbg_read_string(spath, "", "configuration", c_attrs->configuration); } static void usbg_parse_config_bindings(struct config *c) @@ -329,7 +340,8 @@ static int usbg_parse_configs(char *path, struct gadget *g) c->parent = g; strcpy(c->name, dent[i]->d_name); strcpy(c->path, cpath); - usbg_parse_config_attrs(c); + usbg_parse_config_attrs(cpath, c->name, &c->attrs); + usbg_parse_config_strs(cpath, c->name, &c->strs); usbg_parse_config_bindings(c); TAILQ_INSERT_TAIL(&g->configs, c, cnode); free(dent[i]); @@ -886,23 +898,24 @@ struct config *usbg_create_config(struct gadget *g, char *name) return NULL; } - usbg_parse_config_attrs(c); + usbg_parse_config_attrs(c->path, c->name, &c->attrs); + usbg_parse_config_strs(c->path, c->name, &c->strs); INSERT_TAILQ_STRING_ORDER(&g->configs, chead, name, c, cnode); return c; } -void usbg_set_config_max_power(struct config *c, int maxpower) +void usbg_set_config_max_power(struct config *c, int bMaxPower) { - c->maxpower = maxpower; - usbg_write_dec(c->path, c->name, "MaxPower", maxpower); + c->attrs.bMaxPower = bMaxPower; + usbg_write_dec(c->path, c->name, "MaxPower", bMaxPower); } -void usbg_set_config_bm_attrs(struct config *c, int bmattrs) +void usbg_set_config_bm_attrs(struct config *c, int bmAttributes) { - c->bmattrs = bmattrs; - usbg_write_hex8(c->path, c->name, "bmAttributes", bmattrs); + c->attrs.bmAttributes = bmAttributes; + usbg_write_hex8(c->path, c->name, "bmAttributes", bmAttributes); } void usbg_set_config_string(struct config *c, int lang, char *str) @@ -915,7 +928,7 @@ void usbg_set_config_string(struct config *c, int lang, char *str) /* strings in library are hardcoded to US English for now */ if (lang == LANG_US_ENG) - strcpy(c->str_cfg, str); + strcpy(c->strs.configuration, str); usbg_write_string(path, "", "configuration", str); } -- 2.7.4