Skip to content

Commit

Permalink
Merge pull request #80 from EFeru/global-properties
Browse files Browse the repository at this point in the history
Added global properties to DBC class
  • Loading branch information
Adhara3 authored Sep 2, 2024
2 parents da13835 + 6a7011f commit edc092a
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 7 deletions.
126 changes: 126 additions & 0 deletions DbcFiles/ext_multiplexed.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
VERSION ""


NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
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_:


BO_ 2348875518 ExtMX_Message: 5 Vector__XXX
SG_ S7 : 36|4@1+ (1,0) [0|0] "" Vector__XXX
SG_ S5 m6 : 4|24@1- (1,0) [0|0] "" Vector__XXX
SG_ S4 m5 : 4|24@1- (1,0) [0|0] "" Vector__XXX
SG_ S3 m4 : 20|16@1- (1,0) [0|0] "" Vector__XXX
SG_ S2 m4 : 4|16@1- (1,0) [0|0] "" Vector__XXX
SG_ S6 m1 : 2|32@1- (1,0) [0|0] "" Vector__XXX
SG_ S1_m m0M : 2|2@1+ (1,0) [0|0] "" Vector__XXX
SG_ S0_m M : 0|2@1+ (1,0) [0|0] "" Vector__XXX


BA_DEF_ SG_ "SigType" ENUM "Default","Range","RangeSigned","ASCII","Discrete","Control","ReferencePGN","DTC","StringDelimiter","StringLength","StringLengthControl","MessageCounter","MessageChecksum";
BA_DEF_ SG_ "GenSigEVName" STRING ;
BA_DEF_ SG_ "GenSigILSupport" ENUM "No","Yes";
BA_DEF_ SG_ "GenSigSendType" ENUM "Cyclic","OnWrite","OnWriteWithRepetition","OnChange","OnChangeWithRepetition","IfActive","IfActiveWithRepetition","NoSigSendType";
BA_DEF_ BO_ "GenMsgFastOnStart" INT 0 100000;
BA_DEF_ SG_ "GenSigInactiveValue" INT 0 0;
BA_DEF_ BO_ "GenMsgCycleTimeFast" INT 0 3600000;
BA_DEF_ BO_ "GenMsgNrOfRepetition" INT 0 1000000;
BA_DEF_ SG_ "GenSigStartValue" INT 0 2147483647;
BA_DEF_ BO_ "GenMsgDelayTime" INT 0 1000;
BA_DEF_ BO_ "GenMsgILSupport" ENUM "No","Yes";
BA_DEF_ BO_ "GenMsgStartDelayTime" INT 0 100000;
BA_DEF_ BU_ "NodeLayerModules" STRING ;
BA_DEF_ BU_ "ECU" STRING ;
BA_DEF_ BU_ "NmJ1939SystemInstance" INT 0 15;
BA_DEF_ BU_ "NmJ1939System" INT 0 127;
BA_DEF_ BU_ "NmJ1939ManufacturerCode" INT 0 2047;
BA_DEF_ BU_ "NmJ1939IndustryGroup" INT 0 7;
BA_DEF_ BU_ "NmJ1939IdentityNumber" INT 0 2097151;
BA_DEF_ BU_ "NmJ1939FunctionInstance" INT 0 7;
BA_DEF_ BU_ "NmJ1939Function" INT 0 255;
BA_DEF_ BU_ "NmJ1939ECUInstance" INT 0 3;
BA_DEF_ BU_ "NmJ1939AAC" INT 0 1;
BA_DEF_ BU_ "NmStationAddress" INT 0 255;
BA_DEF_ BO_ "GenMsgSendType" ENUM "cyclic","NotUsed","IfActive","NotUsed","NotUsed","NotUsed","NotUsed","NotUsed","noMsgSendType";
BA_DEF_ BO_ "GenMsgRequestable" INT 0 1;
BA_DEF_ BO_ "GenMsgCycleTime" INT 0 3600000;
BA_DEF_ SG_ "SPN" INT 0 524287;
BA_DEF_ "DBName" STRING ;
BA_DEF_ "BusType" STRING ;
BA_DEF_ "ProtocolType" STRING ;
BA_DEF_ BO_ "VFrameFormat" ENUM "StandardCAN","ExtendedCAN","reserved","J1939PG";
BA_DEF_ EV_ "Age" INT 0 100;
BA_DEF_DEF_ "SigType" "Default";
BA_DEF_DEF_ "GenSigEVName" "Env@Nodename_@Signame";
BA_DEF_DEF_ "GenSigILSupport" "Yes";
BA_DEF_DEF_ "GenSigSendType" "NoSigSendType";
BA_DEF_DEF_ "GenMsgFastOnStart" 0;
BA_DEF_DEF_ "GenSigInactiveValue" 0;
BA_DEF_DEF_ "GenMsgCycleTimeFast" 0;
BA_DEF_DEF_ "GenMsgNrOfRepetition" 0;
BA_DEF_DEF_ "GenSigStartValue" 0;
BA_DEF_DEF_ "GenMsgDelayTime" 0;
BA_DEF_DEF_ "GenMsgILSupport" "Yes";
BA_DEF_DEF_ "GenMsgStartDelayTime" 0;
BA_DEF_DEF_ "NodeLayerModules" "";
BA_DEF_DEF_ "ECU" "";
BA_DEF_DEF_ "NmJ1939SystemInstance" 0;
BA_DEF_DEF_ "NmJ1939System" 0;
BA_DEF_DEF_ "NmJ1939ManufacturerCode" 0;
BA_DEF_DEF_ "NmJ1939IndustryGroup" 0;
BA_DEF_DEF_ "NmJ1939IdentityNumber" 0;
BA_DEF_DEF_ "NmJ1939FunctionInstance" 0;
BA_DEF_DEF_ "NmJ1939Function" 0;
BA_DEF_DEF_ "NmJ1939ECUInstance" 0;
BA_DEF_DEF_ "NmJ1939AAC" 0;
BA_DEF_DEF_ "NmStationAddress" 254;
BA_DEF_DEF_ "GenMsgSendType" "noMsgSendType";
BA_DEF_DEF_ "GenMsgRequestable" 1;
BA_DEF_DEF_ "GenMsgCycleTime" 0;
BA_DEF_DEF_ "SPN" 0;
BA_DEF_DEF_ "DBName" "";
BA_DEF_DEF_ "BusType" "CAN";
BA_DEF_DEF_ "ProtocolType" "J1939";
BA_DEF_DEF_ "VFrameFormat" "StandardCAN";
BA_DEF_DEF_ "Age" 42;

