-
Notifications
You must be signed in to change notification settings - Fork 5
/
frame.go
77 lines (66 loc) · 1.44 KB
/
frame.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
// 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.
package aprs
import (
"fmt"
"strconv"
"strings"
)
// Frame represents a complete APRS frame.
type Frame struct {
Dst Addr
Src Addr
Path Path
Text string
}
// Addr represents an APRS callsign, SSID, and associated
// metadata.
type Addr struct {
SSID int
Repeated bool
last bool
Call string
}
// Path represents the APRS digipath.
type Path []Addr
// FromString sets the address from a string.
func (a *Addr) FromString(addr string) (err error) {
if strings.HasSuffix(addr, "*") {
a.Repeated = true
addr = addr[:len(addr)-1]
}
dash := strings.Index(addr, "-")
if dash > -1 {
a.Call = addr[:dash]
a.SSID, err = strconv.Atoi(addr[dash+1:])
if err != nil {
err = fmt.Errorf("address error: SSID is invalid: %s", err.Error())
return
}
} else {
a.Call = addr
}
if len(a.Call) > 6 {
err = fmt.Errorf("address error: Callsign length %d > 6", len(a.Call))
return
}
if a.SSID < 0 || a.SSID > 15 {
err = fmt.Errorf("address error: %d not > 0 & < 15", a.SSID)
return
}
return
}
// FromString sets the Path from a string of comma separated
// addresses.
func (p *Path) FromString(path string) (err error) {
for _, as := range strings.Split(path, ",") {
a := Addr{}
err = a.FromString(as)
if err != nil {
return
}
*p = append(*p, a)
}
return
}