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/urfave/cli/v2"
"golang.org/x/exp/slog"
)
// 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
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
}

View file

@ -3,6 +3,7 @@ package skylab
import (
"encoding/binary"
"encoding/json"
"fmt"
"math"
// 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
// 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) {
{{- if .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
{{- else }}
@ -114,7 +114,7 @@ var idMap = map[uint32]bool{
// If the CAN ID is unknown, it will return an error.
func FromCanFrame(id uint32, data []byte) (Packet, error) {
if !idMap[id] {
return nil, errors.New("unknown id")
return nil, &UnknownIdError{ id }
}
switch id {
{{- 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.
// can be used to share the parsing data for i.e dynamic python gui.
const SkylabDefinitions = `{{json . | printf "%s" }}`
const SkylabDefinitions = `{{json . | printf "%s" }}`