BA_ "DBName" "z_mx";
BA_ "VFrameFormat" BO_ 2348875518 3;
SIG_VALTYPE_ 2348875518 S6 : 1;

SG_MUL_VAL_ 2348875518 S5 S1_m 6-6;
SG_MUL_VAL_ 2348875518 S4 S1_m 5-5;
SG_MUL_VAL_ 2348875518 S3 S1_m 4-4;
SG_MUL_VAL_ 2348875518 S2 S1_m 4-4;
SG_MUL_VAL_ 2348875518 S6 S0_m 1-1;
SG_MUL_VAL_ 2348875518 S1_m S0_m 0-0, 2-2;
1 change: 1 addition & 0 deletions DbcParserLib.Tests/NodeLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void FullLineIsParsed()

[TestCase("BU_: 0nodeName")]
[TestCase("BU_:nodeName")]
[TestCase("BU_:")]
public void NodeSyntaxErrorIsObserved(string line)
{
var observerMock = m_repository.Create<IParseFailureObserver>();
Expand Down
47 changes: 47 additions & 0 deletions DbcParserLib.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,52 @@ public void MultipleEnvironmentVariableToOneNodeTest()
Assert.That(dbc.Nodes.First().EnvironmentVariables.Last().Key, Is.EqualTo("EnvVarName3"));
Assert.That(dbc.Nodes.First().EnvironmentVariables.Last().Value.Type, Is.EqualTo(EnvDataType.Data));
}

