This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
digipeater.c
217 lines (170 loc) · 5.52 KB
/
digipeater.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Digipeater
*
* Stored parameters used by digipeater (can be changed in command interface)
* MYCALL - my callsign
* DIGIPEATER_WIDE1 - true if wide1/fill-in digipeater mode. Meaning that only WIDE1 alias will be reacted on.
* DIGIPEATER_SAR - true if SAR preemption mode. If an alias SAR is found anywhere in the path, it will
* preempt others (moved first) and digipeated upon.
*
* Macros for configuration (defined in defines.h)
* HDLC_DECODER_QUEUE_SIZE - size (in packets) of receiving queue. Normally 7.
* STACK_DIGIPEATER - size of stack for digipeater task.
* STACK_HLIST_TICK - size of stack for tick_thread (for heard list).
*
*/
#include "defines.h"
#include "config.h"
#include "ax25.h"
#include "afsk.h"
#include "hdlc.h"
#include "ui/ui.h"
#include "radio.h"
#include "heardlist.h"
#include "digipeater.h"
#include <string.h>
static bool digi_on = false;
static FBQ rxqueue;
static thread_t* digithr=NULL;
extern fbq_t* outframes;
extern fbq_t* mon_q;
static void check_frame(FBUF *f);
/***********************************************
* digipeater main thread
***********************************************/
static THD_FUNCTION(digipeater, arg)
{
(void) arg;
chRegSetThreadName("Digipeater");
sleep(4000);
beeps("-.. "); blipUp();
while (digi_on)
{
/* Wait for frame.
*/
FBUF frame = fbq_get(&rxqueue);
if (fbuf_empty(&frame)) {
fbuf_release(&frame);
continue;
}
/* Do something about it */
check_frame(&frame);
/* And dispose it */
fbuf_release(&frame);
}
sleep(500);
beeps("-.. "); blipDown();
}
/**********************
* digipeater_init
**********************/
void digipeater_init()
{
FBQ_INIT(rxqueue, HDLC_DECODER_QUEUE_SIZE);
if (GET_BYTE_PARAM(DIGIPEATER_ON))
digipeater_activate(true);
}
/***************************************************************
* Turn digipeater on if argument is true, turn it off
* if false.
***************************************************************/
void digipeater_on(bool m)
{
SET_BYTE_PARAM(DIGIPEATER_ON, (m? 1:0) );
digipeater_activate(m);
}
/***************************************************************
* Activate the digipeater if argument is true
* Deactivate if false
***************************************************************/
void digipeater_activate(bool m)
{
bool tstart = m && !digi_on;
bool tstop = !m && digi_on;
digi_on = m;
FBQ* mq = (digi_on? &rxqueue : NULL);
if (tstart) {
/* Subscribe to RX packets and start treads */
hdlc_subscribe_rx(mq, 1);
digithr = THREAD_DSTART(digipeater, STACK_DIGIPEATER, NORMALPRIO, NULL);
hlist_start();
/* Turn on radio */
radio_require();
}
if (tstop) {
/* Turn off radio */
radio_release();
/* Unsubscribe to RX packets and stop threads */
fbq_signal(&rxqueue);
if (digithr != NULL)
chThdWait(digithr);
digithr = NULL;
hdlc_subscribe_rx(NULL, 1);
}
}
/*******************************************************************
* Check a frame if it is to be digipeated
* If yes, digipeat it :)
*******************************************************************/
static void check_frame(FBUF *f)
{
FBUF newHdr;
addr_t mycall, from, to;
addr_t digis[7], digis2[7];
bool widedigi = false;
uint8_t ctrl, pid;
uint8_t i, j;
int8_t sar_pos = -1;
uint8_t ndigis = ax25_decode_header(f, &from, &to, digis, &ctrl, &pid);
if (hlist_duplicate(&from, &to, f, ndigis))
return;
GET_PARAM(MYCALL, &mycall);
/* Copy items in digi-path that has digipeated flag turned on,
* i.e. the digis that the packet has been through already
*/
for (i=0; i<ndigis && (digis[i].flags & FLAG_DIGI); i++)
digis2[i] = digis[i];
/* Return if it has been through all digis in path */
if (i==ndigis)
return;
/* Check if the WIDE1-1 alias is next in the list */
if (GET_BYTE_PARAM(DIGIP_WIDE1_ON)
&& strncasecmp("WIDE1", digis[i].callsign, 5) == 0 && digis[i].ssid == 1)
widedigi = true;
/* Look for SAR alias in the rest of the path
* NOTE: Don't use SAR-preemption if packet has been digipeated by others first
*/
if (GET_BYTE_PARAM(DIGIP_SAR_ON) && i<=0)
for (j=i; j<ndigis; j++)
if (strncasecmp("SAR", digis[j].callsign, 3) == 0)
{ sar_pos = j; break; }
/* Return if no SAR preemtion and WIDE1 alias not found first */
if (sar_pos < 0 && !widedigi)
return;
/* Mark as digipeated through mycall */
j = i;
mycall.flags = FLAG_DIGI;
digis2[j++] = mycall;
/* do SAR preemption if requested */
if (sar_pos > -1)
str2addr(&digis2[j++], "SAR", true);
/* Otherwise, use wide digipeat method if requested and allowed */
else if (widedigi) {
i++;
str2addr(&digis2[j++], "WIDE1", true);
}
/* Copy rest of the path, exept the SAR alias (if used) */
for (; i<ndigis; i++)
if (sar_pos < 0 || i != sar_pos)
digis2[j++] = digis[i];
/* Write a new header -> newHdr */
fbuf_new(&newHdr);
ax25_encode_header(&newHdr, &from, &to, digis2, j, ctrl, pid);
/* Replace header in original packet with new header.
* Do this non-destructively: Just add rest of existing packet to new header
*/
fbuf_connect(&newHdr, f, AX25_HDR_LEN(ndigis) );
/* Send packet */
beeps("- ");
fbq_put(outframes, newHdr);
}