Skip to content

Commit

Permalink
sync mac
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Apr 29, 2024
1 parent 59c6f2b commit f51f6aa
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 18 deletions.
3 changes: 3 additions & 0 deletions dbc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -531,6 +532,8 @@ func (p *Parser) parseMessage() (*Message, error) {
msg.Signals = append(msg.Signals, sig)
}

slices.SortFunc(msg.Signals, func(a, b *Signal) int { return int(a.StartBit) - int(b.StartBit) })

return msg, nil
}

Expand Down
135 changes: 121 additions & 14 deletions importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type importer struct {
sigDesc map[string]string

signalEnums map[string]*SignalEnum

extMuxMsg map[uint32]bool
dbcExtMuxes map[string]*dbc.ExtendedMux
}

func newImporter() *importer {
Expand All @@ -34,6 +37,9 @@ func newImporter() *importer {
sigDesc: make(map[string]string),

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

extMuxMsg: make(map[uint32]bool),
dbcExtMuxes: make(map[string]*dbc.ExtendedMux),
}
}

Expand All @@ -45,6 +51,10 @@ func (i *importer) getSignalKey(dbcMsgID uint32, sigName string) string {
return fmt.Sprintf("%d_%s", dbcMsgID, sigName)
}

func (i *importer) getMuxSignalKey(dbcMsgID uint32, muxorName, muxedName string) string {
return fmt.Sprintf("%d_%s_%s", dbcMsgID, muxorName, muxedName)
}

func (i *importer) importFile(dbcFile *dbc.File) (*Bus, error) {
bus := NewBus(dbcFile.Location().Filename)
i.bus = bus
Expand All @@ -57,6 +67,8 @@ func (i *importer) importFile(dbcFile *dbc.File) (*Bus, error) {
}
}

i.importExtMuxes(dbcFile.ExtendedMuxes)

if err := i.importNodes(dbcFile.Nodes); err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,6 +119,14 @@ func (i *importer) importValueEncoding(dbcValEnc *dbc.ValueEncoding) error {
return nil
}

func (i *importer) importExtMuxes(dbcExtMuxes []*dbc.ExtendedMux) {
for _, tmpExtMux := range dbcExtMuxes {
key := i.getSignalKey(tmpExtMux.MessageID, tmpExtMux.MultiplexedName)
i.dbcExtMuxes[key] = tmpExtMux
i.extMuxMsg[tmpExtMux.MessageID] = true
}
}

