altos: Make cmd echo per-connection instead of global
[fw/altos] / src / ao_btm.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 uint8_t ao_btm_enable;
21 extern volatile __xdata struct ao_fifo  ao_usart1_rx_fifo;
22
23 void
24 ao_btm(void)
25 {
26         char    c;
27         while (ao_btm_enable) {
28                 c = ao_serial_pollchar();
29                 if (c != AO_READ_AGAIN)
30                         ao_usb_putchar(c);
31                 else {
32                         ao_usb_flush();
33                         ao_sleep(&ao_usart1_rx_fifo);
34                 }
35         }
36         ao_exit();
37 }
38
39 __xdata struct ao_task  ao_btm_task;
40
41 static void
42 ao_btm_forward(void)
43 {
44         char c;
45         ao_btm_enable = 1;
46         flush();
47         ao_add_task(&ao_btm_task, ao_btm, "btm");
48
49         while ((c = ao_usb_getchar()) != '~') {
50                 if (c == '\n') c = '\r';
51                 ao_serial_putchar(c);
52         }
53         ao_btm_enable = 0;
54         while (ao_btm_task.wchan) {
55                 ao_wakeup(&ao_usart1_rx_fifo);
56                 ao_delay(AO_MS_TO_TICKS(10));
57         }
58 }
59
60 __code struct ao_cmds ao_btm_cmds[] = {
61         { ao_btm_forward,       "B <data>\0BTM serial link." },
62         { 0, NULL },
63 };
64
65
66 void
67 ao_btm_init (void)
68 {
69         ao_serial_init();
70         ao_serial_set_speed(AO_SERIAL_SPEED_19200);
71         ao_cmd_register(&ao_btm_cmds[0]);
72 }