-
Notifications
You must be signed in to change notification settings - Fork 11
/
luarpc_serial.c
170 lines (134 loc) · 3.24 KB
/
luarpc_serial.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WIN32_BUILD
#include <malloc.h>
#else
#include <alloca.h>
#endif
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "platform_conf.h"
#include "luarpc_rpc.h"
#include "serial.h"
void transport_open( Transport *tpt, const char *path );
#ifdef LUARPC_ENABLE_SERIAL
// Setup Transport
void transport_init (Transport *tpt)
{
tpt->fd = INVALID_TRANSPORT;
}
void transport_open( Transport *tpt, const char *path )
{
struct exception e;
tpt->fd = ser_open( path );
if( tpt->fd == INVALID_TRANSPORT)
{
e.errnum = transport_errno;
e.type = fatal;
Throw( e );
}
ser_setup( tpt->fd, 115200, SER_DATABITS_8, SER_PARITY_NONE, SER_STOPBITS_1 );
ser_set_timeout_ms( tpt->fd, 1000 );
}
// 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))
luaL_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))
luaL_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;
while( transport_readable( tpt ) == 0 ); // wait for incoming data
atpt->fd = tpt->fd;
}
// Read & Write to Transport
void transport_read_buffer (Transport *tpt, u8 *buffer, int length)
{
u32 n;
struct exception e;
TRANSPORT_VERIFY_OPEN;
while( length > 0 )
{
TRANSPORT_VERIFY_OPEN;
n = ser_read( tpt->fd, buffer, length );
// error handling
if( n == 0 )
{
e.errnum = ERR_NODATA;
e.type = nonfatal;
Throw( e );
}
if( n < 0 )
{
e.errnum = transport_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 = ser_write( tpt->fd, buffer, length );
if ( n != length )
{
e.errnum = transport_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;
int ret;
if (tpt->fd == INVALID_TRANSPORT)
return 0;
ret = ser_readable( tpt->fd );
if ( ret < 0 )
{
e.errnum = transport_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)
{
ser_close( tpt->fd );
tpt->fd = INVALID_TRANSPORT;
}
}
#endif // LUARPC_ENABLE_SERIAL