From: Bartosz Golaszewski Date: Mon, 2 Jan 2017 15:05:59 +0000 (+0100) Subject: core: add libgpiod-specific error numbers X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=db64f467fb7a984d2ff9c17a3bcfb4ac8cd63600;p=qemu-gpiodev%2Flibgpiod.git core: add libgpiod-specific error numbers In order to better report error conditions to users, we need a set of libgpiod-specific errnos. Extend the previous API to be able to use both regular errno and GPIO-related error numbers together. Signed-off-by: Bartosz Golaszewski --- diff --git a/core.c b/core.c index d777f5c..a108eda 100644 --- a/core.c +++ b/core.c @@ -27,6 +27,11 @@ static const char libgpiod_consumer[] = "libgpiod"; static __thread int last_error; +static const char *const error_descr[] = { + "success", + "GPIO line not requested", +}; + static void set_last_error(int errnum) { last_error = errnum; @@ -79,7 +84,12 @@ int gpiod_errno(void) const char * gpiod_strerror(int errnum) { - return strerror(errnum); + if (errnum < __GPIOD_ERRNO_OFFSET) + return strerror(errnum); + else if (errnum > __GPIOD_MAX_ERR) + return "invalid error number"; + else + return error_descr[errnum - __GPIOD_ERRNO_OFFSET]; } int gpiod_simple_get_value(const char *device, unsigned int offset) @@ -254,7 +264,7 @@ int gpiod_line_get_value(struct gpiod_line *line) int status; if (!gpiod_line_is_requested(line)) { - set_last_error(EPERM); + set_last_error(GPIOD_ENOTREQUESTED); return -1; } @@ -274,7 +284,7 @@ int gpiod_line_set_value(struct gpiod_line *line, int value) int status; if (!gpiod_line_is_requested(line)) { - set_last_error(EPERM); + set_last_error(GPIOD_ENOTREQUESTED); return -1; } diff --git a/gpiod.h b/gpiod.h index fa9215a..a433374 100644 --- a/gpiod.h +++ b/gpiod.h @@ -32,6 +32,14 @@ extern "C" { #define GPIOD_BIT(nr) (1UL << (nr)) +#define __GPIOD_ERRNO_OFFSET 10000 + +enum { + GPIOD_ESUCCESS = __GPIOD_ERRNO_OFFSET, + GPIOD_ENOTREQUESTED, + __GPIOD_MAX_ERR, +}; + int gpiod_errno(void) GPIOD_API; const char * gpiod_strerror(int errnum) GPIOD_API;