[PATCH v2 19/23] libusbg: Return error code instead of NULL pointer - config.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Change usbg_get_config_strs() and usbg_get_config_attrs()
to return usbg_error instead of NULL pointer.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 include/usbg/usbg.h |    9 ++++-----
 src/usbg.c          |   50 +++++++++++++++++++++++++-------------------------
 2 files changed, 29 insertions(+), 30 deletions(-)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 6855833..1bb652e 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -502,10 +502,9 @@ extern int usbg_set_config_attrs(usbg_config *c,
  * @brief Get the USB configuration strings
  * @param c Pointer to configuration
  * @param c_attrs Structure to be filled
- * @retur Pointer to filled structure or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern usbg_config_attrs *usbg_get_config_attrs(usbg_config *c,
-		usbg_config_attrs *c_attrs);
+extern int usbg_get_config_attrs(usbg_config *c, usbg_config_attrs *c_attrs);
 
 /**
  * @brief Set the configuration maximum power
@@ -528,9 +527,9 @@ extern int usbg_set_config_bm_attrs(usbg_config *c, int bmAttributes);
  * @param c Pointer to configuration
  * @param lang Language of strings
  * @param c_sttrs Structure to be filled
- * @retur Pointer to filled structure or NULL if error occurred.
+ * @return 0 on success or usbg_error if error occurred.
  */
-extern usbg_config_strs *usbg_get_config_strs(usbg_config *c, int lang,
+extern int usbg_get_config_strs(usbg_config *c, int lang,
 		usbg_config_strs *c_strs);
 
 /**
diff --git a/src/usbg.c b/src/usbg.c
index 2d67bd6..f97ad1d 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -441,20 +441,28 @@ static int usbg_parse_functions(char *path, usbg_gadget *g)
 	return ret;
 }
 
-static void usbg_parse_config_attrs(char *path, char *name,
+static int usbg_parse_config_attrs(char *path, char *name,
 		usbg_config_attrs *c_attrs)
 {
-	int buf;
-	usbg_read_dec(path, name, "MaxPower", &buf);
-	c_attrs->bMaxPower = (uint8_t)buf;
-	usbg_read_hex(path, name, "bmAttributes", &buf);
-	c_attrs->bmAttributes = (uint8_t)buf;
+	int buf, ret;
+
+	ret = usbg_read_dec(path, name, "MaxPower", &buf);
+	if (ret == USBG_SUCCESS) {
+		c_attrs->bMaxPower = (uint8_t)buf;
+
+		ret = usbg_read_hex(path, name, "bmAttributes", &buf);
+		if (ret == USBG_SUCCESS)
+			c_attrs->bmAttributes = (uint8_t)buf;
+	}
+
+	return ret;
 }
 
-static usbg_config_strs *usbg_parse_config_strs(char *path, char *name,
+static int usbg_parse_config_strs(char *path, char *name,
 		int lang, usbg_config_strs *c_strs)
 {
 	DIR *dir;
+	int ret;
 	char spath[USBG_MAX_PATH_LENGTH];
 
 	sprintf(spath, "%s/%s/%s/0x%x", path, name, STRINGS_DIR, lang);
@@ -463,12 +471,13 @@ static usbg_config_strs *usbg_parse_config_strs(char *path, char *name,
 	dir = opendir(spath);
 	if (dir) {
 		closedir(dir);
-		usbg_read_string(spath, "", "configuration", c_strs->configuration);
+		ret = usbg_read_string(spath, "", "configuration",
+				c_strs->configuration);
 	} else {
-		c_strs = NULL;
+		ret = usbg_translate_error(errno);
 	}
 
-	return c_strs;
+	return ret;
 }
 
 static int usbg_parse_config_bindings(usbg_config *c)
@@ -1323,15 +1332,11 @@ int usbg_set_config_attrs(usbg_config *c, usbg_config_attrs *c_attrs)
 	return ret;
 }
 
-usbg_config_attrs *usbg_get_config_attrs(usbg_config *c,
+int usbg_get_config_attrs(usbg_config *c,
 		usbg_config_attrs *c_attrs)
 {
-	if (c && c_attrs)
-		usbg_parse_config_attrs(c->path, c->name, c_attrs);
-	else
-		c_attrs = NULL;
-
-	return c_attrs;
+	return c && c_attrs ? usbg_parse_config_attrs(c->path, c->name, c_attrs)
+			: USBG_ERROR_INVALID_PARAM;
 }
 
 int usbg_set_config_max_power(usbg_config *c, int bMaxPower)
@@ -1346,15 +1351,10 @@ int usbg_set_config_bm_attrs(usbg_config *c, int bmAttributes)
 			: USBG_ERROR_INVALID_PARAM;
 }
 
-usbg_config_strs *usbg_get_config_strs(usbg_config *c, int lang,
-		usbg_config_strs *c_strs)
+int usbg_get_config_strs(usbg_config *c, int lang, usbg_config_strs *c_strs)
 {
-	if (c && c_strs)
-		c_strs = usbg_parse_config_strs(c->path, c->name, lang, c_strs);
-	else
-		c_strs = NULL;
-
-	return c_strs;
+	return c && c_strs ? usbg_parse_config_strs(c->path, c->name, lang, c_strs)
+			: USBG_ERROR_INVALID_PARAM;
 }
 
 int usbg_set_config_strs(usbg_config *c, int lang,
-- 
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




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux