Skip to content

Commit

Permalink
added custom attributes MsgCycleTime, MsgSendType, SigSendType
Browse files Browse the repository at this point in the history
  • Loading branch information
FerroO2000 committed Jan 12, 2024
1 parent 8743fcc commit 2a96f58
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 57 deletions.
53 changes: 28 additions & 25 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.3.7"
const version = "v0.4.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down
13 changes: 10 additions & 3 deletions examples/multiplexed_signal.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ CM_ BO_ 2024 "desc OBD2";
CM_ SG_ 2024 Service "desc Service";
CM_ SG_ 2024 MultiplexedSignalName "desc S2";
CM_ SG_ 2024 ExtendedMuxSignalName "desc S1";
CM_ SG_ 2024 VehicleSpeed "desc VehicleSpeed (send_type: Cyclic)";
CM_ SG_ 2024 ThrottlePosition "desc ThrottlePosition";
CM_ SG_ 2024 VehicleSpeed "desc VehicleSpeed";

BA_DEF_ BO_ "MsgPeriodMS" INT 0 65535;
BA_DEF_ SG_ "TestFloatAtt" FLOAT 0 25.75;
BA_DEF_ BO_ "GenMsgCycleTime" INT 0 1000;
BA_DEF_ BO_ "GenMsgSendType" ENUM "NoMsgSendType","Cyclic","IfActive","cyclicIfActive","NotUsed";
BA_DEF_ SG_ "SPN" INT 0 524287;
BA_DEF_ SG_ "TestFloatAtt" FLOAT 0 25.75;
BA_DEF_ SG_ "GenSigSendType" ENUM "NoSigSendType","Cyclic","OnWrite","OnWriteWithRepetition","OnChange","OnChangeWithRepetition","IfActive","IfActiveWithRepetition","NotUsed";
BA_DEF_DEF_ "MsgPeriodMS" 0;
BA_DEF_DEF_ "GenMsgCycleTime" 0;
BA_DEF_DEF_ "GenMsgSendType" "NoMsgSendType";
BA_DEF_DEF_ "SPN" 0;
BA_DEF_DEF_ "TestFloatAtt" 1.5;
BA_ "TestFloatAtt" SG_ 2024 VehicleSpeed 10.5;
BA_DEF_DEF_ "GenSigSendType" "NoSigSendType";
BA_ "SPN" SG_ 2024 VehicleSpeed 190;
BA_ "TestFloatAtt" SG_ 2024 VehicleSpeed 10.5;
BA_ "GenSigSendType" SG_ 2024 VehicleSpeed 1;

SG_MUL_VAL_ 2024 MultiplexedSignalName Service 2-2;
SG_MUL_VAL_ 2024 VehicleSpeed ExtendedMuxSignalName 13-13;
Expand Down
1 change: 1 addition & 0 deletions examples/multiplexed_signal.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"TestFloatAtt": 10.5
},
"description": "desc VehicleSpeed",
"send_type": "Cyclic",
"mux_switch": 13,
"start_bit": 31,
"size": 8,
Expand Down
13 changes: 11 additions & 2 deletions examples/simple.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,37 @@ BO_ 2364540158 EEC1: 8 Vector__XXX
CM_ "Baudrate: 1000000";
CM_ BU_ Node1 "Node1 desc";
CM_ BU_ Node2 "Node2 desc";
CM_ BO_ 2364540158 "desc 0 (period: 100 ms)";
CM_ BO_ 2364540158 "desc 0 (period: 100 ms) (cycle_time: 10) (send_type: IfActive)";
CM_ SG_ 2364540158 Engine_Speed "desc 1";
CM_ SG_ 2364540158 Engine_Status "desc 2";
CM_ SG_ 2364540158 Engine_Status "desc 2 (send_type: Cyclic)";