func (i *importer) importNodes(dbcNodes *dbc.Nodes) error {
for idx, nodeName := range dbcNodes.Names {
tmpNode := NewNode(nodeName, NodeID(idx))
Expand Down Expand Up @@ -138,19 +158,10 @@ func (i *importer) importMessage(dbcMsg *dbc.Message) error {
}

receivers := make(map[string]bool)
muxSignals := []*dbc.Signal{}
for _, dbcSig := range dbcMsg.Signals {
tmpSig, err := i.importSignal(dbcSig, dbcMsg.ID)
if err != nil {
return err
}

startBit := int(dbcSig.StartBit)
if tmpSig.ByteOrder() == SignalByteOrderBigEndian {
startBit -= (tmpSig.GetSize() - 1)
}

if err := msg.InsertSignal(tmpSig, startBit); err != nil {
return i.errorf(dbcSig, err)
if dbcSig.IsMultiplexor {
muxSignals = append(muxSignals, dbcSig)
}

for _, rec := range dbcSig.Receivers {
Expand Down Expand Up @@ -180,6 +191,104 @@ func (i *importer) importMessage(dbcMsg *dbc.Message) error {
return i.errorf(dbcMsg, err)
}

muxSigCount := len(muxSignals)
if muxSigCount == 0 {
for _, dbcSig := range dbcMsg.Signals {
tmpSig, err := i.importSignal(dbcSig, dbcMsg.ID)
if err != nil {
return err
}

startBit := int(dbcSig.StartBit)
if tmpSig.ByteOrder() == SignalByteOrderBigEndian {
startBit -= (tmpSig.GetSize() - 1)
}

if err := msg.InsertSignal(tmpSig, startBit); err != nil {
return i.errorf(dbcSig, err)
}
}

return nil
} else if muxSigCount == 1 {
lastStartBit := 0
lastSize := 0
for _, dbcSig := range dbcMsg.Signals {
if dbcSig.IsMultiplexed {
lastStartBit = int(dbcSig.StartBit)
lastSize = int(dbcSig.Size)
}
}

dbcMuxSig := muxSignals[0]
muxSigStartBit := int(dbcMuxSig.StartBit)
groupSize := lastStartBit + lastSize - muxSigStartBit - int(dbcMuxSig.Size)

muxSig, err := NewMultiplexerSignal(dbcMuxSig.Name, calcValueFromSize(int(dbcMuxSig.Size)), groupSize)
if err != nil {
i.errorf(dbcMuxSig, err)
}

for _, dbcSig := range dbcMsg.Signals {
if dbcSig.Name == muxSig.name {
continue
}

tmpSig, err := i.importSignal(dbcSig, dbcMsg.ID)
if err != nil {
return err
}

startBit := int(dbcSig.StartBit)
if tmpSig.ByteOrder() == SignalByteOrderBigEndian {
startBit -= (tmpSig.GetSize() - 1)
}

relStartBit := startBit - int(dbcMuxSig.StartBit) - int(dbcMuxSig.Size)

if dbcSig.IsMultiplexed {
groupIDs := []int{}

dbcExtMux, ok := i.dbcExtMuxes[i.getSignalKey(dbcMsg.ID, dbcSig.Name)]
if ok {
for _, valRange := range dbcExtMux.Ranges {
for j := valRange.From; j <= valRange.To; j++ {
groupIDs = append(groupIDs, int(j))
}
}

if len(groupIDs) == muxSig.groupCount {
groupIDs = []int{}
}

} else {
groupIDs = append(groupIDs, int(dbcSig.MuxSwitchValue))
}

if err := muxSig.InsertSignal(tmpSig, relStartBit, groupIDs...); err != nil {
return i.errorf(dbcSig, err)
}

continue
}

if startBit > muxSigStartBit && startBit <= lastSize {
if err := muxSig.InsertSignal(tmpSig, relStartBit); err != nil {
return i.errorf(dbcSig, err)
}
continue
}

if err := msg.InsertSignal(tmpSig, startBit); err != nil {
return i.errorf(dbcSig, err)
}
}

if err := msg.InsertSignal(muxSig, muxSigStartBit); err != nil {
return i.errorf(dbcMuxSig, err)
}
}

return nil
}

Expand All @@ -196,8 +305,6 @@ func (i *importer) importSignal(dbcSig *dbc.Signal, dbcMsgID uint32) (Signal, er
}
sig = enumSig

} else if dbcSig.IsMultiplexor {
// mux signal
} else {
signed := false
if dbcSig.ValueType == dbc.SignalSigned {
Expand Down
12 changes: 9 additions & 3 deletions importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ func Test_ImportDBCFile(t *testing.T) {
dbcFile, err := parser.Parse()
assert.NoError(err)

t.Log(dbcFile)
// t.Log(dbcFile)

// bus, err := ImportDBCFile(dbcFile)
bus, err := ImportDBCFile(dbcFile)
assert.NoError(err)

t.Log(bus)

// resFile, err := os.Create("testdata/res.dbc")
// assert.NoError(err)

// t.Log(bus)
// ExportBus(resFile, bus)
// resFile.Close()
}
4 changes: 3 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func (m *Message) addSignal(sig Signal) {

for tmpSigID, tmpSig := range muxSig.signals.entries() {
m.signals.add(tmpSigID, tmpSig)
tmpSig.setParentMsg(m)
}

for tmpName, tmpSigID := range muxSig.signalNames.entries() {
Expand All @@ -315,8 +316,9 @@ func (m *Message) removeSignal(sig Signal) {
panic(err)
}

for _, tmpSigID := range muxSig.signals.getKeys() {
for tmpSigID, tmpSig := range muxSig.signals.entries() {
m.signals.remove(tmpSigID)
tmpSig.setParentMsg(nil)
}

for _, tmpName := range muxSig.signalNames.getKeys() {
Expand Down
58 changes: 58 additions & 0 deletions testdata/res.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
VERSION "_"

NS_:
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
VAL_TABLE_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SIG_GROUP_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_: node_0 rec_node_0 Vector__XXX

BO_ 16 msg_0 : 8 node_0
SG_ std_sig_0 : 3|4@0+ (1,0) [0|15] "degC" Vector__XXX
SG_ mux_sig_0 M : 4|2@1+ (1,0) [0|3] "" Vector__XXX
SG_ fixed_sig m0 : 6|4@1+ (1,0) [0|15] "" Vector__XXX
SG_ multi_group_sig_0 m0 : 10|4@1+ (1,0) [0|15] "" Vector__XXX
SG_ one_group_sig_0 m1 : 10|4@1+ (1,0) [0|15] "" Vector__XXX

BO_ 32 msg_1 : 8 node_0

BO_ 48 msg_2 : 8 node_0
SG_ enum_sig_0 : 0|4@1+ (1,0) [0|15] "" rec_node_0


CM_ "bus0 description";
CM_ BU_ node_0 "node0 description";
CM_ BO_ 32 "msg1 description";

VAL_ 48 enum_sig_0 0 "VALUE_0" 1 "VALUE_1" 15 "VALUE_15";

SG_MUL_VAL_ 16 fixed_sig mux_sig_0 0-3;
SG_MUL_VAL_ 16 multi_group_sig_0 mux_sig_0 0-0, 2-3;

7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ func calcSizeFromValue(val int) int {
return maxSize
}

func calcValueFromSize(size int) int {
if size <= 0 {
return 1
}
return 1 << size
}

func clearSpaces(str string) string {
return strings.ReplaceAll(strings.TrimSpace(str), " ", "_")
}
Expand Down
9 changes: 9 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ func Test_calcSizeFromValue(t *testing.T) {
assert.Equal(7, calcSizeFromValue(127))
assert.Equal(9, calcSizeFromValue(256))
}

func Test_calcValueFromSize(t *testing.T) {
assert := assert.New(t)

assert.Equal(1, calcValueFromSize(0))
assert.Equal(256, calcValueFromSize(8))
assert.Equal(32, calcValueFromSize(5))
assert.Equal(8, calcValueFromSize(3))
}

0 comments on commit f51f6aa

Please sign in to comment.