update TeleMini turnon script now that we've made a stable firmware release
[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) __critical
22 {
23         char c;
24         while ((c = ao_packet_pollchar()) == AO_READ_AGAIN) {
25                 if (!ao_packet_enable)
26                         break;
27                 if (ao_packet_master_sleeping)
28                         ao_wakeup(&ao_packet_master_sleeping);
29                 flush();
30                 ao_sleep(&ao_stdin_ready);
31         }
32         return c;
33 }
34
35 static void
36 ao_packet_echo(void) __reentrant
37 {
38         char    c;
39         while (ao_packet_enable) {
40                 c = ao_packet_getchar();
41                 if (c != AO_READ_AGAIN)
42                         putchar(c);
43         }
44         ao_exit();
45 }
46
47 static __xdata struct ao_task   ao_packet_echo_task;
48 static __xdata uint16_t         ao_packet_master_delay;
49 static __xdata uint16_t         ao_packet_master_time;
50
51 #define AO_PACKET_MASTER_DELAY_SHORT    AO_MS_TO_TICKS(100)
52 #define AO_PACKET_MASTER_DELAY_LONG     AO_MS_TO_TICKS(1000)
53 #define AO_PACKET_MASTER_DELAY_TIMEOUT  AO_MS_TO_TICKS(2000)
54
55 static void
56 ao_packet_master_busy(void)
57 {
58         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
59         ao_packet_master_time = ao_time();
60 }
61
62 static void
63 ao_packet_master_check_busy(void)
64 {
65         int16_t idle;
66         if (ao_packet_master_delay != AO_PACKET_MASTER_DELAY_SHORT)
67                 return;
68         idle = (int16_t) (ao_time() - ao_packet_master_time);
69
70         if (idle > AO_PACKET_MASTER_DELAY_TIMEOUT)
71                 ao_packet_master_delay = AO_PACKET_MASTER_DELAY_LONG;
72 }
73
74 void
75 ao_packet_master(void)
76 {
77         ao_config_get();
78         ao_tx_packet.addr = ao_serial_number;
79         ao_tx_packet.len = AO_PACKET_SYN;
80         ao_packet_master_time = ao_time();
81         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
82         while (ao_packet_enable) {
83                 memcpy(ao_tx_packet.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
84                 ao_packet_send();
85                 if (ao_tx_packet.len)
86                         ao_packet_master_busy();
87                 ao_packet_master_check_busy();
88                 ao_alarm(ao_packet_master_delay);
89                 if (ao_packet_recv()) {
90                         /* if we can transmit data, do so */
91                         if (ao_packet_tx_used && ao_tx_packet.len == 0)
92                                 continue;
93                         if (ao_rx_packet.packet.len)
94                                 ao_packet_master_busy();
95                         ao_packet_master_sleeping = 1;
96                         ao_alarm(ao_packet_master_delay);
97                         ao_sleep(&ao_packet_master_sleeping);
98                         ao_packet_master_sleeping = 0;
99                 }
100         }
101         ao_exit();
102 }
103
104 static void
105 ao_packet_forward(void) __reentrant
106 {
107         char c;
108         ao_packet_enable = 1;
109         ao_cmd_white();
110
111         flush();
112 #if HAS_MONITOR
113         ao_set_monitor(0);
114 #endif
115         ao_add_task(&ao_packet_task, ao_packet_master, "master");
116         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
117         while ((c = getchar()) != '~') {
118                 if (c == '\r') c = '\n';
119                 ao_packet_putchar(c);
120         }
121
122         /* Wait for a second if there is any pending data */
123         for (c = 0; (ao_packet_tx_used || ao_tx_packet.len) && c < 10; c++)
124                 ao_delay(AO_MS_TO_TICKS(100));
125         ao_packet_enable = 0;
126         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
127                 ao_radio_recv_abort();
128                 ao_wakeup(&ao_stdin_ready);
129                 ao_delay(AO_MS_TO_TICKS(10));
130         }
131 }
132
133
134
135 __code struct ao_cmds ao_packet_master_cmds[] = {
136         { ao_packet_forward,    "p\0Remote packet link." },
137         { 0,    NULL },
138 };
139
140 void
141 ao_packet_master_init(void)
142 {
143         ao_cmd_register(&ao_packet_master_cmds[0]);
144 }