Initial import
This commit is contained in:
106
protocol/aprs/address.go
Normal file
106
protocol/aprs/address.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAddressInvalid = errors.New(`aprs: invalid address`)
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
Call string `json:"call"`
|
||||
SSID string `json:"ssid,omitempty"`
|
||||
IsRepeated bool `json:"is_repeated,omitempty"`
|
||||
}
|
||||
|
||||
func (a Address) EqualTo(b Address) bool {
|
||||
return a.Call == b.Call && a.SSID == b.SSID
|
||||
}
|
||||
|
||||
func (a Address) String() string {
|
||||
var r = ""
|
||||
|
||||
if a.IsRepeated {
|
||||
r = "*"
|
||||
}
|
||||
if a.SSID == "" {
|
||||
return a.Call + r
|
||||
}
|
||||
return fmt.Sprintf("%s-%s%s", a.Call, a.SSID, r)
|
||||
}
|
||||
|
||||
func (a Address) Secret() int16 {
|
||||
var h = int16(0x73e2)
|
||||
var c = a.Call
|
||||
|
||||
if len(c)%2 > 0 {
|
||||
c += "\x00"
|
||||
}
|
||||
for i := 0; i < len(c); i += 2 {
|
||||
h ^= int16(c[i]) << 8
|
||||
h ^= int16(c[i+1])
|
||||
}
|
||||
return h & 0x7fff
|
||||
}
|
||||
|
||||
func ParseAddress(s string) (Address, error) {
|
||||
r := strings.HasSuffix(s, "*")
|
||||
if r {
|
||||
s = s[:len(s)-1]
|
||||
}
|
||||
p := strings.Split(s, "-")
|
||||
if len(p) == 0 || len(p) > 2 {
|
||||
return Address{}, ErrAddressInvalid
|
||||
}
|
||||
|
||||
a := Address{Call: p[0], IsRepeated: r}
|
||||
if len(p) == 2 {
|
||||
a.SSID = p[1]
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func MustParseAddress(s string) Address {
|
||||
a, err := ParseAddress(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func IsQConstruct(call string) bool {
|
||||
return len(call) == 3 && call[0] == 'q'
|
||||
}
|
||||
|
||||
type Path []Address
|
||||
|
||||
func (p Path) String() string {
|
||||
var s = make([]string, len(p))
|
||||
for i, a := range p {
|
||||
s[i] = a.String()
|
||||
}
|
||||
return strings.Join(s, ",")
|
||||
}
|
||||
|
||||
func ParsePath(p string) (Path, error) {
|
||||
ss := strings.Split(p, ",")
|
||||
|
||||
if len(ss) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var err error
|
||||
as := make(Path, len(ss))
|
||||
for i, s := range ss {
|
||||
as[i], err = ParseAddress(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return as, nil
|
||||
}
|
||||
29
protocol/aprs/base91.go
Normal file
29
protocol/aprs/base91.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func base91Decode(s string) (n int, err error) {
|
||||
for i, l := 0, len(s); i < l; i++ {
|
||||
c := s[i]
|
||||
if c < 33 || c > 122 {
|
||||
return 0, fmt.Errorf("aprs: invalid base-91 encoding char %q (%d)", c, c)
|
||||
}
|
||||
|
||||
n *= 91
|
||||
n += int(c) - 33
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func base91Encode(n int) string {
|
||||
var s []string
|
||||
for n > 0 {
|
||||
c := n % 91
|
||||
n /= 91
|
||||
s = append([]string{string(byte(c) + 33)}, s...)
|
||||
}
|
||||
return strings.Join(s, "")
|
||||
}
|
||||
41
protocol/aprs/datatype.go
Normal file
41
protocol/aprs/datatype.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package aprs
|
||||
|
||||
import "fmt"
|
||||
|
||||
type DataType byte
|
||||
|
||||
var (
|
||||
dataTypeName = map[DataType]string{
|
||||
0x1c: "Current Mic-E Data (Rev 0 beta)",
|
||||
0x1d: "Old Mic-E Data (Rev 0 beta)",
|
||||
'!': "Position without timestamp (no APRS messaging), or Ultimeter 2000 WX Station",
|
||||
'#': "Peet Bros U-II Weather Station",
|
||||
'$': "Raw GPS data or Ultimeter 2000",
|
||||
'%': "Agrelo DFJr / MicroFinder",
|
||||
'"': "Old Mic-E Data (but Current data for TM-D700)",
|
||||
')': "Item",
|
||||
'*': "Peet Bros U-II Weather Station",
|
||||
',': "Invalid data or test data",
|
||||
'/': "Position with timestamp (no APRS messaging)",
|
||||
':': "Message",
|
||||
';': "Object",
|
||||
'<': "Station Capabilities",
|
||||
'=': "Position without timestamp (with APRS messaging)",
|
||||
'>': "Status",
|
||||
'?': "Query",
|
||||
'@': "Position with timestamp (with APRS messaging)",
|
||||
'T': "Telemetry data",
|
||||
'[': "Maidenhead grid locator beacon (obsolete)",
|
||||
'_': "Weather Report (without position)",
|
||||
'`': "Current Mic-E Data (not used in TM-D700)",
|
||||
'{': "User-Defined APRS packet format",
|
||||
'}': "Third-party traffic",
|
||||
}
|
||||
)
|
||||
|
||||
func (t DataType) String() string {
|
||||
if s, ok := dataTypeName[t]; ok {
|
||||
return s
|
||||
}
|
||||
return fmt.Sprintf("Unknown packet type %#02x", byte(t))
|
||||
}
|
||||
2
protocol/aprs/doc.go
Normal file
2
protocol/aprs/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package aprs implements Automatic Packet Reporting System message parsing.
|
||||
package aprs
|
||||
481
protocol/aprs/packet.go
Normal file
481
protocol/aprs/packet.go
Normal file
@@ -0,0 +1,481 @@
|
||||
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")
|
||||
)
|
||||
|
||||
// Payload is the raw payload contained within an APRS packet.
|
||||
type Payload string
|
||||
|
||||
// Type of payload.
|
||||
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) }
|
||||
|
||||
// Velocity details.
|
||||
type Velocity struct {
|
||||
Course float64 // Degrees
|
||||
Speed float64 // Knots
|
||||
}
|
||||
|
||||
// Wind details.
|
||||
type Wind struct {
|
||||
Direction float64 // Degrees
|
||||
Speed float64 // Knots
|
||||
}
|
||||
|
||||
// PowerHeightGain details.
|
||||
type PowerHeightGain struct {
|
||||
PowerCode byte
|
||||
HeightCode byte
|
||||
GainCode byte
|
||||
DirectivityCode byte
|
||||
}
|
||||
|
||||
// Power level (in Watts).
|
||||
func (p PowerHeightGain) Power() int {
|
||||
w := int(p.PowerCode - '0')
|
||||
if w <= 0 {
|
||||
return 0
|
||||
}
|
||||
return w * w
|
||||
}
|
||||
|
||||
// Height above ground (in meters).
|
||||
func (p PowerHeightGain) Height() float64 {
|
||||
h := float64(p.HeightCode - '0')
|
||||
if h <= 0 {
|
||||
return 10
|
||||
}
|
||||
return math.Pow(2, h) * 10
|
||||
}
|
||||
|
||||
// Gain level (in dBs).
|
||||
func (p PowerHeightGain) Gain() int {
|
||||
d := int(p.GainCode - '0')
|
||||
if d <= 0 {
|
||||
return 0
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Directivity angle.
|
||||
func (p PowerHeightGain) Directivity() float64 {
|
||||
d := int(p.DirectivityCode - '0')
|
||||
if d <= 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(d%8) * 45.0
|
||||
}
|
||||
|
||||
// OmniDFStrength contains the omni-directional direction finding signal strength (for fox hunting).
|
||||
type OmniDFStrength struct {
|
||||
StrengthCode byte
|
||||
HeightCode byte
|
||||
GainCode byte
|
||||
DirectivityCode byte
|
||||
}
|
||||
|
||||
// Strength of the signal.
|
||||
func (o OmniDFStrength) Strength() int {
|
||||
w := int(o.StrengthCode - '0')
|
||||
if w <= 0 {
|
||||
return 0
|
||||
}
|
||||
return w * w
|
||||
}
|
||||
|
||||
// Height above ground (in meters).
|
||||
func (o OmniDFStrength) Height() float64 {
|
||||
h := float64(o.HeightCode - '0')
|
||||
if h <= 0 {
|
||||
return 10
|
||||
}
|
||||
return math.Pow(2, h) * 10
|
||||
}
|
||||
|
||||
// Gain level (in dBs).
|
||||
func (o OmniDFStrength) Gain() int {
|
||||
d := int(o.GainCode - '0')
|
||||
if d <= 0 {
|
||||
return 0
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Directivity angle.
|
||||
func (o OmniDFStrength) Directivity() float64 {
|
||||
d := int(o.DirectivityCode - '0')
|
||||
if d <= 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(d%8) * 45.0
|
||||
}
|
||||
|
||||
// Packet contains an APRS packet.
|
||||
type Packet struct {
|
||||
// Raw packet (as captured from the air or APRS-IS).
|
||||
Raw string `json:"raw"`
|
||||
|
||||
// Src is the source address.
|
||||
Src Address `json:"src"`
|
||||
|
||||
// Dst is the destination address.
|
||||
Dst Address `json:"dst"`
|
||||
|
||||
// Path contains the digipeater path.
|
||||
Path Path `json:"path,omitempty"`
|
||||
|
||||
// Payload is the raw payload.
|
||||
Payload Payload `json:"payload"`
|
||||
|
||||
// Position encoded in the payload.
|
||||
Position *Position `json:"position,omitempty"`
|
||||
|
||||
// Time encoded in the payload.
|
||||
Time *time.Time `json:"time,omitempty"`
|
||||
|
||||
// Altitude encoded in the payload (in feet).
|
||||
Altitude float64 `json:"altitude,omitempty"`
|
||||
|
||||
// Velocity encoded in the payload.
|
||||
Velocity *Velocity `json:"velocity,omitempty"`
|
||||
|
||||
// Wind details encoded in the payload.
|
||||
Wind *Wind `json:"wind,omitempty"`
|
||||
|
||||
// PHG are the power, height and gain details encoded in the payload.
|
||||
PHG *PowerHeightGain `json:"phg,omitempty"`
|
||||
|
||||
// DFS are the direction finder strength details encoded in the payload.
|
||||
DFS *OmniDFStrength `json:"dfs,omitempty"`
|
||||
|
||||
// Range encoded in the payload (in miles).
|
||||
Range float64 `json:"range,omitempty"`
|
||||
|
||||
// Symbol encoded in the payload.
|
||||
Symbol Symbol `json:"symbol"`
|
||||
|
||||
// Comment encoded in the payload.
|
||||
Comment string `json:"comment,omitempty"`
|
||||
|
||||
// Unparsed data.
|
||||
data string
|
||||
}
|
||||
|
||||
// ParsePacket parses an APRS packet as captured from AX.25 or APRS-IS.
|
||||
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 {
|
||||
if err != ErrInvalidPosition {
|
||||
return err
|
||||
}
|
||||
p.Comment = s[1:]
|
||||
} else {
|
||||
p.Position = &pos
|
||||
p.Comment = 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 <= 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
|
||||
}
|
||||
}
|
||||
329
protocol/aprs/packet_test.go
Normal file
329
protocol/aprs/packet_test.go
Normal file
@@ -0,0 +1,329 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const earthRadius = float64(6378100)
|
||||
|
||||
func testTime(day, hour, min, sec int) *time.Time {
|
||||
t := time.Date(0, 0, day, hour, min, sec, 0, time.UTC)
|
||||
return &t
|
||||
}
|
||||
|
||||
// haversin(θ) function
|
||||
func testHaversin(theta float64) float64 {
|
||||
return math.Pow(math.Sin(theta/2), 2)
|
||||
}
|
||||
|
||||
func testDistance(a *Position, b *Position) float64 {
|
||||
var (
|
||||
lat1 = a.Latitude * math.Pi / 180
|
||||
lng1 = a.Longitude * math.Pi / 180
|
||||
lat2 = b.Latitude * math.Pi / 180
|
||||
lng2 = b.Longitude * math.Pi / 180
|
||||
h = testHaversin(lat2-lat1) + math.Cos(lat1)*math.Cos(lat2)*testHaversin(lng2-lng1)
|
||||
)
|
||||
return 2 * earthRadius * math.Asin(math.Sqrt(h))
|
||||
}
|
||||
|
||||
func TestPacket(t *testing.T) {
|
||||
var tests = []struct {
|
||||
Raw string
|
||||
Src Address
|
||||
Dst Address
|
||||
PathLen int
|
||||
Type DataType
|
||||
Position *Position
|
||||
Velocity *Velocity
|
||||
PHG *PowerHeightGain
|
||||
DFS *OmniDFStrength
|
||||
Altitude float64
|
||||
Range float64
|
||||
Time *time.Time
|
||||
}{
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:!4903.50N/07201.75W-Test 001234",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('!'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:!4903.50N/07201.75W-Test /A=001234",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('!'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:!49 . N/072 . W-",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('!'),
|
||||
Position: &Position{Latitude: 49.0, Longitude: -72.000000},
|
||||
},
|
||||
/*
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:TheNet X-1J4 (BFLD)!4903.50N/07201.75Wn",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('!'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
},
|
||||
*/
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:@092345/4903.50N/07201.75W>Test1234",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('@'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
Time: testTime(9, 23, 45, 0),
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:=4903.50N/07201.75W#PHG5132",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
PHG: &PowerHeightGain{'5', '1', '3', '2'},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:=4903.50N/07201.75W 225/000g000t050r000p001...h00b10138dU2k",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:@092345/4903.50N/07201.75W>088/036",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('@'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
Time: testTime(9, 23, 45, 0),
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:@234517h4903.50N/07201.75W>PHG5132",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('@'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
Time: testTime(0, 23, 45, 17),
|
||||
PHG: &PowerHeightGain{'5', '1', '3', '2'},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:@092345z4903.50N/07201.75W>RNG0050",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('@'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
Time: testTime(9, 23, 45, 0),
|
||||
Range: 50,
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:/234517h4903.50N/07201.75W>DFS2360",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('/'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
Time: testTime(0, 23, 45, 17),
|
||||
DFS: &OmniDFStrength{'2', '3', '6', '0'},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:@092345z4903.50N/07201.75W 090/000g000t066r000p000...dUII",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('@'),
|
||||
Position: &Position{Latitude: 49.058333, Longitude: -72.029167},
|
||||
Time: testTime(9, 23, 45, 00),
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:[IO91SX] 35 miles NNW of London",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('['),
|
||||
Position: &Position{Latitude: 51.958333, Longitude: -0.500000},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL>APRS,qAC:[IO91]",
|
||||
Src: MustParseAddress("N0CALL"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 1,
|
||||
Type: DataType('['),
|
||||
Position: &Position{Latitude: 51.0, Longitude: -2.0},
|
||||
},
|
||||
{
|
||||
Raw: "WX4GSO-9>APN382,qAR,WD4LSS:!3545.18NL07957.08W#PHG5680/R,W,85NC,NCn Mount Shepherd Piedmont Triad NC",
|
||||
Src: MustParseAddress("WX4GSO-9"),
|
||||
Dst: MustParseAddress("APN382"),
|
||||
PathLen: 2,
|
||||
Type: DataType('!'),
|
||||
Position: &Position{Latitude: 35.753000, Longitude: -79.951333},
|
||||
},
|
||||
{
|
||||
Raw: "PA4TW>APRS,qAS,PA4TW-2:=5216.28N/00510.05Er Remco, DMR:2041014, Soms QRV op PI2NOS",
|
||||
Src: MustParseAddress("PA4TW"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 2,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 52.271333, Longitude: 5.167500},
|
||||
},
|
||||
{
|
||||
Raw: "PA4TW-10>APRS,TCPIP*,qAC,FOURTH:=5220.18N/00453.25EIhttp://aprs.pa4tw.nl:14501/",
|
||||
Src: MustParseAddress("PA4TW-10"),
|
||||
Dst: MustParseAddress("APRS"),
|
||||
PathLen: 3,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 52.336333, Longitude: 4.887500},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL-1>T3PY1Y,KQ1L-8*,WIDE1,WIDE2-1,qAR:=/5L!!<*e7> sTComment",
|
||||
Src: MustParseAddress("N0CALL-1"),
|
||||
Dst: MustParseAddress("T3PY1Y"),
|
||||
PathLen: 4,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 49.5, Longitude: -72.75},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL-1>T3PY1Y,KQ1L-8*,WIDE1,WIDE2-1,qAR:=/5L!!<*e7>7P[",
|
||||
Src: MustParseAddress("N0CALL-1"),
|
||||
Dst: MustParseAddress("T3PY1Y"),
|
||||
PathLen: 4,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 49.5, Longitude: -72.75},
|
||||
Velocity: &Velocity{88.0, 36.2},
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL-1>T3PY1Y,KQ1L-8*,WIDE1,WIDE2-1,qAR:=/5L!!<*e7>{?!",
|
||||
Src: MustParseAddress("N0CALL-1"),
|
||||
Dst: MustParseAddress("T3PY1Y"),
|
||||
PathLen: 4,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 49.5, Longitude: -72.75},
|
||||
Range: 20.13,
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL-1>T3PY1Y,KQ1L-8*,WIDE1,WIDE2-1,qAR:=/5L!!<*e7OS]S",
|
||||
Src: MustParseAddress("N0CALL-1"),
|
||||
Dst: MustParseAddress("T3PY1Y"),
|
||||
PathLen: 4,
|
||||
Type: DataType('='),
|
||||
Position: &Position{Latitude: 49.5, Longitude: -72.75},
|
||||
Altitude: 10004,
|
||||
},
|
||||
{
|
||||
Raw: "N0CALL-1>T3PY1Y,KQ1L-8*,WIDE1,WIDE2-1,qAR:@092345z/5L!!<*e7>{?!",
|
||||
Src: MustParseAddress("N0CALL-1"),
|
||||
Dst: MustParseAddress("T3PY1Y"),
|
||||
PathLen: 4,
|
||||
Type: '@',
|
||||
Time: testTime(9, 23, 45, 0),
|
||||
Position: &Position{Latitude: 49.5, Longitude: -72.75},
|
||||
Range: 20.13,
|
||||
},
|
||||
{
|
||||
Raw: "PE1ROG-8>APLC13,qAS,PE1ROG-2:!/49[<P'PC>8pQLoRa___MOBIL___TEST",
|
||||
Src: MustParseAddress("PE1ROG-8"),
|
||||
Dst: MustParseAddress("APLC13"),
|
||||
PathLen: 2,
|
||||
Type: '!',
|
||||
},
|
||||
{
|
||||
Raw: "PE1ROG-1>APPM13,TCPIP*,qAC,T2PRT:>Running on Raspberry Pi with RTL dongle",
|
||||
Src: MustParseAddress("PE1ROG-1"),
|
||||
Dst: MustParseAddress("APPM13"),
|
||||
PathLen: 3,
|
||||
Type: '>',
|
||||
},
|
||||
{
|
||||
Raw: "PD0MZ-4>APE32L,WIDE1-1,qAR,PD0MZ-10:>LoRa APRS Tracker",
|
||||
Src: MustParseAddress("PD0MZ-4"),
|
||||
Dst: MustParseAddress("APE32L"),
|
||||
PathLen: 3,
|
||||
Type: '>',
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.Raw, func(t *testing.T) {
|
||||
p, err := ParsePacket(test.Raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !p.Dst.EqualTo(test.Dst) {
|
||||
t.Fatalf("expected dst %s, got %s", test.Dst, p.Dst)
|
||||
}
|
||||
if !p.Src.EqualTo(test.Src) {
|
||||
t.Fatalf("expected src %s, got %s", test.Src, p.Src)
|
||||
}
|
||||
if len(p.Path) != test.PathLen {
|
||||
t.Fatalf("expected path length %d, got %d: %v", test.PathLen, len(p.Path), p.Path)
|
||||
}
|
||||
if p.Payload.Type() != test.Type {
|
||||
t.Fatalf("expected packet type %s [%c], got %s [%c]",
|
||||
test.Type, test.Type,
|
||||
p.Payload.Type(), p.Payload.Type())
|
||||
}
|
||||
if test.Altitude != 0 {
|
||||
if p.Altitude == 0 {
|
||||
t.Fatalf("expected altitude %f, got none", test.Altitude)
|
||||
}
|
||||
if math.Abs(test.Altitude-p.Altitude) > 1.0 {
|
||||
t.Fatalf("expected altitude %f, got %f", test.Altitude, p.Altitude)
|
||||
}
|
||||
}
|
||||
if test.Velocity != nil {
|
||||
if p.Velocity.Course == 0 {
|
||||
t.Fatalf("expected velocity %v, got none", test.Velocity)
|
||||
}
|
||||
if math.Abs(test.Velocity.Course-p.Velocity.Course) > 1.0 {
|
||||
t.Fatalf("expected course %f, got %f", test.Velocity.Course, p.Velocity.Course)
|
||||
}
|
||||
if math.Abs(test.Velocity.Speed-p.Velocity.Speed) > 1.0 {
|
||||
t.Fatalf("expected speed %f, got %f", test.Velocity.Speed, p.Velocity.Speed)
|
||||
}
|
||||
}
|
||||
if test.Range != 0 {
|
||||
if p.Range == 0 {
|
||||
t.Fatalf("expected range %f, got none", test.Range)
|
||||
}
|
||||
if math.Abs(test.Range-p.Range) > 0.1 {
|
||||
t.Fatalf("expected range %f, got %f", test.Range, p.Range)
|
||||
}
|
||||
}
|
||||
if test.Position != nil {
|
||||
if p.Position == nil {
|
||||
t.Fatalf("expected position %s, got none", test.Position)
|
||||
}
|
||||
if d := testDistance(test.Position, p.Position); d > 1.0 {
|
||||
t.Fatalf("expected position %s, got %s with distance %f meter", test.Position, p.Position, d)
|
||||
}
|
||||
}
|
||||
if test.Time != nil {
|
||||
if p.Time == nil {
|
||||
t.Fatalf("expected time %s", test.Time)
|
||||
}
|
||||
if test.Time.Sub(*p.Time) > time.Minute {
|
||||
t.Fatalf("expected time %s, got %s", test.Time, p.Time)
|
||||
}
|
||||
}
|
||||
//t.Logf("%#+v", p)
|
||||
})
|
||||
}
|
||||
}
|
||||
313
protocol/aprs/position.go
Normal file
313
protocol/aprs/position.go
Normal file
@@ -0,0 +1,313 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.maze.io/go/ham/util/maidenhead"
|
||||
)
|
||||
|
||||
var (
|
||||
// Position ambiguity replacement
|
||||
disambiguation = []int{2, 3, 5, 6, 12, 13, 15, 16}
|
||||
|
||||
miceCodes = map[rune]map[int]string{
|
||||
'0': {0: "0", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'1': {0: "1", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'2': {0: "2", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'3': {0: "3", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'4': {0: "4", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'5': {0: "5", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'6': {0: "6", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'7': {0: "7", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'8': {0: "8", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'9': {0: "9", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'A': {0: "0", 1: "1 (Custom)"},
|
||||
'B': {0: "1", 1: "1 (Custom)"},
|
||||
'C': {0: "2", 1: "1 (Custom)"},
|
||||
'D': {0: "3", 1: "1 (Custom)"},
|
||||
'E': {0: "4", 1: "1 (Custom)"},
|
||||
'F': {0: "5", 1: "1 (Custom)"},
|
||||
'G': {0: "6", 1: "1 (Custom)"},
|
||||
'H': {0: "7", 1: "1 (Custom)"},
|
||||
'I': {0: "8", 1: "1 (Custom)"},
|
||||
'J': {0: "9", 1: "1 (Custom)"},
|
||||
'K': {0: " ", 1: "1 (Custom)"},
|
||||
'L': {0: " ", 1: "0", 2: "S", 3: "0", 4: "E"},
|
||||
'P': {0: "0", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'Q': {0: "1", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'R': {0: "2", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'S': {0: "3", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'T': {0: "4", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'U': {0: "5", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'V': {0: "6", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'W': {0: "7", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'X': {0: "8", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'Y': {0: "9", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
'Z': {0: " ", 1: "1 (Std)", 2: "N", 3: "100", 4: "W"},
|
||||
}
|
||||
|
||||
miceMsgTypes = map[string]string{
|
||||
"000": "Emergency",
|
||||
"001 (Std)": "Priority",
|
||||
"001 (Custom)": "Custom-6",
|
||||
"010 (Std)": "Special",
|
||||
"010 (Custom)": "Custom-5",
|
||||
"011 (Std)": "Committed",
|
||||
"011 (Custom)": "Custom-4",
|
||||
"100 (Std)": "Returning",
|
||||
"100 (Custom)": "Custom-3",
|
||||
"101 (Std)": "In Service",
|
||||
"101 (Custom)": "Custom-2",
|
||||
"110 (Std)": "En Route",
|
||||
"110 (Custom)": "Custom-1",
|
||||
"111 (Std)": "Off Duty",
|
||||
"111 (Custom)": "Custom-0",
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
gridChars = "ABCDEFGHIJKLMNOPQRSTUVWX0123456789"
|
||||
|
||||
messageTypeStd = "Std"
|
||||
messageTypeCustom = "Custom"
|
||||
)
|
||||
|
||||
// Position contains GPS coordinates.
|
||||
type Position struct {
|
||||
Latitude float64 `json:"latitude"` // Degrees
|
||||
Longitude float64 `json:"longitude"` // Degrees
|
||||
Ambiguity int `json:"ambiguity,omitempty"`
|
||||
Symbol Symbol `json:"symbol"`
|
||||
Compressed bool `json:"compressed,omitempty"`
|
||||
}
|
||||
|
||||
func (pos Position) String() string {
|
||||
if pos.Ambiguity == 0 {
|
||||
return fmt.Sprintf("{%f, %f}", pos.Latitude, pos.Longitude)
|
||||
}
|
||||
return fmt.Sprintf("{%f, %f}, ambiguity=%d", pos.Latitude, pos.Longitude, pos.Ambiguity)
|
||||
}
|
||||
|
||||
func ParseUncompressedPosition(s string) (Position, string, error) {
|
||||
// APRS PROTOCOL REFERENCE 1.0.1 Chapter 8, page 32 (42 in PDF)
|
||||
|
||||
pos := Position{}
|
||||
|
||||
if len(s) < 18 {
|
||||
return pos, "", ErrInvalidPosition
|
||||
}
|
||||
|
||||
b := []byte(s)
|
||||
for _, p := range disambiguation {
|
||||
if b[p] == ' ' {
|
||||
pos.Ambiguity++
|
||||
b[p] = '0'
|
||||
}
|
||||
}
|
||||
s = string(b)
|
||||
|
||||
var (
|
||||
err error
|
||||
latDeg, latMin, latMinFrag uint64
|
||||
lngDeg, lngMin, lngMinFrag uint64
|
||||
latHemi, lngHemi byte
|
||||
isSouth, isWest bool
|
||||
)
|
||||
|
||||
if latDeg, err = strconv.ParseUint(s[0:2], 10, 8); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
if latMin, err = strconv.ParseUint(s[2:4], 10, 8); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
if latMinFrag, err = strconv.ParseUint(s[5:7], 10, 8); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
latHemi = s[7]
|
||||
pos.Symbol[0] = s[8]
|
||||
if lngDeg, err = strconv.ParseUint(s[9:12], 10, 8); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
if lngMin, err = strconv.ParseUint(s[12:14], 10, 8); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
if lngMinFrag, err = strconv.ParseUint(s[15:17], 10, 8); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
lngHemi = s[17]
|
||||
pos.Symbol[1] = s[18]
|
||||
|
||||
if latHemi == 'S' || latHemi == 's' {
|
||||
isSouth = true
|
||||
} else if latHemi != 'N' && latHemi != 'n' {
|
||||
return pos, "", ErrInvalidPosition
|
||||
}
|
||||
|
||||
if lngHemi == 'W' || lngHemi == 'w' {
|
||||
isWest = true
|
||||
} else if lngHemi != 'E' && lngHemi != 'e' {
|
||||
return pos, "", ErrInvalidPosition
|
||||
}
|
||||
|
||||
if latDeg > 89 || lngDeg > 179 {
|
||||
return pos, "", ErrInvalidPosition
|
||||
}
|
||||
|
||||
pos.Latitude = float64(latDeg) + float64(latMin)/60.0 + float64(latMinFrag)/6000.0
|
||||
pos.Longitude = float64(lngDeg) + float64(lngMin)/60.0 + float64(lngMinFrag)/6000.0
|
||||
|
||||
if isSouth {
|
||||
pos.Latitude = 0.0 - pos.Latitude
|
||||
}
|
||||
if isWest {
|
||||
pos.Longitude = 0.0 - pos.Longitude
|
||||
}
|
||||
|
||||
if pos.Symbol[1] >= 'a' || pos.Symbol[1] <= 'k' {
|
||||
pos.Symbol[1] -= 32
|
||||
}
|
||||
|
||||
if len(s) > 19 {
|
||||
return pos, s[19:], nil
|
||||
}
|
||||
return pos, "", nil
|
||||
}
|
||||
|
||||
func ParseCompressedPosition(s string) (Position, string, error) {
|
||||
// APRS PROTOCOL REFERENCE 1.0.1 Chapter 9, page 36 (46 in PDF)
|
||||
|
||||
pos := Position{}
|
||||
|
||||
if len(s) < 10 {
|
||||
return pos, "", ErrInvalidPosition
|
||||
}
|
||||
|
||||
// Base-91 check
|
||||
for _, c := range s[1:9] {
|
||||
if c < 0x21 || c > 0x7b {
|
||||
return pos, "", ErrInvalidPosition
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
var lat, lng int
|
||||
if lat, err = base91Decode(s[1:5]); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
if lng, err = base91Decode(s[5:9]); err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
|
||||
pos.Latitude = 90.0 - float64(lat)/380926.0
|
||||
pos.Longitude = -180.0 + float64(lng)/190463.0
|
||||
pos.Compressed = true
|
||||
|
||||
return pos, s[10:], nil
|
||||
}
|
||||
|
||||
func ParseMicE(s, dest string) (Position, error) {
|
||||
// APRS PROTOCOL REFERENCE 1.0.1 Chapter 10, page 42 in PDF
|
||||
|
||||
pos := Position{}
|
||||
|
||||
if len(s) < 9 || len(dest) != 6 {
|
||||
return pos, ErrInvalidPosition
|
||||
}
|
||||
|
||||
ns := miceCodes[rune(dest[3])][2]
|
||||
we := miceCodes[rune(dest[5])][4]
|
||||
|
||||
latF := fmt.Sprintf("%s%s", miceCodes[rune(dest[0])][0], miceCodes[rune(dest[1])][0])
|
||||
latF = strings.Trim(latF, ". ")
|
||||
latD, err := strconv.ParseFloat(latF, 64)
|
||||
if err != nil {
|
||||
return pos, ErrInvalidPosition
|
||||
}
|
||||
lonF := fmt.Sprintf("%s%s.%s%s", miceCodes[rune(dest[2])][0], miceCodes[rune(dest[3])][0], miceCodes[rune(dest[4])][0], miceCodes[rune(dest[5])][0])
|
||||
lonF = strings.Trim(lonF, ". ")
|
||||
latM, err := strconv.ParseFloat(lonF, 64)
|
||||
if err != nil {
|
||||
return pos, ErrInvalidPosition
|
||||
}
|
||||
if latM != 0 {
|
||||
latD += latM / 60
|
||||
}
|
||||
if strings.ToUpper(ns) == "S" {
|
||||
latD = -latD
|
||||
}
|
||||
|
||||
lonOff := miceCodes[rune(dest[4])][3]
|
||||
lonD := float64(s[1]) - 28
|
||||
if lonOff == "100" {
|
||||
lonD += 100
|
||||
}
|
||||
if lonD >= 180 && lonD < 190 {
|
||||
lonD -= 80
|
||||
}
|
||||
if lonD >= 190 && lonD < 200 {
|
||||
lonD -= 190
|
||||
}
|
||||
|
||||
lonM := float64(s[2]) - 28
|
||||
if lonM >= 60 {
|
||||
lonM -= 60
|
||||
}
|
||||
// adding hundreth of minute then add minute as deg fraction
|
||||
lonH := float64(s[3]) - 28
|
||||
if lonH != 0 {
|
||||
lonM += lonH / 100
|
||||
}
|
||||
if lonM != 0 {
|
||||
lonD += lonM / 60
|
||||
}
|
||||
if strings.ToUpper(we) == "W" {
|
||||
lonD = -lonD
|
||||
}
|
||||
|
||||
pos.Latitude = latD
|
||||
pos.Longitude = lonD
|
||||
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
func ParsePositionGrid(s string) (Position, string, error) {
|
||||
var o int
|
||||
for o = 0; o < len(s); o++ {
|
||||
if strings.IndexByte(gridChars, s[o]) < 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
pos := Position{}
|
||||
if o == 2 || o == 4 || o == 6 || o == 8 {
|
||||
p, err := maidenhead.ParseLocator(s[:o])
|
||||
if err != nil {
|
||||
return pos, "", err
|
||||
}
|
||||
pos.Latitude = p.Latitude
|
||||
pos.Longitude = p.Longitude
|
||||
}
|
||||
|
||||
var txt string
|
||||
if o < len(s) {
|
||||
txt = s[o+1:]
|
||||
}
|
||||
return pos, txt, nil
|
||||
}
|
||||
|
||||
func ParsePosition(s string, compressed bool) (Position, string, error) {
|
||||
if compressed {
|
||||
return ParseCompressedPosition(s)
|
||||
}
|
||||
return ParseUncompressedPosition(s)
|
||||
}
|
||||
|
||||
func ParsePositionBoth(s string) (Position, string, error) {
|
||||
pos, txt, err := ParseUncompressedPosition(s)
|
||||
if err != nil {
|
||||
return ParseCompressedPosition(s)
|
||||
}
|
||||
return pos, txt, err
|
||||
}
|
||||
266
protocol/aprs/symbol.go
Normal file
266
protocol/aprs/symbol.go
Normal file
@@ -0,0 +1,266 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var emptySymbol Symbol
|
||||
|
||||
type Symbol [2]byte
|
||||
|
||||
func (s Symbol) IsPrimaryTable() bool { return s[0] != '\\' }
|
||||
|
||||
func (s Symbol) MarshalJSON() ([]byte, error) {
|
||||
if s == emptySymbol {
|
||||
return json.Marshal("")
|
||||
}
|
||||
return json.Marshal(string(s[:]))
|
||||
}
|
||||
|
||||
func (s Symbol) get(idx int) (string, error) {
|
||||
var m map[byte]map[int]string
|
||||
if s.IsPrimaryTable() {
|
||||
m = primarySymbol
|
||||
} else {
|
||||
m = alternateSymbol
|
||||
}
|
||||
n, ok := m[s[1]]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unknown symbol %x", s[1])
|
||||
}
|
||||
if i, ok := n[idx]; ok {
|
||||
return i, nil
|
||||
}
|
||||
return "", fmt.Errorf("symbol doesn't have requested index: %v", n)
|
||||
}
|
||||
|
||||
func (s Symbol) String() string {
|
||||
hr, err := s.get(1)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return hr
|
||||
}
|
||||
|
||||
func (s Symbol) SSID() (string, error) {
|
||||
return s.get(2)
|
||||
}
|
||||
|
||||
func (s Symbol) Emoji() (string, error) {
|
||||
return s.get(3)
|
||||
}
|
||||
|
||||
func IsValidCompressedSymTable(c byte) bool {
|
||||
return c == '/' ||
|
||||
c == '\\' ||
|
||||
(c >= 0x41 && c <= 0x5a) ||
|
||||
(c >= 0x61 && c <= 0x6a)
|
||||
}
|
||||
|
||||
func IsValidUncompressedSymTable(c byte) bool {
|
||||
return c == '/' ||
|
||||
c == '\\' ||
|
||||
(c >= 0x41 && c <= 0x5a) ||
|
||||
(c >= 0x30 && c <= 0x39)
|
||||
}
|
||||
|
||||
var (
|
||||
// Source: http://www.aprs.org/symbols/symbolsX.txt
|
||||
// 0: XYZ code
|
||||
// 1: Human readable
|
||||
// 2: SSID
|
||||
// 3: Emoji
|
||||
primarySymbol = map[byte]map[int]string{
|
||||
'!': {0: "BB", 1: "Police, Sheriff", 3: ":cop:"},
|
||||
'"': {0: "BC", 1: "reserved"},
|
||||
'#': {0: "BD", 1: "Digi"},
|
||||
'$': {0: "BE", 1: "Phone", 3: ":phone:"},
|
||||
'%': {0: "BF", 1: "DX Cluster"},
|
||||
'&': {0: "BG", 1: "HF Gateway"},
|
||||
'\'': {0: "BH", 1: "Small Aircraft", 2: "11", 3: ":airplane:"},
|
||||
'(': {0: "BI", 1: "Mobile Satellite Station", 3: ":satellite:"},
|
||||
')': {0: "BJ", 1: "Wheelchair", 3: ":wheelchair:"},
|
||||
'*': {0: "BK", 1: "Snowmobile"},
|
||||
'+': {0: "BL", 1: "Red Cross"},
|
||||
',': {0: "BM", 1: "Boy Scout"},
|
||||
'-': {0: "BN", 1: "House QTH (VHF)"},
|
||||
'.': {0: "BO", 1: "X"},
|
||||
'/': {0: "BP", 1: "Red Dot"},
|
||||
'0': {0: "P0", 1: "Circle (0)"},
|
||||
'1': {0: "P1", 1: "Circle (1)"},
|
||||
'2': {0: "P2", 1: "Circle (2)"},
|
||||
'3': {0: "P3", 1: "Circle (3)"},
|
||||
'4': {0: "P4", 1: "Circle (4)"},
|
||||
'5': {0: "P5", 1: "Circle (5)"},
|
||||
'6': {0: "P6", 1: "Circle (6)"},
|
||||
'7': {0: "P7", 1: "Circle (7)"},
|
||||
'8': {0: "P8", 1: "Circle (8)"},
|
||||
'9': {0: "P9", 1: "Circle (9)"},
|
||||
':': {0: "MR", 1: "Fire", 3: ":fire:"},
|
||||
';': {0: "MS", 1: "Campground", 3: ":tent:"},
|
||||
'<': {0: "MT", 1: "Motorcycle", 2: "10", 3: ":bike:"},
|
||||
'=': {0: "MU", 1: "Railroad Engine", 3: ":train:"},
|
||||
'>': {0: "MV", 1: "Car", 2: "9", 3: ":car:"},
|
||||
'?': {0: "MW", 1: "File Server"},
|
||||
'@': {0: "MX", 1: "HC Future"},
|
||||
'A': {0: "PA", 1: "Aid Station", 3: ":hospital:"},
|
||||
'B': {0: "PB", 1: "BBS or PBBS"},
|
||||
'C': {0: "PC", 1: "Canoe"},
|
||||
'D': {0: "PD"},
|
||||
'E': {0: "PE", 1: "Eyeball"},
|
||||
'F': {0: "PF", 1: "Tractor", 3: ":tractor:"},
|
||||
'G': {0: "PG", 1: "Grid Square"},
|
||||
'H': {0: "PH", 1: "Hotel", 3: ":hotel:"},
|
||||
'I': {0: "PI", 1: "TCP/IP"},
|
||||
'J': {0: "PJ"},
|
||||
'K': {0: "PK", 1: "School", 3: ":school:"},
|
||||
'L': {0: "PL", 1: "PC User", 3: ":computer:"},
|
||||
'M': {0: "PM", 1: "MacAPRS", 3: ":computer:"},
|
||||
'N': {0: "PN", 1: "NTS Station"},
|
||||
'O': {0: "PO", 1: "Balloon", 2: "11", 3: ":airplane:"},
|
||||
'P': {0: "PP", 1: "Police", 3: ":police_car:"},
|
||||
'Q': {0: "PQ"},
|
||||
'R': {0: "PR", 1: "Recreational Vehicle", 2: "13", 3: ":car:"},
|
||||
'S': {0: "PS", 1: "Shuttle"},
|
||||
'T': {0: "PT", 1: "SSTV"},
|
||||
'U': {0: "PU", 1: "Bus", 2: "2", 3: ":bus:"},
|
||||
'V': {0: "PV", 1: "ATV"},
|
||||
'W': {0: "PW", 1: "National WX Service Site"},
|
||||
'X': {0: "PX", 1: "Helo", 2: "6"},
|
||||
'Y': {0: "PY", 1: "Yacht", 2: "5", 3: ":sailboat:"},
|
||||
'Z': {0: "PZ", 1: "WinAPRS", 3: ":computer:"},
|
||||
'[': {0: "HS", 1: "Human/Person", 2: "7", 3: ":running:"},
|
||||
'\\': {0: "HT", 1: "DF Station"},
|
||||
']': {0: "HU", 1: "Post Office", 3: ":post_office:"},
|
||||
'^': {0: "HV", 1: "Large Aircraft", 3: ":airplane:"},
|
||||
'_': {0: "HW", 1: "Weather Station", 3: ":cloud:"},
|
||||
'`': {0: "HX", 1: "Dish Antenna", 3: ":satellite:"},
|
||||
'a': {0: "LA", 1: "Ambulance", 2: "1", 3: ":ambulance:"},
|
||||
'b': {0: "LB", 1: "Bike", 2: "4", 3: ":bike:"},
|
||||
'c': {0: "LC", 1: "Incident Command Post"},
|
||||
'd': {0: "LD", 1: "Fire Dept", 3: ":fire_engine:"},
|
||||
'e': {0: "LE", 1: "Horse", 3: ":racehorse:"},
|
||||
'f': {0: "LF", 1: "Fire Truck", 2: "3", 3: ":fire_engine:"},
|
||||
'g': {0: "LG", 1: "Glider", 3: ":airplane:"},
|
||||
'h': {0: "LH", 1: "Hospital", 3: ":hospital:"},
|
||||
'i': {0: "LI", 1: "IOTA"},
|
||||
'j': {0: "LJ", 1: "Jeep", 2: "12", 3: ":car:"},
|
||||
'k': {0: "LK", 1: "Truck", 2: "14", 3: ":truck:"},
|
||||
'l': {0: "LL", 1: "Laptop", 3: ":computer:"},
|
||||
'm': {0: "LM", 1: "Mic-E Repeater"},
|
||||
'n': {0: "LN", 1: "Node"},
|
||||
'o': {0: "LO", 1: "EOC"},
|
||||
'p': {0: "LP", 1: "Dog", 3: ":dog2:"},
|
||||
'q': {0: "LQ", 1: "Grid SQ"},
|
||||
'r': {0: "LR", 1: "Repeater"},
|
||||
's': {0: "LS", 1: "Ship", 2: "8", 3: ":ship:"},
|
||||
't': {0: "LT", 1: "Truck Stop"},
|
||||
'u': {0: "LU", 1: "Truck (18 Wheeler)", 3: ":truck:"},
|
||||
'v': {0: "LV", 1: "Van", 2: "15", 3: ":minibus:"},
|
||||
'w': {0: "LW", 1: "Water Station"},
|
||||
'x': {0: "LX", 1: "xAPRS", 3: ":computer:"},
|
||||
'y': {0: "LY", 1: "Yagi @ QTH"},
|
||||
'z': {0: "LZ"},
|
||||
'{': {0: "J1"},
|
||||
'|': {0: "J2", 1: "TNC Stream Switch"},
|
||||
'}': {0: "J3"},
|
||||
'~': {0: "J4", 1: "TNC Stream Switch"},
|
||||
}
|
||||
alternateSymbol = map[byte]map[int]string{
|
||||
'!': {0: "OBO", 1: "Emergency"},
|
||||
'"': {0: "OC", 1: "Reserved"},
|
||||
'#': {0: "OD#", 1: "Overlay Digi"},
|
||||
'$': {0: "OEO", 1: "Bank/ATM", 3: ":atm:"},
|
||||
'%': {0: "OFO", 1: "Power Plant", 3: ":factory:"},
|
||||
'&': {0: "OG#", 1: "I=Igte R=RX T=1hopTX 2=2hopTX"},
|
||||
'\'': {0: "OHO", 1: "Crash Site"},
|
||||
'(': {0: "OIO", 1: "Cloudy", 3: ":cloud:"},
|
||||
')': {0: "OJO", 1: "Firenet MEO"},
|
||||
'*': {0: "OK"},
|
||||
'+': {0: "OL", 1: "Church", 3: ":church:"},
|
||||
',': {0: "OM", 1: "Girl Scouts", 3: ":tent:"},
|
||||
'-': {0: "ONO", 1: "House", 3: ":house:"},
|
||||
'.': {0: "OO", 1: "Ambiguous"},
|
||||
'/': {0: "OP", 1: "Waypoint Destination"},
|
||||
'0': {0: "A0#", 1: "Circle", 3: ":red_circle:"},
|
||||
'1': {0: "A1"},
|
||||
'2': {0: "A2"},
|
||||
'3': {0: "A3"},
|
||||
'4': {0: "A4"},
|
||||
'5': {0: "A5"},
|
||||
'6': {0: "A6"},
|
||||
'7': {0: "A7"},
|
||||
'8': {0: "A8O", 1: "WiFi Network"},
|
||||
'9': {0: "A9", 1: "Gas Station", 3: ":fuelpump:"},
|
||||
':': {0: "NR"},
|
||||
';': {0: "NSO", 1: "Park/Picnic"},
|
||||
'<': {0: "NTO", 1: "Advisory"},
|
||||
'=': {0: "NUO"},
|
||||
'>': {0: "NV#", 1: "Cars & Vehicles", 3: ":car:"},
|
||||
'?': {0: "NW", 1: "Info Kiosk"},
|
||||
'@': {0: "NX", 1: "Hurricane", 3: ":cyclone:"},
|
||||
'A': {0: "AA#", 1: "Box DTMF & RFID"},
|
||||
'B': {0: "AB"},
|
||||
'C': {0: "AC", 1: "Coast Guard"},
|
||||
'D': {0: "ADO", 1: "Depots"},
|
||||
'E': {0: "AE", 1: "Smoke"},
|
||||
'F': {0: "AF"},
|
||||
'G': {0: "AG"},
|
||||
'H': {0: "AHO", 1: "Haze"},
|
||||
'I': {0: "AI", 1: "Rain Shower", 3: ":umbrella:"},
|
||||
'J': {0: "AJ"},
|
||||
'K': {0: "AK", 1: "Kenwood HT"},
|
||||
'L': {0: "AL", 1: "Lighthouse"},
|
||||
'M': {0: "AMO", 1: "MARS"},
|
||||
'N': {0: "AN", 1: "Navigation Buoy"},
|
||||
'O': {0: "AO", 1: "Rocket", 3: ":rocket:"},
|
||||
'P': {0: "AP", 1: "Parking", 3: ":parking:"},
|
||||
'Q': {0: "AQ", 1: "Quake"},
|
||||
'R': {0: "ARO", 1: "Restaurant"},
|
||||
'S': {0: "AS", 1: "Satellite/Pacsat", 3: ":rocket:"},
|
||||
'T': {0: "AT", 1: "Thunderstorm"},
|
||||
'U': {0: "AU", 1: "Sunny"},
|
||||
'V': {0: "AV", 1: "VORTAC Nav Aid"},
|
||||
'W': {0: "AW#", 1: "NWS Site"},
|
||||
'X': {0: "AX", 1: "Pharmacy"},
|
||||
'Y': {0: "AYO", 1: "Radios and devices"},
|
||||
'Z': {0: "AZ"},
|
||||
'[': {0: "DSO", 1: "W. Cloud"},
|
||||
'\\': {0: "DTO", 1: "GPS"},
|
||||
']': {0: "DU"},
|
||||
'^': {0: "DV#", 1: "Other Aircraft", 3: ":airplane:"},
|
||||
'_': {0: "DW#", 1: "WX Site"},
|
||||
'`': {0: "DX", 1: "Rain", 3: ":umbrella:"},
|
||||
'a': {0: "SA#O"},
|
||||
'b': {0: "SB"},
|
||||
'c': {0: "SC#O", 1: "CD Triangle"},
|
||||
'd': {0: "SD", 1: "DX Spot"},
|
||||
'e': {0: "SE", 1: "Sleet"},
|
||||
'f': {0: "SF", 1: "Funnel Cloud"},
|
||||
'g': {0: "SG", 1: "Gale Flags"},
|
||||
'h': {0: "SHO", 1: "Store or Hamfest"},
|
||||
'i': {0: "SI#", 1: "Box / POI"},
|
||||
'j': {0: "SJ", 1: "Work Zone"},
|
||||
'k': {0: "SKO", 1: "Special Vehicle"},
|
||||
'l': {0: "SL", 1: "Areas"},
|
||||
'm': {0: "SM", 1: "Value Sign"},
|
||||
'n': {0: "SN#", 1: "Triangle"},
|
||||
'o': {0: "SO", 1: "Small Circle"},
|
||||
'p': {0: "SP"},
|
||||
'q': {0: "SQ"},
|
||||
'r': {0: "SR", 1: "Restrooms"},
|
||||
's': {0: "SS#", 1: "Ship/Boats", 3: ":speedboat:"},
|
||||
't': {0: "ST", 1: "Tornado", 3: ":cyclone:"},
|
||||
'u': {0: "SU#", 1: "Truck", 3: ":truck:"},
|
||||
'v': {0: "SV#", 1: "Van", 3: ":minibus:"},
|
||||
'w': {0: "SWO", 1: "Flooding"},
|
||||
'x': {0: "SX", 1: "Wreck/Obstruction"},
|
||||
'y': {0: "SY", 1: "Skywarn"},
|
||||
'z': {0: "SZ#", 1: "Shelter"},
|
||||
'{': {0: "Q1"},
|
||||
'|': {0: "Q2", 1: "TNC Stream Switch"},
|
||||
'}': {0: "Q3"},
|
||||
'~': {0: "Q4", 1: "TNC Stream Switch"},
|
||||
}
|
||||
)
|
||||
33
protocol/aprs/time.go
Normal file
33
protocol/aprs/time.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TimeFormatError struct {
|
||||
Time string
|
||||
}
|
||||
|
||||
func (err TimeFormatError) Error() string {
|
||||
return fmt.Sprintf("aprs: unknown time stamp %q", err.Time)
|
||||
}
|
||||
|
||||
func ParseTime(s string) (time.Time, error) {
|
||||
if len(s) < 7 {
|
||||
return time.Time{}, TimeFormatError{s}
|
||||
}
|
||||
|
||||
switch {
|
||||
case s[6] == 'z': // Day/Hours/Minutes (DHM) format
|
||||
return time.Parse("021504", s[:6])
|
||||
case s[6] == '/': // Day/Hours/Minutes (DHM) format
|
||||
return time.Parse("021504", s[:6])
|
||||
case s[6] == 'h': // Hours/Minutes/Seconds (HMS) format
|
||||
return time.Parse("150405", s[:6])
|
||||
case len(s) >= 8: // Month/Day/Hours/Minutes (MDHM) format
|
||||
return time.Parse("01021504", s[:8])
|
||||
default:
|
||||
return time.Time{}, TimeFormatError{s}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user