mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-18 21:53:56 +00:00
gps: revamp validSentence() to avoid heap allocation for errors. This error can occur too frequently to allow for such allocations
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+2
-25
@@ -20,30 +20,9 @@ var (
|
|||||||
errInvalidGLLSentence = errors.New("invalid GLL NMEA sentence")
|
errInvalidGLLSentence = errors.New("invalid GLL NMEA sentence")
|
||||||
errGPSCommandRejected = errors.New("GPS command rejected (NAK)")
|
errGPSCommandRejected = errors.New("GPS command rejected (NAK)")
|
||||||
errNoACKToGPSCommand = errors.New("no ACK to GPS command")
|
errNoACKToGPSCommand = errors.New("no ACK to GPS command")
|
||||||
|
errInvalidNMEASentanceFormat = errors.New("invalid NMEA sentence format")
|
||||||
)
|
)
|
||||||
|
|
||||||
type GPSError struct {
|
|
||||||
Err error
|
|
||||||
Info string
|
|
||||||
Sentence string
|
|
||||||
}
|
|
||||||
|
|
||||||
func newGPSError(err error, sentence string, info string) GPSError {
|
|
||||||
return GPSError{
|
|
||||||
Info: info,
|
|
||||||
Err: err,
|
|
||||||
Sentence: sentence,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ge GPSError) Error() string {
|
|
||||||
return ge.Err.Error() + " " + ge.Info + " " + ge.Sentence
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ge GPSError) Unwrap() error {
|
|
||||||
return ge.Err
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
minimumNMEALength = 7
|
minimumNMEALength = 7
|
||||||
startingDelimiter = '$'
|
startingDelimiter = '$'
|
||||||
@@ -182,9 +161,7 @@ func validSentence(sentence string) error {
|
|||||||
}
|
}
|
||||||
checksum := strings.ToUpper(hex.EncodeToString([]byte{cs}))
|
checksum := strings.ToUpper(hex.EncodeToString([]byte{cs}))
|
||||||
if checksum != sentence[len(sentence)-2:len(sentence)] {
|
if checksum != sentence[len(sentence)-2:len(sentence)] {
|
||||||
return newGPSError(errInvalidNMEAChecksum, sentence,
|
return errInvalidNMEASentanceFormat
|
||||||
"expected "+sentence[len(sentence)-2:len(sentence)]+
|
|
||||||
" got "+checksum)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+1
-1
@@ -104,7 +104,7 @@ func (parser *Parser) Parse(sentence string) (Fix, error) {
|
|||||||
return fix, nil
|
return fix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fix, newGPSError(errUnknownNMEASentence, sentence, typ)
|
return fix, errInvalidNMEASentanceFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
// findTime returns the time from an NMEA sentence:
|
// findTime returns the time from an NMEA sentence:
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestParseUnknownSentence(t *testing.T) {
|
func TestParseUnknownSentence(t *testing.T) {
|
||||||
c := qt.New(t)
|
|
||||||
|
|
||||||
p := NewParser()
|
p := NewParser()
|
||||||
|
|
||||||
val := "$GPGSV,3,1,09,07,14,317,22,08,31,284,25,10,32,133,39,16,85,232,29*7F"
|
val := "$GPGSV,3,1,09,07,14,317,22,08,31,284,25,10,32,133,39,16,85,232,29*7F"
|
||||||
_, err := p.Parse(val)
|
_, err := p.Parse(val)
|
||||||
c.Assert(err.Error(), qt.Contains, "unsupported NMEA sentence type")
|
if err == nil {
|
||||||
|
t.Error("should have unknown sentence err")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseGGA(t *testing.T) {
|
func TestParseGGA(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user