Skip to content

Commit

Permalink
Merge pull request #1 from xerra-eo/master
Browse files Browse the repository at this point in the history
Add support for NMEA 4.10 TAG Blocks in aisnmea convenience library
  • Loading branch information
BertoldVdb committed Oct 30, 2020
2 parents 8b86faa + 2eeec8e commit 7f7997d
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 12 deletions.
9 changes: 9 additions & 0 deletions aisnmea/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type VdmPacket struct {
MessageType string
Payload []byte
Packet ais.Packet
TagBlock nmea.TagBlock
}

type vdmAssemblyWork struct {
Expand Down Expand Up @@ -74,6 +75,7 @@ func (v *vdmAssembler) process(vdm *nmea.VDMVDO) (VdmPacket, bool) {
TalkerID: vdm.BaseSentence.TalkerID(),
MessageType: vdm.BaseSentence.DataType(),
Payload: vdm.Payload,
TagBlock: vdm.TagBlock,
}, true
}

Expand Down Expand Up @@ -113,6 +115,12 @@ func (v *vdmAssembler) process(vdm *nmea.VDMVDO) (VdmPacket, bool) {
}
}

/* Merge multiple TAG Blocks into a single one */
var composedTagBlock nmea.TagBlock
for _, vdm := range workMsg.vdms {
mergeTagBlocks(&composedTagBlock, &vdm.TagBlock)
}

delete(v.msgMap, key)

/* Full payload is assembled */
Expand All @@ -121,6 +129,7 @@ func (v *vdmAssembler) process(vdm *nmea.VDMVDO) (VdmPacket, bool) {
TalkerID: vdm.BaseSentence.TalkerID(),
MessageType: vdm.BaseSentence.DataType(),
Payload: fullPayload,
TagBlock: composedTagBlock,
}, true
}

Expand Down
37 changes: 25 additions & 12 deletions aisnmea/nmea.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ func valueToChar(value byte) byte {
return result
}

func addChecksum(sentence string) string {
checksum := byte(0)
for i := 1; i < len(sentence); i++ {
checksum ^= sentence[i]
}

return fmt.Sprintf("%s*%02X", sentence, checksum)
}

// EncodeSentence encodes the provided packet into zero or more NMEA sentences
func (nc *NMEACodec) EncodeSentence(p VdmPacket) []string {
if p.Payload == nil && p.Packet != nil {
Expand Down Expand Up @@ -142,6 +151,12 @@ func (nc *NMEACodec) EncodeSentence(p VdmPacket) []string {
if nc.MaxLineLength <= 0 || len(asciiPayload) <= maxDataLength {
output = make([]string, 1)
output[0] = fmt.Sprintf("!%s%s,1,1,,%c,%s,%d", p.TalkerID, p.MessageType, channel, asciiPayload, fillBits)
output[0] = addChecksum(output[0])

tagBlock := encodeTagBlock(&p.TagBlock, 1,1,0, false)
if tagBlock != "" {
output[0] = fmt.Sprintf("%s%s", tagBlock, output[0])
}
} else {
dataIndex := 0
msgIndex := 1
Expand All @@ -163,8 +178,16 @@ func (nc *NMEACodec) EncodeSentence(p VdmPacket) []string {
suffix = fillBits
}

output = append(output,
fmt.Sprintf("!%s%s,%d,%d,%d,%c,%s,%d", p.TalkerID, p.MessageType, msgNum, msgIndex, nc.seqNo, channel, sub, suffix))
sentence := fmt.Sprintf("!%s%s,%d,%d,%d,%c,%s,%d", p.TalkerID, p.MessageType, msgNum, msgIndex, nc.seqNo, channel, sub, suffix)

sentence = addChecksum(sentence)

tagBlock := encodeTagBlock(&p.TagBlock, msgIndex, msgNum, nc.seqNo, true)
if tagBlock != "" {
sentence = fmt.Sprintf("%s%s", tagBlock, sentence)
}

output = append(output, sentence)

msgIndex++
}
Expand All @@ -175,15 +198,5 @@ func (nc *NMEACodec) EncodeSentence(p VdmPacket) []string {
}
}

/* Add checksums */
for i := range output {
checksum := byte(0)
for j := 1; j < len(output[i]); j++ {
checksum ^= output[i][j]
}

output[i] += fmt.Sprintf("*%02X", checksum)
}

return output
}
58 changes: 58 additions & 0 deletions aisnmea/nmea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,61 @@ func TestNMEAReencode(t *testing.T) {
}

}

func TestNMEATagBlockDecodeSingleSentence(t *testing.T) {
nm := NMEACodecNew(ais.CodecNew(false, false))
msg, err := nm.ParseSentence("\\s:2156,c:1560234814*36\\!AIVDM,1,1,,B,23aDqDOP0S0:mk2Kv3Ip=wvpR>`<,0*3D")

if err != nil {
t.Error("Error returned for valid message", err)
}

if msg == nil {
t.Error("No error, but no message for single-sentence message")
}

if msg.TagBlock.Source != "2156" {
t.Error("TAG block Source not parsed")
}

if msg.TagBlock.Time != 1560234814 {
t.Error("TAG block Time not parsed")
}
}

