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