-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpacket.go
430 lines (382 loc) · 9.03 KB
/
packet.go
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
package aprs
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"
)
var (
// ErrInvalidPacket signals a corrupted/unknown APRS packet.
ErrInvalidPacket = errors.New("aprs: invalid packet")
// ErrInvalidPosition signals a corrupted APRS position report.
ErrInvalidPosition = errors.New("aprs: invalid position")
)
type Payload string
func (p Payload) Type() DataType {
var t DataType
if len(p) > 0 {
t = DataType(p[0])
}
return t
}
func isDigit(b byte) bool {
return b >= '0' && b <= '9'
}
func (p Payload) Len() int { return len(p) }
type Velocity struct {
Course float64 // Degrees
Speed float64 // Knots
}
type Wind struct {
Direction float64 // Degrees
Speed float64 // Knots
}
type PowerHeightGain struct {
PowerCode byte
HeightCode byte
GainCode byte
DirectivityCode byte
}
func (p PowerHeightGain) Power() int {
w := int(p.PowerCode - '0')
if w <= 0 {
return 0
}
return w * w
}
func (p PowerHeightGain) Height() float64 {
h := float64(p.HeightCode - '0')
if h <= 0 {
return 10
}
return math.Pow(2, h) * 10
}
func (p PowerHeightGain) Gain() int {
d := int(p.GainCode - '0')
if d <= 0 {
return 0
}
return d
}
func (p PowerHeightGain) Directivity() float64 {
d := int(p.DirectivityCode - '0')
if d <= 0 {
return 0
}
return float64(d%8) * 45.0
}
type OmniDFStrength struct {
StrengthCode byte
HeightCode byte
GainCode byte
DirectivityCode byte
}
func (o OmniDFStrength) Strength() int {
w := int(o.StrengthCode - '0')
if w <= 0 {
return 0
}
return w * w
}
func (o OmniDFStrength) Height() float64 {
h := float64(o.HeightCode - '0')
if h <= 0 {
return 10
}
return math.Pow(2, h) * 10
}
func (o OmniDFStrength) Gain() int {
d := int(o.GainCode - '0')
if d <= 0 {
return 0
}
return d
}
func (o OmniDFStrength) Directivity() float64 {
d := int(o.DirectivityCode - '0')
if d <= 0 {
return 0
}
return float64(d%8) * 45.0
}
type Packet struct {
Raw string
Src *Address
Dst *Address
Path Path
Payload Payload
Position *Position
Time *time.Time
Altitude float64 // Feet
Velocity Velocity
Wind Wind
PHG PowerHeightGain
DFS OmniDFStrength
Range float64 // Miles
Symbol Symbol
Comment string
data string // Unparsed data
}
func ParsePacket(raw string) (Packet, error) {
p := Packet{Raw: raw}
var i int
if i = strings.Index(raw, ":"); i < 0 {
return p, ErrInvalidPacket
}
p.Payload = Payload(raw[i+1:])
// Parse src, dst and path
var err error
var a = raw[:i]
if i = strings.Index(a, ">"); i < 0 {
return p, ErrInvalidPacket
}
if p.Src, err = ParseAddress(a[:i]); err != nil {
return p, err
}
var r = strings.Split(a[i+1:], ",")
if p.Dst, err = ParseAddress(r[0]); err != nil {
return p, err
}
if p.Path, err = ParsePath(strings.Join(r[1:], ",")); err != nil {
return p, err
}
// Post processing of payload
err = p.parse()
return p, err
}
func (p *Packet) parse() error {
s := string(p.Payload)
//log.Printf("parse %q [%c]\n", s, p.Payload.Type())
switch p.Payload.Type() {
case '!': // Lat/Long Position Report Format — without Timestamp
var o = strings.IndexByte(s, '!')
pos, txt, err := ParsePosition(s[o+1:], !isDigit(s[o+1]))
if err != nil {
return err
}
p.Position = &pos
p.data = txt
if len(s) >= 20 {
p.Symbol[0] = s[9]
p.Symbol[1] = s[19]
}
case '=':
compressed := IsValidCompressedSymTable(s[1])
pos, txt, err := ParsePosition(s[1:], compressed)
if err != nil {
return err
}
p.Position = &pos
p.data = txt
if compressed {
p.Symbol[0] = s[1]
p.Symbol[1] = s[10]
} else {
p.Symbol[0] = s[9]
p.Symbol[1] = s[19]
}
case '/', '@': // Lat/Long Position Report Format — with Timestamp
if len(s) < 8 {
return ErrInvalidPosition
}
var compressed bool
if s[7] == 'h' || s[7] == 'z' || s[7] == '/' {
if ts, err := ParseTime(s[1:]); err == nil {
p.Time = &ts
}
compressed = IsValidCompressedSymTable(s[8])
pos, txt, err := ParsePosition(s[8:], compressed)
if err != nil {
return err
}
p.Position = &pos
p.data = txt
} else if s[7] >= '0' && s[7] <= '9' {
ts, err := ParseTime(s[1:])
if err != nil {
return err
}
p.Time = &ts
compressed = IsValidCompressedSymTable(s[10])
pos, txt, err := ParsePosition(s[10:], compressed)
if err != nil {
return err
}
p.Position = &pos
p.data = txt
}
if compressed {
p.Symbol[0] = s[8]
p.Symbol[1] = s[17]
} else {
p.Symbol[0] = s[16]
p.Symbol[1] = s[26]
}
case ';':
pos, txt, err := ParsePosition(s[18:], !isDigit(s[18]))
if err != nil {
return err
}
p.Position = &pos
p.data = txt
case '[':
pos, txt, err := ParsePositionGrid(s[1:])
if err != nil {
return err
}
p.Position = &pos
p.data = txt
case '`', '\'':
pos, err := ParseMicE(s, p.Dst.Call)
if err != nil {
return err
}
p.Position = &pos
p.parseMicEData()
return nil // there is no additional data to parse
default:
pos, txt, err := ParsePositionBoth(s)
if err != nil {
return err
}
p.Position = &pos
p.data = txt
}
if p.Position != nil {
if p.Position.Compressed {
return p.parseCompressedData()
}
return p.parseData()
}
return nil
}
func (p *Packet) parseMicEData() error {
// APRS PROTOCOL REFERENCE 1.0.1 Chapter 10, page 42 in PDF
s := string(p.Payload)
// Mic-E Message Type
var mt []string
var t string
for i := 0; i < 3; i++ {
mc := miceCodes[rune(p.Dst.Call[i])][1]
if strings.HasSuffix(mc, "(Custom)") {
t = messageTypeCustom
} else if strings.HasSuffix(mc, "(Std)") {
t = messageTypeStd
}
mt = append(mt, string(mc[0]))
}
switch t {
case messageTypeStd:
mt = append(mt, " (Std)")
case messageTypeCustom:
mt = append(mt, " (Custom)")
}
p.Comment = miceMsgTypes[strings.Join(mt, "")]
// Speed and Course.
speed := float64(int(s[4])-28) * 10
dc := float64(int(s[5])-28) / 10
unit := float64(int(dc))
speed += unit
course := dc - unit
course += float64(int(s[6]) - 28)
if speed >= 800 {
speed -= 800
}
speed = 1.852 * speed // convert speed from knots to km/h
if course >= 400 {
course -= 400
}
// Symbol
p.Symbol[0] = s[7]
p.Symbol[1] = s[8]
p.Comment += fmt.Sprintf(" (%.fkm/h, %.f°)", speed, course)
// Check whether there's additional Telemetry or Status Text data.
if len(s) == 9 {
return nil
}
if s[9] == ',' || s[9] == '\x1d' {
// TODO: Parse telemetry data.
return nil
}
// Parse MicE Status Text data.
// TODO: Parse additional data in the Status Text data:
// - Actual (custom) text message
// - Maidenhead locator
// - Altitude
return nil
}
func (p *Packet) parseCompressedData() error {
// Parse csT bytes
if len(p.data) >= 3 {
// Compression Type (T) Byte Format
// Bit: 7 | 6 | 5 | 4 3 | 2 1 0 |
// -------+--------+---------+-------------+------------------+
// Unused | Unused | GPS Fix | NMEA Source | Origin |
// -------+--------+---------+-------------+------------------+
// Val: 0 | 0 | 0 = old | 00 = other | 000 = Compressed |
// | | 1 = cur | 01 = GLL | 001 = TNC BTex |
// | | | 10 = CGA | 010 = Software |
// | | | 11 = RMC | 011 = [tbd] |
// | | | | 100 = KPC3 |
// | | | | 101 = Pico |
// | | | | 110 = Other |
// | | | | 111 = Digipeater |
cb := p.data[0] - 33
sb := p.data[1] - 33
Tb := p.data[2] - 33
if p.data[0] != ' ' && ((Tb>>3)&3) == 2 {
// CGA sentence, NMEA Source = 0b10
d, err := base91Decode(p.data[0:2])
if err != nil {
return err
}
p.Altitude = math.Pow(1.002, float64(d))
p.Comment = p.data[3:]
} else if cb >= 0 && cb <= 89 { // !..z
// Course/Speed
p.Velocity.Course = float64(cb) * 4.0
p.Velocity.Speed = math.Pow(1.08, float64(sb)) - 1.0
} else if cb == 90 { // {
// Pre-Calculated Radio Range
p.Range = 2 * math.Pow(1.08, float64(sb))
}
}
return nil
}
func (p *Packet) parseData() error {
switch {
case len(p.data) >= 1 && p.data[0] == ' ':
p.Comment = p.data[1:]
case len(p.data) >= 7 && strings.HasPrefix(p.data, "PHG"):
p.PHG.PowerCode = p.data[3]
p.PHG.HeightCode = p.data[4]
p.PHG.GainCode = p.data[5]
p.PHG.DirectivityCode = p.data[6]
p.Range = math.Sqrt(2 * p.PHG.Height() * math.Sqrt((float64(p.PHG.Power())/10)*(float64(p.PHG.Gain())/2)))
p.Comment = p.data[7:]
case len(p.data) >= 7 && strings.HasPrefix(p.data, "RNG"):
var err error
p.Range, err = strconv.ParseFloat(p.data[3:7], 64)
if err != nil {
return err
}
p.Comment = p.data[7:]
case len(p.data) >= 7 && strings.HasPrefix(p.data, "DFS"):
p.DFS.StrengthCode = p.data[3]
p.DFS.HeightCode = p.data[4]
p.DFS.GainCode = p.data[5]
p.DFS.DirectivityCode = p.data[6]
p.Comment = p.data[7:]
}
return nil
}
func (p Payload) Time() (time.Time, error) {
switch p.Type() {
case '/', '@':
return ParseTime(string(p)[1:])
default:
return time.Time{}, nil
}
}