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.opas...@samsung.com>
---
 include/usbg/usbg.h |   24 ++++++++++-----------
 src/usbg.c          |   60 ++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 60 insertions(+), 24 deletions(-)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index ad52a5f..0d76010 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -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
diff --git a/src/usbg.c b/src/usbg.c
index 1098872..55e44c2 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -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)
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to