diff --git a/adapters.go b/adapters.go index c692103..dfd95d6 100644 --- a/adapters.go +++ b/adapters.go @@ -77,7 +77,15 @@ func handleDBCFile(net *Network, file *dbc.File) error { case dbc.SignalUnsigned: signed = false } - tmpSigType, err := NewCustomSignalType(fmt.Sprintf("%s_type", dbcSig.Name), int(dbcSig.Size), signed, dbcSig.Min, dbcSig.Max) + order := SignalTypeOrderLittleEndian + switch dbcSig.ByteOrder { + case dbc.SignalLittleEndian: + order = SignalTypeOrderLittleEndian + case dbc.SignalBigEndian: + order = SignalTypeOrderBigEndian + } + + tmpSigType, err := NewCustomSignalType(fmt.Sprintf("%s_type", dbcSig.Name), int(dbcSig.Size), signed, order, dbcSig.Min, dbcSig.Max) if err != nil { return err } diff --git a/message_test.go b/message_test.go index d81e15c..efdbf84 100644 --- a/message_test.go +++ b/message_test.go @@ -19,8 +19,8 @@ func Test_Message_AppendSignal(t *testing.T) { msg := NewMessage("msg_0", 8) - sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127) - sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, -128, 127) + sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127) + sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, SignalTypeOrderLittleEndian, -128, 127) sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"} @@ -41,7 +41,7 @@ func Test_Message_AppendSignal(t *testing.T) { assert.NoError(msg.AppendSignal(sig4)) - sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, -128, 127) + sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, SignalTypeOrderLittleEndian, -128, 127) massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive) assert.Error(msg.AppendSignal(massiveSig)) @@ -60,8 +60,8 @@ func Test_Message_InsertSignal(t *testing.T) { msg := NewMessage("msg_0", 8) - sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127) - sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, -128, 127) + sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127) + sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, SignalTypeOrderLittleEndian, -128, 127) sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"} @@ -87,7 +87,7 @@ func Test_Message_InsertSignal(t *testing.T) { assert.NoError(msg.InsertSignal(sig3, 8)) assert.NoError(msg.InsertSignal(sig4, 32)) - sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, -128, 127) + sigTypMassive, _ := newSignalType("massive", SignalTypeKindInteger, 128, true, SignalTypeOrderLittleEndian, -128, 127) massiveSig, _ := NewStandardSignal("massive_sig", sigTypMassive) assert.Error(msg.InsertSignal(massiveSig, 0)) @@ -110,8 +110,8 @@ func Test_Message_RemoveSignal(t *testing.T) { msg := NewMessage("msg_0", 8) - sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127) - sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, -128, 127) + sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127) + sigTypInt32, _ := newSignalType("int32", SignalTypeKindInteger, 32, true, SignalTypeOrderLittleEndian, -128, 127) sigNames := []string{"sig_0", "sig_1", "sig_2", "sig_3", "sig_4"} @@ -145,7 +145,7 @@ func Test_Message_CompactSignals(t *testing.T) { msg := NewMessage("msg_0", 8) - sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, -128, 127) + sigTypInt8, _ := newSignalType("int8", SignalTypeKindInteger, 8, true, SignalTypeOrderLittleEndian, -128, 127) sig0, _ := NewStandardSignal("sig_0", sigTypInt8) sig1, _ := NewStandardSignal("sig_1", sigTypInt8) @@ -170,7 +170,7 @@ func Test_Message_ShiftSignalLeft(t *testing.T) { msg := NewMessage("message", 2) - sigTypeInt4, err := newSignalType("int4", SignalTypeKindInteger, 4, true, -8, 7) + sigTypeInt4, err := newSignalType("int4", SignalTypeKindInteger, 4, true, SignalTypeOrderLittleEndian, -8, 7) assert.NoError(err) sig0, err := NewStandardSignal("signal_0", sigTypeInt4) @@ -206,7 +206,7 @@ func Test_Message_ShiftSignalRight(t *testing.T) { msg := NewMessage("message", 2) - sigTypeInt4, _ := newSignalType("int4", SignalTypeKindInteger, 4, true, -8, 7) + sigTypeInt4, _ := newSignalType("int4", SignalTypeKindInteger, 4, true, SignalTypeOrderLittleEndian, -8, 7) sig0, err := NewStandardSignal("signal_0", sigTypeInt4) assert.NoError(err) diff --git a/signal_type.go b/signal_type.go index 8d88f1f..ed96e28 100644 --- a/signal_type.go +++ b/signal_type.go @@ -19,6 +19,16 @@ const ( SignalTypeKindFloat SignalTypeKind = "signal_type-float" ) +// SignalTypeOrder represents the bit order of a [SignalType]. +type SignalTypeOrder string + +const ( + // SignalTypeOrderLittleEndian defines a little endian signal type. + SignalTypeOrderLittleEndian SignalTypeOrder = "signal_type_order-little_endian" + // SignalTypeOrderBigEndian defines a big endian signal type. + SignalTypeOrderBigEndian SignalTypeOrder = "signal_type_order-big_endian" +) + // SignalType is the representation of a signal type. type SignalType struct { *entity @@ -26,11 +36,12 @@ type SignalType struct { kind SignalTypeKind size int signed bool + order SignalTypeOrder min float64 max float64 } -func newSignalType(name string, kind SignalTypeKind, size int, signed bool, min, max float64) (*SignalType, error) { +func newSignalType(name string, kind SignalTypeKind, size int, signed bool, oredr SignalTypeOrder, min, max float64) (*SignalType, error) { if size <= 0 { return nil, fmt.Errorf("signal type size cannot be negative") } @@ -41,22 +52,23 @@ func newSignalType(name string, kind SignalTypeKind, size int, signed bool, min, kind: kind, size: size, signed: signed, + order: oredr, min: min, max: max, }, nil } // NewCustomSignalType creates a new [SignalType] of kind [SignalTypeKindCustom] -// with the given name, size, signed, and min/max values. +// with the given name, size, signed, order, and min/max values. // It may return an error if the size is negative. -func NewCustomSignalType(name string, size int, signed bool, min, max float64) (*SignalType, error) { - return newSignalType(name, SignalTypeKindCustom, size, signed, min, max) +func NewCustomSignalType(name string, size int, signed bool, order SignalTypeOrder, min, max float64) (*SignalType, error) { + return newSignalType(name, SignalTypeKindCustom, size, signed, order, min, max) } // NewFlagSignalType creates a new [SignalType] of kind [SignalTypeKindFlag] -// with the given name. +// with the given name. The order is set to little endian. func NewFlagSignalType(name string) *SignalType { - sig, err := newSignalType(name, SignalTypeKindFlag, 1, false, 0, 1) + sig, err := newSignalType(name, SignalTypeKindFlag, 1, false, SignalTypeOrderLittleEndian, 0, 1) if err != nil { panic(err) } @@ -64,7 +76,7 @@ func NewFlagSignalType(name string) *SignalType { } // NewIntegerSignalType creates a new [SignalType] of kind [SignalTypeKindInteger] -// with the given name, size, and signed. +// with the given name, size, and signed. The order is set to little endian. // It may return an error if the size is negative. func NewIntegerSignalType(name string, size int, signed bool) (*SignalType, error) { var min float64 @@ -81,16 +93,16 @@ func NewIntegerSignalType(name string, size int, signed bool) (*SignalType, erro max = float64(tmp) } - return newSignalType(name, SignalTypeKindInteger, size, signed, min, max) + return newSignalType(name, SignalTypeKindInteger, size, signed, SignalTypeOrderLittleEndian, min, max) } // NewFloatSignalType creates a new [SignalType] of kind [SignalTypeKindFloat] -// with the given name and size. +// with the given name and size. The order is set to little endian. // It may return an error if the size is negative. func NewFloatSignalType(name string, size int) (*SignalType, error) { min := (1<