wip: fix skylabify unknown id crash?

This commit is contained in:
saji 2023-06-27 20:39:57 -05:00
parent 0e8904ebec
commit 2dd6031bdd
4 changed files with 25 additions and 10 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/kschamplin/gotelem/skylab" "github.com/kschamplin/gotelem/skylab"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
) )
// this command can be used to decode candump logs and dump json output. // this command can be used to decode candump logs and dump json output.
@ -112,7 +113,11 @@ func run(ctx *cli.Context) (err error) {
// parse the data []byte to a skylab packet // parse the data []byte to a skylab packet
cd.Data, err = skylab.FromCanFrame(uint32(cd.Id), rawData) cd.Data, err = skylab.FromCanFrame(uint32(cd.Id), rawData)
if err != nil { var idErr *skylab.UnknownIdError
if errors.As(err, &idErr) {
// unknown id
slog.Info("unknown id", "err", err)
} else if err != nil {
return err return err
} }

View file

@ -3,6 +3,7 @@ package skylab
import ( import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt"
"math" "math"
// this is needed so that we can run make_skylab.go // this is needed so that we can run make_skylab.go
@ -162,3 +163,12 @@ func (e *BusEvent) UnmarshalMsg(b []byte) ([]byte, error) {
// we need to be able to parse the JSON as well. this is done using the // we need to be able to parse the JSON as well. this is done using the
// generator since we can use the switch/case thing since it's the fastest // generator since we can use the switch/case thing since it's the fastest
type UnknownIdError struct {
id uint64
}
func (e *UnknownIdError) Error() string {
return fmt.Sprintf("unknown id: %x", e.id)
}

File diff suppressed because one or more lines are too long

View file

@ -48,7 +48,7 @@ type {{$structName}} struct {
func (p *{{$structName}}) CANId() (uint32, error) { func (p *{{$structName}}) CANId() (uint32, error) {
{{- if .Repeat }} {{- if .Repeat }}
if p.Idx >= {{.Repeat}} { if p.Idx >= {{.Repeat}} {
return 0, errors.New("invalid packet index") return 0, &UnknownIdError{ {{ printf "0x%X" .Id }} }
} }
return {{ printf "0x%X" .Id }} + p.Idx, nil return {{ printf "0x%X" .Id }} + p.Idx, nil
{{- else }} {{- else }}
@ -114,7 +114,7 @@ var idMap = map[uint32]bool{
// If the CAN ID is unknown, it will return an error. // If the CAN ID is unknown, it will return an error.
func FromCanFrame(id uint32, data []byte) (Packet, error) { func FromCanFrame(id uint32, data []byte) (Packet, error) {
if !idMap[id] { if !idMap[id] {
return nil, errors.New("unknown id") return nil, &UnknownIdError{ id }
} }
switch id { switch id {
{{- range $p := .Packets }} {{- range $p := .Packets }}
@ -167,4 +167,4 @@ func FromJson (id uint32, raw []byte) (Packet, error) {
// The json representation that was used to generate this data. // The json representation that was used to generate this data.
// can be used to share the parsing data for i.e dynamic python gui. // can be used to share the parsing data for i.e dynamic python gui.
const SkylabDefinitions = `{{json . | printf "%s" }}` const SkylabDefinitions = `{{json . | printf "%s" }}`