Skip to content

Commit

Permalink
Shortened DBC in the README and fixed linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Naveen Venkatesan committed Apr 1, 2021
1 parent ef9ceb3 commit 0248a67
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 69 deletions.
44 changes: 10 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" ;
Expand Down
22 changes: 11 additions & 11 deletions payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<<length)-1))
// done
return masked.Uint64()
Expand Down Expand Up @@ -163,7 +163,7 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
return AsSigned(unsigned, length)
}

// TODO: Implement SetUnsignedBitsLittleEndian for Payload
// TODO: Implement SetUnsignedBitsLittleEndian for Payload.
// SetUnsignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided unsigned value.
// func (d *Data) SetUnsignedBitsLittleEndian(start, length uint8, value uint64) {
// // pack bits into one continuous value
Expand All @@ -180,7 +180,7 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
// d.UnpackLittleEndian(newPacked)
// }

// TODO: Implement SetUnsignedBitsBigEndian for Payload
// TODO: Implement SetUnsignedBitsBigEndian for Payload.
// SetUnsignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided unsigned value.
// func (d *Data) SetUnsignedBitsBigEndian(start, length uint8, value uint64) {
// // pack bits into one continuous value
Expand All @@ -199,13 +199,13 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
// d.UnpackBigEndian(newPacked)
// }

// TODO: Implement SetSignedBitsLittleEndian for Payload
// TODO: Implement SetSignedBitsLittleEndian for Payload.
// SetSignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided signed value.
// func (d *Data) SetSignedBitsLittleEndian(start, length uint8, value int64) {
// d.SetUnsignedBitsLittleEndian(start, length, reinterpret.AsUnsigned(value, length))
// }

// TODO: Implement SetSignedBitsBigEndian for Payload
// TODO: Implement SetSignedBitsBigEndian for Payload.
// SetSignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided signed value.
// func (d *Data) SetSignedBitsBigEndian(start, length uint8, value int64) {
// d.SetUnsignedBitsBigEndian(start, length, reinterpret.AsUnsigned(value, length))
Expand Down Expand Up @@ -240,7 +240,7 @@ func (p *Payload) SetBit(i uint16, value bool) {
}
}

// PackLittleEndian packs the byte array into a continuous little endian big.Int
// PackLittleEndian packs the byte array into a continuous little endian big.Int.
func (p *Payload) PackLittleEndian() *big.Int {
if p.PackedLittleEndian == nil {
packed := new(big.Int).SetBytes(reverse(p.Data))
Expand All @@ -249,7 +249,7 @@ func (p *Payload) PackLittleEndian() *big.Int {
return new(big.Int).Set(p.PackedLittleEndian)
}

// reverse byte array for little endian signals
// Reverse byte array for little endian signals.
func reverse(data []byte) []byte {
reversedArray := make([]byte, len(data))
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
Expand All @@ -258,7 +258,7 @@ func reverse(data []byte) []byte {
return reversedArray
}

// PackBigEndian packs the byte array into a continuous big endian big.Int
// PackBigEndian packs the byte array into a continuous big endian big.Int.
func (p *Payload) PackBigEndian() *big.Int {
if p.PackedBigEndian == nil {
var packed = new(big.Int).SetBytes(p.Data)
Expand All @@ -267,7 +267,7 @@ func (p *Payload) PackBigEndian() *big.Int {
return new(big.Int).Set(p.PackedBigEndian)
}

// TODO: Implement UnpackLittleEndian for Payload
// TODO: Implement UnpackLittleEndian for Payload.
// UnpackLittleEndian sets the value of d.Bytes by unpacking the provided value as sequential little-endian bits.
// func (d *Data) UnpackLittleEndian(packed uint64) {
// d[0] = uint8(packed >> (0 * 8))
Expand All @@ -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))
Expand Down
12 changes: 2 additions & 10 deletions payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/descriptor/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -73,5 +72,4 @@ func (m *Message) Decode(p *can.Payload) []DecodedSignal {

}
return signals

}
6 changes: 3 additions & 3 deletions pkg/descriptor/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 12 additions & 8 deletions tests/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -195,14 +193,15 @@ func TestDecodeEvaporatorVariables(t *testing.T) {
},
}

var expectedMap = make(map[string]signal)
expectedMap := make(map[string]signal)
for _, s := range expected {
expectedMap[s.name] = s
}

for _, signal := range message.Signals {
value := signal.UnmarshalPhysicalPayload(&payload)
valueDesc, _ := signal.UnmarshalValueDescriptionPayload(&payload)
unit := signal.Unit
name := signal.Name

if value != expectedMap[name].value {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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[:]}

Expand All @@ -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[:]}

Expand All @@ -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}
Expand All @@ -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))
Expand Down

0 comments on commit 0248a67

Please sign in to comment.