BBIO_err gpio_unexport(unsigned int gpio)
{
    int fd, len;
    char str_gpio[10];

    //If gpio is not exported by us - no need to do anything
    if (exported_gpios[gpio] != GPIO_EXPORTED)
        return 0;
    //close gpio pin file descriptor
    close_value_fd(gpio);

#define GPIO_UNEXPORT "/sys/class/gpio/unexport"

    if ((fd = open(GPIO_UNEXPORT, O_WRONLY)) < 0) {
      syslog(LOG_ERR, "Adafruit_BBIO: gpio_unexport(): %u couldn't open '"GPIO_UNEXPORT"': %i-%s",
             gpio, errno, strerror(errno));
      return BBIO_SYSFS;
    }

    len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
    int ret = write(fd, str_gpio, len);
    close(fd);
    if (ret < 0) {
      syslog(LOG_ERR, "Adafruit_BBIO: gpio_unexport(): %u couldn't write '"GPIO_UNEXPORT"': %i-%s",
             gpio, errno, strerror(errno));
      return BBIO_SYSFS;
    }

    // remove from list
    exported_gpios[gpio] = GPIO_NOT_EXPORTED;

    syslog(LOG_DEBUG, "Adafruit_BBIO: gpio_unexport(): %u OK", gpio);
    return BBIO_OK;
}
int gpio_unexport(unsigned int gpio)
{
    int fd, len;
    char str_gpio[3];
    struct gpio_exp *g, *temp, *prev_g = NULL;

    close_value_fd(gpio);

    if ((fd = open("/sys/class/gpio/unexport", O_WRONLY)) < 0)
        return -1;

    len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio);
    write(fd, str_gpio, len);
    close(fd);

    // remove from list
    g = exported_gpios;
    while (g != NULL)
    {
        if (g->gpio == gpio)
        {
            if (prev_g == NULL)
                exported_gpios = g->next;
            else
                prev_g->next = g->next;
            temp = g;
            g = g->next;
            free(temp);
        } else {
            prev_g = g;
            g = g->next;
        }
    }
        return 0;
}
Esempio n. 3
0
void remove_edge_detect(unsigned int gpio)
{
    struct epoll_event ev;
    int fd = fd_lookup(gpio);

    // delete callbacks for gpio
    remove_callbacks(gpio);

    // delete epoll of fd
    epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);

    // close fd and remove from list
    close_value_fd(gpio);

    // set edge to none
    gpio_set_edge(gpio, NO_EDGE);

    // unexport gpio
    gpio_unexport(gpio);

    // clear detected flag
    event_occurred[gpio] = 0;
}