diff --git a/api/mraa/gpio.h b/api/mraa/gpio.h index 2168f50..2287c0c 100644 --- a/api/mraa/gpio.h +++ b/api/mraa/gpio.h @@ -154,6 +154,15 @@ mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode); */ mraa_result_t mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir); +/** + * Read Gpio direction + * + * @param dev The Gpio context + * @param dir The address where to store the Gpio direction + * @return Result of operation + */ +mraa_result_t mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir); + /** * Close the Gpio context * - Will free the memory for the context and unexport the Gpio diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index b1a757c..f67f1ab 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -654,6 +654,38 @@ mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir) return MRAA_SUCCESS; } +mraa_result_t +mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir) +{ + char value[5]; + char filepath[MAX_SIZE]; + int fd, rc; + mraa_result_t result = MRAA_SUCCESS; + + snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin); + fd = open(filepath, O_RDONLY); + if (fd == -1) { + return MRAA_ERROR_INVALID_RESOURCE; + } + + memset(value, '\0', sizeof(value)); + rc = read(fd, value, sizeof(value)); + close(fd); + if (rc <= 0) { + return MRAA_ERROR_INVALID_RESOURCE; + } + + if (strcmp(value, "out\n") == 0) { + *dir = MRAA_GPIO_OUT; + } else if (strcmp(value, "in\n") == 0) { + *dir = MRAA_GPIO_IN; + } else { + result = MRAA_ERROR_INVALID_RESOURCE; + } + + return result; +} + int mraa_gpio_read(mraa_gpio_context dev) {