-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPRS.cpp
502 lines (423 loc) · 13.9 KB
/
APRS.cpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include "APRS.hpp"
#include <QDateTime>
#include <cmath>
#include <QUdpSocket>
#include <QHostInfo>
#include <QHostAddress>
#include <QTimer>
#include <QMessageBox>
#include <iostream>
APRS::APRS()
:server_(""),
port_(0),
callsign_(""),
callpass_(""),
timerActive(false),
theSocket(0)
{
}
APRS::APRS(const QString &server,quint16 port, const QString &callsign,const QString &callpass)
:server_(server),
port_(port),
callsign_(callsign),
callpass_(callpass),
timerActive(false),
theSocket(0)
{
}
void APRS::setServer(const QString &server)
{
server_=server;
}
void APRS::setCallsign(const QString &callsign)
{
callsign_=callsign;
}
void APRS::setCallpass(const QString &callpass)
{
callpass_=callpass;
}
void APRS::setPort(const quint16 port)
{
port_=port;
}
const QString & APRS::getServer() const
{
return server_;
}
const QString & APRS::getCallsign() const
{
return callsign_;
}
const QString & APRS::getCallpass() const
{
return callpass_;
}
const quint16 APRS::getPort() const
{
return port_;
}
QString APRS::createDFObject(const QString &oName,const std::vector<double> &coords,
double bearing,double sigma, const QString &comment)
{
QString BRG;
QString NRQ;
QString rep;
BRG.sprintf("%03.0lf/",bearing);
int Q=9-int(log(sigma)/log(2.0));
NRQ.sprintf("86%1d ",Q);
rep="000/000/";
rep.append(BRG);
rep.append(NRQ);
rep.append(comment.left(26));
return createObject(oName,coords,"/\\",rep);
}
QString APRS::createObject(const QString &oName,const std::vector<double> &coords,
const QString &symbol, const QString &comment)
{
QString formattedPosit;
// APRS objects always have 9 character names, pad with spaces.
QString objectName=oName.leftJustified(9,' ');
// we are given coordinates in decimal degrees. Convert to APRS format.
QString NS="N";
QString EW="E";
QString latS;
QString lonS;
double lat=coords[1];
double lon=coords[0];
if (lon<0)
{
lon*=-1;
EW="W";
}
if (lat<0)
{
lat*=-1;
NS="S";
}
int deg;
double min;
deg=(int) lat;
min=(lat-deg)*60;
latS.sprintf("%02d%05.2lf",deg,min);
latS.append(NS);
deg=(int) lon;
min=(lon-deg)*60;
lonS.sprintf("%03d%05.2lf",deg,min);
lonS.append(EW);
formattedPosit=callsign_;
formattedPosit.append(">APZTVR:;");
formattedPosit.append(objectName);
formattedPosit.append("*");
QDateTime theCurrentDateTime=QDateTime::currentDateTime().toUTC();
formattedPosit.append(theCurrentDateTime.toString("ddhhmm"));
formattedPosit.append("z");
formattedPosit.append(latS);
formattedPosit.append(symbol[0]);
formattedPosit.append(lonS);
formattedPosit.append(symbol[1]);
formattedPosit.append(comment.left(43));
activeObjects[oName]=formattedPosit;
sendPacketToServer(formattedPosit);
return formattedPosit;
}
QString APRS::deleteObject(const QString &oName)
{
QMap<QString, QString>::iterator i = activeObjects.find(oName);
QString retval="";
if (i != activeObjects.end())
{
retval=i.value();
if (!retval.isEmpty())
{
retval.replace(retval.indexOf('*'),1,'_');
activeObjects[oName]="";
sendPacketToServer(retval);
}
}
return retval;
}
QStringList APRS::deleteAllObjects()
{
QStringList returnList;
QMap<QString, QString>::iterator i;
for (i=activeObjects.begin(); i!=activeObjects.end(); i++)
{
QString retString=deleteObject(i.key());
if (!retString.isEmpty()) returnList.append(retString);
}
return returnList;
}
void APRS::sendPacketToServer(const QString &payload)
{
if (!payload.isEmpty())
{
// simply add to the queue of pending packets
pendingPackets.enqueue(QPair<QString,int>(payload,0));
// if there's more than one in the queue, then we will be taken care of
// eventually. But if the queue's empty except for us, need to start the
// processing of the queue
if (pendingPackets.size()==1)
{
checkPendingDatagrams();
}
}
}
// makeMultiline
// Create an APRS multiline string given an array of lat/lon pairs.
//
// Allocates memory that must be freed by the caller.
//
// lon and lat are arrays. lonObj, latObj are return values of object
// location (point from which offsets are computed).
//
// lineType is 0 for a closed polygon, 1 for a polyline
//
// colorStyle is a character as defined in the wxsvr.net multiline protocol
// web site at wxsvr.net.
//
// character | color | style
// a red solid
// b red dashed
// c red double-dashed
// d yellow solid
// e yellow dashed
// f yellow double-dashed
// g blue solid
// h blue dashed
// i blue double-dashed
// j green solid
// k green dashed
// l green double-dashed
// Returns a null pointer if user requested too many vertices, or if scale
// is out of range, or if we fail to malloc the string.
//
// One could pass only a list of lat/lons here and get back a point at which
// to create an object (at the centroid) and a string representing the
// multiline.
#define minFun(a,b) ( ((a)<(b))?(a):(b))
#define maxFun(a,b) ( ((a)>(b))?(a):(b))
QString APRS::makeMultiline(const std::vector<double> &lon, const std::vector<double> &lat,
char colorStyle, int lineType, const QString &sequence,
double *lonCentr, double *latCentr )
{
QString returnString;
int numPairs=lon.size();
// the APRS spec requires a max of 43 chars in the comment section of
// objects, which leaves room for only so many vertices in a multiline
// number allowed= (43-(6-5))/2=16
// 43chars - 6 for the sequence number- 5 for the starting pattern leaves
// 32 characters for lat/lon pairs, or 16 pairs
int maxPairs = (43-std::max(sequence.length(),6)-5)/2;
if ( numPairs > maxPairs) {
returnString = "";
} else {
double minLat, minLon;
double maxLat, maxLon;
int iPair;
double scale1,scale2,scale;
// find min/max of arrays
minLat=minLon=180;
maxLat=maxLon=-180;
for ( iPair=0; iPair < numPairs; iPair++) {
minLon = minFun(minLon,lon[iPair]);
minLat = minFun(minLat,lat[iPair]);
maxLon = maxFun(maxLon,lon[iPair]);
maxLat = maxFun(maxLat,lat[iPair]);
}
*lonCentr = (maxLon+minLon)/2;
*latCentr = (maxLat+minLat)/2;
// Compute scale:
// The scale is the value that makes the maximum or minimum offset
// map to +44 or -45. Pick the scale factor that keeps the
// offsets in that range:
if (maxLat > maxLon) {
scale1= (maxLat-*latCentr)/44.0;
} else {
scale1= (maxLon-*lonCentr)/44.0;
}
if (minLat < minLon) {
scale2 = (minLat-*latCentr)/(-45.0);
} else {
scale2 = (minLon-*lonCentr)/(-45.0);
}
scale = maxFun(scale1,scale2);
if (scale < .0001) {
scale=0.0001;
}
if (scale > 1) {
// Out of range, no shape returned
returnString = "";
} else {
// Not all systems have a log10(), but they all have log()
// So let's stick with natural logs
double ln10=log(10.0);
// KLUDGE: the multiline spec says we use
// 20*(int)(log10(scale/.0001)) to generate the scale char,
// but this means we'll often produce real scales that are smaller
// than the one we just calculated, which means we'd produce
// offsets outside the (-45,44) allowed range. So kludge and
// add 1 to the value
int lnscalefac=20*log(scale/.0001)/ln10+1;
// Now recompute the scale to be the one we actually transmitted
// This pretty much means we'll never have the best precision
// we could possibly have, but it'll be close enough
scale=pow(10,(double)lnscalefac/20-4);
// We're ready to produce the multiline string. So get on with it
// multiline string is "}CTS" (literal "}" followed by
// line Color-style specifier, followed by open/closed
// Type specifier, followed by Scale character), followed
// by even number of character pairs, followed by an optional
// "{seqnc" (sequence number).
returnString=" }";
returnString.append(QChar(colorStyle));
returnString.append(QChar((lineType == 0)?'0':'1'));
returnString.append(QChar(lnscalefac+33));
for ( iPair=0; iPair<numPairs; ++iPair) {
double latOffset=lat[iPair]-*latCentr;
// the wxsvr protocol is Western Hemisphere-Centric,
// and treats positive offsets in longitude as being
// west of the reference point. So have to reverse
// the sense of direction here.
// This will yield positive offsets if lonCenter is
// negative (west) and lon[iPair] is more negative
// (more west)
double lonOffset=*lonCentr-lon[iPair];
returnString.append( QChar(((int)(latOffset/scale)+78)));
returnString.append(QChar(((int)(lonOffset/scale)+78)));
}
returnString.append(QChar('{'));
returnString.append(sequence.left(6));
}
}
return (returnString);
}
// For generating error ellipses.
void APRS::generateEllipse(double lonCentr, double latCentr,
double axisLon, double axisLat,
int numPoints,
std::vector<double> &lons, std::vector<double> &lats)
{
int iPair;
double pi=4*atan(1.0);
lons.resize(numPoints);
lats.resize(numPoints);
for (iPair = 0; iPair < numPoints; ++iPair)
{
lons[iPair] = lonCentr + axisLon*cos(2.0*pi/((double)(numPoints-1))*iPair);
lats[iPair] = latCentr + axisLat*sin(2.0*pi/((double)(numPoints-1))*iPair);
}
}
QString APRS::createDFErrorObject(const QString &oName,
const std::vector<double> &coords,
double axisLon,double axisLat)
{
std::vector<double> lats;
std::vector<double> lons;
generateEllipse(coords[0],coords[1],axisLon,axisLat,16,lons,lats);
return(createMultilineObject(oName,lats,lons,coords,'e',0,"\\l"));
}
QString APRS::createMultilineObject(const QString &oName,
const std::vector<double> &lats,
const std::vector<double> &lons,
const std::vector<double> &coords,
char colorStyle, int lineType,
const QString &oSym)
{
double latC=coords[1];
double lonC=coords[0];
QString theMultiline=makeMultiline(lons,lats,colorStyle,lineType,"",
&lonC,&latC);
if (theMultiline.isEmpty())
return (QString(""));
return(createObject(oName,coords,oSym,theMultiline));
}
void APRS::checkPendingDatagrams()
{
// do nothing if queue is empty
if (pendingPackets.size())
{
QPair<QString,int> packet=pendingPackets.head();
int ntries=packet.second;
QString payload=packet.first;
// check that we have a socket opened
if (theSocket==0)
{
theSocket = new QUdpSocket;
}
bool sendIt=false;
bool acked=false;
if (ntries>0) // we have already sent it, check for its ack
{
ntries++;
int bytesRead;
QByteArray datagram;
QHostAddress sender;
quint16 senderPort;
datagram.resize(256);
bool retry=false;
if ((bytesRead=theSocket->readDatagram(datagram.data(),
datagram.size(),&sender,
&senderPort))!= -1)
{
QString theDatagram(datagram);
if (theDatagram=="NACK")
retry=true;
else
acked=true;
// either way, we got a packet from this, so delete the socket and
// open a new one. It seems that trying to reuse the socket
// gets us weird warnings from the abstract socket class.
theSocket->deleteLater();
theSocket=new QUdpSocket;
}
// save the updated ntries
pendingPackets.head().second=ntries;
if (retry && ntries<10)
sendIt=true;
if (acked||ntries>=10)
{
pendingPackets.dequeue(); // remove if from the queue
// just grab next one, whose retries is definitely 0 if there is one.
if (pendingPackets.size())
{
packet=pendingPackets.head();
ntries=packet.second;
payload=packet.first;
sendIt=true;
}
}
}
if (sendIt || ntries==0)
{
QString sendText=QString("%1,%2\n%3\n").arg(callsign_).arg(callpass_)
.arg(payload);
QByteArray datagram=sendText.toLatin1();
QHostInfo info=QHostInfo::fromName(server_);
if (!info.addresses().isEmpty())
{
theSocket->writeDatagram(datagram,info.addresses().first(),port_);
}
else
{
QMessageBox::warning(0,tr("APRS"),
QString(tr("Host name %1 could not be resolved."))
.arg(server_),
QMessageBox::Ok);
}
if (ntries==0) ntries=1; // only bump tries if this is the first attempt,
// otherwise we already bumped it
pendingPackets.head().second=ntries;
}
// When we get here, we've either cleared the queue or we're waiting for
// an ack. set a timer to come back here in 1ms.
if (pendingPackets.size())
QTimer::singleShot(10,this,SLOT(checkPendingDatagrams()));
else
emit(queueCleared());
}
else
{
emit(queueCleared());
}
}