2012-02-24 02:12:37 +01:00
|
|
|
/*=========================================================================*\
|
|
|
|
* Serial stream
|
|
|
|
* LuaSocket toolkit
|
|
|
|
\*=========================================================================*/
|
2019-02-26 00:00:38 +01:00
|
|
|
#include "luasocket.h"
|
|
|
|
|
2012-02-24 02:12:37 +01:00
|
|
|
#include "auxiliar.h"
|
|
|
|
#include "socket.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "unix.h"
|
2019-02-28 05:00:38 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
2015-08-21 20:39:34 +02:00
|
|
|
#include <sys/un.h>
|
2012-02-24 02:12:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Reuses userdata definition from unix.h, since it is useful for all
|
|
|
|
stream-like objects.
|
|
|
|
|
|
|
|
If we stored the serial path for use in error messages or userdata
|
|
|
|
printing, we might need our own userdata definition.
|
|
|
|
|
|
|
|
Group usage is semi-inherited from unix.c, but unnecessary since we
|
|
|
|
have only one object type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Internal function prototypes
|
|
|
|
\*=========================================================================*/
|
|
|
|
static int global_create(lua_State *L);
|
|
|
|
static int meth_send(lua_State *L);
|
|
|
|
static int meth_receive(lua_State *L);
|
|
|
|
static int meth_close(lua_State *L);
|
|
|
|
static int meth_settimeout(lua_State *L);
|
|
|
|
static int meth_getfd(lua_State *L);
|
|
|
|
static int meth_setfd(lua_State *L);
|
|
|
|
static int meth_dirty(lua_State *L);
|
|
|
|
static int meth_getstats(lua_State *L);
|
|
|
|
static int meth_setstats(lua_State *L);
|
|
|
|
|
|
|
|
/* serial object methods */
|
2012-04-11 23:18:20 +02:00
|
|
|
static luaL_Reg serial_methods[] = {
|
2012-02-24 02:12:37 +01:00
|
|
|
{"__gc", meth_close},
|
|
|
|
{"__tostring", auxiliar_tostring},
|
|
|
|
{"close", meth_close},
|
|
|
|
{"dirty", meth_dirty},
|
|
|
|
{"getfd", meth_getfd},
|
|
|
|
{"getstats", meth_getstats},
|
|
|
|
{"setstats", meth_setstats},
|
|
|
|
{"receive", meth_receive},
|
|
|
|
{"send", meth_send},
|
|
|
|
{"setfd", meth_setfd},
|
|
|
|
{"settimeout", meth_settimeout},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Initializes module
|
|
|
|
\*-------------------------------------------------------------------------*/
|
2012-04-11 23:18:20 +02:00
|
|
|
LUASOCKET_API int luaopen_socket_serial(lua_State *L) {
|
2012-02-24 02:12:37 +01:00
|
|
|
/* create classes */
|
2012-04-11 23:18:20 +02:00
|
|
|
auxiliar_newclass(L, "serial{client}", serial_methods);
|
2012-02-24 02:12:37 +01:00
|
|
|
/* create class groups */
|
|
|
|
auxiliar_add2group(L, "serial{client}", "serial{any}");
|
2013-05-29 10:56:56 +02:00
|
|
|
lua_pushcfunction(L, global_create);
|
2012-02-24 02:12:37 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Lua methods
|
|
|
|
\*=========================================================================*/
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Just call buffered IO methods
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int meth_send(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1);
|
|
|
|
return buffer_meth_send(L, &un->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int meth_receive(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1);
|
|
|
|
return buffer_meth_receive(L, &un->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int meth_getstats(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1);
|
|
|
|
return buffer_meth_getstats(L, &un->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int meth_setstats(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1);
|
|
|
|
return buffer_meth_setstats(L, &un->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Select support methods
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int meth_getfd(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1);
|
|
|
|
lua_pushnumber(L, (int) un->sock);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is very dangerous, but can be handy for those that are brave enough */
|
|
|
|
static int meth_setfd(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1);
|
2015-08-21 20:39:34 +02:00
|
|
|
un->sock = (t_socket) luaL_checknumber(L, 2);
|
2012-02-24 02:12:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int meth_dirty(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1);
|
|
|
|
lua_pushboolean(L, !buffer_isempty(&un->buf));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* Closes socket used by object
|
2012-02-24 02:12:37 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int meth_close(lua_State *L)
|
|
|
|
{
|
|
|
|
p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1);
|
|
|
|
socket_destroy(&un->sock);
|
|
|
|
lua_pushnumber(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
|
|
|
* Just call tm methods
|
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int meth_settimeout(lua_State *L) {
|
|
|
|
p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1);
|
|
|
|
return timeout_meth_settimeout(L, &un->tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*=========================================================================*\
|
|
|
|
* Library functions
|
|
|
|
\*=========================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*\
|
2015-08-21 20:39:34 +02:00
|
|
|
* Creates a serial object
|
2012-02-24 02:12:37 +01:00
|
|
|
\*-------------------------------------------------------------------------*/
|
|
|
|
static int global_create(lua_State *L) {
|
|
|
|
const char* path = luaL_checkstring(L, 1);
|
|
|
|
|
|
|
|
/* allocate unix object */
|
|
|
|
p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix));
|
|
|
|
|
|
|
|
/* open serial device */
|
|
|
|
t_socket sock = open(path, O_NOCTTY|O_RDWR);
|
|
|
|
|
|
|
|
/*printf("open %s on %d\n", path, sock);*/
|
|
|
|
|
|
|
|
if (sock < 0) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_pushstring(L, socket_strerror(errno));
|
|
|
|
lua_pushnumber(L, errno);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
/* set its type as client object */
|
|
|
|
auxiliar_setclass(L, "serial{client}", -1);
|
|
|
|
/* initialize remaining structure fields */
|
|
|
|
socket_setnonblocking(&sock);
|
|
|
|
un->sock = sock;
|
2015-08-21 20:39:34 +02:00
|
|
|
io_init(&un->io, (p_send) socket_write, (p_recv) socket_read,
|
2012-02-24 02:12:37 +01:00
|
|
|
(p_error) socket_ioerror, &un->sock);
|
|
|
|
timeout_init(&un->tm, -1, -1);
|
|
|
|
buffer_init(&un->buf, &un->io, &un->tm);
|
|
|
|
return 1;
|
|
|
|
}
|