1*4ab4902eSlneto /* $NetBSD: gpio.c,v 1.9 2014/07/19 18:38:34 lneto Exp $ */
266e9bcb4Smbalmer
315918852Smbalmer /*
415918852Smbalmer * Copyright (c) 2011 Marc Balmer <marc@msys.ch>
515918852Smbalmer * All rights reserved.
615918852Smbalmer *
715918852Smbalmer * Redistribution and use in source and binary forms, with or without
815918852Smbalmer * modification, are permitted provided that the following conditions
915918852Smbalmer * are met:
1015918852Smbalmer * 1. Redistributions of source code must retain the above copyright
1115918852Smbalmer * notice, this list of conditions and the following disclaimer.
1215918852Smbalmer * 2. Redistributions in binary form must reproduce the above copyright
1315918852Smbalmer * notice, this list of conditions and the following disclaimer in the
1415918852Smbalmer * documentation and/or other materials provided with the distribution.
1515918852Smbalmer *
1615918852Smbalmer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1715918852Smbalmer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1815918852Smbalmer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1915918852Smbalmer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2015918852Smbalmer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2115918852Smbalmer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2215918852Smbalmer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2315918852Smbalmer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2415918852Smbalmer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2515918852Smbalmer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2615918852Smbalmer */
2715918852Smbalmer
2815918852Smbalmer /* GPIO interface for Lua */
2915918852Smbalmer
3015918852Smbalmer #include <errno.h>
3115918852Smbalmer #include <fcntl.h>
3215918852Smbalmer #include <stdarg.h>
3315918852Smbalmer #include <stdio.h>
3415918852Smbalmer #include <string.h>
3515918852Smbalmer #include <ctype.h>
3615918852Smbalmer #include <stdlib.h>
3715918852Smbalmer #include <unistd.h>
3815918852Smbalmer
3915918852Smbalmer #include <lua.h>
4015918852Smbalmer #include <lauxlib.h>
4115918852Smbalmer #include <lualib.h>
4215918852Smbalmer
4315918852Smbalmer #include <sys/gpio.h>
4415918852Smbalmer #include <sys/ioctl.h>
4515918852Smbalmer
4615918852Smbalmer #define GPIO_METATABLE "GPIO object methods"
4715918852Smbalmer
4866dd2755Sjoerg static __printflike(2, 3) void
gpio_error(lua_State * L,const char * fmt,...)4915918852Smbalmer gpio_error(lua_State *L, const char *fmt, ...)
5015918852Smbalmer {
5115918852Smbalmer va_list ap;
5215918852Smbalmer int len;
5315918852Smbalmer char *msg;
5415918852Smbalmer
5515918852Smbalmer va_start(ap, fmt);
5615918852Smbalmer len = vasprintf(&msg, fmt, ap);
5715918852Smbalmer va_end(ap);
5815918852Smbalmer
5915918852Smbalmer if (len != -1) {
6015918852Smbalmer lua_pushstring(L, msg);
6115918852Smbalmer free(msg);
6215918852Smbalmer } else
6315918852Smbalmer lua_pushstring(L, "vasprintf failed");
6415918852Smbalmer lua_error(L);
6515918852Smbalmer }
6615918852Smbalmer
6715918852Smbalmer static int
gpio_open(lua_State * L)6815918852Smbalmer gpio_open(lua_State *L)
6915918852Smbalmer {
7015918852Smbalmer int *fd;
7115918852Smbalmer
7215918852Smbalmer fd = lua_newuserdata(L, sizeof(int));
7315918852Smbalmer *fd = open(luaL_checkstring(L, -2), O_RDWR);
7415918852Smbalmer if (*fd == -1) {
7515918852Smbalmer gpio_error(L, "%s", strerror(errno));
7615918852Smbalmer /* NOTREACHED */
7715918852Smbalmer return 0;
7815918852Smbalmer }
7915918852Smbalmer luaL_getmetatable(L, GPIO_METATABLE);
8015918852Smbalmer lua_setmetatable(L, -2);
8115918852Smbalmer return 1;
8215918852Smbalmer }
8315918852Smbalmer
8415918852Smbalmer static int
gpio_close(lua_State * L)8515918852Smbalmer gpio_close(lua_State *L)
8615918852Smbalmer {
8715918852Smbalmer int *fd;
8815918852Smbalmer
8915918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
9015918852Smbalmer if (*fd != -1) {
9115918852Smbalmer close(*fd);
9215918852Smbalmer *fd = -1;
9315918852Smbalmer }
9415918852Smbalmer return 0;
9515918852Smbalmer }
9615918852Smbalmer
9715918852Smbalmer static int
gpio_info(lua_State * L)9815918852Smbalmer gpio_info(lua_State *L)
9915918852Smbalmer {
10015918852Smbalmer struct gpio_info info;
10115918852Smbalmer int *fd;
10215918852Smbalmer
10315918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
10415918852Smbalmer if (ioctl(*fd, GPIOINFO, &info) == -1)
10515918852Smbalmer gpio_error(L, "GPIOINFO");
10615918852Smbalmer lua_pushinteger(L, info.gpio_npins);
10715918852Smbalmer return 1;
10815918852Smbalmer }
10915918852Smbalmer
11015918852Smbalmer static void
gpio_get_pin(lua_State * L,int n,struct gpio_req * req)11115918852Smbalmer gpio_get_pin(lua_State *L, int n, struct gpio_req *req)
11215918852Smbalmer {
11315918852Smbalmer switch (lua_type(L, n)) {
11415918852Smbalmer case LUA_TNUMBER:
115f6606034Smbalmer req->gp_pin = (int)lua_tointeger(L, n); /* not 1 based! */
11615918852Smbalmer break;
11715918852Smbalmer case LUA_TSTRING:
11815918852Smbalmer strlcpy(req->gp_name, lua_tostring(L, n), sizeof(req->gp_name));
11915918852Smbalmer break;
12015918852Smbalmer default:
12115918852Smbalmer luaL_argerror(L, n, "expected string or integer");
12215918852Smbalmer /* NOTREACHED */
12315918852Smbalmer }
12415918852Smbalmer }
12515918852Smbalmer
12615918852Smbalmer static int
gpio_set(lua_State * L)12715918852Smbalmer gpio_set(lua_State *L)
12815918852Smbalmer {
12915918852Smbalmer struct gpio_set set;
13015918852Smbalmer int *fd;
13115918852Smbalmer
13215918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
13315918852Smbalmer memset(&set, 0, sizeof(set));
134974dae71Schristos gpio_get_pin(L, 2, (void *)&set);
135974dae71Schristos set.gp_flags = (int)luaL_checkinteger(L, 3);
13615918852Smbalmer if (ioctl(*fd, GPIOSET, &set) == -1)
13715918852Smbalmer gpio_error(L, "GPIOSET");
13815918852Smbalmer return 0;
13915918852Smbalmer }
14015918852Smbalmer
14115918852Smbalmer static int
gpio_unset(lua_State * L)14215918852Smbalmer gpio_unset(lua_State *L)
14315918852Smbalmer {
14415918852Smbalmer struct gpio_set set;
14515918852Smbalmer int *fd;
14615918852Smbalmer
14715918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
14815918852Smbalmer memset(&set, 0, sizeof(set));
149974dae71Schristos gpio_get_pin(L, 2, (void *)&set);
15015918852Smbalmer if (ioctl(*fd, GPIOUNSET, &set) == -1)
15115918852Smbalmer gpio_error(L, "GPIOUNSET");
15215918852Smbalmer return 0;
15315918852Smbalmer }
15415918852Smbalmer
15515918852Smbalmer static int
gpio_read(lua_State * L)15615918852Smbalmer gpio_read(lua_State *L)
15715918852Smbalmer {
15815918852Smbalmer struct gpio_req req;
15915918852Smbalmer int *fd;
16015918852Smbalmer
16115918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
16215918852Smbalmer memset(&req, 0, sizeof(req));
16315918852Smbalmer gpio_get_pin(L, 2, &req);
16415918852Smbalmer if (ioctl(*fd, GPIOREAD, &req) == -1)
16515918852Smbalmer gpio_error(L, "GPIOREAD");
16615918852Smbalmer lua_pushinteger(L, req.gp_value);
16715918852Smbalmer return 1;
16815918852Smbalmer }
16915918852Smbalmer
17015918852Smbalmer
17115918852Smbalmer static int
gpio_write(lua_State * L)17215918852Smbalmer gpio_write(lua_State *L)
17315918852Smbalmer {
17415918852Smbalmer struct gpio_req req;
17515918852Smbalmer int *fd, val;
17615918852Smbalmer
17715918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
178974dae71Schristos val = (int)luaL_checkinteger(L, 3);
17915918852Smbalmer if (val != GPIO_PIN_HIGH && val != GPIO_PIN_LOW)
18015918852Smbalmer gpio_error(L, "%d: invalid value", val);
18115918852Smbalmer memset(&req, 0, sizeof(req));
18215918852Smbalmer gpio_get_pin(L, 2, &req);
18315918852Smbalmer req.gp_value = val;
18415918852Smbalmer if (ioctl(*fd, GPIOWRITE, &req) == -1)
18515918852Smbalmer gpio_error(L, "GPIOWRITE");
18615918852Smbalmer lua_pushinteger(L, req.gp_value);
18715918852Smbalmer return 1;
18815918852Smbalmer }
18915918852Smbalmer
19015918852Smbalmer static int
gpio_toggle(lua_State * L)19115918852Smbalmer gpio_toggle(lua_State *L)
19215918852Smbalmer {
19315918852Smbalmer struct gpio_req req;
194974dae71Schristos int *fd;
19515918852Smbalmer
19615918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
19715918852Smbalmer memset(&req, 0, sizeof(req));
19815918852Smbalmer gpio_get_pin(L, 2, &req);
19915918852Smbalmer if (ioctl(*fd, GPIOTOGGLE, &req) == -1)
20015918852Smbalmer gpio_error(L, "GPIOTOGGLE");
20115918852Smbalmer lua_pushinteger(L, req.gp_value);
20215918852Smbalmer return 1;
20315918852Smbalmer }
20415918852Smbalmer
20515918852Smbalmer static int
gpio_attach(lua_State * L)20615918852Smbalmer gpio_attach(lua_State *L)
20715918852Smbalmer {
20815918852Smbalmer struct gpio_attach attach;
20915918852Smbalmer int *fd;
21015918852Smbalmer
21115918852Smbalmer fd = luaL_checkudata(L, 1, GPIO_METATABLE);
21215918852Smbalmer memset(&attach, 0, sizeof(attach));
21315918852Smbalmer strlcpy(attach.ga_dvname, luaL_checkstring(L, 2),
21415918852Smbalmer sizeof(attach.ga_dvname));
215974dae71Schristos attach.ga_offset = (int)luaL_checkinteger(L, 3);
216974dae71Schristos attach.ga_mask = (int)luaL_checkinteger(L, 4);
21715918852Smbalmer if (lua_gettop(L) > 4)
218974dae71Schristos attach.ga_flags = (int)luaL_checkinteger(L, 5);
21915918852Smbalmer else
22015918852Smbalmer attach.ga_flags = 0;
22115918852Smbalmer
22215918852Smbalmer if (ioctl(*fd, GPIOATTACH, &attach) == -1)
22315918852Smbalmer gpio_error(L, "GPIOATTACH");
22415918852Smbalmer return 0;
22515918852Smbalmer }
22615918852Smbalmer
22715918852Smbalmer struct constant {
228974dae71Schristos const char *name;
22915918852Smbalmer int value;
23015918852Smbalmer };
23115918852Smbalmer
232974dae71Schristos static const struct constant gpio_constant[] = {
23315918852Smbalmer /* GPIO pin states */
23415918852Smbalmer { "PIN_LOW", GPIO_PIN_LOW },
23515918852Smbalmer { "PIN_HIGH", GPIO_PIN_HIGH },
23615918852Smbalmer
23715918852Smbalmer /* GPIO pin configuration flags */
23815918852Smbalmer { "PIN_INPUT", GPIO_PIN_INPUT },
23915918852Smbalmer { "PIN_OUTPUT", GPIO_PIN_OUTPUT },
24015918852Smbalmer { "PIN_INOUT", GPIO_PIN_INOUT },
24115918852Smbalmer { "PIN_OPENDRAIN", GPIO_PIN_OPENDRAIN },
24215918852Smbalmer { "PIN_PUSHPULL", GPIO_PIN_PUSHPULL },
24315918852Smbalmer { "PIN_TRISTATE", GPIO_PIN_TRISTATE },
24415918852Smbalmer { "PIN_PULLUP", GPIO_PIN_PULLUP },
24515918852Smbalmer { "PIN_PULLDOWN", GPIO_PIN_PULLDOWN },
24615918852Smbalmer { "PIN_INVIN", GPIO_PIN_INVIN },
24715918852Smbalmer { "PIN_INVOUT", GPIO_PIN_INVOUT },
24815918852Smbalmer { "PIN_USER", GPIO_PIN_USER },
24915918852Smbalmer { "PIN_PULSATE", GPIO_PIN_PULSATE },
25015918852Smbalmer { "PIN_SET", GPIO_PIN_SET },
25115918852Smbalmer { NULL, 0 }
25215918852Smbalmer };
25315918852Smbalmer
25415918852Smbalmer static void
gpio_set_info(lua_State * L)25515918852Smbalmer gpio_set_info(lua_State *L)
25615918852Smbalmer {
25715918852Smbalmer lua_pushliteral(L, "_COPYRIGHT");
25836e5572bSmbalmer lua_pushliteral(L, "Copyright (C) 2011, 2013 Marc Balmer "
25936e5572bSmbalmer "<marc@msys.ch>");
26015918852Smbalmer lua_settable(L, -3);
26115918852Smbalmer lua_pushliteral(L, "_DESCRIPTION");
26215918852Smbalmer lua_pushliteral(L, "GPIO interface for Lua");
26315918852Smbalmer lua_settable(L, -3);
26415918852Smbalmer lua_pushliteral(L, "_VERSION");
26536e5572bSmbalmer lua_pushliteral(L, "gpio 1.0.2");
26615918852Smbalmer lua_settable(L, -3);
26715918852Smbalmer }
26815918852Smbalmer
269974dae71Schristos int luaopen_gpio(lua_State*);
270974dae71Schristos
27115918852Smbalmer int
luaopen_gpio(lua_State * L)27215918852Smbalmer luaopen_gpio(lua_State* L)
27315918852Smbalmer {
27415918852Smbalmer static const struct luaL_Reg methods[] = {
27515918852Smbalmer { "open", gpio_open },
27615918852Smbalmer { NULL, NULL }
27715918852Smbalmer };
27815918852Smbalmer static const struct luaL_Reg gpio_methods[] = {
27915918852Smbalmer { "info", gpio_info },
28015918852Smbalmer { "close", gpio_close },
28115918852Smbalmer { "set", gpio_set },
28215918852Smbalmer { "unset", gpio_unset },
28315918852Smbalmer { "read", gpio_read },
28415918852Smbalmer { "write", gpio_write },
28515918852Smbalmer { "toggle", gpio_toggle },
28615918852Smbalmer { "attach", gpio_attach },
28715918852Smbalmer { NULL, NULL }
28815918852Smbalmer };
28915918852Smbalmer int n;
29015918852Smbalmer
291*4ab4902eSlneto luaL_newlib(L, methods);
292*4ab4902eSlneto luaL_setfuncs(L, gpio_methods, 0);
29315918852Smbalmer gpio_set_info(L);
29415918852Smbalmer
29515918852Smbalmer /* The gpio metatable */
29615918852Smbalmer if (luaL_newmetatable(L, GPIO_METATABLE)) {
297*4ab4902eSlneto luaL_setfuncs(L, gpio_methods, 0);
29815918852Smbalmer
29915918852Smbalmer lua_pushliteral(L, "__gc");
30015918852Smbalmer lua_pushcfunction(L, gpio_close);
30115918852Smbalmer lua_settable(L, -3);
30215918852Smbalmer
30315918852Smbalmer lua_pushliteral(L, "__index");
30415918852Smbalmer lua_pushvalue(L, -2);
30515918852Smbalmer lua_settable(L, -3);
30615918852Smbalmer
30715918852Smbalmer lua_pushliteral(L, "__metatable");
30815918852Smbalmer lua_pushliteral(L, "must not access this metatable");
30915918852Smbalmer lua_settable(L, -3);
31015918852Smbalmer }
31115918852Smbalmer lua_pop(L, 1);
31215918852Smbalmer
31315918852Smbalmer for (n = 0; gpio_constant[n].name != NULL; n++) {
31415918852Smbalmer lua_pushinteger(L, gpio_constant[n].value);
31515918852Smbalmer lua_setfield(L, -2, gpio_constant[n].name);
31615918852Smbalmer };
31715918852Smbalmer return 1;
31815918852Smbalmer }
319