2 #include <aura/private.h>
9 static int file_get(
const char *path,
char *dbuf,
int dlen)
12 int fd =
open(path, O_RDONLY);
14 slog(0, SLOG_ERROR,
"sysfsgpio: Failed to open %s for reading", path);
17 ret = read(fd, dbuf, dlen);
23 static int file_put(
const char *path,
const char *dbuf)
26 int fd =
open(path, O_WRONLY);
28 slog(0, SLOG_ERROR,
"sysfsgpio: Failed to open %s for writing", path);
31 ret = write(fd, dbuf, strlen(dbuf));
36 static int gpio_export(
int gpio)
39 sprintf(tmp,
"/sys/class/gpio/gpio%d", gpio);
40 if (0 == access(tmp, F_OK)) {
41 slog(0, SLOG_WARN,
"sysfsgpio: GPIO %d already exported", gpio);
45 sprintf(tmp,
"%d", gpio);
46 if (0 < file_put(
"/sys/class/gpio/export", tmp))
51 static int check_exported(
int gpio)
55 sprintf(tmp,
"/sys/class/gpio/gpio%d", gpio);
56 if (0 != access(tmp, F_OK)) {
57 slog(0, SLOG_WARN,
"sysfsgpio: GPIO %d not exported, exporting", gpio);
58 if ((ret = gpio_export(gpio)))
65 static int gpio_write(
int gpio,
int value)
70 if ((ret = check_exported(gpio)))
73 sprintf(tmp,
"/sys/class/gpio%d/value", gpio);
74 if (0 < file_put(tmp, value ?
"1" :
"0"))
80 static int gpio_read(
int gpio,
int *value)
85 if ((ret = check_exported(gpio)))
88 sprintf(tmp,
"/sys/class/gpio%d/value", gpio);
89 if (0 < file_get(tmp, tmp, 256))
97 static int gpio_in(
int gpio)
102 if ((ret = check_exported(gpio)))
105 sprintf(tmp,
"/sys/class/gpio%d/direction", gpio);
106 if (0 < file_put(tmp,
"in"))
112 static int gpio_out(
int gpio)
117 if ((ret = check_exported(gpio)))
120 sprintf(tmp,
"/sys/class/gpio%d/direction", gpio);
121 if (0 < file_put(tmp,
"out"))
127 static int gpio_open(
struct aura_node *node,
const char *opts)
129 slog(0, SLOG_INFO,
"Opening sysfs/gpio transport");
130 struct aura_export_table *etbl = aura_etable_create(node, 16);
132 BUG(node,
"Failed to create etable");
133 aura_etable_add(etbl,
"write",
"33",
"");
134 aura_etable_add(etbl,
"read",
"3",
"3");
135 aura_etable_add(etbl,
"out",
"3",
"");
136 aura_etable_add(etbl,
"in",
"3",
"");
137 aura_etable_add(etbl,
"export",
"3",
"3");
138 aura_etable_add(etbl,
"watch",
"3",
"");
140 aura_etable_add(etbl,
"gpio_changed", NULL,
"333");
141 aura_etable_activate(etbl);
143 slog(0, SLOG_INFO,
"Opened sysfs/gpio transport");
147 static void gpio_close(
struct aura_node *node)
149 slog(0, SLOG_INFO,
"Closing dummy transport");
152 #define OPCODE(_name) (strcmp(o->name, _name)==0)
154 static void handle_outbound(
struct aura_node *node,
struct aura_object *o,
struct aura_buffer *buf)
157 if (OPCODE(
"export")) {
159 slog(4, SLOG_DEBUG,
"gpio: export %d", gpio);
160 ret = gpio_export(gpio);
161 }
else if (OPCODE(
"write")) {
164 slog(4, SLOG_DEBUG,
"gpio: write gpio %d value %d", gpio, value);
165 ret = gpio_write(gpio, value);
166 }
else if (OPCODE(
"in")) {
169 }
else if (OPCODE(
"out")) {
171 ret = gpio_out(gpio);
172 }
else if (OPCODE(
"read")) {
174 ret = gpio_read(gpio, &gpio);
178 slog(0, SLOG_DEBUG,
"gpio ret = %d", ret);
186 static void gpio_loop(
struct aura_node *node,
const struct aura_pollfds *fd)
189 struct aura_object *o;
196 handle_outbound(node, o, buf);
209 AURA_TRANSPORT(gpio);
void aura_set_status(struct aura_node *node, int status)
struct aura_buffer * aura_dequeue_buffer(struct list_head *head)
const char * name
Required.
struct aura_eventloop * aura_eventloop_get_data(struct aura_node *node)
void aura_buffer_put_u32(struct aura_buffer *buf, uint32_t value)
Put an unsigned 32 bit integer to aura buffer.
static void aura_buffer_rewind(struct aura_buffer *buf)
uint32_t aura_buffer_get_u32(struct aura_buffer *buf)
Get an unsigned 32 bit integer from aura buffer.
void(* close)(struct aura_node *node)
Required.
void aura_call_fail(struct aura_node *node, struct aura_object *o)
int(* open)(struct aura_node *node, const char *opts)
Required.
void aura_queue_buffer(struct list_head *list, struct aura_buffer *buf)
void aura_buffer_release(struct aura_buffer *buf)
struct aura_object * object