Skip to content

Commit

Permalink
dbc importer improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed May 30, 2024
1 parent 555925e commit 2a55e65
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 32 deletions.
6 changes: 3 additions & 3 deletions bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (b *Bus) stringify(builder *strings.Builder, tabs int) {
}

builder.WriteString(fmt.Sprintf("%sattached_node_interfaces:\n", tabStr))
for _, nodeInt := range b.Nodes() {
for _, nodeInt := range b.NodeInterfaces() {
nodeInt.stringify(builder, tabs+1)
builder.WriteRune('\n')
}
Expand Down Expand Up @@ -230,8 +230,8 @@ func (b *Bus) RemoveAllNodeInterfaces() {
b.nodeIDs.clear()
}

// Nodes returns a slice of all nodes in the [Bus] sorted by node id.
func (b *Bus) Nodes() []*NodeInterface {
// NodeInterfaces returns a slice of all node interfaces connected to the [Bus] sorted by node id.
func (b *Bus) NodeInterfaces() []*NodeInterface {
nodeSlice := b.nodeInts.getValues()
slices.SortFunc(nodeSlice, func(a, b *NodeInterface) int { return int(a.node.id) - int(b.node.id) })
return nodeSlice
Expand Down
6 changes: 3 additions & 3 deletions bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_Bus_AddNodeInterface(t *testing.T) {
assert.NoError(bus.AddNodeInterface(node2))
expectedIDs := []NodeID{0, 1, 2}
expectedNames := []string{"node_0", "node_1", "node_2"}
for idx, tmpNode := range bus.Nodes() {
for idx, tmpNode := range bus.NodeInterfaces() {
assert.Equal(expectedIDs[idx], tmpNode.node.ID())
assert.Equal(expectedNames[idx], tmpNode.node.Name())
}
Expand Down Expand Up @@ -54,7 +54,7 @@ func Test_Bus_RemoveNodeInterface(t *testing.T) {
assert.NoError(bus.RemoveNodeInterface(node2.EntityID()))
expectedIDs := []NodeID{0, 1, 3}
expectedNames := []string{"node_0", "node_1", "node_3"}
for idx, tmpNode := range bus.Nodes() {
for idx, tmpNode := range bus.NodeInterfaces() {
assert.Equal(expectedIDs[idx], tmpNode.node.ID())
assert.Equal(expectedNames[idx], tmpNode.node.Name())
}
Expand All @@ -80,7 +80,7 @@ func Test_Bus_RemoveAllNodeInterfaces(t *testing.T) {

bus.RemoveAllNodeInterfaces()

assert.Equal(0, len(bus.Nodes()))
assert.Equal(0, len(bus.NodeInterfaces()))
}

func Test_Bus_UpdateName(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (e *exporter) exportBus(bus *Bus) *dbc.File {
Baudrate: uint32(bus.baudrate),
}

e.exportNodeInterfaces(bus.Nodes())
e.exportNodeInterfaces(bus.NodeInterfaces())

for _, sigEnum := range e.sigEnums {
e.exportSignalEnum(sigEnum)
Expand Down
80 changes: 57 additions & 23 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type importer struct {
signals map[string]Signal

flagSigType *SignalType
signalTypes map[string]*SignalType

signalUnits map[string]*SignalUnit

signalEnumRegistry []*SignalEnum
signalEnums map[string]*SignalEnum
Expand All @@ -55,6 +58,9 @@ func newImporter() *importer {
signals: make(map[string]Signal),

flagSigType: NewFlagSignalType("flag"),
signalTypes: make(map[string]*SignalType),

signalUnits: make(map[string]*SignalUnit),

signalEnumRegistry: []*SignalEnum{},
signalEnums: make(map[string]*SignalEnum),
Expand All @@ -71,6 +77,14 @@ func (i *importer) getSignalKey(dbcMsgID uint32, sigName string) string {
return fmt.Sprintf("%d_%s", dbcMsgID, sigName)
}

func (i *importer) getSignalTypeKey(dbcSig *dbc.Signal) string {
signStr := "u"
if dbcSig.ValueType == dbc.SignalSigned {
signStr = "s"
}
return fmt.Sprintf("%s%d_%g-%g_%g_%g", signStr, dbcSig.Size, dbcSig.Min, dbcSig.Max, dbcSig.Factor, dbcSig.Offset)
}

func (i *importer) importFile(dbcFile *dbc.File) (*Bus, error) {
bus := NewBus(dbcFile.Location().Filename)
i.bus = bus
Expand Down Expand Up @@ -654,36 +668,25 @@ func (i *importer) importSignal(dbcSig *dbc.Signal, dbcMsgID uint32) (Signal, er
sig = enumSig

} else {
signed := false
if dbcSig.ValueType == dbc.SignalSigned {
signed = true
}

sigSize := int(dbcSig.Size)
var sigType *SignalType
if sigSize == 1 && dbcSig.ValueType == dbc.SignalUnsigned {
sigType = i.flagSigType
} else {
tmpSigType, err := NewIntegerSignalType(fmt.Sprintf("%s_Type", sigName), sigSize, signed)
if err != nil {
return nil, i.errorf(dbcSig, err)
}

tmpSigType.SetMin(dbcSig.Min)
tmpSigType.SetMax(dbcSig.Max)
tmpSigType.SetScale(dbcSig.Factor)
tmpSigType.SetOffset(dbcSig.Offset)

sigType = tmpSigType
sigType, err := i.importSignalType(dbcSig)
if err != nil {
return nil, err
}

stdSig, err := NewStandardSignal(sigName, sigType)
if err != nil {
return nil, i.errorf(dbcSig, err)
}

if dbcSig.Unit != "" {
stdSig.SetUnit(NewSignalUnit(fmt.Sprintf("%s_Unit", sigName), SignalUnitKindCustom, dbcSig.Unit))
symbol := dbcSig.Unit
if symbol != "" {
if sigUnit, ok := i.signalUnits[symbol]; ok {
stdSig.SetUnit(sigUnit)
} else {
sigUnit := NewSignalUnit(symbol, SignalUnitKindCustom, symbol)
stdSig.SetUnit(sigUnit)
i.signalUnits[symbol] = sigUnit
}
}

sig = stdSig
Expand All @@ -697,3 +700,34 @@ func (i *importer) importSignal(dbcSig *dbc.Signal, dbcMsgID uint32) (Signal, er

return sig, nil
}

func (i *importer) importSignalType(dbcSig *dbc.Signal) (*SignalType, error) {
signed := false
if dbcSig.ValueType == dbc.SignalSigned {
signed = true
}

sigSize := int(dbcSig.Size)
if sigSize == 1 && !signed {
return i.flagSigType, nil
}

sigTypeKey := i.getSignalTypeKey(dbcSig)
if sigType, ok := i.signalTypes[sigTypeKey]; ok {
return sigType, nil
}

sigType, err := NewIntegerSignalType(sigTypeKey, sigSize, signed)
if err != nil {
return nil, i.errorf(dbcSig, err)
}

sigType.SetMin(dbcSig.Min)
sigType.SetMax(dbcSig.Max)
sigType.SetScale(dbcSig.Factor)
sigType.SetOffset(dbcSig.Offset)

i.signalTypes[sigTypeKey] = sigType

return sigType, nil
}
17 changes: 17 additions & 0 deletions importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ func Test_ImportDBCFile(t *testing.T) {
fileStr := strings.ReplaceAll(fileBuf.String(), "\n", "")

assert.Equal(expectedFileStr, fileStr)

// testing signal types aggregation
sigTypeIDs := make(map[EntityID]bool)
for _, tmpNodeInt := range bus.NodeInterfaces() {
for _, tmpMsg := range tmpNodeInt.Messages() {
for _, sig := range tmpMsg.signals.getValues() {
if sig.Kind() != SignalKindStandard {
continue
}

stdSig, err := sig.ToStandard()
assert.NoError(err)
sigTypeIDs[stdSig.Type().EntityID()] = true
}
}
}
assert.Len(sigTypeIDs, 1)
}
4 changes: 2 additions & 2 deletions md_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (e *mdExporter) exportNetwork(net *Network) {
func (e *mdExporter) exportTOC(net *Network) {
for _, bus := range net.Buses() {
e.w.BulletList(e.getHeaderLink(bus.name))
for _, nodeInt := range bus.Nodes() {
for _, nodeInt := range bus.NodeInterfaces() {
e.w.PlainTextf("\t- %s", e.getHeaderLink(nodeInt.node.name))
for _, msg := range nodeInt.Messages() {
e.w.PlainTextf("\t\t- %s", e.getHeaderLink(msg.name))
Expand All @@ -80,7 +80,7 @@ func (e *mdExporter) exportBus(bus *Bus) {
}
e.w.PlainTextf("Baudrate: %s bps", baudrateStr).LF()

for _, node := range bus.Nodes() {
for _, node := range bus.NodeInterfaces() {
e.exportNode(node)
}
}
Expand Down

0 comments on commit 2a55e65

Please sign in to comment.