func TestNMEATagBlockDecodeMultiSentence(t *testing.T) {
nm := NMEACodecNew(ais.CodecNew(false, false))
msg, err := nm.ParseSentence(
"\\g:1-2-2449555,s:2251,c:1560234814*7E\\!AIVDM,2,1,7,A,"+
"8h3OwjQKP@5UUEPPP121IoCol54cd0Wws7wwjp:@`P1UUFD9e2B94oCPH54M`3kw,0*7A")

if err != nil {
t.Error("Error returned for valid message", err)
}

if msg != nil {
t.Error("Premature return of message")
}

msg, err = nm.ParseSentence("\\g:2-2-2449555*63\\!AIVDM,2,2,7,A,sUwwjt;HvP1,2*4F")

if err != nil {
t.Error("Error returned for valid message", err)
}

if msg == nil {
t.Error("No error, but no message for multi-sentence message")
}

if msg.TagBlock.Source != "2251" {
t.Error("TAG block Source not parsed")
}

if msg.TagBlock.Time != 1560234814 {
t.Error("TAG block Time not parsed")
}

if msg.TagBlock.Grouping != "" {
t.Error("TAG block Grouping parsed (should be ignored)")
}
}
110 changes: 110 additions & 0 deletions aisnmea/tagblocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package aisnmea

import (
"fmt"
"strings"

nmea "github.com/adrianmo/go-nmea"
)

// BlankTagBlock is a pseudo-constant for convenient comparisons to a blank value
var BlankTagBlock nmea.TagBlock

// addTagBlockChecksum performs the NMEA checksum from byte 0 (unlike addChecksum from nmea.go, which starts at 1)
func addTagBlockChecksum(sentence string) string {
checksum := byte(0)
for i := 0; i < len(sentence); i++ {
checksum ^= sentence[i]
}

return fmt.Sprintf("%s*%02X", sentence, checksum)
}

/* mergeTagBlocks tries to summarise NMEA 4.10 TAG Blocks relating to a multi-sentence
VDMVDO message (src) into one (dst) upon decoding. It is neccessarily lossy.
Here are the assumptions:
- Certain tags should be never have more than one value (time, source, etc), so we pick
the first value, and ignore subsequent ones. If for some weird reason the sentences are
timestamped instead of the message, we'd want to use the earliest timestamp.
- The grouping tag should be different for every sentence, so there is no sense in including it.
- Most examples in the wild seem to send the same tag block for every sentence of a message,
in those cases this function does no useful work, but no harm either.
- If each sentence contributes a different subset of tags, we'll get a complete set at the end.
*/
func mergeTagBlocks(dst *nmea.TagBlock, src *nmea.TagBlock) {
if dst == nil || src == nil {
return
}

if src.Time != 0 && dst.Time == 0 {
dst.Time = src.Time
}

if src.Text != "" && dst.Text == "" {
dst.Text = src.Text
}

if src.Destination != "" && dst.Destination == "" {
dst.Destination = src.Destination
}

if src.Source != "" && dst.Source == "" {
dst.Source = src.Source
}

if src.RelativeTime != 0 && dst.RelativeTime == 0 {
dst.RelativeTime = src.RelativeTime
}

if src.LineCount != 0 && dst.LineCount == 0 {
dst.LineCount = src.LineCount
}
}

// encodeTagBlock encodes the fields of tagBlock into a NMEA 4.10 TAG Block string
func encodeTagBlock(tagBlock *nmea.TagBlock, msgIndex, msgNum, seqNo int, addLineCount bool) string {
if tagBlock == nil || *tagBlock == BlankTagBlock {
return ""
}

tags := []string{}

if msgNum > 1 {
// We add the group tag for all sentences in a multi-sentence message
tags = append(tags, fmt.Sprintf("g:%d-%d-%d", msgIndex, msgNum, seqNo))

// We don't duplicate the rest of the tags for subsequent sentences of multi-sentence message
if msgIndex > 1 {
return fmt.Sprintf("\\%s\\", addTagBlockChecksum(tags[0]))
}

// If we got this far, it is a multi-sentence message AND this is the first sentence
if addLineCount {
tags = append(tags, fmt.Sprintf("n:%d", msgNum))
}
}

if tagBlock.Source != "" {
tags = append(tags, fmt.Sprintf("s:%s", tagBlock.Source))
}

if tagBlock.Time != 0 {
tags = append(tags, fmt.Sprintf("c:%d", tagBlock.Time))
}

if tagBlock.RelativeTime != 0 {
tags = append(tags, fmt.Sprintf("r:%d", tagBlock.RelativeTime))
}

if tagBlock.Destination != "" {
tags = append(tags, fmt.Sprintf("d:%s", tagBlock.Destination))
}

if tagBlock.Text != "" {
tags = append(tags, fmt.Sprintf("t:%s", tagBlock.Text))
}

return fmt.Sprintf("\\%s\\", addTagBlockChecksum(strings.Join(tags, ",")))
}
Loading

0 comments on commit 7f7997d

Please sign in to comment.