summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/xusb.c2
-rw-r--r--libusb/core.c46
-rw-r--r--libusb/libusb.h6
3 files changed, 53 insertions, 1 deletions
diff --git a/examples/xusb.c b/examples/xusb.c
index 05a4610..69b3d55 100644
--- a/examples/xusb.c
+++ b/examples/xusb.c
@@ -53,7 +53,7 @@ inline static int perr(char const *format, ...)
return r;
}
-#define ERR_EXIT(errcode) do { perr(" libusb error: %d\n", errcode); return -1; } while (0)
+#define ERR_EXIT(errcode) do { perr(" %s\n", libusb_strerror(errcode)); return -1; } while (0)
#define CALL_CHECK(fcall) do { r=fcall; if (r < 0) ERR_EXIT(r); } while (0);
#define B(x) (((x)!=0)?1:0)
#define be_to_int32(buf) (((buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<8)|(buf)[3])
diff --git a/libusb/core.c b/libusb/core.c
index 53c5988..a7355b5 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -1615,3 +1615,49 @@ void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
va_end (args);
}
+/** \ingroup misc
+ * Returns a constant string with an English short description of the given
+ * error code. The caller should never free() the returned pointer since it
+ * points to a constant string.
+ * The returned string is encoded in ASCII form and always starts with a capital
+ * letter and ends without any dot.
+ * \param errcode the error code whose description is desired
+ * \returns a short description of the error code in English
+ */
+const char* libusb_strerror(enum libusb_error errcode)
+{
+ switch (errcode)
+ {
+ case LIBUSB_SUCCESS:
+ return "Success";
+ case LIBUSB_ERROR_IO:
+ return "Input/output error";
+ case LIBUSB_ERROR_INVALID_PARAM:
+ return "Invalid parameter";
+ case LIBUSB_ERROR_ACCESS:
+ return "Access denied (insufficient permissions)";
+ case LIBUSB_ERROR_NO_DEVICE:
+ return "No such device (it may have been disconnected)";
+ case LIBUSB_ERROR_NOT_FOUND:
+ return "Entity not found";
+ case LIBUSB_ERROR_BUSY:
+ return "Resource busy";
+ case LIBUSB_ERROR_TIMEOUT:
+ return "Operation timed out";
+ case LIBUSB_ERROR_OVERFLOW:
+ return "Overflow";
+ case LIBUSB_ERROR_PIPE:
+ return "Pipe error";
+ case LIBUSB_ERROR_INTERRUPTED:
+ return "System call interrupted (perhaps due to signal)";
+ case LIBUSB_ERROR_NO_MEM:
+ return "Insufficient memory";
+ case LIBUSB_ERROR_NOT_SUPPORTED:
+ return "Operation not supported or unimplemented on this platform";
+ case LIBUSB_ERROR_OTHER:
+ return "Other error";
+
+ default:
+ return "Unknown error";
+ }
+}
diff --git a/libusb/libusb.h b/libusb/libusb.h
index a7d34bf..d8b17de 100644
--- a/libusb/libusb.h
+++ b/libusb/libusb.h
@@ -609,6 +609,8 @@ typedef struct libusb_device_handle libusb_device_handle;
/** \ingroup misc
* Error codes. Most libusb functions return 0 on success or one of these
* codes on failure.
+ * You can use libusb_strerror() to retrieve a short string description of
+ * a libusb_error enumeration value.
*/
enum libusb_error {
/** Success (no error) */
@@ -652,6 +654,9 @@ enum libusb_error {
/** Other error */
LIBUSB_ERROR_OTHER = -99
+
+ /* IMPORTANT: when adding new values to this enum, remember to update the
+ libusb_strerror() function implementation! */
};
/** \ingroup asyncio
@@ -790,6 +795,7 @@ struct libusb_transfer {
int libusb_init(libusb_context **ctx);
void libusb_exit(libusb_context *ctx);
void libusb_set_debug(libusb_context *ctx, int level);
+const char* libusb_strerror(enum libusb_error errcode);
ssize_t libusb_get_device_list(libusb_context *ctx,
libusb_device ***list);