Skip to content

Commit 358fde2

Browse files
committed
fix(canOutputCfg): fix binary (un)marshaling
1 parent 97ba7d8 commit 358fde2

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed
File renamed without changes.

canoutputconfiguration.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,51 @@ const (
2626
canOutputCfgCANDataIDMask = ^byte(1 << 7) // Masks reserved bit in canOutputCfg Block 1 MSB
2727

2828
// CANIDLengthFlag.
29-
canOutputCfgOffsetBlock2 = canOutputCfgLengthBlock1
29+
canOutputCfgOffsetBlock2 = canOutputCfgOffsetBlock1 + canOutputCfgLengthBlock1
3030
canOutputCfgLengthBlock2 = 1
3131
canOutputCfgIDLengthFlagMask = byte(0b1) // Masks reserved bit in canOutputCfg Block 2 MSB
3232

3333
// IDMask.
34-
canOutputCfgOffsetBlock3 = canOutputCfgLengthBlock2
34+
canOutputCfgOffsetBlock3 = canOutputCfgOffsetBlock2 + canOutputCfgLengthBlock2
3535
canOutputCfgLengthBlock3 = 4
3636
canOutputCfgIDMaskMask = ^byte(0b111 << 5) // Masks reserved bit in canOutputCfg Block 3 MSB
3737

3838
// OutputFrequency.
39-
canOutputCfgOffsetBlock4 = canOutputCfgLengthBlock3
39+
canOutputCfgOffsetBlock4 = canOutputCfgOffsetBlock3 + canOutputCfgLengthBlock3
4040
canOutputCfgLengthBlock4 = 2
4141
canOutputCfgOutputFreqMask = byte(0b111) // Masks reserved bit in canOutputCfg Block 4 MSB
4242
)
4343

4444
// CANOutputConfigurationSetting is the output configuration for a single CAN measurement data type.
4545
type CANOutputConfigurationSetting struct {
4646
// DataIdentifier is the data identifier of the data.
47-
CANDataIdentifier
47+
CANDataIdentifier CANDataIdentifier
4848

49-
CANIDLengthFlag
49+
// CANIDLengthFlag specifies whether the CAN address is 11 (CANIDLengthFlag11bits)
50+
// or 29 bits (CANIDLengthFlag29bits).
51+
CANIDLengthFlag CANIDLengthFlag
5052

51-
// TODO: Double check mask over interface
53+
// IDMask is a uint32 version of CANDataIdentifier.
54+
//
55+
// Only used for reading. DefaultIDMask is used for writing.
5256
IDMask IDMask
5357

5458
// OutputFrequency is the output frequency of the data.
5559
//
5660
// Selecting an Output OutputFrequency of either 0x0000 or 0xFFFF, makes the device select the
5761
// maximum frequency for the given data identifier. The device reports the resulting effective
5862
// frequencies in its response message.
59-
OutputFrequency
63+
OutputFrequency OutputFrequency
64+
}
65+
66+
// DefaultIDMask returns the default CAN ID Mask.
67+
func (o *CANOutputConfigurationSetting) DefaultIDMask() IDMask {
68+
return uint32(o.CANDataIdentifier)
6069
}
6170

6271
// MarshalBinary returns the wire representation of the CAN output configuration.
6372
func (o *CANOutputConfiguration) MarshalBinary() ([]byte, error) {
64-
buf := make([]byte, len(*o)*4)
73+
buf := make([]byte, len(*o)*canOutputCfgSettingSize)
6574
var b []byte
6675
// Push each setting to the buffer
6776
for i, setting := range *o {
@@ -80,7 +89,7 @@ func (o *CANOutputConfiguration) MarshalBinary() ([]byte, error) {
8089

8190
// push IDMask
8291
b = extractBytes(w, canOutputCfgOffsetBlock3, canOutputCfgLengthBlock3)
83-
binary.BigEndian.PutUint32(b, setting.IDMask)
92+
binary.BigEndian.PutUint32(b, setting.DefaultIDMask())
8493
b[0] &= canOutputCfgIDMaskMask // mask last byte
8594

8695
// push OutputFrequency
@@ -152,30 +161,9 @@ func (o *CANOutputConfiguration) UnmarshalBinary(data []byte) error {
152161
return nil
153162
}
154163

155-
// IDMask returns the ID mask expected.
156-
func (f CANIDLengthFlag) IDMask() IDMask {
157-
b := make([]byte, 0, 4)
158-
// bits 1-8
159-
b[0] = ^byte(0)
160-
161-
// bits 9-11(or 16)
162-
if f == CANIDLengthFlag11bits {
163-
b[1] = 0b111
164-
return binary.LittleEndian.Uint32(b)
165-
}
166-
b[1] = ^byte(0)
167-
168-
// bits 17-24
169-
b[2] = ^byte(0)
170-
171-
// bits 25-29
172-
b[3] = 0b11111
173-
return binary.LittleEndian.Uint32(b)
174-
}
175-
176164
// copyBytes returns a copy of the sub-slice of the byte array.
177165
func copyBytes(w []byte, offset, length int) []byte {
178-
b := make([]byte, 0, length)
166+
b := make([]byte, length)
179167
copy(b, w[offset:offset+length])
180168
return b
181169
}

0 commit comments

Comments
 (0)