Clean up some altosui comments
[fw/altos] / src / ao_packet_master.c
1 /*
2  * Copyright © 2009 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 static char
21 ao_packet_getchar(void)
22 {
23         char c;
24         while ((c = ao_packet_pollchar()) == AO_READ_AGAIN)
25         {
26                 if (!ao_packet_enable)
27                         break;
28                 if (ao_packet_master_sleeping)
29                         ao_wake_task(&ao_packet_task);
30                 ao_usb_flush();
31                 ao_sleep(&ao_stdin_ready);
32         }
33         return c;
34 }
35
36 static void
37 ao_packet_echo(void) __reentrant
38 {
39         uint8_t c;
40         while (ao_packet_enable) {
41                 c = ao_packet_getchar();
42                 if (ao_packet_enable)
43                         ao_usb_putchar(c);
44         }
45         ao_exit();
46 }
47
48 static __xdata struct ao_task   ao_packet_echo_task;
49 static __xdata uint16_t         ao_packet_master_delay;
50 static __xdata uint16_t         ao_packet_master_time;
51
52 #define AO_PACKET_MASTER_DELAY_SHORT    AO_MS_TO_TICKS(100)
53 #define AO_PACKET_MASTER_DELAY_LONG     AO_MS_TO_TICKS(1000)
54 #define AO_PACKET_MASTER_DELAY_TIMEOUT  AO_MS_TO_TICKS(2000)
55
56 static void
57 ao_packet_master_busy(void)
58 {
59         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
60         ao_packet_master_time = ao_time();
61 }
62
63 static void
64 ao_packet_master_check_busy(void)
65 {
66         int16_t idle;
67         if (ao_packet_master_delay != AO_PACKET_MASTER_DELAY_SHORT)
68                 return;
69         idle = (int16_t) (ao_time() - ao_packet_master_time);
70
71         if (idle > AO_PACKET_MASTER_DELAY_TIMEOUT)
72                 ao_packet_master_delay = AO_PACKET_MASTER_DELAY_LONG;
73 }
74
75 void
76 ao_packet_master(void)
77 {
78         uint8_t status;
79
80         ao_radio_set_packet();
81         ao_tx_packet.addr = ao_serial_number;
82         ao_tx_packet.len = AO_PACKET_SYN;
83         ao_packet_master_time = ao_time();
84         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
85         while (ao_packet_enable) {
86                 ao_packet_send();
87                 if (ao_tx_packet.len)
88                         ao_packet_master_busy();
89                 ao_packet_master_check_busy();
90                 ao_alarm(ao_packet_master_delay);
91                 status = ao_packet_recv();
92                 if (status & AO_DMA_DONE) {
93                         /* if we can transmit data, do so */
94                         if (ao_packet_tx_used && ao_tx_packet.len == 0)
95                                 continue;
96                         if (ao_rx_packet.packet.len)
97                                 ao_packet_master_busy();
98                         ao_packet_master_sleeping = 1;
99                         ao_delay(ao_packet_master_delay);
100                         ao_packet_master_sleeping = 0;
101                 }
102         }
103         ao_radio_set_telemetry();
104         ao_exit();
105 }
106
107 static void
108 ao_packet_forward(void) __reentrant
109 {
110         char c;
111         ao_packet_enable = 1;
112         ao_cmd_white();
113
114         flush();
115         ao_set_monitor(0);
116         ao_add_task(&ao_packet_task, ao_packet_master, "master");
117         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
118         while ((c = ao_usb_getchar()) != '~') {
119                 if (c == '\r') c = '\n';
120                 ao_packet_putchar(c);
121         }
122         ao_packet_enable = 0;
123         ao_radio_abort();
124         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
125                 ao_wake_task(&ao_packet_echo_task);
126                 ao_wake_task(&ao_packet_task);
127                 ao_yield();
128         }
129 }
130
131
132
133 __code struct ao_cmds ao_packet_master_cmds[] = {
134         { 'p',  ao_packet_forward,      "p                                  Remote packet link." },
135         { 0,    ao_packet_forward,      NULL },
136 };
137
138 void
139 ao_packet_master_init(void)
140 {
141         ao_cmd_register(&ao_packet_master_cmds[0]);
142 }