uart_pty: Check value of environment variable
[simavr] / examples / parts / uart_pty.c
1 /*
2         uart_pty.c
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <sys/select.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <pty.h>
30 #include <signal.h>
31
32 #include "uart_pty.h"
33 #include "avr_uart.h"
34 #include "sim_hex.h"
35
36 DEFINE_FIFO(uint8_t,uart_pty_fifo);
37
38 //#define TRACE(_w) _w
39 #ifndef TRACE
40 #define TRACE(_w)
41 #endif
42
43 /*
44  * called when a byte is send via the uart on the AVR
45  */
46 static void
47 uart_pty_in_hook(
48                 struct avr_irq_t * irq,
49                 uint32_t value,
50                 void * param)
51 {
52         uart_pty_t * p = (uart_pty_t*)param;
53         TRACE(printf("uart_pty_in_hook %02x\n", value);)
54         uart_pty_fifo_write(&p->in, value);
55 }
56
57 // try to empty our fifo, the uart_pty_xoff_hook() will be called when
58 // other side is full
59 static void
60 uart_pty_flush_incoming(
61                 uart_pty_t * p)
62 {
63         while (p->xon && !uart_pty_fifo_isempty(&p->out)) {
64                 uint8_t byte = uart_pty_fifo_read(&p->out);
65                 TRACE(printf("uart_pty_flush_incoming send %02x\n", byte);)
66                 avr_raise_irq(p->irq + IRQ_UART_PTY_BYTE_OUT, byte);
67         }
68 }
69
70 /*
71  * Called when the uart has room in it's input buffer. This is called repeateadly
72  * if necessary, while the xoff is called only when the uart fifo is FULL
73  */
74 static void
75 uart_pty_xon_hook(
76                 struct avr_irq_t * irq,
77                 uint32_t value,
78                 void * param)
79 {
80         uart_pty_t * p = (uart_pty_t*)param;
81         TRACE(if (!p->xon) printf("uart_pty_xon_hook\n");)
82         p->xon = 1;
83         uart_pty_flush_incoming(p);
84 }
85
86 /*
87  * Called when the uart ran out of room in it's input buffer
88  */
89 static void
90 uart_pty_xoff_hook(
91                 struct avr_irq_t * irq,
92                 uint32_t value,
93                 void * param)
94 {
95         uart_pty_t * p = (uart_pty_t*)param;
96         TRACE(if (p->xon) printf("uart_pty_xoff_hook\n");)
97         p->xon = 0;
98 }
99
100 static void *
101 uart_pty_thread(
102                 void * param)
103 {
104         uart_pty_t * p = (uart_pty_t*)param;
105
106         while (1) {
107                 fd_set read_set, write_set;
108                 int max = p->s + 1;
109                 FD_ZERO(&read_set);
110                 FD_ZERO(&write_set);
111
112                 // read more only if buffer was flushed
113                 if (p->buffer_len == p->buffer_done)
114                         FD_SET(p->s, &read_set);
115                 if (!uart_pty_fifo_isempty(&p->in))
116                         FD_SET(p->s, &write_set);
117
118                 struct timeval timo = { 0, 500 };       // short, but not too short interval
119                 int ret = select(max, &read_set, &write_set, NULL, &timo);
120
121                 if (!ret)
122                         continue;
123                 if (ret < 0)
124                         break;
125
126                 if (FD_ISSET(p->s, &read_set)) {
127                         ssize_t r = read(p->s, p->buffer, sizeof(p->buffer)-1);
128                         p->buffer_len = r;
129                         p->buffer_done = 0;
130                         TRACE(hdump("pty recv", p->buffer, r);)
131                 }
132                 if (p->buffer_done < p->buffer_len) {
133                         // write them in fifo
134                         while (p->buffer_done < p->buffer_len && !uart_pty_fifo_isfull(&p->out))
135                                 uart_pty_fifo_write(&p->out, p->buffer[p->buffer_done++]);
136                 }
137                 if (FD_ISSET(p->s, &write_set)) {
138                         uint8_t buffer[512];
139                         // write them in fifo
140                         uint8_t * dst = buffer;
141                         while (!uart_pty_fifo_isempty(&p->in) && dst < (buffer+sizeof(buffer)))
142                                 *dst++ = uart_pty_fifo_read(&p->in);
143                         size_t len = dst - buffer;
144                         TRACE(size_t r =) write(p->s, buffer, len);
145                         TRACE(hdump("pty send", buffer, r);)
146                 }
147                 uart_pty_flush_incoming(p);
148         }
149         return NULL;
150 }
151
152 static const char * irq_names[IRQ_UART_PTY_COUNT] = {
153         [IRQ_UART_PTY_BYTE_IN] = "8<uart_pty.in",
154         [IRQ_UART_PTY_BYTE_OUT] = "8>uart_pty.out",
155 };
156
157 void
158 uart_pty_init(
159                 struct avr_t * avr,
160                 uart_pty_t * p)
161 {
162         p->avr = avr;
163         p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_UART_PTY_COUNT, irq_names);
164         avr_irq_register_notify(p->irq + IRQ_UART_PTY_BYTE_IN, uart_pty_in_hook, p);
165
166         int m, s;
167
168         if (openpty(&m, &s, p->slavename, NULL, NULL) < 0) {
169                 fprintf(stderr, "%s: Can't create pty: %s", __FUNCTION__, strerror(errno));
170                 return ;
171         }
172         p->s = m;
173
174         printf("uart_pty_init bridge on port *** %s ***\n", p->slavename);
175
176         pthread_create(&p->thread, NULL, uart_pty_thread, p);
177
178 }
179
180 void
181 uart_pty_stop(
182                 uart_pty_t * p)
183 {
184         puts(__func__);
185         pthread_kill(p->thread, SIGINT);
186         close(p->s);
187         void * ret;
188         pthread_join(p->thread, &ret);
189 }
190
191 void
192 uart_pty_connect(
193                 uart_pty_t * p,
194                 char uart)
195 {
196         // disable the stdio dump, as we are sending binary there
197         uint32_t f = 0;
198         avr_ioctl(p->avr, AVR_IOCTL_UART_GET_FLAGS(uart), &f);
199         f &= ~AVR_UART_FLAG_STDIO;
200         avr_ioctl(p->avr, AVR_IOCTL_UART_SET_FLAGS(uart), &f);
201
202         avr_irq_t * src = avr_io_getirq(p->avr, AVR_IOCTL_UART_GETIRQ(uart), UART_IRQ_OUTPUT);
203         avr_irq_t * dst = avr_io_getirq(p->avr, AVR_IOCTL_UART_GETIRQ(uart), UART_IRQ_INPUT);
204         avr_irq_t * xon = avr_io_getirq(p->avr, AVR_IOCTL_UART_GETIRQ(uart), UART_IRQ_OUT_XON);
205         avr_irq_t * xoff = avr_io_getirq(p->avr, AVR_IOCTL_UART_GETIRQ(uart), UART_IRQ_OUT_XOFF);
206         if (src && dst) {
207                 avr_connect_irq(src, p->irq + IRQ_UART_PTY_BYTE_IN);
208                 avr_connect_irq(p->irq + IRQ_UART_PTY_BYTE_OUT, dst);
209         }
210         if (xon)
211                 avr_irq_register_notify(xon, uart_pty_xon_hook, p);
212         if (xoff)
213                 avr_irq_register_notify(xoff, uart_pty_xoff_hook, p);
214
215         char link[128];
216         sprintf(link, "/tmp/simavr-uart%c", uart);
217         unlink(link);
218         if (symlink(p->slavename, link) != 0) {
219                 fprintf(stderr, "WARN %s: Can't create %s: %s", __func__, link, strerror(errno));
220         } else {
221                 printf("%s: %s now points to %s\n", __func__, link, p->slavename);
222         }
223         if (getenv("SIMAVR_UART_XTERM") && atoi(getenv("SIMAVR_UART_XTERM"))) {
224                 char cmd[256];
225                 sprintf(cmd, "nohup xterm -e picocom -b 115200 %s >/dev/null 2>&1 &", p->slavename);
226                 system(cmd);
227         } else
228                 printf("note: export SIMAVR_UART_XTERM=1 and install picocom to get a terminal\n");
229 }
230