diff --git a/lib/libx52/Makefile.am b/lib/libx52/Makefile.am index 9fe6c59..2218dbf 100644 --- a/lib/libx52/Makefile.am +++ b/lib/libx52/Makefile.am @@ -4,8 +4,9 @@ lib_LTLIBRARIES = libx52.la # Core libx52 library # This library handles the USB communication between the host and the X52 -libx52_la_SOURCES = x52_control.c x52_core.c x52_date_time.c x52_mfd_led.c -libx52_la_LDFLAGS = -version-info 2:0:0 -lusb-1.0 +libx52_la_SOURCES = x52_control.c x52_core.c x52_date_time.c x52_mfd_led.c \ + x52_strerror.c +libx52_la_LDFLAGS = -version-info 3:0:1 -lusb-1.0 # Header files that need to be copied x52includedir = $(includedir)/x52pro diff --git a/lib/libx52/libx52.h b/lib/libx52/libx52.h index cab3f8b..44493e7 100644 --- a/lib/libx52/libx52.h +++ b/lib/libx52/libx52.h @@ -460,6 +460,16 @@ int libx52_update(libx52_device *x52); */ int libx52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value); +/** + * @brief Return a string representation of the error code + * + * @param[in] error Error code returned by libx52 function + * + * @returns Pointer to a NULL terminated string describing the error. + * Returned pointer must not be freed. + */ +char * libx52_strerror(libx52_error_code error); + #ifdef __cplusplus } #endif diff --git a/lib/libx52/x52_strerror.c b/lib/libx52/x52_strerror.c new file mode 100644 index 0000000..2b8c912 --- /dev/null +++ b/lib/libx52/x52_strerror.c @@ -0,0 +1,81 @@ +/* + * Saitek X52 Pro MFD & LED driver + * + * Copyright (C) 2012-2017 Nirenjan Krishnan (nirenjan@nirenjan.org) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, version 2. + * + */ + +#include + +#include "libx52.h" + +/** For future use in i18n */ +#define _(str) str + +/* Error buffer used for building custom error strings */ +static char error_buffer[256]; + +char * libx52_strerror(libx52_error_code error) +{ + switch (error) { + case LIBX52_SUCCESS: + return _("Success"); + + case LIBX52_ERROR_INIT_FAILURE: + return _("Initialization failure"); + + case LIBX52_ERROR_OUT_OF_MEMORY: + return _("Insufficient memory"); + + case LIBX52_ERROR_INVALID_PARAM: + return _("Invalid parameter"); + + case LIBX52_ERROR_NOT_SUPPORTED: + return _("Operation not supported"); + + case LIBX52_ERROR_TRY_AGAIN: + return _("Try again"); + + case LIBX52_ERROR_OUT_OF_RANGE: + return _("Input parameter out of range"); + + case LIBX52_ERROR_USB_FAILURE: + return _("USB transaction failure"); + + case LIBX52_ERROR_IO: + return _("USB input/output error"); + + case LIBX52_ERROR_PERM: + return _("Access denied"); + + case LIBX52_ERROR_NO_DEVICE: + return _("No such device"); + + case LIBX52_ERROR_NOT_FOUND: + return _("Entity not found"); + + case LIBX52_ERROR_BUSY: + return _("Resource busy"); + + case LIBX52_ERROR_TIMEOUT: + return _("Operation timeout"); + + case LIBX52_ERROR_OVERFLOW: + return _("Overflow"); + + case LIBX52_ERROR_PIPE: + return _("Pipe error"); + + case LIBX52_ERROR_INTERRUPTED: + return _("System call interrupted"); + + default: + snprintf(error_buffer, sizeof(error_buffer), + _("Unknown error %d"), error); + return error_buffer; + } +}