blob: 24d1405314bf84b3671a501ea9b5950475f2591b [file] [log] [blame]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <termios.h>
#include <string.h>
#include <poll.h>
#include "gpio.h"
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF]={0};
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d", gpio);
if (access(buf, F_OK) == 0)
/* already exist */
return 0;
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
printf("gpio/export: %d\n", gpio);
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF]={0};
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
printf("gpio: %d\n", gpio);
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
int fd, len;
char buf[MAX_BUF]={0};
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
(void)len;
fd = open(buf, O_WRONLY);
if (fd < 0) {
printf("gpio: %d, dir: %d\n", gpio, out_flag);
return fd;
}
if (out_flag == GPIO_DIR_OUT)
write(fd, "out", 4);
else
write(fd, "in", 3);
close(fd);
return 0;
}
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF]={0};
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
(void)len;
fd = open(buf, O_WRONLY);
if (fd < 0) {
printf("gpio: %d, val: %d\n", gpio, value);
return fd;
}
if (value)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF]={0};
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
(void)len;
fd = open(buf, O_RDONLY);
if (fd < 0) {
printf("gpio: %d\n", gpio);
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
int gpio_set_edge(unsigned int gpio, const unsigned char *edge)
{
int fd, len;
char buf[MAX_BUF]={0};
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
(void)len;
fd = open(buf, O_WRONLY);
if (fd < 0) {
printf("gpio: %d\n", gpio);
return fd;
}
write(fd, edge, strlen((char *)edge));
close(fd);
return 0;
}
int gpio_value_open(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF]={0};
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
(void)len;
fd = open(buf, O_RDONLY | O_NONBLOCK);
if (fd < 0)
printf("gpio: %d\n", gpio);
return fd;
}
int gpio_value_close(int fd)
{
return close(fd);
}