forked from markqvist/LibAPRS
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLibAPRS.cpp
More file actions
371 lines (316 loc) · 8.96 KB
/
LibAPRS.cpp
File metadata and controls
371 lines (316 loc) · 8.96 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "Arduino.h"
#include "AFSK.h"
#include "AX25.h"
Afsk modem;
AX25Ctx AX25;
extern void aprs_msg_callback(struct AX25Msg *msg);
#define countof(a) sizeof(a)/sizeof(a[0])
int LibAPRS_vref = REF_3V3;
bool LibAPRS_open_squelch = false;
unsigned long custom_preamble = 350UL;
unsigned long custom_tail = 50UL;
AX25Call src;
AX25Call dst;
AX25Call path1;
AX25Call path2;
char CALL[7] = "NOCALL";
int CALL_SSID = 0;
char DST[7] = "APZMDM";
int DST_SSID = 0;
char PATH1[7] = "WIDE1";
int PATH1_SSID = 1;
char PATH2[7] = "WIDE2";
int PATH2_SSID = 2;
AX25Call path[4];
// Location packet assembly fields
char latitude[9];
char longtitude[10];
char symbolTable = '/';
char symbol = 'n';
char speed[4] = "000";
char course[4] = "000";
uint8_t power = 10;
uint8_t height = 10;
uint8_t gain = 10;
uint8_t directivity = 10;
/////////////////////////
// Message packet assembly fields
char message_recip[7];
int message_recip_ssid = -1;
int message_seq = 0;
char lastMessage[67];
size_t lastMessageLen;
bool message_autoAck = false;
/////////////////////////
void APRS_init(int reference, bool open_squelch) {
LibAPRS_vref = reference;
LibAPRS_open_squelch = open_squelch;
AFSK_init(&modem);
ax25_init(&AX25, aprs_msg_callback);
}
void APRS_poll(void) {
ax25_poll(&AX25);
}
void APRS_setCallsign(char *call, int ssid) {
memset(CALL, 0, 7);
int i = 0;
while (i < 6 && call[i] != 0) {
CALL[i] = call[i];
i++;
}
CALL_SSID = ssid;
}
void APRS_setDestination(char *call, int ssid) {
memset(DST, 0, 7);
int i = 0;
while (i < 6 && call[i] != 0) {
DST[i] = call[i];
i++;
}
DST_SSID = ssid;
}
void APRS_setPath1(char *call, int ssid) {
memset(PATH1, 0, 7);
int i = 0;
while (i < 6 && call[i] != 0) {
PATH1[i] = call[i];
i++;
}
PATH1_SSID = ssid;
}
void APRS_setPath2(char *call, int ssid) {
memset(PATH2, 0, 7);
int i = 0;
while (i < 6 && call[i] != 0) {
PATH2[i] = call[i];
i++;
}
PATH2_SSID = ssid;
}
void APRS_setMessageDestination(char *call, int ssid) {
memset(message_recip, 0, 7);
int i = 0;
while (i < 6 && call[i] != 0) {
message_recip[i] = call[i];
i++;
}
message_recip_ssid = ssid;
}
void APRS_setPreamble(unsigned long pre) {
custom_preamble = pre;
}
void APRS_setTail(unsigned long tail) {
custom_tail = tail;
}
void APRS_useAlternateSymbolTable(bool use) {
if (use) {
symbolTable = '\\';
} else {
symbolTable = '/';
}
}
void APRS_setSymbol(char sym) {
symbol = sym;
}
void APRS_setLat(char *lat) {
memset(latitude, 0, 9);
int i = 0;
while (i < 8 && lat[i] != 0) {
latitude[i] = lat[i];
i++;
}
}
void APRS_setLon(char *lon) {
memset(longtitude, 0, 10);
int i = 0;
while (i < 9 && lon[i] != 0) {
longtitude[i] = lon[i];
i++;
}
}
void APRS_setPower(int s) {
if (s >= 0 && s < 10) {
power = s;
}
}
void APRS_setHeight(int s) {
if (s >= 0 && s < 10) {
height = s;
}
}
void APRS_setGain(int s) {
if (s >= 0 && s < 10) {
gain = s;
}
}
void APRS_setDirectivity(int s) {
if (s >= 0 && s < 10) {
directivity = s;
}
}
void APRS_setCourse(int s){
sprintf(course, "%03d", s);
}
void APRS_setSpeed(int s){
sprintf(speed, "%03d", s);
}
void APRS_printSettings() {
Serial.println(F("LibAPRS Settings:"));
Serial.print(F("Callsign: ")); Serial.print(CALL); Serial.print(F("-")); Serial.println(CALL_SSID);
Serial.print(F("Destination: ")); Serial.print(DST); Serial.print(F("-")); Serial.println(DST_SSID);
Serial.print(F("Path1: ")); Serial.print(PATH1); Serial.print(F("-")); Serial.println(PATH1_SSID);
Serial.print(F("Path2: ")); Serial.print(PATH2); Serial.print(F("-")); Serial.println(PATH2_SSID);
Serial.print(F("Message dst: ")); if (message_recip[0] == 0) { Serial.println(F("N/A")); } else { Serial.print(message_recip); Serial.print(F("-")); Serial.println(message_recip_ssid); }
Serial.print(F("TX Preamble: ")); Serial.println(custom_preamble);
Serial.print(F("TX Tail: ")); Serial.println(custom_tail);
Serial.print(F("Symbol table: ")); if (symbolTable = '/') { Serial.println(F("Normal")); } else { Serial.println(F("Alternate")); }
Serial.print(F("Symbol: ")); Serial.println(symbol);
Serial.print(F("Power: ")); if (power < 10) { Serial.println(power); } else { Serial.println(F("N/A")); }
Serial.print(F("Height: ")); if (height < 10) { Serial.println(height); } else { Serial.println(F("N/A")); }
Serial.print(F("Gain: ")); if (gain < 10) { Serial.println(gain); } else { Serial.println(F("N/A")); }
Serial.print(F("Directivity: ")); if (directivity < 10) { Serial.println(directivity); } else { Serial.println(F("N/A")); }
Serial.print(F("Latitude: ")); if (latitude[0] != 0) { Serial.println(latitude); } else { Serial.println(F("N/A")); }
Serial.print(F("Longtitude: ")); if (longtitude[0] != 0) { Serial.println(longtitude); } else { Serial.println(F("N/A")); }
}
void APRS_sendPkt(void *_buffer, size_t length) {
uint8_t *buffer = (uint8_t *)_buffer;
memcpy(dst.call, DST, 6);
dst.ssid = DST_SSID;
memcpy(src.call, CALL, 6);
src.ssid = CALL_SSID;
memcpy(path1.call, PATH1, 6);
path1.ssid = PATH1_SSID;
memcpy(path2.call, PATH2, 6);
path2.ssid = PATH2_SSID;
path[0] = dst;
path[1] = src;
path[2] = path1;
path[3] = path2;
ax25_sendVia(&AX25, path, countof(path), buffer, length);
}
// Dynamic RAM usage of this function is 30 bytes
void APRS_sendLoc(void *_buffer, size_t length) {
size_t payloadLength = 20+length;
bool usePHG = false;
bool useCS = false;
if (power < 10 && height < 10 && gain < 10 && directivity < 9) {
usePHG = true;
payloadLength += 7;
}
else if ( speed[2] != '0' ){
useCS = true;
payloadLength += 7;
}
uint8_t *packet = (uint8_t*)malloc(payloadLength);
uint8_t *ptr = packet;
packet[0] = '=';
packet[9] = symbolTable;
packet[19] = symbol;
ptr++;
memcpy(ptr, latitude, 8);
ptr += 9;
memcpy(ptr, longtitude, 9);
ptr += 10;
if (usePHG) {
packet[20] = 'P';
packet[21] = 'H';
packet[22] = 'G';
packet[23] = power+48;
packet[24] = height+48;
packet[25] = gain+48;
packet[26] = directivity+48;
ptr+=7;
}
if (useCS){
memcpy(ptr, course, 3);
packet[23] = '/';
ptr += 4;
memcpy(ptr, speed, 3);
ptr += 3;
}
if (length > 0) {
uint8_t *buffer = (uint8_t *)_buffer;
memcpy(ptr, buffer, length);
}
APRS_sendPkt(packet, payloadLength);
free(packet);
}
// Dynamic RAM usage of this function is 18 bytes
void APRS_sendMsg(void *_buffer, size_t length) {
if (length > 67) length = 67;
size_t payloadLength = 11+length+4;
uint8_t *packet = (uint8_t*)malloc(payloadLength);
uint8_t *ptr = packet;
packet[0] = ':';
int callSize = 6;
int count = 0;
while (callSize--) {
if (message_recip[count] != 0) {
packet[1+count] = message_recip[count];
count++;
}
}
if (message_recip_ssid != -1) {
packet[1+count] = '-'; count++;
if (message_recip_ssid < 10) {
packet[1+count] = message_recip_ssid+48; count++;
} else {
packet[1+count] = 49; count++;
packet[1+count] = message_recip_ssid-10+48; count++;
}
}
while (count < 9) {
packet[1+count] = ' '; count++;
}
packet[1+count] = ':';
ptr += 11;
if (length > 0) {
uint8_t *buffer = (uint8_t *)_buffer;
memcpy(ptr, buffer, length);
memcpy(lastMessage, buffer, length);
lastMessageLen = length;
}
message_seq++;
if (message_seq > 999) message_seq = 0;
packet[11+length] = '{';
int n = message_seq % 10;
int d = ((message_seq % 100) - n)/10;
int h = (message_seq - d - n) / 100;
packet[12+length] = h+48;
packet[13+length] = d+48;
packet[14+length] = n+48;
APRS_sendPkt(packet, payloadLength);
free(packet);
}
void APRS_msgRetry() {
message_seq--;
APRS_sendMsg(lastMessage, lastMessageLen);
}
// For getting free memory, from:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/15
extern unsigned int __heap_start;
extern void *__brkval;
struct __freelist {
size_t sz;
struct __freelist *nx;
};
extern struct __freelist *__flp;
int freeListSize() {
struct __freelist* current;
int total = 0;
for (current = __flp; current; current = current->nx) {
total += 2; /* Add two bytes for the memory block's header */
total += (int) current->sz;
}
return total;
}
int freeMemory() {
int free_memory;
if ((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__heap_start);
} else {
free_memory = ((int)&free_memory) - ((int)__brkval);
free_memory += freeListSize();
}
return free_memory;
}