public
Description: Lua-RPC for Lua 5.1.x
Homepage:
Clone URL: git://github.com/jsnyder/luarpc.git
luarpc / luarpc_serial.c
100644 206 lines (162 sloc) 4.223 kb
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#include <alloca.h>
 
/* FIXME: I know not all of the above is necessary, should pare it down sometime */
 
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
 
#include "config.h"
#include "luarpc_rpc.h"
 
#ifdef LUARPC_ENABLE_SERIAL
 
/* Setup Transport */
void transport_init (Transport *tpt)
{
tpt->fd = INVALID_TRANSPORT;
}
 
void transport_open( Transport *tpt, const char *path )
{
struct termios options;
struct exception e;
 
tpt->fd = open(path , O_RDWR | O_NOCTTY | O_NDELAY );
 
if( tpt->fd == INVALID_TRANSPORT)
{
e.errnum = errno;
e.type = fatal;
Throw( e );
}
 
tcgetattr( tpt->fd, &options);
 
cfsetispeed( &options, B115200 );
cfsetospeed( &options, B115200 );
 
options.c_cflag |= (CLOCAL | CREAD);
  
  /* raw processing */
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  options.c_oflag &= ~OPOST;
  
  /* 8N1 */
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  
  options.c_cc[VMIN] = 0;
  options.c_cc[VTIME] = 10;
 
tcsetattr(tpt->fd, TCSANOW, &options);
fcntl(tpt->fd, F_SETFL, 0);
}
 
/* Open Listener / Server */
void transport_open_listener(lua_State *L, ServerHandle *handle)
{
check_num_args (L,2); /* 1st arg is path, 2nd is handle */
  if (!lua_isstring (L,1))
    my_lua_error (L,"first argument must be serial serial port");
 
transport_open( &handle->ltpt, lua_tostring (L,1) );
 
while( transport_readable ( &handle->ltpt ) == 0 ); /* wait for incoming data */
}
 
/* Open Connection / Client */
int transport_open_connection(lua_State *L, Handle *handle)
{
  check_num_args (L,2); /* 1st arg is path, 2nd is handle */
  if (!lua_isstring (L,1))
    my_lua_error (L,"first argument must be serial serial port");
 
transport_open( &handle->tpt, lua_tostring (L,1) );
 
return 1;
}
 
/* Accept Connection */
void transport_accept (Transport *tpt, Transport *atpt)
{
struct exception e;
TRANSPORT_VERIFY_OPEN;
atpt->fd = tpt->fd;
}
 
 
/* Read & Write to Transport */
void transport_read_buffer (Transport *tpt, u8 *buffer, int length)
{
int n;
struct exception e;
TRANSPORT_VERIFY_OPEN;
 
while( length > 0 )
{
TRANSPORT_VERIFY_OPEN;
 
    n = read( tpt->fd, ( void * )buffer, length );
   
/* error handling */
if( n == 0 )
{
e.errnum = ERR_NODATA;
e.type = nonfatal;
Throw( e );
}
 
    if( n < 0 )
{
e.errnum = errno;
e.type = fatal;
Throw( e );
}
   
buffer += n;
    length -= n;
  }
}
 
void transport_write_buffer( Transport *tpt, const u8 *buffer, int length )
{
int n;
struct exception e;
  TRANSPORT_VERIFY_OPEN;
 
n = write( tpt->fd, buffer,length );
 
  if ( n != length )
{
e.errnum = errno;
e.type = fatal;
Throw( e );
}
}
 
/* Check if data is available on connection without reading:
- 1 = data available, 0 = no data available */
int transport_readable (Transport *tpt)
{
struct exception e;
fd_set rdfs;
  int ret, bytes;
  struct timeval tv;
 
  if (tpt->fd == INVALID_TRANSPORT)
return 0;
 
FD_ZERO (&rdfs);
  FD_SET (tpt->fd, &rdfs);
  
/* Wait up to five seconds. */
  tv.tv_sec = 5;
  tv.tv_usec = 0;
 
ret = select( tpt->fd+1, &rdfs, NULL, NULL, &tv );
 
if ( ret < 0 )
{
e.errnum = errno;
e.type = fatal;
Throw( e );
}
 
  return ( ret > 0 );
}
 
/* Check if transport is open:
1 = connection open, 0 = connection closed */
int transport_is_open (Transport *tpt)
{
return (tpt->fd != INVALID_TRANSPORT);
}
 
/* Shut down connection */
void transport_close (Transport *tpt)
{
if (tpt->fd != INVALID_TRANSPORT)
{
/* close (tpt->fd); -- not closing for now since atpt and ltpt use same fd,
should use some method to detect whether one has
already dropped, and properly close out on exit */
 
/* Send break to the other side to indicate to opposing side that connection is ending */
tcsendbreak(tpt->fd, 0);
   tpt->fd = INVALID_TRANSPORT;
}
}
 
#endif /* LUARPC_ENABLE_SERIAL */