[Test]
public void CheckGlobalPropertiesTest()
{
var file = @"..\..\..\..\DbcFiles\ext_multiplexed.dbc";
var dbc = Parser.ParseFromPath(file);

Assert.That(dbc.GlobalProperties.Count(), Is.EqualTo(3));

var dbName = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("DBName"));
Assert.That(dbName, Is.Not.Null);
Assert.That(dbName.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String));
Assert.That(dbName.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null);
Assert.That(dbName.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo(string.Empty));
Assert.That(dbName.StringCustomProperty, Is.Not.Null);
Assert.That(dbName.StringCustomProperty.Value, Is.EqualTo("z_mx"));

var busType = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("BusType"));
Assert.That(busType, Is.Not.Null);
Assert.That(busType.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String));
Assert.That(busType.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null);
Assert.That(busType.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo("CAN"));
Assert.That(busType.StringCustomProperty, Is.Not.Null);
Assert.That(busType.StringCustomProperty.Value, Is.EqualTo("CAN"));

var protocolType = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("ProtocolType"));
Assert.That(protocolType, Is.Not.Null);
Assert.That(protocolType.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String));
Assert.That(protocolType.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null);
Assert.That(protocolType.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo("J1939"));
Assert.That(protocolType.StringCustomProperty, Is.Not.Null);
Assert.That(protocolType.StringCustomProperty.Value, Is.EqualTo("J1939"));


var targetMessage = dbc.Messages.FirstOrDefault(x => x.ID == 201391870); // Extended ID
Assert.That(targetMessage, Is.Not.Null);
Assert.That(targetMessage.CustomProperties, Has.Count.EqualTo(10));
Assert.That(targetMessage.CustomProperties["VFrameFormat"].EnumCustomProperty.Value, Is.EqualTo("J1939PG"));

Assert.That(targetMessage.Signals.Count, Is.EqualTo(8));

var floatSignal = targetMessage.Signals.FirstOrDefault(x => x.Name.Equals("S6"));
Assert.That(floatSignal, Is.Not.Null);
Assert.That(floatSignal.ValueType, Is.EqualTo(DbcValueType.IEEEFloat)); // Set with a property

// Should check the extended multiplexing stuff once implemented
}
}
}
29 changes: 29 additions & 0 deletions DbcParserLib.Tests/PropertiesLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ public void StringDefinitionCustomPropertyIsParsedTest()
Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider), Is.True);
}

[Test]
public void StringDefinitionCustomPropertyOnEnvironmentVariableIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());

var customPropertyLineParsers = CreateParser();
var nextLineProvider = new NextLineProvider(new StringReader(string.Empty));
Assert.That(ParseLine(@"BA_DEF_ EV_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True);
Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider), Is.True);
}

[Test]
public void StringDefinitionCustomPropertyAsGlobalIsParsedTest()
{
var builder = new DbcBuilder(new SilentFailureObserver());

var customPropertyLineParsers = CreateParser();
var nextLineProvider = new NextLineProvider(new StringReader(string.Empty));
Assert.That(ParseLine(@"BA_DEF_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True);
Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider), Is.True);

var dbc = builder.Build();

var globalProperty = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("AttributeName"));
Assert.That(globalProperty, Is.Not.Null);
Assert.That(globalProperty.StringCustomProperty, Is.Not.Null);
Assert.That(globalProperty.StringCustomProperty.Value, Is.EqualTo("DefaultString"));
}

