forked from einride/can-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
57 lines (51 loc) · 1.09 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package descriptor
import (
"path"
"strings"
)
// Database represents a CAN database.
type Database struct {
// SourceFile of the database.
//
// Example:
// github.com/einride/can-databases/dbc/j1939.dbc
SourceFile string
// Version of the database.
Version string
// Messages in the database.
Messages []*Message
// Nodes in the database.
Nodes []*Node
}
func (d *Database) Node(nodeName string) (*Node, bool) {
for _, n := range d.Nodes {
if n.Name == nodeName {
return n, true
}
}
return nil, false
}
func (d *Database) Message(id uint32) (*Message, bool) {
for _, m := range d.Messages {
if m.ID == id {
return m, true
}
}
return nil, false
}
func (d *Database) Signal(messageID uint32, signalName string) (*Signal, bool) {
message, ok := d.Message(messageID)
if !ok {
return nil, false
}
for _, s := range message.Signals {
if s.Name == signalName {
return s, true
}
}
return nil, false
}
// Description returns the name of the Database.
func (d *Database) Name() string {
return strings.TrimSuffix(path.Base(d.SourceFile), path.Ext(d.SourceFile))
}