]> git.gag.com Git - fw/altos/blob - src/drivers/ao_packet.c
build-dep on default-jdk instead of openjdk-6-jdk, closes: #655580
[fw/altos] / src / drivers / ao_packet.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 __xdata struct ao_packet_recv ao_rx_packet;
21 __xdata struct ao_packet ao_tx_packet;
22 __pdata uint8_t ao_packet_rx_len, ao_packet_rx_used, ao_packet_tx_used;
23
24 static __xdata char tx_data[AO_PACKET_MAX];
25 static __xdata char rx_data[AO_PACKET_MAX];
26 static __pdata uint8_t rx_seq;
27
28 __xdata struct ao_task  ao_packet_task;
29 __xdata uint8_t ao_packet_enable;
30 __xdata uint8_t ao_packet_master_sleeping;
31
32 void
33 ao_packet_send(void)
34 {
35 #ifdef AO_LED_RED
36         ao_led_on(AO_LED_RED);
37 #endif
38         /* If any tx data is pending then copy it into the tx packet */
39         if (ao_packet_tx_used && ao_tx_packet.len == 0) {
40                 ao_xmemcpy(&ao_tx_packet.d, tx_data, ao_packet_tx_used);
41                 ao_tx_packet.len = ao_packet_tx_used;
42                 ao_tx_packet.seq++;
43                 ao_packet_tx_used = 0;
44                 ao_wakeup(&tx_data);
45         }
46         ao_radio_send(&ao_tx_packet, sizeof (ao_tx_packet));
47 #ifdef AO_LED_RED
48         ao_led_off(AO_LED_RED);
49 #endif
50 }
51
52 uint8_t
53 ao_packet_recv(void)
54 {
55         uint8_t dma_done;
56
57 #ifdef AO_LED_GREEN
58         ao_led_on(AO_LED_GREEN);
59 #endif
60         dma_done = ao_radio_recv(&ao_rx_packet, sizeof (struct ao_packet_recv));
61 #ifdef AO_LED_GREEN
62         ao_led_off(AO_LED_GREEN);
63 #endif
64
65         /* Check to see if we got a valid packet */
66         if (!dma_done)
67                 return 0;
68 #ifdef PKT_APPEND_STATUS_1_CRC_OK
69         if (!(ao_rx_packet.status & PKT_APPEND_STATUS_1_CRC_OK))
70                 return 0;
71 #endif
72
73         /* Accept packets with matching call signs, or any packet if
74          * our callsign hasn't been configured
75          */
76         if (ao_xmemcmp(ao_rx_packet.packet.callsign,
77                        ao_config.callsign,
78                        AO_MAX_CALLSIGN) != 0 &&
79             ao_xmemcmp(ao_config.callsign, CODE_TO_XDATA("N0CALL"), 7) != 0)
80                 return 0;
81
82         /* SYN packets carry no data */
83         if (ao_rx_packet.packet.len == AO_PACKET_SYN) {
84                 rx_seq = ao_rx_packet.packet.seq;
85                 ao_tx_packet.seq = ao_rx_packet.packet.ack;
86                 ao_tx_packet.ack = rx_seq;
87         } else if (ao_rx_packet.packet.len) {
88
89                 /* Check for incoming data at the next sequence and
90                  * for an empty data buffer
91                  */
92                 if (ao_rx_packet.packet.seq == (uint8_t) (rx_seq + (uint8_t) 1) &&
93                     ao_packet_rx_used == ao_packet_rx_len) {
94
95                         /* Copy data to the receive data buffer and set up the
96                          * offsets
97                          */
98                         ao_xmemcpy(rx_data, ao_rx_packet.packet.d, ao_rx_packet.packet.len);
99                         ao_packet_rx_used = 0;
100                         ao_packet_rx_len = ao_rx_packet.packet.len;
101
102                         /* Mark the sequence that we've received to
103                          * let the sender know when we return a packet
104                          */
105                         rx_seq = ao_rx_packet.packet.seq;
106                         ao_tx_packet.ack = rx_seq;
107
108                         /* Poke anyone looking for received data */
109                         ao_wakeup(&ao_stdin_ready);
110                 }
111         }
112
113         /* If the other side has seen the latest data we queued,
114          * wake up any task waiting to send data and let them go again
115          */
116         if (ao_rx_packet.packet.ack == ao_tx_packet.seq) {
117                 ao_tx_packet.len = 0;
118                 ao_wakeup(&ao_tx_packet);
119         }
120         return 1;
121 }
122
123 #ifndef PACKET_HAS_MASTER
124 #define PACKET_HAS_MASTER 1
125 #endif
126
127 #if PACKET_HAS_MASTER
128 void
129 ao_packet_flush(void)
130 {
131         /* If there is data to send, and this is the master,
132          * then poke the master to send all queued data
133          */
134         if (ao_packet_tx_used && ao_packet_master_sleeping)
135                 ao_wakeup(&ao_packet_master_sleeping);
136 }
137 #endif /* PACKET_HAS_MASTER */
138
139 void
140 ao_packet_putchar(char c) __reentrant
141 {
142         while (ao_packet_tx_used == AO_PACKET_MAX && ao_packet_enable) {
143 #if PACKET_HAS_MASTER
144                 ao_packet_flush();
145 #endif
146                 ao_sleep(&tx_data);
147         }
148
149         if (ao_packet_enable)
150                 tx_data[ao_packet_tx_used++] = c;
151 }
152
153 char
154 ao_packet_pollchar(void) __critical
155 {
156         if (!ao_packet_enable)
157                 return AO_READ_AGAIN;
158
159         if (ao_packet_rx_used == ao_packet_rx_len)
160                 return AO_READ_AGAIN;
161
162         return rx_data[ao_packet_rx_used++];
163 }