2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #define AOVIEW_SERIAL_IN_BUF 64
22 #define AOVIEW_SERIAL_OUT_BUF 64
32 aoview_buf_write(struct aoview_buf *buf, char *data, int len)
34 if (buf->count + len > buf->size) {
35 int new_size = buf->size * 2;
39 buf->buf = realloc (buf->buf, new_size);
41 buf->buf = malloc (new_size);
44 memcpy(buf->buf + buf->count, data, len);
50 aoview_buf_read(struct aoview_buf *buf, char *data, int len)
52 if (len > buf->count - buf->off)
53 len = buf->count - buf->off;
54 memcpy (data, buf->buf + buf->off, len);
56 if (buf->off == buf->count)
57 buf->off = buf->count = 0;
62 aoview_buf_getc(struct aoview_buf *buf)
67 r = aoview_buf_read(buf, &b, 1);
74 aoview_buf_flush(struct aoview_buf *buf, int fd)
78 if (buf->count > buf->off) {
79 ret = write(fd, buf->buf + buf->off, buf->count - buf->off);
82 if (buf->off == buf->count)
83 buf->off = buf->count = 0;
89 aoview_buf_fill(struct aoview_buf *buf, int fd)
93 while (buf->count >= buf->size) {
94 int new_size = buf->size * 2;
95 buf->buf = realloc (buf->buf, new_size);
99 ret = read(fd, buf->buf + buf->count, buf->size - buf->count);
105 aoview_buf_init(struct aoview_buf *buf)
107 buf->buf = malloc (buf->size = 1024);
112 aoview_buf_fini(struct aoview_buf *buf)
117 struct aoview_serial {
120 struct termios save_termios;
121 struct aoview_buf in_buf;
122 struct aoview_buf out_buf;
128 aoview_serial_printf(struct aoview_serial *serial, char *format, ...)
134 /* sprintf to a local buffer */
135 va_start(ap, format);
136 ret = vsnprintf(buf, sizeof(buf), format, ap);
138 if (ret > sizeof(buf)) {
139 fprintf(stderr, "printf overflow for format %s\n",
143 /* flush local buffer to the wire */
144 aoview_buf_write(&serial->out_buf, buf, ret);
145 aoview_buf_flush(&serial->out_buf, serial->fd);
149 aoview_serial_read(struct aoview_serial *serial, char *buf, int len)
151 return aoview_buf_read(&serial->in_buf, buf, len);
155 aoview_serial_getc(struct aoview_serial *serial)
157 return aoview_buf_getc(&serial->in_buf);
161 serial_prepare(GSource *source, gint *timeout)
163 struct aoview_serial *serial = (struct aoview_serial *) source;
166 if (serial->out_buf.count)
167 serial->poll_fd.events |= G_IO_OUT;
169 serial->poll_fd.events &= ~G_IO_OUT;
174 serial_check(GSource *source)
176 struct aoview_serial *serial = (struct aoview_serial *) source;
177 gint revents = serial->poll_fd.revents;
179 if (revents & G_IO_NVAL)
181 if (revents & G_IO_IN)
183 if (revents & G_IO_OUT)
189 serial_dispatch(GSource *source,
190 GSourceFunc callback,
193 struct aoview_serial *serial = (struct aoview_serial *) source;
194 aoview_serial_callback func = (aoview_serial_callback) callback;
195 gint revents = serial->poll_fd.revents;
197 if (revents & G_IO_IN)
198 aoview_buf_fill(&serial->in_buf, serial->fd);
200 if (revents & G_IO_OUT)
201 aoview_buf_flush(&serial->out_buf, serial->fd);
204 (*func)(user_data, serial, revents);
209 serial_finalize(GSource *source)
211 struct aoview_serial *serial = (struct aoview_serial *) source;
213 aoview_buf_fini(&serial->in_buf);
214 aoview_buf_fini(&serial->out_buf);
215 tcsetattr(serial->fd, TCSAFLUSH, &serial->save_termios);
219 static GSourceFuncs serial_funcs = {
226 struct aoview_serial *
227 aoview_serial_open(const char *tty)
229 struct aoview_serial *serial;
230 struct termios termios;
232 serial = (struct aoview_serial *) g_source_new(&serial_funcs, sizeof (struct aoview_serial));
233 aoview_buf_init(&serial->in_buf);
234 aoview_buf_init(&serial->out_buf);
235 serial->fd = open (tty, O_RDWR | O_NONBLOCK);
236 if (serial->fd < 0) {
237 g_source_destroy(&serial->source);
240 tcgetattr(serial->fd, &termios);
241 serial->save_termios = termios;
243 tcsetattr(serial->fd, TCSAFLUSH, &termios);
245 aoview_serial_printf(serial, "E 0\n");
248 tcflush(serial->fd, TCIFLUSH);
249 serial->poll_fd.fd = serial->fd;
250 serial->poll_fd.events = G_IO_IN | G_IO_OUT | G_IO_HUP | G_IO_ERR;
251 g_source_attach(&serial->source, NULL);
252 g_source_add_poll(&serial->source,&serial->poll_fd);
253 aoview_serial_set_callback(serial, NULL);
258 aoview_serial_close(struct aoview_serial *serial)
260 g_source_remove_poll(&serial->source, &serial->poll_fd);
262 g_source_destroy(&serial->source);
266 aoview_serial_set_callback(struct aoview_serial *serial,
267 aoview_serial_callback func)
269 g_source_set_callback(&serial->source, (GSourceFunc) func, serial, NULL);