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
|
/*
* Copyright © 2009 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "aoview.h"
#include <termios.h>
#define AOVIEW_SERIAL_IN_BUF 64
#define AOVIEW_SERIAL_OUT_BUF 64
struct aoview_buf {
char *buf;
int off;
int count;
int size;
};
static int
aoview_buf_write(struct aoview_buf *buf, char *data, int len)
{
if (buf->count + len > buf->size) {
int new_size = buf->size * 2;
if (new_size == 0)
new_size = 1024;
if (buf->buf)
buf->buf = realloc (buf->buf, new_size);
else
buf->buf = malloc (new_size);
buf->size = new_size;
}
memcpy(buf->buf + buf->count, data, len);
buf->count += len;
return len;
}
static int
aoview_buf_read(struct aoview_buf *buf, char *data, int len)
{
if (len > buf->count - buf->off)
len = buf->count - buf->off;
memcpy (data, buf->buf + buf->off, len);
buf->off += len;
if (buf->off == buf->count)
buf->off = buf->count = 0;
return len;
}
static int
aoview_buf_getc(struct aoview_buf *buf)
{
char b;
int r;
r = aoview_buf_read(buf, &b, 1);
if (r == 1)
return (int) b;
return -1;
}
static void
aoview_buf_flush(struct aoview_buf *buf, int fd)
{
int ret;
if (buf->count > buf->off) {
ret = write(fd, buf->buf + buf->off, buf->count - buf->off);
if (ret > 0) {
buf->off += ret;
if (buf->off == buf->count)
buf->off = buf->count = 0;
}
}
}
static void
aoview_buf_fill(struct aoview_buf *buf, int fd)
{
int ret;
while (buf->count >= buf->size) {
int new_size = buf->size * 2;
buf->buf = realloc (buf->buf, new_size);
buf->size = new_size;
}
ret = read(fd, buf->buf + buf->count, buf->size - buf->count);
if (ret > 0)
buf->count += ret;
}
static void
aoview_buf_init(struct aoview_buf *buf)
{
buf->buf = malloc (buf->size = 1024);
buf->count = 0;
}
static void
aoview_buf_fini(struct aoview_buf *buf)
{
free(buf->buf);
}
struct aoview_serial {
GSource source;
int fd;
struct termios save_termios;
struct aoview_buf in_buf;
struct aoview_buf out_buf;
GPollFD poll_fd;
};
void
aoview_serial_printf(struct aoview_serial *serial, char *format, ...)
{
char buf[1024];
va_list ap;
int ret;
/* sprintf to a local buffer */
va_start(ap, format);
ret = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
if (ret > sizeof(buf)) {
fprintf(stderr, "printf overflow for format %s\n",
format);
}
/* flush local buffer to the wire */
aoview_buf_write(&serial->out_buf, buf, ret);
aoview_buf_flush(&serial->out_buf, serial->fd);
}
int
aoview_serial_read(struct aoview_serial *serial, char *buf, int len)
{
return aoview_buf_read(&serial->in_buf, buf, len);
}
int
aoview_serial_getc(struct aoview_serial *serial)
{
return aoview_buf_getc(&serial->in_buf);
}
static gboolean
serial_prepare(GSource *source, gint *timeout)
{
struct aoview_serial *serial = (struct aoview_serial *) source;
*timeout = -1;
if (serial->out_buf.count)
serial->poll_fd.events |= G_IO_OUT;
else
serial->poll_fd.events &= ~G_IO_OUT;
return FALSE;
}
static gboolean
serial_check(GSource *source)
{
struct aoview_serial *serial = (struct aoview_serial *) source;
gint revents = serial->poll_fd.revents;
if (revents & G_IO_NVAL)
return FALSE;
if (revents & G_IO_IN)
return TRUE;
if (revents & G_IO_OUT)
return TRUE;
return FALSE;
}
static gboolean
serial_dispatch(GSource *source,
GSourceFunc callback,
gpointer user_data)
{
struct aoview_serial *serial = (struct aoview_serial *) source;
gint revents = serial->poll_fd.revents;
if (revents & G_IO_IN)
aoview_buf_fill(&serial->in_buf, serial->fd);
if (revents & G_IO_OUT)
aoview_buf_flush(&serial->out_buf, serial->fd);
if (callback && (revents & G_IO_IN))
(*callback)(user_data);
return TRUE;
}
static void
serial_finalize(GSource *source)
{
struct aoview_serial *serial = (struct aoview_serial *) source;
aoview_buf_fini(&serial->in_buf);
aoview_buf_fini(&serial->out_buf);
tcsetattr(serial->fd, TCSAFLUSH, &serial->save_termios);
close (serial->fd);
}
static GSourceFuncs serial_funcs = {
serial_prepare,
serial_check,
serial_dispatch,
serial_finalize
};
struct aoview_serial *
aoview_serial_open(const char *tty)
{
struct aoview_serial *serial;
struct termios termios;
serial = (struct aoview_serial *) g_source_new(&serial_funcs, sizeof (struct aoview_serial));
aoview_buf_init(&serial->in_buf);
aoview_buf_init(&serial->out_buf);
serial->fd = open (tty, O_RDWR | O_NONBLOCK);
if (serial->fd < 0) {
free (serial);
return NULL;
}
tcgetattr(serial->fd, &termios);
serial->save_termios = termios;
cfmakeraw(&termios);
tcsetattr(serial->fd, TCSAFLUSH, &termios);
aoview_serial_printf(serial, "E 0\n");
tcdrain(serial->fd);
usleep(15*1000);
tcflush(serial->fd, TCIFLUSH);
serial->poll_fd.fd = serial->fd;
serial->poll_fd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
g_source_attach(&serial->source, NULL);
g_source_add_poll(&serial->source,&serial->poll_fd);
return serial;
}
void
aoview_serial_close(struct aoview_serial *serial)
{
g_source_remove_poll(&serial->source, &serial->poll_fd);
g_source_destroy(&serial->source);
g_source_unref(&serial->source);
}
void
aoview_serial_set_callback(struct aoview_serial *serial,
GSourceFunc func,
gpointer data,
GDestroyNotify notify)
{
g_source_set_callback(&serial->source, func, data, notify);
}
|