core: add libgpiod-specific error numbers
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 15:05:59 +0000 (16:05 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 2 Jan 2017 15:05:59 +0000 (16:05 +0100)
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 <bartekgola@gmail.com>
core.c
gpiod.h

diff --git a/core.c b/core.c
index d777f5c0af195a7ae37011d669541268d768e6b5..a108eda59397b893cbb6906680b471877751b425 100644 (file)
--- 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 fa9215a86978244fb0b2982a9817820829f77fbd..a433374856cd3ddf397e5e340cc76433ab183790 100644 (file)
--- 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;