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)
}

View file

@ -1,4 +1,4 @@
// generated by gen_skylab.go at 2023-05-30 21:59:25.165613242 -0500 CDT m=+0.002722998 DO NOT EDIT! // generated by gen_skylab.go at 2023-06-27 20:32:07.43630691 -0500 CDT m=+0.003975675 DO NOT EDIT!
package skylab package skylab
@ -236,7 +236,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 {
case 0x10: case 0x10:
@ -1559,7 +1559,7 @@ type BmsModule struct {
func (p *BmsModule) CANId() (uint32, error) { func (p *BmsModule) CANId() (uint32, error) {
if p.Idx >= 36 { if p.Idx >= 36 {
return 0, errors.New("invalid packet index") return 0, &UnknownIdError{ 0x1C }
} }
return 0x1C + p.Idx, nil return 0x1C + p.Idx, nil
} }
@ -2546,7 +2546,7 @@ type TrackerEnable struct {
func (p *TrackerEnable) CANId() (uint32, error) { func (p *TrackerEnable) CANId() (uint32, error) {
if p.Idx >= 6 { if p.Idx >= 6 {
return 0, errors.New("invalid packet index") return 0, &UnknownIdError{ 0x610 }
} }
return 0x610 + p.Idx, nil return 0x610 + p.Idx, nil
} }
@ -3636,7 +3636,7 @@ type TrackerData struct {
func (p *TrackerData) CANId() (uint32, error) { func (p *TrackerData) CANId() (uint32, error) {
if p.Idx >= 6 { if p.Idx >= 6 {
return 0, errors.New("invalid packet index") return 0, &UnknownIdError{ 0x600 }
} }
return 0x600 + p.Idx, nil return 0x600 + p.Idx, nil
} }

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 }}