BA_DEF_ "FloatAtt" FLOAT 0 25.75;
BA_DEF_ "Baudrate" INT 0 1000000;
BA_DEF_ BU_ "TestString" STRING ;
BA_DEF_ BO_ "GenMsgCycleTime" INT 0 1000;
BA_DEF_ BO_ "GenMsgSendType" ENUM "NoMsgSendType","Cyclic","IfActive","cyclicIfActive","NotUsed";
BA_DEF_ BO_ "VFrameFormat" ENUM "StandardCAN","ExtendedCAN","reserved","J1939PG";
BA_DEF_ BO_ "MsgPeriodMS" INT 0 65535;
BA_DEF_ SG_ "SPN" INT 0 524287;
BA_DEF_ SG_ "TestFloatAtt" FLOAT 0 25.75;
BA_DEF_ SG_ "GenSigSendType" ENUM "NoSigSendType","Cyclic","OnWrite","OnWriteWithRepetition","OnChange","OnChangeWithRepetition","IfActive","IfActiveWithRepetition","NotUsed";
BA_DEF_DEF_ "FloatAtt" 1.5;
BA_DEF_DEF_ "Baudrate" 1000000;
BA_DEF_DEF_ "TestString" "";
BA_DEF_DEF_ "GenMsgSendType" "NoMsgSendType";
BA_DEF_DEF_ "VFrameFormat" "J1939PG";
BA_DEF_DEF_ "MsgPeriodMS" 0;
BA_DEF_DEF_ "GenMsgCycleTime" 0;
BA_DEF_DEF_ "SPN" 0;
BA_DEF_DEF_ "TestFloatAtt" 1.5;
BA_DEF_DEF_ "GenSigSendType" "NoSigSendType";
BA_ "TestString" BU_ Node2 "test";
BA_ "VFrameFormat" BO_ 2364540158 0;
BA_ "MsgPeriodMS" BO_ 2364540158 100;
BA_ "GenMsgCycleTime" BO_ 2364540158 10;
BA_ "GenMsgSendType" BO_ 2364540158 2;
BA_ "SPN" SG_ 2364540158 Engine_Speed 190;
BA_ "TestFloatAtt" SG_ 2364540158 Engine_Speed 10.5;
BA_ "GenSigSendType" SG_ 2364540158 Engine_Status 1;

