diff --git a/README.md b/README.md index 979984b..60d2520 100644 --- a/README.md +++ b/README.md @@ -34,40 +34,16 @@ func main() { BU_: DBG DRIVER IO MOTOR SENSOR BO_ 1530 DisconnectState: 14 MOTOR - SG_ LockCountRearRight : 91|20@0+ (1,0) [0|1048575] "" IO - SG_ DisconnectStateRearRight : 95|4@0+ (1,0) [0|5] "" IO - SG_ CurrentRearRight : 79|16@0+ (1,0) [0|65535] "" IO - SG_ DisconnectStateRearRightTarget : 64|1@0+ (1,0) [0|1] "" IO - SG_ TargetSpeedRearRight : 63|15@0+ (0.125,-2048) [-2048|2047.875] "rad/s" IO - SG_ LockCountRearLeft : 35|20@0+ (1,0) [0|1048575] "" IO - SG_ DisconnectStateRearLeft : 39|4@0+ (1,0) [0|5] "" IO - SG_ CurrentRearLeft : 23|16@0+ (1,0) [0|65535] "" IO - SG_ DisconnectStateRearLeftTarget : 8|1@0+ (1,0) [0|1] "" IO - SG_ TargetSpeedRearLeft : 7|15@0+ (0.125,-2048) [-2048|2047.875] "rad/s" IO - - BA_DEF_ "BusType" STRING ; - BA_DEF_ BO_ "GenMsgSendType" ENUM "None","Cyclic","OnEvent"; - BA_DEF_ BO_ "GenMsgCycleTime" INT 0 0; - BA_DEF_ SG_ "FieldType" STRING ; - BA_DEF_ SG_ "GenSigStartValue" INT 0 10000; - BA_DEF_DEF_ "BusType" "CAN"; - BA_DEF_DEF_ "FieldType" ""; - BA_DEF_DEF_ "GenMsgCycleTime" 0; - BA_DEF_DEF_ "GenSigStartValue" 0; - - BA_ "GenMsgSendType" BO_ 1 0; - BA_ "GenMsgSendType" BO_ 100 1; - BA_ "GenMsgCycleTime" BO_ 100 1000; - BA_ "GenMsgSendType" BO_ 101 1; - BA_ "GenMsgCycleTime" BO_ 101 100; - BA_ "GenMsgSendType" BO_ 200 1; - BA_ "GenMsgCycleTime" BO_ 200 100; - BA_ "GenMsgSendType" BO_ 400 1; - BA_ "GenMsgCycleTime" BO_ 400 100; - BA_ "GenMsgSendType" BO_ 500 2; - BA_ "FieldType" SG_ 100 Command "Command"; - BA_ "FieldType" SG_ 500 TestEnum "TestEnum"; - BA_ "GenSigStartValue" SG_ 500 TestEnum 2; + SG_ LockCountRearRight : 91|20@0+ (1,0) [0|1048575] "" IO + SG_ DisconnectStateRearRight : 95|4@0+ (1,0) [0|5] "" IO + SG_ CurrentRearRight : 79|16@0+ (1,0) [0|65535] "" IO + SG_ DisconnectStateRearRightTarget : 64|1@0+ (1,0) [0|1] "" IO + SG_ TargetSpeedRearRight : 63|15@0+ (0.125,-2048) [-2048|2047.875] "rad/s" IO + SG_ LockCountRearLeft : 35|20@0+ (1,0) [0|1048575] "" IO + SG_ DisconnectStateRearLeft : 39|4@0+ (1,0) [0|5] "" IO + SG_ CurrentRearLeft : 23|16@0+ (1,0) [0|65535] "" IO + SG_ DisconnectStateRearLeftTarget : 8|1@0+ (1,0) [0|1] "" IO + SG_ TargetSpeedRearLeft : 7|15@0+ (0.125,-2048) [-2048|2047.875] "rad/s" IO VAL_ 1530 DisconnectStateRearRight 0 "Undefined" 1 "Locked" 2 "Unlocked" 3 "Locking" 4 "Unlocking" 5 "Faulted" ; VAL_ 1530 DisconnectStateRearLeft 0 "Undefined" 1 "Locked" 2 "Unlocked" 3 "Locking" 4 "Unlocking" 5 "Faulted" ; diff --git a/payload.go b/payload.go index 12d4002..9939732 100644 --- a/payload.go +++ b/payload.go @@ -104,11 +104,13 @@ type Payload struct { PackedBigEndian *big.Int } +// Hex returns the hexadecimal representation of the byte array in a Payload func (p *Payload) Hex() string { h := hex.EncodeToString(p.Data) return h } +// PayloadFromHex generates a Payload from a hexadecimal string. func PayloadFromHex(hexString string) (Payload, error) { b, err := hex.DecodeString(hexString) var p Payload @@ -122,14 +124,12 @@ func PayloadFromHex(hexString string) (Payload, error) { // UnsignedBitsLittleEndian returns the little-endian bit range [start, start+length) as an unsigned value. func (p *Payload) UnsignedBitsLittleEndian(start, length uint16) uint64 { // pack bits into one continuous value - packed := p.PackLittleEndian() // lsb index in the packed value is the start bit lsbIndex := uint(start) // shift away lower bits shifted := packed.Rsh(packed, lsbIndex) // mask away higher bits - //masked := shifted & ((1 << length) - 1) masked := shifted.And(shifted, big.NewInt((1<> (0 * 8)) @@ -280,7 +280,7 @@ func (p *Payload) PackBigEndian() *big.Int { // d[7] = uint8(packed >> (7 * 8)) // } -// TODO: Implement UnpackBigEndian for Payload +// TODO: Implement UnpackBigEndian for Payload. // UnpackBigEndian sets the value of d.Bytes by unpacking the provided value as sequential big-endian bits. // func (d *Data) UnpackBigEndian(packed uint64) { // d[0] = uint8(packed >> (7 * 8)) diff --git a/payload_test.go b/payload_test.go index 0ea7e29..589f058 100644 --- a/payload_test.go +++ b/payload_test.go @@ -20,11 +20,7 @@ func TestPackLittleEndian(t *testing.T) { dataLittleEndian := payload.PackLittleEndian() fmt.Println(dataLittleEndian) - fmt.Println(fmt.Sprintf("%b", dataLittleEndian)) - - // if fmt.Sprintf("%08b", dataLittleEndian) != "10010000000010010001101000000" { - // t.Errorf("fmt.Sprintf('%08b', dataLittleEndian) = %s); want 10010000000010010001101000000", fmt.Sprintf("%08b", dataLittleEndian)) - // } + fmt.Printf("%b\n", dataLittleEndian) } func TestPackBigEndian(t *testing.T) { @@ -35,11 +31,7 @@ func TestPackBigEndian(t *testing.T) { dataBigEndian := payload.PackBigEndian() fmt.Println(dataBigEndian) - fmt.Println(fmt.Sprintf("%b", dataBigEndian)) - - // if fmt.Sprintf("%08b", dataBigEndian) != "10000000010001100000001000100100" { - // t.Errorf("fmt.Sprintf('%08b', dataBigEndian) = %s); want 10000000010001100000001000100100", fmt.Sprintf("%08b", dataBigEndian)) - // } + fmt.Printf("%b\n", dataBigEndian) } func TestUnsignedLittleEndian(t *testing.T) { diff --git a/pkg/descriptor/message.go b/pkg/descriptor/message.go index 1f9e6f4..6bcd617 100644 --- a/pkg/descriptor/message.go +++ b/pkg/descriptor/message.go @@ -40,9 +40,8 @@ func (m *Message) MultiplexerSignal() (*Signal, bool) { return nil, false } -// Decode decodes a can Payload into a decoded signal array +// Decode decodes a can Payload into a decoded signal array. func (m *Message) Decode(p *can.Payload) []DecodedSignal { - var data can.Data if m.Length <= 8 { copy(data[:], p.Data) @@ -73,5 +72,4 @@ func (m *Message) Decode(p *can.Payload) []DecodedSignal { } return signals - } diff --git a/pkg/descriptor/signal.go b/pkg/descriptor/signal.go index fbc7034..1a40c5d 100644 --- a/pkg/descriptor/signal.go +++ b/pkg/descriptor/signal.go @@ -47,7 +47,7 @@ type Signal struct { type DecodedSignal struct { // Value is the physical value of a decoded signal Value float64 - // Description physical descritpion of a decoded signal + // Description physical description of a decoded signal Description string // Signal is a pointer to the dbc signal Signal *Signal @@ -246,9 +246,9 @@ func (s *Signal) UnmarshalValueDescriptionPayload(p *can.Payload) (string, bool) // UnmarshalSigned returns the signed value of the signal in the provided CAN frame. func (s *Signal) UnmarshalSigned(d can.Data) int64 { if s.IsBigEndian { - return d.SignedBitsBigEndian(uint8(s.Start), uint8(uint8(s.Length))) + return d.SignedBitsBigEndian(uint8(s.Start), uint8(s.Length)) } - return d.SignedBitsLittleEndian(uint8(s.Start), uint8(uint8(s.Length))) + return d.SignedBitsLittleEndian(uint8(s.Start), uint8(s.Length)) } // UnmarshalSignedPayload returns the signed value of the signal in the provided CAN frame. diff --git a/tests/decode_test.go b/tests/decode_test.go index eee6235..584701c 100644 --- a/tests/decode_test.go +++ b/tests/decode_test.go @@ -125,7 +125,6 @@ func getDatabase() descriptor.Database { } func TestDecodeEvaporatorVariables(t *testing.T) { - c, err := generate.Compile("test.dbc", dbc) if err != nil { t.Errorf("err = %v; want nil", err) @@ -137,7 +136,6 @@ func TestDecodeEvaporatorVariables(t *testing.T) { canDataHexString := "008232204e027600ca4b0007d296" payload, err := can.PayloadFromHex(canDataHexString) - // ci := packet.Metadata().CaptureInfo if err != nil { t.Errorf("err = %v; want nil", err) } @@ -195,7 +193,7 @@ func TestDecodeEvaporatorVariables(t *testing.T) { }, } - var expectedMap = make(map[string]signal) + expectedMap := make(map[string]signal) for _, s := range expected { expectedMap[s.name] = s } @@ -203,6 +201,7 @@ func TestDecodeEvaporatorVariables(t *testing.T) { for _, signal := range message.Signals { value := signal.UnmarshalPhysicalPayload(&payload) valueDesc, _ := signal.UnmarshalValueDescriptionPayload(&payload) + unit := signal.Unit name := signal.Name if value != expectedMap[name].value { @@ -212,6 +211,10 @@ func TestDecodeEvaporatorVariables(t *testing.T) { if valueDesc != expectedMap[name].description { t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description) } + + if unit != expectedMap[name].unit { + t.Errorf("signal[%s] value = %s ; expected: %s", name, unit, expectedMap[name].unit) + } } for _, signal := range message.Decode(&payload) { @@ -311,7 +314,7 @@ func TestDecodeDisconnectState(t *testing.T) { }, } - var expectedMap = make(map[string]signal) + expectedMap := make(map[string]signal) for _, s := range expected { expectedMap[s.name] = s } @@ -320,6 +323,7 @@ func TestDecodeDisconnectState(t *testing.T) { for _, signal := range message.Signals { value := signal.UnmarshalPhysicalPayload(&p) valueDesc, _ := signal.UnmarshalValueDescriptionPayload(&p) + unit := signal.Unit name := signal.Name if value != expectedMap[name].value { @@ -329,11 +333,14 @@ func TestDecodeDisconnectState(t *testing.T) { if valueDesc != expectedMap[name].description { t.Errorf("signal[%s] value = %s ; expected: %s", name, valueDesc, expectedMap[name].description) } + + if unit != expectedMap[name].unit { + t.Errorf("signal[%s] value = %s ; expected: %s", name, unit, expectedMap[name].unit) + } } } func TestDecodeSensorSonarsData(t *testing.T) { - data := can.Data{0x01, 0x01, 0x01, 0x02, 0x01, 0x00} payload := can.Payload{Data: data[:]} @@ -359,7 +366,6 @@ func TestDecodeSensorSonarsData(t *testing.T) { } func TestMessageDecodeSensorSonarsData(t *testing.T) { - data := can.Data{0x01, 0x01, 0x01, 0x02, 0x01, 0x00} payload := can.Payload{Data: data[:]} @@ -386,7 +392,6 @@ func TestMessageDecodeSensorSonarsData(t *testing.T) { } func BenchmarkDecodeData(b *testing.B) { - message, _ := db.Message(uint32(500)) decodeSignal := func() { data := can.Data{0x01, 0x01, 0x01, 0x02, 0x01, 0x00} @@ -401,7 +406,6 @@ func BenchmarkDecodeData(b *testing.B) { } func BenchmarkDecodePayload(b *testing.B) { - // {0x01, 0x01, 0x01, 0x02, 0x01, 0x00} message, _ := db.Message(uint32(500))