mraa_result_t mraa_gpio_write(mraa_gpio_context dev, int value) { if (dev == NULL) return MRAA_ERROR_INVALID_HANDLE; if (dev->mmap_write != NULL) return dev->mmap_write(dev,value); if (advance_func->gpio_write_pre != NULL) { mraa_result_t pre_ret = (advance_func->gpio_write_pre(dev,value)); if(pre_ret != MRAA_SUCCESS) return pre_ret; } if (dev->value_fp == -1) { if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) { return MRAA_ERROR_INVALID_RESOURCE; } } if (lseek(dev->value_fp, 0, SEEK_SET) == -1) { return MRAA_ERROR_INVALID_RESOURCE; } char bu[MAX_SIZE]; int length = snprintf(bu, sizeof(bu), "%d", value); if (write(dev->value_fp, bu, length*sizeof(char)) == -1) { return MRAA_ERROR_INVALID_HANDLE; } if (advance_func->gpio_write_post != NULL) return advance_func->gpio_write_post(dev,value); return MRAA_SUCCESS; }
int mraa_gpio_read(mraa_gpio_context dev) { if (dev == NULL) return -1; if (dev->mmap_read != NULL) return dev->mmap_read(dev); if (dev->value_fp == -1) { if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) { syslog(LOG_ERR, "gpio: Failed to get value file pointer"); return -1; } } else { // if value_fp is new this is pointless lseek(dev->value_fp, 0, SEEK_SET); } char bu[2]; if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) { syslog(LOG_ERR, "gpio: Failed to read a sensible value from sysfs"); return -1; } lseek(dev->value_fp, 0, SEEK_SET); return (int) strtol(bu, NULL, 10); }
int mraa_gpio_read(mraa_gpio_context dev) { if (dev == NULL) { syslog(LOG_ERR, "gpio: read: context is invalid"); return -1; } if (IS_FUNC_DEFINED(dev, gpio_read_replace)) return dev->advance_func->gpio_read_replace(dev); if (dev->mmap_read != NULL) return dev->mmap_read(dev); if (dev->value_fp == -1) { if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) { return -1; } } else { // if value_fp is new this is pointless lseek(dev->value_fp, 0, SEEK_SET); } char bu[2]; if (read(dev->value_fp, bu, 2 * sizeof(char)) != 2) { syslog(LOG_ERR, "gpio%i: read: Failed to read a sensible value from sysfs: %s", dev->pin, strerror(errno)); return -1; } lseek(dev->value_fp, 0, SEEK_SET); return (int) strtol(bu, NULL, 10); }
mraa_result_t mraa_gpio_write(mraa_gpio_context dev, int value) { if (dev == NULL) { syslog(LOG_ERR, "gpio: write: context is invalid"); return MRAA_ERROR_INVALID_HANDLE; } if (dev->mmap_write != NULL) return dev->mmap_write(dev, value); if (IS_FUNC_DEFINED(dev, gpio_write_pre)) { mraa_result_t pre_ret = (dev->advance_func->gpio_write_pre(dev, value)); if (pre_ret != MRAA_SUCCESS) return pre_ret; } if (IS_FUNC_DEFINED(dev, gpio_write_replace)) { return dev->advance_func->gpio_write_replace(dev, value); } if (dev->value_fp == -1) { if (mraa_gpio_get_valfp(dev) != MRAA_SUCCESS) { return MRAA_ERROR_INVALID_RESOURCE; } } if (lseek(dev->value_fp, 0, SEEK_SET) == -1) { syslog(LOG_ERR, "gpio%i: write: Failed to lseek 'value': %s", dev->pin, strerror(errno)); return MRAA_ERROR_UNSPECIFIED; } char bu[MAX_SIZE]; int length = snprintf(bu, sizeof(bu), "%d", value); if (write(dev->value_fp, bu, length * sizeof(char)) == -1) { 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)) return dev->advance_func->gpio_write_post(dev, value); return MRAA_SUCCESS; }