Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Sep 25, 2024
1 parent 4e9782d commit f2cee92
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 13 additions & 3 deletions RDMSharp/Metadata/JSON/OneOfTypes/BitFieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public BitFieldType(string name,
ValueForUnspecified = valueForUnspecified;
Bits = bits;
}
public BitFieldType(string name, ushort size, BitType[] bits) : this(name, null, null, null, "bitField", size, null, bits)
public BitFieldType(string name, ushort size, BitType[] bits, bool valueForUnspecified = false) : this(name, null, null, null, "bitField", size, valueForUnspecified, bits)
{
}

Expand All @@ -91,9 +91,9 @@ public override byte[] ParsePayloadToData(DataTree dataTree)
throw new ArithmeticException($"The given {nameof(dataTree.Children)}.{nameof(dataTree.Children.Length)}({dataTree.Children.Length}) not match {nameof(Bits)}.{nameof(Bits.Length)}({Bits.Length})");

bool[] data = new bool[Size];
if (ValueForUnspecified.HasValue)
if (ValueForUnspecified == true)
for (int i = 0; i < Size; i++)
data[i] = ValueForUnspecified.Value;
data[i] = true;

foreach (DataTree bitDataTree in dataTree.Children)
{
Expand Down Expand Up @@ -128,6 +128,16 @@ public override DataTree ParseDataToPayload(ref byte[] data)
BitType bitType = Bits[i];
bitDataTrees.Add(new DataTree(bitType.Name, i, bools[bitType.Index]));
}
bool valueForUnspecified = ValueForUnspecified == true;
for(int i = 0; i < bools.Length; i++)
{
if (Bits.Any(b => b.Index == i))
continue;

bool bit= bools[i];
if (bit != valueForUnspecified)
issueList.Add(new DataTreeIssue($"The Bit at Index {i} is Unspecified, but the Value is not {valueForUnspecified} as defined for Unspecified Bits"));
}
return new DataTree(this.Name, 0, children: bitDataTrees.OrderBy(b=>b.Index).ToArray(), issueList.Count != 0 ? issueList.ToArray() : null);
}
}
Expand Down
5 changes: 5 additions & 0 deletions RDMSharpTests/Metadata/JSON/TestBitFieldType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public void TestMany()
var dataTree= new DataTree(bitFieldType.Name,0,new DataTree[] { new DataTree(bitTypes[0].Name, 0, true), new DataTree(bitTypes[1].Name, 1, true), new DataTree(bitTypes[2].Name, 2, true), });

DoParseDataTest(bitFieldType, dataTree, new byte[] { 0b00010100, 0b10000000 });

bitFieldType = new BitFieldType("NAME_BIT_FIELD", 16, bitTypes, true);
dataTree = new DataTree(bitFieldType.Name, 0, new DataTree[] { new DataTree(bitTypes[0].Name, 0, false), new DataTree(bitTypes[1].Name, 1, false), new DataTree(bitTypes[2].Name, 2, false), });
Assert.That(bitFieldType.ValueForUnspecified, Is.True);
DoParseDataTest(bitFieldType, dataTree, new byte[] { 0b11101011, 0b01111111 });
}
private void DoParseDataTest(BitFieldType bitFieldType, DataTree dataTree, byte[] expectedData, string message = null)
{
Expand Down

0 comments on commit f2cee92

Please sign in to comment.