-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluars232.c
469 lines (411 loc) · 11.9 KB
/
luars232.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright (c) 2011 Petr Stetiar <[email protected]>, Gaben Ltd.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <lauxlib.h>
#include <lua.h>
#include <string.h>
#include <stdlib.h>
#include "rs232.h"
#define MODULE_TIMESTAMP __DATE__ " " __TIME__
#define MODULE_NAMESPACE "luars232"
#define MODULE_VERSION "1.0.3"
#define MODULE_BUILD "$Id: luars232.c 15 2011-02-23 09:02:20Z sp $"
#define MODULE_COPYRIGHT "Copyright (c) 2011 Petr Stetiar <[email protected]>, Gaben Ltd."
static struct {
const char *name;
unsigned long value;
} luars232_ulong_consts[] = {
/* baudrates */
{ "RS232_BAUD_300", RS232_BAUD_300 },
{ "RS232_BAUD_2400", RS232_BAUD_2400 },
{ "RS232_BAUD_4800", RS232_BAUD_4800 },
{ "RS232_BAUD_9600", RS232_BAUD_9600 },
{ "RS232_BAUD_19200", RS232_BAUD_19200 },
{ "RS232_BAUD_38400", RS232_BAUD_38400 },
{ "RS232_BAUD_57600", RS232_BAUD_57600 },
{ "RS232_BAUD_115200", RS232_BAUD_115200 },
{ "RS232_BAUD_460800", RS232_BAUD_460800 },
/* databits */
{ "RS232_DATA_5", RS232_DATA_5 },
{ "RS232_DATA_6", RS232_DATA_6 },
{ "RS232_DATA_7", RS232_DATA_7 },
{ "RS232_DATA_8", RS232_DATA_8 },
/* stop bits */
{ "RS232_STOP_1", RS232_STOP_1 },
{ "RS232_STOP_2", RS232_STOP_2 },
/* parity */
{ "RS232_PARITY_NONE", RS232_PARITY_NONE },
{ "RS232_PARITY_ODD", RS232_PARITY_ODD },
{ "RS232_PARITY_EVEN", RS232_PARITY_EVEN },
/* flow */
{ "RS232_FLOW_OFF", RS232_FLOW_OFF },
{ "RS232_FLOW_HW", RS232_FLOW_HW },
{ "RS232_FLOW_XON_XOFF", RS232_FLOW_XON_XOFF },
/* DTR and RTS */
{ "RS232_DTR_ON", RS232_DTR_ON },
{ "RS232_DTR_OFF", RS232_DTR_OFF },
{ "RS232_RTS_ON", RS232_RTS_ON },
{ "RS232_RTS_OFF", RS232_RTS_OFF },
/* errors */
{ "RS232_ERR_NOERROR", RS232_ERR_NOERROR },
{ "RS232_ERR_UNKNOWN", RS232_ERR_UNKNOWN },
{ "RS232_ERR_OPEN", RS232_ERR_OPEN },
{ "RS232_ERR_CLOSE", RS232_ERR_CLOSE },
{ "RS232_ERR_FLUSH", RS232_ERR_FLUSH },
{ "RS232_ERR_CONFIG", RS232_ERR_CONFIG },
{ "RS232_ERR_READ", RS232_ERR_READ },
{ "RS232_ERR_WRITE", RS232_ERR_WRITE },
{ "RS232_ERR_SELECT", RS232_ERR_SELECT },
{ "RS232_ERR_TIMEOUT", RS232_ERR_TIMEOUT },
{ "RS232_ERR_IOCTL", RS232_ERR_IOCTL },
{ "RS232_ERR_PORT_CLOSED", RS232_ERR_PORT_CLOSED },
{ NULL, 0 }
};
#ifdef WIN32
BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
return TRUE;
}
#endif
/*
* rs232 = require("luars232")
* error, port = rs232.open(device)
* error, port = rs232.open("/dev/ttyUSB0")
*/
static int lua_port_open(lua_State *L)
{
int ret = 0;
struct rs232_port_t *p = NULL;
p = rs232_init();
if (p == NULL) {
lua_pushinteger(L, RS232_ERR_CONFIG);
lua_pushnil(L);
return 2;
}
DBG("p=%p \n", (void *)p);
rs232_set_device(p, (char *) luaL_checkstring(L, 1));
ret = rs232_open(p);
if (ret > RS232_ERR_NOERROR) {
free(p->pt);
free(p);
lua_pushinteger(L, ret);
lua_pushnil(L);
return 2;
}
lua_pushinteger(L, RS232_ERR_NOERROR);
lua_pushlightuserdata(L, p);
luaL_getmetatable(L, MODULE_NAMESPACE);
lua_setmetatable(L, -2);
return 2;
}
/*
* error, data, read_len = port:read(max_read_len [[, timeout_ms], forced])
*
* if forced > 0 then read() blocks until 'timeout_ms' or there's 'max_read_len'
* bytes available
*/
static int lua_port_read(lua_State *L)
{
int ret = 0;
int argc = 0;
int forced = 0;
unsigned int timeout = 0;
unsigned int len = 0;
unsigned int bytes_read = 0;
unsigned char *data = NULL;
struct rs232_port_t *p = NULL;
p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
lua_remove(L, 1);
if (p == NULL || !rs232_port_open(p)) {
lua_pushinteger(L, RS232_ERR_PORT_CLOSED);
lua_pushnil(L);
lua_pushinteger(L, 0);
return 3;
}
argc = lua_gettop(L);
switch (argc) {
case 1:
len = (unsigned int) luaL_checkinteger(L, 1);
data = (unsigned char*) malloc(len * sizeof(unsigned char *));
memset(data, 0, len);
ret = rs232_read(p, data, len, &bytes_read);
break;
case 2:
case 3:
len = (unsigned int) luaL_checknumber(L, 1);
data = (unsigned char*) malloc(len * sizeof(unsigned char *));
memset(data, 0, len);
timeout = (unsigned int) luaL_checknumber(L, 2);
forced = luaL_optint(L, 3, 0);
if (forced > 0)
ret = rs232_read_timeout_forced(p, data, len, &bytes_read, timeout);
else
ret = rs232_read_timeout(p, data, len, &bytes_read, timeout);
break;
default:
lua_pushinteger(L, RS232_ERR_UNKNOWN);
lua_pushnil(L);
lua_pushinteger(L, 0);
return 3;
}
DBG("ret=%d hex='%s' bytes_read=%d\n",
ret, rs232_hex_dump(data, bytes_read), bytes_read);
lua_pushinteger(L, ret);
if (bytes_read > 0)
lua_pushlstring(L, (char *) data, bytes_read);
else
lua_pushnil(L);
if (data)
free(data);
lua_pushinteger(L, bytes_read);
return 3;
}
/*
* error, written_len = port:write(data [, timeout_ms])
*/
static int lua_port_write(lua_State *L)
{
int ret = 0;
int argc = 0;
unsigned int timeout = 0;
unsigned int wlen = 0;
size_t len = 0;
const char *data;
struct rs232_port_t *p = NULL;
p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
lua_remove(L, 1);
if (p == NULL || !rs232_port_open(p)) {
lua_pushinteger(L, RS232_ERR_PORT_CLOSED);
lua_pushinteger(L, 0);
return 2;
}
argc = lua_gettop(L);
switch (argc) {
case 1: {
data = luaL_checklstring(L, 1, &len);
ret = rs232_write(p, (unsigned char*) data, (unsigned int) len, &wlen);
break;
}
case 2:
data = luaL_checklstring(L, 1, &len);
timeout = (unsigned int) luaL_checknumber(L, 2);
ret = rs232_write_timeout(p, (unsigned char*) data, (unsigned int) len, &wlen, timeout);
break;
default:
lua_pushinteger(L, RS232_ERR_CONFIG);
lua_pushinteger(L, 0);
return 2;
}
lua_pushinteger(L, ret);
lua_pushinteger(L, wlen);
return 2;
}
/* error = port:close() */
static int lua_port_close(lua_State *L)
{
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
if (p == NULL || !rs232_port_open(p)) {
lua_pushinteger(L, RS232_ERR_PORT_CLOSED);
return 1;
}
lua_pushinteger(L, rs232_close(p));
return 1;
}
/* error = port:flush() */
static int lua_port_flush(lua_State *L)
{
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
if (p == NULL || !rs232_port_open(p)) {
lua_pushinteger(L, RS232_ERR_PORT_CLOSED);
return 1;
}
lua_pushinteger(L, rs232_flush(p));
return 1;
}
/* __tostring metamethod */
static int lua_port_tostring(lua_State *L)
{
const char *ret;
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
if (p == NULL) {
lua_pushnil(L);
return 1;
}
ret = rs232_to_string(p);
if (ret == NULL)
lua_pushnil(L);
else
lua_pushstring(L, ret);
return 1;
}
static int lua_port_device(lua_State *L)
{
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
const char *ret = rs232_get_device(p);
if (ret == NULL)
lua_pushnil(L);
else
lua_pushstring(L, ret);
return 1;
}
static int lua_port_fd(lua_State *L)
{
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE);
lua_pushlightuserdata(L, rs232_fd(p));
return 1;
}
/*
* print(port:error_tostring(error))
*/
static int lua_port_strerror(lua_State *L)
{
const char *ret = rs232_strerror((unsigned int) luaL_checkinteger(L, 1));
if (ret == NULL)
lua_pushnil(L);
else
lua_pushstring(L, ret);
return 1;
}
#define FN_SET_PORT(type) \
static int lua_port_set_##type(lua_State *L) \
{ \
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE); \
lua_pushnumber(L, rs232_set_##type(p, (unsigned int) luaL_checknumber(L, 2))); \
return 1; \
} \
#define FN_GET_PORT(type) \
static int lua_port_get_##type(lua_State *L) \
{ \
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE); \
lua_pushnumber(L, rs232_get_##type(p)); \
return 1; \
}
#define FN_GET_PORT_STRING(type) \
static int lua_port_get_str##type(lua_State *L) \
{ \
struct rs232_port_t *p = (struct rs232_port_t*) luaL_checkudata(L, 1, MODULE_NAMESPACE); \
int param = (int) luaL_optinteger(L, 2, -1); \
const char *ret = rs232_str##type(param == -1 ? rs232_get_##type(p) : (unsigned int) param); \
if (ret == NULL) { \
lua_pushnil(L); \
} else { \
lua_pushstring(L, ret); \
} \
return 1; \
}
FN_SET_PORT(baud)
FN_SET_PORT(data)
FN_SET_PORT(stop)
FN_SET_PORT(parity)
FN_SET_PORT(flow)
FN_SET_PORT(dtr)
FN_SET_PORT(rts)
FN_GET_PORT(baud)
FN_GET_PORT(data)
FN_GET_PORT(stop)
FN_GET_PORT(parity)
FN_GET_PORT(flow)
FN_GET_PORT(dtr)
FN_GET_PORT(rts)
FN_GET_PORT_STRING(baud)
FN_GET_PORT_STRING(data)
FN_GET_PORT_STRING(stop)
FN_GET_PORT_STRING(parity)
FN_GET_PORT_STRING(flow)
FN_GET_PORT_STRING(dtr)
FN_GET_PORT_STRING(rts)
static luaL_Reg port_methods[] = {
{ "__tostring", lua_port_tostring },
{ "__gc", lua_port_close },
{ "read", lua_port_read },
{ "write", lua_port_write },
{ "close", lua_port_close },
{ "flush", lua_port_flush },
{ "device", lua_port_device },
{ "fd", lua_port_fd },
/* baud */
{ "baud_rate", lua_port_get_baud },
{ "baud_rate_tostring", lua_port_get_strbaud },
{ "set_baud_rate", lua_port_set_baud },
/* data */
{ "data_bits", lua_port_get_data },
{ "data_bits_tostring", lua_port_get_strdata },
{ "set_data_bits", lua_port_set_data },
/* stop */
{ "stop_bits", lua_port_get_stop },
{ "stop_bits_tostring", lua_port_get_strstop },
{ "set_stop_bits", lua_port_set_stop },
/* parity */
{ "parity", lua_port_get_parity },
{ "parity_tostring", lua_port_get_strparity },
{ "set_parity", lua_port_set_parity },
/* flow */
{ "flow_control", lua_port_get_flow },
{ "flow_control_tostring", lua_port_get_strflow },
{ "set_flow_control", lua_port_set_flow },
/* dtr */
{ "dtr", lua_port_get_dtr },
{ "dtr_tostring", lua_port_get_strdtr },
{ "set_dtr", lua_port_set_dtr },
/* rts */
{ "rts", lua_port_get_rts },
{ "rts_tostring", lua_port_get_strrts },
{ "set_rts", lua_port_set_rts },
{ NULL, NULL }
};
static luaL_Reg port_functions[] = {
{ "open", lua_port_open },
{ "error_tostring", lua_port_strerror },
{ NULL, NULL }
};
static void create_metatables(lua_State *L, const char *name, const luaL_Reg *methods)
{
luaL_newmetatable(L, name);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, methods);
}
RS232_LIB int luaopen_luars232(lua_State *L);
RS232_LIB int luaopen_luars232(lua_State *L)
{
int i;
create_metatables(L, MODULE_NAMESPACE, port_methods);
luaL_register(L, MODULE_NAMESPACE, port_functions);
for (i = 0; luars232_ulong_consts[i].name != NULL; i++) {
lua_pushstring(L, luars232_ulong_consts[i].name);
lua_pushnumber(L, luars232_ulong_consts[i].value);
lua_settable(L, -3);
}
lua_pushstring(L, MODULE_VERSION);
lua_setfield(L, -2, "_VERSION");
lua_pushstring(L, MODULE_BUILD);
lua_setfield(L, -2, "_BUILD");
lua_pushstring(L, MODULE_TIMESTAMP);
lua_setfield(L, -2, "_TIMESTAMP");
lua_pushstring(L, MODULE_COPYRIGHT);
lua_setfield(L, -2, "_COPYRIGHT");
DBG("[*] luaopen_luars232(Version: '%s' Build: '%s' TimeStamp: '%s')\n",
MODULE_VERSION, MODULE_BUILD, MODULE_TIMESTAMP);
return 0;
}