libusbgx: Return usbg_error instead of char* in usbg_get_*_name().
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 26 Feb 2014 15:06:31 +0000 (16:06 +0100)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 22 Dec 2015 19:35:21 +0000 (20:35 +0100)
API should be consistent and use error codes to determine what
type of error occurred instead of returning NULL only.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
[Port from libusbg and update description]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
include/usbg/usbg.h
src/usbg.c

index 0e31f90..c41993c 100644 (file)
@@ -217,9 +217,9 @@ extern size_t usbg_get_configfs_path_len(usbg_state *s);
  * @param s Pointer to state
  * @param buf Buffer where path should be copied
  * @param len Length of given buffer
- * @return Pointer to destination or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern char *usbg_get_configfs_path(usbg_state *s, char *buf, size_t len);
+extern int usbg_get_configfs_path(usbg_state *s, char *buf, size_t len);
 
 /* USB gadget queries */
 
@@ -302,9 +302,9 @@ extern size_t usbg_get_gadget_name_len(usbg_gadget *g);
  * @param b Pointer to gadget
  * @param buf Buffer where name should be copied
  * @param len Length of given buffer
- * @return Pointer to destination or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern char *usbg_get_gadget_name(usbg_gadget *g, char *buf, size_t len);
+extern int usbg_get_gadget_name(usbg_gadget *g, char *buf, size_t len);
 
 /**
  * @brief Set the USB gadget vendor id
@@ -436,9 +436,9 @@ extern size_t usbg_get_function_name_len(usbg_function *f);
  * @param f Pointer to function
  * @param buf Buffer where name should be copied
  * @param len Length of given buffer
- * @return Pointer to destination or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern char *usbg_get_function_name(usbg_function *f, char *buf, size_t len);
+extern int usbg_get_function_name(usbg_function *f, char *buf, size_t len);
 
 /* USB configurations allocation and configuration */
 
@@ -465,9 +465,9 @@ extern size_t usbg_get_config_name_len(usbg_config *c);
  * @param c Pointer to config
  * @param buf Buffer where name should be copied
  * @param len Length of given buffer
- * @return Pointer to destination or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern char *usbg_get_config_name(usbg_config *c, char *buf, size_t len);
+extern int usbg_get_config_name(usbg_config *c, char *buf, size_t len);
 
 /**
  * @brief Set the USB configuration attributes
@@ -555,9 +555,9 @@ extern size_t usbg_get_binding_name_len(usbg_binding *b);
  * @param b Pointer to binding
  * @param buf Buffer where name should be copied
  * @param len Length of given buffer
- * @return Pointer to destination or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern char *usbg_get_binding_name(usbg_binding *b, char *buf, size_t len);
+extern int usbg_get_binding_name(usbg_binding *b, char *buf, size_t len);
 
 /* USB gadget setup and teardown */
 
@@ -594,10 +594,10 @@ extern size_t usbg_get_gadget_udc_len(usbg_gadget *g);
  * @param b Pointer to gadget
  * @param buf Buffer where udc name should be copied
  * @param len Length of given buffer
- * @return Pointer to destination or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  * @note If gadget isn't enabled on any udc returned string is empty.
  */