[Test]
public void EnumDefinitionCustomPropertyIsParsedTest()
{
Expand Down
5 changes: 4 additions & 1 deletion DbcParserLib/Dbc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ public class Dbc
public IEnumerable<Node> Nodes {get;}
public IEnumerable<Message> Messages {get;}
public IEnumerable<EnvironmentVariable> EnvironmentVariables { get; }
public IEnumerable<CustomProperty> GlobalProperties { get; }

public Dbc(IEnumerable<Node> nodes, IEnumerable<Message> messages, IEnumerable<EnvironmentVariable> environmentVariables)
public Dbc(IEnumerable<Node> nodes, IEnumerable<Message> messages, IEnumerable<EnvironmentVariable> environmentVariables,
IEnumerable<CustomProperty> globalProperties)
{
Nodes = nodes;
Messages = messages;
EnvironmentVariables = environmentVariables;
GlobalProperties = globalProperties;
}
}
}
70 changes: 66 additions & 4 deletions DbcParserLib/DbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ internal class DbcBuilder : IDbcBuilder
private readonly IParseFailureObserver m_observer;

private readonly ISet<Node> m_nodes = new HashSet<Node>(new NodeEqualityComparer());

private readonly IDictionary<uint, Message> m_messages = new Dictionary<uint, Message>();
private readonly IDictionary<uint, IDictionary<string, Signal>> m_signals = new Dictionary<uint, IDictionary<string, Signal>>();
private readonly IDictionary<string, EnvironmentVariable> m_environmentVariables = new Dictionary<string, EnvironmentVariable>();
private readonly IDictionary<string, CustomProperty> m_globalCustomProperties = new Dictionary<string, CustomProperty>();

private readonly IDictionary<string, ValuesTable> m_namedTablesMap = new Dictionary<string, ValuesTable>();
private readonly IDictionary<CustomPropertyObjectType, IDictionary<string, CustomPropertyDefinition>> m_customProperties = new Dictionary<CustomPropertyObjectType, IDictionary<string, CustomPropertyDefinition>>() {
{CustomPropertyObjectType.Node, new Dictionary<string, CustomPropertyDefinition>()},
{CustomPropertyObjectType.Message, new Dictionary<string, CustomPropertyDefinition>()},
{CustomPropertyObjectType.Signal, new Dictionary<string, CustomPropertyDefinition>()},
{CustomPropertyObjectType.Environment, new Dictionary<string, CustomPropertyDefinition>()},
{CustomPropertyObjectType.Global, new Dictionary<string, CustomPropertyDefinition>()},
};

private Message m_currentMessage;
Expand All @@ -35,10 +39,8 @@ public DbcBuilder(IParseFailureObserver observer)

public void AddNode(Node node)
{
if(m_nodes.Contains(node))
if(m_nodes.Add(node) == false)
m_observer.DuplicatedNode(node.Name);
else
m_nodes.Add(node);
}

public void AddMessage(Message message)
Expand Down Expand Up @@ -117,6 +119,23 @@ public void AddNodeCustomProperty(string propertyName, string nodeName, string v
m_observer.PropertyNameNotFound(propertyName);
}

public void AddGlobalCustomProperty(string propertyName, string value, bool isNumeric)
{
if(m_customProperties[CustomPropertyObjectType.Global].TryGetValue(propertyName, out var customPropertyDefinition))
{
var property = new CustomProperty(customPropertyDefinition);
if(!property.SetCustomPropertyValue(value, isNumeric))
return;

if(m_globalCustomProperties.TryGetValue(propertyName, out _))
m_observer.DuplicatedGlobalProperty(propertyName);
else
m_globalCustomProperties[propertyName] = property;
}
else
m_observer.PropertyNameNotFound(propertyName);
}

public void AddEnvironmentVariableCustomProperty(string propertyName, string variableName, string value, bool isNumeric)
{
if (m_customProperties[CustomPropertyObjectType.Environment].TryGetValue(propertyName, out var customProperty))
Expand Down Expand Up @@ -332,6 +351,47 @@ private void FillNodesNotSetCustomPropertyWithDefault()
}
}

