-
Notifications
You must be signed in to change notification settings - Fork 5
/
frame_tnc2.go
86 lines (74 loc) · 2.12 KB
/
frame_tnc2.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
// Copyright (c) 2016 Eric Barkie. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
// Refer to Automatic Position Reporting System (APRS) Protocol
// Reference - Protocol version 1.0.
package aprs
import (
"fmt"
"regexp"
"strings"
)
// String returns the address as a TNC2 formatted string.
func (a Addr) String() (addr string) {
addr = a.Call
if a.SSID > 0 {
addr += fmt.Sprintf("-%d", a.SSID)
}
if a.Repeated {
addr += "*"
}
return
}
// FromString sets the Frame from a TNC2 formatted string.
//
// This strictly enforces the AX.25 specification and will
// return errors if callsigns are greater than 6 characters or
// SSID's are not numeric values between 0 and 15.
func (f *Frame) FromString(frame string) (err error) {
// SRC>DST[,PATH]:TEXT
const reCall = "[[:alnum:]]{1,6}(?:-(?:[0-9]|1[0-5]))?"
re := regexp.MustCompile(fmt.Sprintf("^(%[1]s)>(%[1]s)((?:(?:,)(?:%[1]s\\*?))*):(.*)", reCall))
matches := re.FindStringSubmatch(frame)
if matches == nil {
err = ErrFrameInvalid
return
}
// Nothing should ever error unless there is a mistake
// in the regular expression.
err = f.Src.FromString(matches[1])
if err != nil {
return
}
err = f.Dst.FromString(matches[2])
if err != nil {
return
}
err = f.Path.FromString(strings.TrimLeft(matches[3], ","))
if err != nil {
return
}
f.Text = matches[4]
return
}
// String returns the Frame as a TNC2 formatted string. This is
// suitable for sending to APRS-IS servers.
func (f Frame) String() (frame string) {
// We have to manipulate the addresses a little because only
// the last repeated address should have an asterisk.
// Destination and source addresses
frame = fmt.Sprintf("%s>%s",
Addr{Call: f.Src.Call, SSID: f.Src.SSID},
Addr{Call: f.Dst.Call, SSID: f.Dst.SSID})
// Path (optional)
for i := 0; i < len(f.Path); i++ {
a := f.Path[i]
// Is there another address in the path and is it repeated?
if i+1 < len(f.Path) && f.Path[i+1].Repeated {
a.Repeated = false
}
frame += fmt.Sprintf(",%s", a)
}
frame += fmt.Sprintf(":%s", f.Text) // Information Field
return
}