-extern char *usbg_get_gadget_udc(usbg_gadget *g, char *buf, size_t len);
+extern int usbg_get_gadget_udc(usbg_gadget *g, char *buf, size_t len);
 
 /*
  * USB function-specific attribute configuration
index 1098872..55e44c2 100644 (file)
@@ -741,9 +741,15 @@ size_t usbg_get_configfs_path_len(usbg_state *s)
        return s ? strlen(s->path) : USBG_ERROR_INVALID_PARAM;
 }
 
-char *usbg_get_configfs_path(usbg_state *s, char *buf, size_t len)
+int usbg_get_configfs_path(usbg_state *s, char *buf, size_t len)
 {
-       return s ? strncpy(buf, s->path, len) : NULL;
+       int ret = USBG_SUCCESS;
+       if (s && buf)
+               strncpy(buf, s->path, len);
+       else
+               ret = USBG_ERROR_INVALID_PARAM;
+
+       return ret;
 }
 
 usbg_gadget *usbg_get_gadget(usbg_state *s, const char *name)
@@ -909,9 +915,15 @@ size_t usbg_get_gadget_name_len(usbg_gadget *g)
        return g ? strlen(g->name) : USBG_ERROR_INVALID_PARAM;
 }
 
-char *usbg_get_gadget_name(usbg_gadget *g, char *buf, size_t len)
+int usbg_get_gadget_name(usbg_gadget *g, char *buf, size_t len)
 {
-       return g ? strncpy(buf, g->name, len) : NULL;
+       int ret = USBG_SUCCESS;
+       if (g && buf)
+               strncpy(buf, g->name, len);
+       else
+               ret = USBG_ERROR_INVALID_PARAM;
+
+       return ret;
 }
 
 size_t usbg_get_gadget_udc_len(usbg_gadget *g)
@@ -919,9 +931,15 @@ size_t usbg_get_gadget_udc_len(usbg_gadget *g)
        return g ? strlen(g->udc) : USBG_ERROR_INVALID_PARAM;
 }
 
-char *usbg_get_gadget_udc(usbg_gadget *g, char *buf, size_t len)
+int usbg_get_gadget_udc(usbg_gadget *g, char *buf, size_t len)
 {
-       return g ? strncpy(buf, g->udc, len) : NULL;
+       int ret = USBG_SUCCESS;
+       if (g && buf)
+               strncpy(buf, g->udc, len);
+       else
+               ret = USBG_ERROR_INVALID_PARAM;
+
+       return ret;
 }
 
 void usbg_set_gadget_attrs(usbg_gadget *g, usbg_gadget_attrs *g_attrs)
@@ -1139,9 +1157,15 @@ size_t usbg_get_config_name_len(usbg_config *c)
        return c ? strlen(c->name) : USBG_ERROR_INVALID_PARAM;
 }
 
-char *usbg_get_config_name(usbg_config *c, char *buf, size_t len)
+int usbg_get_config_name(usbg_config *c, char *buf, size_t len)
 {
-       return c ? strncpy(buf, c->name, len) : NULL;
+       int ret = USBG_SUCCESS;
+       if (c && buf)
+               strncpy(buf, c->name, len);
+       else
+               ret = USBG_ERROR_INVALID_PARAM;
+
+       return ret;
 }
 
 size_t usbg_get_function_name_len(usbg_function *f)
@@ -1149,9 +1173,15 @@ size_t usbg_get_function_name_len(usbg_function *f)
        return f ? strlen(f->name) : USBG_ERROR_INVALID_PARAM;
 }
 
-char *usbg_get_function_name(usbg_function *f, char *buf, size_t len)
+int usbg_get_function_name(usbg_function *f, char *buf, size_t len)
 {
-       return f ? strncpy(buf, f->name, len) : NULL;
+       int ret = USBG_SUCCESS;
+       if (f && buf)
+               strncpy(buf, f->name, len);
+       else
+               ret = USBG_ERROR_INVALID_PARAM;
+
+       return ret;
 }
 
 void usbg_set_config_attrs(usbg_config *c, usbg_config_attrs *c_attrs)
@@ -1269,9 +1299,15 @@ size_t usbg_get_binding_name_len(usbg_binding *b)
        return b ? strlen(b->name) : USBG_ERROR_INVALID_PARAM;
 }
 
-char *usbg_get_binding_name(usbg_binding *b, char *buf, size_t len)
+int usbg_get_binding_name(usbg_binding *b, char *buf, size_t len)
 {
-       return b ? strncpy(buf, b->name, len) : NULL;
+       int ret = USBG_SUCCESS;
+       if (b && buf)
+               strncpy(buf, b->name, len);
+       else
+               ret = USBG_ERROR_INVALID_PARAM;
+
+       return ret;
 }
 
 int usbg_get_udcs(struct dirent ***udc_list)