diff options
author | Pawel Szewczyk <p.szewczyk@samsung.com> | 2015-07-06 14:12:06 +0200 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2015-08-19 14:29:49 +0200 |
commit | 2817eb2390cd9557c5677b04ad905734d7b480ec (patch) | |
tree | f25b4f2fea1db75ce4cd2fc94e65099a0caa3ead | |
parent | 514cfb156a6447a16ce668d00e66f1beee9da753 (diff) | |
download | libusbg-2817eb2390cd9557c5677b04ad905734d7b480ec.tar.gz libusbg-2817eb2390cd9557c5677b04ad905734d7b480ec.tar.bz2 libusbg-2817eb2390cd9557c5677b04ad905734d7b480ec.zip |
libusbg: Add enum for gadget strings
This commit provides functionality for gadget strings similar to
existing gadget attributes api. It allows to iterate over gadget
strings and to write cleaner and more flexible code.
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Reviewed-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | include/usbg/usbg.h | 32 | ||||
-rw-r--r-- | src/usbg.c | 64 |
2 files changed, 96 insertions, 0 deletions
diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h index 1080962..88391be 100644 --- a/include/usbg/usbg.h +++ b/include/usbg/usbg.h @@ -134,6 +134,14 @@ typedef struct uint16_t bcdDevice; } usbg_gadget_attrs; +typedef enum { + USBG_GADGET_STR_MIN = 0, + STR_PRODUCT = USBG_GADGET_STR_MIN, + STR_MANUFACTURER, + STR_SERIAL_NUMBER, + USBG_GADGET_STR_MAX, +} usbg_gadget_str; + /** * @typedef usbg_gadget_strs * @brief USB gadget device strings @@ -510,6 +518,20 @@ extern const char *usbg_get_gadget_attr_str(usbg_gadget_attr attr); extern int usbg_lookup_gadget_attr(const char *name); /** + * @brief Lookup str code based on its name + * @param name of string + * @return code of suitable string or usbg_error + */ +extern int usbg_lookup_gadget_str(const char *name); + +/** + * @brief Get name of selected gadget string + * @param str Gadget string code + * @return Name of string associated with this code + */ +extern const char *usbg_get_gadget_str_name(usbg_gadget_str str); + +/** * @brief Set selected attribute to value * @param g Pointer to gadget * @param attr Code of selected attribute @@ -654,6 +676,16 @@ extern int usbg_get_gadget_strs(usbg_gadget *g, int lang, usbg_gadget_strs *g_strs); /** + * @brief Set selected string + * @param g Pointer to gadget + * @param str Code of selected string + * @param val value to be set + * @return 0 on success, usbg_error otherwise + */ +extern int usbg_set_gadget_str(usbg_gadget *g, usbg_gadget_str str, int lang, + const char *val); + +/** * @brief Set the USB gadget strings * @param g Pointer to gadget * @param lang USB language ID @@ -70,6 +70,15 @@ const char *gadget_attr_names[] = ARRAY_SIZE_SENTINEL(gadget_attr_names, USBG_GADGET_ATTR_MAX); +const char *gadget_str_names[] = +{ + "product", + "manufacturer", + "serialnumber", +}; + +ARRAY_SIZE_SENTINEL(gadget_str_names, USBG_GADGET_STR_MAX); + int usbg_translate_error(int error) { int ret; @@ -311,6 +320,22 @@ int usbg_lookup_gadget_attr(const char *name) return USBG_ERROR_NOT_FOUND; } +int usbg_lookup_gadget_str(const char *name) +{ + int i = USBG_GADGET_STR_MIN; + + if (!name) + return USBG_ERROR_INVALID_PARAM; + + do { + if (!strcmp(name, gadget_str_names[i])) + return i; + i++; + } while (i != USBG_GADGET_STR_MAX); + + return USBG_ERROR_NOT_FOUND; +} + const char *usbg_get_gadget_attr_str(usbg_gadget_attr attr) { return attr >= USBG_GADGET_ATTR_MIN && @@ -318,6 +343,13 @@ const char *usbg_get_gadget_attr_str(usbg_gadget_attr attr) gadget_attr_names[attr] : NULL; } +const char *usbg_get_gadget_str_name(usbg_gadget_str str) +{ + return str >= USBG_GADGET_STR_MIN && + str < USBG_GADGET_STR_MAX ? + gadget_str_names[str] : NULL; +} + static usbg_error usbg_split_function_instance_type(const char *full_name, usbg_function_type *f_type, const char **instance) { @@ -2401,6 +2433,38 @@ static int usbg_check_dir(const char *path) return ret; } +int usbg_set_gadget_str(usbg_gadget *g, usbg_gadget_str str, int lang, + const char *val) +{ + const char *str_name; + int ret = USBG_ERROR_INVALID_PARAM; + char path[USBG_MAX_PATH_LENGTH]; + int nmb; + + if (!g) + goto out; + + str_name = usbg_get_gadget_str_name(str); + if (!str_name) + goto out; + + nmb = snprintf(path, sizeof(path), "%s/%s/%s/0x%x", g->path, g->name, + STRINGS_DIR, lang); + if (nmb >= sizeof(path)) { + ret = USBG_ERROR_PATH_TOO_LONG; + goto out; + } + + ret = usbg_check_dir(path); + if (ret != USBG_SUCCESS) + goto out; + + ret = usbg_write_string(path, "", str_name, val); + +out: + return ret; +} + int usbg_set_gadget_strs(usbg_gadget *g, int lang, const usbg_gadget_strs *g_strs) { |