Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 static int
22 ao_packet_getchar(void)
23 {
24         int c;
25
26         /* No need to block interrupts in this function as
27          * all packet variables are only modified from task
28          * context, not an interrupt handler
29          */
30         while ((c = _ao_packet_pollchar()) == AO_READ_AGAIN) {
31                 if (!ao_packet_enable)
32                         break;
33                 if (ao_packet_master_sleeping)
34                         ao_wakeup(&ao_packet_master_sleeping);
35                 flush();
36                 ao_sleep(&ao_stdin_ready);
37         }
38         return c;
39 }
40
41 static void
42 ao_packet_echo(void) 
43 {
44         int     c;
45         while (ao_packet_enable) {
46                 c = ao_packet_getchar();
47                 if (c != AO_READ_AGAIN)
48                         putchar(c);
49         }
50         ao_exit();
51 }
52
53 static struct ao_task   ao_packet_echo_task;
54 static uint16_t         ao_packet_master_delay;
55 static uint16_t         ao_packet_master_time;
56
57 #define AO_PACKET_MASTER_DELAY_SHORT    AO_MS_TO_TICKS(100)
58 #define AO_PACKET_MASTER_DELAY_LONG     AO_MS_TO_TICKS(1000)
59 #define AO_PACKET_MASTER_DELAY_TIMEOUT  AO_MS_TO_TICKS(2000)
60
61 #if HAS_RADIO_RATE
62 #define AO_PACKET_MASTER_RECV_DELAY     AO_MS_TO_TICKS(100) << (ao_config.radio_rate << 1)
63 #else
64 #define AO_PACKET_MASTER_RECV_DELAY     AO_MS_TO_TICKS(100)
65 #endif
66
67 static void
68 ao_packet_master_busy(void)
69 {
70         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
71         ao_packet_master_time = ao_time();
72 }
73
74 static void
75 ao_packet_master_check_busy(void)
76 {
77         int16_t idle;
78         if (ao_packet_master_delay != AO_PACKET_MASTER_DELAY_SHORT)
79                 return;
80         idle = (int16_t) (ao_time() - ao_packet_master_time);
81
82         if (idle > AO_PACKET_MASTER_DELAY_TIMEOUT)
83                 ao_packet_master_delay = AO_PACKET_MASTER_DELAY_LONG;
84 }
85
86 void
87 ao_packet_master(void)
88 {
89         ao_config_get();
90         ao_tx_packet.addr = ao_serial_number;
91         ao_tx_packet.len = AO_PACKET_SYN;
92         ao_packet_master_time = ao_time();
93         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
94         while (ao_packet_enable) {
95                 uint8_t r;
96                 ao_xmemcpy(ao_tx_packet.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
97                 ao_packet_send();
98                 if (ao_tx_packet.len)
99                         ao_packet_master_busy();
100                 ao_packet_master_check_busy();
101                 r = ao_packet_recv(AO_PACKET_MASTER_RECV_DELAY);
102                 if (r) {
103                         /* if we can transmit data, do so */
104                         if (ao_packet_tx_used && ao_tx_packet.len == 0)
105                                 continue;
106                         if (ao_rx_packet.packet.len)
107                                 ao_packet_master_busy();
108                         ao_packet_master_sleeping = 1;
109                         ao_sleep_for(&ao_packet_master_sleeping, ao_packet_master_delay);
110                         ao_packet_master_sleeping = 0;
111                 }
112         }
113         ao_exit();
114 }
115
116 static void
117 ao_packet_forward(void) 
118 {
119         char c;
120         ao_packet_enable = 1;
121         ao_packet_tx_used = 0;
122         ao_cmd_white();
123
124         flush();
125 #if HAS_MONITOR
126         ao_monitor_disable();
127 #endif
128         ao_add_task(&ao_packet_task, ao_packet_master, "master");
129         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
130         while ((c = getchar()) != '~') {
131                 if (c == '\r') c = '\n';
132                 ao_packet_putchar(c);
133         }
134
135         /* Wait for a second if there is any pending data */
136         for (c = 0; (ao_packet_tx_used || ao_tx_packet.len) && c < 10; c++)
137                 ao_delay(AO_MS_TO_TICKS(100));
138         ao_packet_enable = 0;
139         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
140                 ao_radio_recv_abort();
141                 ao_wakeup(&ao_stdin_ready);
142                 ao_delay(AO_MS_TO_TICKS(10));
143         }
144 #if HAS_MONITOR
145         ao_monitor_enable();
146 #endif
147 }
148
149 static void
150 ao_packet_signal(void)
151 {
152         printf ("RSSI: %d\n", ao_radio_rssi);
153 }
154
155 const struct ao_cmds ao_packet_master_cmds[] = {
156         { ao_packet_forward,    "p\0Remote packet link." },
157         { ao_packet_signal,     "s\0Report signal strength." },
158         { 0,    NULL },
159 };
160
161 void
162 ao_packet_master_init(void)
163 {
164         ao_cmd_register(&ao_packet_master_cmds[0]);
165 }