[PATCH v2 22/23] libusbg: Add functions for getting error strings.

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

 



Add usbg_error_name() to get error name as a string.

Add usbg_strerror() to get brief error description in English.

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 include/usbg/usbg.h |   16 ++++++++++
 src/usbg.c          |   86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)

diff --git a/include/usbg/usbg.h b/include/usbg/usbg.h
index 249b40f..354ba38 100644
--- a/include/usbg/usbg.h
+++ b/include/usbg/usbg.h
@@ -175,6 +175,8 @@ typedef union {
 	usbg_f_phonet_attrs phonet;
 } usbg_function_attrs;
 
+/* Error codes */
+
 /**
  * @typedef usbg_error
  * @brief Errors which could be returned by library functions
@@ -193,6 +195,20 @@ typedef enum  {
 	USBG_ERROR_OTHER_ERROR = -99
 } usbg_error;
 
+/*
+ * @brief Get the error name as a constant string
+ * @param e error code
+ * @return Constant string with error name
+ */
+extern const char *usbg_error_name(usbg_error e);
+
+/*
+ * @brief Get the short description of error
+ * @param e error code
+ * @return Constant string with error description
+ */
+extern const char *usbg_strerror(usbg_error e);
+
 /* Library init and cleanup */
 
 /**
diff --git a/src/usbg.c b/src/usbg.c
index 944dd10..699ed87 100644
--- a/src/usbg.c
+++ b/src/usbg.c
@@ -172,6 +172,92 @@ static int usbg_translate_error(int error)
 	return ret;
 }
 
+const char *usbg_error_name(usbg_error e)
+{
+	char *ret = "UNKNOWN";
+
+	switch (e) {
+	case USBG_SUCCESS:
+		ret = "USBG_SUCCESS";
+		break;
+	case USBG_ERROR_NO_MEM:
+		ret = "USBG_ERROR_NO_MEM";
+		break;
+	case USBG_ERROR_NO_ACCESS:
+		ret = "USBG_ERROR_NO_ACCESS";
+		break;
+	case USBG_ERROR_INVALID_PARAM:
+		ret = "USBG_ERROR_INVALID_PARAM";
+		break;
+	case USBG_ERROR_NOT_FOUND:
+		ret = "USBG_ERROR_NOT_FOUND";
+		break;
+	case USBG_ERROR_IO:
+		ret = "USBG_ERROR_IO";
+		break;
+	case USBG_ERROR_EXIST:
+		ret = "USBG_ERROR_EXIST";
+		break;
+	case USBG_ERROR_NO_DEV:
+		ret = "USBG_ERROR_NO_DEV";
+		break;
+	case USBG_ERROR_BUSY:
+		ret = "USBG_ERROR_BUSY";
+		break;
+	case USBG_ERROR_NOT_SUPPORTED:
+		ret = "USBG_ERROR_NOT_SUPPORTED";
+		break;
+	case USBG_ERROR_OTHER_ERROR:
+		ret = "USBG_ERROR_OTHER_ERROR";
+		break;
+	}
+
+	return ret;
+}
+
+const char *usbg_strerror(usbg_error e)
+{
+	char *ret = "Unknown error";
+
+	switch (e) {
+	case USBG_SUCCESS:
+		ret = "Success";
+		break;
+	case USBG_ERROR_NO_MEM:
+		ret = "Insufficient memory";
+		break;
+	case USBG_ERROR_NO_ACCESS:
+		ret = "Access denied (insufficient permissions)";
+		break;
+	case USBG_ERROR_INVALID_PARAM:
+		ret = "Invalid parameter";
+		break;
+	case USBG_ERROR_NOT_FOUND:
+		ret = "Not found (file or directory removed)";
+		break;
+	case USBG_ERROR_IO:
+		ret = "Input/output error";
+		break;
+	case USBG_ERROR_EXIST:
+		ret = "Already exist";
+		break;
+	case USBG_ERROR_NO_DEV:
+		ret = "No such device (illegal device name)";
+		break;
+	case USBG_ERROR_BUSY:
+		ret = "Busy (gadget enabled)";
+		break;
+	case USBG_ERROR_NOT_SUPPORTED:
+		ret = "Function not supported";
+		break;
+	case USBG_ERROR_OTHER_ERROR:
+		ret = "Other error";
+		break;
+	}
+
+	return ret;
+}
+
 static int usbg_lookup_function_type(char *name)
 {
 	int i = 0;
-- 
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