8000 dbc: distinguish inactive signals during payload decoding by tomrford · Pull Request #92 · tomrford/gocan · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions dbc/codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbc

import (
"errors"
"fmt"
"math"
"reflect"
@@ -10,6 +11,10 @@ import (
"github.com/tomrford/gocan/j1939"
)

// ErrInactiveSignal means the selected multiplexing path excludes the signal
// from this frame. It is an expected absence, not a malformed-payload error.
var ErrInactiveSignal = errors.New("inactive DBC signal")

type messageCodec struct {
signalsByName map[string]int
constraints []map[int][]MultiplexRange
Expand Down Expand Up @@ -199,6 +204,7 @@ func (message *Message) Patch(frame *gocan.Frame, changes Values) error {
// supported by the codec.
// J1939 definitions match by canonical PGN, regardless of priority, source or
// destination. Select the appropriate definition first when a PGN is ambiguous.
// An inactive signal returns an error wrapping ErrInactiveSignal.
func (message *Message) Decode(frame gocan.Frame, name string) (any, error) {
if err := message.validateFrame(frame); err != nil {
return nil, err
Expand All @@ -207,9 +213,10 @@ func (message *Message) Decode(frame gocan.Frame, name string) (any, error) {
}

// DecodePayload returns the physical value of one named signal from a payload.
// The payload may be shorter than the declared message length when the signal
// and every multiplexor needed to select it are present. It must not be longer
// than the declared message length.
// The payload may be shorter than the declared message length. Only multiplexors
// needed to determine activity and the active signal's bits must be present.
// An inactive signal returns an error wrapping ErrInactiveSignal, even if its
// bits are absent. The payload must not exceed the declared message length.
func (message *Message) DecodePayload(payload []byte, name string) (any, error) {
codec, err := message.usableCodec()
if err != nil {
Expand All @@ -222,19 +229,15 @@ func (message *Message) DecodePayload(payload []byte, name string) (any, error)
if !ok {
return nil, fmt.Errorf("DBC message %q has no signal %q", message.Name, name)
}
if !signalAvailable(message.Signals[index], len(payload)) {
return nil, fmt.Errorf("DBC signal %q is not present in the %d-byte payload", name, len(payload))
}
selectors := make(map[int]uint64)
if err := codec.readSelectorPath(message, payload, index, selectors); err != nil {
return nil, err
}
active, err := codec.signalActive(message, index, selectors, make(map[int]bool))
active, err := codec.signalActiveInPayload(message, payload, index)
if err != nil {
return nil, err
}
if !active {
return nil, fmt.Errorf("DBC signal %q is inactive in this frame", name)
return nil, fmt.Errorf("%w %q in this frame", ErrInactiveSignal, name)
}
if !signalAvailable(message.Signals[index], len(payload)) {
return nil, fmt.Errorf("DBC signal %q is not present in the %d-byte payload", name, len(payload))
}
return decodeSignalValue(message.Signals[index], readSignalBits(payload, message.Signals[index])), nil
}
Expand Down Expand Up @@ -453,20 +456,20 @@ func (codec *messageCodec) selectorValues(message *Message, payload []byte) map[
return values
}

func (codec *messageCodec) readSelectorPath(message *Message, payload []byte, index int, values map[int]uint64) error {
func (codec *messageCodec) signalActiveInPayload(message *Message, payload []byte, index int) (bool, error) {
condition := message.Signals[index].Multiplex
if condition == nil {
return nil
return true, nil
}
selector := codec.signalsByName[condition.Selector]
if !signalAvailable(message.Signals[selector], len(payload)) {
return fmt.Errorf("DBC multiplexor %q is not present in the %d-byte payload", message.Signals[selector].Name, len(payload))
active, err := codec.signalActiveInPayload(message, payload, selector)
if err != nil || !active {
return false, err
}
if err := codec.readSelectorPath(message, payload, selector, values); err != nil {
return err
if !signalAvailable(message.Signals[selector], len(payload)) {
return false, fmt.Errorf("DBC multiplexor %q is not present in the %d-byte payload", message.Signals[selector].Name, len(payload))
}
values[selector] = readSignalBits(payload, message.Signals[selector])
return nil
return rangesContain(condition.Ranges, readSignalBits(payload, message.Signals[selector])), nil
}

func (message *Message) newFrame() (gocan.Frame, error) {
Expand Down
33 changes: 32 additions & 1 deletion dbc/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dbc
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math"
"strings"
Expand Down Expand Up @@ -223,9 +224,39 @@ func TestMultiplexedPatchAndJ1939(t *testing.T) {
if err := multiplexed.Patch(&inactive, Values{"RootA": uint64(1)}); err != nil {
t.Fatalf("Patch to inactive branch: %v", err)
}
if _, err := multiplexed.Decode(inactive, "Leaf"); err == nil || !strings.Contains(err.Error(), "inactive") {
if value, err := multiplexed.Decode(inactive, "Leaf"); value != nil || !errors.Is(err, ErrInactiveSignal) || !strings.Contains(err.Error(), "Leaf") {
t.Fatalf("inactive Decode error = %v", err)
}
for _, test := range []struct {
payload []byte
signal string
value any
error string
ina 8C78 ctive bool
}{
{[]byte{1}, "Leaf", nil, "Leaf", true},
{[]byte{1}, "ChildSelector", nil, "ChildSelector", true},
{inactive.Data[:multiplexed.Length], "Leaf", nil, "Leaf", true},
{[]byte{2, 2}, "Leaf", nil, "Leaf", true},
{[]byte{2, 3, 44}, "Leaf", uint64(44), "", false},
{nil, "Leaf", nil, "multiplexor \"RootA\" is not present", false},
{[]byte{2}, "Leaf", nil, "multiplexor \"ChildSelector\" is not present", false},
{[]byte{2, 3}, "Leaf", nil, "signal \"Leaf\" is not present", false},
{[]byte{2}, "ChildSelector", nil, "signal \"ChildSelector\" is not present", false},
{[]byte{1}, "Unknown", nil, "has no signal", false},
{make([]byte, 9), "Leaf", nil, "exceeds declared length", false},
} {
value, err := multiplexed.DecodePayload(test.payload, test.signal)
if value != test.value || errors.Is(err, ErrInactiveSignal) != test.inactive ||
(test.error == "" && err != nil) || (test.error != "" && (err == nil || !strings.Contains(err.Error(), test.error))) {
t.Fatalf("DecodePayload(% x, %q) = %v, %v; want %v, %q, inactive %t", test.payload, test.signal, value, err, test.value, test.error, test.inactive)
}
}
short := inactive
short.DLC = 1
if _, err := multiplexed.Decode(short, "Leaf"); err == nil || errors.Is(err, ErrInactiveSignal) || !strings.Contains(err.Error(), "payload length") {
t.Fatalf("short inactive frame Decode error = %v", err)
}
if err := multiplexed.Patch(&inactive, Values{"RootA": uint64(2)}); err == nil || !strings.Contains(err.Error(), "requires a value") {
t.Fatalf("incomplete path change error = %v", err)
}
Expand Down
Loading
0