From 1307a5cdf39ab7eb0622243644a978cff5dbaabd Mon Sep 17 00:00:00 2001 From: NobseVomBerg <168748760+NobseVomBerg@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:48:45 +0100 Subject: [PATCH] FEAT: Warn about ARRAY elements with differing attributes in V4 export CANopenNode V4 stores one attribute set (OD_obj_array_t) for all elements of an ARRAY, so the exporter writes the attributes of the first element for every element. The existing warning was one terse line per differing element and did not say which attribute was used. - Add EDSsharp.VerifyArrayElementAttributes(), which compares SDO, PDO and SRDO access of ARRAY elements and returns one message per ARRAY. - The V4 exporter now reports one warning per ARRAY. It gives the index, the object name, the differing sub-indexes, the sub-index whose attributes are used, and what to do (equal access or RECORD). - Show the same warning when an EDS or XDD file is opened, if the selected exporter is CANopenNode V4. Nothing changes when the legacy exporter is selected. - Add unit tests and a GUI regression test. ExporterTestsV4, IssueRegressionTests and the new ArrayElementAttributeTests share the "Warnings" xUnit collection, because Warnings.warning_list is static. Co-Authored-By: Claude Fable 5.2 --- EDSEditorGUI/Form1.cs | 13 ++ .../EditorRegressionTests.cs | 105 +++++++++++- Tests/ArrayElementAttributeTests.cs | 150 ++++++++++++++++++ Tests/ExporterTestsV4.cs | 2 + Tests/IssueRegressionTests.cs | 2 + libEDSsharp/CanOpenNodeExporter_V4.cs | 8 +- libEDSsharp/eds.cs | 58 +++++++ 7 files changed, 336 insertions(+), 2 deletions(-) create mode 100644 Tests/ArrayElementAttributeTests.cs diff --git a/EDSEditorGUI/Form1.cs b/EDSEditorGUI/Form1.cs index 8aae707a..edef9f57 100644 --- a/EDSEditorGUI/Form1.cs +++ b/EDSEditorGUI/Form1.cs @@ -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(); @@ -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); @@ -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); diff --git a/EditorRegressionTests/EditorRegressionTests.cs b/EditorRegressionTests/EditorRegressionTests.cs index 18b00d11..520ed314 100644 --- a/EditorRegressionTests/EditorRegressionTests.cs +++ b/EditorRegressionTests/EditorRegressionTests.cs @@ -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
().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().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) { @@ -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 { diff --git a/Tests/ArrayElementAttributeTests.cs b/Tests/ArrayElementAttributeTests.cs new file mode 100644 index 00000000..419108d9 --- /dev/null +++ b/Tests/ArrayElementAttributeTests.cs @@ -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"; + + /// + /// Build a model with one object 0x2000 and the given element accesses (sub-index 1, 2, ...). + /// Sub-index 0 is the element count. + /// + 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); + } + } + } +} diff --git a/Tests/ExporterTestsV4.cs b/Tests/ExporterTestsV4.cs index a1a45a7c..b664cc9a 100644 --- a/Tests/ExporterTestsV4.cs +++ b/Tests/ExporterTestsV4.cs @@ -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; diff --git a/Tests/IssueRegressionTests.cs b/Tests/IssueRegressionTests.cs index 5b123865..3dbcc051 100644 --- a/Tests/IssueRegressionTests.cs +++ b/Tests/IssueRegressionTests.cs @@ -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() diff --git a/libEDSsharp/CanOpenNodeExporter_V4.cs b/libEDSsharp/CanOpenNodeExporter_V4.cs index 8a523412..1882b431 100644 --- a/libEDSsharp/CanOpenNodeExporter_V4.cs +++ b/libEDSsharp/CanOpenNodeExporter_V4.cs @@ -262,6 +262,8 @@ private int Prepare_arr(ODentry od, string indexH, string varName, string group) string attrElem = ""; List ODStorageValues = new List(); UInt16 i = 0; + UInt16 firstElemSubIndex = 0; + List differingAttrSubIndexes = new List(); foreach (ODentry sub in od.subobjects.Values) { // If sub datatype is not known, use the od datatype @@ -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. @@ -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) diff --git a/libEDSsharp/eds.cs b/libEDSsharp/eds.cs index e1191472..b149b453 100644 --- a/libEDSsharp/eds.cs +++ b/libEDSsharp/eds.cs @@ -1954,6 +1954,64 @@ public List VerifyPDOMapping() return mappingErrors; } + /// + /// Build the warning text for an ARRAY whose elements do not all have the same attributes. + /// + /// The ARRAY object. + /// Sub-index of the first element, whose attributes are used for all elements. + /// Sub-indexes of the elements that differ from the first element. + /// A single-line warning message. + internal static string ArrayElementAttributesWarning(ODentry array, ushort firstElementSubIndex, IEnumerable differingSubIndexes) + { + var differingList = differingSubIndexes.Select(subIndex => $"0x{subIndex:X2}").ToList(); + string differing = string.Join(", ", differingList); + string first = $"0x{firstElementSubIndex:X2}"; + return $"Error in 0x{array.Index:X4} \"{array.parameter_name}\": Attributes of elements in ARRAY must be equal, " + + (differingList.Count == 1 ? $"but sub-index {differing} differs" : $"but sub-indexes {differing} differ") + $" from sub-index {first}. " + + $"CANopenNode stores one attribute for all elements of an ARRAY, so the attribute of sub-index {first} is used for all of them. " + + "Make the access of all elements equal or change the object type to RECORD."; + } + + /// + /// Find ARRAY objects whose elements do not all have the same attributes. + /// CANopenNode V4 stores a single attribute for all elements of an ARRAY (OD_obj_array_t), + /// so the attributes of the first element are used for all of them on export. + /// Only the attributes the V4 exporter writes are compared (SDO access, PDO access and SRDO access); + /// the data type is not part of this check. + /// + /// List of warning strings, one per affected ARRAY, empty if nothing found. + public List VerifyArrayElementAttributes() + { + var warnings = new List(); + foreach (ODentry entry in ods.Values) + { + if (entry.prop.CO_disabled || entry.objecttype != ObjectType.ARRAY) + continue; + + // Sub-index 0 holds the element count; the elements start at sub-index 1. + // The subobjects are sorted by sub-index. + var elements = entry.subobjects.Where(kvp => kvp.Key > 0).ToList(); + if (elements.Count < 2) + continue; + + ODentry first = elements[0].Value; + var differing = new List(); + foreach (var element in elements.Skip(1)) + { + if (element.Value.AccessSDO() != first.AccessSDO() || + element.Value.AccessPDO() != first.AccessPDO() || + element.Value.prop.CO_accessSRDO != first.prop.CO_accessSRDO) + { + differing.Add(element.Key); + } + } + + if (differing.Count > 0) + warnings.Add(ArrayElementAttributesWarning(entry, elements[0].Key, differing)); + } + return warnings; + } + public DataType Getdatatype(ODentry od) {