]> git.gag.com Git - fw/altos/blob - src/ao_packet_master.c
Added PKGBUILDs for deps into contribs
[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         {
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                 if (!ao_packet_enable)
33                         break;
34         }
35         return c;
36 }
37
38 static void
39 ao_packet_echo(void) __reentrant
40 {
41         uint8_t c;
42         while (ao_packet_enable) {
43                 c = ao_packet_getchar();
44                 if (ao_packet_enable)
45                         ao_usb_putchar(c);
46         }
47         ao_exit();
48 }
49
50 static __xdata struct ao_task   ao_packet_echo_task;
51 static __xdata uint16_t         ao_packet_master_delay;
52 static __xdata uint16_t         ao_packet_master_time;
53
54 #define AO_PACKET_MASTER_DELAY_SHORT    AO_MS_TO_TICKS(100)
55 #define AO_PACKET_MASTER_DELAY_LONG     AO_MS_TO_TICKS(1000)
56 #define AO_PACKET_MASTER_DELAY_TIMEOUT  AO_MS_TO_TICKS(2000)
57
58 static void
59 ao_packet_master_busy(void)
60 {
61         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
62         ao_packet_master_time = ao_time();
63 }
64
65 static void
66 ao_packet_master_check_busy(void)
67 {
68         int16_t idle;
69         if (ao_packet_master_delay != AO_PACKET_MASTER_DELAY_SHORT)
70                 return;
71         idle = (int16_t) (ao_time() - ao_packet_master_time);
72
73         if (idle > AO_PACKET_MASTER_DELAY_TIMEOUT)
74                 ao_packet_master_delay = AO_PACKET_MASTER_DELAY_LONG;
75 }
76
77 void
78 ao_packet_master(void)
79 {
80         uint8_t status;
81
82         ao_config_get();
83         ao_radio_set_packet();
84         ao_tx_packet.addr = ao_serial_number;
85         ao_tx_packet.len = AO_PACKET_SYN;
86         ao_packet_master_time = ao_time();
87         ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
88         while (ao_packet_enable) {
89                 memcpy(ao_tx_packet.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
90                 ao_packet_send();
91                 if (ao_tx_packet.len)
92                         ao_packet_master_busy();
93                 ao_packet_master_check_busy();
94                 ao_alarm(ao_packet_master_delay);
95                 status = ao_packet_recv();
96                 if (status & AO_DMA_DONE) {
97                         /* if we can transmit data, do so */
98                         if (ao_packet_tx_used && ao_tx_packet.len == 0)
99                                 continue;
100                         if (ao_rx_packet.packet.len)
101                                 ao_packet_master_busy();
102                         ao_packet_master_sleeping = 1;
103                         ao_delay(ao_packet_master_delay);
104                         ao_packet_master_sleeping = 0;
105                 }
106         }
107         ao_radio_set_telemetry();
108         ao_exit();
109 }
110
111 static void
112 ao_packet_forward(void) __reentrant
113 {
114         char c;
115         ao_packet_enable = 1;
116         ao_cmd_white();
117
118         flush();
119         ao_set_monitor(0);
120         ao_add_task(&ao_packet_task, ao_packet_master, "master");
121         ao_add_task(&ao_packet_echo_task, ao_packet_echo, "echo");
122         while ((c = ao_usb_getchar()) != '~') {
123                 if (c == '\r') c = '\n';
124                 ao_packet_putchar(c);
125         }
126
127         /* Wait for a second if there is any pending data */
128         for (c = 0; (ao_packet_tx_used || ao_tx_packet.len) && c < 10; c++)
129                 ao_delay(AO_MS_TO_TICKS(100));
130         ao_packet_enable = 0;
131         while (ao_packet_echo_task.wchan || ao_packet_task.wchan) {
132                 if (ao_packet_echo_task.wchan)
133                         ao_wake_task(&ao_packet_echo_task);
134                 ao_delay(AO_MS_TO_TICKS(10));
135         }
136 }
137
138
139
140 __code struct ao_cmds ao_packet_master_cmds[] = {
141         { 'p',  ao_packet_forward,      "p                                  Remote packet link." },
142         { 0,    ao_packet_forward,      NULL },
143 };
144
145 void
146 ao_packet_master_init(void)
147 {
148         ao_cmd_register(&ao_packet_master_cmds[0]);
149 }