Skip to content
Open
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
13 changes: 13 additions & 0 deletions EDSEditorGUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ void ProfileAddClick(object sender, EventArgs e)
}
}

private void AddArrayElementAttributeWarnings(EDSsharp eds)
{
// Only CANopenNode V4 stores a single attribute for all elements of an ARRAY.
// For other stacks different access of the elements is valid, so do not warn.
if ((ExporterFactory.Exporter)Properties.Settings.Default.ExporterType != ExporterFactory.Exporter.CANOPENNODE_V4)
return;

foreach (string warning in eds.VerifyArrayElementAttributes())
Warnings.AddWarning(warning, Warnings.warning_class.WARNING_BUILD);
}

private void openEDSfile(string path,InfoSection.Filetype ft)
{
Warnings.warning_list.Clear();
Expand All @@ -190,6 +201,7 @@ private void openEDSfile(string path,InfoSection.Filetype ft)

eds.Loadfile(path);
Warnings.warning_list.AddRange(eds.VerifyCompoundSubIndexZero());
AddArrayElementAttributeWarnings(eds);

DeviceView device = new DeviceView(eds, network);

Expand Down Expand Up @@ -332,6 +344,7 @@ private void openXDDfile(string path)
{
EDSsharp eds = XddLoader.Load(path);
Warnings.warning_list.AddRange(eds.VerifyCompoundSubIndexZero());
AddArrayElementAttributeWarnings(eds);
eds.projectFilename = path;

tabControl1.TabPages.Add(eds.di.ProductName);
Expand Down
105 changes: 104 additions & 1 deletion EditorRegressionTests/EditorRegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,109 @@ private static void XddSchemaLoading()
}
}

private static void SetEnum(object target, string name, string value)
{
FieldInfo field = target.GetType().GetField(name);
field.SetValue(target, Enum.Parse(field.FieldType, value));
}

private static void AddCompoundElement(object compound, ushort subindex, string datatype, string access, string defaultvalue)
{
object element = Activator.CreateInstance(compound.GetType());
compound.GetType().GetField("parent").SetValue(element, compound);
compound.GetType().GetField("parameter_name").SetValue(element, "Element " + subindex);
compound.GetType().GetField("defaultvalue").SetValue(element, defaultvalue);
SetEnum(element, "objecttype", "VAR");
SetEnum(element, "datatype", datatype);
SetEnum(element, "accesstype", access);
((IDictionary)Field(compound, "subobjects")).Add(subindex, element);
}

private static Form[] OpenWarningForms()
{
return Application.OpenForms.Cast<Form>().Where(form => form.GetType().Name == "WarningsFrm").ToArray();
}

private static void CloseWarningForms()
{
foreach (Form warning in OpenWarningForms())
{
warning.Close();
}
}

private static int CountArrayWarnings(IList warnings)
{
// Copy first: the list is static and shared with the editor
return warnings.Cast<string>().ToArray().Count(text => text.Contains("Attributes of elements in ARRAY must be equal") && text.Contains("0x2100"));
}

private static void LoadEdsInEditor(object model, string path, string folder, IList warningList)
{
object filetype = Enum.Parse(library.GetType("libEDSsharp.InfoSection+Filetype", true), "File_EDS");
Call(model, "Savefile", path, filetype);
warningList.Clear();
using (var form = (Form)Activator.CreateInstance(editor.GetType("ODEditor.ODEditor_MainForm", true)))
{
form.GetType().GetField("appdatafolder", Fields).SetValue(form, folder);
((TabControl)Field(form, "tabControl1")).TabPages.Clear();
((IList)Field(form, "network")).Clear();
// The main form constructor applies the user's warning mask
library.GetType("libEDSsharp.Warnings", true).GetField("warning_mask").SetValue(null, (uint)0xffff);
Call(form, "openEDSfile", path, filetype);
}
}

private static void ArrayElementAttributeWarning()
{
// Make the outcome independent of the user settings (not saved): exporter CANopenNode V4, all warnings enabled
object settings = editor.GetType("ODEditor.Properties.Settings", true).GetProperty("Default").GetValue(null, null);
settings.GetType().GetProperty("ExporterType").SetValue(settings, 0, null);
settings.GetType().GetProperty("WarningMask").SetValue(settings, (uint)0xffff, null);
Type warningsType = library.GetType("libEDSsharp.Warnings", true);
var warningList = (IList)warningsType.GetField("warning_list").GetValue(null);
warningsType.GetField("warning_mask").SetValue(null, (uint)0xffff);
warningList.Clear();
CloseWarningForms();

// RECORD with sub 0 = count, sub 1 write-only, sub 2 read-only
object model = Model();
var objects = (IDictionary)Field(model, "ods");
objects.Clear();
Type entryType = library.GetType("libEDSsharp.ODentry", true);
object compound = Activator.CreateInstance(entryType);
entryType.GetProperty("Index").SetValue(compound, (ushort)0x2100);
entryType.GetField("parameter_name").SetValue(compound, "Generic compound");
SetEnum(compound, "objecttype", "RECORD");
AddCompoundElement(compound, 0, "UNSIGNED8", "ro", "2");
AddCompoundElement(compound, 1, "UNSIGNED16", "wo", "0");
AddCompoundElement(compound, 2, "UNSIGNED16", "ro", "0");
objects.Add((ushort)0x2100, compound);

string folder = Path.Combine(Path.GetTempPath(), "editor-array-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(folder);
try
{
// The same elements as a RECORD are valid: no ARRAY warning
LoadEdsInEditor(model, Path.Combine(folder, "record.eds"), folder, warningList);
Require(CountArrayWarnings(warningList) == 0, "Loading a RECORD with different element access produced an ARRAY warning");
CloseWarningForms();

// The same elements as an ARRAY share one attribute in CANopenNode V4
SetEnum(compound, "objecttype", "ARRAY");
LoadEdsInEditor(model, Path.Combine(folder, "array.eds"), folder, warningList);
Require(OpenWarningForms().Length > 0, "Loading an EDS with a mixed ARRAY did not show a warning");
Require(CountArrayWarnings(warningList) == 1, "Expected exactly one ARRAY attribute warning after loading, got " + CountArrayWarnings(warningList));
CloseWarningForms();
}
finally
{
CloseWarningForms();
Directory.Delete(folder, true);
}
Console.WriteLine("PASS: loading an EDS with an ARRAY whose elements have different access reports it once, a RECORD does not (CANopenNode V4)");
}

[STAThread]
private static int Main(string[] args)
{
Expand All @@ -349,7 +452,7 @@ private static int Main(string[] args)
editor = Assembly.LoadFrom(Path.Combine(directory, "EDSEditor.exe"));
Application.EnableVisualStyles();
int failed = 0;
foreach (Action test in new Action[] { PendingDeviceInfoSave, SparsePdoNumbers, AdditionalMetadata, StorageGroupSelection, MappingInvalidationWarning, RecordCreationAndDocumentClose, XddSchemaLoading })
foreach (Action test in new Action[] { PendingDeviceInfoSave, SparsePdoNumbers, AdditionalMetadata, StorageGroupSelection, MappingInvalidationWarning, RecordCreationAndDocumentClose, XddSchemaLoading, ArrayElementAttributeWarning })
{
try
{
Expand Down
150 changes: 150 additions & 0 deletions Tests/ArrayElementAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.IO;
using System.Linq;
using Xunit;
using libEDSsharp;

namespace Tests
{
// Warnings.warning_list is static, so the tests that read it must not run in parallel with each other
[Collection("Warnings")]
public class ArrayElementAttributeTests
{
private const string ArrayName = "Mixed array";
private const string WarningText = "Attributes of elements in ARRAY must be equal";

/// <summary>
/// Build a model with one object 0x2000 and the given element accesses (sub-index 1, 2, ...).
/// Sub-index 0 is the element count.
/// </summary>
private static EDSsharp BuildModel(ObjectType type, params EDSsharp.AccessType[] elementAccess)
{
var model = new EDSsharp();
model.ods.Clear();
var obj = new ODentry
{
Index = 0x2000,
parameter_name = ArrayName,
objecttype = type,
datatype = DataType.UNSIGNED16
};
obj.subobjects.Add(0, new ODentry
{
parent = obj,
parameter_name = "Number of elements",
datatype = DataType.UNSIGNED8,
accesstype = EDSsharp.AccessType.ro,
defaultvalue = elementAccess.Length.ToString()
});
for (ushort i = 0; i < elementAccess.Length; i++)
{
obj.subobjects.Add((ushort)(i + 1), new ODentry
{
parent = obj,
parameter_name = "Element " + (i + 1),
datatype = DataType.UNSIGNED16,
accesstype = elementAccess[i],
defaultvalue = "0"
});
}
model.ods.Add(obj.Index, obj);
return model;
}

private static EDSsharp BuildMixedArray()
{
return BuildModel(ObjectType.ARRAY, EDSsharp.AccessType.wo, EDSsharp.AccessType.ro, EDSsharp.AccessType.rw);
}

[Fact]
public void UniformArrayHasNoWarning()
{
var model = BuildModel(ObjectType.ARRAY, EDSsharp.AccessType.rw, EDSsharp.AccessType.rw, EDSsharp.AccessType.rw);
Assert.Empty(model.VerifyArrayElementAttributes());
}

[Fact]
public void MixedArrayReportsOneMessageWithDetails()
{
var warnings = BuildMixedArray().VerifyArrayElementAttributes();

string message = Assert.Single(warnings);
Assert.Equal(
"Error in 0x2000 \"Mixed array\": Attributes of elements in ARRAY must be equal, " +
"but sub-indexes 0x02, 0x03 differ from sub-index 0x01. " +
"CANopenNode stores one attribute for all elements of an ARRAY, so the attribute of sub-index 0x01 is used for all of them. " +
"Make the access of all elements equal or change the object type to RECORD.",
message);
Assert.DoesNotContain("\n", message);
}

[Fact]
public void MixedRecordHasNoWarning()
{
var model = BuildModel(ObjectType.RECORD, EDSsharp.AccessType.wo, EDSsharp.AccessType.ro, EDSsharp.AccessType.rw);
Assert.Empty(model.VerifyArrayElementAttributes());
}

[Fact]
public void DisabledMixedArrayHasNoWarning()
{
var model = BuildMixedArray();
model.ods[0x2000].prop.CO_disabled = true;
Assert.Empty(model.VerifyArrayElementAttributes());
}

[Fact]
public void ReadOnlyAndConstElementsAreEqual()
{
// Both are exported as ODA_SDO_R
var model = BuildModel(ObjectType.ARRAY, EDSsharp.AccessType.ro, EDSsharp.AccessType.@const, EDSsharp.AccessType.ro);
Assert.Empty(model.VerifyArrayElementAttributes());
}

[Fact]
public void DifferentPdoMappingIsReported()
{
var model = BuildModel(ObjectType.ARRAY, EDSsharp.AccessType.rw, EDSsharp.AccessType.rw, EDSsharp.AccessType.rw);
model.ods[0x2000].subobjects[1].PDOtype = PDOMappingType.optional;
model.ods[0x2000].subobjects[2].PDOtype = PDOMappingType.optional;
model.ods[0x2000].subobjects[3].PDOtype = PDOMappingType.no;

string message = Assert.Single(model.VerifyArrayElementAttributes());
Assert.Contains("sub-index 0x03 differs from sub-index 0x01", message);
}

[Fact]
public void DifferentSrdoAccessIsReported()
{
var model = BuildModel(ObjectType.ARRAY, EDSsharp.AccessType.rw, EDSsharp.AccessType.rw);
model.ods[0x2000].subobjects[2].prop.CO_accessSRDO = AccessSRDO.rx;

string message = Assert.Single(model.VerifyArrayElementAttributes());
Assert.Contains("sub-index 0x02 differs from sub-index 0x01", message);
}

[Fact]
public void V4ExporterReportsMixedArrayOnce()
{
string folder = Path.Combine(Path.GetTempPath(), "ArrayElementAttributeTests_" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(folder);
try
{
Warnings.warning_list.Clear();
new CanOpenNodeExporter_V4().export(Path.Combine(folder, "OD.c"), BuildMixedArray());

// One warning for the whole ARRAY, not one per differing element.
// Copy the list first: tests outside the collection may still add to it.
var warnings = Warnings.warning_list.ToArray().Where(warning => warning.Contains(WarningText)).ToList();
string message = Assert.Single(warnings);
Assert.Contains(ArrayName, message);
Assert.Contains("0x2000", message);
Assert.Contains("sub-indexes 0x02, 0x03 differ from sub-index 0x01", message);
}
finally
{
Directory.Delete(folder, true);
}
}
}
}
2 changes: 2 additions & 0 deletions Tests/ExporterTestsV4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Tests
{
// The exporter adds to the static Warnings.warning_list, which other tests also read
[Collection("Warnings")]
public class ExporterTestsV4 : CanOpenNodeExporter_V4
{
readonly EDSsharp _eds;
Expand Down
2 changes: 2 additions & 0 deletions Tests/IssueRegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Tests
{
// Kept API-neutral so the same issue contracts can run against CANopenEditor upstream.
// The V4 exporter adds to the static Warnings.warning_list, which other tests also read
[Collection("Warnings")]
public class IssueRegressionTests : CanOpenNodeExporter
{
private static EDSsharp Blank()
Expand Down
8 changes: 7 additions & 1 deletion libEDSsharp/CanOpenNodeExporter_V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ private int Prepare_arr(ODentry od, string indexH, string varName, string group)
string attrElem = "";
List<string> ODStorageValues = new List<string>();
UInt16 i = 0;
UInt16 firstElemSubIndex = 0;
List<UInt16> differingAttrSubIndexes = new List<UInt16>();
foreach (ODentry sub in od.subobjects.Values)
{
// If sub datatype is not known, use the od datatype
Expand All @@ -288,6 +290,7 @@ private int Prepare_arr(ODentry od, string indexH, string varName, string group)
// First array element. Other array elements must match this elements in data type and attributes
dataElem = data;
attrElem = attr;
firstElemSubIndex = sub.Subindex;
}
else
{// Following checks are requirement of CANopenNode. Arrays must be C arrays.
Expand All @@ -296,12 +299,15 @@ private int Prepare_arr(ODentry od, string indexH, string varName, string group)
if ((data.cValue == null && dataElem.cValue != null) || (data.cValue != null && dataElem.cValue == null))
Warnings.AddWarning($"Error in 0x{indexH}: Default value must be defined on all ARRAY elements or must be undefined on all ARRAY elements!", Warnings.warning_class.WARNING_BUILD);
if (attr != attrElem)
Warnings.AddWarning($"Error in 0x{indexH}: Attributes of elements in ARRAY must be equal", Warnings.warning_class.WARNING_BUILD);
differingAttrSubIndexes.Add(sub.Subindex);
}
ODStorageValues.Add(data.cValue);
}
i++;
}
// Report all differing elements in one warning, the attribute of the first element is used for all
if (differingAttrSubIndexes.Count > 0)
Warnings.AddWarning(EDSsharp.ArrayElementAttributesWarning(od, firstElemSubIndex, differingAttrSubIndexes), Warnings.warning_class.WARNING_BUILD);
string dataPtr0 = "NULL";
string dataPtr = "NULL";
if (cValue0 != null)
Expand Down
Loading