private void FillEnvironmentVariablesNotSetCustomPropertyWithDefault()
{
var environmentCustomProperties = m_customProperties[CustomPropertyObjectType.Environment];
foreach (var customProperty in environmentCustomProperties)
{
foreach (var envVariable in m_environmentVariables.Values)
{
if (!envVariable.CustomProperties.TryGetValue(customProperty.Key, out _))
{
envVariable.CustomProperties[customProperty.Key] = new CustomProperty(customProperty.Value);
envVariable.CustomProperties[customProperty.Key].SetCustomPropertyValueFromDefault();
}
}

foreach (var node in m_nodes)
{
foreach (var envVariable in node.EnvironmentVariables.Values)
{
if (envVariable.CustomProperties.TryGetValue(customProperty.Key, out _) == false)
{
envVariable.CustomProperties[customProperty.Key] = new CustomProperty(customProperty.Value);
envVariable.CustomProperties[customProperty.Key].SetCustomPropertyValueFromDefault();
}
}
}
}
}

private void FillGlobalCustomPropertiesNotSetCustomPropertyWithDefault()
{
var globalCustomProperties = m_customProperties[CustomPropertyObjectType.Global];
foreach (var customPropertyPair in globalCustomProperties)
{
if (!m_globalCustomProperties.TryGetValue(customPropertyPair.Key, out _))
{
m_globalCustomProperties[customPropertyPair.Key] = new CustomProperty(customPropertyPair.Value);
m_globalCustomProperties[customPropertyPair.Key].SetCustomPropertyValueFromDefault();
}
}
}

private void FillMessagesNotSetCustomPropertyWithDefault()
{
var messageCustomProperties = m_customProperties[CustomPropertyObjectType.Message];
Expand Down Expand Up @@ -369,6 +429,8 @@ public Dbc Build()
{
FillNodesNotSetCustomPropertyWithDefault();
FillMessagesNotSetCustomPropertyWithDefault();
FillEnvironmentVariablesNotSetCustomPropertyWithDefault();
FillGlobalCustomPropertiesNotSetCustomPropertyWithDefault();

foreach (var message in m_messages)
{
Expand Down Expand Up @@ -399,7 +461,7 @@ public Dbc Build()
//}
//return new Dbc(nodes, messages, environmentVariables);

return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray());
return new Dbc(m_nodes.ToArray(), m_messages.Values.ToArray(), m_environmentVariables.Values.ToArray(), m_globalCustomProperties.Values);
}
}

Expand Down
1 change: 1 addition & 0 deletions DbcParserLib/IDbcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal interface IDbcBuilder
void AddCustomProperty(CustomPropertyObjectType objectType, CustomPropertyDefinition customProperty);
void AddCustomPropertyDefaultValue(string propertyName, string value, bool isNumeric);
void AddNodeCustomProperty(string propertyName, string nodeName, string value, bool isNumeric);
void AddGlobalCustomProperty(string propertyName, string value, bool isNumeric);
void AddEnvironmentVariableCustomProperty(string propertyName, string variableName, string value, bool isNumeric);
void AddMessageCustomProperty(string propertyName, uint messageId, string value, bool isNumeric);
void AddSignalCustomProperty(string propertyName, uint messageId, string signalName, string value, bool isNumeric);
Expand Down
2 changes: 1 addition & 1 deletion DbcParserLib/Model/CustomPropertyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public class EnumCustomPropertyDefinition

public enum CustomPropertyObjectType
{
Node, Message, Signal, Environment
Global, Node, Message, Signal, Environment
}

public enum CustomPropertyDataType
Expand Down
1 change: 1 addition & 0 deletions DbcParserLib/Observers/IParseFailureObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IParseFailureObserver
void DuplicatedProperty(string propertyName);
void DuplicatedPropertyInNode(string propertyName, string nodeName);
void DuplicatedPropertyInEnvironmentVariable(string propertyName, string environmentVariableName);
void DuplicatedGlobalProperty(string propertyName);
void DuplicatedPropertyInMessage(string propertyName, uint messageId);
void DuplicatedPropertyInSignal(string propertyName, string signalName);
void DuplicatedEnvironmentVariableInNode(string environmentVariableName, string nodeName);
Expand Down
Loading

0 comments on commit edc092a

Please sign in to comment.