8000 dbc: add semantic DBC round-tripping and database edits by tomrford · Pull Request #111 · tomrford/gocan · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
| [`asc`](https://pkg.go.dev/github.com/tomrford/gocan/asc) | Vector ASCII trace writing |
| [`mf4`](https://pkg.go.dev/github.com/tomrford/gocan/mf4) | MDF 4.10 raw CAN/CAN FD export with DBC attachments, event markers, compression and application metadata |
| [`recorder`](https://pkg.go.dev/github.com/tomrford/gocan/recorder) | Lifecycled trace recording with safe flush checkpoints |
| [`dbc`](https://pkg.go.dev/github.com/tomrford/gocan/dbc) | DBC parsing with original source retention, and CAN frame encoding and decoding |
| [`dbc`](https://pkg.go.dev/github.com/tomrford/gocan/dbc) | DBC parsing with original source retention, DBC writing, and CAN frame encoding and decoding |
| [`j1939`](https://pkg.go.dev/github.com/tomrford/gocan/j1939) | J1939 identifiers, passive decoding, active address claiming and classical BAM/RTS-CTS transport |
| [`isotp`](https://pkg.go.dev/github.com/tomrford/gocan/isotp) | ISO-TP payload transport over classical CAN and CAN FD |
| [`uds`](https://pkg.go.dev/github.com/tomrford/gocan/uds) | Raw exchanges and typed Unified Diagnostic Services operations |
Expand Down
175 changes: 175 additions & 0 deletions dbc/edit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package dbc_test

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/tomrford/gocan"
"github.com/tomrford/gocan/dbc"
"golang.org/x/text/encoding/charmap"
)

// The device fixture describes device ID 0, whose ID occupies identifier bits
// 8-11, the way a single-device interface DBC is shipped and then derived for
// each installed device.
func TestEditDerivesDeviceDatabases(t *testing.T) {
base, err := dbc.ParseFile("testdata/device.dbc")
if err != nil {
t.Fatal(err)
}
forDevice := func(device uint32) func(*dbc.Message) {
return func(message *dbc.Message) {
message.ID |= device << 8
message.Name = strings.Replace(message.Name, "_0_", fmt.Sprintf("_%X_", device), 1)
}
}

deviceA, err := base.Edit("device-A.dbc", func(db *dbc.Database) {
for index := range db.Messages {
forDevice(0xA)(&db.Messages[index])
}
})
if err != nil {
t.Fatal(err)
}
assertDevice := func(t *testing.T, db *dbc.Database, device uint32) {
t.Helper()
name := fmt.Sprintf("DEV_%X_STAT", device)
status, ok := db.MessageByID(0x10061003|device<<8, true)
if !ok || status.Name != name || status.Comment != "Device status" ||
status.Attributes["GenMsgCycleTime"].Integer != 100 {
t.Fatalf("device %X status message: %#v", device, status)
}
coolant, _ := status.SignalByName("Coolant")
state, _ := status.SignalByName("State")
if coolant == nil || coolant.Comment != "Kühlmittel inlet" || coolant.Unit != "°C" ||
state == nil || len(state.Values) != 3 || state.Values[2].Label != "Fault" {
t.Fatalf("device %X status signals: %#v", device, status.Signals)
}
control, ok := db.MessageByName(fmt.Sprintf("DEV_%X_CTRL", device))
if !ok {
t.Fatalf("device %X control message is missing", device)
}
frame, err := control.Encode(dbc.Values{"Enable": true, "Setpoint": 12.5})
if err != nil {
t.Fatal(err)
}
if frame.ID != 0x10063001|device<<8 || !frame.Flags.Has(gocan.FrameExtended) || frame.Data[0] != 1 || frame.Data[1] != 25 {
t.Fatalf("device %X control frame: %v", device, frame)
}
}
assertDevice(t, deviceA, 0xA)
if _, ok := deviceA.MessageByName("DEV_0_STAT"); ok {
t.Fatal("derived database still answers the device 0 name")
}
// The derived source is the DBC to store alongside device A's traces.
reparsed, err := dbc.Parse("device-A-source.dbc", deviceA.Source())
if err != nil {
t.Fatal(err)
}
assertDevice(t, reparsed, 0xA)

// Frame format is edited through the DBC attributes that declare it.
fd, err := deviceA.Edit("device-A-fd.dbc", func(db *dbc.Database) {
db.AttributeDefinitions = append(db.AttributeDefinitions, dbc.AttributeDefinition{
Name: "VFrameFormat", Scope: dbc.AttributeScopeMessage, Kind: dbc.AttributeKindInteger, Maximum: 15,
})
db.Messages[1].Attributes["VFrameFormat"] = dbc.AttributeValue{Kind: dbc.AttributeKindInteger, Integer: 15}
})
if err != nil {
t.Fatal(err)
}
fdSource, err := dbc.Parse("device-A-fd-source.dbc", fd.Source())
if err != nil {
t.Fatal(err)
}
for _, db := range []*dbc.Database{fd, fdSource} {
assertDevice(t, db, 0xA)
control, _ := db.MessageByName("DEV_A_CTRL")
frame, err := control.Encode(dbc.Values{"Enable": true, "Setpoint": 12.5})
if err != nil || control.Format != dbc.FrameFormatExtendedCANFD || !frame.Flags.Has(gocan.FrameFD) {
t.Fatalf("edited CAN FD control: format=%v frame=%v error=%v", control.Format, frame, err)
}
}
if _, changed := deviceA.Messages[1].Attributes["VFrameFormat"]; changed || deviceA.Messages[1].Format != dbc.FrameFormatExtendedCAN {
t.Fatal("frame-format edit changed the original database")
}

// One database for every device, as a release would ship it.
everyDevice, err := base.Edit("device-all.dbc", func(db *dbc.Database) {
device0 := db.Messages
db.Messages = nil
for device := range uint32(16) {
for _, message := range device0 {
forDevice(device)(&message)
db.Messages = append(db.Messages, message)
}
}
})
if err != nil {
t.Fatal(err)
}
if len(everyDevice.Messages) != 48 {
t.Fatalf("expanded database has %d messages, want 48", len(everyDevice.Messages))
}
for _, device := range []uint32{0, 7, 0xF} {
assertDevice(t, everyDevice, device)
}

// Windows-1252 for tools that expect it; the file is the test's artifact.
path := filepath.Join(t.TempDir(), "device-A.dbc")
file, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
if err := deviceA.Write(charmap.Windows1252.NewEncoder().Writer(file)); err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
written, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(written, []byte("K\xfchlmittel")) {
t.Fatal("written file is not Windows-1252")
}
fromFile, err := dbc.ParseFile(path)
if err != nil {
t.Fatal(err)
}
assertDevice(t, fromFile, 0xA)

for name, test := range map[string]struct {
edit func(*dbc.Database)
want string
}{
"colliding identifiers": {func(db *dbc.Database) { db.Messages[1].ID = db.Messages[0].ID }, "duplicate message ID"},
"identifier beyond 29 bits": {func(db *dbc.Database) { db.Messages[0].ID |= 0xF << 28 }, "29-bit"},
"extended identifier bit 31": {func(db *dbc.Database) { db.Messages[0].ID |= 1 << 31 }, "29-bit"},
"standard identifier bit 31": {func(db *dbc.Database) {
db.Messages[0].Extended = false
db.Messages[0].ID = 0x80000001
}, "11-bit"},
"name with a space": {func(db *dbc.Database) { db.Messages[0].Name = "DEV 0 STAT" }, `"DEV 0 STAT"`},
"comment ending in a backslash": {func(db *dbc.Database) { db.Comment = `C:\logs\` }, `C:\\logs\\`},
} {
t.Run(name, func(t *testing.T) {
if _, err := base.Edit("invalid.dbc", test.edit); err == nil || !strings.Contains(err.Error(), test.want) {
t.Fatalf("Edit() error = %v, want it to mention %s", err, test.want)
}
})
}

// Every edit worked on a copy.
status, ok := base.MessageByID(0x10061003, true)
if !ok || status.Name != "DEV_0_STAT" || len(base.Messages) != 3 || base.Messages[1].ID != 0x10063001 ||
base.Comment == `C:\logs\` {
t.Fatalf("base database changed: %#v", base.Messages)
}
}
6 changes: 3 additions & 3 deletions dbc/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestParseFile(t *testing.T) {
"BU_: ECU\n" +
"BO_ 256 Status: 1 ECU\n" +
" SG_ State : 0|8@1+ (1,0) [0|255] \"Fahrzeuggr"
suffix = "e\" ECU\nCM_ \"ignored comment\";\nFILTER ignored\n"
suffix = "e\" ECU\nCM_ \"Database comment\";\nFILTER ignored\n"
)

utf8Source := append([]byte("\xef\xbb\xbf"), []byte(prefix+"öß"+suffix)...)
Expand Down Expand Up @@ -40,8 +40,8 @@ func TestParseFile(t *testing.T) {
"Windows-1252": parse(windows1252Source),
} {
t.Run(encoding, func(t *testing.T) {
if db.Version != "1.0" {
t.Fatalf("unexpected database version: %q", db.Version)
if db.Version != "1.0" || db.Comment != "Database comment" {
t.Fatalf("unexpected database header: version=%q comment=%q", db.Version, db.Comment)
}
if len(db.Nodes) != 1 || db.Nodes[0].Name != "ECU" || len(db.Messages) != 1 {
t.Fatalf("unexpected resolved model: nodes=%#v messages=%#v", db.Nodes, db.Messages)
Expand Down
27 changes: 5 additions & 22 deletions dbc/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,13 @@ func (l *lexer) quoted() (token, error) {
l.column = 1
continue
}
if character != '\\' {
value.WriteByte(character)
// \" is the only escape, as in cantools and canmatrix; other
// backslashes, such as those in Windows paths, are literal.
if character == '\\' && l.offset+1 < len(l.input) && l.input[l.offset+1] == '"' {
l.advanceByte()
continue
}

l.advanceByte()
if l.offset >= len(l.input) {
break
}
escaped := l.input[l.offset]
switch escaped {
case 'n':
value.WriteByte('\n')
case 'r':
value.WriteByte('\r')
case 't':
value.WriteByte('\t')
case '"', '\\':
value.WriteByte(escaped)
default:
value.WriteByte('\\')
value.WriteByte(escaped)
character = '"'
}
value.WriteByte(character)
l.advanceByte()
}

Expand Down
20 changes: 15 additions & 5 deletions dbc/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Package dbc parses CAN database files into a resolved semantic model and
// encodes or lazily decodes raw CAN frames.
// Package dbc parses CAN database files into a resolved semantic model,
// writes that model back as DBC text, and encodes or lazily decodes raw CAN
// frames.
package dbc

import (
Expand All @@ -13,13 +14,15 @@ import (
// remain on their definitions so callers can distinguish the two.
// Treat the database and its nested models as read-only: lookups and codecs
// trust the model that Parse resolved, and modifications are not revalidated.
// Use Edit to derive a changed database.
type Database struct {
Version string
Nodes []Node
Messages []Message
ValueTables []ValueTable
AttributeDefinitions []AttributeDefinition
Attributes map[string]AttributeValue
Comment string
Diagnostics []Diagnostic

messagesByName map[string]int
Expand All @@ -45,6 +48,7 @@ func (db *Database) Source() string { return db.source }
type Node struct {
Name string
Attributes map[string]AttributeValue
Comment string
}

// Message is one resolved BO_ definition.
Expand All @@ -57,8 +61,12 @@ type Message struct {
Transmitters []string
Signals []Signal
SignalGroups []SignalGroup
Format FrameFormat
Attributes map[string]AttributeValue
// Format is derived from VFrameFormat and ProtocolType attributes and the
// message's identifier and length. Edit those declarations to change it;
// Write does not serialize Format itself.
Format FrameFormat
Attributes map[string]AttributeValue
Comment string

bitRateSwitch bool
codec *messageCodec
Expand Down Expand Up @@ -103,6 +111,7 @@ type Signal struct {
IsMultiplexer bool
Multiplex *MultiplexCondition
Attributes map[string]AttributeValue
Comment string
}

// ByteOrder is the DBC signal byte order.
Expand Down Expand Up @@ -205,7 +214,8 @@ type Position struct {

// Diagnostic reports a record that was dropped without making the database
// unsafe to use: unsupported record types, dangling references to unknown
// objects, and the Vector independent-signal pseudo-message.
// objects, the Vector independent-signal pseudo-message, and comments replaced
// by a later comment for the same object.
type Diagnostic struct {
Position Position
Keyword string
Expand Down
Loading
Loading
0