altos: Scale packet master receive delay by baud rate
[fw/altos] / src / drivers / 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         int c;
24
25         /* No need to block interrupts in this function as
26          * all packet variables are only modified from task
27          * context, not an interrupt handler
28          */
29         while ((c = _ao_packet_pollchar()) == AO_READ_AGAIN) {
30                 if (!ao_packet_enable)
31                         break;
32                 if (ao_packet_master_sleeping)
33                         ao_wakeup(&ao_packet_master_sleeping);
34                 flush();
35                 ao_sleep(&ao_stdin_ready);
36         }
37         return c;
38 }
39
40 static void
41 ao_packet_echo(void) __reentrant
42 {
43         int     c;
44         while (ao_packet_enable) {
45                 c = ao_packet_getchar();
46                 if (c != AO_READ_AGAIN)
47                         putchar(c);
48         }
49         ao_exit();
50 }
51
52 static __xdata struct ao_task   ao_packet_echo_task;
53 static __xdata uint16_t         ao_packet_master_delay;
54 static __xdata uint16_t         ao_packet_master_time;
55
56 #define AO_PACKET_MASTER_DELAY_SHORT    AO_MS_TO_TICKS(100)
57 #define AO_PACKET_MASTER_DELAY_LONG     AO_MS_TO_TICKS(1000)
58 #define AO_PACKET_MASTER_DELAY_TIMEOUT  AO_MS_TO_TICKS(2000)
59
60 #if HAS_RADIO_RATE
61 #define AO_PACKET_MASTER_RECV_DELAY     AO_MS_TO_TICKS(100) << (ao_config.radio_rate << 1)
62 #else
63 #define AO_PACKET_MASTER_RECV_DELAY     AO_MS_TO_TICKS(100)
64 #endif
65
66 static void
67 ao_packet_master_busy(void)
68 {
69         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
70         ao_packet_master_time = ao_time();
71 }
72
73 static void
74 ao_packet_master_check_busy(void)
75 {
76         int16_t idle;
77         if (ao_packet_master_delay != AO_PACKET_MASTER_DELAY_SHORT)
78                 return;
79         idle = (int16_t) (ao_time() - ao_packet_master_time);
80
81         if (idle > AO_PACKET_MASTER_DELAY_TIMEOUT)
82                 ao_packet_master_delay = AO_PACKET_MASTER_DELAY_LONG;
83 }
84
85 void
86 ao_packet_master(void)
87 {
88         ao_config_get();
89         ao_tx_packet.addr = ao_serial_number;
90         ao_tx_packet.len = AO_PACKET_SYN;
91         ao_packet_master_time = ao_time();
92         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
93         while (ao_packet_enable) {
94                 uint8_t r;
95                 ao_xmemcpy(ao_tx_packet.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
96                 ao_packet_send();
97                 if (ao_tx_packet.len)
98                         ao_packet_master_busy();
99                 ao_packet_master_check_busy();
100                 ao_alarm(AO_PACKET_MASTER_RECV_DELAY);
101                 r = ao_packet_recv();
102                 ao_clear_alarm();
103                 if (r) {
104                         /* if we can transmit data, do so */
105                         if (ao_packet_tx_used && ao_tx_packet.len == 0)
106                                 continue;
107                         if (ao_rx_packet.packet.len)
108                                 ao_packet_master_busy();
109                         ao_packet_master_sleeping = 1;
110                         ao_alarm(ao_packet_master_delay);
111                         ao_sleep(&ao_packet_master_sleeping);
112                         ao_clear_alarm();
113                         ao_packet_master_sleeping = 0;
114                 }
115         }
116         ao_exit();
117 }
118
119 static void
120 ao_packet_forward(void) __reentrant
121 {
122         char c;
123         ao_packet_enable = 1;
124         ao_cmd_white();
125
126         flush();
127 #if HAS_MONITOR
128         ao_monitor_disable();
129 #endif
130         ao_add_task(&ao_packet_task, ao_packet_master, "master");
131         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
132         while ((c = getchar()) != '~') {
133                 if (c == '\r') c = '\n';
134                 ao_packet_putchar(c);
135         }
136
137         /* Wait for a second if there is any pending data */
138         for (c = 0; (ao_packet_tx_used || ao_tx_packet.len) && c < 10; c++)
139                 ao_delay(AO_MS_TO_TICKS(100));
140         ao_packet_enable = 0;
141         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
142                 ao_radio_recv_abort();
143                 ao_wakeup(&ao_stdin_ready);
144                 ao_delay(AO_MS_TO_TICKS(10));
145         }
146 #if HAS_MONITOR
147         ao_monitor_enable();
148 #endif
149 }
150
151 static void
152 ao_packet_signal(void)
153 {
154         printf ("RSSI: %d\n", ao_radio_rssi);
155 }
156
157 __code struct ao_cmds ao_packet_master_cmds[] = {
158         { ao_packet_forward,    "p\0Remote packet link." },
159         { ao_packet_signal,     "s\0Report signal strength." },
160         { 0,    NULL },
161 };
162
163 void
164 ao_packet_master_init(void)
165 {
166         ao_cmd_register(&ao_packet_master_cmds[0]);
167 }