Skip to content

Commit

Permalink
dbc importer/exporter value table support
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed May 29, 2024
1 parent cfc5cd5 commit 94fc0af
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
29 changes: 25 additions & 4 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type exporter struct {
nodeAttNames map[string]bool
msgAttNames map[string]bool
sigAttNames map[string]bool

sigEnums map[EntityID]*SignalEnum
}

func newExporter() *exporter {
Expand All @@ -77,6 +79,8 @@ func newExporter() *exporter {
nodeAttNames: make(map[string]bool),
msgAttNames: make(map[string]bool),
sigAttNames: make(map[string]bool),

sigEnums: make(map[EntityID]*SignalEnum),
}
}

Expand Down Expand Up @@ -261,6 +265,10 @@ func (e *exporter) exportBus(bus *Bus) *dbc.File {

e.exportNodeInterfaces(bus.Nodes())

for _, sigEnum := range e.sigEnums {
e.exportSignalEnum(sigEnum)
}

return e.dbcFile
}

Expand Down Expand Up @@ -464,15 +472,28 @@ func (e *exporter) exportEnumSignal(enumSig *EnumSignal, dbcMsgID uint32, dbcSig
dbcValEnc.Kind = dbc.ValueEncodingSignal
dbcValEnc.MessageID = dbcMsgID
dbcValEnc.SignalName = clearSpaces(enumSig.Name())
dbcValEnc.Values = e.getDBCValueDescription(enumSig.enum.Values())

for _, val := range enumSig.enum.Values() {
dbcValEnc.Values = append(dbcValEnc.Values, &dbc.ValueDescription{
e.dbcFile.ValueEncodings = append(e.dbcFile.ValueEncodings, dbcValEnc)
e.sigEnums[enumSig.enum.entityID] = enumSig.enum
}

func (e *exporter) getDBCValueDescription(enumValues []*SignalEnumValue) []*dbc.ValueDescription {
dbcValDesc := make([]*dbc.ValueDescription, len(enumValues))
for idx, val := range enumValues {
dbcValDesc[idx] = &dbc.ValueDescription{
ID: uint32(val.index),
Name: val.name,
})
}
}
return dbcValDesc
}

e.dbcFile.ValueEncodings = append(e.dbcFile.ValueEncodings, dbcValEnc)
func (e *exporter) exportSignalEnum(enum *SignalEnum) {
e.dbcFile.ValueTables = append(e.dbcFile.ValueTables, &dbc.ValueTable{
Name: enum.name,
Values: e.getDBCValueDescription(enum.Values()),
})
}

func (e *exporter) exportMultiplexerSignal(muxSig *MultiplexerSignal, dbcMsgID uint32, dbcSig *dbc.Signal) {
Expand Down
65 changes: 59 additions & 6 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ type importer struct {
signals map[string]Signal

flagSigType *SignalType
signalEnums map[string]*SignalEnum

signalEnumRegistry []*SignalEnum
signalEnums map[string]*SignalEnum

dbcExtMuxes map[string]*dbc.ExtendedMux
}
Expand All @@ -53,7 +55,9 @@ func newImporter() *importer {
signals: make(map[string]Signal),

flagSigType: NewFlagSignalType("flag"),
signalEnums: make(map[string]*SignalEnum),

signalEnumRegistry: []*SignalEnum{},
signalEnums: make(map[string]*SignalEnum),

dbcExtMuxes: make(map[string]*dbc.ExtendedMux),
}
Expand All @@ -73,6 +77,12 @@ func (i *importer) importFile(dbcFile *dbc.File) (*Bus, error) {

i.importComments(dbcFile.Comments)

for _, dbcValTable := range dbcFile.ValueTables {
if err := i.importValueTable(dbcValTable); err != nil {
return nil, err
}
}

for _, dbcValEnc := range dbcFile.ValueEncodings {
if err := i.importValueEncoding(dbcValEnc); err != nil {
return nil, err
Expand Down Expand Up @@ -287,17 +297,60 @@ func (i *importer) importAttributes(dbcAtts []*dbc.Attribute, dbcAttDefs []*dbc.
return nil
}

func (i *importer) importValueTable(dbcValTable *dbc.ValueTable) error {
sigEnum := NewSignalEnum(dbcValTable.Name)

for _, dbcVal := range dbcValTable.Values {
if err := sigEnum.AddValue(NewSignalEnumValue(dbcVal.Name, int(dbcVal.ID))); err != nil {
return i.errorf(dbcVal, err)
}
}

i.signalEnumRegistry = append(i.signalEnumRegistry, sigEnum)

return nil
}

func (i *importer) importValueEncoding(dbcValEnc *dbc.ValueEncoding) error {
if dbcValEnc.Kind != dbc.ValueEncodingSignal {
return nil
}

values := dbcValEnc.Values
slices.SortFunc(values, func(a, b *dbc.ValueDescription) int { return int(a.ID) - int(b.ID) })

sigEnum := new(SignalEnum)
inRegistry := false
for _, tmpSigEnum := range i.signalEnumRegistry {
if len(values) != tmpSigEnum.values.size() {
continue
}

for idx, tmpVal := range tmpSigEnum.Values() {
if tmpVal.name != values[idx].Name || tmpVal.index != int(values[idx].ID) {
break
}

if idx == (len(values) - 1) {
inRegistry = true
}
}

if inRegistry {
sigEnum = tmpSigEnum
break
}
}

sigName := dbcValEnc.SignalName
sigEnum := NewSignalEnum(fmt.Sprintf("%s_Enum", sigName))
for _, dbcVal := range dbcValEnc.Values {
if err := sigEnum.AddValue(NewSignalEnumValue(dbcVal.Name, int(dbcVal.ID))); err != nil {
return i.errorf(dbcVal, err)
if !inRegistry {
sigEnum = NewSignalEnum(fmt.Sprintf("%s_Enum", sigName))
for _, dbcVal := range dbcValEnc.Values {
if err := sigEnum.AddValue(NewSignalEnumValue(dbcVal.Name, int(dbcVal.ID))); err != nil {
return i.errorf(dbcVal, err)
}
}

}

i.signalEnums[i.getSignalKey(dbcValEnc.MessageID, sigName)] = sigEnum
Expand Down
2 changes: 2 additions & 0 deletions testdata/expected.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ BS_:

BU_: node_0 rec_node_0

VAL_TABLE_ enum 0 "VALUE_0" 1 "VALUE_1" 15 "VALUE_15";

BO_ 16 msg_0 : 8 node_0
SG_ std_sig_0 : 0|4@1+ (1,0) [0|15] "degC" Vector__XXX
SG_ mux_sig_0 M : 4|2@1+ (1,0) [0|3] "" Vector__XXX
Expand Down

0 comments on commit 94fc0af

Please sign in to comment.