VAL_ 2364540158 Engine_Status 0 "Off" 1 "Idle" 2 "Running";
3 changes: 3 additions & 0 deletions examples/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"EEC1": {
"id": 2364540158,
"period_ms": 100,
"cycle_time": 10,
"send_type": "IfActive",
"description": "desc 0",
"attributes": {
"VFrameFormat": "StandardCAN"
Expand Down Expand Up @@ -87,6 +89,7 @@
},
"Engine_Status": {
"description": "desc 2",
"send_type": "Cyclic",
"start_bit": 0,
"size": 2,
"endianness": "little",
Expand Down
70 changes: 69 additions & 1 deletion pkg/can_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import (
"github.com/squadracorsepolito/jsondbc/pkg/sym"
)

type sourceType int

const (
sourceTypeJSON sourceType = iota
sourceTypeDBC
)

type Reader interface {
Read(file *os.File) (*CanModel, error)
}
Expand All @@ -26,6 +33,8 @@ type CanModel struct {
MessageAttributes map[string]*MessageAttribute `json:"message_attributes"`
SignalAttributes map[string]*SignalAttribute `json:"signal_attributes"`
Messages map[string]*Message `json:"messages"`

source sourceType
}

func (c *CanModel) Init() {
Expand Down Expand Up @@ -72,6 +81,17 @@ func (c *CanModel) Init() {
},
}
}

if c.MessageAttributes == nil {
c.MessageAttributes = make(map[string]*MessageAttribute)
}

if c.SignalAttributes == nil {
c.SignalAttributes = make(map[string]*SignalAttribute)
}

c.handleCustomAttributes()

for attName, att := range c.MessageAttributes {
att.initMessageAttribute(attName)
}
Expand All @@ -85,7 +105,7 @@ func (c *CanModel) Init() {
}

for msgName, msg := range c.Messages {
msg.initMessage(msgName)
msg.initMessage(msgName, c.source)
}

for _, node := range c.Nodes {
Expand All @@ -112,6 +132,54 @@ func (c *CanModel) Init() {
}
}

func (c *CanModel) handleCustomAttributes() {
switch c.source {
case sourceTypeJSON:
// MsgCycleTime
c.MessageAttributes[sym.MsgCycleTime] = &MessageAttribute{
Attribute: &Attribute{
Int: &AttributeInt{0, 0, 1000},
},
}

// MsgSendType
c.MessageAttributes[sym.MsgSendType] = &MessageAttribute{
Attribute: &Attribute{
Enum: &AttributeEnum{
Default: sym.MsgSendTypeValues[0],
Values: sym.MsgSendTypeValues,
},
},
}

// SigSendType
c.SignalAttributes[sym.SigSendType] = &SignalAttribute{
Attribute: &Attribute{
Enum: &AttributeEnum{
Default: sym.SigSendTypeValues[0],
Values: sym.SigSendTypeValues,
},
},
}

case sourceTypeDBC:
// MsgCycleTime
if _, ok := c.MessageAttributes[sym.MsgCycleTime]; ok {
delete(c.MessageAttributes, sym.MsgCycleTime)
}

// MsgSendType
if _, ok := c.MessageAttributes[sym.MsgSendType]; ok {
delete(c.MessageAttributes, sym.MsgSendType)
}

// SigSendType
if _, ok := c.SignalAttributes[sym.SigSendType]; ok {
delete(c.SignalAttributes, sym.SigSendType)
}
}
}

// Validate validates the CAN model.
func (c *CanModel) Validate() error {
msgIDMap := make(map[uint32]string)
Expand Down
2 changes: 2 additions & 0 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ func (r *JsonReader) Read(file *os.File) (*CanModel, error) {
return nil, err
}

canModel.source = sourceTypeJSON

return canModel, nil
}
66 changes: 57 additions & 9 deletions pkg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@ import (
// Message represents a CAN message.
type Message struct {
*AttributeAssignments
ID uint32 `json:"id"`
Description string `json:"description,omitempty"`
Period uint32 `json:"period_ms,omitempty"`
Length uint32 `json:"length"`
Sender string `json:"sender,omitempty"`
Signals map[string]*Signal `json:"signals"`
ID uint32 `json:"id"`
Description string `json:"description,omitempty"`

// Custom attributes
CycleTime int `json:"cycle_time,omitempty"`
SendType string `json:"send_type,omitempty"`

Period uint32 `json:"period_ms,omitempty"`
Length uint32 `json:"length"`
Sender string `json:"sender,omitempty"`
Signals map[string]*Signal `json:"signals"`

messageName string
childSignals map[string]*Signal
fromDBC bool
source sourceType
}

func (m *Message) initSignalRec(sigName string, sig *Signal) {
sig.initSignal(sigName)
sig.initSignal(sigName, m.source)
m.childSignals[sigName] = sig
sig.isMultiplexed = true
if !sig.isMultiplexor {
Expand All @@ -36,8 +42,9 @@ func (m *Message) initSignalRec(sigName string, sig *Signal) {
}
}

func (m *Message) initMessage(msgName string) {
func (m *Message) initMessage(msgName string, source sourceType) {
m.messageName = msgName
m.source = source

if m.AttributeAssignments == nil {
m.AttributeAssignments = &AttributeAssignments{
Expand All @@ -60,10 +67,12 @@ func (m *Message) initMessage(msgName string) {
delete(m.AttributeAssignments.Attributes, sym.MsgPeriodAttribute)
}

m.handleCustomAttributes()

m.childSignals = make(map[string]*Signal)

for sigName, sig := range m.Signals {
sig.initSignal(sigName)
sig.initSignal(sigName, m.source)
m.childSignals[sigName] = sig
if !sig.isMultiplexor {
continue
Expand All @@ -76,6 +85,45 @@ func (m *Message) initMessage(msgName string) {

}

func (m *Message) appendDescription(format string, a ...any) {
m.Description = appendString(m.Description, format, a...)
}

func (m *Message) handleCustomAttributes() {
location := fmt.Sprintf("signal '%s'", m.messageName)

switch m.source {
case sourceTypeJSON:
// MsgCycleTime
if m.CycleTime > 0 {
m.AttributeAssignments.Attributes[sym.MsgCycleTime] = float64(m.CycleTime)
m.appendDescription("(cycle_time: %d)", m.CycleTime)
}

// MsgSendType
if len(m.SendType) > 0 {
tmpST := checkCustomEnumAttribute(m.SendType, "message.send_type", sym.MsgSendTypeValues, location)
m.AttributeAssignments.Attributes[sym.MsgSendType] = tmpST
m.appendDescription("(send_type: %s)", tmpST)
}

case sourceTypeDBC:
// MsgCycleTime
ctAtt, hasCT := m.AttributeAssignments.Attributes[sym.MsgCycleTime]
if hasCT {
m.CycleTime = ctAtt.(int)
delete(m.AttributeAssignments.Attributes, sym.MsgCycleTime)
}

// MsgSendType
stAtt, hasST := m.AttributeAssignments.Attributes[sym.MsgSendType]
if hasST {
m.SendType = checkCustomEnumAttribute(stAtt.(string), sym.MsgSendType, sym.MsgSendTypeValues, location)
delete(m.AttributeAssignments.Attributes, sym.MsgSendType)
}
}
}

// HasDescription returns true if the message has a description.
func (m *Message) HasDescription() bool {
return len(m.Description) > 0
Expand Down

0 comments on commit 2a96f58

Please sign in to comment.