gpio.c: Improve gpio error handling
Changed syslog messages and some error returns. Signed-off-by: Martin G Lane-Smith <mlanesmith@gmail.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
156
src/gpio/gpio.c
156
src/gpio/gpio.c
@@ -34,6 +34,7 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#define SYSFS_CLASS_GPIO "/sys/class/gpio"
|
#define SYSFS_CLASS_GPIO "/sys/class/gpio"
|
||||||
#define MAX_SIZE 64
|
#define MAX_SIZE 64
|
||||||
@@ -46,6 +47,7 @@ mraa_gpio_get_valfp(mraa_gpio_context dev)
|
|||||||
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
|
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
|
||||||
dev->value_fp = open(bu, O_RDWR);
|
dev->value_fp = open(bu, O_RDWR);
|
||||||
if (dev->value_fp == -1) {
|
if (dev->value_fp == -1) {
|
||||||
|
syslog(LOG_ERR, "gpio%i: Failed to open 'value': %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +66,7 @@ mraa_gpio_init_internal(mraa_adv_func_t* func_table, int pin)
|
|||||||
|
|
||||||
mraa_gpio_context dev = (mraa_gpio_context) calloc(1, sizeof(struct _gpio));
|
mraa_gpio_context dev = (mraa_gpio_context) calloc(1, sizeof(struct _gpio));
|
||||||
if (dev == NULL) {
|
if (dev == NULL) {
|
||||||
syslog(LOG_CRIT, "gpio: Failed to allocate memory for context");
|
syslog(LOG_CRIT, "gpio%i: Failed to allocate memory for context", pin);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,15 +104,15 @@ mraa_gpio_init_internal(mraa_adv_func_t* func_table, int pin)
|
|||||||
} else {
|
} else {
|
||||||
int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
|
int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
|
||||||
if (export == -1) {
|
if (export == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to open export for writing");
|
syslog(LOG_ERR, "gpio%i: init: Failed to open 'export' for writing: %s", pin, strerror(errno));
|
||||||
status = MRAA_ERROR_NO_RESOURCES;
|
status = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
goto init_internal_cleanup;
|
goto init_internal_cleanup;
|
||||||
}
|
}
|
||||||
length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
||||||
if (write(export, bu, length * sizeof(char)) == -1) {
|
if (write(export, bu, length * sizeof(char)) == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to write %d to export", dev->pin);
|
syslog(LOG_ERR, "gpio%i: init: Failed to write to 'export': %s", pin, strerror(errno));
|
||||||
close(export);
|
close(export);
|
||||||
status = MRAA_ERROR_NO_RESOURCES;
|
status = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
goto init_internal_cleanup;
|
goto init_internal_cleanup;
|
||||||
}
|
}
|
||||||
dev->owner = 1;
|
dev->owner = 1;
|
||||||
@@ -131,38 +133,37 @@ mraa_gpio_init(int pin)
|
|||||||
{
|
{
|
||||||
mraa_board_t* board = plat;
|
mraa_board_t* board = plat;
|
||||||
if (board == NULL) {
|
if (board == NULL) {
|
||||||
syslog(LOG_ERR, "gpio: platform not initialised");
|
syslog(LOG_ERR, "gpio%i: init: platform not initialised", pin);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mraa_is_sub_platform_id(pin)) {
|
if (mraa_is_sub_platform_id(pin)) {
|
||||||
syslog(LOG_NOTICE, "gpio: Using sub platform");
|
syslog(LOG_NOTICE, "gpio%i: init: Using sub platform", pin);
|
||||||
board = board->sub_platform;
|
board = board->sub_platform;
|
||||||
if (board == NULL) {
|
if (board == NULL) {
|
||||||
syslog(LOG_ERR, "gpio: Sub platform Not Initialised");
|
syslog(LOG_ERR, "gpio%i: init: Sub platform not initialised", pin);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
pin = mraa_get_sub_platform_index(pin);
|
pin = mraa_get_sub_platform_index(pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pin < 0 || pin >= board->phy_pin_count) {
|
if (pin < 0 || pin >= board->phy_pin_count) {
|
||||||
syslog(LOG_ERR, "gpio: pin %i beyond platform definition", pin);
|
syslog(LOG_ERR, "gpio: init: pin %i beyond platform pin count (%i)", pin, board->phy_pin_count);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (board->pins[pin].capabilites.gpio != 1) {
|
if (board->pins[pin].capabilites.gpio != 1) {
|
||||||
syslog(LOG_ERR, "gpio: pin %i not capable of gpio", pin);
|
syslog(LOG_ERR, "gpio: init: pin %i not capable of gpio", pin);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (board->pins[pin].gpio.mux_total > 0) {
|
if (board->pins[pin].gpio.mux_total > 0) {
|
||||||
if (mraa_setup_mux_mapped(board->pins[pin].gpio) != MRAA_SUCCESS) {
|
if (mraa_setup_mux_mapped(board->pins[pin].gpio) != MRAA_SUCCESS) {
|
||||||
syslog(LOG_ERR, "gpio: unable to setup muxes");
|
syslog(LOG_ERR, "gpio%i: init: unable to setup muxes", pin);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa_gpio_context r = mraa_gpio_init_internal(board->adv_func, board->pins[pin].gpio.pinmap);
|
mraa_gpio_context r = mraa_gpio_init_internal(board->adv_func, board->pins[pin].gpio.pinmap);
|
||||||
if (r == NULL) {
|
if (r == NULL) {
|
||||||
syslog(LOG_CRIT, "gpio: mraa_gpio_init_raw(%d) returned error", pin);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (r->phy_pin == -1)
|
if (r->phy_pin == -1)
|
||||||
@@ -199,12 +200,12 @@ mraa_gpio_wait_interrupt(int fd
|
|||||||
struct pollfd pfd[2];
|
struct pollfd pfd[2];
|
||||||
|
|
||||||
if (control_fd < 0) {
|
if (control_fd < 0) {
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup poll on POLLPRI
|
// setup poll on POLLPRI
|
||||||
@@ -218,14 +219,14 @@ mraa_gpio_wait_interrupt(int fd
|
|||||||
#ifdef HAVE_PTHREAD_CANCEL
|
#ifdef HAVE_PTHREAD_CANCEL
|
||||||
// Wait for it forever or until pthread_cancel
|
// Wait for it forever or until pthread_cancel
|
||||||
// poll is a cancelable point like sleep()
|
// poll is a cancelable point like sleep()
|
||||||
int x = poll(pfd, 1, -1);
|
poll(pfd, 1, -1);
|
||||||
#else
|
#else
|
||||||
// setup poll on the controling fd
|
// setup poll on the controling fd
|
||||||
pfd[1].fd = control_fd;
|
pfd[1].fd = control_fd;
|
||||||
pfd[1].events = 0; // POLLHUP, POLLERR, and POLLNVAL
|
pfd[1].events = 0; // POLLHUP, POLLERR, and POLLNVAL
|
||||||
|
|
||||||
// Wait for it forever or until control fd is closed
|
// Wait for it forever or until control fd is closed
|
||||||
int x = poll(pfd, 2, -1);
|
poll(pfd, 2, -1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// do a final read to clear interrupt
|
// do a final read to clear interrupt
|
||||||
@@ -250,14 +251,14 @@ mraa_gpio_interrupt_handler(void* arg)
|
|||||||
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
|
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
|
||||||
fp = open(bu, O_RDONLY);
|
fp = open(bu, O_RDONLY);
|
||||||
if (fp < 0) {
|
if (fp < 0) {
|
||||||
syslog(LOG_ERR, "gpio: failed to open gpio%d/value", dev->pin);
|
syslog(LOG_ERR, "gpio%i: interrupt_handler: failed to open 'value' : %s", dev->pin, strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef HAVE_PTHREAD_CANCEL
|
#ifndef HAVE_PTHREAD_CANCEL
|
||||||
if (pipe(dev->isr_control_pipe)) {
|
if (pipe(dev->isr_control_pipe)) {
|
||||||
syslog(LOG_ERR, "gpio: failed to create isr control pipe");
|
syslog(LOG_ERR, "gpio%i: interrupt_handler: failed to create isr control pipe: %s", dev->pin, strerror(errno));
|
||||||
close(fp);
|
close(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -322,7 +323,12 @@ mraa_gpio_interrupt_handler(void* arg)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
|
mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
|
||||||
{
|
{
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_edge_mode_replace))
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: edge_mode: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IS_FUNC_DEFINED(dev, gpio_edge_mode_replace))
|
||||||
return dev->advance_func->gpio_edge_mode_replace(dev, mode);
|
return dev->advance_func->gpio_edge_mode_replace(dev, mode);
|
||||||
|
|
||||||
if (dev->value_fp != -1) {
|
if (dev->value_fp != -1) {
|
||||||
@@ -335,7 +341,7 @@ mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
|
|||||||
|
|
||||||
int edge = open(filepath, O_RDWR);
|
int edge = open(filepath, O_RDWR);
|
||||||
if (edge == -1) {
|
if (edge == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to open edge for writing");
|
syslog(LOG_ERR, "gpio%i: edge_mode: Failed to open 'edge' for writing: %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,9 +365,9 @@ mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
|
|||||||
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
if (write(edge, bu, length * sizeof(char)) == -1) {
|
if (write(edge, bu, length * sizeof(char)) == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to write to edge");
|
syslog(LOG_ERR, "gpio%i: edge_mode: Failed to write to 'edge': %s", dev->pin, strerror(errno));
|
||||||
close(edge);
|
close(edge);
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(edge);
|
close(edge);
|
||||||
@@ -371,13 +377,19 @@ mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t mode, void (*fptr)(void*), void* args)
|
mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t mode, void (*fptr)(void*), void* args)
|
||||||
{
|
{
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: isr: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
// we only allow one isr per mraa_gpio_context
|
// we only allow one isr per mraa_gpio_context
|
||||||
if (dev->thread_id != 0) {
|
if (dev->thread_id != 0) {
|
||||||
return MRAA_ERROR_NO_RESOURCES;
|
return MRAA_ERROR_NO_RESOURCES;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MRAA_SUCCESS != mraa_gpio_edge_mode(dev, mode)) {
|
mraa_result_t ret = mraa_gpio_edge_mode(dev, mode);
|
||||||
return MRAA_ERROR_UNSPECIFIED;
|
if (ret != MRAA_SUCCESS) {
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev->isr = fptr;
|
dev->isr = fptr;
|
||||||
@@ -401,6 +413,10 @@ mraa_gpio_isr_exit(mraa_gpio_context dev)
|
|||||||
{
|
{
|
||||||
mraa_result_t ret = MRAA_SUCCESS;
|
mraa_result_t ret = MRAA_SUCCESS;
|
||||||
|
|
||||||
|
if (dev == NULL) {
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
// wasting our time, there is no isr to exit from
|
// wasting our time, there is no isr to exit from
|
||||||
if (dev->thread_id == 0 && dev->isr_value_fp == -1) {
|
if (dev->thread_id == 0 && dev->isr_value_fp == -1) {
|
||||||
return ret;
|
return ret;
|
||||||
@@ -414,12 +430,12 @@ mraa_gpio_isr_exit(mraa_gpio_context dev)
|
|||||||
if ((dev->thread_id != 0)) {
|
if ((dev->thread_id != 0)) {
|
||||||
#ifdef HAVE_PTHREAD_CANCEL
|
#ifdef HAVE_PTHREAD_CANCEL
|
||||||
if ((pthread_cancel(dev->thread_id) != 0) || (pthread_join(dev->thread_id, NULL) != 0)) {
|
if ((pthread_cancel(dev->thread_id) != 0) || (pthread_join(dev->thread_id, NULL) != 0)) {
|
||||||
ret = MRAA_ERROR_INVALID_HANDLE;
|
ret = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
close(dev->isr_control_pipe[1]);
|
close(dev->isr_control_pipe[1]);
|
||||||
if (pthread_join(dev->thread_id, NULL) != 0)
|
if (pthread_join(dev->thread_id, NULL) != 0)
|
||||||
ret = MRAA_ERROR_INVALID_HANDLE;
|
ret = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
|
|
||||||
close(dev->isr_control_pipe[0]);
|
close(dev->isr_control_pipe[0]);
|
||||||
dev->isr_control_pipe[0] = dev->isr_control_pipe[1] = -1;
|
dev->isr_control_pipe[0] = dev->isr_control_pipe[1] = -1;
|
||||||
@@ -429,7 +445,7 @@ mraa_gpio_isr_exit(mraa_gpio_context dev)
|
|||||||
// close the filehandle in case it's still open
|
// close the filehandle in case it's still open
|
||||||
if (dev->isr_value_fp != -1) {
|
if (dev->isr_value_fp != -1) {
|
||||||
if (close(dev->isr_value_fp) != 0) {
|
if (close(dev->isr_value_fp) != 0) {
|
||||||
ret = MRAA_ERROR_INVALID_PARAMETER;
|
ret = MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,6 +459,11 @@ mraa_gpio_isr_exit(mraa_gpio_context dev)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
||||||
{
|
{
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: mode: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_mode_replace))
|
if (IS_FUNC_DEFINED(dev, gpio_mode_replace))
|
||||||
return dev->advance_func->gpio_mode_replace(dev, mode);
|
return dev->advance_func->gpio_mode_replace(dev, mode);
|
||||||
|
|
||||||
@@ -462,7 +483,7 @@ mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
|||||||
|
|
||||||
int drive = open(filepath, O_WRONLY);
|
int drive = open(filepath, O_WRONLY);
|
||||||
if (drive == -1) {
|
if (drive == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to open drive for writing");
|
syslog(LOG_ERR, "gpio%i: mode: Failed to open 'drive' for writing: %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -486,8 +507,8 @@ mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
|||||||
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
if (write(drive, bu, length * sizeof(char)) == -1) {
|
if (write(drive, bu, length * sizeof(char)) == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to write to drive mode");
|
syslog(LOG_ERR, "gpio%i: mode: Failed to write to 'drive': %s", dev->pin, strerror(errno));
|
||||||
close(drive);
|
close(drive);
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -500,6 +521,11 @@ mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
||||||
{
|
{
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: dir: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_dir_replace)) {
|
if (IS_FUNC_DEFINED(dev, gpio_dir_replace)) {
|
||||||
return dev->advance_func->gpio_dir_replace(dev, dir);
|
return dev->advance_func->gpio_dir_replace(dev, dir);
|
||||||
}
|
}
|
||||||
@@ -511,9 +537,6 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dev == NULL) {
|
|
||||||
return MRAA_ERROR_INVALID_HANDLE;
|
|
||||||
}
|
|
||||||
if (dev->value_fp != -1) {
|
if (dev->value_fp != -1) {
|
||||||
close(dev->value_fp);
|
close(dev->value_fp);
|
||||||
dev->value_fp = -1;
|
dev->value_fp = -1;
|
||||||
@@ -532,8 +555,9 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
case MRAA_GPIO_OUT_LOW:
|
case MRAA_GPIO_OUT_LOW:
|
||||||
return mraa_gpio_write(dev, 0);
|
return mraa_gpio_write(dev, 0);
|
||||||
default:
|
default:
|
||||||
|
syslog(LOG_ERR, "gpio%i: dir: Failed to open 'direction' for writing: %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char bu[MAX_SIZE];
|
char bu[MAX_SIZE];
|
||||||
@@ -558,7 +582,8 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
|
|||||||
|
|
||||||
if (write(direction, bu, length * sizeof(char)) == -1) {
|
if (write(direction, bu, length * sizeof(char)) == -1) {
|
||||||
close(direction);
|
close(direction);
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
syslog(LOG_ERR, "gpio%i: dir: Failed to write to 'direction': %s", dev->pin, strerror(errno));
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(direction);
|
close(direction);
|
||||||
@@ -575,9 +600,15 @@ mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
|||||||
int fd, rc;
|
int fd, rc;
|
||||||
mraa_result_t result = MRAA_SUCCESS;
|
mraa_result_t result = MRAA_SUCCESS;
|
||||||
|
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: read_dir: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
|
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
|
||||||
fd = open(filepath, O_RDONLY);
|
fd = open(filepath, O_RDONLY);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
|
syslog(LOG_ERR, "gpio%i: read_dir: Failed to open 'direction' for reading: %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -585,6 +616,7 @@ mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
|||||||
rc = read(fd, value, sizeof(value));
|
rc = read(fd, value, sizeof(value));
|
||||||
close(fd);
|
close(fd);
|
||||||
if (rc <= 0) {
|
if (rc <= 0) {
|
||||||
|
syslog(LOG_ERR, "gpio%i: read_dir: Failed to read 'direction': %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -593,7 +625,8 @@ mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
|||||||
} else if (strcmp(value, "in\n") == 0) {
|
} else if (strcmp(value, "in\n") == 0) {
|
||||||
*dir = MRAA_GPIO_IN;
|
*dir = MRAA_GPIO_IN;
|
||||||
} else {
|
} else {
|
||||||
result = MRAA_ERROR_INVALID_RESOURCE;
|
syslog(LOG_ERR, "gpio%i: read_dir: unknown direction: %s", dev->pin, value);
|
||||||
|
result = MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -602,8 +635,10 @@ mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
|
|||||||
int
|
int
|
||||||
mraa_gpio_read(mraa_gpio_context dev)
|
mraa_gpio_read(mraa_gpio_context dev)
|
||||||
{
|
{
|
||||||
if (dev == NULL)
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: read: context is invalid");
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_read_replace))
|
if (IS_FUNC_DEFINED(dev, gpio_read_replace))
|
||||||
return dev->advance_func->gpio_read_replace(dev);
|
return dev->advance_func->gpio_read_replace(dev);
|
||||||
@@ -613,7 +648,6 @@ mraa_gpio_read(mraa_gpio_context dev)
|
|||||||
|
|
||||||
if (dev->value_fp == -1) {
|
if (dev->value_fp == -1) {
|
||||||
if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) {
|
if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to get value file pointer");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -622,7 +656,7 @@ mraa_gpio_read(mraa_gpio_context dev)
|
|||||||
}
|
}
|
||||||
char bu[2];
|
char bu[2];
|
||||||
if (read(dev->value_fp, bu, 2 * sizeof(char)) != 2) {
|
if (read(dev->value_fp, bu, 2 * sizeof(char)) != 2) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to read a sensible value from sysfs");
|
syslog(LOG_ERR, "gpio%i: read: Failed to read a sensible value from sysfs: %s", dev->pin, strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
lseek(dev->value_fp, 0, SEEK_SET);
|
lseek(dev->value_fp, 0, SEEK_SET);
|
||||||
@@ -633,8 +667,10 @@ mraa_gpio_read(mraa_gpio_context dev)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_write(mraa_gpio_context dev, int value)
|
mraa_gpio_write(mraa_gpio_context dev, int value)
|
||||||
{
|
{
|
||||||
if (dev == NULL)
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: write: context is invalid");
|
||||||
return MRAA_ERROR_INVALID_HANDLE;
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
if (dev->mmap_write != NULL)
|
if (dev->mmap_write != NULL)
|
||||||
return dev->mmap_write(dev, value);
|
return dev->mmap_write(dev, value);
|
||||||
@@ -656,13 +692,15 @@ mraa_gpio_write(mraa_gpio_context dev, int value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
|
if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
syslog(LOG_ERR, "gpio%i: write: Failed to lseek 'value': %s", dev->pin, strerror(errno));
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
char bu[MAX_SIZE];
|
char bu[MAX_SIZE];
|
||||||
int length = snprintf(bu, sizeof(bu), "%d", value);
|
int length = snprintf(bu, sizeof(bu), "%d", value);
|
||||||
if (write(dev->value_fp, bu, length * sizeof(char)) == -1) {
|
if (write(dev->value_fp, bu, length * sizeof(char)) == -1) {
|
||||||
return MRAA_ERROR_INVALID_HANDLE;
|
syslog(LOG_ERR, "gpio%i: write: Failed to write to 'value': %s", dev->pin, strerror(errno));
|
||||||
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_write_post))
|
if (IS_FUNC_DEFINED(dev, gpio_write_post))
|
||||||
@@ -675,16 +713,16 @@ mraa_gpio_unexport_force(mraa_gpio_context dev)
|
|||||||
{
|
{
|
||||||
int unexport = open(SYSFS_CLASS_GPIO "/unexport", O_WRONLY);
|
int unexport = open(SYSFS_CLASS_GPIO "/unexport", O_WRONLY);
|
||||||
if (unexport == -1) {
|
if (unexport == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to open unexport for writing");
|
syslog(LOG_ERR, "gpio%i: Failed to open 'unexport' for writing: %s", dev->pin, strerror(errno));
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_RESOURCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
char bu[MAX_SIZE];
|
char bu[MAX_SIZE];
|
||||||
int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
|
||||||
if (write(unexport, bu, length * sizeof(char)) == -1) {
|
if (write(unexport, bu, length * sizeof(char)) == -1) {
|
||||||
syslog(LOG_ERR, "gpio: Failed to write to unexport");
|
syslog(LOG_ERR, "gpio%i: Failed to write to 'unexport': %s", dev->pin, strerror(errno));
|
||||||
close(unexport);
|
close(unexport);
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(unexport);
|
close(unexport);
|
||||||
@@ -694,10 +732,15 @@ mraa_gpio_unexport_force(mraa_gpio_context dev)
|
|||||||
static mraa_result_t
|
static mraa_result_t
|
||||||
mraa_gpio_unexport(mraa_gpio_context dev)
|
mraa_gpio_unexport(mraa_gpio_context dev)
|
||||||
{
|
{
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: unexport: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
if (dev->owner) {
|
if (dev->owner) {
|
||||||
return mraa_gpio_unexport_force(dev);
|
return mraa_gpio_unexport_force(dev);
|
||||||
}
|
}
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
return MRAA_ERROR_INVALID_PARAMETER;
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa_result_t
|
mraa_result_t
|
||||||
@@ -705,6 +748,11 @@ mraa_gpio_close(mraa_gpio_context dev)
|
|||||||
{
|
{
|
||||||
mraa_result_t result = MRAA_SUCCESS;
|
mraa_result_t result = MRAA_SUCCESS;
|
||||||
|
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: close: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_close_replace)) {
|
if (IS_FUNC_DEFINED(dev, gpio_close_replace)) {
|
||||||
return dev->advance_func->gpio_close_replace(dev);
|
return dev->advance_func->gpio_close_replace(dev);
|
||||||
}
|
}
|
||||||
@@ -726,9 +774,10 @@ mraa_result_t
|
|||||||
mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t own)
|
mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t own)
|
||||||
{
|
{
|
||||||
if (dev == NULL) {
|
if (dev == NULL) {
|
||||||
return MRAA_ERROR_INVALID_RESOURCE;
|
syslog(LOG_ERR, "gpio: owner: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
}
|
}
|
||||||
syslog(LOG_DEBUG, "gpio: Set owner to %d", (int) own);
|
syslog(LOG_DEBUG, "gpio%i: owner: Set owner to %d", dev->pin, (int) own);
|
||||||
dev->owner = own;
|
dev->owner = own;
|
||||||
return MRAA_SUCCESS;
|
return MRAA_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -736,11 +785,16 @@ mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t own)
|
|||||||
mraa_result_t
|
mraa_result_t
|
||||||
mraa_gpio_use_mmaped(mraa_gpio_context dev, mraa_boolean_t mmap_en)
|
mraa_gpio_use_mmaped(mraa_gpio_context dev, mraa_boolean_t mmap_en)
|
||||||
{
|
{
|
||||||
|
if (dev == NULL) {
|
||||||
|
syslog(LOG_ERR, "gpio: use_mmaped: context is invalid");
|
||||||
|
return MRAA_ERROR_INVALID_HANDLE;
|
||||||
|
}
|
||||||
|
|
||||||
if (IS_FUNC_DEFINED(dev, gpio_mmap_setup)) {
|
if (IS_FUNC_DEFINED(dev, gpio_mmap_setup)) {
|
||||||
return dev->advance_func->gpio_mmap_setup(dev, mmap_en);
|
return dev->advance_func->gpio_mmap_setup(dev, mmap_en);
|
||||||
}
|
}
|
||||||
|
|
||||||
syslog(LOG_ERR, "gpio: mmap not implemented on this platform");
|
syslog(LOG_ERR, "gpio%i: use_mmaped: mmap not implemented on this platform", dev->pin);
|
||||||
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -748,7 +802,7 @@ int
|
|||||||
mraa_gpio_get_pin(mraa_gpio_context dev)
|
mraa_gpio_get_pin(mraa_gpio_context dev)
|
||||||
{
|
{
|
||||||
if (dev == NULL) {
|
if (dev == NULL) {
|
||||||
syslog(LOG_ERR, "gpio: context is invalid");
|
syslog(LOG_ERR, "gpio: get_pin: context is invalid");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return dev->phy_pin;
|
return dev->phy_pin;
|
||||||
@@ -758,7 +812,7 @@ int
|
|||||||
mraa_gpio_get_pin_raw(mraa_gpio_context dev)
|
mraa_gpio_get_pin_raw(mraa_gpio_context dev)
|
||||||
{
|
{
|
||||||
if (dev == NULL) {
|
if (dev == NULL) {
|
||||||
syslog(LOG_ERR, "gpio: context is invalid");
|
syslog(LOG_ERR, "gpio: get_pin: context is invalid");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return dev->pin;
|
return dev->pin;
|
||||||
|
|||||||
Reference in New Issue
Block a user