1*0a6a1f1dSLionel Sambuc /* $NetBSD: gpio.c,v 1.9 2014/07/19 18:38:34 lneto Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
411be35a1SLionel Sambuc * Copyright (c) 2011 Marc Balmer <marc@msys.ch>
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1711be35a1SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1811be35a1SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1911be35a1SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2011be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2111be35a1SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2211be35a1SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2311be35a1SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2411be35a1SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2511be35a1SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2611be35a1SLionel Sambuc */
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambuc /* GPIO interface for Lua */
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #include <errno.h>
3111be35a1SLionel Sambuc #include <fcntl.h>
3211be35a1SLionel Sambuc #include <stdarg.h>
3311be35a1SLionel Sambuc #include <stdio.h>
3411be35a1SLionel Sambuc #include <string.h>
3511be35a1SLionel Sambuc #include <ctype.h>
3611be35a1SLionel Sambuc #include <stdlib.h>
3711be35a1SLionel Sambuc #include <unistd.h>
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambuc #include <lua.h>
4011be35a1SLionel Sambuc #include <lauxlib.h>
4111be35a1SLionel Sambuc #include <lualib.h>
4211be35a1SLionel Sambuc
4311be35a1SLionel Sambuc #include <sys/gpio.h>
4411be35a1SLionel Sambuc #include <sys/ioctl.h>
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc #define GPIO_METATABLE "GPIO object methods"
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc static __printflike(2, 3) void
gpio_error(lua_State * L,const char * fmt,...)4911be35a1SLionel Sambuc gpio_error(lua_State *L, const char *fmt, ...)
5011be35a1SLionel Sambuc {
5111be35a1SLionel Sambuc va_list ap;
5211be35a1SLionel Sambuc int len;
5311be35a1SLionel Sambuc char *msg;
5411be35a1SLionel Sambuc
5511be35a1SLionel Sambuc va_start(ap, fmt);
5611be35a1SLionel Sambuc len = vasprintf(&msg, fmt, ap);
5711be35a1SLionel Sambuc va_end(ap);
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc if (len != -1) {
6011be35a1SLionel Sambuc lua_pushstring(L, msg);
6111be35a1SLionel Sambuc free(msg);
6211be35a1SLionel Sambuc } else
6311be35a1SLionel Sambuc lua_pushstring(L, "vasprintf failed");
6411be35a1SLionel Sambuc lua_error(L);
6511be35a1SLionel Sambuc }
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc static int
gpio_open(lua_State * L)6811be35a1SLionel Sambuc gpio_open(lua_State *L)
6911be35a1SLionel Sambuc {
7011be35a1SLionel Sambuc int *fd;
7111be35a1SLionel Sambuc
7211be35a1SLionel Sambuc fd = lua_newuserdata(L, sizeof(int));
7311be35a1SLionel Sambuc *fd = open(luaL_checkstring(L, -2), O_RDWR);
7411be35a1SLionel Sambuc if (*fd == -1) {
7511be35a1SLionel Sambuc gpio_error(L, "%s", strerror(errno));
7611be35a1SLionel Sambuc /* NOTREACHED */
7711be35a1SLionel Sambuc return 0;
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc luaL_getmetatable(L, GPIO_METATABLE);
8011be35a1SLionel Sambuc lua_setmetatable(L, -2);
8111be35a1SLionel Sambuc return 1;
8211be35a1SLionel Sambuc }
8311be35a1SLionel Sambuc
8411be35a1SLionel Sambuc static int
gpio_close(lua_State * L)8511be35a1SLionel Sambuc gpio_close(lua_State *L)
8611be35a1SLionel Sambuc {
8711be35a1SLionel Sambuc int *fd;
8811be35a1SLionel Sambuc
8911be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
9011be35a1SLionel Sambuc if (*fd != -1) {
9111be35a1SLionel Sambuc close(*fd);
9211be35a1SLionel Sambuc *fd = -1;
9311be35a1SLionel Sambuc }
9411be35a1SLionel Sambuc return 0;
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc static int
gpio_info(lua_State * L)9811be35a1SLionel Sambuc gpio_info(lua_State *L)
9911be35a1SLionel Sambuc {
10011be35a1SLionel Sambuc struct gpio_info info;
10111be35a1SLionel Sambuc int *fd;
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
10411be35a1SLionel Sambuc if (ioctl(*fd, GPIOINFO, &info) == -1)
10511be35a1SLionel Sambuc gpio_error(L, "GPIOINFO");
10611be35a1SLionel Sambuc lua_pushinteger(L, info.gpio_npins);
10711be35a1SLionel Sambuc return 1;
10811be35a1SLionel Sambuc }
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc static void
gpio_get_pin(lua_State * L,int n,struct gpio_req * req)11111be35a1SLionel Sambuc gpio_get_pin(lua_State *L, int n, struct gpio_req *req)
11211be35a1SLionel Sambuc {
11311be35a1SLionel Sambuc switch (lua_type(L, n)) {
11411be35a1SLionel Sambuc case LUA_TNUMBER:
11511be35a1SLionel Sambuc req->gp_pin = (int)lua_tointeger(L, n); /* not 1 based! */
11611be35a1SLionel Sambuc break;
11711be35a1SLionel Sambuc case LUA_TSTRING:
11811be35a1SLionel Sambuc strlcpy(req->gp_name, lua_tostring(L, n), sizeof(req->gp_name));
11911be35a1SLionel Sambuc break;
12011be35a1SLionel Sambuc default:
12111be35a1SLionel Sambuc luaL_argerror(L, n, "expected string or integer");
12211be35a1SLionel Sambuc /* NOTREACHED */
12311be35a1SLionel Sambuc }
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc static int
gpio_set(lua_State * L)12711be35a1SLionel Sambuc gpio_set(lua_State *L)
12811be35a1SLionel Sambuc {
12911be35a1SLionel Sambuc struct gpio_set set;
13011be35a1SLionel Sambuc int *fd;
13111be35a1SLionel Sambuc
13211be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
13311be35a1SLionel Sambuc memset(&set, 0, sizeof(set));
13411be35a1SLionel Sambuc gpio_get_pin(L, 2, (void *)&set);
13511be35a1SLionel Sambuc set.gp_flags = (int)luaL_checkinteger(L, 3);
13611be35a1SLionel Sambuc if (ioctl(*fd, GPIOSET, &set) == -1)
13711be35a1SLionel Sambuc gpio_error(L, "GPIOSET");
13811be35a1SLionel Sambuc return 0;
13911be35a1SLionel Sambuc }
14011be35a1SLionel Sambuc
14111be35a1SLionel Sambuc static int
gpio_unset(lua_State * L)14211be35a1SLionel Sambuc gpio_unset(lua_State *L)
14311be35a1SLionel Sambuc {
14411be35a1SLionel Sambuc struct gpio_set set;
14511be35a1SLionel Sambuc int *fd;
14611be35a1SLionel Sambuc
14711be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
14811be35a1SLionel Sambuc memset(&set, 0, sizeof(set));
14911be35a1SLionel Sambuc gpio_get_pin(L, 2, (void *)&set);
15011be35a1SLionel Sambuc if (ioctl(*fd, GPIOUNSET, &set) == -1)
15111be35a1SLionel Sambuc gpio_error(L, "GPIOUNSET");
15211be35a1SLionel Sambuc return 0;
15311be35a1SLionel Sambuc }
15411be35a1SLionel Sambuc
15511be35a1SLionel Sambuc static int
gpio_read(lua_State * L)15611be35a1SLionel Sambuc gpio_read(lua_State *L)
15711be35a1SLionel Sambuc {
15811be35a1SLionel Sambuc struct gpio_req req;
15911be35a1SLionel Sambuc int *fd;
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
16211be35a1SLionel Sambuc memset(&req, 0, sizeof(req));
16311be35a1SLionel Sambuc gpio_get_pin(L, 2, &req);
16411be35a1SLionel Sambuc if (ioctl(*fd, GPIOREAD, &req) == -1)
16511be35a1SLionel Sambuc gpio_error(L, "GPIOREAD");
16611be35a1SLionel Sambuc lua_pushinteger(L, req.gp_value);
16711be35a1SLionel Sambuc return 1;
16811be35a1SLionel Sambuc }
16911be35a1SLionel Sambuc
17011be35a1SLionel Sambuc
17111be35a1SLionel Sambuc static int
gpio_write(lua_State * L)17211be35a1SLionel Sambuc gpio_write(lua_State *L)
17311be35a1SLionel Sambuc {
17411be35a1SLionel Sambuc struct gpio_req req;
17511be35a1SLionel Sambuc int *fd, val;
17611be35a1SLionel Sambuc
17711be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
17811be35a1SLionel Sambuc val = (int)luaL_checkinteger(L, 3);
17911be35a1SLionel Sambuc if (val != GPIO_PIN_HIGH && val != GPIO_PIN_LOW)
18011be35a1SLionel Sambuc gpio_error(L, "%d: invalid value", val);
18111be35a1SLionel Sambuc memset(&req, 0, sizeof(req));
18211be35a1SLionel Sambuc gpio_get_pin(L, 2, &req);
18311be35a1SLionel Sambuc req.gp_value = val;
18411be35a1SLionel Sambuc if (ioctl(*fd, GPIOWRITE, &req) == -1)
18511be35a1SLionel Sambuc gpio_error(L, "GPIOWRITE");
18611be35a1SLionel Sambuc lua_pushinteger(L, req.gp_value);
18711be35a1SLionel Sambuc return 1;
18811be35a1SLionel Sambuc }
18911be35a1SLionel Sambuc
19011be35a1SLionel Sambuc static int
gpio_toggle(lua_State * L)19111be35a1SLionel Sambuc gpio_toggle(lua_State *L)
19211be35a1SLionel Sambuc {
19311be35a1SLionel Sambuc struct gpio_req req;
19411be35a1SLionel Sambuc int *fd;
19511be35a1SLionel Sambuc
19611be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
19711be35a1SLionel Sambuc memset(&req, 0, sizeof(req));
19811be35a1SLionel Sambuc gpio_get_pin(L, 2, &req);
19911be35a1SLionel Sambuc if (ioctl(*fd, GPIOTOGGLE, &req) == -1)
20011be35a1SLionel Sambuc gpio_error(L, "GPIOTOGGLE");
20111be35a1SLionel Sambuc lua_pushinteger(L, req.gp_value);
20211be35a1SLionel Sambuc return 1;
20311be35a1SLionel Sambuc }
20411be35a1SLionel Sambuc
20511be35a1SLionel Sambuc static int
gpio_attach(lua_State * L)20611be35a1SLionel Sambuc gpio_attach(lua_State *L)
20711be35a1SLionel Sambuc {
20811be35a1SLionel Sambuc struct gpio_attach attach;
20911be35a1SLionel Sambuc int *fd;
21011be35a1SLionel Sambuc
21111be35a1SLionel Sambuc fd = luaL_checkudata(L, 1, GPIO_METATABLE);
21211be35a1SLionel Sambuc memset(&attach, 0, sizeof(attach));
21311be35a1SLionel Sambuc strlcpy(attach.ga_dvname, luaL_checkstring(L, 2),
21411be35a1SLionel Sambuc sizeof(attach.ga_dvname));
21511be35a1SLionel Sambuc attach.ga_offset = (int)luaL_checkinteger(L, 3);
21611be35a1SLionel Sambuc attach.ga_mask = (int)luaL_checkinteger(L, 4);
21711be35a1SLionel Sambuc if (lua_gettop(L) > 4)
21811be35a1SLionel Sambuc attach.ga_flags = (int)luaL_checkinteger(L, 5);
21911be35a1SLionel Sambuc else
22011be35a1SLionel Sambuc attach.ga_flags = 0;
22111be35a1SLionel Sambuc
22211be35a1SLionel Sambuc if (ioctl(*fd, GPIOATTACH, &attach) == -1)
22311be35a1SLionel Sambuc gpio_error(L, "GPIOATTACH");
22411be35a1SLionel Sambuc return 0;
22511be35a1SLionel Sambuc }
22611be35a1SLionel Sambuc
22711be35a1SLionel Sambuc struct constant {
22811be35a1SLionel Sambuc const char *name;
22911be35a1SLionel Sambuc int value;
23011be35a1SLionel Sambuc };
23111be35a1SLionel Sambuc
23211be35a1SLionel Sambuc static const struct constant gpio_constant[] = {
23311be35a1SLionel Sambuc /* GPIO pin states */
23411be35a1SLionel Sambuc { "PIN_LOW", GPIO_PIN_LOW },
23511be35a1SLionel Sambuc { "PIN_HIGH", GPIO_PIN_HIGH },
23611be35a1SLionel Sambuc
23711be35a1SLionel Sambuc /* GPIO pin configuration flags */
23811be35a1SLionel Sambuc { "PIN_INPUT", GPIO_PIN_INPUT },
23911be35a1SLionel Sambuc { "PIN_OUTPUT", GPIO_PIN_OUTPUT },
24011be35a1SLionel Sambuc { "PIN_INOUT", GPIO_PIN_INOUT },
24111be35a1SLionel Sambuc { "PIN_OPENDRAIN", GPIO_PIN_OPENDRAIN },
24211be35a1SLionel Sambuc { "PIN_PUSHPULL", GPIO_PIN_PUSHPULL },
24311be35a1SLionel Sambuc { "PIN_TRISTATE", GPIO_PIN_TRISTATE },
24411be35a1SLionel Sambuc { "PIN_PULLUP", GPIO_PIN_PULLUP },
24511be35a1SLionel Sambuc { "PIN_PULLDOWN", GPIO_PIN_PULLDOWN },
24611be35a1SLionel Sambuc { "PIN_INVIN", GPIO_PIN_INVIN },
24711be35a1SLionel Sambuc { "PIN_INVOUT", GPIO_PIN_INVOUT },
24811be35a1SLionel Sambuc { "PIN_USER", GPIO_PIN_USER },
24911be35a1SLionel Sambuc { "PIN_PULSATE", GPIO_PIN_PULSATE },
25011be35a1SLionel Sambuc { "PIN_SET", GPIO_PIN_SET },
25111be35a1SLionel Sambuc { NULL, 0 }
25211be35a1SLionel Sambuc };
25311be35a1SLionel Sambuc
25411be35a1SLionel Sambuc static void
gpio_set_info(lua_State * L)25511be35a1SLionel Sambuc gpio_set_info(lua_State *L)
25611be35a1SLionel Sambuc {
25711be35a1SLionel Sambuc lua_pushliteral(L, "_COPYRIGHT");
25884d9c625SLionel Sambuc lua_pushliteral(L, "Copyright (C) 2011, 2013 Marc Balmer "
25984d9c625SLionel Sambuc "<marc@msys.ch>");
26011be35a1SLionel Sambuc lua_settable(L, -3);
26111be35a1SLionel Sambuc lua_pushliteral(L, "_DESCRIPTION");
26211be35a1SLionel Sambuc lua_pushliteral(L, "GPIO interface for Lua");
26311be35a1SLionel Sambuc lua_settable(L, -3);
26411be35a1SLionel Sambuc lua_pushliteral(L, "_VERSION");
26584d9c625SLionel Sambuc lua_pushliteral(L, "gpio 1.0.2");
26611be35a1SLionel Sambuc lua_settable(L, -3);
26711be35a1SLionel Sambuc }
26811be35a1SLionel Sambuc
26911be35a1SLionel Sambuc int luaopen_gpio(lua_State*);
27011be35a1SLionel Sambuc
27111be35a1SLionel Sambuc int
luaopen_gpio(lua_State * L)27211be35a1SLionel Sambuc luaopen_gpio(lua_State* L)
27311be35a1SLionel Sambuc {
27411be35a1SLionel Sambuc static const struct luaL_Reg methods[] = {
27511be35a1SLionel Sambuc { "open", gpio_open },
27611be35a1SLionel Sambuc { NULL, NULL }
27711be35a1SLionel Sambuc };
27811be35a1SLionel Sambuc static const struct luaL_Reg gpio_methods[] = {
27911be35a1SLionel Sambuc { "info", gpio_info },
28011be35a1SLionel Sambuc { "close", gpio_close },
28111be35a1SLionel Sambuc { "set", gpio_set },
28211be35a1SLionel Sambuc { "unset", gpio_unset },
28311be35a1SLionel Sambuc { "read", gpio_read },
28411be35a1SLionel Sambuc { "write", gpio_write },
28511be35a1SLionel Sambuc { "toggle", gpio_toggle },
28611be35a1SLionel Sambuc { "attach", gpio_attach },
28711be35a1SLionel Sambuc { NULL, NULL }
28811be35a1SLionel Sambuc };
28911be35a1SLionel Sambuc int n;
29011be35a1SLionel Sambuc
291*0a6a1f1dSLionel Sambuc luaL_newlib(L, methods);
292*0a6a1f1dSLionel Sambuc luaL_setfuncs(L, gpio_methods, 0);
29311be35a1SLionel Sambuc gpio_set_info(L);
29411be35a1SLionel Sambuc
29511be35a1SLionel Sambuc /* The gpio metatable */
29611be35a1SLionel Sambuc if (luaL_newmetatable(L, GPIO_METATABLE)) {
297*0a6a1f1dSLionel Sambuc luaL_setfuncs(L, gpio_methods, 0);
29811be35a1SLionel Sambuc
29911be35a1SLionel Sambuc lua_pushliteral(L, "__gc");
30011be35a1SLionel Sambuc lua_pushcfunction(L, gpio_close);
30111be35a1SLionel Sambuc lua_settable(L, -3);
30211be35a1SLionel Sambuc
30311be35a1SLionel Sambuc lua_pushliteral(L, "__index");
30411be35a1SLionel Sambuc lua_pushvalue(L, -2);
30511be35a1SLionel Sambuc lua_settable(L, -3);
30611be35a1SLionel Sambuc
30711be35a1SLionel Sambuc lua_pushliteral(L, "__metatable");
30811be35a1SLionel Sambuc lua_pushliteral(L, "must not access this metatable");
30911be35a1SLionel Sambuc lua_settable(L, -3);
31011be35a1SLionel Sambuc }
31111be35a1SLionel Sambuc lua_pop(L, 1);
31211be35a1SLionel Sambuc
31311be35a1SLionel Sambuc for (n = 0; gpio_constant[n].name != NULL; n++) {
31411be35a1SLionel Sambuc lua_pushinteger(L, gpio_constant[n].value);
31511be35a1SLionel Sambuc lua_setfield(L, -2, gpio_constant[n].name);
31611be35a1SLionel Sambuc };
31711be35a1SLionel Sambuc return 1;
31811be35a1SLionel Sambuc }
319