forked from adrianmo/go-nmea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentence.go
More file actions
321 lines (293 loc) · 7.03 KB
/
sentence.go
File metadata and controls
321 lines (293 loc) · 7.03 KB
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
package nmea
import (
"fmt"
"strings"
"sync"
)
const (
// SentenceStart is the token to indicate the start of a sentence.
SentenceStart = "$"
// SentenceStartEncapsulated is the token to indicate the start of encapsulated data.
SentenceStartEncapsulated = "!"
// FieldSep is the token to delimit fields of a sentence.
FieldSep = ","
// ChecksumSep is the token to delimit the checksum of a sentence.
ChecksumSep = "*"
)
var (
customParsersMu = &sync.Mutex{}
customParsers = map[string]ParserFunc{}
)
// ParserFunc callback used to parse specific sentence variants
type ParserFunc func(BaseSentence) (Sentence, error)
// NotSupportedError is returned when parsed sentence is not supported
type NotSupportedError struct {
Prefix string
}
// Error returns error message
func (p *NotSupportedError) Error() string {
return fmt.Sprintf("nmea: sentence prefix '%s' not supported", p.Prefix)
}
// Sentence interface for all NMEA sentence
type Sentence interface {
fmt.Stringer
Prefix() string
DataType() string
TalkerID() string
}
// BaseSentence contains the information about the NMEA sentence
type BaseSentence struct {
Talker string // The talker id (e.g GP)
Type string // The data type (e.g GSA)
Fields []string // Array of fields
Checksum string // The Checksum
Raw string // The raw NMEA sentence received
TagBlock TagBlock // NMEA tagblock
}
// Prefix returns the talker and type of message
func (s BaseSentence) Prefix() string {
return s.Talker + s.Type
}
// DataType returns the type of the message
func (s BaseSentence) DataType() string {
return s.Type
}
// TalkerID returns the talker of the message
func (s BaseSentence) TalkerID() string {
return s.Talker
}
// String formats the sentence into a string
func (s BaseSentence) String() string { return s.Raw }
// parseSentence parses a raw message into it's fields
func parseSentence(raw string) (BaseSentence, error) {
raw = strings.TrimSpace(raw)
tagBlockParts := strings.SplitN(raw, `\`, 3)
var (
tagBlock TagBlock
err error
)
if len(tagBlockParts) == 3 {
tags := tagBlockParts[1]
raw = tagBlockParts[2]
tagBlock, err = parseTagBlock(tags)
if err != nil {
return BaseSentence{}, err
}
}
startIndex := strings.IndexAny(raw, SentenceStart+SentenceStartEncapsulated)
if startIndex != 0 {
return BaseSentence{}, fmt.Errorf("nmea: sentence does not start with a '$' or '!'")
}
sumSepIndex := strings.Index(raw, ChecksumSep)
if sumSepIndex == -1 {
return BaseSentence{}, fmt.Errorf("nmea: sentence does not contain checksum separator")
}
var (
fieldsRaw = raw[startIndex+1 : sumSepIndex]
fields = strings.Split(fieldsRaw, FieldSep)
checksumRaw = strings.ToUpper(raw[sumSepIndex+1:])
checksum = Checksum(fieldsRaw)
)
// Validate the checksum
if checksum != checksumRaw {
return BaseSentence{}, fmt.Errorf(
"nmea: sentence checksum mismatch [%s != %s]", checksum, checksumRaw)
}
talker, typ := parsePrefix(fields[0])
return BaseSentence{
Talker: talker,
Type: typ,
Fields: fields[1:],
Checksum: checksumRaw,
Raw: raw,
TagBlock: tagBlock,
}, nil
}
// parsePrefix takes the first field and splits it into a talker id and data type.
func parsePrefix(s string) (string, string) {
if strings.HasPrefix(s, "PMTK") {
return "PMTK", s[4:]
}
if strings.HasPrefix(s, "P") {
return "P", s[1:]
}
if len(s) < 2 {
return s, ""
}
return s[:2], s[2:]
}
// Checksum xor all the bytes in a string an return it
// as an uppercase hex string
func Checksum(s string) string {
var checksum uint8
for i := 0; i < len(s); i++ {
checksum ^= s[i]
}
return fmt.Sprintf("%02X", checksum)
}
// MustRegisterParser register a custom parser or panic
func MustRegisterParser(sentenceType string, parser ParserFunc) {
if err := RegisterParser(sentenceType, parser); err != nil {
panic(err)
}
}
// RegisterParser register a custom parser
func RegisterParser(sentenceType string, parser ParserFunc) error {
customParsersMu.Lock()
defer customParsersMu.Unlock()
if _, ok := customParsers[sentenceType]; ok {
return fmt.Errorf("nmea: parser for sentence type '%q' already exists", sentenceType)
}
customParsers[sentenceType] = parser
return nil
}
// Parse parses the given string into the correct sentence type.
func Parse(raw string) (Sentence, error) {
s, err := parseSentence(raw)
if err != nil {
return nil, err
}
// Custom parser allow overriding of existing parsers
if parser, ok := customParsers[s.Type]; ok {
return parser(s)
}
if strings.HasPrefix(s.Raw, SentenceStart) {
// MTK message types share the same format
// so we return the same struct for all types.
switch s.Talker {
case TypeMTK:
return newMTK(s)
}
switch s.Type {
case TypeRMC:
return newRMC(s)
case TypeAAM:
return newAAM(s)
case TypeALA:
return newALA(s)
case TypeAPB:
return newAPB(s)
case TypeBEC:
return newBEC(s)
case TypeBOD:
return newBOD(s)
case TypeBWC:
return newBWC(s)
case TypeBWR:
return newBWR(s)
case TypeBWW:
return newBWW(s)
case TypeDOR:
return newDOR(s)
case TypeDSC:
return newDSC(s)
case TypeDSE:
return newDSE(s)
case TypeDTM:
return newDTM(s)
case TypeEVE:
return newEVE(s)
case TypeFIR:
return newFIR(s)
case TypeGGA:
return newGGA(s)
case TypeGSA:
return newGSA(s)
case TypeGLL:
return newGLL(s)
case TypeVTG:
return newVTG(s)
case TypeZDA:
return newZDA(s)
case TypePGRME:
return newPGRME(s)
case TypePHTRO:
return newPHTRO(s)
case TypePRDID:
return newPRDID(s)
case TypePSKPDPT:
return newPSKPDPT(s)
case TypePSONCMS:
return newPSONCMS(s)
case TypeGSV:
return newGSV(s)
case TypeHDG:
return newHDG(s)
case TypeHDT:
return newHDT(s)
case TypeHDM:
return newHDM(s)
case TypeHSC:
return newHSC(s)
case TypeGNS:
return newGNS(s)
case TypeTHS:
return newTHS(s)
case TypeTLL:
return newTLL(s)
case TypeTTM:
return newTTM(s)
case TypeTXT:
return newTXT(s)
case TypeWPL:
return newWPL(s)
case TypeRMB:
return newRMB(s)
case TypeRPM:
return newRPM(s)
case TypeRSA:
return newRSA(s)
case TypeRSD:
return newRSD(s)
case TypeRTE:
return newRTE(s)
case TypeROT:
return newROT(s)
case TypeVBW:
return newVBW(s)
case TypeVDR:
return newVDR(s)
case TypeVHW:
return newVHW(s)
case TypeVPW:
return newVPW(s)
case TypeVLW:
return newVLW(s)
case TypeVWR:
return newVWR(s)
case TypeVWT:
return newVWT(s)
case TypeDPT:
return newDPT(s)
case TypeDBT:
return newDBT(s)
case TypeDBK:
return newDBK(s)
case TypeDBS:
return newDBS(s)
case TypeMDA:
return newMDA(s)
case TypeMTA:
return newMTA(s)
case TypeMTW:
return newMTW(s)
case TypeMWD:
return newMWD(s)
case TypeMWV:
return newMWV(s)
case TypeOSD:
return newOSD(s)
case TypeXDR:
return newXDR(s)
case TypeXTE:
return newXTE(s)
}
}
if strings.HasPrefix(s.Raw, SentenceStartEncapsulated) {
switch s.Type {
case TypeVDM, TypeVDO:
return newVDMVDO(s)
}
}
return nil, &NotSupportedError{Prefix: s.Prefix()}
}